phong.glsl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * vec3 lightDirectionEC = // ...
  24. * out_FragColor = czm_phong(normalize(positionToEyeEC), material, lightDirectionEC);
  25. *
  26. * @see czm_getMaterial
  27. */
  28. vec4 czm_phong(vec3 toEye, czm_material material, vec3 lightDirectionEC)
  29. {
  30. // Diffuse from directional light sources at eye (for top-down)
  31. float diffuse = czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material);
  32. if (czm_sceneMode == czm_sceneMode3D) {
  33. // (and horizon views in 3D)
  34. diffuse += czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material);
  35. }
  36. float specular = czm_private_getSpecularOfMaterial(lightDirectionEC, 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 * czm_lightColor;
  42. color += material.specular * specular * czm_lightColor;
  43. return vec4(color, material.alpha);
  44. }
  45. vec4 czm_private_phong(vec3 toEye, czm_material material, vec3 lightDirectionEC)
  46. {
  47. float diffuse = czm_private_getLambertDiffuseOfMaterial(lightDirectionEC, material);
  48. float specular = czm_private_getSpecularOfMaterial(lightDirectionEC, toEye, material);
  49. vec3 ambient = vec3(0.0);
  50. vec3 color = ambient + material.emission;
  51. color += material.diffuse * diffuse * czm_lightColor;
  52. color += material.specular * specular * czm_lightColor;
  53. return vec4(color, material.alpha);
  54. }