Hi guys,
I’m fairly new to working with shaders but having good results so far. I’m working on a tile based game and I need to find a way to perform an action in the vertex shader based on what type of tile is being rendered. See shader below:
attribute vec3 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoord;
varying float blockLight;
void main()
{
v_color = a_color;
v_texCoord = a_texCoord0;
gl_Position = u_projTrans * vec4(a_position, 1.0);
if(a_position.y > 15)
{
blockLight = a_position.y * 0.03;
if(blockLight > 0.98) blockLight = 0.98;
}
else blockLight = 0;
}
I want the tiles to begin getting darker the deeper they get but following the terrain (I am using a Y-down coordinate system) instead of just starting at level 15. Since the terrain is varying in height, how can I change this to only set the blockLight value if the tile we are working on is a solid block or certain type of block? LibGDX does provide a way to set vertex attributes but the documentation is lacking and I don’t use the shader in my world generation code, only in the world renderer.
If it helps this is my fragment shader:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoord;
varying float blockLight;
uniform sampler2D u_texture;
uniform sampler2D u_lightmap;
uniform vec4 ambientColor;
uniform vec2 resolution;
void main()
{
vec4 diffuseColor = texture2D(u_texture, v_texCoord);
vec2 lightCoord = (gl_FragCoord.xy / resolution.xy);
vec4 light = texture2D(u_lightmap, lightCoord);
vec3 ambient = ambientColor.rgb * (ambientColor.a - blockLight);
vec3 intensity = ambient + light.rgb;
vec3 finalColor = diffuseColor.rgb * intensity;
gl_FragColor = v_color * vec4(finalColor, diffuseColor.a);
}
Finally a screenshot showing the effect so far:
https://dl.dropboxusercontent.com/u/39277637/screenshot.png
Any help would be greatly appreciated.