// Parallax Bumpmapping shader
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform vec4 lightColor;
uniform vec2 plxCoeffs;
uniform bool hasParallax;
varying vec2 texCoord;
varying vec3 lVec;
varying vec3 vVec;
float saturate( float inValue)
{
return clamp(inValue, 0.0, 1.0);
}
void main(){
// Calculate light intensity
float atten = saturate(1.0 - dot(lVec, lVec));
vec3 lightVec = normalize(lVec);
vec3 viewVec = normalize(vVec);
// Calculate parallax texcoords
vec2 plxTexCoord = texCoord;
if (hasParallax) {
float height = texture2D(tex1, texCoord).w;
float offset = height * plxCoeffs.x + plxCoeffs.y;
plxTexCoord += offset * viewVec.xy;
}
vec4 base = texture2D(tex0, plxTexCoord);
vec3 bump = texture2D(tex1, plxTexCoord).xyz * 2.0 - 1.0;
bump = normalize(bump);
float diffuse = saturate(dot(lightVec, bump));
vec4 lighting = diffuse * base;
float specular = saturate(4.0 * dot(reflect(-viewVec, bump), lightVec) - 3.0);
specular *= specular;
lighting += 0.6 * specular;
gl_FragColor = lightColor * lighting * atten;
}