sphericalHarmonics.glsl 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Computes a color from the third order spherical harmonic coefficients and a normalized direction vector.
  3. * <p>
  4. * The order of the coefficients is [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22].
  5. * </p>
  6. *
  7. * @name czm_sphericalHarmonics
  8. * @glslFunction
  9. *
  10. * @param {vec3} normal The normalized direction.
  11. * @param {vec3[9]} coefficients The third order spherical harmonic coefficients.
  12. * @returns {vec3} The color at the direction.
  13. *
  14. * @see https://graphics.stanford.edu/papers/envmap/envmap.pdf
  15. */
  16. vec3 czm_sphericalHarmonics(vec3 normal, vec3 coefficients[9])
  17. {
  18. const float c1 = 0.429043;
  19. const float c2 = 0.511664;
  20. const float c3 = 0.743125;
  21. const float c4 = 0.886227;
  22. const float c5 = 0.247708;
  23. vec3 L00 = coefficients[0];
  24. vec3 L1_1 = coefficients[1];
  25. vec3 L10 = coefficients[2];
  26. vec3 L11 = coefficients[3];
  27. vec3 L2_2 = coefficients[4];
  28. vec3 L2_1 = coefficients[5];
  29. vec3 L20 = coefficients[6];
  30. vec3 L21 = coefficients[7];
  31. vec3 L22 = coefficients[8];
  32. float x = normal.x;
  33. float y = normal.y;
  34. float z = normal.z;
  35. return c1 * L22 * (x * x - y * y) + c3 * L20 * z * z + c4 * L00 - c5 * L20 +
  36. 2.0 * c1 * (L2_2 * x * y + L21 * x * z + L2_1 * y * z) +
  37. 2.0 * c2 * (L11 * x + L1_1 * y + L10 * z);
  38. }