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.
- package/README.md +391 -394
- package/package.json +52 -52
- package/src/CompositePass.js +223 -214
- package/src/CopyPass.js +75 -0
- package/src/DenoisePass.js +207 -202
- package/src/GBufferPass.js +228 -228
- package/src/RTLightingPass.js +730 -680
- package/src/RealtimeRaytracer.js +834 -730
- package/src/RestirPass.js +12 -2
- package/src/SceneCompiler.js +447 -424
- package/src/TAAPass.js +255 -230
- package/src/VolumetricPass.js +398 -329
- package/src/blueNoise.js +17 -17
- package/src/index.d.ts +20 -2
package/src/DenoisePass.js
CHANGED
|
@@ -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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
float
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
this.
|
|
150
|
-
this.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
u
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
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
|
+
}
|