diffuse-specular.fsh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifdef OPENGL_ES
  2. precision highp float;
  3. #endif
  4. // Uniforms
  5. uniform vec3 u_lightColor; // Light color
  6. uniform vec3 u_ambientColor; // Ambient color
  7. uniform float u_specularExponent; // Specular exponent or shininess property.
  8. uniform sampler2D u_diffuseTexture; // Diffuse texture.
  9. // Inputs
  10. varying vec3 v_normalVector; // NormalVector in view space.
  11. varying vec2 v_texCoord; // Texture coordinate (u, v).
  12. varying vec3 v_cameraDirection; // Camera direction
  13. // Global variables
  14. vec4 _baseColor; // Base color
  15. vec3 _ambientColor; // Ambient Color
  16. vec3 _diffuseColor; // Diffuse Color
  17. vec3 _specularColor; // Specular color
  18. void lighting(vec3 normalVector, vec3 cameraDirection, vec3 lightDirection, float attenuation)
  19. {
  20. // Ambient
  21. _ambientColor = _baseColor.rgb * u_ambientColor;
  22. // Diffuse
  23. float diffuseIntensity = attenuation * max(0.0, dot(normalVector, lightDirection));
  24. diffuseIntensity = max(0.0, diffuseIntensity);
  25. _diffuseColor = u_lightColor * _baseColor.rgb * diffuseIntensity;
  26. // Specular
  27. vec3 halfVector = normalize(cameraDirection + lightDirection);
  28. float specularIntensity = attenuation * max(0.0, pow(dot(normalVector, halfVector), u_specularExponent));
  29. specularIntensity = max(0.0, specularIntensity);
  30. _specularColor = u_lightColor * _baseColor.rgb * specularIntensity;
  31. }
  32. #if defined(POINT_LIGHT)
  33. varying vec4 v_vertexToPointLightDirection; // Light direction w.r.t current vertex.
  34. void applyLight()
  35. {
  36. // Normalize the vectors.
  37. vec3 normalVector = normalize(v_normalVector);
  38. vec3 cameraDirection = normalize(v_cameraDirection);
  39. vec3 vertexToPointLightDirection = normalize(v_vertexToPointLightDirection.xyz);
  40. // Fetch point light attenuation.
  41. float pointLightAttenuation = v_vertexToPointLightDirection.w;
  42. lighting(normalVector, cameraDirection, vertexToPointLightDirection, pointLightAttenuation);
  43. }
  44. #elif defined(SPOT_LIGHT)
  45. uniform vec3 u_spotLightDirection; // Direction of the spot light.
  46. uniform float u_spotLightInnerAngleCos; // The bright spot [0.0 - 1.0]
  47. uniform float u_spotLightOuterAngleCos; // The soft outer part [0.0 - 1.0]
  48. varying vec3 v_vertexToSpotLightDirection; // Light direction w.r.t current vertex.
  49. varying float v_spotLightAttenuation; // Attenuation of spot light.
  50. float lerpstep( float lower, float upper, float s)
  51. {
  52. return clamp( ( s - lower ) / ( upper - lower ), 0.0, 1.0 );
  53. }
  54. void applyLight()
  55. {
  56. // Normalize the vectors.
  57. vec3 normalVector = normalize(v_normalVector);
  58. vec3 cameraDirection = normalize(v_cameraDirection);
  59. vec3 spotLightDirection = normalize(u_spotLightDirection);
  60. vec3 vertexToSpotLightDirection = normalize(v_vertexToSpotLightDirection);
  61. // "-lightDirection" is used because light direction points in opposite direction to
  62. // to spot direction.
  63. // Calculate spot light effect.
  64. float spotCurrentAngleCos = max(0.0, dot(spotLightDirection, -vertexToSpotLightDirection));
  65. // Intensity of spot depends on the spot light attenuation and the
  66. // part of the cone vertexToSpotLightDirection points to (inner or outer).
  67. float spotLightAttenuation = clamp(v_spotLightAttenuation, 0.0, 1.0);
  68. spotLightAttenuation *= lerpstep(u_spotLightOuterAngleCos, u_spotLightInnerAngleCos, spotCurrentAngleCos);
  69. lighting(normalVector, cameraDirection, vertexToSpotLightDirection, spotLightAttenuation);
  70. }
  71. #else
  72. uniform vec3 u_lightDirection; // Light direction
  73. void applyLight()
  74. {
  75. // Normalize the vectors.
  76. vec3 normalVector = normalize(v_normalVector);
  77. vec3 cameraDirection = normalize(v_cameraDirection);
  78. vec3 lightDirection = normalize(u_lightDirection);
  79. lighting(normalVector, cameraDirection, -lightDirection, 1.0);
  80. }
  81. #endif
  82. void main()
  83. {
  84. // Fetch diffuse color from texture.
  85. _baseColor = texture2D(u_diffuseTexture, v_texCoord);
  86. // Apply light
  87. applyLight();
  88. // Light the pixel
  89. gl_FragColor.a = _baseColor.a;
  90. gl_FragColor.rgb = _ambientColor + _diffuseColor + _specularColor;
  91. }