phong.glsl 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. float czm_private_getLambertDiffuseOfMaterial(vec3 lightDirectionEC, czm_material material)
  2. {
  3. return czm_getLambertDiffuse(lightDirectionEC, material.normal);
  4. }
  5. float czm_private_getSpecularOfMaterial(vec3 lightDirectionEC, vec3 toEyeEC, czm_material material)
  6. {
  7. return czm_getSpecular(lightDirectionEC, toEyeEC, material.normal, material.shininess);
  8. }
  9. /**
  10. * Computes a color using the Phong lighting model.
  11. *
  12. * @name czm_phong
  13. * @glslFunction
  14. *
  15. * @param {vec3} toEye A normalized vector from the fragment to the eye in eye coordinates.
  16. * @param {czm_material} material The fragment's material.
  17. *
  18. * @returns {vec4} The computed color.
  19. *
  20. * @example
  21. * vec3 positionToEyeEC = // ...
  22. * czm_material material = // ...
  23. * gl_FragColor = czm_phong(normalize(positionToEyeEC), material);
  24. *
  25. * @see czm_getMaterial
  26. */
  27. vec4 czm_phong(vec3 toEye, czm_material material)
  28. {
  29. // Diffuse from directional light sources at eye (for top-down)
  30. float diffuse = czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material);
  31. if (czm_sceneMode == czm_sceneMode3D) {
  32. // (and horizon views in 3D)
  33. diffuse += czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material);
  34. }
  35. // Specular from sun and pseudo-moon
  36. float specular = czm_private_getSpecularOfMaterial(czm_sunDirectionEC, toEye, material) + czm_private_getSpecularOfMaterial(czm_moonDirectionEC, toEye, material);
  37. // Temporary workaround for adding ambient.
  38. vec3 materialDiffuse = material.diffuse * 0.5;
  39. vec3 ambient = materialDiffuse;
  40. vec3 color = ambient + material.emission;
  41. color += materialDiffuse * diffuse;
  42. color += material.specular * specular;
  43. #ifdef HDR
  44. float sunDiffuse = czm_private_getLambertDiffuseOfMaterial(czm_sunDirectionEC, material);
  45. color += materialDiffuse * sunDiffuse * czm_sunColor;
  46. #endif
  47. return vec4(color, material.alpha);
  48. }
  49. vec4 czm_private_phong(vec3 toEye, czm_material material)
  50. {
  51. float diffuse = czm_private_getLambertDiffuseOfMaterial(czm_sunDirectionEC, material);
  52. float specular = czm_private_getSpecularOfMaterial(czm_sunDirectionEC, toEye, material);
  53. vec3 ambient = vec3(0.0);
  54. vec3 color = ambient + material.emission;
  55. color += material.diffuse * diffuse;
  56. color += material.specular * specular;
  57. return vec4(color, material.alpha);
  58. }