@react-three/drei 10.7.8 → 10.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),t=require("three"),r=require("react"),a=require("@react-three/fiber"),i=require("./shaderMaterial.cjs.js"),s=require("../materials/DiscardMaterial.cjs.js"),n=require("../helpers/constants.cjs.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function l(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var a=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,a.get?a:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var h=o(e),c=l(t),u=l(r);const d=u.createContext(null),p=i.shaderMaterial({color:new c.Color,blend:2,alphaTest:.75,opacity:0,map:null},"varying vec2 vUv;\n void main() {\n gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);\n vUv = uv;\n }",`varying vec2 vUv;\n uniform sampler2D map;\n uniform vec3 color;\n uniform float opacity;\n uniform float alphaTest;\n uniform float blend;\n void main() {\n vec4 sampledDiffuseColor = texture2D(map, vUv);\n gl_FragColor = vec4(color * sampledDiffuseColor.r * blend, max(0.0, (1.0 - (sampledDiffuseColor.r + sampledDiffuseColor.g + sampledDiffuseColor.b) / alphaTest)) * opacity);\n #include <tonemapping_fragment>\n #include <${n.version>=154?"colorspace_fragment":"encodings_fragment"}>\n }`),f=u.forwardRef((({children:e,temporal:t,frames:r=40,limit:i=1/0,blend:s=20,scale:n=10,opacity:o=1,alphaTest:l=.75,color:h="black",colorBlend:c=2,resolution:f=1024,toneMapped:g=!0,...v},M)=>{a.extend({SoftShadowMaterial:p});const b=a.useThree((e=>e.gl)),w=a.useThree((e=>e.scene)),y=a.useThree((e=>e.camera)),x=a.useThree((e=>e.invalidate)),C=u.useRef(null),S=u.useRef(null),[j]=u.useState((()=>new m(b,w,f)));u.useLayoutEffect((()=>{j.configure(C.current)}),[]);const T=u.useMemo((()=>({lights:new Map,temporal:!!t,frames:Math.max(2,r),blend:Math.max(2,r===1/0?s:r),count:0,getMesh:()=>C.current,reset:()=>{j.clear();const e=C.current.material;e.opacity=0,e.alphaTest=0,T.count=0},update:(e=1)=>{const t=C.current.material;T.temporal?(t.opacity=Math.min(o,t.opacity+o/T.blend),t.alphaTest=Math.min(l,t.alphaTest+l/T.blend)):(t.opacity=o,t.alphaTest=l),S.current.visible=!0,j.prepare();for(let t=0;t<e;t++)T.lights.forEach((e=>e.update())),j.update(y,T.blend);S.current.visible=!1,j.finish()}})),[j,y,w,t,r,s,o,l]);return u.useLayoutEffect((()=>{T.reset(),T.temporal||T.frames===1/0||T.update(T.blend)})),u.useImperativeHandle(M,(()=>T),[T]),a.useFrame((()=>{(T.temporal||T.frames===1/0)&&T.count<T.frames&&T.count<i&&(x(),T.update(),T.count++)})),u.createElement("group",v,u.createElement("group",{traverse:()=>null,ref:S},u.createElement(d.Provider,{value:T},e)),u.createElement("mesh",{receiveShadow:!0,ref:C,scale:n,rotation:[-Math.PI/2,0,0]},u.createElement("planeGeometry",null),u.createElement("softShadowMaterial",{transparent:!0,depthWrite:!1,toneMapped:g,color:h,blend:c,map:j.progressiveLightMap2.texture})))})),g=u.forwardRef((({castShadow:e=!0,bias:t=.001,mapSize:r=512,size:a=5,near:i=.5,far:s=500,frames:o=1,position:l=[0,0,0],radius:p=1,amount:f=8,intensity:g=(n.version>=155?Math.PI:1),ambient:m=.5,...v},M)=>{const b=u.useRef(null),w=new c.Vector3(...l).length(),y=u.useContext(d),x=u.useCallback((()=>{let e;if(b.current)for(let t=0;t<b.current.children.length;t++)if(e=b.current.children[t],Math.random()>m)e.position.set(l[0]+c.MathUtils.randFloatSpread(p),l[1]+c.MathUtils.randFloatSpread(p),l[2]+c.MathUtils.randFloatSpread(p));else{let t=Math.acos(2*Math.random()-1)-Math.PI/2,r=2*Math.PI*Math.random();e.position.set(Math.cos(t)*Math.cos(r)*w,Math.abs(Math.cos(t)*Math.sin(r)*w),Math.sin(t)*w)}}),[p,m,w,...l]),C=u.useMemo((()=>({update:x})),[x]);return u.useImperativeHandle(M,(()=>C),[C]),u.useLayoutEffect((()=>{var e;const t=b.current;return y&&(null==(e=y.lights)||e.set(t.uuid,C)),()=>{var e;null==y||null==(e=y.lights)||e.delete(t.uuid)}}),[y,C]),u.createElement("group",h.default({ref:b},v),Array.from({length:f},((n,o)=>u.createElement("directionalLight",{key:o,castShadow:e,"shadow-bias":t,"shadow-mapSize":[r,r],intensity:g/f},u.createElement("orthographicCamera",{attach:"shadow-camera",args:[-a,a,a,-a,i,s]})))))}));class m{constructor(e,t,r=1024){this.renderer=e,this.res=r,this.scene=t,this.buffer1Active=!1,this.lights=[],this.meshes=[],this.object=null,this.clearColor=new c.Color,this.clearAlpha=0;const a={type:c.HalfFloatType,magFilter:c.NearestFilter,minFilter:c.NearestFilter};this.progressiveLightMap1=new c.WebGLRenderTarget(this.res,this.res,a),this.progressiveLightMap2=new c.WebGLRenderTarget(this.res,this.res,a),this.discardMat=new s.DiscardMaterial,this.targetMat=new c.MeshLambertMaterial({fog:!1}),this.previousShadowMap={value:this.progressiveLightMap1.texture},this.averagingWindow={value:100},this.targetMat.onBeforeCompile=e=>{e.vertexShader="varying vec2 vUv;\n"+e.vertexShader.slice(0,-1)+"vUv = uv; gl_Position = vec4((uv - 0.5) * 2.0, 1.0, 1.0); }";const t=e.fragmentShader.indexOf("void main() {");e.fragmentShader="varying vec2 vUv;\n"+e.fragmentShader.slice(0,t)+"uniform sampler2D previousShadowMap;\n\tuniform float averagingWindow;\n"+e.fragmentShader.slice(t-1,-1)+"\nvec3 texelOld = texture2D(previousShadowMap, vUv).rgb;\n gl_FragColor.rgb = mix(texelOld, gl_FragColor.rgb, 1.0/ averagingWindow);\n }",e.uniforms.previousShadowMap=this.previousShadowMap,e.uniforms.averagingWindow=this.averagingWindow}}clear(){this.renderer.getClearColor(this.clearColor),this.clearAlpha=this.renderer.getClearAlpha(),this.renderer.setClearColor("black",1),this.renderer.setRenderTarget(this.progressiveLightMap1),this.renderer.clear(),this.renderer.setRenderTarget(this.progressiveLightMap2),this.renderer.clear(),this.renderer.setRenderTarget(null),this.renderer.setClearColor(this.clearColor,this.clearAlpha),this.lights=[],this.meshes=[],this.scene.traverse((e=>{!function(e){return!!e.geometry}(e)?function(e){return e.isLight}(e)&&this.lights.push({object:e,intensity:e.intensity}):this.meshes.push({object:e,material:e.material})}))}prepare(){this.lights.forEach((e=>e.object.intensity=0)),this.meshes.forEach((e=>e.object.material=this.discardMat))}finish(){this.lights.forEach((e=>e.object.intensity=e.intensity)),this.meshes.forEach((e=>e.object.material=e.material))}configure(e){this.object=e}update(e,t=100){if(!this.object)return;this.averagingWindow.value=t,this.object.material=this.targetMat;const r=this.buffer1Active?this.progressiveLightMap1:this.progressiveLightMap2,a=this.buffer1Active?this.progressiveLightMap2:this.progressiveLightMap1,i=this.scene.background;this.scene.background=null,this.renderer.setRenderTarget(r),this.previousShadowMap.value=a.texture,this.buffer1Active=!this.buffer1Active,this.renderer.render(this.scene,e),this.renderer.setRenderTarget(null),this.scene.background=i}}exports.AccumulativeShadows=f,exports.RandomizedLight=g,exports.accumulativeContext=d;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@babel/runtime/helpers/extends"),t=require("three"),r=require("react"),a=require("@react-three/fiber"),i=require("./shaderMaterial.cjs.js"),s=require("../materials/DiscardMaterial.cjs.js"),n=require("../helpers/constants.cjs.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function l(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach((function(r){if("default"!==r){var a=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,a.get?a:{enumerable:!0,get:function(){return e[r]}})}})),t.default=e,Object.freeze(t)}var h=o(e),c=l(t),u=l(r);const d=u.createContext(null),p=i.shaderMaterial({color:new c.Color,blend:2,alphaTest:.75,opacity:0,map:null},"varying vec2 vUv;\n void main() {\n gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);\n vUv = uv;\n }",`varying vec2 vUv;\n uniform sampler2D map;\n uniform vec3 color;\n uniform float opacity;\n uniform float alphaTest;\n uniform float blend;\n void main() {\n vec4 sampledDiffuseColor = texture2D(map, vUv);\n gl_FragColor = vec4(color * sampledDiffuseColor.r * blend, max(0.0, (1.0 - (sampledDiffuseColor.r + sampledDiffuseColor.g + sampledDiffuseColor.b) / alphaTest)) * opacity);\n #include <tonemapping_fragment>\n #include <${n.version>=154?"colorspace_fragment":"encodings_fragment"}>\n }`),m=u.forwardRef((({children:e,temporal:t,frames:r=40,limit:i=1/0,blend:s=20,scale:n=10,opacity:o=1,alphaTest:l=.75,color:h="black",colorBlend:c=2,resolution:m=1024,toneMapped:f=!0,...v},M)=>{a.extend({SoftShadowMaterial:p});const b=a.useThree((e=>e.gl)),w=a.useThree((e=>e.scene)),y=a.useThree((e=>e.camera)),x=a.useThree((e=>e.invalidate)),C=u.useRef(null),S=u.useRef(null),[j]=u.useState((()=>new g(b,w,m)));u.useLayoutEffect((()=>{j.configure(C.current)}),[]);const T=u.useMemo((()=>({lights:new Map,temporal:!!t,frames:Math.max(2,r),blend:Math.max(2,r===1/0?s:r),count:0,getMesh:()=>C.current,reset:()=>{j.clear();const e=C.current.material;e.opacity=0,e.alphaTest=0,T.count=0},update:(e=1)=>{const t=C.current.material;T.temporal?(t.opacity=Math.min(o,t.opacity+o/T.blend),t.alphaTest=Math.min(l,t.alphaTest+l/T.blend)):(t.opacity=o,t.alphaTest=l),S.current.visible=!0,j.prepare();for(let t=0;t<e;t++)T.lights.forEach((e=>e.update())),j.update(y,T.blend);S.current.visible=!1,j.finish()}})),[j,y,w,t,r,s,o,l]);return u.useLayoutEffect((()=>{T.reset(),T.temporal||T.frames===1/0||T.update(T.blend)})),u.useImperativeHandle(M,(()=>T),[T]),a.useFrame((()=>{(T.temporal||T.frames===1/0)&&T.count<T.frames&&T.count<i&&(x(),T.update(),T.count++)})),u.createElement("group",v,u.createElement("group",{traverse:()=>null,ref:S},u.createElement(d.Provider,{value:T},e)),u.createElement("mesh",{receiveShadow:!0,ref:C,scale:n,rotation:[-Math.PI/2,0,0]},u.createElement("planeGeometry",null),u.createElement("softShadowMaterial",{transparent:!0,depthWrite:!1,toneMapped:f,color:h,blend:c,map:j.progressiveLightMap2.texture})))})),f=u.forwardRef((({castShadow:e=!0,bias:t=.001,mapSize:r=512,size:a=5,near:i=.5,far:s=500,frames:o=1,position:l=[0,0,0],radius:p=1,amount:m=8,intensity:f=(n.version>=155?Math.PI:1),ambient:g=.5,...v},M)=>{const b=u.useRef(null),w=new c.Vector3(...l).length(),y=u.useContext(d),x=u.useCallback((()=>{let e;if(b.current)for(let t=0;t<b.current.children.length;t++)if(e=b.current.children[t],Math.random()>g)e.position.set(l[0]+c.MathUtils.randFloatSpread(p),l[1]+c.MathUtils.randFloatSpread(p),l[2]+c.MathUtils.randFloatSpread(p));else{let t=Math.acos(2*Math.random()-1)-Math.PI/2,r=2*Math.PI*Math.random();e.position.set(Math.cos(t)*Math.cos(r)*w,Math.abs(Math.cos(t)*Math.sin(r)*w),Math.sin(t)*w)}}),[p,g,w,...l]),C=u.useMemo((()=>({update:x})),[x]);return u.useImperativeHandle(M,(()=>C),[C]),u.useLayoutEffect((()=>{var e;const t=b.current;return y&&(null==(e=y.lights)||e.set(t.uuid,C)),()=>{var e;null==y||null==(e=y.lights)||e.delete(t.uuid)}}),[y,C]),u.createElement("group",h.default({ref:b},v),Array.from({length:m},((n,o)=>u.createElement("directionalLight",{key:o,castShadow:e,"shadow-bias":t,"shadow-mapSize":[r,r],intensity:f/m},u.createElement("orthographicCamera",{attach:"shadow-camera",args:[-a,a,a,-a,i,s]})))))}));class g{constructor(e,t,r=1024){this.renderer=e,this.res=r,this.scene=t,this.buffer1Active=!1,this.lights=[],this.meshes=[],this.object=null,this.clearColor=new c.Color,this.clearAlpha=0;const a={type:c.HalfFloatType,magFilter:c.NearestFilter,minFilter:c.NearestFilter};this.progressiveLightMap1=new c.WebGLRenderTarget(this.res,this.res,a),this.progressiveLightMap2=new c.WebGLRenderTarget(this.res,this.res,a),this.discardMat=new s.DiscardMaterial,this.targetMat=new c.MeshLambertMaterial({fog:!1}),this.previousShadowMap={value:this.progressiveLightMap1.texture},this.averagingWindow={value:100},this.targetMat.onBeforeCompile=e=>{e.vertexShader="varying vec2 vUv;\n"+e.vertexShader.slice(0,-1)+"vUv = uv; gl_Position = vec4((uv - 0.5) * 2.0, 1.0, 1.0); }";const t=e.fragmentShader.indexOf("void main() {");e.fragmentShader="varying vec2 vUv;\n"+e.fragmentShader.slice(0,t)+"uniform sampler2D previousShadowMap;\n\tuniform float averagingWindow;\n"+e.fragmentShader.slice(t-1,-1)+"\nvec3 texelOld = texture2D(previousShadowMap, vUv).rgb;\n gl_FragColor.rgb = mix(texelOld, gl_FragColor.rgb, 1.0/ averagingWindow);\n }",e.uniforms.previousShadowMap=this.previousShadowMap,e.uniforms.averagingWindow=this.averagingWindow}}clear(){this.renderer.getClearColor(this.clearColor),this.clearAlpha=this.renderer.getClearAlpha(),this.renderer.setClearColor("black",1),this.renderer.setRenderTarget(this.progressiveLightMap1),this.renderer.clear(),this.renderer.setRenderTarget(this.progressiveLightMap2),this.renderer.clear(),this.renderer.setRenderTarget(null),this.renderer.setClearColor(this.clearColor,this.clearAlpha),this.lights=[],this.meshes=[],this.scene.traverse((e=>{!function(e){return!!e.geometry}(e)?function(e){return e.isLight}(e)&&this.lights.push({object:e,intensity:e.intensity}):this.meshes.push({object:e,material:e.material})}))}prepare(){this.lights.forEach((e=>e.object.intensity=0)),this.meshes.forEach((e=>e.object.material=this.discardMat))}finish(){this.lights.forEach((e=>e.object.intensity=e.intensity)),this.meshes.forEach((e=>e.object.material=e.material))}configure(e){this.object=e}update(e,t=100){if(!this.object)return;this.averagingWindow.value=t,this.object.material=this.targetMat;const r=this.buffer1Active?this.progressiveLightMap1:this.progressiveLightMap2,a=this.buffer1Active?this.progressiveLightMap2:this.progressiveLightMap1,i=this.scene.background,s=this.scene.environment;this.scene.background=null,this.scene.environment=null,this.renderer.setRenderTarget(r),this.previousShadowMap.value=a.texture,this.buffer1Active=!this.buffer1Active,this.renderer.render(this.scene,e),this.renderer.setRenderTarget(null),this.scene.background=i,this.scene.environment=s}}exports.AccumulativeShadows=m,exports.RandomizedLight=f,exports.accumulativeContext=d;
@@ -295,15 +295,19 @@ class ProgressiveLightMap {
295
295
  // Ping-pong two surface buffers for reading/writing
296
296
  const activeMap = this.buffer1Active ? this.progressiveLightMap1 : this.progressiveLightMap2;
297
297
  const inactiveMap = this.buffer1Active ? this.progressiveLightMap2 : this.progressiveLightMap1;
298
- // Render the object's surface maps
298
+ // Render the object's surface maps with direct light only: since three r182
299
+ // scene.environment also lights MeshLambertMaterial, so clear it like the background
299
300
  const oldBg = this.scene.background;
301
+ const oldEnv = this.scene.environment;
300
302
  this.scene.background = null;
303
+ this.scene.environment = null;
301
304
  this.renderer.setRenderTarget(activeMap);
302
305
  this.previousShadowMap.value = inactiveMap.texture;
303
306
  this.buffer1Active = !this.buffer1Active;
304
307
  this.renderer.render(this.scene, camera);
305
308
  this.renderer.setRenderTarget(null);
306
309
  this.scene.background = oldBg;
310
+ this.scene.environment = oldEnv;
307
311
  }
308
312
  }
309
313
 
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),n=require("three"),a=require("@react-three/fiber");function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(a){if("default"!==a){var t=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(n,a,t.get?t:{enumerable:!0,get:function(){return e[a]}})}})),n.default=e,Object.freeze(n)}var r=t(e),o=t(n);function l(e,n,a){n.traverse((n=>{n.material&&(e.properties.remove(n.material),null==n.material.dispose||n.material.dispose())})),e.info.programs.length=0,e.compile(n,a)}exports.SoftShadows=function({focus:e=0,samples:n=10,size:t=25}){const s=a.useThree((e=>e.gl)),i=a.useThree((e=>e.scene)),u=a.useThree((e=>e.camera));return r.useEffect((()=>{const a=o.ShaderChunk.shadowmap_pars_fragment;return o.ShaderChunk.shadowmap_pars_fragment=o.ShaderChunk.shadowmap_pars_fragment.replace("#ifdef USE_SHADOWMAP","#ifdef USE_SHADOWMAP\n"+(({focus:e=0,size:n=25,samples:a=10}={})=>`\n#define PENUMBRA_FILTER_SIZE float(${n})\n#define RGB_NOISE_FUNCTION(uv) (randRGB(uv))\nvec3 randRGB(vec2 uv) {\n return vec3(\n fract(sin(dot(uv, vec2(12.75613, 38.12123))) * 13234.76575),\n fract(sin(dot(uv, vec2(19.45531, 58.46547))) * 43678.23431),\n fract(sin(dot(uv, vec2(23.67817, 78.23121))) * 93567.23423)\n );\n}\n\nvec3 lowPassRandRGB(vec2 uv) {\n // 3x3 convolution (average)\n // can be implemented as separable with an extra buffer for a total of 6 samples instead of 9\n vec3 result = vec3(0);\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, +1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, +1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, +1.0));\n result *= 0.111111111; // 1.0 / 9.0\n return result;\n}\nvec3 highPassRandRGB(vec2 uv) {\n // by subtracting the low-pass signal from the original signal, we're being left with the high-pass signal\n // hp(x) = x - lp(x)\n return RGB_NOISE_FUNCTION(uv) - lowPassRandRGB(uv) + 0.5;\n}\n\n\nvec2 vogelDiskSample(int sampleIndex, int sampleCount, float angle) {\n const float goldenAngle = 2.399963f; // radians\n float r = sqrt(float(sampleIndex) + 0.5f) / sqrt(float(sampleCount));\n float theta = float(sampleIndex) * goldenAngle + angle;\n float sine = sin(theta);\n float cosine = cos(theta);\n return vec2(cosine, sine) * r;\n}\nfloat penumbraSize( const in float zReceiver, const in float zBlocker ) { // Parallel plane estimation\n return (zReceiver - zBlocker) / zBlocker;\n}\nfloat findBlocker(sampler2D shadowMap, vec2 uv, float compare, float angle) {\n float texelSize = 1.0 / float(textureSize(shadowMap, 0).x);\n float blockerDepthSum = float(${e});\n float blockers = 0.0;\n\n int j = 0;\n vec2 offset = vec2(0.);\n float depth = 0.;\n\n #pragma unroll_loop_start\n for(int i = 0; i < ${a}; i ++) {\n offset = (vogelDiskSample(j, ${a}, angle) * texelSize) * 2.0 * PENUMBRA_FILTER_SIZE;\n depth = unpackRGBAToDepth( texture2D( shadowMap, uv + offset));\n if (depth < compare) {\n blockerDepthSum += depth;\n blockers++;\n }\n j++;\n }\n #pragma unroll_loop_end\n\n if (blockers > 0.0) {\n return blockerDepthSum / blockers;\n }\n return -1.0;\n}\n\n \nfloat vogelFilter(sampler2D shadowMap, vec2 uv, float zReceiver, float filterRadius, float angle) {\n float texelSize = 1.0 / float(textureSize(shadowMap, 0).x);\n float shadow = 0.0f;\n int j = 0;\n vec2 vogelSample = vec2(0.0);\n vec2 offset = vec2(0.0);\n #pragma unroll_loop_start\n for (int i = 0; i < ${a}; i++) {\n vogelSample = vogelDiskSample(j, ${a}, angle) * texelSize;\n offset = vogelSample * (1.0 + filterRadius * float(${n}));\n shadow += step( zReceiver, unpackRGBAToDepth( texture2D( shadowMap, uv + offset ) ) );\n j++;\n }\n #pragma unroll_loop_end\n return shadow * 1.0 / ${a}.0;\n}\n\nfloat PCSS (sampler2D shadowMap, vec4 coords) {\n vec2 uv = coords.xy;\n float zReceiver = coords.z; // Assumed to be eye-space z in this code\n float angle = highPassRandRGB(gl_FragCoord.xy).r * PI2;\n float avgBlockerDepth = findBlocker(shadowMap, uv, zReceiver, angle);\n if (avgBlockerDepth == -1.0) {\n return 1.0;\n }\n float penumbraRatio = penumbraSize(zReceiver, avgBlockerDepth);\n return vogelFilter(shadowMap, uv, zReceiver, 1.25 * penumbraRatio, angle);\n}`)({size:t,samples:n,focus:e})).replace("#if defined( SHADOWMAP_TYPE_PCF )","\nreturn PCSS(shadowMap, shadowCoord);\n#if defined( SHADOWMAP_TYPE_PCF )"),l(s,i,u),()=>{o.ShaderChunk.shadowmap_pars_fragment=a,l(s,i,u)}}),[e,t,n]),null};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),n=require("three"),a=require("@react-three/fiber");function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(a){if("default"!==a){var t=Object.getOwnPropertyDescriptor(e,a);Object.defineProperty(n,a,t.get?t:{enumerable:!0,get:function(){return e[a]}})}})),n.default=e,Object.freeze(n)}var r=t(e),o=t(n);function s(e,n,a){n.traverse((n=>{if(n.material){(Array.isArray(n.material)?n.material:[n.material]).forEach((n=>{e.properties.remove(n),null==n.dispose||n.dispose(),n.needsUpdate=!0}))}})),e.info.programs.length=0,e.compile(n,a)}exports.SoftShadows=function({focus:e=0,samples:n=10,size:t=25}){const l=a.useThree((e=>e.gl)),i=a.useThree((e=>e.scene)),c=a.useThree((e=>e.camera));return r.useEffect((()=>{const a=o.ShaderChunk.shadowmap_pars_fragment,r=!a.includes("sampler2DShadow"),u=a.lastIndexOf("float getShadow( sampler2D shadowMap"),f=a.indexOf("if ( frustumTest ) {",u)+20;if(u<0||f<20)return void console.warn("[SoftShadows] Could not find injection point in shadow shader");const p=l.shadowMap.type;r||(l.shadowMap.type=o.BasicShadowMap);const d=a.slice(u,f).includes("shadowIntensity")?"return mix( 1.0, PCSS( shadowMap, shadowCoord ), shadowIntensity );":"return PCSS( shadowMap, shadowCoord );",v=(a.slice(0,f)+"\n"+d+a.slice(f)).replace("#ifdef USE_SHADOWMAP","#ifdef USE_SHADOWMAP\n"+(({focus:e=0,size:n=25,samples:a=10},t)=>{const r=t?"unpackRGBAToDepth( texture2D( shadowMap, uv + offset ) )":"texture2D( shadowMap, uv + offset ).r";return`\n#define PENUMBRA_FILTER_SIZE float(${n})\n#define RGB_NOISE_FUNCTION(uv) (randRGB(uv))\nvec3 randRGB(vec2 uv) {\n return vec3(\n fract(sin(dot(uv, vec2(12.75613, 38.12123))) * 13234.76575),\n fract(sin(dot(uv, vec2(19.45531, 58.46547))) * 43678.23431),\n fract(sin(dot(uv, vec2(23.67817, 78.23121))) * 93567.23423)\n );\n}\n\nvec3 lowPassRandRGB(vec2 uv) {\n // 3x3 convolution (average)\n // can be implemented as separable with an extra buffer for a total of 6 samples instead of 9\n vec3 result = vec3(0);\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(-1.0, +1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2( 0.0, +1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, -1.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, 0.0));\n result += RGB_NOISE_FUNCTION(uv + vec2(+1.0, +1.0));\n result *= 0.111111111; // 1.0 / 9.0\n return result;\n}\nvec3 highPassRandRGB(vec2 uv) {\n // by subtracting the low-pass signal from the original signal, we're being left with the high-pass signal\n // hp(x) = x - lp(x)\n return RGB_NOISE_FUNCTION(uv) - lowPassRandRGB(uv) + 0.5;\n}\n\n\nvec2 pcssVogelDiskSample(int sampleIndex, int sampleCount, float angle) {\n const float goldenAngle = 2.399963f; // radians\n float r = sqrt(float(sampleIndex) + 0.5f) / sqrt(float(sampleCount));\n float theta = float(sampleIndex) * goldenAngle + angle;\n float sine = sin(theta);\n float cosine = cos(theta);\n return vec2(cosine, sine) * r;\n}\nfloat penumbraSize( const in float zReceiver, const in float zBlocker ) { // Parallel plane estimation\n return (zReceiver - zBlocker) / zBlocker;\n}\nfloat findBlocker(sampler2D shadowMap, vec2 uv, float compare, float angle) {\n float texelSize = 1.0 / float(textureSize(shadowMap, 0).x);\n float blockerDepthSum = float(${e});\n float blockers = 0.0;\n\n int j = 0;\n vec2 offset = vec2(0.);\n float depth = 0.;\n\n #pragma unroll_loop_start\n for(int i = 0; i < ${a}; i ++) {\n offset = (pcssVogelDiskSample(j, ${a}, angle) * texelSize) * 2.0 * PENUMBRA_FILTER_SIZE;\n depth = ${r};\n if (depth < compare) {\n blockerDepthSum += depth;\n blockers++;\n }\n j++;\n }\n #pragma unroll_loop_end\n\n if (blockers > 0.0) {\n return blockerDepthSum / blockers;\n }\n return -1.0;\n}\n\n \nfloat vogelFilter(sampler2D shadowMap, vec2 uv, float zReceiver, float filterRadius, float angle) {\n float texelSize = 1.0 / float(textureSize(shadowMap, 0).x);\n float shadow = 0.0f;\n int j = 0;\n vec2 vogelSample = vec2(0.0);\n vec2 offset = vec2(0.0);\n #pragma unroll_loop_start\n for (int i = 0; i < ${a}; i++) {\n vogelSample = pcssVogelDiskSample(j, ${a}, angle) * texelSize;\n offset = vogelSample * (1.0 + filterRadius * float(${n}));\n shadow += step( zReceiver, ${r} );\n j++;\n }\n #pragma unroll_loop_end\n return shadow * 1.0 / ${a}.0;\n}\n\nfloat PCSS (sampler2D shadowMap, vec4 coords) {\n vec2 uv = coords.xy;\n float zReceiver = coords.z; // Assumed to be eye-space z in this code\n float angle = highPassRandRGB(gl_FragCoord.xy).r * PI2;\n float avgBlockerDepth = findBlocker(shadowMap, uv, zReceiver, angle);\n if (avgBlockerDepth == -1.0) {\n return 1.0;\n }\n float penumbraRatio = penumbraSize(zReceiver, avgBlockerDepth);\n return vogelFilter(shadowMap, uv, zReceiver, 1.25 * penumbraRatio, angle);\n}`})({size:t,samples:n,focus:e},r));return o.ShaderChunk.shadowmap_pars_fragment=v,s(l,i,c),()=>{o.ShaderChunk.shadowmap_pars_fragment=a,l.shadowMap.type=p,s(l,i,c)}}),[e,t,n]),null};
@@ -20,7 +20,9 @@ const pcss = ({
20
20
  focus = 0,
21
21
  size = 25,
22
22
  samples = 10
23
- } = {}) => `
23
+ }, packedDepth) => {
24
+ const depthSample = packedDepth ? 'unpackRGBAToDepth( texture2D( shadowMap, uv + offset ) )' : 'texture2D( shadowMap, uv + offset ).r';
25
+ return `
24
26
  #define PENUMBRA_FILTER_SIZE float(${size})
25
27
  #define RGB_NOISE_FUNCTION(uv) (randRGB(uv))
26
28
  vec3 randRGB(vec2 uv) {
@@ -54,7 +56,7 @@ vec3 highPassRandRGB(vec2 uv) {
54
56
  }
55
57
 
56
58
 
57
- vec2 vogelDiskSample(int sampleIndex, int sampleCount, float angle) {
59
+ vec2 pcssVogelDiskSample(int sampleIndex, int sampleCount, float angle) {
58
60
  const float goldenAngle = 2.399963f; // radians
59
61
  float r = sqrt(float(sampleIndex) + 0.5f) / sqrt(float(sampleCount));
60
62
  float theta = float(sampleIndex) * goldenAngle + angle;
@@ -76,8 +78,8 @@ float findBlocker(sampler2D shadowMap, vec2 uv, float compare, float angle) {
76
78
 
77
79
  #pragma unroll_loop_start
78
80
  for(int i = 0; i < ${samples}; i ++) {
79
- offset = (vogelDiskSample(j, ${samples}, angle) * texelSize) * 2.0 * PENUMBRA_FILTER_SIZE;
80
- depth = unpackRGBAToDepth( texture2D( shadowMap, uv + offset));
81
+ offset = (pcssVogelDiskSample(j, ${samples}, angle) * texelSize) * 2.0 * PENUMBRA_FILTER_SIZE;
82
+ depth = ${depthSample};
81
83
  if (depth < compare) {
82
84
  blockerDepthSum += depth;
83
85
  blockers++;
@@ -101,9 +103,9 @@ float vogelFilter(sampler2D shadowMap, vec2 uv, float zReceiver, float filterRad
101
103
  vec2 offset = vec2(0.0);
102
104
  #pragma unroll_loop_start
103
105
  for (int i = 0; i < ${samples}; i++) {
104
- vogelSample = vogelDiskSample(j, ${samples}, angle) * texelSize;
106
+ vogelSample = pcssVogelDiskSample(j, ${samples}, angle) * texelSize;
105
107
  offset = vogelSample * (1.0 + filterRadius * float(${size}));
106
- shadow += step( zReceiver, unpackRGBAToDepth( texture2D( shadowMap, uv + offset ) ) );
108
+ shadow += step( zReceiver, ${depthSample} );
107
109
  j++;
108
110
  }
109
111
  #pragma unroll_loop_end
@@ -121,11 +123,16 @@ float PCSS (sampler2D shadowMap, vec4 coords) {
121
123
  float penumbraRatio = penumbraSize(zReceiver, avgBlockerDepth);
122
124
  return vogelFilter(shadowMap, uv, zReceiver, 1.25 * penumbraRatio, angle);
123
125
  }`;
126
+ };
124
127
  function reset(gl, scene, camera) {
125
128
  scene.traverse(object => {
126
129
  if (object.material) {
127
- gl.properties.remove(object.material);
128
- object.material.dispose == null || object.material.dispose();
130
+ const materials = Array.isArray(object.material) ? object.material : [object.material];
131
+ materials.forEach(material => {
132
+ gl.properties.remove(material);
133
+ material.dispose == null || material.dispose();
134
+ material.needsUpdate = true;
135
+ });
129
136
  }
130
137
  });
131
138
  gl.info.programs.length = 0;
@@ -141,14 +148,32 @@ function SoftShadows({
141
148
  const camera = useThree(state => state.camera);
142
149
  React.useEffect(() => {
143
150
  const original = THREE.ShaderChunk.shadowmap_pars_fragment;
144
- THREE.ShaderChunk.shadowmap_pars_fragment = THREE.ShaderChunk.shadowmap_pars_fragment.replace('#ifdef USE_SHADOWMAP', '#ifdef USE_SHADOWMAP\n' + pcss({
151
+ // PCF shadows use a comparison sampler in newer three.js versions.
152
+ // That sampler cannot expose the raw depth needed for blocker search.
153
+ const packedDepth = !original.includes('sampler2DShadow');
154
+ // In newer three.js the final sampler2D overload is the Basic shadow path.
155
+ const getShadowStart = original.lastIndexOf('float getShadow( sampler2D shadowMap');
156
+ const frustumEnd = original.indexOf('if ( frustumTest ) {', getShadowStart) + 'if ( frustumTest ) {'.length;
157
+ if (getShadowStart < 0 || frustumEnd < 'if ( frustumTest ) {'.length) {
158
+ console.warn('[SoftShadows] Could not find injection point in shadow shader');
159
+ return;
160
+ }
161
+ const originalType = gl.shadowMap.type;
162
+ // Newer three.js uses sampler2DShadow for PCF. PCSS needs the raw depth,
163
+ // which is available through the Basic shadow map's sampler2D instead.
164
+ if (!packedDepth) gl.shadowMap.type = THREE.BasicShadowMap;
165
+ const hasIntensity = original.slice(getShadowStart, frustumEnd).includes('shadowIntensity');
166
+ const pcssReturn = hasIntensity ? 'return mix( 1.0, PCSS( shadowMap, shadowCoord ), shadowIntensity );' : 'return PCSS( shadowMap, shadowCoord );';
167
+ const shader = (original.slice(0, frustumEnd) + '\n' + pcssReturn + original.slice(frustumEnd)).replace('#ifdef USE_SHADOWMAP', '#ifdef USE_SHADOWMAP\n' + pcss({
145
168
  size,
146
169
  samples,
147
170
  focus
148
- })).replace('#if defined( SHADOWMAP_TYPE_PCF )', '\nreturn PCSS(shadowMap, shadowCoord);\n#if defined( SHADOWMAP_TYPE_PCF )');
171
+ }, packedDepth));
172
+ THREE.ShaderChunk.shadowmap_pars_fragment = shader;
149
173
  reset(gl, scene, camera);
150
174
  return () => {
151
175
  THREE.ShaderChunk.shadowmap_pars_fragment = original;
176
+ gl.shadowMap.type = originalType;
152
177
  reset(gl, scene, camera);
153
178
  };
154
179
  }, [focus, size, samples]);