shadowsFragmentFunctions.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // Do not edit.
  2. import { ShaderStore } from "../../Engines/shaderStore.js";
  3. const name = "shadowsFragmentFunctions";
  4. const shader = `#ifdef SHADOWS
  5. #if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
  6. #define TEXTUREFUNC(s,c,l) texture2DLodEXT(s,c,l)
  7. #else
  8. #define TEXTUREFUNC(s,c,b) texture2D(s,c,b)
  9. #endif
  10. #ifndef SHADOWFLOAT
  11. float unpack(vec4 color)
  12. {const vec4 bit_shift=vec4(1.0/(255.0*255.0*255.0),1.0/(255.0*255.0),1.0/255.0,1.0);return dot(color,bit_shift);}
  13. #endif
  14. float computeFallOff(float value,vec2 clipSpace,float frustumEdgeFalloff)
  15. {float mask=smoothstep(1.0-frustumEdgeFalloff,1.00000012,clamp(dot(clipSpace,clipSpace),0.,1.));return mix(value,1.0,mask);}
  16. #define inline
  17. float computeShadowCube(vec3 worldPos,vec3 lightPosition,samplerCube shadowSampler,float darkness,vec2 depthValues)
  18. {vec3 directionToLight=worldPos-lightPosition;float depth=length(directionToLight);depth=(depth+depthValues.x)/(depthValues.y);depth=clamp(depth,0.,1.0);directionToLight=normalize(directionToLight);directionToLight.y=-directionToLight.y;
  19. #ifndef SHADOWFLOAT
  20. float shadow=unpack(textureCube(shadowSampler,directionToLight));
  21. #else
  22. float shadow=textureCube(shadowSampler,directionToLight).x;
  23. #endif
  24. return depth>shadow ? darkness : 1.0;}
  25. #define inline
  26. float computeShadowWithPoissonSamplingCube(vec3 worldPos,vec3 lightPosition,samplerCube shadowSampler,float mapSize,float darkness,vec2 depthValues)
  27. {vec3 directionToLight=worldPos-lightPosition;float depth=length(directionToLight);depth=(depth+depthValues.x)/(depthValues.y);depth=clamp(depth,0.,1.0);directionToLight=normalize(directionToLight);directionToLight.y=-directionToLight.y;float visibility=1.;vec3 poissonDisk[4];poissonDisk[0]=vec3(-1.0,1.0,-1.0);poissonDisk[1]=vec3(1.0,-1.0,-1.0);poissonDisk[2]=vec3(-1.0,-1.0,-1.0);poissonDisk[3]=vec3(1.0,-1.0,1.0);
  28. #ifndef SHADOWFLOAT
  29. if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize))<depth) visibility-=0.25;if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize))<depth) visibility-=0.25;if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize))<depth) visibility-=0.25;if (unpack(textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize))<depth) visibility-=0.25;
  30. #else
  31. if (textureCube(shadowSampler,directionToLight+poissonDisk[0]*mapSize).x<depth) visibility-=0.25;if (textureCube(shadowSampler,directionToLight+poissonDisk[1]*mapSize).x<depth) visibility-=0.25;if (textureCube(shadowSampler,directionToLight+poissonDisk[2]*mapSize).x<depth) visibility-=0.25;if (textureCube(shadowSampler,directionToLight+poissonDisk[3]*mapSize).x<depth) visibility-=0.25;
  32. #endif
  33. return min(1.0,visibility+darkness);}
  34. #define inline
  35. float computeShadowWithESMCube(vec3 worldPos,vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
  36. {vec3 directionToLight=worldPos-lightPosition;float depth=length(directionToLight);depth=(depth+depthValues.x)/(depthValues.y);float shadowPixelDepth=clamp(depth,0.,1.0);directionToLight=normalize(directionToLight);directionToLight.y=-directionToLight.y;
  37. #ifndef SHADOWFLOAT
  38. float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
  39. #else
  40. float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
  41. #endif
  42. float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness);return esm;}
  43. #define inline
  44. float computeShadowWithCloseESMCube(vec3 worldPos,vec3 lightPosition,samplerCube shadowSampler,float darkness,float depthScale,vec2 depthValues)
  45. {vec3 directionToLight=worldPos-lightPosition;float depth=length(directionToLight);depth=(depth+depthValues.x)/(depthValues.y);float shadowPixelDepth=clamp(depth,0.,1.0);directionToLight=normalize(directionToLight);directionToLight.y=-directionToLight.y;
  46. #ifndef SHADOWFLOAT
  47. float shadowMapSample=unpack(textureCube(shadowSampler,directionToLight));
  48. #else
  49. float shadowMapSample=textureCube(shadowSampler,directionToLight).x;
  50. #endif
  51. float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);return esm;}
  52. #if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
  53. #define inline
  54. float computeShadowCSM(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArray shadowSampler,float darkness,float frustumEdgeFalloff)
  55. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec2 uv=0.5*clipSpace.xy+vec2(0.5);vec3 uvLayer=vec3(uv.x,uv.y,layer);float shadowPixelDepth=clamp(depthMetric,0.,1.0);
  56. #ifndef SHADOWFLOAT
  57. float shadow=unpack(texture2D(shadowSampler,uvLayer));
  58. #else
  59. float shadow=texture2D(shadowSampler,uvLayer).x;
  60. #endif
  61. return shadowPixelDepth>shadow ? computeFallOff(darkness,clipSpace.xy,frustumEdgeFalloff) : 1.;}
  62. #endif
  63. #define inline
  64. float computeShadow(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float frustumEdgeFalloff)
  65. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec2 uv=0.5*clipSpace.xy+vec2(0.5);if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
  66. {return 1.0;}
  67. else
  68. {float shadowPixelDepth=clamp(depthMetric,0.,1.0);
  69. #ifndef SHADOWFLOAT
  70. float shadow=unpack(TEXTUREFUNC(shadowSampler,uv,0.));
  71. #else
  72. float shadow=TEXTUREFUNC(shadowSampler,uv,0.).x;
  73. #endif
  74. return shadowPixelDepth>shadow ? computeFallOff(darkness,clipSpace.xy,frustumEdgeFalloff) : 1.;}}
  75. #define inline
  76. float computeShadowWithPoissonSampling(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float mapSize,float darkness,float frustumEdgeFalloff)
  77. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec2 uv=0.5*clipSpace.xy+vec2(0.5);if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
  78. {return 1.0;}
  79. else
  80. {float shadowPixelDepth=clamp(depthMetric,0.,1.0);float visibility=1.;vec2 poissonDisk[4];poissonDisk[0]=vec2(-0.94201624,-0.39906216);poissonDisk[1]=vec2(0.94558609,-0.76890725);poissonDisk[2]=vec2(-0.094184101,-0.92938870);poissonDisk[3]=vec2(0.34495938,0.29387760);
  81. #ifndef SHADOWFLOAT
  82. if (unpack(TEXTUREFUNC(shadowSampler,uv+poissonDisk[0]*mapSize,0.))<shadowPixelDepth) visibility-=0.25;if (unpack(TEXTUREFUNC(shadowSampler,uv+poissonDisk[1]*mapSize,0.))<shadowPixelDepth) visibility-=0.25;if (unpack(TEXTUREFUNC(shadowSampler,uv+poissonDisk[2]*mapSize,0.))<shadowPixelDepth) visibility-=0.25;if (unpack(TEXTUREFUNC(shadowSampler,uv+poissonDisk[3]*mapSize,0.))<shadowPixelDepth) visibility-=0.25;
  83. #else
  84. if (TEXTUREFUNC(shadowSampler,uv+poissonDisk[0]*mapSize,0.).x<shadowPixelDepth) visibility-=0.25;if (TEXTUREFUNC(shadowSampler,uv+poissonDisk[1]*mapSize,0.).x<shadowPixelDepth) visibility-=0.25;if (TEXTUREFUNC(shadowSampler,uv+poissonDisk[2]*mapSize,0.).x<shadowPixelDepth) visibility-=0.25;if (TEXTUREFUNC(shadowSampler,uv+poissonDisk[3]*mapSize,0.).x<shadowPixelDepth) visibility-=0.25;
  85. #endif
  86. return computeFallOff(min(1.0,visibility+darkness),clipSpace.xy,frustumEdgeFalloff);}}
  87. #define inline
  88. float computeShadowWithESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
  89. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec2 uv=0.5*clipSpace.xy+vec2(0.5);if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
  90. {return 1.0;}
  91. else
  92. {float shadowPixelDepth=clamp(depthMetric,0.,1.0);
  93. #ifndef SHADOWFLOAT
  94. float shadowMapSample=unpack(TEXTUREFUNC(shadowSampler,uv,0.));
  95. #else
  96. float shadowMapSample=TEXTUREFUNC(shadowSampler,uv,0.).x;
  97. #endif
  98. float esm=1.0-clamp(exp(min(87.,depthScale*shadowPixelDepth))*shadowMapSample,0.,1.-darkness);return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);}}
  99. #define inline
  100. float computeShadowWithCloseESM(vec4 vPositionFromLight,float depthMetric,sampler2D shadowSampler,float darkness,float depthScale,float frustumEdgeFalloff)
  101. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec2 uv=0.5*clipSpace.xy+vec2(0.5);if (uv.x<0. || uv.x>1.0 || uv.y<0. || uv.y>1.0)
  102. {return 1.0;}
  103. else
  104. {float shadowPixelDepth=clamp(depthMetric,0.,1.0);
  105. #ifndef SHADOWFLOAT
  106. float shadowMapSample=unpack(TEXTUREFUNC(shadowSampler,uv,0.));
  107. #else
  108. float shadowMapSample=TEXTUREFUNC(shadowSampler,uv,0.).x;
  109. #endif
  110. float esm=clamp(exp(min(87.,-depthScale*(shadowPixelDepth-shadowMapSample))),darkness,1.);return computeFallOff(esm,clipSpace.xy,frustumEdgeFalloff);}}
  111. #ifdef IS_NDC_HALF_ZRANGE
  112. #define ZINCLIP clipSpace.z
  113. #else
  114. #define ZINCLIP uvDepth.z
  115. #endif
  116. #if defined(WEBGL2) || defined(WEBGPU) || defined(NATIVE)
  117. #define GREATEST_LESS_THAN_ONE 0.99999994
  118. /* disable_uniformity_analysis */
  119. #define inline
  120. float computeShadowWithCSMPCF1(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArrayShadow shadowSampler,float darkness,float frustumEdgeFalloff)
  121. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=clamp(ZINCLIP,0.,GREATEST_LESS_THAN_ONE);vec4 uvDepthLayer=vec4(uvDepth.x,uvDepth.y,layer,uvDepth.z);float shadow=texture2D(shadowSampler,uvDepthLayer);shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}
  122. #define inline
  123. float computeShadowWithCSMPCF3(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArrayShadow shadowSampler,vec2 shadowMapSizeAndInverse,float darkness,float frustumEdgeFalloff)
  124. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=clamp(ZINCLIP,0.,GREATEST_LESS_THAN_ONE);vec2 uv=uvDepth.xy*shadowMapSizeAndInverse.x;
  125. uv+=0.5;
  126. vec2 st=fract(uv);
  127. vec2 base_uv=floor(uv)-0.5;
  128. base_uv*=shadowMapSizeAndInverse.y;
  129. vec2 uvw0=3.-2.*st;vec2 uvw1=1.+2.*st;vec2 u=vec2((2.-st.x)/uvw0.x-1.,st.x/uvw1.x+1.)*shadowMapSizeAndInverse.y;vec2 v=vec2((2.-st.y)/uvw0.y-1.,st.y/uvw1.y+1.)*shadowMapSizeAndInverse.y;float shadow=0.;shadow+=uvw0.x*uvw0.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[0],v[0]),layer,uvDepth.z));shadow+=uvw1.x*uvw0.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[1],v[0]),layer,uvDepth.z));shadow+=uvw0.x*uvw1.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[0],v[1]),layer,uvDepth.z));shadow+=uvw1.x*uvw1.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[1],v[1]),layer,uvDepth.z));shadow=shadow/16.;shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}
  130. #define inline
  131. float computeShadowWithCSMPCF5(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArrayShadow shadowSampler,vec2 shadowMapSizeAndInverse,float darkness,float frustumEdgeFalloff)
  132. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=clamp(ZINCLIP,0.,GREATEST_LESS_THAN_ONE);vec2 uv=uvDepth.xy*shadowMapSizeAndInverse.x;
  133. uv+=0.5;
  134. vec2 st=fract(uv);
  135. vec2 base_uv=floor(uv)-0.5;
  136. base_uv*=shadowMapSizeAndInverse.y;
  137. vec2 uvw0=4.-3.*st;vec2 uvw1=vec2(7.);vec2 uvw2=1.+3.*st;vec3 u=vec3((3.-2.*st.x)/uvw0.x-2.,(3.+st.x)/uvw1.x,st.x/uvw2.x+2.)*shadowMapSizeAndInverse.y;vec3 v=vec3((3.-2.*st.y)/uvw0.y-2.,(3.+st.y)/uvw1.y,st.y/uvw2.y+2.)*shadowMapSizeAndInverse.y;float shadow=0.;shadow+=uvw0.x*uvw0.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[0],v[0]),layer,uvDepth.z));shadow+=uvw1.x*uvw0.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[1],v[0]),layer,uvDepth.z));shadow+=uvw2.x*uvw0.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[2],v[0]),layer,uvDepth.z));shadow+=uvw0.x*uvw1.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[0],v[1]),layer,uvDepth.z));shadow+=uvw1.x*uvw1.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[1],v[1]),layer,uvDepth.z));shadow+=uvw2.x*uvw1.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[2],v[1]),layer,uvDepth.z));shadow+=uvw0.x*uvw2.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[0],v[2]),layer,uvDepth.z));shadow+=uvw1.x*uvw2.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[1],v[2]),layer,uvDepth.z));shadow+=uvw2.x*uvw2.y*texture2D(shadowSampler,vec4(base_uv.xy+vec2(u[2],v[2]),layer,uvDepth.z));shadow=shadow/144.;shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}
  138. #define inline
  139. float computeShadowWithPCF1(vec4 vPositionFromLight,float depthMetric,highp sampler2DShadow shadowSampler,float darkness,float frustumEdgeFalloff)
  140. {if (depthMetric>1.0 || depthMetric<0.0) {return 1.0;}
  141. else
  142. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=ZINCLIP;float shadow=TEXTUREFUNC(shadowSampler,uvDepth,0.);shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}}
  143. #define inline
  144. float computeShadowWithPCF3(vec4 vPositionFromLight,float depthMetric,highp sampler2DShadow shadowSampler,vec2 shadowMapSizeAndInverse,float darkness,float frustumEdgeFalloff)
  145. {if (depthMetric>1.0 || depthMetric<0.0) {return 1.0;}
  146. else
  147. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=ZINCLIP;vec2 uv=uvDepth.xy*shadowMapSizeAndInverse.x;
  148. uv+=0.5;
  149. vec2 st=fract(uv);
  150. vec2 base_uv=floor(uv)-0.5;
  151. base_uv*=shadowMapSizeAndInverse.y;
  152. vec2 uvw0=3.-2.*st;vec2 uvw1=1.+2.*st;vec2 u=vec2((2.-st.x)/uvw0.x-1.,st.x/uvw1.x+1.)*shadowMapSizeAndInverse.y;vec2 v=vec2((2.-st.y)/uvw0.y-1.,st.y/uvw1.y+1.)*shadowMapSizeAndInverse.y;float shadow=0.;shadow+=uvw0.x*uvw0.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[0],v[0]),uvDepth.z),0.);shadow+=uvw1.x*uvw0.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[1],v[0]),uvDepth.z),0.);shadow+=uvw0.x*uvw1.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[0],v[1]),uvDepth.z),0.);shadow+=uvw1.x*uvw1.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[1],v[1]),uvDepth.z),0.);shadow=shadow/16.;shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}}
  153. #define inline
  154. float computeShadowWithPCF5(vec4 vPositionFromLight,float depthMetric,highp sampler2DShadow shadowSampler,vec2 shadowMapSizeAndInverse,float darkness,float frustumEdgeFalloff)
  155. {if (depthMetric>1.0 || depthMetric<0.0) {return 1.0;}
  156. else
  157. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=ZINCLIP;vec2 uv=uvDepth.xy*shadowMapSizeAndInverse.x;
  158. uv+=0.5;
  159. vec2 st=fract(uv);
  160. vec2 base_uv=floor(uv)-0.5;
  161. base_uv*=shadowMapSizeAndInverse.y;
  162. vec2 uvw0=4.-3.*st;vec2 uvw1=vec2(7.);vec2 uvw2=1.+3.*st;vec3 u=vec3((3.-2.*st.x)/uvw0.x-2.,(3.+st.x)/uvw1.x,st.x/uvw2.x+2.)*shadowMapSizeAndInverse.y;vec3 v=vec3((3.-2.*st.y)/uvw0.y-2.,(3.+st.y)/uvw1.y,st.y/uvw2.y+2.)*shadowMapSizeAndInverse.y;float shadow=0.;shadow+=uvw0.x*uvw0.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[0],v[0]),uvDepth.z),0.);shadow+=uvw1.x*uvw0.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[1],v[0]),uvDepth.z),0.);shadow+=uvw2.x*uvw0.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[2],v[0]),uvDepth.z),0.);shadow+=uvw0.x*uvw1.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[0],v[1]),uvDepth.z),0.);shadow+=uvw1.x*uvw1.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[1],v[1]),uvDepth.z),0.);shadow+=uvw2.x*uvw1.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[2],v[1]),uvDepth.z),0.);shadow+=uvw0.x*uvw2.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[0],v[2]),uvDepth.z),0.);shadow+=uvw1.x*uvw2.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[1],v[2]),uvDepth.z),0.);shadow+=uvw2.x*uvw2.y*TEXTUREFUNC(shadowSampler,vec3(base_uv.xy+vec2(u[2],v[2]),uvDepth.z),0.);shadow=shadow/144.;shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}}
  163. const vec3 PoissonSamplers32[64]=vec3[64](
  164. vec3(0.06407013,0.05409927,0.),
  165. vec3(0.7366577,0.5789394,0.),
  166. vec3(-0.6270542,-0.5320278,0.),
  167. vec3(-0.4096107,0.8411095,0.),
  168. vec3(0.6849564,-0.4990818,0.),
  169. vec3(-0.874181,-0.04579735,0.),
  170. vec3(0.9989998,0.0009880066,0.),
  171. vec3(-0.004920578,-0.9151649,0.),
  172. vec3(0.1805763,0.9747483,0.),
  173. vec3(-0.2138451,0.2635818,0.),
  174. vec3(0.109845,0.3884785,0.),
  175. vec3(0.06876755,-0.3581074,0.),
  176. vec3(0.374073,-0.7661266,0.),
  177. vec3(0.3079132,-0.1216763,0.),
  178. vec3(-0.3794335,-0.8271583,0.),
  179. vec3(-0.203878,-0.07715034,0.),
  180. vec3(0.5912697,0.1469799,0.),
  181. vec3(-0.88069,0.3031784,0.),
  182. vec3(0.5040108,0.8283722,0.),
  183. vec3(-0.5844124,0.5494877,0.),
  184. vec3(0.6017799,-0.1726654,0.),
  185. vec3(-0.5554981,0.1559997,0.),
  186. vec3(-0.3016369,-0.3900928,0.),
  187. vec3(-0.5550632,-0.1723762,0.),
  188. vec3(0.925029,0.2995041,0.),
  189. vec3(-0.2473137,0.5538505,0.),
  190. vec3(0.9183037,-0.2862392,0.),
  191. vec3(0.2469421,0.6718712,0.),
  192. vec3(0.3916397,-0.4328209,0.),
  193. vec3(-0.03576927,-0.6220032,0.),
  194. vec3(-0.04661255,0.7995201,0.),
  195. vec3(0.4402924,0.3640312,0.),
  196. vec3(0.),
  197. vec3(0.),
  198. vec3(0.),
  199. vec3(0.),
  200. vec3(0.),
  201. vec3(0.),
  202. vec3(0.),
  203. vec3(0.),
  204. vec3(0.),
  205. vec3(0.),
  206. vec3(0.),
  207. vec3(0.),
  208. vec3(0.),
  209. vec3(0.),
  210. vec3(0.),
  211. vec3(0.),
  212. vec3(0.),
  213. vec3(0.),
  214. vec3(0.),
  215. vec3(0.),
  216. vec3(0.),
  217. vec3(0.),
  218. vec3(0.),
  219. vec3(0.),
  220. vec3(0.),
  221. vec3(0.),
  222. vec3(0.),
  223. vec3(0.),
  224. vec3(0.),
  225. vec3(0.),
  226. vec3(0.),
  227. vec3(0.)
  228. );const vec3 PoissonSamplers64[64]=vec3[64](
  229. vec3(-0.613392,0.617481,0.),
  230. vec3(0.170019,-0.040254,0.),
  231. vec3(-0.299417,0.791925,0.),
  232. vec3(0.645680,0.493210,0.),
  233. vec3(-0.651784,0.717887,0.),
  234. vec3(0.421003,0.027070,0.),
  235. vec3(-0.817194,-0.271096,0.),
  236. vec3(-0.705374,-0.668203,0.),
  237. vec3(0.977050,-0.108615,0.),
  238. vec3(0.063326,0.142369,0.),
  239. vec3(0.203528,0.214331,0.),
  240. vec3(-0.667531,0.326090,0.),
  241. vec3(-0.098422,-0.295755,0.),
  242. vec3(-0.885922,0.215369,0.),
  243. vec3(0.566637,0.605213,0.),
  244. vec3(0.039766,-0.396100,0.),
  245. vec3(0.751946,0.453352,0.),
  246. vec3(0.078707,-0.715323,0.),
  247. vec3(-0.075838,-0.529344,0.),
  248. vec3(0.724479,-0.580798,0.),
  249. vec3(0.222999,-0.215125,0.),
  250. vec3(-0.467574,-0.405438,0.),
  251. vec3(-0.248268,-0.814753,0.),
  252. vec3(0.354411,-0.887570,0.),
  253. vec3(0.175817,0.382366,0.),
  254. vec3(0.487472,-0.063082,0.),
  255. vec3(-0.084078,0.898312,0.),
  256. vec3(0.488876,-0.783441,0.),
  257. vec3(0.470016,0.217933,0.),
  258. vec3(-0.696890,-0.549791,0.),
  259. vec3(-0.149693,0.605762,0.),
  260. vec3(0.034211,0.979980,0.),
  261. vec3(0.503098,-0.308878,0.),
  262. vec3(-0.016205,-0.872921,0.),
  263. vec3(0.385784,-0.393902,0.),
  264. vec3(-0.146886,-0.859249,0.),
  265. vec3(0.643361,0.164098,0.),
  266. vec3(0.634388,-0.049471,0.),
  267. vec3(-0.688894,0.007843,0.),
  268. vec3(0.464034,-0.188818,0.),
  269. vec3(-0.440840,0.137486,0.),
  270. vec3(0.364483,0.511704,0.),
  271. vec3(0.034028,0.325968,0.),
  272. vec3(0.099094,-0.308023,0.),
  273. vec3(0.693960,-0.366253,0.),
  274. vec3(0.678884,-0.204688,0.),
  275. vec3(0.001801,0.780328,0.),
  276. vec3(0.145177,-0.898984,0.),
  277. vec3(0.062655,-0.611866,0.),
  278. vec3(0.315226,-0.604297,0.),
  279. vec3(-0.780145,0.486251,0.),
  280. vec3(-0.371868,0.882138,0.),
  281. vec3(0.200476,0.494430,0.),
  282. vec3(-0.494552,-0.711051,0.),
  283. vec3(0.612476,0.705252,0.),
  284. vec3(-0.578845,-0.768792,0.),
  285. vec3(-0.772454,-0.090976,0.),
  286. vec3(0.504440,0.372295,0.),
  287. vec3(0.155736,0.065157,0.),
  288. vec3(0.391522,0.849605,0.),
  289. vec3(-0.620106,-0.328104,0.),
  290. vec3(0.789239,-0.419965,0.),
  291. vec3(-0.545396,0.538133,0.),
  292. vec3(-0.178564,-0.596057,0.)
  293. );
  294. #define inline
  295. float computeShadowWithCSMPCSS(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArray depthSampler,highp sampler2DArrayShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff,int searchTapCount,int pcfTapCount,vec3[64] poissonSamplers,vec2 lightSizeUVCorrection,float depthCorrection,float penumbraDarkness)
  296. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=clamp(ZINCLIP,0.,GREATEST_LESS_THAN_ONE);vec4 uvDepthLayer=vec4(uvDepth.x,uvDepth.y,layer,uvDepth.z);float blockerDepth=0.0;float sumBlockerDepth=0.0;float numBlocker=0.0;for (int i=0; i<searchTapCount; i ++) {blockerDepth=texture2D(depthSampler,vec3(uvDepth.xy+(lightSizeUV*lightSizeUVCorrection*shadowMapSizeInverse*PoissonSamplers32[i].xy),layer)).r;if (blockerDepth<depthMetric) {sumBlockerDepth+=blockerDepth;numBlocker++;}}
  297. float avgBlockerDepth=sumBlockerDepth/numBlocker;float AAOffset=shadowMapSizeInverse*10.;float penumbraRatio=((depthMetric-avgBlockerDepth)*depthCorrection+AAOffset);vec4 filterRadius=vec4(penumbraRatio*lightSizeUV*lightSizeUVCorrection*shadowMapSizeInverse,0.,0.);float random=getRand(vPositionFromLight.xy);float rotationAngle=random*3.1415926;vec2 rotationVector=vec2(cos(rotationAngle),sin(rotationAngle));float shadow=0.;for (int i=0; i<pcfTapCount; i++) {vec4 offset=vec4(poissonSamplers[i],0.);offset=vec4(offset.x*rotationVector.x-offset.y*rotationVector.y,offset.y*rotationVector.x+offset.x*rotationVector.y,0.,0.);shadow+=texture2D(shadowSampler,uvDepthLayer+offset*filterRadius);}
  298. shadow/=float(pcfTapCount);shadow=mix(shadow,1.,min((depthMetric-avgBlockerDepth)*depthCorrection*penumbraDarkness,1.));shadow=mix(darkness,1.,shadow);if (numBlocker<1.0) {return 1.0;}
  299. else
  300. {return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}}
  301. #define inline
  302. float computeShadowWithPCSS(vec4 vPositionFromLight,float depthMetric,sampler2D depthSampler,highp sampler2DShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff,int searchTapCount,int pcfTapCount,vec3[64] poissonSamplers)
  303. {if (depthMetric>1.0 || depthMetric<0.0) {return 1.0;}
  304. else
  305. {vec3 clipSpace=vPositionFromLight.xyz/vPositionFromLight.w;vec3 uvDepth=vec3(0.5*clipSpace.xyz+vec3(0.5));uvDepth.z=ZINCLIP;float blockerDepth=0.0;float sumBlockerDepth=0.0;float numBlocker=0.0;for (int i=0; i<searchTapCount; i ++) {blockerDepth=TEXTUREFUNC(depthSampler,uvDepth.xy+(lightSizeUV*shadowMapSizeInverse*PoissonSamplers32[i].xy),0.).r;if (blockerDepth<depthMetric) {sumBlockerDepth+=blockerDepth;numBlocker++;}}
  306. if (numBlocker<1.0) {return 1.0;}
  307. else
  308. {float avgBlockerDepth=sumBlockerDepth/numBlocker;float AAOffset=shadowMapSizeInverse*10.;float penumbraRatio=((depthMetric-avgBlockerDepth)+AAOffset);float filterRadius=penumbraRatio*lightSizeUV*shadowMapSizeInverse;float random=getRand(vPositionFromLight.xy);float rotationAngle=random*3.1415926;vec2 rotationVector=vec2(cos(rotationAngle),sin(rotationAngle));float shadow=0.;for (int i=0; i<pcfTapCount; i++) {vec3 offset=poissonSamplers[i];offset=vec3(offset.x*rotationVector.x-offset.y*rotationVector.y,offset.y*rotationVector.x+offset.x*rotationVector.y,0.);shadow+=TEXTUREFUNC(shadowSampler,uvDepth+offset*filterRadius,0.);}
  309. shadow/=float(pcfTapCount);shadow=mix(shadow,1.,depthMetric-avgBlockerDepth);shadow=mix(darkness,1.,shadow);return computeFallOff(shadow,clipSpace.xy,frustumEdgeFalloff);}}}
  310. #define inline
  311. float computeShadowWithPCSS16(vec4 vPositionFromLight,float depthMetric,sampler2D depthSampler,highp sampler2DShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff)
  312. {return computeShadowWithPCSS(vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,16,16,PoissonSamplers32);}
  313. #define inline
  314. float computeShadowWithPCSS32(vec4 vPositionFromLight,float depthMetric,sampler2D depthSampler,highp sampler2DShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff)
  315. {return computeShadowWithPCSS(vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,16,32,PoissonSamplers32);}
  316. #define inline
  317. float computeShadowWithPCSS64(vec4 vPositionFromLight,float depthMetric,sampler2D depthSampler,highp sampler2DShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff)
  318. {return computeShadowWithPCSS(vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,32,64,PoissonSamplers64);}
  319. #define inline
  320. float computeShadowWithCSMPCSS16(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArray depthSampler,highp sampler2DArrayShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff,vec2 lightSizeUVCorrection,float depthCorrection,float penumbraDarkness)
  321. {return computeShadowWithCSMPCSS(layer,vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,16,16,PoissonSamplers32,lightSizeUVCorrection,depthCorrection,penumbraDarkness);}
  322. #define inline
  323. float computeShadowWithCSMPCSS32(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArray depthSampler,highp sampler2DArrayShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff,vec2 lightSizeUVCorrection,float depthCorrection,float penumbraDarkness)
  324. {return computeShadowWithCSMPCSS(layer,vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,16,32,PoissonSamplers32,lightSizeUVCorrection,depthCorrection,penumbraDarkness);}
  325. #define inline
  326. float computeShadowWithCSMPCSS64(float layer,vec4 vPositionFromLight,float depthMetric,highp sampler2DArray depthSampler,highp sampler2DArrayShadow shadowSampler,float shadowMapSizeInverse,float lightSizeUV,float darkness,float frustumEdgeFalloff,vec2 lightSizeUVCorrection,float depthCorrection,float penumbraDarkness)
  327. {return computeShadowWithCSMPCSS(layer,vPositionFromLight,depthMetric,depthSampler,shadowSampler,shadowMapSizeInverse,lightSizeUV,darkness,frustumEdgeFalloff,32,64,PoissonSamplers64,lightSizeUVCorrection,depthCorrection,penumbraDarkness);}
  328. #endif
  329. #endif
  330. `;
  331. // Sideeffect
  332. ShaderStore.IncludesShadersStore[name] = shader;
  333. /** @internal */
  334. export const shadowsFragmentFunctions = { name, shader };
  335. //# sourceMappingURL=shadowsFragmentFunctions.js.map