getSpecular.glsl 1.2 KB

1234567891011121314151617181920212223242526272829
  1. /**
  2. * Calculates the specular intensity of reflected light.
  3. *
  4. * @name czm_getSpecular
  5. * @glslFunction
  6. *
  7. * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates.
  8. * @param {vec3} toEyeEC Unit vector pointing to the eye position in eye coordinates.
  9. * @param {vec3} normalEC The surface normal in eye coordinates.
  10. * @param {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight.
  11. *
  12. * @returns {float} The intensity of the specular highlight.
  13. *
  14. * @see czm_phong
  15. *
  16. * @example
  17. * float diffuseIntensity = czm_getLambertDiffuse(lightDirectionEC, normalEC);
  18. * float specularIntensity = czm_getSpecular(lightDirectionEC, toEyeEC, normalEC, 200);
  19. * vec3 color = (diffuseColor * diffuseIntensity) + (specularColor * specularIntensity);
  20. */
  21. float czm_getSpecular(vec3 lightDirectionEC, vec3 toEyeEC, vec3 normalEC, float shininess)
  22. {
  23. vec3 toReflectedLight = reflect(-lightDirectionEC, normalEC);
  24. float specular = max(dot(toReflectedLight, toEyeEC), 0.0);
  25. // pow has undefined behavior if both parameters <= 0.
  26. // Prevent this by making sure shininess is at least czm_epsilon2.
  27. return pow(specular, max(shininess, czm_epsilon2));
  28. }