Masterchoc

Untitled

Dec 13th, 2017
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. precision highp float;
  2.  
  3. varying vec2 fragTexCoords;
  4. varying vec3 fragNormal;
  5. varying vec3 fragToLight[8];
  6. varying vec3 fragToCamera;
  7. varying float visibility;
  8.  
  9. uniform sampler2D diffuseTexture;
  10. uniform sampler2D normalTexture;
  11. uniform sampler2D specularTexture;
  12.  
  13. uniform vec3 lightColor[8];
  14. uniform vec3 lightAttenuation[8];
  15. uniform vec3 clearColor;
  16. uniform float ambientLight;
  17. uniform float specularFactor;
  18. uniform float specularPower;
  19. uniform vec2 tiling;
  20. uniform float hasNormalMap;
  21. uniform float hasSpecularMap;
  22. uniform float fading;
  23.  
  24. void main() {
  25. vec3 normal;
  26. vec4 normalMapValue;
  27. vec4 texel = texture2D(diffuseTexture, fragTexCoords * tiling);
  28.  
  29. if(texel.a < 0.6666)
  30. discard;
  31.  
  32. if(hasNormalMap == 1.0) {
  33. normalMapValue = 2.0 * texture2D(normalTexture, fragTexCoords) - 1.0;
  34. normal = normalize(normalMapValue.rgb);
  35. }
  36. else
  37. normal = normalize(fragNormal);
  38.  
  39. vec3 toCamera = normalize(fragToCamera);
  40.  
  41. vec3 totalDiffuse = vec3(0.0, 0.0, 0.0);
  42. vec3 totalSpecular = vec3(0.0, 0.0, 0.0);
  43.  
  44. for(int i = 0; i < 8; i++) {
  45. float distance = length(fragToLight[i]);
  46. float attenuationFactor = lightAttenuation[i].x + (lightAttenuation[i].y * distance) + (lightAttenuation[i].z * distance * distance);
  47.  
  48. vec3 toLight = normalize(fragToLight[i]);
  49. float nDotL = dot(normal, toLight);
  50. float brightness = max(nDotL, 0.0);
  51. totalDiffuse += (brightness * lightColor[i]) / attenuationFactor;
  52.  
  53. vec3 lightDirection = -toLight;
  54. vec3 reflectedLight = reflect(lightDirection, normal);
  55.  
  56. float factor = dot(reflectedLight, toCamera);
  57. factor = max(factor, 0.0);
  58. float power = pow(factor, specularFactor);
  59. totalSpecular += (power * specularPower * lightColor[i]) / attenuationFactor;
  60.  
  61. }
  62.  
  63. totalDiffuse = max(totalDiffuse, ambientLight);
  64.  
  65. if(hasSpecularMap == 1.0) {
  66. vec4 specularData = texture2D(specularTexture, fragTexCoords);
  67. totalSpecular *= specularData.r;
  68. }
  69.  
  70. gl_FragColor = vec4(totalDiffuse, 1.0) * texel + vec4(totalSpecular, 1.0);
  71. gl_FragColor = mix(vec4(clearColor, 0.5), gl_FragColor, visibility);
  72. }
RAW Paste Data