diffuse.fsh 3.5 KB

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