three-realtime-rt 0.3.0 → 0.3.2

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,202 +1,207 @@
1
- import * as THREE from "three";
2
-
3
- const fullscreenVert = /* glsl */ `
4
- out vec2 vUv;
5
- void main() {
6
- vUv = uv;
7
- gl_Position = vec4(position.xy, 0.0, 1.0);
8
- }
9
- `;
10
-
11
- const atrousFrag = /* glsl */ `
12
- precision highp float;
13
-
14
- layout(location = 0) out vec4 outColor;
15
-
16
- in vec2 vUv;
17
-
18
- uniform sampler2D uIrradiance; // rgb = irradiance, a = history count
19
- uniform sampler2D uGWorldPos; // full-res guides
20
- uniform sampler2D uGNormalMetal;
21
- uniform vec2 uTexelSize; // of the irradiance target
22
- uniform float uStep; // à-trous step: 1, 2, 4, ...
23
- uniform vec3 uCameraPos;
24
- uniform float uEps;
25
- uniform float uLumSigma;
26
-
27
- float luminance(vec3 c) {
28
- return dot(c, vec3(0.299, 0.587, 0.114));
29
- }
30
-
31
- void main() {
32
- vec4 center = texture(uIrradiance, vUv);
33
- vec4 wp = texture(uGWorldPos, vUv);
34
- if (wp.w < 0.5) {
35
- outColor = center;
36
- return;
37
- }
38
- vec3 P = wp.xyz;
39
- vec4 nm = texture(uGNormalMetal, vUv);
40
- vec3 N = normalize(nm.xyz);
41
- // Specular surfaces (mirror metals, glass) carry traced reflections whose
42
- // detail is NOT in the G-buffer guides — filtering would smear them, and
43
- // their signal is nearly deterministic anyway. Scale the filter down as the
44
- // surface gets more mirror-like.
45
- float matW = nm.w;
46
- float specAmount = matW >= 2.0 ? clamp(matW - 2.0, 0.0, 1.0) : matW;
47
- float specKeep = specAmount * (1.0 - clamp(wp.w - 1.0, 0.0, 1.0));
48
-
49
- // Fewer accumulated samples -> noisier pixel -> wider luminance tolerance.
50
- // A converged pixel (high count) is barely touched, preserving detail.
51
- float count = max(center.a, 1.0);
52
- float sigmaL = uLumSigma * clamp(8.0 / sqrt(count), 0.75, 8.0);
53
-
54
- float distToCam = distance(P, uCameraPos);
55
- float planeTol = 0.01 * distToCam + 20.0 * uEps;
56
-
57
- // Despeckle (first iteration, short history only): a freshly disoccluded
58
- // pixel can carry one huge sample that the center-weighted filter would
59
- // preserve as a bright "rain drop" at silhouettes. Such a pixel has no
60
- // business being brighter than its entire neighbourhood clamp its
61
- // luminance to the brightest neighbour. Converged pixels are exempt, so
62
- // real small highlights survive.
63
- if (uStep < 1.5 && count < 8.0) {
64
- float maxL = 0.0;
65
- float found = 0.0;
66
- for (int dy = -1; dy <= 1; dy++) {
67
- for (int dx = -1; dx <= 1; dx++) {
68
- if (dx == 0 && dy == 0) continue;
69
- vec2 tuv = vUv + vec2(float(dx), float(dy)) * uTexelSize;
70
- if (tuv.x < 0.0 || tuv.x > 1.0 || tuv.y < 0.0 || tuv.y > 1.0) continue;
71
- if (texture(uGWorldPos, tuv).w < 0.5) continue;
72
- maxL = max(maxL, luminance(texture(uIrradiance, tuv).rgb));
73
- found = 1.0;
74
- }
75
- }
76
- float cap = maxL * 1.25 + 1e-4;
77
- float l = luminance(center.rgb);
78
- if (found > 0.5 && l > cap) center.rgb *= cap / l;
79
- }
80
-
81
- float lumC = luminance(center.rgb);
82
-
83
- // 3x3 B-spline-ish kernel, edge-avoiding weights.
84
- vec3 sum = center.rgb * 4.0;
85
- float wsum = 4.0;
86
- for (int dy = -1; dy <= 1; dy++) {
87
- for (int dx = -1; dx <= 1; dx++) {
88
- if (dx == 0 && dy == 0) continue;
89
- vec2 tuv = vUv + vec2(float(dx), float(dy)) * uStep * uTexelSize;
90
- if (tuv.x < 0.0 || tuv.x > 1.0 || tuv.y < 0.0 || tuv.y > 1.0) continue;
91
-
92
- vec4 g = texture(uGWorldPos, tuv);
93
- if (g.w < 0.5) continue;
94
- vec4 s = texture(uIrradiance, tuv);
95
- vec3 Nt = normalize(texture(uGNormalMetal, tuv).xyz);
96
-
97
- float k = (dx == 0 || dy == 0) ? 2.0 : 1.0;
98
- float wN = pow(max(dot(N, Nt), 0.0), 32.0);
99
- float wZ = exp(-abs(dot(g.xyz - P, N)) / planeTol);
100
- // Tighten the luminance gate as the à-trous step widens: a shadow on a
101
- // flat floor has no geometric edge to protect it, so at high iteration
102
- // counts the wide passes would average it away ("floating" objects with
103
- // no contact shadow). Wide steps only get to blend near-equal luminance.
104
- float wL = exp(-abs(luminance(s.rgb) - lumC) / (sigmaL * inversesqrt(uStep)));
105
- float w = k * wN * wZ * wL * (1.0 - specKeep);
106
- sum += s.rgb * w;
107
- wsum += w;
108
- }
109
- }
110
- outColor = vec4(sum / wsum, center.a);
111
- }
112
- `;
113
-
114
- /**
115
- * Edge-avoiding à-trous wavelet filter (SVGF-lite) on the demodulated
116
- * irradiance buffer, guided by the full-res G-buffer (plane distance +
117
- * normals) and modulated by per-pixel history count: freshly disoccluded
118
- * pixels get blurred hard, converged pixels stay crisp.
119
- */
120
- export class DenoisePass {
121
- constructor(width, height) {
122
- this.targetA = this._makeTarget(width, height);
123
- this.targetB = this._makeTarget(width, height);
124
-
125
- this.material = new THREE.ShaderMaterial({
126
- glslVersion: THREE.GLSL3,
127
- vertexShader: fullscreenVert,
128
- fragmentShader: atrousFrag,
129
- uniforms: {
130
- uIrradiance: { value: null },
131
- uGWorldPos: { value: null },
132
- uGNormalMetal: { value: null },
133
- uTexelSize: { value: new THREE.Vector2() },
134
- uStep: { value: 1 },
135
- uCameraPos: { value: new THREE.Vector3() },
136
- uEps: { value: 1e-3 },
137
- uLumSigma: { value: 0.25 },
138
- },
139
- depthTest: false,
140
- depthWrite: false,
141
- });
142
-
143
- this.scene = new THREE.Scene();
144
- this.camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
145
- this.quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.material);
146
- this.quad.frustumCulled = false;
147
- this.scene.add(this.quad);
148
-
149
- this._width = width;
150
- this._height = height;
151
- }
152
-
153
- _makeTarget(width, height) {
154
- const t = new THREE.WebGLRenderTarget(width, height, {
155
- minFilter: THREE.LinearFilter,
156
- magFilter: THREE.LinearFilter,
157
- format: THREE.RGBAFormat,
158
- type: THREE.HalfFloatType,
159
- depthBuffer: false,
160
- stencilBuffer: false,
161
- });
162
- t.texture.generateMipmaps = false;
163
- return t;
164
- }
165
-
166
- setSize(width, height) {
167
- this._width = width;
168
- this._height = height;
169
- this.targetA.setSize(width, height);
170
- this.targetB.setSize(width, height);
171
- }
172
-
173
- /** Runs `iterations` à-trous passes; returns the final filtered texture. */
174
- render(renderer, inputTexture, gbuffer, cameraPos, eps, iterations = 3) {
175
- const u = this.material.uniforms;
176
- u.uGWorldPos.value = gbuffer.worldPos;
177
- u.uGNormalMetal.value = gbuffer.normalMetal;
178
- u.uTexelSize.value.set(1 / this._width, 1 / this._height);
179
- u.uCameraPos.value.copy(cameraPos);
180
- u.uEps.value = eps;
181
-
182
- let read = inputTexture;
183
- let write = this.targetA;
184
- for (let i = 0; i < iterations; i++) {
185
- u.uIrradiance.value = read;
186
- u.uStep.value = 1 << i;
187
- renderer.setRenderTarget(write);
188
- renderer.render(this.scene, this.camera);
189
- read = write.texture;
190
- write = write === this.targetA ? this.targetB : this.targetA;
191
- }
192
- renderer.setRenderTarget(null);
193
- return read;
194
- }
195
-
196
- dispose() {
197
- this.targetA.dispose();
198
- this.targetB.dispose();
199
- this.material.dispose();
200
- this.quad.geometry.dispose();
201
- }
202
- }
1
+ import * as THREE from "three";
2
+
3
+ const fullscreenVert = /* glsl */ `
4
+ out vec2 vUv;
5
+ void main() {
6
+ vUv = uv;
7
+ gl_Position = vec4(position.xy, 0.0, 1.0);
8
+ }
9
+ `;
10
+
11
+ const atrousFrag = /* glsl */ `
12
+ precision highp float;
13
+
14
+ layout(location = 0) out vec4 outColor;
15
+
16
+ in vec2 vUv;
17
+
18
+ uniform sampler2D uIrradiance; // rgb = irradiance, a = history count
19
+ uniform sampler2D uGWorldPos; // full-res guides
20
+ uniform sampler2D uGNormalMetal;
21
+ uniform vec2 uTexelSize; // of the irradiance target
22
+ uniform float uStep; // à-trous step: 1, 2, 4, ...
23
+ uniform vec3 uCameraPos;
24
+ uniform float uEps;
25
+ uniform float uLumSigma;
26
+
27
+ float luminance(vec3 c) {
28
+ return dot(c, vec3(0.299, 0.587, 0.114));
29
+ }
30
+
31
+ void main() {
32
+ vec4 center = texture(uIrradiance, vUv);
33
+ vec4 wp = texture(uGWorldPos, vUv);
34
+ if (wp.w < 0.5) {
35
+ outColor = center;
36
+ return;
37
+ }
38
+ vec3 P = wp.xyz;
39
+ vec4 nm = texture(uGNormalMetal, vUv);
40
+ vec3 N = normalize(nm.xyz);
41
+ // Specular surfaces (mirror metals, glass) carry traced reflections whose
42
+ // detail is NOT in the G-buffer guides — filtering would smear them, and
43
+ // their signal is nearly deterministic anyway. Scale the filter down as the
44
+ // surface gets more mirror-like.
45
+ float matW = nm.w;
46
+ float specAmount = matW >= 2.0 ? clamp(matW - 2.0, 0.0, 1.0) : matW;
47
+ float specKeep = specAmount * (1.0 - clamp(wp.w - 1.0, 0.0, 1.0));
48
+
49
+ // Fewer accumulated samples -> noisier pixel -> wider luminance tolerance.
50
+ // A converged pixel (high count) is barely touched, preserving detail.
51
+ // The widening is CAPPED at 3x: during camera motion every pixel is fresh,
52
+ // and an 8x-wide gate across five à-trous passes erased small contact
53
+ // shadows entirely — objects visibly floated while orbiting ("ghostly
54
+ // apparitions") and only grounded once the camera stopped. Blue-noise
55
+ // sampling + ReSTIR keep fresh pixels clean enough for the tighter gate.
56
+ float count = max(center.a, 1.0);
57
+ float sigmaL = uLumSigma * clamp(8.0 / sqrt(count), 0.75, 3.0);
58
+
59
+ float distToCam = distance(P, uCameraPos);
60
+ float planeTol = 0.01 * distToCam + 20.0 * uEps;
61
+
62
+ // Despeckle (first iteration, short history only): a freshly disoccluded
63
+ // pixel can carry one huge sample that the center-weighted filter would
64
+ // preserve as a bright "rain drop" at silhouettes. Such a pixel has no
65
+ // business being brighter than its entire neighbourhood — clamp its
66
+ // luminance to the brightest neighbour. Converged pixels are exempt, so
67
+ // real small highlights survive.
68
+ if (uStep < 1.5 && count < 8.0) {
69
+ float maxL = 0.0;
70
+ float found = 0.0;
71
+ for (int dy = -1; dy <= 1; dy++) {
72
+ for (int dx = -1; dx <= 1; dx++) {
73
+ if (dx == 0 && dy == 0) continue;
74
+ vec2 tuv = vUv + vec2(float(dx), float(dy)) * uTexelSize;
75
+ if (tuv.x < 0.0 || tuv.x > 1.0 || tuv.y < 0.0 || tuv.y > 1.0) continue;
76
+ if (texture(uGWorldPos, tuv).w < 0.5) continue;
77
+ maxL = max(maxL, luminance(texture(uIrradiance, tuv).rgb));
78
+ found = 1.0;
79
+ }
80
+ }
81
+ float cap = maxL * 1.25 + 1e-4;
82
+ float l = luminance(center.rgb);
83
+ if (found > 0.5 && l > cap) center.rgb *= cap / l;
84
+ }
85
+
86
+ float lumC = luminance(center.rgb);
87
+
88
+ // 3x3 B-spline-ish kernel, edge-avoiding weights.
89
+ vec3 sum = center.rgb * 4.0;
90
+ float wsum = 4.0;
91
+ for (int dy = -1; dy <= 1; dy++) {
92
+ for (int dx = -1; dx <= 1; dx++) {
93
+ if (dx == 0 && dy == 0) continue;
94
+ vec2 tuv = vUv + vec2(float(dx), float(dy)) * uStep * uTexelSize;
95
+ if (tuv.x < 0.0 || tuv.x > 1.0 || tuv.y < 0.0 || tuv.y > 1.0) continue;
96
+
97
+ vec4 g = texture(uGWorldPos, tuv);
98
+ if (g.w < 0.5) continue;
99
+ vec4 s = texture(uIrradiance, tuv);
100
+ vec3 Nt = normalize(texture(uGNormalMetal, tuv).xyz);
101
+
102
+ float k = (dx == 0 || dy == 0) ? 2.0 : 1.0;
103
+ float wN = pow(max(dot(N, Nt), 0.0), 32.0);
104
+ float wZ = exp(-abs(dot(g.xyz - P, N)) / planeTol);
105
+ // Tighten the luminance gate as the à-trous step widens: a shadow on a
106
+ // flat floor has no geometric edge to protect it, so at high iteration
107
+ // counts the wide passes would average it away ("floating" objects with
108
+ // no contact shadow). Wide steps only get to blend near-equal luminance.
109
+ float wL = exp(-abs(luminance(s.rgb) - lumC) / (sigmaL * inversesqrt(uStep)));
110
+ float w = k * wN * wZ * wL * (1.0 - specKeep);
111
+ sum += s.rgb * w;
112
+ wsum += w;
113
+ }
114
+ }
115
+ outColor = vec4(sum / wsum, center.a);
116
+ }
117
+ `;
118
+
119
+ /**
120
+ * Edge-avoiding à-trous wavelet filter (SVGF-lite) on the demodulated
121
+ * irradiance buffer, guided by the full-res G-buffer (plane distance +
122
+ * normals) and modulated by per-pixel history count: freshly disoccluded
123
+ * pixels get blurred hard, converged pixels stay crisp.
124
+ */
125
+ export class DenoisePass {
126
+ constructor(width, height) {
127
+ this.targetA = this._makeTarget(width, height);
128
+ this.targetB = this._makeTarget(width, height);
129
+
130
+ this.material = new THREE.ShaderMaterial({
131
+ glslVersion: THREE.GLSL3,
132
+ vertexShader: fullscreenVert,
133
+ fragmentShader: atrousFrag,
134
+ uniforms: {
135
+ uIrradiance: { value: null },
136
+ uGWorldPos: { value: null },
137
+ uGNormalMetal: { value: null },
138
+ uTexelSize: { value: new THREE.Vector2() },
139
+ uStep: { value: 1 },
140
+ uCameraPos: { value: new THREE.Vector3() },
141
+ uEps: { value: 1e-3 },
142
+ uLumSigma: { value: 0.25 },
143
+ },
144
+ depthTest: false,
145
+ depthWrite: false,
146
+ });
147
+
148
+ this.scene = new THREE.Scene();
149
+ this.camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
150
+ this.quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.material);
151
+ this.quad.frustumCulled = false;
152
+ this.scene.add(this.quad);
153
+
154
+ this._width = width;
155
+ this._height = height;
156
+ }
157
+
158
+ _makeTarget(width, height) {
159
+ const t = new THREE.WebGLRenderTarget(width, height, {
160
+ minFilter: THREE.LinearFilter,
161
+ magFilter: THREE.LinearFilter,
162
+ format: THREE.RGBAFormat,
163
+ type: THREE.HalfFloatType,
164
+ depthBuffer: false,
165
+ stencilBuffer: false,
166
+ });
167
+ t.texture.generateMipmaps = false;
168
+ return t;
169
+ }
170
+
171
+ setSize(width, height) {
172
+ this._width = width;
173
+ this._height = height;
174
+ this.targetA.setSize(width, height);
175
+ this.targetB.setSize(width, height);
176
+ }
177
+
178
+ /** Runs `iterations` à-trous passes; returns the final filtered texture. */
179
+ render(renderer, inputTexture, gbuffer, cameraPos, eps, iterations = 3) {
180
+ const u = this.material.uniforms;
181
+ u.uGWorldPos.value = gbuffer.worldPos;
182
+ u.uGNormalMetal.value = gbuffer.normalMetal;
183
+ u.uTexelSize.value.set(1 / this._width, 1 / this._height);
184
+ u.uCameraPos.value.copy(cameraPos);
185
+ u.uEps.value = eps;
186
+
187
+ let read = inputTexture;
188
+ let write = this.targetA;
189
+ for (let i = 0; i < iterations; i++) {
190
+ u.uIrradiance.value = read;
191
+ u.uStep.value = 1 << i;
192
+ renderer.setRenderTarget(write);
193
+ renderer.render(this.scene, this.camera);
194
+ read = write.texture;
195
+ write = write === this.targetA ? this.targetB : this.targetA;
196
+ }
197
+ renderer.setRenderTarget(null);
198
+ return read;
199
+ }
200
+
201
+ dispose() {
202
+ this.targetA.dispose();
203
+ this.targetB.dispose();
204
+ this.material.dispose();
205
+ this.quad.geometry.dispose();
206
+ }
207
+ }