Code: Select all
void main(void)
{
vec3 blend_weights = (abs(vNormal) -0.2)*7.0;
blend_weights = max(blend_weights, 0.0);
blend_weights /= blend_weights.x + blend_weights.y + blend_weights.z;
vec2 coord1 = vPosition.yz * uTexturesScale.x;
vec2 coord2 = vPosition.zx * uTexturesScale.y;
vec2 coord3 = vPosition.xy * uTexturesScale.z;
vec3 colX = texture2D(uTexX, coord1).xyz;
vec3 colY = texture2D(uTexY, coord2).xyz;
vec3 colZ = texture2D(uTexZ, coord3).xyz;
vec3 plainColor = colX * blend_weights.x +
colY * blend_weights.y +
colZ * blend_weights.z;
// Tangent space normal mapping
vec3 tangent = vec3(1, 0, 0);
vec3 binormal = normalize(cross(tangent, vNormal));
tangent = normalize(cross(vNormal, binormal));
mat3 rotMatrix = transpose(mat3(tangent, binormal, vNormal));
vec3 normalX = (texture2D(uTexXNormal, coord1).xyz - 0.5)*2.0;
vec3 normalY = (texture2D(uTexYNormal, coord2).xyz - 0.5)*2.0;
vec3 normalZ = (texture2D(uTexZNormal, coord3).xyz - 0.5)*2.0;
vec3 normal = normalX.xyz * blend_weights.x +
normalY.xyz * blend_weights.y +
normalZ.xyz * blend_weights.z;
// Lighting
vec3 lightDir = rotMatrix * normalize(uLightPosition_-vPosition);
vec3 viewDir = rotMatrix * normalize(uCameraPosition-vPosition);
float nDotL = dot(normal, lightDir);
// Diffuse
vec3 diffuseColor = uLightColor * clamp(nDotL, 0.0, 1.0);
// Specular
vec3 reflected = normalize(2.0 * nDotL * normal - lightDir); // Reflected vector
float dotProduct = clamp(dot(reflected, viewDir), 0.0, 1.0);
vec3 specularColor = uLightColor * max(pow(dotProduct, uShininess), 0.0);
// Linear attenuation
float attenuation = uAttenuationDistance/(uAttenuationDistance+vLightDistance);
vec3 finalColor = plainColor*(uAmbientColor + attenuation*diffuseColor) + attenuation*specularColor;
gl_FragColor = vec4(finalColor, 1.0);
}
P.D.: I've the HLSL version too, if needed!