Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- precision highp float;
- varying vec2 fragTexCoords;
- varying vec3 fragNormal;
- varying vec3 fragToLight[8];
- varying vec3 fragToCamera;
- varying float visibility;
- uniform sampler2D diffuseTexture;
- uniform sampler2D normalTexture;
- uniform sampler2D specularTexture;
- uniform vec3 lightColor[8];
- uniform vec3 lightAttenuation[8];
- uniform vec3 clearColor;
- uniform float ambientLight;
- uniform float specularFactor;
- uniform float specularPower;
- uniform vec2 tiling;
- uniform float hasNormalMap;
- uniform float hasSpecularMap;
- uniform float fading;
- void main() {
- vec3 normal;
- vec4 normalMapValue;
- vec4 texel = texture2D(diffuseTexture, fragTexCoords * tiling);
- if(texel.a < 0.6666)
- discard;
- if(hasNormalMap == 1.0) {
- normalMapValue = 2.0 * texture2D(normalTexture, fragTexCoords) - 1.0;
- normal = normalize(normalMapValue.rgb);
- }
- else
- normal = normalize(fragNormal);
- vec3 toCamera = normalize(fragToCamera);
- vec3 totalDiffuse = vec3(0.0, 0.0, 0.0);
- vec3 totalSpecular = vec3(0.0, 0.0, 0.0);
- for(int i = 0; i < 8; i++) {
- float distance = length(fragToLight[i]);
- float attenuationFactor = lightAttenuation[i].x + (lightAttenuation[i].y * distance) + (lightAttenuation[i].z * distance * distance);
- vec3 toLight = normalize(fragToLight[i]);
- float nDotL = dot(normal, toLight);
- float brightness = max(nDotL, 0.0);
- totalDiffuse += (brightness * lightColor[i]) / attenuationFactor;
- vec3 lightDirection = -toLight;
- vec3 reflectedLight = reflect(lightDirection, normal);
- float factor = dot(reflectedLight, toCamera);
- factor = max(factor, 0.0);
- float power = pow(factor, specularFactor);
- totalSpecular += (power * specularPower * lightColor[i]) / attenuationFactor;
- }
- totalDiffuse = max(totalDiffuse, ambientLight);
- if(hasSpecularMap == 1.0) {
- vec4 specularData = texture2D(specularTexture, fragTexCoords);
- totalSpecular *= specularData.r;
- }
- gl_FragColor = vec4(totalDiffuse, 1.0) * texel + vec4(totalSpecular, 1.0);
- gl_FragColor = mix(vec4(clearColor, 0.5), gl_FragColor, visibility);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement