@react-three/postprocessing 2.14.12 → 2.15.0
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.
- package/dist/effects/LensFlare.d.ts +52 -0
- package/dist/index.cjs +1289 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1292 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -2
- /package/dist/effects/{SSR.d.ts → SSR/index.d.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
2
|
import React, { createContext, useState, useMemo, useRef, useContext, useEffect, forwardRef, useLayoutEffect, useImperativeHandle, useCallback } from "react";
|
|
3
3
|
import * as THREE from "three";
|
|
4
|
-
import { HalfFloatType, Vector3, TextureLoader, sRGBEncoding, RepeatWrapping, Uniform, Texture as Texture$1, Color, CanvasTexture, NearestFilter,
|
|
4
|
+
import { HalfFloatType, Vector3, TextureLoader, sRGBEncoding, RepeatWrapping, Uniform, Texture as Texture$1, Color, CanvasTexture, NearestFilter, ShaderChunk, Vector2, WebGLCubeRenderTarget, CubeCamera, PMREMGenerator, LinearFilter, WebGLRenderTarget, WebGLMultipleRenderTargets, Quaternion, FramebufferTexture, RGBAFormat, ShaderMaterial, Matrix3, REVISION, TangentSpaceNormalMap, GLSL3, Matrix4, VideoTexture, DataTexture, FloatType, BufferGeometry, BufferAttribute, Sphere, OrthographicCamera, NoColorSpace, RedFormat, Mesh } from "three";
|
|
5
5
|
import { useThree, useFrame, useInstanceHandle, extend, createPortal, useLoader, applyProps } from "@react-three/fiber";
|
|
6
|
-
import { EffectComposer as EffectComposer$1, RenderPass, NormalPass, DepthDownsamplingPass, Effect, EffectPass, Pass, DepthOfFieldEffect, MaskFunction, DepthPickingPass, CopyPass, BlendFunction, BloomEffect, BrightnessContrastEffect, ChromaticAberrationEffect, ColorAverageEffect, ColorDepthEffect, DepthEffect, DotScreenEffect, GlitchEffect, GlitchMode, GodRaysEffect, GridEffect, HueSaturationEffect, NoiseEffect, OutlineEffect, PixelationEffect, ScanlineEffect, SelectiveBloomEffect, SepiaEffect, SSAOEffect, SMAAEffect, FXAAEffect, TextureEffect, ToneMappingEffect, VignetteEffect, ShockWaveEffect, LUT3DEffect, TiltShiftEffect as TiltShiftEffect$1, EffectAttribute } from "postprocessing";
|
|
7
|
-
import { isWebGL2Available } from "three-stdlib";
|
|
6
|
+
import { EffectComposer as EffectComposer$1, RenderPass, NormalPass, DepthDownsamplingPass, Effect, EffectPass, Pass, DepthOfFieldEffect, MaskFunction, DepthPickingPass, CopyPass, BlendFunction, BloomEffect, BrightnessContrastEffect, ChromaticAberrationEffect, ColorAverageEffect, ColorDepthEffect, DepthEffect, DotScreenEffect, GlitchEffect, GlitchMode, GodRaysEffect, GridEffect, HueSaturationEffect, NoiseEffect, OutlineEffect, PixelationEffect, ScanlineEffect, SelectiveBloomEffect, SepiaEffect, SSAOEffect, SMAAEffect, FXAAEffect, TextureEffect, ToneMappingEffect, VignetteEffect, ShockWaveEffect, LUT3DEffect, TiltShiftEffect as TiltShiftEffect$1, EffectAttribute, Selection as Selection$1, DepthPass } from "postprocessing";
|
|
7
|
+
import { isWebGL2Available as isWebGL2Available$1 } from "three-stdlib";
|
|
8
8
|
import { easing } from "maath";
|
|
9
|
-
import { SSREffect } from "screen-space-reflections";
|
|
10
9
|
import "three-stdlib";
|
|
11
10
|
const selectionContext = createContext(null);
|
|
12
11
|
function Selection({ children, enabled = true }) {
|
|
@@ -57,7 +56,7 @@ const EffectComposer = React.memo(
|
|
|
57
56
|
const scene = _scene || defaultScene;
|
|
58
57
|
const camera = _camera || defaultCamera;
|
|
59
58
|
const [composer, normalPass, downSamplingPass] = useMemo(() => {
|
|
60
|
-
const webGL2Available = isWebGL2Available();
|
|
59
|
+
const webGL2Available = isWebGL2Available$1();
|
|
61
60
|
const effectComposer = new EffectComposer$1(gl, {
|
|
62
61
|
depthBuffer,
|
|
63
62
|
stencilBuffer,
|
|
@@ -306,6 +305,166 @@ const Autofocus = forwardRef(
|
|
|
306
305
|
] });
|
|
307
306
|
}
|
|
308
307
|
);
|
|
308
|
+
const LensFlareShader = {
|
|
309
|
+
fragmentShader: (
|
|
310
|
+
/* glsl */
|
|
311
|
+
`
|
|
312
|
+
|
|
313
|
+
uniform float iTime;
|
|
314
|
+
uniform vec2 lensPosition;
|
|
315
|
+
uniform vec2 iResolution;
|
|
316
|
+
uniform vec3 colorGain;
|
|
317
|
+
uniform float starPoints;
|
|
318
|
+
uniform float glareSize;
|
|
319
|
+
uniform float flareSize;
|
|
320
|
+
uniform float flareSpeed;
|
|
321
|
+
uniform float flareShape;
|
|
322
|
+
uniform float haloScale;
|
|
323
|
+
uniform float opacity;
|
|
324
|
+
uniform bool animated;
|
|
325
|
+
uniform bool anamorphic;
|
|
326
|
+
uniform bool enabled;
|
|
327
|
+
uniform bool secondaryGhosts;
|
|
328
|
+
uniform bool starBurst;
|
|
329
|
+
uniform float ghostScale;
|
|
330
|
+
uniform bool aditionalStreaks;
|
|
331
|
+
uniform sampler2D lensDirtTexture;
|
|
332
|
+
vec2 vxtC;
|
|
333
|
+
|
|
334
|
+
float rndf(float n){return fract(sin(n) * 43758.5453123);}float niz(float p){float fl = floor(p);float fc = fract(p);return mix(rndf(fl),rndf(fl + 1.0), fc);}
|
|
335
|
+
vec3 hsv2rgb(vec3 c){vec4 k = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);vec3 p = abs(fract(c.xxx + k.xyz) * 6.0 - k.www);return c.z * mix(k.xxx, clamp(p - k.xxx, 0.0, 1.0), c.y);}
|
|
336
|
+
float satU(float x){return clamp(x, 0.,1.);}vec2 rtU(vec2 naz, float rtn){return vec2(cos(rtn) * naz.x + sin(rtn) * naz.y,cos(rtn) * naz.y - sin(rtn) * naz.x);}
|
|
337
|
+
vec3 drwF(vec2 p, float intensity, float rnd, float speed, int id){float flhos = (1. / 32.) * float(id) * 0.1;float lingrad = distance(vec2(0.), p);float expg = 1. / exp(lingrad * (fract(rnd) * 0.66 + 0.33));vec3 qzTg = hsv2rgb(vec3( fract( (expg * 8.) + speed * flareSpeed + flhos), pow(1.-abs(expg*2.-1.), 0.45), 20.0 * expg * intensity));float internalStarPoints;if(anamorphic){internalStarPoints = 1.0;} else{internalStarPoints = starPoints;}float ams = length(p * flareShape * sin(internalStarPoints * atan(p.x, p.y)));float kJhg = pow(1.-satU(ams), ( anamorphic ? 100. : 12.));kJhg += satU(expg-0.9) * 3.;kJhg = pow(kJhg * expg, 8. + (1.-intensity) * 5.);if(flareSpeed > 0.0){return vec3(kJhg) * qzTg;} else{return vec3(kJhg) * flareSize * 15.;}}
|
|
338
|
+
float ams2(vec3 a, vec3 b) { return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);}vec3 satU(vec3 x){return clamp(x, vec3(0.0), vec3(1.0));}
|
|
339
|
+
float glR(vec2 naz, vec2 pos, float zsi){vec2 mni;if(animated){mni = rtU(naz-pos, iTime * 0.1);} else{mni = naz-pos;}float ang = atan(mni.y, mni.x) * (anamorphic ? 1.0 : starPoints);float ams2 = length(mni);ams2 = pow(ams2, .9);float f0 = 1.0/(length(naz-pos)*(1.0/zsi*16.0)+.2);return f0+f0*(sin((ang))*.2 +.3);}
|
|
340
|
+
float sdHex(vec2 p){p = abs(p);vec2 q = vec2(p.x*2.0*0.5773503, p.y + p.x*0.5773503);return dot(step(q.xy,q.yx), 1.0-q.yx);}float fpow(float x, float k){return x > k ? pow((x-k)/(1.0-k),2.0) : 0.0;}
|
|
341
|
+
vec3 rHx(vec2 naz, vec2 p, float s, vec3 col){naz -= p;if (abs(naz.x) < 0.2*s && abs(naz.y) < 0.2*s){return mix(vec3(0),mix(vec3(0),col,0.1 + fpow(length(naz/s),0.1)*10.0),smoothstep(0.0,0.1,sdHex(naz*20.0/s)));}return vec3(0);}
|
|
342
|
+
vec3 mLs(vec2 naz, vec2 pos){vec2 mni = naz-pos;vec2 zxMp = naz*(length(naz));float ang = atan(mni.x,mni.y);float f0 = .3/(length(naz-pos)*16.0+1.0);f0 = f0*(sin(niz(sin(ang*3.9-(animated ? iTime : 0.0) * 0.3) * starPoints))*.2 );float f1 = max(0.01-pow(length(naz+1.2*pos),1.9),.0)*7.0;float f2 = max(.9/(10.0+32.0*pow(length(zxMp+0.99*pos),2.0)),.0)*0.35;float f22 = max(.9/(11.0+32.0*pow(length(zxMp+0.85*pos),2.0)),.0)*0.23;float f23 = max(.9/(12.0+32.0*pow(length(zxMp+0.95*pos),2.0)),.0)*0.6;vec2 ztX = mix(naz,zxMp, 0.1);float f4 = max(0.01-pow(length(ztX+0.4*pos),2.9),.0)*4.02;float f42 = max(0.0-pow(length(ztX+0.45*pos),2.9),.0)*4.1;float f43 = max(0.01-pow(length(ztX+0.5*pos),2.9),.0)*4.6;ztX = mix(naz,zxMp,-.4);float f5 = max(0.01-pow(length(ztX+0.1*pos),5.5),.0)*2.0;float f52 = max(0.01-pow(length(ztX+0.2*pos),5.5),.0)*2.0;float f53 = max(0.01-pow(length(ztX+0.1*pos),5.5),.0)*2.0;ztX = mix(naz,zxMp, 2.1);float f6 = max(0.01-pow(length(ztX-0.3*pos),1.61),.0)*3.159;float f62 = max(0.01-pow(length(ztX-0.325*pos),1.614),.0)*3.14;float f63 = max(0.01-pow(length(ztX-0.389*pos),1.623),.0)*3.12;vec3 c = vec3(glR(naz,pos, glareSize));vec2 prot;if(animated){prot = rtU(naz - pos, (iTime * 0.1));} else if(anamorphic){prot = rtU(naz - pos, 1.570796);} else {prot = naz - pos;}c += drwF(prot, (anamorphic ? flareSize * 10. : flareSize), 0.1, iTime, 1);c.r+=f1+f2+f4+f5+f6; c.g+=f1+f22+f42+f52+f62; c.b+=f1+f23+f43+f53+f63;c = c*1.3 * vec3(length(zxMp)+.09);c+=vec3(f0);return c;}
|
|
343
|
+
vec3 cc(vec3 clr, float fct,float fct2){float w = clr.x+clr.y+clr.z;return mix(clr,vec3(w)*fct,w*fct2);}float rnd(vec2 p){float f = fract(sin(dot(p, vec2(12.1234, 72.8392) )*45123.2));return f;}float rnd(float w){float f = fract(sin(w)*1000.);return f;}
|
|
344
|
+
float rShp(vec2 p, int N){float f;float a=atan(p.x,p.y)+.2;float b=6.28319/float(N);f=smoothstep(.5,.51, cos(floor(.5+a/b)*b-a)*length(p.xy)* 2.0 -ghostScale);return f;}
|
|
345
|
+
vec3 drC(vec2 p, float zsi, float dCy, vec3 clr, vec3 clr2, float ams2, vec2 esom){float l = length(p + esom*(ams2*2.))+zsi/2.;float l2 = length(p + esom*(ams2*4.))+zsi/3.;float c = max(0.01-pow(length(p + esom*ams2), zsi*ghostScale), 0.0)*10.;float c1 = max(0.001-pow(l-0.3, 1./40.)+sin(l*20.), 0.0)*3.;float c2 = max(0.09/pow(length(p-esom*ams2/.5)*1., .95), 0.0)/20.;float s = max(0.02-pow(rShp(p*5. + esom*ams2*5. + dCy, 6) , 1.), 0.0)*1.5;clr = cos(vec3(0.44, .24, .2)*8. + ams2*4.)*0.5+.5;vec3 f = c*clr;f += c1*clr;f += c2*clr;f += s*clr;return f-0.01;}
|
|
346
|
+
vec4 geLC(float x){return vec4(vec3(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(mix(vec3(0., 0., 0.),vec3(0., 0., 0.), smoothstep(0.0, 0.063, x)),vec3(0., 0., 0.), smoothstep(0.063, 0.125, x)),vec3(0.0, 0., 0.), smoothstep(0.125, 0.188, x)),vec3(0.188, 0.131, 0.116), smoothstep(0.188, 0.227, x)),vec3(0.31, 0.204, 0.537), smoothstep(0.227, 0.251, x)),vec3(0.192, 0.106, 0.286), smoothstep(0.251, 0.314, x)),vec3(0.102, 0.008, 0.341), smoothstep(0.314, 0.392, x)),vec3(0.086, 0.0, 0.141), smoothstep(0.392, 0.502, x)),vec3(1.0, 0.31, 0.0), smoothstep(0.502, 0.604, x)),vec3(.1, 0.1, 0.1), smoothstep(0.604, 0.643, x)),vec3(1.0, 0.929, 0.0), smoothstep(0.643, 0.761, x)),vec3(1.0, 0.086, 0.424), smoothstep(0.761, 0.847, x)),vec3(1.0, 0.49, 0.0), smoothstep(0.847, 0.89, x)),vec3(0.945, 0.275, 0.475), smoothstep(0.89, 0.941, x)),vec3(0.251, 0.275, 0.796), smoothstep(0.941, 1.0, x))),1.0);}
|
|
347
|
+
float diTN(vec2 p){vec2 f = fract(p);f = (f * f) * (3.0 - (2.0 * f));float n = dot(floor(p), vec2(1.0, 157.0));vec4 a = fract(sin(vec4(n + 0.0, n + 1.0, n + 157.0, n + 158.0)) * 43758.5453123);return mix(mix(a.x, a.y, f.x), mix(a.z, a.w, f.x), f.y);}
|
|
348
|
+
float fbm(vec2 p){const mat2 m = mat2(0.80, -0.60, 0.60, 0.80);float f = 0.0;f += 0.5000*diTN(p); p = m*p*2.02;f += 0.2500*diTN(p); p = m*p*2.03;f += 0.1250*diTN(p); p = m*p*2.01;f += 0.0625*diTN(p);return f/0.9375;}
|
|
349
|
+
vec4 geLS(vec2 p){vec2 pp = (p - vec2(0.5)) * 2.0;float a = atan(pp.y, pp.x);vec4 cp = vec4(sin(a * 1.0), length(pp), sin(a * 13.0), sin(a * 53.0));float d = sin(clamp(pow(length(vec2(0.5) - p) * 0.5 + haloScale /2., 5.0), 0.0, 1.0) * 3.14159);vec3 c = vec3(d) * vec3(fbm(cp.xy * 16.0) * fbm(cp.zw * 9.0) * max(max(max(max(0.5, sin(a * 1.0)), sin(a * 3.0) * 0.8), sin(a * 7.0) * 0.8), sin(a * 9.0) * 10.6));c *= vec3(mix(2.0, (sin(length(pp.xy) * 256.0) * 0.5) + 0.5, sin((clamp((length(pp.xy) - 0.875) / 0.1, 0.0, 1.0) + 0.0) * 2.0 * 3.14159) * 1.5) + 0.5) * 0.3275;return vec4(vec3(c * 1.0), d);}
|
|
350
|
+
vec4 geLD(vec2 p){p.xy += vec2(fbm(p.yx * 3.0), fbm(p.yx * 2.0)) * 0.0825;vec3 o = vec3(mix(0.125, 0.25, max(max(smoothstep(0.1, 0.0, length(p - vec2(0.25))),smoothstep(0.4, 0.0, length(p - vec2(0.75)))),smoothstep(0.8, 0.0, length(p - vec2(0.875, 0.125))))));o += vec3(max(fbm(p * 1.0) - 0.5, 0.0)) * 0.5;o += vec3(max(fbm(p * 2.0) - 0.5, 0.0)) * 0.5;o += vec3(max(fbm(p * 4.0) - 0.5, 0.0)) * 0.25;o += vec3(max(fbm(p * 8.0) - 0.75, 0.0)) * 1.0;o += vec3(max(fbm(p * 16.0) - 0.75, 0.0)) * 0.75;o += vec3(max(fbm(p * 64.0) - 0.75, 0.0)) * 0.5;return vec4(clamp(o, vec3(0.15), vec3(1.0)), 1.0);}
|
|
351
|
+
vec4 txL(sampler2D tex, vec2 xtC){if(((xtC.x < 0.) || (xtC.y < 0.)) || ((xtC.x > 1.) || (xtC.y > 1.))){return vec4(0.0);}else{return texture(tex, xtC); }}
|
|
352
|
+
vec4 txD(sampler2D tex, vec2 xtC, vec2 dir, vec3 ditn) {return vec4(txL(tex, (xtC + (dir * ditn.r))).r,txL(tex, (xtC + (dir * ditn.g))).g,txL(tex, (xtC + (dir * ditn.b))).b,1.0);}
|
|
353
|
+
vec4 strB(){vec2 aspXtc = vec2(1.0) - (((vxtC - vec2(0.5)) * vec2(1.0)) + vec2(0.5)); vec2 xtC = vec2(1.0) - vxtC; vec2 ghvc = (vec2(0.5) - xtC) * 0.3 - lensPosition; vec2 ghNm = normalize(ghvc * vec2(1.0)) * vec2(1.0);vec2 haloVec = normalize(ghvc) * 0.6;vec2 hlNm = ghNm * 0.6;vec2 texelSize = vec2(1.0) / vec2(iResolution.xy);vec3 ditn = vec3(-(texelSize.x * 1.5), 0.2, texelSize.x * 1.5);vec4 c = vec4(0.0);for (int i = 0; i < 8; i++) {vec2 offset = xtC + (ghvc * float(i));c += txD(lensDirtTexture, offset, ghNm, ditn) * pow(max(0.0, 1.0 - (length(vec2(0.5) - offset) / length(vec2(0.5)))), 10.0);}vec2 uyTrz = xtC + hlNm; return (c * geLC((length(vec2(0.5) - aspXtc) / length(vec2(haloScale))))) +(txD(lensDirtTexture, uyTrz, ghNm, ditn) * pow(max(0.0, 1.0 - (length(vec2(0.5) - uyTrz) / length(vec2(0.5)))), 10.0));}
|
|
354
|
+
void mainImage(vec4 v,vec2 r,out vec4 i){vec2 g=r-.5;g.y*=iResolution.y/iResolution.x;vec2 l=lensPosition*.5;l.y*=iResolution.y/iResolution.x;vec3 f=mLs(g,l)*20.*colorGain/256.;if(aditionalStreaks){vec3 o=vec3(.9,.2,.1),p=vec3(.3,.1,.9);for(float n=0.;n<10.;n++)f+=drC(g,pow(rnd(n*2e3)*2.8,.1)+1.41,0.,o+n,p+n,rnd(n*20.)*3.+.2-.5,lensPosition);}if(secondaryGhosts){vec3 n=vec3(0);n+=rHx(g,-lensPosition*.25,ghostScale*1.4,vec3(.25,.35,0));n+=rHx(g,lensPosition*.25,ghostScale*.5,vec3(1,.5,.5));n+=rHx(g,lensPosition*.1,ghostScale*1.6,vec3(1));n+=rHx(g,lensPosition*1.8,ghostScale*2.,vec3(0,.5,.75));n+=rHx(g,lensPosition*1.25,ghostScale*.8,vec3(1,1,.5));n+=rHx(g,-lensPosition*1.25,ghostScale*5.,vec3(.5,.5,.25));n+=fpow(1.-abs(distance(lensPosition*.8,g)-.7),.985)*colorGain/2100.;f+=n;}if(starBurst){vxtC=g+.5;vec4 n=geLD(g);float o=1.-clamp(0.5,0.,.5)*2.;n+=mix(n,pow(n*2.,vec4(2))*.5,o);float s=(g.x+g.y)*(1./6.);vec2 d=mat2(cos(s),-sin(s),sin(s),cos(s))*vxtC;n+=geLS(d)*2.;f+=clamp(n.xyz*strB().xyz,.01,1.);}i=enabled?vec4(mix(f,vec3(0),opacity)+v.xyz,v.w):vec4(v);}
|
|
355
|
+
`
|
|
356
|
+
)
|
|
357
|
+
};
|
|
358
|
+
class LensFlareEffect extends Effect {
|
|
359
|
+
constructor({
|
|
360
|
+
blendFunction = BlendFunction.NORMAL,
|
|
361
|
+
enabled = true,
|
|
362
|
+
glareSize = 0.2,
|
|
363
|
+
lensPosition = [0.01, 0.01],
|
|
364
|
+
iResolution = [0, 0],
|
|
365
|
+
starPoints = 6,
|
|
366
|
+
flareSize = 0.01,
|
|
367
|
+
flareSpeed = 0.01,
|
|
368
|
+
flareShape = 0.01,
|
|
369
|
+
animated = true,
|
|
370
|
+
anamorphic = false,
|
|
371
|
+
colorGain = new THREE.Color(20, 20, 20),
|
|
372
|
+
lensDirtTexture = null,
|
|
373
|
+
haloScale = 0.5,
|
|
374
|
+
secondaryGhosts = true,
|
|
375
|
+
aditionalStreaks = true,
|
|
376
|
+
ghostScale = 0,
|
|
377
|
+
opacity = 1,
|
|
378
|
+
starBurst = false
|
|
379
|
+
} = {}) {
|
|
380
|
+
super("LensFlareEffect", LensFlareShader.fragmentShader, {
|
|
381
|
+
blendFunction,
|
|
382
|
+
uniforms: /* @__PURE__ */ new Map([
|
|
383
|
+
["enabled", new THREE.Uniform(enabled)],
|
|
384
|
+
["glareSize", new THREE.Uniform(glareSize)],
|
|
385
|
+
["lensPosition", new THREE.Uniform(lensPosition)],
|
|
386
|
+
["iTime", new THREE.Uniform(0)],
|
|
387
|
+
["iResolution", new THREE.Uniform(iResolution)],
|
|
388
|
+
["starPoints", new THREE.Uniform(starPoints)],
|
|
389
|
+
["flareSize", new THREE.Uniform(flareSize)],
|
|
390
|
+
["flareSpeed", new THREE.Uniform(flareSpeed)],
|
|
391
|
+
["flareShape", new THREE.Uniform(flareShape)],
|
|
392
|
+
["animated", new THREE.Uniform(animated)],
|
|
393
|
+
["anamorphic", new THREE.Uniform(anamorphic)],
|
|
394
|
+
["colorGain", new THREE.Uniform(colorGain)],
|
|
395
|
+
["lensDirtTexture", new THREE.Uniform(lensDirtTexture)],
|
|
396
|
+
["haloScale", new THREE.Uniform(haloScale)],
|
|
397
|
+
["secondaryGhosts", new THREE.Uniform(secondaryGhosts)],
|
|
398
|
+
["aditionalStreaks", new THREE.Uniform(aditionalStreaks)],
|
|
399
|
+
["ghostScale", new THREE.Uniform(ghostScale)],
|
|
400
|
+
["starBurst", new THREE.Uniform(starBurst)],
|
|
401
|
+
["opacity", new THREE.Uniform(opacity)]
|
|
402
|
+
])
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
update(_renderer, _inputBuffer, deltaTime) {
|
|
406
|
+
const iTime = this.uniforms.get("iTime");
|
|
407
|
+
if (iTime) {
|
|
408
|
+
iTime.value += deltaTime;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
const LensFlare = forwardRef(
|
|
413
|
+
({ position = new THREE.Vector3(-25, 6, -60), followMouse = false, smoothTime = 0.07, ...props }, ref) => {
|
|
414
|
+
const viewport = useThree(({ viewport: viewport2 }) => viewport2);
|
|
415
|
+
const raycaster = useThree(({ raycaster: raycaster2 }) => raycaster2);
|
|
416
|
+
const pointer = useThree(({ pointer: pointer2 }) => pointer2);
|
|
417
|
+
const { scene, camera } = useContext(EffectComposerContext);
|
|
418
|
+
const [projectedPosition] = useState(() => new THREE.Vector3());
|
|
419
|
+
const [mouse2d] = useState(() => new THREE.Vector2());
|
|
420
|
+
const effect = useMemo(() => new LensFlareEffect(props), [props]);
|
|
421
|
+
useFrame((_, delta) => {
|
|
422
|
+
var _a, _b, _c;
|
|
423
|
+
const uLensPosition = effect.uniforms.get("lensPosition");
|
|
424
|
+
const uOpacity = effect.uniforms.get("opacity");
|
|
425
|
+
if (!uLensPosition || !uOpacity)
|
|
426
|
+
return;
|
|
427
|
+
let target = 1;
|
|
428
|
+
if (followMouse) {
|
|
429
|
+
uLensPosition.value.x = pointer.x;
|
|
430
|
+
uLensPosition.value.y = pointer.y;
|
|
431
|
+
target = 0;
|
|
432
|
+
} else {
|
|
433
|
+
projectedPosition.copy(position).project(camera);
|
|
434
|
+
if (projectedPosition.z > 1)
|
|
435
|
+
return;
|
|
436
|
+
uLensPosition.value.x = projectedPosition.x;
|
|
437
|
+
uLensPosition.value.y = projectedPosition.y;
|
|
438
|
+
mouse2d.set(projectedPosition.x, projectedPosition.y);
|
|
439
|
+
raycaster.setFromCamera(mouse2d, camera);
|
|
440
|
+
const intersects = raycaster.intersectObjects(scene.children, true);
|
|
441
|
+
const { object } = intersects[0];
|
|
442
|
+
if (object) {
|
|
443
|
+
if (((_a = object.userData) == null ? void 0 : _a.lensflare) === "no-occlusion") {
|
|
444
|
+
target = 0;
|
|
445
|
+
} else if (object instanceof THREE.Mesh) {
|
|
446
|
+
if (((_c = (_b = object.material.uniforms) == null ? void 0 : _b._transmission) == null ? void 0 : _c.value) > 0.2) {
|
|
447
|
+
target = 0.2;
|
|
448
|
+
} else if (object.material._transmission && object.material._transmission > 0.2) {
|
|
449
|
+
target = 0.2;
|
|
450
|
+
} else if (object.material.transparent) {
|
|
451
|
+
target = object.material.opacity;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
easing.damp(uOpacity, "value", target, smoothTime, delta);
|
|
457
|
+
});
|
|
458
|
+
useEffect(() => {
|
|
459
|
+
const iResolution = effect.uniforms.get("iResolution");
|
|
460
|
+
if (iResolution) {
|
|
461
|
+
iResolution.value.x = viewport.width;
|
|
462
|
+
iResolution.value.y = viewport.height;
|
|
463
|
+
}
|
|
464
|
+
}, [effect, viewport]);
|
|
465
|
+
return /* @__PURE__ */ jsx("primitive", { ref, object: effect, dispose: null });
|
|
466
|
+
}
|
|
467
|
+
);
|
|
309
468
|
const Bloom = wrapEffect(BloomEffect, {
|
|
310
469
|
blendFunction: BlendFunction.ADD
|
|
311
470
|
});
|
|
@@ -764,6 +923,1132 @@ const ASCII = forwardRef(
|
|
|
764
923
|
return /* @__PURE__ */ jsx("primitive", { ref: fref, object: effect });
|
|
765
924
|
}
|
|
766
925
|
);
|
|
926
|
+
var boxBlur = "#define GLSLIFY 1\nuniform float blur;uniform float blurSharpness;uniform int blurKernel;vec3 denoise(vec3 center,sampler2D tex,vec2 uv,vec2 invTexSize,float blur,float blurSharpness,int blurKernel){vec3 color;float total;vec3 col;float weight;for(int x=-blurKernel;x<=blurKernel;x++){for(int y=-blurKernel;y<=blurKernel;y++){col=textureLod(tex,uv+vec2(x,y)*invTexSize,0.).rgb;weight=1.0-abs(dot(col-center,vec3(0.25)));weight=pow(weight,blurSharpness);color+=col*weight;total+=weight;}}return color/total;}";
|
|
927
|
+
var finalSSRShader = "#define GLSLIFY 1\n#define MODE_DEFAULT 0\n#define MODE_REFLECTIONS 1\n#define MODE_RAW_REFLECTION 2\n#define MODE_BLURRED_REFLECTIONS 3\n#define MODE_INPUT 4\n#define MODE_BLUR_MIX 5\n#define FLOAT_EPSILON 0.00001\nuniform sampler2D inputTexture;uniform sampler2D reflectionsTexture;uniform float samples;\n#include <boxBlur>\nvoid mainImage(const in vec4 inputColor,const in vec2 uv,out vec4 outputColor){vec4 reflectionsTexel=texture2D(reflectionsTexture,vUv);ivec2 size=textureSize(reflectionsTexture,0);vec2 invTexSize=1./vec2(size.x,size.y);vec3 reflectionClr=reflectionsTexel.xyz;if(blur>FLOAT_EPSILON){vec3 blurredReflectionsColor=denoise(reflectionsTexel.rgb,reflectionsTexture,vUv,invTexSize,blur,blurSharpness,blurKernel);reflectionClr=mix(reflectionClr,blurredReflectionsColor.rgb,blur);}\n#if RENDER_MODE == MODE_DEFAULT\noutputColor=vec4(inputColor.rgb+reflectionClr,1.0);\n#endif\n#if RENDER_MODE == MODE_REFLECTIONS\noutputColor=vec4(reflectionClr,1.0);\n#endif\n#if RENDER_MODE == MODE_RAW_REFLECTION\noutputColor=vec4(reflectionsTexel.xyz,1.0);\n#endif\n#if RENDER_MODE == MODE_BLURRED_REFLECTIONS\noutputColor=vec4(blurredReflectionsTexel.xyz,1.0);\n#endif\n#if RENDER_MODE == MODE_INPUT\noutputColor=vec4(inputColor.xyz,1.0);\n#endif\n#if RENDER_MODE == MODE_BLUR_MIX\noutputColor=vec4(vec3(blur),1.0);\n#endif\n}";
|
|
928
|
+
var helperFunctions = "#define GLSLIFY 1\nvec3 getViewPosition(const float depth){float clipW=_projectionMatrix[2][3]*depth+_projectionMatrix[3][3];vec4 clipPosition=vec4((vec3(vUv,depth)-0.5)*2.0,1.0);clipPosition*=clipW;return(_inverseProjectionMatrix*clipPosition).xyz;}float getViewZ(const in float depth){\n#ifdef PERSPECTIVE_CAMERA\nreturn perspectiveDepthToViewZ(depth,cameraNear,cameraFar);\n#else\nreturn orthographicDepthToViewZ(depth,cameraNear,cameraFar);\n#endif\n}vec3 screenSpaceToWorldSpace(const vec2 uv,const float depth){vec4 ndc=vec4((uv.x-0.5)*2.0,(uv.y-0.5)*2.0,(depth-0.5)*2.0,1.0);vec4 clip=_inverseProjectionMatrix*ndc;vec4 view=cameraMatrixWorld*(clip/clip.w);return view.xyz;}\n#define Scale (vec3(0.8, 0.8, 0.8))\n#define K (19.19)\nvec3 hash(vec3 a){a=fract(a*Scale);a+=dot(a,a.yxz+K);return fract((a.xxy+a.yxx)*a.zyx);}float fresnel_dielectric_cos(float cosi,float eta){float c=abs(cosi);float g=eta*eta-1.0+c*c;float result;if(g>0.0){g=sqrt(g);float A=(g-c)/(g+c);float B=(c*(g+c)-1.0)/(c*(g-c)+1.0);result=0.5*A*A*(1.0+B*B);}else{result=1.0;}return result;}float fresnel_dielectric(vec3 Incoming,vec3 Normal,float eta){float cosine=dot(Incoming,Normal);return min(1.0,5.0*fresnel_dielectric_cos(cosine,eta));}";
|
|
929
|
+
var trCompose = "#define GLSLIFY 1\n#define INV_EULER 0.36787944117144233\nalpha=velocityDisocclusion<FLOAT_EPSILON ?(alpha+0.0075): 0.0;alpha=clamp(alpha,0.0,1.0);bool needsBlur=!didReproject||velocityDisocclusion>0.5;\n#ifdef boxBlur\nif(needsBlur)inputColor=boxBlurredColor;\n#endif\nif(alpha==1.0){outputColor=accumulatedColor;}else{float m=mix(alpha,1.0,blend);if(needsBlur)m=0.0;outputColor=accumulatedColor*m+inputColor*(1.0-m);}";
|
|
930
|
+
class MRTMaterial extends ShaderMaterial {
|
|
931
|
+
constructor() {
|
|
932
|
+
super({
|
|
933
|
+
type: "MRTMaterial",
|
|
934
|
+
defines: {
|
|
935
|
+
USE_UV: "",
|
|
936
|
+
TEMPORAL_RESOLVE: ""
|
|
937
|
+
},
|
|
938
|
+
uniforms: {
|
|
939
|
+
opacity: new Uniform(1),
|
|
940
|
+
normalMap: new Uniform(null),
|
|
941
|
+
normalScale: new Uniform(new Vector2(1, 1)),
|
|
942
|
+
uvTransform: new Uniform(new Matrix3()),
|
|
943
|
+
roughness: new Uniform(1),
|
|
944
|
+
roughnessMap: new Uniform(null)
|
|
945
|
+
},
|
|
946
|
+
vertexShader: (
|
|
947
|
+
/* glsl */
|
|
948
|
+
`
|
|
949
|
+
#ifdef USE_MRT
|
|
950
|
+
varying vec2 vHighPrecisionZW;
|
|
951
|
+
#endif
|
|
952
|
+
#define NORMAL
|
|
953
|
+
#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )
|
|
954
|
+
varying vec3 vViewPosition;
|
|
955
|
+
#endif
|
|
956
|
+
#include <common>
|
|
957
|
+
#include <uv_pars_vertex>
|
|
958
|
+
#include <displacementmap_pars_vertex>
|
|
959
|
+
#include <normal_pars_vertex>
|
|
960
|
+
#include <morphtarget_pars_vertex>
|
|
961
|
+
#include <skinning_pars_vertex>
|
|
962
|
+
#include <logdepthbuf_pars_vertex>
|
|
963
|
+
#include <clipping_planes_pars_vertex>
|
|
964
|
+
#ifdef USE_UV
|
|
965
|
+
${REVISION.replace(/\D+/g, "") >= 151 ? "uniform mat3 uvTransform;" : ""}
|
|
966
|
+
#endif
|
|
967
|
+
void main() {
|
|
968
|
+
#include <uv_vertex>
|
|
969
|
+
#include <beginnormal_vertex>
|
|
970
|
+
#include <morphnormal_vertex>
|
|
971
|
+
#include <skinbase_vertex>
|
|
972
|
+
#include <skinnormal_vertex>
|
|
973
|
+
#include <defaultnormal_vertex>
|
|
974
|
+
#include <normal_vertex>
|
|
975
|
+
#include <begin_vertex>
|
|
976
|
+
#include <morphtarget_vertex>
|
|
977
|
+
#include <skinning_vertex>
|
|
978
|
+
#include <displacementmap_vertex>
|
|
979
|
+
#include <project_vertex>
|
|
980
|
+
#include <logdepthbuf_vertex>
|
|
981
|
+
#include <clipping_planes_vertex>
|
|
982
|
+
#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )
|
|
983
|
+
vViewPosition = - mvPosition.xyz;
|
|
984
|
+
#endif
|
|
985
|
+
#ifdef USE_MRT
|
|
986
|
+
vHighPrecisionZW = gl_Position.zw;
|
|
987
|
+
#endif
|
|
988
|
+
#ifdef USE_UV
|
|
989
|
+
vUv = ( uvTransform * vec3( uv, 1 ) ).xy;
|
|
990
|
+
#endif
|
|
991
|
+
}
|
|
992
|
+
`
|
|
993
|
+
),
|
|
994
|
+
fragmentShader: (
|
|
995
|
+
/* glsl */
|
|
996
|
+
`
|
|
997
|
+
#define NORMAL
|
|
998
|
+
#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )
|
|
999
|
+
varying vec3 vViewPosition;
|
|
1000
|
+
#endif
|
|
1001
|
+
#include <packing>
|
|
1002
|
+
#include <uv_pars_fragment>
|
|
1003
|
+
#include <normal_pars_fragment>
|
|
1004
|
+
#include <bumpmap_pars_fragment>
|
|
1005
|
+
#include <normalmap_pars_fragment>
|
|
1006
|
+
#include <logdepthbuf_pars_fragment>
|
|
1007
|
+
#include <clipping_planes_pars_fragment>
|
|
1008
|
+
#include <roughnessmap_pars_fragment>
|
|
1009
|
+
|
|
1010
|
+
#ifdef USE_MRT
|
|
1011
|
+
layout(location = 0) out vec4 gNormal;
|
|
1012
|
+
layout(location = 1) out vec4 gDepth;
|
|
1013
|
+
|
|
1014
|
+
varying vec2 vHighPrecisionZW;
|
|
1015
|
+
#endif
|
|
1016
|
+
uniform float roughness;
|
|
1017
|
+
void main() {
|
|
1018
|
+
#include <clipping_planes_fragment>
|
|
1019
|
+
#include <logdepthbuf_fragment>
|
|
1020
|
+
#include <normal_fragment_begin>
|
|
1021
|
+
#include <normal_fragment_maps>
|
|
1022
|
+
|
|
1023
|
+
float roughnessFactor = roughness;
|
|
1024
|
+
|
|
1025
|
+
if(roughness > 10.0e9){
|
|
1026
|
+
roughnessFactor = 1.;
|
|
1027
|
+
}else{
|
|
1028
|
+
#ifdef useRoughnessMap
|
|
1029
|
+
vec4 texelRoughness = texture2D( roughnessMap, vUv );
|
|
1030
|
+
// reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
|
|
1031
|
+
roughnessFactor *= texelRoughness.g;
|
|
1032
|
+
#endif
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
vec3 normalColor = packNormalToRGB( normal );
|
|
1036
|
+
#ifdef USE_MRT
|
|
1037
|
+
float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
|
|
1038
|
+
vec4 depthColor = packDepthToRGBA( fragCoordZ );
|
|
1039
|
+
gNormal = vec4( normalColor, roughnessFactor );
|
|
1040
|
+
gDepth = depthColor;
|
|
1041
|
+
#else
|
|
1042
|
+
gl_FragColor = vec4(normalColor, roughnessFactor);
|
|
1043
|
+
#endif
|
|
1044
|
+
}
|
|
1045
|
+
`
|
|
1046
|
+
),
|
|
1047
|
+
toneMapped: false
|
|
1048
|
+
});
|
|
1049
|
+
this.normalMapType = TangentSpaceNormalMap;
|
|
1050
|
+
this.normalScale = new Vector2(1, 1);
|
|
1051
|
+
Object.defineProperty(this, "glslVersion", {
|
|
1052
|
+
get() {
|
|
1053
|
+
return "USE_MRT" in this.defines ? GLSL3 : null;
|
|
1054
|
+
},
|
|
1055
|
+
set(_) {
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
var vertexShader$1 = "#define GLSLIFY 1\nvarying vec2 vUv;void main(){vUv=position.xy*0.5+0.5;gl_Position=vec4(position.xy,1.0,1.0);}";
|
|
1061
|
+
var fragmentShader = "#define GLSLIFY 1\nvarying vec2 vUv;uniform sampler2D inputTexture;uniform sampler2D accumulatedTexture;uniform sampler2D normalTexture;uniform sampler2D depthTexture;uniform sampler2D envMap;uniform mat4 _projectionMatrix;uniform mat4 _inverseProjectionMatrix;uniform mat4 cameraMatrixWorld;uniform float cameraNear;uniform float cameraFar;uniform float rayDistance;uniform float intensity;uniform float maxDepthDifference;uniform float roughnessFade;uniform float maxRoughness;uniform float fade;uniform float thickness;uniform float ior;uniform float samples;uniform float jitter;uniform float jitterRoughness;\n#define INVALID_RAY_COORDS vec2(-1.0);\n#define EARLY_OUT_COLOR vec4(0.0, 0.0, 0.0, 1.0)\n#define FLOAT_EPSILON 0.00001\nfloat nearMinusFar;float nearMulFar;float farMinusNear;\n#include <packing>\n#include <helperFunctions>\nvec2 RayMarch(vec3 dir,inout vec3 hitPos,inout float rayHitDepthDifference);vec2 BinarySearch(in vec3 dir,inout vec3 hitPos,inout float rayHitDepthDifference);float fastGetViewZ(const in float depth);vec3 getIBLRadiance(const in vec3 viewDir,const in vec3 normal,const in float roughness);void main(){vec4 depthTexel=textureLod(depthTexture,vUv,0.0);if(dot(depthTexel.rgb,depthTexel.rgb)<FLOAT_EPSILON){gl_FragColor=EARLY_OUT_COLOR;return;}float unpackedDepth=unpackRGBAToDepth(depthTexel);vec4 normalTexel=textureLod(normalTexture,vUv,0.0);float roughness=normalTexel.a;float specular=1.0-roughness;nearMinusFar=cameraNear-cameraFar;nearMulFar=cameraNear*cameraFar;farMinusNear=cameraFar-cameraNear;normalTexel.rgb=unpackRGBToNormal(normalTexel.rgb);float depth=fastGetViewZ(unpackedDepth);vec3 viewPos=getViewPosition(depth);vec3 viewDir=normalize(viewPos);vec3 viewNormal=normalTexel.xyz;vec3 worldPos=screenSpaceToWorldSpace(vUv,unpackedDepth);vec3 jitt=vec3(0.0);if(jitterRoughness!=0.0||jitter!=0.0){vec3 randomJitter=hash(50.0*samples*worldPos)-0.5;float spread=((2.0-specular)+roughness*jitterRoughness);float jitterMix=jitter*0.25+jitterRoughness*roughness;if(jitterMix>1.0)jitterMix=1.0;jitt=mix(vec3(0.0),randomJitter*spread,jitterMix);}viewNormal+=jitt;float fresnelFactor=fresnel_dielectric(viewDir,viewNormal,ior);vec3 iblRadiance=getIBLRadiance(-viewDir,viewNormal,0.)*fresnelFactor;float lastFrameAlpha=textureLod(accumulatedTexture,vUv,0.0).a;if(roughness>maxRoughness||(roughness>1.0-FLOAT_EPSILON&&roughnessFade>1.0-FLOAT_EPSILON)){gl_FragColor=vec4(iblRadiance,lastFrameAlpha);return;}vec3 reflected=reflect(viewDir,viewNormal);vec3 rayDir=reflected*-viewPos.z;vec3 hitPos=viewPos;float rayHitDepthDifference;vec2 coords=RayMarch(rayDir,hitPos,rayHitDepthDifference);if(coords.x==-1.0){gl_FragColor=vec4(iblRadiance,lastFrameAlpha);return;}vec4 SSRTexel=textureLod(inputTexture,coords.xy,0.0);vec4 SSRTexelReflected=textureLod(accumulatedTexture,coords.xy,0.0);vec3 SSR=SSRTexel.rgb+SSRTexelReflected.rgb;float roughnessFactor=mix(specular,1.0,max(0.0,1.0-roughnessFade));vec2 coordsNDC=(coords.xy*2.0-1.0);float screenFade=0.1;float maxDimension=min(1.0,max(abs(coordsNDC.x),abs(coordsNDC.y)));float reflectionIntensity=1.0-(max(0.0,maxDimension-screenFade)/(1.0-screenFade));reflectionIntensity=max(0.,reflectionIntensity);vec3 finalSSR=mix(iblRadiance,SSR,reflectionIntensity)*roughnessFactor;if(fade!=0.0){vec3 hitWorldPos=screenSpaceToWorldSpace(coords,rayHitDepthDifference);float reflectionDistance=distance(hitWorldPos,worldPos)+1.0;float opacity=1.0/(reflectionDistance*fade*0.1);if(opacity>1.0)opacity=1.0;finalSSR*=opacity;}finalSSR*=fresnelFactor*intensity;finalSSR=min(vec3(1.0),finalSSR);float alpha=hitPos.z==1.0 ? 1.0 : SSRTexelReflected.a;alpha=min(lastFrameAlpha,alpha);gl_FragColor=vec4(finalSSR,alpha);}vec2 RayMarch(vec3 dir,inout vec3 hitPos,inout float rayHitDepthDifference){dir=normalize(dir);dir*=rayDistance/float(steps);float depth;vec4 projectedCoord;vec4 lastProjectedCoord;float unpackedDepth;vec4 depthTexel;for(int i=0;i<steps;i++){hitPos+=dir;projectedCoord=_projectionMatrix*vec4(hitPos,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=projectedCoord.xy*0.5+0.5;\n#ifndef missedRays\nif(projectedCoord.x<0.0||projectedCoord.x>1.0||projectedCoord.y<0.0||projectedCoord.y>1.0){return INVALID_RAY_COORDS;}\n#endif\ndepthTexel=textureLod(depthTexture,projectedCoord.xy,0.0);unpackedDepth=unpackRGBAToDepth(depthTexel);depth=fastGetViewZ(unpackedDepth);rayHitDepthDifference=depth-hitPos.z;if(rayHitDepthDifference>=0.0&&rayHitDepthDifference<thickness){\n#if refineSteps == 0\nif(dot(depthTexel.rgb,depthTexel.rgb)<FLOAT_EPSILON)return INVALID_RAY_COORDS;\n#else\nreturn BinarySearch(dir,hitPos,rayHitDepthDifference);\n#endif\n}\n#ifndef missedRays\nif(hitPos.z>0.0){return INVALID_RAY_COORDS;}\n#endif\nlastProjectedCoord=projectedCoord;}hitPos.z=1.0;\n#ifndef missedRays\nreturn INVALID_RAY_COORDS;\n#endif\nrayHitDepthDifference=unpackedDepth;return projectedCoord.xy;}vec2 BinarySearch(in vec3 dir,inout vec3 hitPos,inout float rayHitDepthDifference){float depth;vec4 projectedCoord;vec2 lastMinProjectedCoordXY;float unpackedDepth;vec4 depthTexel;for(int i=0;i<refineSteps;i++){projectedCoord=_projectionMatrix*vec4(hitPos,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=projectedCoord.xy*0.5+0.5;depthTexel=textureLod(depthTexture,projectedCoord.xy,0.0);unpackedDepth=unpackRGBAToDepth(depthTexel);depth=fastGetViewZ(unpackedDepth);rayHitDepthDifference=depth-hitPos.z;dir*=0.5;if(rayHitDepthDifference>0.0){hitPos-=dir;}else{hitPos+=dir;}}if(dot(depthTexel.rgb,depthTexel.rgb)<FLOAT_EPSILON)return INVALID_RAY_COORDS;if(abs(rayHitDepthDifference)>maxDepthDifference)return INVALID_RAY_COORDS;projectedCoord=_projectionMatrix*vec4(hitPos,1.0);projectedCoord.xy/=projectedCoord.w;projectedCoord.xy=projectedCoord.xy*0.5+0.5;rayHitDepthDifference=unpackedDepth;return projectedCoord.xy;}float fastGetViewZ(const in float depth){\n#ifdef PERSPECTIVE_CAMERA\nreturn nearMulFar/(farMinusNear*depth-cameraFar);\n#else\nreturn depth*nearMinusFar-cameraNear;\n#endif\n}\n#include <common>\n#include <cube_uv_reflection_fragment>\nvec3 getIBLRadiance(const in vec3 viewDir,const in vec3 normal,const in float roughness){\n#if defined(ENVMAP_TYPE_CUBE_UV)\nvec3 reflectVec=reflect(-viewDir,normal);reflectVec=normalize(mix(reflectVec,normal,roughness*roughness));reflectVec=inverseTransformDirection(reflectVec,viewMatrix);vec4 envMapColor=textureCubeUV(envMap,reflectVec,roughness);return envMapColor.rgb*intensity;\n#else\nreturn vec3(0.0);\n#endif\n}";
|
|
1062
|
+
class ReflectionsMaterial extends ShaderMaterial {
|
|
1063
|
+
constructor() {
|
|
1064
|
+
super({
|
|
1065
|
+
type: "ReflectionsMaterial",
|
|
1066
|
+
uniforms: {
|
|
1067
|
+
inputTexture: new Uniform(null),
|
|
1068
|
+
accumulatedTexture: new Uniform(null),
|
|
1069
|
+
normalTexture: new Uniform(null),
|
|
1070
|
+
depthTexture: new Uniform(null),
|
|
1071
|
+
_projectionMatrix: new Uniform(new Matrix4()),
|
|
1072
|
+
_inverseProjectionMatrix: new Uniform(new Matrix4()),
|
|
1073
|
+
cameraMatrixWorld: new Uniform(new Matrix4()),
|
|
1074
|
+
cameraNear: new Uniform(0),
|
|
1075
|
+
cameraFar: new Uniform(0),
|
|
1076
|
+
rayDistance: new Uniform(0),
|
|
1077
|
+
intensity: new Uniform(0),
|
|
1078
|
+
roughnessFade: new Uniform(0),
|
|
1079
|
+
fade: new Uniform(0),
|
|
1080
|
+
thickness: new Uniform(0),
|
|
1081
|
+
ior: new Uniform(0),
|
|
1082
|
+
maxDepthDifference: new Uniform(0),
|
|
1083
|
+
jitter: new Uniform(0),
|
|
1084
|
+
jitterRoughness: new Uniform(0),
|
|
1085
|
+
maxRoughness: new Uniform(0),
|
|
1086
|
+
samples: new Uniform(0),
|
|
1087
|
+
envMap: new Uniform(null),
|
|
1088
|
+
envMapPosition: new Uniform(new Vector3()),
|
|
1089
|
+
envMapSize: new Uniform(new Vector3()),
|
|
1090
|
+
viewMatrix: new Uniform(new Matrix4())
|
|
1091
|
+
},
|
|
1092
|
+
defines: {
|
|
1093
|
+
steps: 20,
|
|
1094
|
+
refineSteps: 5,
|
|
1095
|
+
CUBEUV_TEXEL_WIDTH: 0,
|
|
1096
|
+
CUBEUV_TEXEL_HEIGHT: 0,
|
|
1097
|
+
CUBEUV_MAX_MIP: 0,
|
|
1098
|
+
vWorldPosition: "worldPos"
|
|
1099
|
+
},
|
|
1100
|
+
fragmentShader: fragmentShader.replace("#include <helperFunctions>", helperFunctions),
|
|
1101
|
+
vertexShader: vertexShader$1,
|
|
1102
|
+
toneMapped: false,
|
|
1103
|
+
depthWrite: false,
|
|
1104
|
+
depthTest: false
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
const getVisibleChildren = (object) => {
|
|
1109
|
+
const queue = [object];
|
|
1110
|
+
const objects = [];
|
|
1111
|
+
while (queue.length !== 0) {
|
|
1112
|
+
const mesh = queue.shift();
|
|
1113
|
+
if (mesh.material)
|
|
1114
|
+
objects.push(mesh);
|
|
1115
|
+
for (const c of mesh.children) {
|
|
1116
|
+
if (c.visible)
|
|
1117
|
+
queue.push(c);
|
|
1118
|
+
}
|
|
1119
|
+
}
|
|
1120
|
+
return objects;
|
|
1121
|
+
};
|
|
1122
|
+
const generateCubeUVSize = (parameters) => {
|
|
1123
|
+
const imageHeight = parameters.envMapCubeUVHeight;
|
|
1124
|
+
if (imageHeight === null)
|
|
1125
|
+
return null;
|
|
1126
|
+
const maxMip = Math.log2(imageHeight) - 2;
|
|
1127
|
+
const texelHeight = 1 / imageHeight;
|
|
1128
|
+
const texelWidth = 1 / (3 * Math.max(Math.pow(2, maxMip), 7 * 16));
|
|
1129
|
+
return {
|
|
1130
|
+
texelWidth,
|
|
1131
|
+
texelHeight,
|
|
1132
|
+
maxMip
|
|
1133
|
+
};
|
|
1134
|
+
};
|
|
1135
|
+
const setupEnvMap = (reflectionsMaterial, envMap, envMapCubeUVHeight) => {
|
|
1136
|
+
reflectionsMaterial.uniforms.envMap.value = envMap;
|
|
1137
|
+
const envMapCubeUVSize = generateCubeUVSize({
|
|
1138
|
+
envMapCubeUVHeight
|
|
1139
|
+
});
|
|
1140
|
+
reflectionsMaterial.defines.ENVMAP_TYPE_CUBE_UV = "";
|
|
1141
|
+
reflectionsMaterial.defines.CUBEUV_TEXEL_WIDTH = envMapCubeUVSize.texelWidth;
|
|
1142
|
+
reflectionsMaterial.defines.CUBEUV_TEXEL_HEIGHT = envMapCubeUVSize.texelHeight;
|
|
1143
|
+
reflectionsMaterial.defines.CUBEUV_MAX_MIP = envMapCubeUVSize.maxMip + ".0";
|
|
1144
|
+
reflectionsMaterial.needsUpdate = true;
|
|
1145
|
+
};
|
|
1146
|
+
const isWebGL2Available = () => {
|
|
1147
|
+
try {
|
|
1148
|
+
const canvas = document.createElement("canvas");
|
|
1149
|
+
return !!(window.WebGL2RenderingContext && canvas.getContext("webgl2"));
|
|
1150
|
+
} catch (e) {
|
|
1151
|
+
return false;
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
class ReflectionsPass extends Pass {
|
|
1155
|
+
constructor(ssrEffect, options = {}) {
|
|
1156
|
+
super("ReflectionsPass");
|
|
1157
|
+
this.ssrEffect = void 0;
|
|
1158
|
+
this.cachedMaterials = /* @__PURE__ */ new WeakMap();
|
|
1159
|
+
this.USE_MRT = false;
|
|
1160
|
+
this.webgl1DepthPass = null;
|
|
1161
|
+
this.visibleMeshes = [];
|
|
1162
|
+
this.ssrEffect = ssrEffect;
|
|
1163
|
+
this._scene = ssrEffect._scene;
|
|
1164
|
+
this._camera = ssrEffect._camera;
|
|
1165
|
+
this.fullscreenMaterial = new ReflectionsMaterial();
|
|
1166
|
+
if (ssrEffect._camera.isPerspectiveCamera)
|
|
1167
|
+
this.fullscreenMaterial.defines.PERSPECTIVE_CAMERA = "";
|
|
1168
|
+
const width = options.width || typeof window !== "undefined" ? window.innerWidth : 2e3;
|
|
1169
|
+
const height = options.height || typeof window !== "undefined" ? window.innerHeight : 1e3;
|
|
1170
|
+
this.renderTarget = new WebGLRenderTarget(width, height, {
|
|
1171
|
+
minFilter: LinearFilter,
|
|
1172
|
+
magFilter: LinearFilter,
|
|
1173
|
+
type: HalfFloatType,
|
|
1174
|
+
depthBuffer: false
|
|
1175
|
+
});
|
|
1176
|
+
this.renderPass = new RenderPass(this._scene, this._camera);
|
|
1177
|
+
this.USE_MRT = isWebGL2Available();
|
|
1178
|
+
if (this.USE_MRT) {
|
|
1179
|
+
this.gBuffersRenderTarget = new WebGLMultipleRenderTargets(width, height, 2, {
|
|
1180
|
+
minFilter: LinearFilter,
|
|
1181
|
+
magFilter: LinearFilter
|
|
1182
|
+
});
|
|
1183
|
+
this.normalTexture = this.gBuffersRenderTarget.texture[0];
|
|
1184
|
+
this.depthTexture = this.gBuffersRenderTarget.texture[1];
|
|
1185
|
+
} else {
|
|
1186
|
+
this.webgl1DepthPass = new DepthPass(this._scene, this._camera);
|
|
1187
|
+
this.webgl1DepthPass.renderTarget.minFilter = LinearFilter;
|
|
1188
|
+
this.webgl1DepthPass.renderTarget.magFilter = LinearFilter;
|
|
1189
|
+
this.webgl1DepthPass.renderTarget.texture.minFilter = LinearFilter;
|
|
1190
|
+
this.webgl1DepthPass.renderTarget.texture.magFilter = LinearFilter;
|
|
1191
|
+
this.webgl1DepthPass.setSize(
|
|
1192
|
+
typeof window !== "undefined" ? window.innerWidth : 2e3,
|
|
1193
|
+
typeof window !== "undefined" ? window.innerHeight : 1e3
|
|
1194
|
+
);
|
|
1195
|
+
this.gBuffersRenderTarget = new WebGLRenderTarget(width, height, {
|
|
1196
|
+
minFilter: LinearFilter,
|
|
1197
|
+
magFilter: LinearFilter
|
|
1198
|
+
});
|
|
1199
|
+
this.normalTexture = this.gBuffersRenderTarget.texture;
|
|
1200
|
+
this.depthTexture = this.webgl1DepthPass.texture;
|
|
1201
|
+
}
|
|
1202
|
+
this.fullscreenMaterial.uniforms.normalTexture.value = this.normalTexture;
|
|
1203
|
+
this.fullscreenMaterial.uniforms.depthTexture.value = this.depthTexture;
|
|
1204
|
+
this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.ssrEffect.temporalResolvePass.accumulatedTexture;
|
|
1205
|
+
this.fullscreenMaterial.uniforms.cameraMatrixWorld.value = this._camera.matrixWorld;
|
|
1206
|
+
this.fullscreenMaterial.uniforms._projectionMatrix.value = this._camera.projectionMatrix;
|
|
1207
|
+
this.fullscreenMaterial.uniforms._inverseProjectionMatrix.value = this._camera.projectionMatrixInverse;
|
|
1208
|
+
}
|
|
1209
|
+
setSize(width, height) {
|
|
1210
|
+
this.renderTarget.setSize(width * this.ssrEffect.resolutionScale, height * this.ssrEffect.resolutionScale);
|
|
1211
|
+
this.gBuffersRenderTarget.setSize(width * this.ssrEffect.resolutionScale, height * this.ssrEffect.resolutionScale);
|
|
1212
|
+
this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.ssrEffect.temporalResolvePass.accumulatedTexture;
|
|
1213
|
+
this.fullscreenMaterial.needsUpdate = true;
|
|
1214
|
+
}
|
|
1215
|
+
dispose() {
|
|
1216
|
+
this.renderTarget.dispose();
|
|
1217
|
+
this.gBuffersRenderTarget.dispose();
|
|
1218
|
+
this.renderPass.dispose();
|
|
1219
|
+
if (!this.USE_MRT)
|
|
1220
|
+
this.webgl1DepthPass.dispose();
|
|
1221
|
+
this.fullscreenMaterial.dispose();
|
|
1222
|
+
this.normalTexture = null;
|
|
1223
|
+
this.depthTexture = null;
|
|
1224
|
+
this.velocityTexture = null;
|
|
1225
|
+
}
|
|
1226
|
+
keepMaterialMapUpdated(mrtMaterial, originalMaterial, prop, define) {
|
|
1227
|
+
if (this.ssrEffect[define]) {
|
|
1228
|
+
if (originalMaterial[prop] !== mrtMaterial[prop]) {
|
|
1229
|
+
mrtMaterial[prop] = originalMaterial[prop];
|
|
1230
|
+
mrtMaterial.uniforms[prop].value = originalMaterial[prop];
|
|
1231
|
+
if (originalMaterial[prop]) {
|
|
1232
|
+
mrtMaterial.defines[define] = "";
|
|
1233
|
+
} else {
|
|
1234
|
+
delete mrtMaterial.defines[define];
|
|
1235
|
+
}
|
|
1236
|
+
mrtMaterial.needsUpdate = true;
|
|
1237
|
+
}
|
|
1238
|
+
} else if (mrtMaterial[prop] !== void 0) {
|
|
1239
|
+
mrtMaterial[prop] = void 0;
|
|
1240
|
+
mrtMaterial.uniforms[prop].value = void 0;
|
|
1241
|
+
delete mrtMaterial.defines[define];
|
|
1242
|
+
mrtMaterial.needsUpdate = true;
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
setMRTMaterialInScene() {
|
|
1246
|
+
this.visibleMeshes = getVisibleChildren(this._scene);
|
|
1247
|
+
for (const c of this.visibleMeshes) {
|
|
1248
|
+
if (c.material) {
|
|
1249
|
+
const originalMaterial = c.material;
|
|
1250
|
+
let [cachedOriginalMaterial, mrtMaterial] = this.cachedMaterials.get(c) || [];
|
|
1251
|
+
if (originalMaterial !== cachedOriginalMaterial) {
|
|
1252
|
+
if (mrtMaterial)
|
|
1253
|
+
mrtMaterial.dispose();
|
|
1254
|
+
mrtMaterial = new MRTMaterial();
|
|
1255
|
+
if (this.USE_MRT)
|
|
1256
|
+
mrtMaterial.defines.USE_MRT = "";
|
|
1257
|
+
mrtMaterial.normalScale = originalMaterial.normalScale;
|
|
1258
|
+
mrtMaterial.uniforms.normalScale.value = originalMaterial.normalScale;
|
|
1259
|
+
const map = originalMaterial.map || originalMaterial.normalMap || originalMaterial.roughnessMap || originalMaterial.metalnessMap;
|
|
1260
|
+
if (map)
|
|
1261
|
+
mrtMaterial.uniforms.uvTransform.value = map.matrix;
|
|
1262
|
+
this.cachedMaterials.set(c, [originalMaterial, mrtMaterial]);
|
|
1263
|
+
}
|
|
1264
|
+
this.keepMaterialMapUpdated(mrtMaterial, originalMaterial, "normalMap", "useNormalMap");
|
|
1265
|
+
this.keepMaterialMapUpdated(mrtMaterial, originalMaterial, "roughnessMap", "useRoughnessMap");
|
|
1266
|
+
mrtMaterial.uniforms.roughness.value = this.ssrEffect.selection.size === 0 || this.ssrEffect.selection.has(c) ? originalMaterial.roughness || 0 : 1e11;
|
|
1267
|
+
c.material = mrtMaterial;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
unsetMRTMaterialInScene() {
|
|
1272
|
+
for (const c of this.visibleMeshes) {
|
|
1273
|
+
var _c$material;
|
|
1274
|
+
if (((_c$material = c.material) == null ? void 0 : _c$material.type) === "MRTMaterial") {
|
|
1275
|
+
c.visible = true;
|
|
1276
|
+
const [originalMaterial] = this.cachedMaterials.get(c);
|
|
1277
|
+
c.material = originalMaterial;
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
render(renderer, inputBuffer) {
|
|
1282
|
+
this.setMRTMaterialInScene();
|
|
1283
|
+
renderer.setRenderTarget(this.gBuffersRenderTarget);
|
|
1284
|
+
this.renderPass.render(renderer, this.gBuffersRenderTarget);
|
|
1285
|
+
this.unsetMRTMaterialInScene();
|
|
1286
|
+
if (!this.USE_MRT)
|
|
1287
|
+
this.webgl1DepthPass.renderPass.render(renderer, this.webgl1DepthPass.renderTarget);
|
|
1288
|
+
this.fullscreenMaterial.uniforms.inputTexture.value = inputBuffer.texture;
|
|
1289
|
+
this.fullscreenMaterial.uniforms.samples.value = this.ssrEffect.temporalResolvePass.samples;
|
|
1290
|
+
this.fullscreenMaterial.uniforms.cameraNear.value = this._camera.near;
|
|
1291
|
+
this.fullscreenMaterial.uniforms.cameraFar.value = this._camera.far;
|
|
1292
|
+
this.fullscreenMaterial.uniforms.viewMatrix.value.copy(this._camera.matrixWorldInverse);
|
|
1293
|
+
renderer.setRenderTarget(this.renderTarget);
|
|
1294
|
+
renderer.render(this.scene, this.camera);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
const defaultSSROptions = {
|
|
1298
|
+
intensity: 1,
|
|
1299
|
+
exponent: 1,
|
|
1300
|
+
distance: 10,
|
|
1301
|
+
fade: 0,
|
|
1302
|
+
roughnessFade: 1,
|
|
1303
|
+
thickness: 10,
|
|
1304
|
+
ior: 1.45,
|
|
1305
|
+
maxRoughness: 1,
|
|
1306
|
+
maxDepthDifference: 10,
|
|
1307
|
+
blend: 0.9,
|
|
1308
|
+
correction: 1,
|
|
1309
|
+
correctionRadius: 1,
|
|
1310
|
+
blur: 0.5,
|
|
1311
|
+
blurKernel: 1,
|
|
1312
|
+
blurSharpness: 10,
|
|
1313
|
+
jitter: 0,
|
|
1314
|
+
jitterRoughness: 0,
|
|
1315
|
+
steps: 20,
|
|
1316
|
+
refineSteps: 5,
|
|
1317
|
+
missedRays: true,
|
|
1318
|
+
useNormalMap: true,
|
|
1319
|
+
useRoughnessMap: true,
|
|
1320
|
+
resolutionScale: 1,
|
|
1321
|
+
velocityResolutionScale: 1
|
|
1322
|
+
};
|
|
1323
|
+
var vertexShader = "#define GLSLIFY 1\nvarying vec2 vUv;void main(){vUv=position.xy*0.5+0.5;gl_Position=vec4(position.xy,1.0,1.0);}";
|
|
1324
|
+
var temporalResolve = "#define GLSLIFY 1\nuniform sampler2D inputTexture;uniform sampler2D accumulatedTexture;uniform sampler2D velocityTexture;uniform sampler2D lastVelocityTexture;uniform float blend;uniform float correction;uniform float exponent;uniform float samples;uniform vec2 invTexSize;uniform mat4 curInverseProjectionMatrix;uniform mat4 curCameraMatrixWorld;uniform mat4 prevInverseProjectionMatrix;uniform mat4 prevCameraMatrixWorld;varying vec2 vUv;\n#define MAX_NEIGHBOR_DEPTH_DIFFERENCE 0.001\n#define FLOAT_EPSILON 0.00001\n#define FLOAT_ONE_MINUS_EPSILON 0.99999\nvec3 transformexponent;vec3 undoColorTransformExponent;vec3 transformColor(vec3 color){if(exponent==1.0)return color;return pow(abs(color),transformexponent);}vec3 undoColorTransform(vec3 color){if(exponent==1.0)return color;return max(pow(abs(color),undoColorTransformExponent),vec3(0.0));}void main(){if(exponent!=1.0){transformexponent=vec3(1.0/exponent);undoColorTransformExponent=vec3(exponent);}vec4 inputTexel=textureLod(inputTexture,vUv,0.0);vec4 accumulatedTexel;vec3 inputColor=transformColor(inputTexel.rgb);vec3 accumulatedColor;float alpha=inputTexel.a;float velocityDisocclusion;bool didReproject=false;\n#ifdef boxBlur\nvec3 boxBlurredColor=inputTexel.rgb;\n#endif\nvec4 velocity=textureLod(velocityTexture,vUv,0.0);bool isMoving=alpha<1.0||dot(velocity.xy,velocity.xy)>0.0;if(isMoving){vec3 minNeighborColor=inputColor;vec3 maxNeighborColor=inputColor;vec3 col;vec2 neighborUv;vec2 reprojectedUv=vUv-velocity.xy;vec4 lastVelocity=textureLod(lastVelocityTexture,reprojectedUv,0.0);float depth=velocity.b;float closestDepth=depth;float lastClosestDepth=lastVelocity.b;float neighborDepth;float lastNeighborDepth;for(int x=-correctionRadius;x<=correctionRadius;x++){for(int y=-correctionRadius;y<=correctionRadius;y++){if(x!=0||y!=0){neighborUv=vUv+vec2(x,y)*invTexSize;vec4 neigborVelocity=textureLod(velocityTexture,neighborUv,0.0);neighborDepth=neigborVelocity.b;col=textureLod(inputTexture,neighborUv,0.0).xyz;int absX=abs(x);int absY=abs(y);\n#ifdef dilation\nif(absX==1&&absY==1){if(neighborDepth>closestDepth){velocity=neigborVelocity;closestDepth=neighborDepth;}vec4 lastNeighborVelocity=textureLod(velocityTexture,vUv+vec2(x,y)*invTexSize,0.0);lastNeighborDepth=lastNeighborVelocity.b;if(neighborDepth>closestDepth){lastVelocity=lastNeighborVelocity;lastClosestDepth=lastNeighborDepth;}}\n#endif\nif(abs(depth-neighborDepth)<MAX_NEIGHBOR_DEPTH_DIFFERENCE){\n#ifdef boxBlur\nif(absX<=2&&absY<=2)boxBlurredColor+=col;\n#endif\ncol=transformColor(col);minNeighborColor=min(col,minNeighborColor);maxNeighborColor=max(col,maxNeighborColor);}}}}float velocityLength=length(lastVelocity.xy-velocity.xy);velocityDisocclusion=(velocityLength-0.000005)*10.0;velocityDisocclusion*=velocityDisocclusion;reprojectedUv=vUv-velocity.xy;\n#ifdef boxBlur\nfloat pxRadius=correctionRadius>5 ? 121.0 : pow(float(correctionRadius*2+1),2.0);boxBlurredColor/=pxRadius;boxBlurredColor=transformColor(boxBlurredColor);\n#endif\nif(reprojectedUv.x>=0.0&&reprojectedUv.x<=1.0&&reprojectedUv.y>=0.0&&reprojectedUv.y<=1.0){accumulatedTexel=textureLod(accumulatedTexture,reprojectedUv,0.0);accumulatedColor=transformColor(accumulatedTexel.rgb);vec3 clampedColor=clamp(accumulatedColor,minNeighborColor,maxNeighborColor);accumulatedColor=mix(accumulatedColor,clampedColor,correction);didReproject=true;}else{\n#ifdef boxBlur\naccumulatedColor=boxBlurredColor;\n#else\naccumulatedColor=inputColor;\n#endif\n}if(velocity.r>FLOAT_ONE_MINUS_EPSILON&&velocity.g>FLOAT_ONE_MINUS_EPSILON){alpha=0.0;velocityDisocclusion=1.0;}}else{accumulatedColor=transformColor(textureLod(accumulatedTexture,vUv,0.0).rgb);}vec3 outputColor=inputColor;\n#include <custom_compose_shader>\ngl_FragColor=vec4(undoColorTransform(outputColor),alpha);}";
|
|
1325
|
+
class TemporalResolveMaterial extends ShaderMaterial {
|
|
1326
|
+
constructor(customComposeShader) {
|
|
1327
|
+
const fragmentShader2 = temporalResolve.replace("#include <custom_compose_shader>", customComposeShader);
|
|
1328
|
+
super({
|
|
1329
|
+
type: "TemporalResolveMaterial",
|
|
1330
|
+
uniforms: {
|
|
1331
|
+
inputTexture: new Uniform(null),
|
|
1332
|
+
accumulatedTexture: new Uniform(null),
|
|
1333
|
+
velocityTexture: new Uniform(null),
|
|
1334
|
+
lastVelocityTexture: new Uniform(null),
|
|
1335
|
+
samples: new Uniform(1),
|
|
1336
|
+
blend: new Uniform(0.5),
|
|
1337
|
+
correction: new Uniform(1),
|
|
1338
|
+
exponent: new Uniform(1),
|
|
1339
|
+
invTexSize: new Uniform(new Vector2())
|
|
1340
|
+
},
|
|
1341
|
+
defines: {
|
|
1342
|
+
correctionRadius: 1
|
|
1343
|
+
},
|
|
1344
|
+
vertexShader,
|
|
1345
|
+
fragmentShader: fragmentShader2
|
|
1346
|
+
});
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
const prev_skinning_pars_vertex = (
|
|
1350
|
+
/* glsl */
|
|
1351
|
+
`
|
|
1352
|
+
#ifdef USE_SKINNING
|
|
1353
|
+
#ifdef BONE_TEXTURE
|
|
1354
|
+
uniform sampler2D prevBoneTexture;
|
|
1355
|
+
mat4 getPrevBoneMatrix( const in float i ) {
|
|
1356
|
+
float j = i * 4.0;
|
|
1357
|
+
float x = mod( j, float( boneTextureSize ) );
|
|
1358
|
+
float y = floor( j / float( boneTextureSize ) );
|
|
1359
|
+
float dx = 1.0 / float( boneTextureSize );
|
|
1360
|
+
float dy = 1.0 / float( boneTextureSize );
|
|
1361
|
+
y = dy * ( y + 0.5 );
|
|
1362
|
+
vec4 v1 = texture2D( prevBoneTexture, vec2( dx * ( x + 0.5 ), y ) );
|
|
1363
|
+
vec4 v2 = texture2D( prevBoneTexture, vec2( dx * ( x + 1.5 ), y ) );
|
|
1364
|
+
vec4 v3 = texture2D( prevBoneTexture, vec2( dx * ( x + 2.5 ), y ) );
|
|
1365
|
+
vec4 v4 = texture2D( prevBoneTexture, vec2( dx * ( x + 3.5 ), y ) );
|
|
1366
|
+
mat4 bone = mat4( v1, v2, v3, v4 );
|
|
1367
|
+
return bone;
|
|
1368
|
+
}
|
|
1369
|
+
#else
|
|
1370
|
+
uniform mat4 prevBoneMatrices[ MAX_BONES ];
|
|
1371
|
+
mat4 getPrevBoneMatrix( const in float i ) {
|
|
1372
|
+
mat4 bone = prevBoneMatrices[ int(i) ];
|
|
1373
|
+
return bone;
|
|
1374
|
+
}
|
|
1375
|
+
#endif
|
|
1376
|
+
#endif
|
|
1377
|
+
`
|
|
1378
|
+
);
|
|
1379
|
+
const velocity_vertex = (
|
|
1380
|
+
/* glsl */
|
|
1381
|
+
`
|
|
1382
|
+
vec3 transformed;
|
|
1383
|
+
|
|
1384
|
+
// Get the normal
|
|
1385
|
+
${ShaderChunk.skinbase_vertex}
|
|
1386
|
+
${ShaderChunk.beginnormal_vertex}
|
|
1387
|
+
${ShaderChunk.skinnormal_vertex}
|
|
1388
|
+
${ShaderChunk.defaultnormal_vertex}
|
|
1389
|
+
|
|
1390
|
+
// Get the current vertex position
|
|
1391
|
+
transformed = vec3( position );
|
|
1392
|
+
${ShaderChunk.skinning_vertex}
|
|
1393
|
+
newPosition = velocityMatrix * vec4( transformed, 1.0 );
|
|
1394
|
+
|
|
1395
|
+
// Get the previous vertex position
|
|
1396
|
+
transformed = vec3( position );
|
|
1397
|
+
${ShaderChunk.skinbase_vertex.replace(/mat4 /g, "").replace(/getBoneMatrix/g, "getPrevBoneMatrix")}
|
|
1398
|
+
${ShaderChunk.skinning_vertex.replace(/vec4 /g, "")}
|
|
1399
|
+
prevPosition = prevVelocityMatrix * vec4( transformed, 1.0 );
|
|
1400
|
+
|
|
1401
|
+
gl_Position = newPosition;
|
|
1402
|
+
`
|
|
1403
|
+
);
|
|
1404
|
+
class VelocityMaterial extends ShaderMaterial {
|
|
1405
|
+
constructor() {
|
|
1406
|
+
super({
|
|
1407
|
+
uniforms: {
|
|
1408
|
+
prevVelocityMatrix: {
|
|
1409
|
+
value: new Matrix4()
|
|
1410
|
+
},
|
|
1411
|
+
velocityMatrix: {
|
|
1412
|
+
value: new Matrix4()
|
|
1413
|
+
},
|
|
1414
|
+
prevBoneTexture: {
|
|
1415
|
+
value: null
|
|
1416
|
+
},
|
|
1417
|
+
interpolateGeometry: {
|
|
1418
|
+
value: 0
|
|
1419
|
+
},
|
|
1420
|
+
intensity: {
|
|
1421
|
+
value: 1
|
|
1422
|
+
},
|
|
1423
|
+
boneTexture: {
|
|
1424
|
+
value: null
|
|
1425
|
+
},
|
|
1426
|
+
alphaTest: {
|
|
1427
|
+
value: 0
|
|
1428
|
+
},
|
|
1429
|
+
map: {
|
|
1430
|
+
value: null
|
|
1431
|
+
},
|
|
1432
|
+
alphaMap: {
|
|
1433
|
+
value: null
|
|
1434
|
+
},
|
|
1435
|
+
opacity: {
|
|
1436
|
+
value: 1
|
|
1437
|
+
}
|
|
1438
|
+
},
|
|
1439
|
+
vertexShader: (
|
|
1440
|
+
/* glsl */
|
|
1441
|
+
`
|
|
1442
|
+
#define MAX_BONES 1024
|
|
1443
|
+
|
|
1444
|
+
${ShaderChunk.skinning_pars_vertex}
|
|
1445
|
+
${prev_skinning_pars_vertex}
|
|
1446
|
+
|
|
1447
|
+
uniform mat4 velocityMatrix;
|
|
1448
|
+
uniform mat4 prevVelocityMatrix;
|
|
1449
|
+
uniform float interpolateGeometry;
|
|
1450
|
+
varying vec4 prevPosition;
|
|
1451
|
+
varying vec4 newPosition;
|
|
1452
|
+
varying vec2 vHighPrecisionZW;
|
|
1453
|
+
|
|
1454
|
+
void main() {
|
|
1455
|
+
|
|
1456
|
+
${velocity_vertex}
|
|
1457
|
+
|
|
1458
|
+
vHighPrecisionZW = gl_Position.zw;
|
|
1459
|
+
|
|
1460
|
+
}`
|
|
1461
|
+
),
|
|
1462
|
+
fragmentShader: (
|
|
1463
|
+
/* glsl */
|
|
1464
|
+
`
|
|
1465
|
+
uniform float intensity;
|
|
1466
|
+
varying vec4 prevPosition;
|
|
1467
|
+
varying vec4 newPosition;
|
|
1468
|
+
varying vec2 vHighPrecisionZW;
|
|
1469
|
+
|
|
1470
|
+
void main() {
|
|
1471
|
+
#ifdef FULL_MOVEMENT
|
|
1472
|
+
gl_FragColor = vec4( 1., 1., 1. - gl_FragCoord.z, 0. );
|
|
1473
|
+
return;
|
|
1474
|
+
#endif
|
|
1475
|
+
|
|
1476
|
+
vec2 pos0 = (prevPosition.xy / prevPosition.w) * 0.5 + 0.5;
|
|
1477
|
+
vec2 pos1 = (newPosition.xy / newPosition.w) * 0.5 + 0.5;
|
|
1478
|
+
|
|
1479
|
+
vec2 vel = pos1 - pos0;
|
|
1480
|
+
|
|
1481
|
+
float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
|
|
1482
|
+
|
|
1483
|
+
gl_FragColor = vec4( vel, 1. - fragCoordZ, 0. );
|
|
1484
|
+
|
|
1485
|
+
}`
|
|
1486
|
+
)
|
|
1487
|
+
});
|
|
1488
|
+
this.isVelocityMaterial = true;
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
const backgroundColor = new Color(0);
|
|
1492
|
+
const updateProperties = ["visible", "wireframe", "side"];
|
|
1493
|
+
class VelocityPass extends Pass {
|
|
1494
|
+
constructor(scene, camera) {
|
|
1495
|
+
var _window, _window2;
|
|
1496
|
+
super("VelocityPass");
|
|
1497
|
+
this.cachedMaterials = /* @__PURE__ */ new WeakMap();
|
|
1498
|
+
this.lastCameraTransform = {
|
|
1499
|
+
position: new Vector3(),
|
|
1500
|
+
quaternion: new Quaternion()
|
|
1501
|
+
};
|
|
1502
|
+
this.visibleMeshes = [];
|
|
1503
|
+
this.renderedMeshesThisFrame = 0;
|
|
1504
|
+
this.renderedMeshesLastFrame = 0;
|
|
1505
|
+
this._scene = scene;
|
|
1506
|
+
this._camera = camera;
|
|
1507
|
+
this.renderTarget = new WebGLRenderTarget(
|
|
1508
|
+
((_window = window) == null ? void 0 : _window.innerWidth) || 1e3,
|
|
1509
|
+
((_window2 = window) == null ? void 0 : _window2.innerHeight) || 1e3,
|
|
1510
|
+
{
|
|
1511
|
+
type: HalfFloatType
|
|
1512
|
+
}
|
|
1513
|
+
);
|
|
1514
|
+
}
|
|
1515
|
+
setVelocityMaterialInScene() {
|
|
1516
|
+
this.renderedMeshesThisFrame = 0;
|
|
1517
|
+
this.visibleMeshes = getVisibleChildren(this._scene);
|
|
1518
|
+
for (const c of this.visibleMeshes) {
|
|
1519
|
+
var _c$skeleton2;
|
|
1520
|
+
const originalMaterial = c.material;
|
|
1521
|
+
let [cachedOriginalMaterial, velocityMaterial] = this.cachedMaterials.get(c) || [];
|
|
1522
|
+
if (originalMaterial !== cachedOriginalMaterial) {
|
|
1523
|
+
var _c$skeleton;
|
|
1524
|
+
velocityMaterial = new VelocityMaterial();
|
|
1525
|
+
velocityMaterial.lastMatrixWorld = new Matrix4();
|
|
1526
|
+
c.material = velocityMaterial;
|
|
1527
|
+
if ((_c$skeleton = c.skeleton) != null && _c$skeleton.boneTexture)
|
|
1528
|
+
this.saveBoneTexture(c);
|
|
1529
|
+
this.cachedMaterials.set(c, [originalMaterial, velocityMaterial]);
|
|
1530
|
+
}
|
|
1531
|
+
velocityMaterial.uniforms.velocityMatrix.value.multiplyMatrices(this._camera.projectionMatrix, c.modelViewMatrix);
|
|
1532
|
+
if (c.userData.needsUpdatedReflections || originalMaterial.map instanceof VideoTexture) {
|
|
1533
|
+
if (!("FULL_MOVEMENT" in velocityMaterial.defines))
|
|
1534
|
+
velocityMaterial.needsUpdate = true;
|
|
1535
|
+
velocityMaterial.defines.FULL_MOVEMENT = "";
|
|
1536
|
+
} else {
|
|
1537
|
+
if ("FULL_MOVEMENT" in velocityMaterial.defines) {
|
|
1538
|
+
delete velocityMaterial.defines.FULL_MOVEMENT;
|
|
1539
|
+
velocityMaterial.needsUpdate = true;
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1542
|
+
c.visible = this.cameraMovedThisFrame || !c.matrixWorld.equals(velocityMaterial.lastMatrixWorld) || c.skeleton || "FULL_MOVEMENT" in velocityMaterial.defines;
|
|
1543
|
+
c.material = velocityMaterial;
|
|
1544
|
+
if (!c.visible)
|
|
1545
|
+
continue;
|
|
1546
|
+
this.renderedMeshesThisFrame++;
|
|
1547
|
+
for (const prop of updateProperties)
|
|
1548
|
+
velocityMaterial[prop] = originalMaterial[prop];
|
|
1549
|
+
if ((_c$skeleton2 = c.skeleton) != null && _c$skeleton2.boneTexture) {
|
|
1550
|
+
velocityMaterial.defines.USE_SKINNING = "";
|
|
1551
|
+
velocityMaterial.defines.BONE_TEXTURE = "";
|
|
1552
|
+
velocityMaterial.uniforms.boneTexture.value = c.skeleton.boneTexture;
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
saveBoneTexture(object) {
|
|
1557
|
+
let boneTexture = object.material.uniforms.prevBoneTexture.value;
|
|
1558
|
+
if (boneTexture && boneTexture.image.width === object.skeleton.boneTexture.width) {
|
|
1559
|
+
boneTexture = object.material.uniforms.prevBoneTexture.value;
|
|
1560
|
+
boneTexture.image.data.set(object.skeleton.boneTexture.image.data);
|
|
1561
|
+
} else {
|
|
1562
|
+
var _boneTexture;
|
|
1563
|
+
(_boneTexture = boneTexture) == null ? void 0 : _boneTexture.dispose();
|
|
1564
|
+
const boneMatrices = object.skeleton.boneTexture.image.data.slice();
|
|
1565
|
+
const size = object.skeleton.boneTexture.image.width;
|
|
1566
|
+
boneTexture = new DataTexture(boneMatrices, size, size, RGBAFormat, FloatType);
|
|
1567
|
+
object.material.uniforms.prevBoneTexture.value = boneTexture;
|
|
1568
|
+
boneTexture.needsUpdate = true;
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
unsetVelocityMaterialInScene() {
|
|
1572
|
+
for (const c of this.visibleMeshes) {
|
|
1573
|
+
if (c.material.isVelocityMaterial) {
|
|
1574
|
+
var _c$skeleton3;
|
|
1575
|
+
c.visible = true;
|
|
1576
|
+
c.material.lastMatrixWorld.copy(c.matrixWorld);
|
|
1577
|
+
c.material.uniforms.prevVelocityMatrix.value.multiplyMatrices(this._camera.projectionMatrix, c.modelViewMatrix);
|
|
1578
|
+
if ((_c$skeleton3 = c.skeleton) != null && _c$skeleton3.boneTexture)
|
|
1579
|
+
this.saveBoneTexture(c);
|
|
1580
|
+
c.material = this.cachedMaterials.get(c)[0];
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
setSize(width, height) {
|
|
1585
|
+
this.renderTarget.setSize(width, height);
|
|
1586
|
+
}
|
|
1587
|
+
renderVelocity(renderer) {
|
|
1588
|
+
renderer.setRenderTarget(this.renderTarget);
|
|
1589
|
+
if (this.renderedMeshesThisFrame > 0) {
|
|
1590
|
+
const { background } = this._scene;
|
|
1591
|
+
this._scene.background = backgroundColor;
|
|
1592
|
+
renderer.render(this._scene, this._camera);
|
|
1593
|
+
this._scene.background = background;
|
|
1594
|
+
} else {
|
|
1595
|
+
renderer.clearColor();
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
checkCameraMoved() {
|
|
1599
|
+
const moveDist = this.lastCameraTransform.position.distanceToSquared(this._camera.position);
|
|
1600
|
+
const rotateDist = 8 * (1 - this.lastCameraTransform.quaternion.dot(this._camera.quaternion));
|
|
1601
|
+
if (moveDist > 1e-6 || rotateDist > 1e-6) {
|
|
1602
|
+
this.lastCameraTransform.position.copy(this._camera.position);
|
|
1603
|
+
this.lastCameraTransform.quaternion.copy(this._camera.quaternion);
|
|
1604
|
+
return true;
|
|
1605
|
+
}
|
|
1606
|
+
return false;
|
|
1607
|
+
}
|
|
1608
|
+
render(renderer) {
|
|
1609
|
+
this.cameraMovedThisFrame = this.checkCameraMoved();
|
|
1610
|
+
this.setVelocityMaterialInScene();
|
|
1611
|
+
if (this.renderedMeshesThisFrame > 0 || this.renderedMeshesLastFrame > 0)
|
|
1612
|
+
this.renderVelocity(renderer);
|
|
1613
|
+
this.unsetVelocityMaterialInScene();
|
|
1614
|
+
this.renderedMeshesLastFrame = this.renderedMeshesThisFrame;
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
const zeroVec2 = new Vector2();
|
|
1618
|
+
class TemporalResolvePass extends Pass {
|
|
1619
|
+
constructor(scene, camera, customComposeShader, options = {}) {
|
|
1620
|
+
super("TemporalResolvePass");
|
|
1621
|
+
this.velocityPass = null;
|
|
1622
|
+
this.velocityResolutionScale = 1;
|
|
1623
|
+
this.samples = 1;
|
|
1624
|
+
this.lastCameraTransform = {
|
|
1625
|
+
position: new Vector3(),
|
|
1626
|
+
quaternion: new Quaternion()
|
|
1627
|
+
};
|
|
1628
|
+
this._scene = scene;
|
|
1629
|
+
this._camera = camera;
|
|
1630
|
+
this.renderTarget = new WebGLRenderTarget(1, 1, {
|
|
1631
|
+
minFilter: LinearFilter,
|
|
1632
|
+
magFilter: LinearFilter,
|
|
1633
|
+
type: HalfFloatType,
|
|
1634
|
+
depthBuffer: false
|
|
1635
|
+
});
|
|
1636
|
+
this.velocityPass = new VelocityPass(scene, camera);
|
|
1637
|
+
this.fullscreenMaterial = new TemporalResolveMaterial(customComposeShader);
|
|
1638
|
+
this.fullscreenMaterial.defines.correctionRadius = options.correctionRadius || 1;
|
|
1639
|
+
if (options.dilation)
|
|
1640
|
+
this.fullscreenMaterial.defines.dilation = "";
|
|
1641
|
+
if (options.boxBlur)
|
|
1642
|
+
this.fullscreenMaterial.defines.boxBlur = "";
|
|
1643
|
+
this.setupFramebuffers(1, 1);
|
|
1644
|
+
this.checkCanUseSharedVelocityTexture();
|
|
1645
|
+
}
|
|
1646
|
+
dispose() {
|
|
1647
|
+
if (this._scene.userData.velocityTexture === this.velocityPass.renderTarget.texture) {
|
|
1648
|
+
delete this._scene.userData.velocityTexture;
|
|
1649
|
+
delete this._scene.userData.lastVelocityTexture;
|
|
1650
|
+
}
|
|
1651
|
+
this.renderTarget.dispose();
|
|
1652
|
+
this.accumulatedTexture.dispose();
|
|
1653
|
+
this.fullscreenMaterial.dispose();
|
|
1654
|
+
this.velocityPass.dispose();
|
|
1655
|
+
}
|
|
1656
|
+
setSize(width, height) {
|
|
1657
|
+
this.renderTarget.setSize(width, height);
|
|
1658
|
+
this.velocityPass.setSize(width * this.velocityResolutionScale, height * this.velocityResolutionScale);
|
|
1659
|
+
this.velocityPass.renderTarget.texture.minFilter = this.velocityResolutionScale === 1 ? NearestFilter : LinearFilter;
|
|
1660
|
+
this.velocityPass.renderTarget.texture.magFilter = this.velocityResolutionScale === 1 ? NearestFilter : LinearFilter;
|
|
1661
|
+
this.velocityPass.renderTarget.texture.needsUpdate = true;
|
|
1662
|
+
this.fullscreenMaterial.uniforms.invTexSize.value.set(1 / width, 1 / height);
|
|
1663
|
+
this.setupFramebuffers(width, height);
|
|
1664
|
+
}
|
|
1665
|
+
setupFramebuffers(width, height) {
|
|
1666
|
+
if (this.accumulatedTexture)
|
|
1667
|
+
this.accumulatedTexture.dispose();
|
|
1668
|
+
if (this.lastVelocityTexture)
|
|
1669
|
+
this.lastVelocityTexture.dispose();
|
|
1670
|
+
this.accumulatedTexture = new FramebufferTexture(width, height, RGBAFormat);
|
|
1671
|
+
this.accumulatedTexture.minFilter = LinearFilter;
|
|
1672
|
+
this.accumulatedTexture.magFilter = LinearFilter;
|
|
1673
|
+
this.accumulatedTexture.type = HalfFloatType;
|
|
1674
|
+
this.lastVelocityTexture = new FramebufferTexture(
|
|
1675
|
+
width * this.velocityResolutionScale,
|
|
1676
|
+
height * this.velocityResolutionScale,
|
|
1677
|
+
RGBAFormat
|
|
1678
|
+
);
|
|
1679
|
+
this.lastVelocityTexture.minFilter = this.velocityResolutionScale === 1 ? NearestFilter : LinearFilter;
|
|
1680
|
+
this.lastVelocityTexture.magFilter = this.velocityResolutionScale === 1 ? NearestFilter : LinearFilter;
|
|
1681
|
+
this.lastVelocityTexture.type = HalfFloatType;
|
|
1682
|
+
this.fullscreenMaterial.uniforms.accumulatedTexture.value = this.accumulatedTexture;
|
|
1683
|
+
this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this.lastVelocityTexture;
|
|
1684
|
+
this.fullscreenMaterial.needsUpdate = true;
|
|
1685
|
+
}
|
|
1686
|
+
checkCanUseSharedVelocityTexture() {
|
|
1687
|
+
const canUseSharedVelocityTexture = this._scene.userData.velocityTexture && this.velocityPass.renderTarget.texture !== this._scene.userData.velocityTexture;
|
|
1688
|
+
if (canUseSharedVelocityTexture) {
|
|
1689
|
+
if (this.velocityPass.renderTarget.texture === this.fullscreenMaterial.uniforms.velocityTexture.value) {
|
|
1690
|
+
this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this._scene.userData.lastVelocityTexture;
|
|
1691
|
+
this.fullscreenMaterial.uniforms.velocityTexture.value = this._scene.userData.velocityTexture;
|
|
1692
|
+
this.fullscreenMaterial.needsUpdate = true;
|
|
1693
|
+
}
|
|
1694
|
+
} else {
|
|
1695
|
+
if (this.velocityPass.renderTarget.texture !== this.fullscreenMaterial.uniforms.velocityTexture.value) {
|
|
1696
|
+
this.fullscreenMaterial.uniforms.velocityTexture.value = this.velocityPass.renderTarget.texture;
|
|
1697
|
+
this.fullscreenMaterial.uniforms.lastVelocityTexture.value = this.lastVelocityTexture;
|
|
1698
|
+
this.fullscreenMaterial.needsUpdate = true;
|
|
1699
|
+
if (!this._scene.userData.velocityTexture) {
|
|
1700
|
+
this._scene.userData.velocityTexture = this.velocityPass.renderTarget.texture;
|
|
1701
|
+
this._scene.userData.lastVelocityTexture = this.lastVelocityTexture;
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
return this.velocityPass.renderTarget.texture !== this.fullscreenMaterial.uniforms.velocityTexture.value;
|
|
1706
|
+
}
|
|
1707
|
+
checkNeedsResample() {
|
|
1708
|
+
const moveDist = this.lastCameraTransform.position.distanceToSquared(this._camera.position);
|
|
1709
|
+
const rotateDist = 8 * (1 - this.lastCameraTransform.quaternion.dot(this._camera.quaternion));
|
|
1710
|
+
if (moveDist > 1e-6 || rotateDist > 1e-6) {
|
|
1711
|
+
this.samples = 1;
|
|
1712
|
+
this.lastCameraTransform.position.copy(this._camera.position);
|
|
1713
|
+
this.lastCameraTransform.quaternion.copy(this._camera.quaternion);
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
render(renderer) {
|
|
1717
|
+
this.samples++;
|
|
1718
|
+
this.checkNeedsResample();
|
|
1719
|
+
this.fullscreenMaterial.uniforms.samples.value = this.samples;
|
|
1720
|
+
renderer.setRenderTarget(this.renderTarget);
|
|
1721
|
+
renderer.render(this.scene, this.camera);
|
|
1722
|
+
renderer.copyFramebufferToTexture(zeroVec2, this.accumulatedTexture);
|
|
1723
|
+
renderer.setRenderTarget(this.velocityPass.renderTarget);
|
|
1724
|
+
renderer.copyFramebufferToTexture(zeroVec2, this.lastVelocityTexture);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
const halton = function halton2(index, base) {
|
|
1728
|
+
let fraction = 1;
|
|
1729
|
+
let result = 0;
|
|
1730
|
+
while (index > 0) {
|
|
1731
|
+
fraction /= base;
|
|
1732
|
+
result += fraction * (index % base);
|
|
1733
|
+
index = ~~(index / base);
|
|
1734
|
+
}
|
|
1735
|
+
return result;
|
|
1736
|
+
};
|
|
1737
|
+
const generateHalton23Points = (count) => {
|
|
1738
|
+
const data = [];
|
|
1739
|
+
let i2 = 1;
|
|
1740
|
+
const end = i2 + count;
|
|
1741
|
+
for (; i2 < end; i2++) {
|
|
1742
|
+
data.push([halton(i2, 2) - 0.5, halton(i2, 3) - 0.5]);
|
|
1743
|
+
}
|
|
1744
|
+
return data;
|
|
1745
|
+
};
|
|
1746
|
+
function escapeRegExp(string) {
|
|
1747
|
+
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1748
|
+
}
|
|
1749
|
+
const worldposReplace = (
|
|
1750
|
+
/* glsl */
|
|
1751
|
+
`
|
|
1752
|
+
#if defined( USE_ENVMAP ) || defined( ) || defined ( USE_SHADOWMAP )
|
|
1753
|
+
vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 );
|
|
1754
|
+
|
|
1755
|
+
#ifdef BOX_PROJECTED_ENV_MAP
|
|
1756
|
+
vWorldPosition = worldPosition.xyz;
|
|
1757
|
+
#endif
|
|
1758
|
+
#endif
|
|
1759
|
+
`
|
|
1760
|
+
);
|
|
1761
|
+
const boxProjectDefinitions = (
|
|
1762
|
+
/* glsl */
|
|
1763
|
+
`
|
|
1764
|
+
#ifdef BOX_PROJECTED_ENV_MAP
|
|
1765
|
+
uniform vec3 envMapSize;
|
|
1766
|
+
uniform vec3 envMapPosition;
|
|
1767
|
+
varying vec3 vWorldPosition;
|
|
1768
|
+
|
|
1769
|
+
vec3 parallaxCorrectNormal( vec3 v, vec3 cubeSize, vec3 cubePos ) {
|
|
1770
|
+
vec3 nDir = normalize( v );
|
|
1771
|
+
|
|
1772
|
+
vec3 rbmax = ( .5 * cubeSize + cubePos - vWorldPosition ) / nDir;
|
|
1773
|
+
vec3 rbmin = ( -.5 * cubeSize + cubePos - vWorldPosition ) / nDir;
|
|
1774
|
+
|
|
1775
|
+
vec3 rbminmax;
|
|
1776
|
+
|
|
1777
|
+
rbminmax.x = ( nDir.x > 0. ) ? rbmax.x : rbmin.x;
|
|
1778
|
+
rbminmax.y = ( nDir.y > 0. ) ? rbmax.y : rbmin.y;
|
|
1779
|
+
rbminmax.z = ( nDir.z > 0. ) ? rbmax.z : rbmin.z;
|
|
1780
|
+
|
|
1781
|
+
float correction = min( min( rbminmax.x, rbminmax.y ), rbminmax.z );
|
|
1782
|
+
vec3 boxIntersection = vWorldPosition + nDir * correction;
|
|
1783
|
+
|
|
1784
|
+
return boxIntersection - cubePos;
|
|
1785
|
+
}
|
|
1786
|
+
#endif
|
|
1787
|
+
`
|
|
1788
|
+
);
|
|
1789
|
+
const getIBLIrradiance_patch = (
|
|
1790
|
+
/* glsl */
|
|
1791
|
+
`
|
|
1792
|
+
#ifdef BOX_PROJECTED_ENV_MAP
|
|
1793
|
+
worldNormal = parallaxCorrectNormal( worldNormal, envMapSize, envMapPosition );
|
|
1794
|
+
#endif
|
|
1795
|
+
`
|
|
1796
|
+
);
|
|
1797
|
+
const getIBLRadiance_patch = (
|
|
1798
|
+
/* glsl */
|
|
1799
|
+
`
|
|
1800
|
+
#ifdef BOX_PROJECTED_ENV_MAP
|
|
1801
|
+
reflectVec = parallaxCorrectNormal( reflectVec, envMapSize, envMapPosition );
|
|
1802
|
+
#endif
|
|
1803
|
+
`
|
|
1804
|
+
);
|
|
1805
|
+
function useBoxProjectedEnvMap(shader, envMapPosition, envMapSize) {
|
|
1806
|
+
shader.defines.BOX_PROJECTED_ENV_MAP = "";
|
|
1807
|
+
shader.uniforms.envMapPosition = {
|
|
1808
|
+
value: envMapPosition
|
|
1809
|
+
};
|
|
1810
|
+
shader.uniforms.envMapSize = {
|
|
1811
|
+
value: envMapSize
|
|
1812
|
+
};
|
|
1813
|
+
const line1 = new RegExp(
|
|
1814
|
+
escapeRegExp("vec3 worldNormal = inverseTransformDirection ( normal , viewMatrix ) ;").replaceAll(" ", "\\s*"),
|
|
1815
|
+
"g"
|
|
1816
|
+
);
|
|
1817
|
+
const line2 = new RegExp(
|
|
1818
|
+
escapeRegExp("reflectVec = inverseTransformDirection ( reflectVec , viewMatrix ) ;").replaceAll(" ", "\\s*"),
|
|
1819
|
+
"g"
|
|
1820
|
+
);
|
|
1821
|
+
shader.vertexShader = "varying vec3 vWorldPosition;\n" + shader.vertexShader.replace("#include <worldpos_vertex>", worldposReplace);
|
|
1822
|
+
shader.fragmentShader = boxProjectDefinitions + "\n" + shader.fragmentShader.replace("#include <envmap_physical_pars_fragment>", ShaderChunk.envmap_physical_pars_fragment).replace(
|
|
1823
|
+
line1,
|
|
1824
|
+
`vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
|
|
1825
|
+
${getIBLIrradiance_patch}`
|
|
1826
|
+
).replace(
|
|
1827
|
+
line2,
|
|
1828
|
+
`reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
|
|
1829
|
+
${getIBLRadiance_patch}`
|
|
1830
|
+
);
|
|
1831
|
+
}
|
|
1832
|
+
const finalFragmentShader = finalSSRShader.replace("#include <helperFunctions>", helperFunctions).replace("#include <boxBlur>", boxBlur);
|
|
1833
|
+
const noResetSamplesProperties = ["blur", "blurSharpness", "blurKernel"];
|
|
1834
|
+
const defaultCubeRenderTarget = new WebGLCubeRenderTarget(1);
|
|
1835
|
+
let pmremGenerator;
|
|
1836
|
+
class SSREffect extends Effect {
|
|
1837
|
+
/**
|
|
1838
|
+
* @param {THREE.Scene} scene The scene of the SSR effect
|
|
1839
|
+
* @param {THREE.Camera} camera The camera with which SSR is being rendered
|
|
1840
|
+
* @param {SSROptions} [options] The optional options for the SSR effect
|
|
1841
|
+
*/
|
|
1842
|
+
constructor(scene, camera, options = defaultSSROptions) {
|
|
1843
|
+
super("SSREffect", finalFragmentShader, {
|
|
1844
|
+
type: "FinalSSRMaterial",
|
|
1845
|
+
uniforms: /* @__PURE__ */ new Map([
|
|
1846
|
+
["reflectionsTexture", new Uniform(null)],
|
|
1847
|
+
["blur", new Uniform(0)],
|
|
1848
|
+
["blurSharpness", new Uniform(0)],
|
|
1849
|
+
["blurKernel", new Uniform(0)]
|
|
1850
|
+
]),
|
|
1851
|
+
defines: /* @__PURE__ */ new Map([["RENDER_MODE", "0"]])
|
|
1852
|
+
});
|
|
1853
|
+
this.haltonSequence = generateHalton23Points(1024);
|
|
1854
|
+
this.haltonIndex = 0;
|
|
1855
|
+
this.selection = new Selection$1();
|
|
1856
|
+
this.lastSize = void 0;
|
|
1857
|
+
this.cubeCamera = new CubeCamera(1e-3, 1e3, defaultCubeRenderTarget);
|
|
1858
|
+
this.usingBoxProjectedEnvMap = false;
|
|
1859
|
+
this._scene = scene;
|
|
1860
|
+
this._camera = camera;
|
|
1861
|
+
const trOptions = {
|
|
1862
|
+
boxBlur: true,
|
|
1863
|
+
dilation: true
|
|
1864
|
+
};
|
|
1865
|
+
options = { ...defaultSSROptions, ...options, ...trOptions };
|
|
1866
|
+
this.temporalResolvePass = new TemporalResolvePass(scene, camera, trCompose, options);
|
|
1867
|
+
this.uniforms.get("reflectionsTexture").value = this.temporalResolvePass.renderTarget.texture;
|
|
1868
|
+
this.reflectionsPass = new ReflectionsPass(this, options);
|
|
1869
|
+
this.temporalResolvePass.fullscreenMaterial.uniforms.inputTexture.value = this.reflectionsPass.renderTarget.texture;
|
|
1870
|
+
this.lastSize = {
|
|
1871
|
+
width: options.width,
|
|
1872
|
+
height: options.height,
|
|
1873
|
+
resolutionScale: options.resolutionScale,
|
|
1874
|
+
velocityResolutionScale: options.velocityResolutionScale
|
|
1875
|
+
};
|
|
1876
|
+
this.setSize(options.width, options.height);
|
|
1877
|
+
this.makeOptionsReactive(options);
|
|
1878
|
+
}
|
|
1879
|
+
makeOptionsReactive(options) {
|
|
1880
|
+
let needsUpdate = false;
|
|
1881
|
+
const reflectionPassFullscreenMaterialUniforms = this.reflectionsPass.fullscreenMaterial.uniforms;
|
|
1882
|
+
const reflectionPassFullscreenMaterialUniformsKeys = Object.keys(reflectionPassFullscreenMaterialUniforms);
|
|
1883
|
+
for (const key of Object.keys(options)) {
|
|
1884
|
+
Object.defineProperty(this, key, {
|
|
1885
|
+
get() {
|
|
1886
|
+
return options[key];
|
|
1887
|
+
},
|
|
1888
|
+
set(value) {
|
|
1889
|
+
if (options[key] === value && needsUpdate)
|
|
1890
|
+
return;
|
|
1891
|
+
options[key] = value;
|
|
1892
|
+
if (!noResetSamplesProperties.includes(key)) {
|
|
1893
|
+
this.setSize(this.lastSize.width, this.lastSize.height, true);
|
|
1894
|
+
}
|
|
1895
|
+
switch (key) {
|
|
1896
|
+
case "resolutionScale":
|
|
1897
|
+
this.setSize(this.lastSize.width, this.lastSize.height);
|
|
1898
|
+
break;
|
|
1899
|
+
case "velocityResolutionScale":
|
|
1900
|
+
this.temporalResolvePass.velocityResolutionScale = value;
|
|
1901
|
+
this.setSize(this.lastSize.width, this.lastSize.height, true);
|
|
1902
|
+
break;
|
|
1903
|
+
case "blur":
|
|
1904
|
+
this.uniforms.get("blur").value = value;
|
|
1905
|
+
break;
|
|
1906
|
+
case "blurSharpness":
|
|
1907
|
+
this.uniforms.get("blurSharpness").value = value;
|
|
1908
|
+
break;
|
|
1909
|
+
case "blurKernel":
|
|
1910
|
+
this.uniforms.get("blurKernel").value = value;
|
|
1911
|
+
break;
|
|
1912
|
+
case "steps":
|
|
1913
|
+
this.reflectionsPass.fullscreenMaterial.defines.steps = parseInt(value);
|
|
1914
|
+
this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
|
|
1915
|
+
break;
|
|
1916
|
+
case "refineSteps":
|
|
1917
|
+
this.reflectionsPass.fullscreenMaterial.defines.refineSteps = parseInt(value);
|
|
1918
|
+
this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
|
|
1919
|
+
break;
|
|
1920
|
+
case "missedRays":
|
|
1921
|
+
if (value) {
|
|
1922
|
+
this.reflectionsPass.fullscreenMaterial.defines.missedRays = "";
|
|
1923
|
+
} else {
|
|
1924
|
+
delete this.reflectionsPass.fullscreenMaterial.defines.missedRays;
|
|
1925
|
+
}
|
|
1926
|
+
this.reflectionsPass.fullscreenMaterial.needsUpdate = needsUpdate;
|
|
1927
|
+
break;
|
|
1928
|
+
case "correctionRadius":
|
|
1929
|
+
this.temporalResolvePass.fullscreenMaterial.defines.correctionRadius = Math.round(value);
|
|
1930
|
+
this.temporalResolvePass.fullscreenMaterial.needsUpdate = needsUpdate;
|
|
1931
|
+
break;
|
|
1932
|
+
case "blend":
|
|
1933
|
+
this.temporalResolvePass.fullscreenMaterial.uniforms.blend.value = value;
|
|
1934
|
+
break;
|
|
1935
|
+
case "correction":
|
|
1936
|
+
this.temporalResolvePass.fullscreenMaterial.uniforms.correction.value = value;
|
|
1937
|
+
break;
|
|
1938
|
+
case "exponent":
|
|
1939
|
+
this.temporalResolvePass.fullscreenMaterial.uniforms.exponent.value = value;
|
|
1940
|
+
break;
|
|
1941
|
+
case "distance":
|
|
1942
|
+
reflectionPassFullscreenMaterialUniforms.rayDistance.value = value;
|
|
1943
|
+
default:
|
|
1944
|
+
if (reflectionPassFullscreenMaterialUniformsKeys.includes(key)) {
|
|
1945
|
+
reflectionPassFullscreenMaterialUniforms[key].value = value;
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
}
|
|
1949
|
+
});
|
|
1950
|
+
this[key] = options[key];
|
|
1951
|
+
}
|
|
1952
|
+
needsUpdate = true;
|
|
1953
|
+
}
|
|
1954
|
+
setSize(width, height, force = false) {
|
|
1955
|
+
if (!force && width === this.lastSize.width && height === this.lastSize.height && this.resolutionScale === this.lastSize.resolutionScale && this.velocityResolutionScale === this.lastSize.velocityResolutionScale)
|
|
1956
|
+
return;
|
|
1957
|
+
this.temporalResolvePass.setSize(width, height);
|
|
1958
|
+
this.reflectionsPass.setSize(width, height);
|
|
1959
|
+
this.lastSize = {
|
|
1960
|
+
width,
|
|
1961
|
+
height,
|
|
1962
|
+
resolutionScale: this.resolutionScale,
|
|
1963
|
+
velocityResolutionScale: this.velocityResolutionScale
|
|
1964
|
+
};
|
|
1965
|
+
}
|
|
1966
|
+
generateBoxProjectedEnvMapFallback(renderer, position = new Vector3(), size = new Vector3(), envMapSize = 512) {
|
|
1967
|
+
this.cubeCamera.renderTarget.dispose();
|
|
1968
|
+
this.cubeCamera.renderTarget = new WebGLCubeRenderTarget(envMapSize);
|
|
1969
|
+
this.cubeCamera.position.copy(position);
|
|
1970
|
+
this.cubeCamera.updateMatrixWorld();
|
|
1971
|
+
this.cubeCamera.update(renderer, this._scene);
|
|
1972
|
+
if (!pmremGenerator) {
|
|
1973
|
+
pmremGenerator = new PMREMGenerator(renderer);
|
|
1974
|
+
pmremGenerator.compileCubemapShader();
|
|
1975
|
+
}
|
|
1976
|
+
const envMap = pmremGenerator.fromCubemap(this.cubeCamera.renderTarget.texture).texture;
|
|
1977
|
+
envMap.minFilter = LinearFilter;
|
|
1978
|
+
envMap.magFilter = LinearFilter;
|
|
1979
|
+
const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
|
|
1980
|
+
useBoxProjectedEnvMap(reflectionsMaterial, position, size);
|
|
1981
|
+
reflectionsMaterial.fragmentShader = reflectionsMaterial.fragmentShader.replace("vec3 worldPos", "worldPos").replace("varying vec3 vWorldPosition;", "vec3 worldPos;");
|
|
1982
|
+
reflectionsMaterial.uniforms.envMapPosition.value.copy(position);
|
|
1983
|
+
reflectionsMaterial.uniforms.envMapSize.value.copy(size);
|
|
1984
|
+
setupEnvMap(reflectionsMaterial, envMap, envMapSize);
|
|
1985
|
+
this.usingBoxProjectedEnvMap = true;
|
|
1986
|
+
return envMap;
|
|
1987
|
+
}
|
|
1988
|
+
setIBLRadiance(iblRadiance, renderer) {
|
|
1989
|
+
this._scene.traverse((c) => {
|
|
1990
|
+
if (c.material) {
|
|
1991
|
+
var _renderer$properties$;
|
|
1992
|
+
const uniforms = (_renderer$properties$ = renderer.properties.get(c.material)) == null ? void 0 : _renderer$properties$.uniforms;
|
|
1993
|
+
if (uniforms && "disableIBLRadiance" in uniforms) {
|
|
1994
|
+
uniforms.disableIBLRadiance.value = iblRadiance;
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
});
|
|
1998
|
+
}
|
|
1999
|
+
deleteBoxProjectedEnvMapFallback() {
|
|
2000
|
+
const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
|
|
2001
|
+
reflectionsMaterial.uniforms.envMap.value = null;
|
|
2002
|
+
reflectionsMaterial.fragmentShader = reflectionsMaterial.fragmentShader.replace("worldPos = ", "vec3 worldPos = ");
|
|
2003
|
+
delete reflectionsMaterial.defines.BOX_PROJECTED_ENV_MAP;
|
|
2004
|
+
reflectionsMaterial.needsUpdate = true;
|
|
2005
|
+
this.usingBoxProjectedEnvMap = false;
|
|
2006
|
+
}
|
|
2007
|
+
dispose() {
|
|
2008
|
+
super.dispose();
|
|
2009
|
+
this.reflectionsPass.dispose();
|
|
2010
|
+
this.temporalResolvePass.dispose();
|
|
2011
|
+
}
|
|
2012
|
+
update(renderer, inputBuffer) {
|
|
2013
|
+
if (!this.usingBoxProjectedEnvMap && this._scene.environment) {
|
|
2014
|
+
const reflectionsMaterial = this.reflectionsPass.fullscreenMaterial;
|
|
2015
|
+
let envMap = null;
|
|
2016
|
+
this._scene.traverse((c) => {
|
|
2017
|
+
if (!envMap && c.material && !c.material.envMap) {
|
|
2018
|
+
const properties = renderer.properties.get(c.material);
|
|
2019
|
+
if ("envMap" in properties && properties.envMap instanceof Texture$1)
|
|
2020
|
+
envMap = properties.envMap;
|
|
2021
|
+
}
|
|
2022
|
+
});
|
|
2023
|
+
if (envMap) {
|
|
2024
|
+
const envMapCubeUVHeight = this._scene.environment.image.height;
|
|
2025
|
+
setupEnvMap(reflectionsMaterial, envMap, envMapCubeUVHeight);
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
this.haltonIndex = (this.haltonIndex + 1) % this.haltonSequence.length;
|
|
2029
|
+
const [x, y] = this.haltonSequence[this.haltonIndex];
|
|
2030
|
+
const { width, height } = this.lastSize;
|
|
2031
|
+
this.temporalResolvePass.velocityPass.render(renderer);
|
|
2032
|
+
if (this._camera.setViewOffset)
|
|
2033
|
+
this._camera.setViewOffset(width, height, x, y, width, height);
|
|
2034
|
+
this.reflectionsPass.render(renderer, inputBuffer);
|
|
2035
|
+
this.temporalResolvePass.render(renderer);
|
|
2036
|
+
this._camera.clearViewOffset();
|
|
2037
|
+
}
|
|
2038
|
+
static patchDirectEnvIntensity(envMapIntensity = 0) {
|
|
2039
|
+
if (envMapIntensity === 0) {
|
|
2040
|
+
ShaderChunk.envmap_physical_pars_fragment = ShaderChunk.envmap_physical_pars_fragment.replace(
|
|
2041
|
+
"vec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) {",
|
|
2042
|
+
"vec3 getIBLRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness ) { return vec3(0.0);"
|
|
2043
|
+
);
|
|
2044
|
+
} else {
|
|
2045
|
+
ShaderChunk.envmap_physical_pars_fragment = ShaderChunk.envmap_physical_pars_fragment.replace(
|
|
2046
|
+
"vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );",
|
|
2047
|
+
"vec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness ) * " + envMapIntensity.toFixed(5) + ";"
|
|
2048
|
+
);
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
}
|
|
767
2052
|
const SSR = forwardRef(function SSR2({ ENABLE_BLUR = true, USE_MRT = true, ...props }, ref) {
|
|
768
2053
|
const { invalidate } = useThree();
|
|
769
2054
|
const { scene, camera } = useContext(EffectComposerContext);
|
|
@@ -2289,6 +3574,8 @@ export {
|
|
|
2289
3574
|
Grid,
|
|
2290
3575
|
HueSaturation,
|
|
2291
3576
|
LUT,
|
|
3577
|
+
LensFlare,
|
|
3578
|
+
LensFlareEffect,
|
|
2292
3579
|
N8AO,
|
|
2293
3580
|
Noise,
|
|
2294
3581
|
Outline,
|