Home Reference Source

cables_dev/cables/src/libs/cgl/light/createshaders.js

  1. export function getShadowPassVertexShader()
  2. {
  3. return `
  4. IN vec3 vPosition;
  5. IN vec2 attrTexCoord;
  6. IN vec3 attrVertNormal;
  7. IN float attrVertIndex;
  8. IN vec3 attrTangent;
  9. IN vec3 attrBiTangent;
  10.  
  11. UNI mat4 projMatrix;
  12. UNI mat4 modelMatrix;
  13. UNI mat4 viewMatrix;
  14.  
  15.  
  16. OUT vec2 texCoord;
  17. OUT vec3 norm;
  18.  
  19. {{MODULES_HEAD}}
  20.  
  21. ${this.type === "point" ? "OUT vec3 modelPos;" : ""}
  22. void main() {
  23. texCoord=attrTexCoord;
  24. texCoord.y = 1. - texCoord.y;
  25. norm=attrVertNormal;
  26. vec4 pos = vec4(vPosition, 1.0);
  27. mat4 mMatrix=modelMatrix;
  28. vec3 tangent = attrTangent;
  29. vec3 bitangent = attrBiTangent;
  30.  
  31. {{MODULE_VERTEX_POSITION}}
  32.  
  33. mat4 mvMatrix=viewMatrix * mMatrix;
  34. vec4 vPos = projMatrix * mvMatrix * pos;
  35. ${this.type === "point" ? "modelPos = (mMatrix * pos).xyz;" : ""}
  36. gl_Position = vPos;
  37. }
  38. `;
  39. }
  40.  
  41. export function getShadowPassFragmentShader()
  42. {
  43. /*
  44. // http://fabiensanglard.net/shadowmappingVSM/
  45. #define SQRT3 1.73205081
  46. #define SQRT3DIV2 0.86602540378
  47. #define SQRT12DIV9 -0.38490017946
  48.  
  49. // FOR MOMENT SHADOW MAPPING
  50. const mat4 ENCODE_MATRIX = mat4(
  51. vec4(1.5, 0., SQRT3DIV2, 0.),
  52. vec4(0., 4., 0., 0.5),
  53. vec4(-2., 0., SQRT12DIV9, 0.),
  54. vec4(0., -4., 0., 0.5)
  55. );
  56. */
  57. /*
  58. dot(x, x) = x*x
  59. Finally, it is usually beneficial to clamp the partial derivative portion of M 2
  60. to avoid an excessively high variance if an occluder is almost parallel to the light direction.
  61. Hardware-generated partial derivatives become somewhat unstable in these cases
  62. and a correspondingly unstable variance can produce random, flashing pixels of light
  63. in regions that should be fully shadowed.
  64. https://developer.nvidia.com/gpugems/gpugems3/part-ii-light-and-shadows/chapter-8-summed-area-variance-shadow-maps
  65. */
  66. return `
  67. {{MODULES_HEAD}}
  68. ${this.type === "point" ? "IN vec3 modelPos;" : ""}
  69. ${this.type === "point" ? "UNI vec3 inLightPosition;" : ""}
  70. ${this.type === "point" ? "UNI vec2 inNearFar;" : ""}
  71.  
  72. IN vec2 texCoord;
  73.  
  74. void main() {
  75. {{MODULE_BEGIN_FRAG}}
  76. vec4 col = vec4(1.);
  77.  
  78.  
  79. outColor = vec4(1.);
  80.  
  81. {{MODULE_COLOR}}
  82.  
  83. ${this.type === "point" ? "vec3 fromLightToFrag = (modelPos - inLightPosition);" : ""}
  84.  
  85.  
  86. ${this.type === "point" ? "float depth = (length(fromLightToFrag) - inNearFar.x) / (inNearFar.y - inNearFar.x);" : "float depth = gl_FragCoord.z;"}
  87.  
  88. float dx = dFdx(depth); // for biasing depth-per-pixel
  89. float dy = dFdy(depth); // for biasing depth-per-pixel
  90.  
  91. float clampedDerivative = clamp(dot(dx, dx) + dot(dy, dy), 0., 1.);
  92. float moment2 = dot(depth, depth) + 0.25 * clampedDerivative;
  93.  
  94. outColor.x = depth;
  95. outColor.y = moment2;
  96. outColor.z = depth;
  97. }
  98. `;
  99. }
  100.  
  101. export function getBlurPassVertexShader()
  102. {
  103. if (this.type === "point") return "";
  104. return `
  105.  
  106. IN vec3 vPosition;
  107. IN vec2 attrTexCoord;
  108.  
  109. OUT vec2 texCoord;
  110. OUT vec2 coord0;
  111. OUT vec2 coord1;
  112. OUT vec2 coord2;
  113. OUT vec2 coord3;
  114. OUT vec2 coord4;
  115. OUT vec2 coord5;
  116. OUT vec2 coord6;
  117.  
  118. UNI mat4 projMatrix;
  119. UNI mat4 mvMatrix;
  120. UNI mat4 modelMatrix;
  121.  
  122. UNI vec2 inXY;
  123.  
  124. void main() {
  125. texCoord=attrTexCoord;
  126.  
  127. vec4 pos = vec4(vPosition, 1.0);
  128.  
  129. {{MODULE_VERTEX_POSITION}}
  130.  
  131. coord3 = attrTexCoord;
  132.  
  133.  
  134. coord0 = attrTexCoord + (-3.0368997744118595 * inXY);
  135. coord0 = clamp(coord0, 0., 1.);
  136. coord1 = attrTexCoord + (-2.089778445362373 * inXY);
  137. coord1 = clamp(coord1, 0., 1.);
  138. coord2 = attrTexCoord + (-1.2004366090034069 * inXY);
  139. coord2 = clamp(coord2, 0., 1.);
  140. coord4 = attrTexCoord + (1.2004366090034069 * inXY);
  141. coord4 = clamp(coord4, 0., 1.);
  142. coord5 = attrTexCoord + (2.089778445362373* inXY);
  143. coord5 = clamp(coord5, 0., 1.);
  144. coord6 = attrTexCoord + (3.0368997744118595 * inXY);
  145. coord6 = clamp(coord6, 0., 1.);
  146.  
  147. gl_Position = projMatrix * mvMatrix * pos;
  148. }
  149. `;
  150. }
  151.  
  152. export function getBlurPassFragmentShader()
  153. {
  154. if (this.type === "point") return "";
  155.  
  156. return `
  157. UNI sampler2D tex;
  158.  
  159. IN vec2 coord0;
  160. IN vec2 coord1;
  161. IN vec2 coord2;
  162. IN vec2 coord3;
  163. IN vec2 coord4;
  164. IN vec2 coord5;
  165. IN vec2 coord6;
  166.  
  167. void main() {
  168.  
  169. vec4 color = vec4(0.0);
  170.  
  171.  
  172. color.xyz += texture(tex, coord0).xyz * 0.06927096443792478; // 1/64
  173. color.xyz += texture(tex, coord1).xyz * 0.1383328848652136; // 6/64
  174. color.xyz += texture(tex, coord2).xyz * 0.21920904690397863; // 15/64
  175. color.xyz += texture(tex, coord3).xyz * 0.14637421; // 20/64
  176. color.xyz += texture(tex, coord4).xyz * 0.21920904690397863; // 15/64
  177. color.xyz += texture(tex, coord5).xyz * 0.1383328848652136; // 6/64
  178. color.xyz += texture(tex, coord6).xyz * 0.06927096443795711; // 1/64
  179.  
  180. color.a = 1.;
  181.  
  182. outColor = color;
  183. }
  184. `;
  185. }