harmonicsFunctions.fx 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #ifdef USESPHERICALFROMREFLECTIONMAP
  2. #ifdef SPHERICAL_HARMONICS
  3. uniform vec3 vSphericalL00;
  4. uniform vec3 vSphericalL1_1;
  5. uniform vec3 vSphericalL10;
  6. uniform vec3 vSphericalL11;
  7. uniform vec3 vSphericalL2_2;
  8. uniform vec3 vSphericalL2_1;
  9. uniform vec3 vSphericalL20;
  10. uniform vec3 vSphericalL21;
  11. uniform vec3 vSphericalL22;
  12. // Please note the the coefficient have been prescaled.
  13. //
  14. // This uses the theory from both Sloan and Ramamoothi:
  15. // https://www.ppsloan.org/publications/SHJCGT.pdf
  16. // http://www-graphics.stanford.edu/papers/envmap/
  17. // The only difference is the integration of the reconstruction coefficients direcly
  18. // into the vectors as well as the 1 / pi multiplication to simulate a lambertian diffuse.
  19. vec3 computeEnvironmentIrradiance(vec3 normal) {
  20. return vSphericalL00
  21. + vSphericalL1_1 * (normal.y)
  22. + vSphericalL10 * (normal.z)
  23. + vSphericalL11 * (normal.x)
  24. + vSphericalL2_2 * (normal.y * normal.x)
  25. + vSphericalL2_1 * (normal.y * normal.z)
  26. + vSphericalL20 * ((3.0 * normal.z * normal.z) - 1.0)
  27. + vSphericalL21 * (normal.z * normal.x)
  28. + vSphericalL22 * (normal.x * normal.x - (normal.y * normal.y));
  29. }
  30. #else
  31. uniform vec3 vSphericalX;
  32. uniform vec3 vSphericalY;
  33. uniform vec3 vSphericalZ;
  34. uniform vec3 vSphericalXX_ZZ;
  35. uniform vec3 vSphericalYY_ZZ;
  36. uniform vec3 vSphericalZZ;
  37. uniform vec3 vSphericalXY;
  38. uniform vec3 vSphericalYZ;
  39. uniform vec3 vSphericalZX;
  40. // By Matthew Jones.
  41. vec3 computeEnvironmentIrradiance(vec3 normal) {
  42. // Fast method for evaluating a fixed spherical harmonics function on the sphere (e.g. irradiance or radiance).
  43. // Cost: 24 scalar operations on modern GPU "scalar" shader core, or 8 multiply-adds of 3D vectors:
  44. // "Function Cost 24 24x mad"
  45. // Note: the lower operation count compared to other methods (e.g. Sloan) is by further
  46. // taking advantage of the input 'normal' being normalised, which affords some further algebraic simplification.
  47. // Namely, the SH coefficients are first converted to spherical polynomial (SP) basis, then
  48. // a substitution is performed using Z^2 = (1 - X^2 - Y^2).
  49. // As with other methods for evaluation spherical harmonic, the input 'normal' is assumed to be normalised (or near normalised).
  50. // This isn't as critical as it is with other calculations (e.g. specular highlight), but the result will be slightly incorrect nonetheless.
  51. float Nx = normal.x;
  52. float Ny = normal.y;
  53. float Nz = normal.z;
  54. vec3 C1 = vSphericalZZ.rgb;
  55. vec3 Cx = vSphericalX.rgb;
  56. vec3 Cy = vSphericalY.rgb;
  57. vec3 Cz = vSphericalZ.rgb;
  58. vec3 Cxx_zz = vSphericalXX_ZZ.rgb;
  59. vec3 Cyy_zz = vSphericalYY_ZZ.rgb;
  60. vec3 Cxy = vSphericalXY.rgb;
  61. vec3 Cyz = vSphericalYZ.rgb;
  62. vec3 Czx = vSphericalZX.rgb;
  63. vec3 a1 = Cyy_zz * Ny + Cy;
  64. vec3 a2 = Cyz * Nz + a1;
  65. vec3 b1 = Czx * Nz + Cx;
  66. vec3 b2 = Cxy * Ny + b1;
  67. vec3 b3 = Cxx_zz * Nx + b2;
  68. vec3 t1 = Cz * Nz + C1;
  69. vec3 t2 = a2 * Ny + t1;
  70. vec3 t3 = b3 * Nx + t2;
  71. return t3;
  72. }
  73. #endif
  74. #endif