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/GBufferPass.js
CHANGED
|
@@ -1,228 +1,228 @@
|
|
|
1
|
-
import * as THREE from "three";
|
|
2
|
-
|
|
3
|
-
const gbufferVert = /* glsl */ `
|
|
4
|
-
out vec3 vWorldPos;
|
|
5
|
-
out vec3 vWorldNormal;
|
|
6
|
-
out vec2 vUvCoord;
|
|
7
|
-
|
|
8
|
-
uniform mat3 uNormalMatrixWorld;
|
|
9
|
-
|
|
10
|
-
void main() {
|
|
11
|
-
vec4 wp = modelMatrix * vec4(position, 1.0);
|
|
12
|
-
vWorldPos = wp.xyz;
|
|
13
|
-
vWorldNormal = normalize(uNormalMatrixWorld * normal);
|
|
14
|
-
vUvCoord = uv;
|
|
15
|
-
gl_Position = projectionMatrix * viewMatrix * wp;
|
|
16
|
-
}
|
|
17
|
-
`;
|
|
18
|
-
|
|
19
|
-
const gbufferFrag = /* glsl */ `
|
|
20
|
-
precision highp float;
|
|
21
|
-
|
|
22
|
-
layout(location = 0) out vec4 gAlbedoRough;
|
|
23
|
-
layout(location = 1) out vec4 gNormalMetal;
|
|
24
|
-
layout(location = 2) out vec4 gWorldPos;
|
|
25
|
-
layout(location = 3) out vec4 gEmissive;
|
|
26
|
-
|
|
27
|
-
in vec3 vWorldPos;
|
|
28
|
-
in vec3 vWorldNormal;
|
|
29
|
-
in vec2 vUvCoord;
|
|
30
|
-
|
|
31
|
-
uniform vec3 uColor;
|
|
32
|
-
uniform float uRoughness;
|
|
33
|
-
uniform float uMetalness;
|
|
34
|
-
uniform float uTransmission;
|
|
35
|
-
uniform vec3 uEmissive;
|
|
36
|
-
uniform sampler2D uMap;
|
|
37
|
-
uniform bool uHasMap;
|
|
38
|
-
uniform sampler2D uEmissiveMap;
|
|
39
|
-
uniform bool uHasEmissiveMap;
|
|
40
|
-
|
|
41
|
-
void main() {
|
|
42
|
-
vec3 albedo = uColor;
|
|
43
|
-
if (uHasMap) {
|
|
44
|
-
albedo *= texture(uMap, vUvCoord).rgb;
|
|
45
|
-
}
|
|
46
|
-
vec3 emissive = uEmissive;
|
|
47
|
-
if (uHasEmissiveMap) {
|
|
48
|
-
emissive *= texture(uEmissiveMap, vUvCoord).rgb;
|
|
49
|
-
}
|
|
50
|
-
vec3 n = normalize(vWorldNormal) * (gl_FrontFacing ? 1.0 : -1.0);
|
|
51
|
-
gAlbedoRough = vec4(albedo, uRoughness);
|
|
52
|
-
// .w is a packed material word: >= 2 means transmissive glass (w - 2 =
|
|
53
|
-
// transmission amount), else it is plain metalness. Lets the lighting pass
|
|
54
|
-
// read specular/glass properties without an extra G-buffer sampler (it
|
|
55
|
-
// already sits at the WebGL2 16-sampler minimum).
|
|
56
|
-
gNormalMetal = vec4(n, uTransmission > 0.0 ? 2.0 + uTransmission : uMetalness);
|
|
57
|
-
// .w packs the valid flag AND roughness: 0 = background, 1 + roughness
|
|
58
|
-
// otherwise. Every consumer only tests w < 0.5, so this stays compatible.
|
|
59
|
-
gWorldPos = vec4(vWorldPos, 1.0 + uRoughness);
|
|
60
|
-
gEmissive = vec4(emissive, 1.0);
|
|
61
|
-
}
|
|
62
|
-
`;
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Rasterizes the scene into a 4-target G-buffer (all RGBA32F):
|
|
66
|
-
* [0] albedo.rgb + roughness [1] worldNormal.xyz + metalness
|
|
67
|
-
* [2] worldPos.xyz + validFlag [3] emissive.rgb
|
|
68
|
-
*
|
|
69
|
-
* Uses a per-mesh material swap (cached) so each mesh's Standard/Basic/Lambert/Phong
|
|
70
|
-
* material properties flow into the buffer without touching user materials.
|
|
71
|
-
*/
|
|
72
|
-
export class GBufferPass {
|
|
73
|
-
constructor(width, height, { mixedPrecision = true } = {}) {
|
|
74
|
-
// Mixed fp16/fp32 attachments are legal WebGL2 but some implementations
|
|
75
|
-
// (notably Apple's Metal backend) may reject the framebuffer — the caller
|
|
76
|
-
// probes support and passes the verdict here.
|
|
77
|
-
this._mixedPrecision = mixedPrecision;
|
|
78
|
-
// Two G-buffers, ping-ponged each frame: the previous frame's worldPos +
|
|
79
|
-
// normals are needed to validate reprojected history (stage 2).
|
|
80
|
-
this._targets = [
|
|
81
|
-
this._makeTarget(width, height),
|
|
82
|
-
this._makeTarget(width, height),
|
|
83
|
-
];
|
|
84
|
-
this._current = 0;
|
|
85
|
-
|
|
86
|
-
this._materialCache = new WeakMap(); // mesh -> gbuffer ShaderMaterial
|
|
87
|
-
this._swapped = []; // [mesh, originalMaterial] pairs during render
|
|
88
|
-
this._normalMat3 = new THREE.Matrix3();
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
_makeTarget(width, height) {
|
|
92
|
-
const t = new THREE.WebGLMultipleRenderTargets(width, height, 4, {
|
|
93
|
-
minFilter: THREE.NearestFilter,
|
|
94
|
-
magFilter: THREE.NearestFilter,
|
|
95
|
-
type: THREE.FloatType,
|
|
96
|
-
depthBuffer: true,
|
|
97
|
-
});
|
|
98
|
-
for (const tex of t.texture) tex.generateMipmaps = false;
|
|
99
|
-
// Mixed precision: only world position (reprojection + plane-distance
|
|
100
|
-
// tests) needs fp32. Albedo/normal/emissive are fine in fp16, which halves
|
|
101
|
-
// G-buffer bandwidth on 3 of the 4 targets — a large win on mobile GPUs.
|
|
102
|
-
if (this._mixedPrecision) {
|
|
103
|
-
t.texture[0].type = THREE.HalfFloatType; // albedo + roughness
|
|
104
|
-
t.texture[1].type = THREE.HalfFloatType; // normal + packed material word
|
|
105
|
-
t.texture[3].type = THREE.HalfFloatType; // emissive
|
|
106
|
-
}
|
|
107
|
-
return t;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get target() {
|
|
111
|
-
return this._targets[this._current];
|
|
112
|
-
}
|
|
113
|
-
get _prev() {
|
|
114
|
-
return this._targets[1 - this._current];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
get albedoRough() {
|
|
118
|
-
return this.target.texture[0];
|
|
119
|
-
}
|
|
120
|
-
get normalMetal() {
|
|
121
|
-
return this.target.texture[1];
|
|
122
|
-
}
|
|
123
|
-
get worldPos() {
|
|
124
|
-
return this.target.texture[2];
|
|
125
|
-
}
|
|
126
|
-
get emissive() {
|
|
127
|
-
return this.target.texture[3];
|
|
128
|
-
}
|
|
129
|
-
get prevNormalMetal() {
|
|
130
|
-
return this._prev.texture[1];
|
|
131
|
-
}
|
|
132
|
-
get prevWorldPos() {
|
|
133
|
-
return this._prev.texture[2];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
setSize(width, height) {
|
|
137
|
-
for (const t of this._targets) t.setSize(width, height);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
_gbufferMaterialFor(mesh) {
|
|
141
|
-
let material = this._materialCache.get(mesh);
|
|
142
|
-
if (!material) {
|
|
143
|
-
material = new THREE.ShaderMaterial({
|
|
144
|
-
glslVersion: THREE.GLSL3,
|
|
145
|
-
vertexShader: gbufferVert,
|
|
146
|
-
fragmentShader: gbufferFrag,
|
|
147
|
-
uniforms: {
|
|
148
|
-
uNormalMatrixWorld: { value: new THREE.Matrix3() },
|
|
149
|
-
uColor: { value: new THREE.Color(1, 1, 1) },
|
|
150
|
-
uRoughness: { value: 1.0 },
|
|
151
|
-
uMetalness: { value: 0.0 },
|
|
152
|
-
uTransmission: { value: 0.0 },
|
|
153
|
-
uEmissive: { value: new THREE.Color(0, 0, 0) },
|
|
154
|
-
uMap: { value: null },
|
|
155
|
-
uHasMap: { value: false },
|
|
156
|
-
uEmissiveMap: { value: null },
|
|
157
|
-
uHasEmissiveMap: { value: false },
|
|
158
|
-
},
|
|
159
|
-
side: THREE.FrontSide,
|
|
160
|
-
});
|
|
161
|
-
this._materialCache.set(mesh, material);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Sync properties from the user's material every frame (cheap).
|
|
165
|
-
const src = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material;
|
|
166
|
-
const u = material.uniforms;
|
|
167
|
-
if (src.color) u.uColor.value.copy(src.color);
|
|
168
|
-
u.uRoughness.value = src.roughness ?? 1.0;
|
|
169
|
-
u.uMetalness.value = src.metalness ?? 0.0;
|
|
170
|
-
u.uTransmission.value = src.transmission ?? 0.0; // MeshPhysicalMaterial
|
|
171
|
-
|
|
172
|
-
if (src.emissive) {
|
|
173
|
-
u.uEmissive.value
|
|
174
|
-
.copy(src.emissive)
|
|
175
|
-
.multiplyScalar(src.emissiveIntensity ?? 1);
|
|
176
|
-
}
|
|
177
|
-
u.uMap.value = src.map ?? null;
|
|
178
|
-
u.uHasMap.value = !!src.map;
|
|
179
|
-
u.uEmissiveMap.value = src.emissiveMap ?? null;
|
|
180
|
-
u.uHasEmissiveMap.value = !!src.emissiveMap;
|
|
181
|
-
u.uNormalMatrixWorld.value.getNormalMatrix(mesh.matrixWorld);
|
|
182
|
-
material.side = src.side ?? THREE.FrontSide;
|
|
183
|
-
return material;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
render(renderer, scene, camera) {
|
|
187
|
-
// Ping-pong: what was "current" becomes "previous".
|
|
188
|
-
this._current = 1 - this._current;
|
|
189
|
-
|
|
190
|
-
// Swap in G-buffer materials.
|
|
191
|
-
this._swapped.length = 0;
|
|
192
|
-
scene.traverse((obj) => {
|
|
193
|
-
if (obj.isMesh && obj.geometry && obj.visible) {
|
|
194
|
-
// The G-buffer has no alpha blending — a nearly invisible surface
|
|
195
|
-
// (glass display case, ghost meshes) would write itself opaque over
|
|
196
|
-
// everything behind it. Hide it from the raster pass instead.
|
|
197
|
-
const m = Array.isArray(obj.material) ? obj.material[0] : obj.material;
|
|
198
|
-
if (m.transparent && (m.opacity ?? 1) < 0.5) {
|
|
199
|
-
obj.visible = false;
|
|
200
|
-
this._swapped.push([obj, null]); // restore visibility after render
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
this._swapped.push([obj, obj.material]);
|
|
204
|
-
obj.material = this._gbufferMaterialFor(obj);
|
|
205
|
-
}
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
const prevBackground = scene.background;
|
|
209
|
-
scene.background = null; // background writes nothing; worldPos.w stays 0
|
|
210
|
-
|
|
211
|
-
renderer.setRenderTarget(this.target);
|
|
212
|
-
renderer.setClearColor(0x000000, 0);
|
|
213
|
-
renderer.clear(true, true, false);
|
|
214
|
-
renderer.render(scene, camera);
|
|
215
|
-
renderer.setRenderTarget(null);
|
|
216
|
-
|
|
217
|
-
scene.background = prevBackground;
|
|
218
|
-
for (const [mesh, mat] of this._swapped) {
|
|
219
|
-
if (mat === null) mesh.visible = true; // was hidden, not material-swapped
|
|
220
|
-
else mesh.material = mat;
|
|
221
|
-
}
|
|
222
|
-
this._swapped.length = 0;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
dispose() {
|
|
226
|
-
for (const t of this._targets) t.dispose();
|
|
227
|
-
}
|
|
228
|
-
}
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
|
|
3
|
+
const gbufferVert = /* glsl */ `
|
|
4
|
+
out vec3 vWorldPos;
|
|
5
|
+
out vec3 vWorldNormal;
|
|
6
|
+
out vec2 vUvCoord;
|
|
7
|
+
|
|
8
|
+
uniform mat3 uNormalMatrixWorld;
|
|
9
|
+
|
|
10
|
+
void main() {
|
|
11
|
+
vec4 wp = modelMatrix * vec4(position, 1.0);
|
|
12
|
+
vWorldPos = wp.xyz;
|
|
13
|
+
vWorldNormal = normalize(uNormalMatrixWorld * normal);
|
|
14
|
+
vUvCoord = uv;
|
|
15
|
+
gl_Position = projectionMatrix * viewMatrix * wp;
|
|
16
|
+
}
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
const gbufferFrag = /* glsl */ `
|
|
20
|
+
precision highp float;
|
|
21
|
+
|
|
22
|
+
layout(location = 0) out vec4 gAlbedoRough;
|
|
23
|
+
layout(location = 1) out vec4 gNormalMetal;
|
|
24
|
+
layout(location = 2) out vec4 gWorldPos;
|
|
25
|
+
layout(location = 3) out vec4 gEmissive;
|
|
26
|
+
|
|
27
|
+
in vec3 vWorldPos;
|
|
28
|
+
in vec3 vWorldNormal;
|
|
29
|
+
in vec2 vUvCoord;
|
|
30
|
+
|
|
31
|
+
uniform vec3 uColor;
|
|
32
|
+
uniform float uRoughness;
|
|
33
|
+
uniform float uMetalness;
|
|
34
|
+
uniform float uTransmission;
|
|
35
|
+
uniform vec3 uEmissive;
|
|
36
|
+
uniform sampler2D uMap;
|
|
37
|
+
uniform bool uHasMap;
|
|
38
|
+
uniform sampler2D uEmissiveMap;
|
|
39
|
+
uniform bool uHasEmissiveMap;
|
|
40
|
+
|
|
41
|
+
void main() {
|
|
42
|
+
vec3 albedo = uColor;
|
|
43
|
+
if (uHasMap) {
|
|
44
|
+
albedo *= texture(uMap, vUvCoord).rgb;
|
|
45
|
+
}
|
|
46
|
+
vec3 emissive = uEmissive;
|
|
47
|
+
if (uHasEmissiveMap) {
|
|
48
|
+
emissive *= texture(uEmissiveMap, vUvCoord).rgb;
|
|
49
|
+
}
|
|
50
|
+
vec3 n = normalize(vWorldNormal) * (gl_FrontFacing ? 1.0 : -1.0);
|
|
51
|
+
gAlbedoRough = vec4(albedo, uRoughness);
|
|
52
|
+
// .w is a packed material word: >= 2 means transmissive glass (w - 2 =
|
|
53
|
+
// transmission amount), else it is plain metalness. Lets the lighting pass
|
|
54
|
+
// read specular/glass properties without an extra G-buffer sampler (it
|
|
55
|
+
// already sits at the WebGL2 16-sampler minimum).
|
|
56
|
+
gNormalMetal = vec4(n, uTransmission > 0.0 ? 2.0 + uTransmission : uMetalness);
|
|
57
|
+
// .w packs the valid flag AND roughness: 0 = background, 1 + roughness
|
|
58
|
+
// otherwise. Every consumer only tests w < 0.5, so this stays compatible.
|
|
59
|
+
gWorldPos = vec4(vWorldPos, 1.0 + uRoughness);
|
|
60
|
+
gEmissive = vec4(emissive, 1.0);
|
|
61
|
+
}
|
|
62
|
+
`;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Rasterizes the scene into a 4-target G-buffer (all RGBA32F):
|
|
66
|
+
* [0] albedo.rgb + roughness [1] worldNormal.xyz + metalness
|
|
67
|
+
* [2] worldPos.xyz + validFlag [3] emissive.rgb
|
|
68
|
+
*
|
|
69
|
+
* Uses a per-mesh material swap (cached) so each mesh's Standard/Basic/Lambert/Phong
|
|
70
|
+
* material properties flow into the buffer without touching user materials.
|
|
71
|
+
*/
|
|
72
|
+
export class GBufferPass {
|
|
73
|
+
constructor(width, height, { mixedPrecision = true } = {}) {
|
|
74
|
+
// Mixed fp16/fp32 attachments are legal WebGL2 but some implementations
|
|
75
|
+
// (notably Apple's Metal backend) may reject the framebuffer — the caller
|
|
76
|
+
// probes support and passes the verdict here.
|
|
77
|
+
this._mixedPrecision = mixedPrecision;
|
|
78
|
+
// Two G-buffers, ping-ponged each frame: the previous frame's worldPos +
|
|
79
|
+
// normals are needed to validate reprojected history (stage 2).
|
|
80
|
+
this._targets = [
|
|
81
|
+
this._makeTarget(width, height),
|
|
82
|
+
this._makeTarget(width, height),
|
|
83
|
+
];
|
|
84
|
+
this._current = 0;
|
|
85
|
+
|
|
86
|
+
this._materialCache = new WeakMap(); // mesh -> gbuffer ShaderMaterial
|
|
87
|
+
this._swapped = []; // [mesh, originalMaterial] pairs during render
|
|
88
|
+
this._normalMat3 = new THREE.Matrix3();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_makeTarget(width, height) {
|
|
92
|
+
const t = new THREE.WebGLMultipleRenderTargets(width, height, 4, {
|
|
93
|
+
minFilter: THREE.NearestFilter,
|
|
94
|
+
magFilter: THREE.NearestFilter,
|
|
95
|
+
type: THREE.FloatType,
|
|
96
|
+
depthBuffer: true,
|
|
97
|
+
});
|
|
98
|
+
for (const tex of t.texture) tex.generateMipmaps = false;
|
|
99
|
+
// Mixed precision: only world position (reprojection + plane-distance
|
|
100
|
+
// tests) needs fp32. Albedo/normal/emissive are fine in fp16, which halves
|
|
101
|
+
// G-buffer bandwidth on 3 of the 4 targets — a large win on mobile GPUs.
|
|
102
|
+
if (this._mixedPrecision) {
|
|
103
|
+
t.texture[0].type = THREE.HalfFloatType; // albedo + roughness
|
|
104
|
+
t.texture[1].type = THREE.HalfFloatType; // normal + packed material word
|
|
105
|
+
t.texture[3].type = THREE.HalfFloatType; // emissive
|
|
106
|
+
}
|
|
107
|
+
return t;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
get target() {
|
|
111
|
+
return this._targets[this._current];
|
|
112
|
+
}
|
|
113
|
+
get _prev() {
|
|
114
|
+
return this._targets[1 - this._current];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get albedoRough() {
|
|
118
|
+
return this.target.texture[0];
|
|
119
|
+
}
|
|
120
|
+
get normalMetal() {
|
|
121
|
+
return this.target.texture[1];
|
|
122
|
+
}
|
|
123
|
+
get worldPos() {
|
|
124
|
+
return this.target.texture[2];
|
|
125
|
+
}
|
|
126
|
+
get emissive() {
|
|
127
|
+
return this.target.texture[3];
|
|
128
|
+
}
|
|
129
|
+
get prevNormalMetal() {
|
|
130
|
+
return this._prev.texture[1];
|
|
131
|
+
}
|
|
132
|
+
get prevWorldPos() {
|
|
133
|
+
return this._prev.texture[2];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
setSize(width, height) {
|
|
137
|
+
for (const t of this._targets) t.setSize(width, height);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_gbufferMaterialFor(mesh) {
|
|
141
|
+
let material = this._materialCache.get(mesh);
|
|
142
|
+
if (!material) {
|
|
143
|
+
material = new THREE.ShaderMaterial({
|
|
144
|
+
glslVersion: THREE.GLSL3,
|
|
145
|
+
vertexShader: gbufferVert,
|
|
146
|
+
fragmentShader: gbufferFrag,
|
|
147
|
+
uniforms: {
|
|
148
|
+
uNormalMatrixWorld: { value: new THREE.Matrix3() },
|
|
149
|
+
uColor: { value: new THREE.Color(1, 1, 1) },
|
|
150
|
+
uRoughness: { value: 1.0 },
|
|
151
|
+
uMetalness: { value: 0.0 },
|
|
152
|
+
uTransmission: { value: 0.0 },
|
|
153
|
+
uEmissive: { value: new THREE.Color(0, 0, 0) },
|
|
154
|
+
uMap: { value: null },
|
|
155
|
+
uHasMap: { value: false },
|
|
156
|
+
uEmissiveMap: { value: null },
|
|
157
|
+
uHasEmissiveMap: { value: false },
|
|
158
|
+
},
|
|
159
|
+
side: THREE.FrontSide,
|
|
160
|
+
});
|
|
161
|
+
this._materialCache.set(mesh, material);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// Sync properties from the user's material every frame (cheap).
|
|
165
|
+
const src = Array.isArray(mesh.material) ? mesh.material[0] : mesh.material;
|
|
166
|
+
const u = material.uniforms;
|
|
167
|
+
if (src.color) u.uColor.value.copy(src.color);
|
|
168
|
+
u.uRoughness.value = src.roughness ?? 1.0;
|
|
169
|
+
u.uMetalness.value = src.metalness ?? 0.0;
|
|
170
|
+
u.uTransmission.value = src.transmission ?? 0.0; // MeshPhysicalMaterial
|
|
171
|
+
|
|
172
|
+
if (src.emissive) {
|
|
173
|
+
u.uEmissive.value
|
|
174
|
+
.copy(src.emissive)
|
|
175
|
+
.multiplyScalar(src.emissiveIntensity ?? 1);
|
|
176
|
+
}
|
|
177
|
+
u.uMap.value = src.map ?? null;
|
|
178
|
+
u.uHasMap.value = !!src.map;
|
|
179
|
+
u.uEmissiveMap.value = src.emissiveMap ?? null;
|
|
180
|
+
u.uHasEmissiveMap.value = !!src.emissiveMap;
|
|
181
|
+
u.uNormalMatrixWorld.value.getNormalMatrix(mesh.matrixWorld);
|
|
182
|
+
material.side = src.side ?? THREE.FrontSide;
|
|
183
|
+
return material;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
render(renderer, scene, camera) {
|
|
187
|
+
// Ping-pong: what was "current" becomes "previous".
|
|
188
|
+
this._current = 1 - this._current;
|
|
189
|
+
|
|
190
|
+
// Swap in G-buffer materials.
|
|
191
|
+
this._swapped.length = 0;
|
|
192
|
+
scene.traverse((obj) => {
|
|
193
|
+
if (obj.isMesh && obj.geometry && obj.visible) {
|
|
194
|
+
// The G-buffer has no alpha blending — a nearly invisible surface
|
|
195
|
+
// (glass display case, ghost meshes) would write itself opaque over
|
|
196
|
+
// everything behind it. Hide it from the raster pass instead.
|
|
197
|
+
const m = Array.isArray(obj.material) ? obj.material[0] : obj.material;
|
|
198
|
+
if (m.transparent && (m.opacity ?? 1) < 0.5) {
|
|
199
|
+
obj.visible = false;
|
|
200
|
+
this._swapped.push([obj, null]); // restore visibility after render
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
this._swapped.push([obj, obj.material]);
|
|
204
|
+
obj.material = this._gbufferMaterialFor(obj);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
const prevBackground = scene.background;
|
|
209
|
+
scene.background = null; // background writes nothing; worldPos.w stays 0
|
|
210
|
+
|
|
211
|
+
renderer.setRenderTarget(this.target);
|
|
212
|
+
renderer.setClearColor(0x000000, 0);
|
|
213
|
+
renderer.clear(true, true, false);
|
|
214
|
+
renderer.render(scene, camera);
|
|
215
|
+
renderer.setRenderTarget(null);
|
|
216
|
+
|
|
217
|
+
scene.background = prevBackground;
|
|
218
|
+
for (const [mesh, mat] of this._swapped) {
|
|
219
|
+
if (mat === null) mesh.visible = true; // was hidden, not material-swapped
|
|
220
|
+
else mesh.material = mat;
|
|
221
|
+
}
|
|
222
|
+
this._swapped.length = 0;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
dispose() {
|
|
226
|
+
for (const t of this._targets) t.dispose();
|
|
227
|
+
}
|
|
228
|
+
}
|