DisplaySH.hx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package hrt.shader;
  2. class DisplaySH extends hxsl.Shader {
  3. static var SRC = {
  4. @const var order : Int;
  5. @const var SIZE : Int; // Equal to order*order
  6. @param var shCoefsRed : Array<Float,SIZE>;
  7. @param var shCoefsGreen : Array<Float,SIZE>;
  8. @param var shCoefsBlue : Array<Float,SIZE>;
  9. @param var strength : Float;
  10. var transformedNormal : Vec3;
  11. var output : {
  12. color : Vec4
  13. };
  14. function computeIrradiance(norm : Vec3) : Vec3 {
  15. // An Efficient Representation for Irradiance Environment Maps
  16. // Ravi Ramamoorthi Pat Hanrahan
  17. var c1 = 0.429043;
  18. var c2 = 0.511664;
  19. var c3 = 0.743125;
  20. var c4 = 0.886227;
  21. var c5 = 0.247708;
  22. var irradiance = vec3(0,0,0);
  23. if( order >= 1 ) {
  24. var L00 = vec3(shCoefsRed[0], shCoefsGreen[0], shCoefsBlue[0]);
  25. irradiance += c4 * L00;
  26. }
  27. if( order >= 2 ) {
  28. var L1n1 = vec3(shCoefsRed[1], shCoefsGreen[1], shCoefsBlue[1]);
  29. var L10 = vec3(shCoefsRed[2], shCoefsGreen[2], shCoefsBlue[2]);
  30. var L11 = vec3(shCoefsRed[3], shCoefsGreen[3], shCoefsBlue[3]);
  31. irradiance += 2 * c2 * (L11 * norm.x + L1n1 * norm.y + L10 * norm.z);
  32. }
  33. if( order >= 3 ) {
  34. var L2n2 = vec3(shCoefsRed[4], shCoefsGreen[4], shCoefsBlue[4]);
  35. var L2n1 = vec3(shCoefsRed[5], shCoefsGreen[5], shCoefsBlue[5]);
  36. var L20 = vec3(shCoefsRed[6], shCoefsGreen[6], shCoefsBlue[6]);
  37. var L21 = vec3(shCoefsRed[7], shCoefsGreen[7], shCoefsBlue[7]);
  38. var L22 = vec3(shCoefsRed[8], shCoefsGreen[8], shCoefsBlue[8]);
  39. irradiance += c1 * L22 * (norm.x*norm.x - norm.y*norm.y) + c3 * L20 * (norm.z * norm.z) - c5 * L20 +
  40. 2 * c1 * (L2n2 * (norm.x*norm.y) + L21 * (norm.x*norm.z) + L2n1 * (norm.y*norm.z));
  41. }
  42. return irradiance / 3.14;
  43. }
  44. function fragment() {
  45. output.color = vec4(computeIrradiance(transformedNormal.xyz) * strength, 1.0);
  46. }
  47. }
  48. public function new() {
  49. super();
  50. strength = 1.0;
  51. }
  52. }