roamwright 0.0.0-stage → 0.1.1
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/CONTRIBUTING.md +12 -0
- package/LICENSE +21 -0
- package/README.md +62 -2
- package/examples/index.html +19 -0
- package/examples/island.js +62 -0
- package/geometry/builder.js +543 -0
- package/geometry/instances.js +76 -0
- package/geometry/placement.js +65 -0
- package/geometry/primitives.js +172 -0
- package/index.js +8 -0
- package/licenses/baking-lab.txt +21 -0
- package/licenses/fidelityfx-fsr.txt +19 -0
- package/licenses/three.txt +21 -0
- package/math/curve.js +93 -0
- package/math/noise.js +80 -0
- package/math/random.js +27 -0
- package/math/scalar.js +12 -0
- package/motion/player.js +248 -0
- package/motion/spaces.js +206 -0
- package/package.json +50 -4
- package/rendering/context.js +20 -0
- package/rendering/dof.js +59 -0
- package/rendering/eye-adapt.js +119 -0
- package/rendering/piece-batch.js +411 -0
- package/rendering/post-shaders.js +236 -0
- package/rendering/post.js +519 -0
- package/rendering/still-casters.js +32 -0
- package/rendering/taa.js +171 -0
- package/rendering/textures.js +63 -0
- package/rendering/world-matrices.js +75 -0
- package/runtime/first-use.js +60 -0
- package/runtime/gpu-timing.js +75 -0
- package/runtime/hand-over.js +102 -0
- package/runtime/pool.js +153 -0
- package/runtime/quality-fit.js +190 -0
- package/runtime/quality-store.js +58 -0
- package/runtime/seen.js +36 -0
- package/runtime/worker.js +53 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { createEyeAdaptation } from './eye-adapt.js';
|
|
3
|
+
import { DOF_BLUR, DOF_MOST, createDepthOfField } from './dof.js';
|
|
4
|
+
import { createTemporalAA } from './taa.js';
|
|
5
|
+
import { AO_FS, BLOOM, BLUR_FS, EASU_FS, FINAL_FS, GRADE, PRESENT_FS, QUAD_VS, RAY_FS, RCAS_FS } from './post-shaders.js';
|
|
6
|
+
|
|
7
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
8
|
+
// Post-processing: HDR scene, occlusion and light shafts, bloom -> ACES filmic, grade, vignette, sRGB
|
|
9
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
10
|
+
// Two full-size passes a frame: the scene, and the final pass that puts everything together and writes the screen. Occlusion and light
|
|
11
|
+
// shafts are soft, so they are worked out at half size; bloom works at half size and below. None of them writes a full-size copy of the frame.
|
|
12
|
+
// Where a device cannot hold its preset at the picture's size, the world is drawn smaller (post.scale of each side, set by the quality
|
|
13
|
+
// fit) and brought up to the picture's size after the final pass, by an edge-adaptive upscale and a sharpening pass (the method of
|
|
14
|
+
// AMD's FSR 1). Overlay sprites render at the output resolution (drawnAtFullSize).
|
|
15
|
+
// With temporal anti-aliasing (taa.js) the scene is drawn with the lens moved a little each frame, the final pass keeps a running
|
|
16
|
+
// picture of it, tone-mapped, and a third full-size pass grades that onto the screen.
|
|
17
|
+
const DOF_EASE = 0.08; // how much of depth of field comes in or goes out a frame
|
|
18
|
+
const SHARPEN_STOPS = 0.25, // how far below its fullest the upscale's sharpening is held, in stops (0 is the strongest; FSR suggests about 0.2)
|
|
19
|
+
STEADY_SHARPEN = 0.5; // and the sharpening of the running picture where there is no upscale (taa.js), from 0 to 1
|
|
20
|
+
|
|
21
|
+
const OVER_ENCODED = `/* glsl */
|
|
22
|
+
if (uGraded > 0.5) {
|
|
23
|
+
gl_FragColor.rgb = gradeTail(gl_FragColor.rgb, gl_FragCoord.xy * uScreenPx);
|
|
24
|
+
gl_FragColor.a = 1.0 - pow(1.0 - gl_FragColor.a, 1.0 / 2.2);
|
|
25
|
+
}`;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create HDR, bloom, exposure, antialiasing and presentation passes for one camera.
|
|
29
|
+
* createComposer(target) must return an EffectComposer using the supplied color target.
|
|
30
|
+
* skyMask is a mesh whose material has tDepth and uPx uniforms and encodes occlusion in red.
|
|
31
|
+
* getFrame() identifies the current frame for focusOn. expectedLogLuminance() gives the
|
|
32
|
+
* scene's reference brightness in stops; adaptationStrength and maxExposureStops control
|
|
33
|
+
* the exposure correction. Subsystems can also be shared with the host's camera controls.
|
|
34
|
+
* Call setPost before sizePost, then render post.composer for each enabled frame.
|
|
35
|
+
*/
|
|
36
|
+
export function createPostProcessing({
|
|
37
|
+
renderer,
|
|
38
|
+
camera,
|
|
39
|
+
scene,
|
|
40
|
+
fxScene,
|
|
41
|
+
skyMask,
|
|
42
|
+
createComposer,
|
|
43
|
+
getFrame,
|
|
44
|
+
referenceLogLuminance = 0,
|
|
45
|
+
expectedLogLuminance = () => referenceLogLuminance,
|
|
46
|
+
adaptationStrength = 0.35,
|
|
47
|
+
maxExposureStops = 0.8,
|
|
48
|
+
eyeAdaptation = createEyeAdaptation(),
|
|
49
|
+
depthOfField = createDepthOfField(),
|
|
50
|
+
temporalAA = createTemporalAA(camera),
|
|
51
|
+
}) {
|
|
52
|
+
const { adaptEye, eye, eyeMaterials } = eyeAdaptation;
|
|
53
|
+
const { dof } = depthOfField;
|
|
54
|
+
const { disposeHistory, historyInto, historyNow, jitterLens, makeHistory, sizeHistory, steadyLens, taaUniforms } = temporalAA;
|
|
55
|
+
|
|
56
|
+
// What the particles read to fade where they meet the scene: the distance to what the scene drew at each pixel (the occlusion pass's
|
|
57
|
+
// half-size target keeps it beside the occlusion), the size of a pixel, and whether there is one (only with post-processing).
|
|
58
|
+
const sceneDepth = { uDepth: { value: null }, uDepthPx: { value: new THREE.Vector2(1, 1) }, uSoft: { value: 0 } };
|
|
59
|
+
const post = { composer: null, bloom: null, final: null, on: false, samples: -1, taa: false, scale: 1, drawn: [1, 1] };
|
|
60
|
+
// Uniforms shared by the final pass and the names drawn after it.
|
|
61
|
+
const grade = {
|
|
62
|
+
uVig: { value: 0.34 },
|
|
63
|
+
uSat: { value: 0.95 },
|
|
64
|
+
uTime: { value: 0 },
|
|
65
|
+
uGraded: { value: 0 }, // 1 while post-processing is on: the names are then graded here as the picture is; else as the canvas does
|
|
66
|
+
uScreenPx: { value: new THREE.Vector2(1, 1) }, // one pixel of the screen, as a share of it
|
|
67
|
+
};
|
|
68
|
+
// The upscale: its two passes, and its targets, which exist only while the world is drawn smaller (low: the graded picture at the drawn
|
|
69
|
+
// size; full: after the first pass, at the screen's).
|
|
70
|
+
const upscaleTarget = () =>
|
|
71
|
+
new THREE.WebGLRenderTarget(2, 2, { depthBuffer: false, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter });
|
|
72
|
+
const upscale = {
|
|
73
|
+
low: null,
|
|
74
|
+
full: null,
|
|
75
|
+
easu: new THREE.ShaderMaterial({
|
|
76
|
+
depthTest: false,
|
|
77
|
+
depthWrite: false,
|
|
78
|
+
vertexShader: QUAD_VS,
|
|
79
|
+
fragmentShader: EASU_FS,
|
|
80
|
+
uniforms: { tSrc: { value: null }, uSrcSize: { value: new THREE.Vector2(1, 1) } },
|
|
81
|
+
}),
|
|
82
|
+
rcas: new THREE.ShaderMaterial({
|
|
83
|
+
depthFunc: THREE.AlwaysDepth,
|
|
84
|
+
vertexShader: QUAD_VS,
|
|
85
|
+
fragmentShader: RCAS_FS,
|
|
86
|
+
uniforms: {
|
|
87
|
+
tSrc: { value: null },
|
|
88
|
+
tDepth: { value: null },
|
|
89
|
+
uPx: { value: new THREE.Vector2(1, 1) },
|
|
90
|
+
uSharp: { value: Math.pow(2, -SHARPEN_STOPS) },
|
|
91
|
+
uTime: grade.uTime,
|
|
92
|
+
},
|
|
93
|
+
}),
|
|
94
|
+
};
|
|
95
|
+
// With temporal anti-aliasing, the pass that grades the running picture onto the screen (or into the upscale's first target).
|
|
96
|
+
const present = new THREE.ShaderMaterial({
|
|
97
|
+
depthFunc: THREE.AlwaysDepth,
|
|
98
|
+
vertexShader: QUAD_VS,
|
|
99
|
+
fragmentShader: PRESENT_FS,
|
|
100
|
+
uniforms: {
|
|
101
|
+
tSrc: { value: null },
|
|
102
|
+
tDepth: { value: null },
|
|
103
|
+
uPx: { value: new THREE.Vector2(1, 1) },
|
|
104
|
+
uSharp: { value: STEADY_SHARPEN },
|
|
105
|
+
uVig: grade.uVig,
|
|
106
|
+
uSat: grade.uSat,
|
|
107
|
+
uTime: grade.uTime,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
// Render overlays at output resolution with depth testing against the main scene.
|
|
111
|
+
const overlay = new THREE.Scene();
|
|
112
|
+
// A scene of one quad that covers the screen, for a post-processing pass.
|
|
113
|
+
function fullScreen(material) {
|
|
114
|
+
const s = new THREE.Scene(),
|
|
115
|
+
q = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), material);
|
|
116
|
+
q.frustumCulled = false;
|
|
117
|
+
s.add(q);
|
|
118
|
+
return s;
|
|
119
|
+
}
|
|
120
|
+
// Depth of field while a scene's shot names a focus (dof.js): eased in over a few frames, and out when none is named.
|
|
121
|
+
function focus(rn) {
|
|
122
|
+
const named = dof.named >= getFrame() - 1;
|
|
123
|
+
dof.weight = named ? Math.min(1, dof.weight + DOF_EASE) : Math.max(0, dof.weight - DOF_EASE);
|
|
124
|
+
const d = dof.material.uniforms;
|
|
125
|
+
d.uDof.value.x = dof.focus;
|
|
126
|
+
d.uDof.value.w = dof.weight;
|
|
127
|
+
if (dof.weight <= 0) return;
|
|
128
|
+
d.tScene.value = post.sceneRT.texture;
|
|
129
|
+
d.tDepth.value = post.sceneRT.depthTexture;
|
|
130
|
+
d.uNF.value.set(camera.near, camera.far);
|
|
131
|
+
rn.setRenderTarget(dof.target);
|
|
132
|
+
rn.render(post.dofScene, post.aoCam);
|
|
133
|
+
}
|
|
134
|
+
// Whether any particle is alive (empty pools have their visibility turned off).
|
|
135
|
+
function particlesShown() {
|
|
136
|
+
const list = fxScene.children;
|
|
137
|
+
for (let i = 0; i < list.length; i++) if (list[i].visible) return true;
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
// Draws the particles over what is in `target` (null: the screen). With a target of one sample a pixel they fade by the depth the
|
|
141
|
+
// occlusion pass has just kept (Medium and High); without post-processing, or over a multisampled target, they meet the scene hard
|
|
142
|
+
// as before (its depth is not read: a multisampled target's colour is resolved again after them, its depth is not).
|
|
143
|
+
function drawParticles(rn, target) {
|
|
144
|
+
if (!particlesShown()) return;
|
|
145
|
+
const soft = target !== null && !post.samples;
|
|
146
|
+
sceneDepth.uSoft.value = soft ? 1 : 0;
|
|
147
|
+
if (soft) {
|
|
148
|
+
sceneDepth.uDepth.value = post.aoRT.texture;
|
|
149
|
+
sceneDepth.uDepthPx.value.set(1 / post.aoRT.width, 1 / post.aoRT.height);
|
|
150
|
+
}
|
|
151
|
+
const clears = rn.autoClear,
|
|
152
|
+
resolves = target?.resolveDepthBuffer;
|
|
153
|
+
rn.autoClear = false;
|
|
154
|
+
if (target) target.resolveDepthBuffer = false;
|
|
155
|
+
rn.setRenderTarget(target);
|
|
156
|
+
rn.render(fxScene, camera);
|
|
157
|
+
if (target) target.resolveDepthBuffer = resolves;
|
|
158
|
+
rn.autoClear = clears;
|
|
159
|
+
}
|
|
160
|
+
// Builds the post-processing chain described at the top of this module, with samples of anti-aliasing, and temporal
|
|
161
|
+
// anti-aliasing where taa is true.
|
|
162
|
+
function buildPost(samples, taa) {
|
|
163
|
+
if (post.composer) {
|
|
164
|
+
post.composer.renderTarget1.dispose();
|
|
165
|
+
post.composer.renderTarget2.dispose();
|
|
166
|
+
post.bloom.dispose();
|
|
167
|
+
post.sceneRT.dispose();
|
|
168
|
+
post.aoRT.dispose();
|
|
169
|
+
post.rayRT.dispose();
|
|
170
|
+
post.maskRT.dispose();
|
|
171
|
+
dof.target.dispose();
|
|
172
|
+
disposeHistory();
|
|
173
|
+
}
|
|
174
|
+
if (taa) makeHistory();
|
|
175
|
+
post.taa = taa;
|
|
176
|
+
dof.target = new THREE.WebGLRenderTarget(2, 2, { type: THREE.HalfFloatType, format: THREE.RGBFormat, depthBuffer: false });
|
|
177
|
+
dof.target.texture.internalFormat = 'R11F_G11F_B10F';
|
|
178
|
+
// Only the scene itself is drawn into a multisampled target (with its depth, read back for ambient occlusion). Its colour is packed
|
|
179
|
+
// floats with no alpha (32 bits a pixel, against 64 for half floats with alpha): writing, resolving and reading it is the larger part
|
|
180
|
+
// of post-processing's cost, and the 6-bit mantissas are finer than the 8-bit screen after tone mapping.
|
|
181
|
+
const sceneRT = (post.sceneRT = new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType, format: THREE.RGBFormat, samples }));
|
|
182
|
+
sceneRT.texture.internalFormat = 'R11F_G11F_B10F';
|
|
183
|
+
sceneRT.depthTexture = new THREE.DepthTexture(4, 4);
|
|
184
|
+
sceneRT.depthTexture.type = THREE.UnsignedIntType;
|
|
185
|
+
// The composer's own buffers stay at their smallest: no pass reads or writes them (shader warm-up renders into one).
|
|
186
|
+
post.composer = createComposer(new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType, depthBuffer: false }));
|
|
187
|
+
post.samples = samples;
|
|
188
|
+
post.composer.renderTarget1.setSize = post.composer.renderTarget2.setSize = function () {};
|
|
189
|
+
post.rayRT = new THREE.WebGLRenderTarget(2, 2, { type: THREE.HalfFloatType, format: THREE.RedFormat, depthBuffer: false });
|
|
190
|
+
post.maskRT = new THREE.WebGLRenderTarget(2, 2, { format: THREE.RedFormat, depthBuffer: false });
|
|
191
|
+
const mask = skyMask.material.uniforms;
|
|
192
|
+
mask.tDepth.value = sceneRT.depthTexture;
|
|
193
|
+
post.maskScene = new THREE.Scene();
|
|
194
|
+
post.maskScene.add(skyMask);
|
|
195
|
+
post.aoRT = new THREE.WebGLRenderTarget(2, 2, {
|
|
196
|
+
type: THREE.HalfFloatType,
|
|
197
|
+
format: THREE.RGFormat,
|
|
198
|
+
depthBuffer: false,
|
|
199
|
+
minFilter: THREE.NearestFilter,
|
|
200
|
+
magFilter: THREE.NearestFilter,
|
|
201
|
+
});
|
|
202
|
+
post.rayMat = new THREE.ShaderMaterial({
|
|
203
|
+
depthTest: false,
|
|
204
|
+
depthWrite: false,
|
|
205
|
+
vertexShader: QUAD_VS,
|
|
206
|
+
fragmentShader: RAY_FS,
|
|
207
|
+
uniforms: { tMask: { value: post.maskRT.texture }, uSun: { value: new THREE.Vector2(0.5, 0.5) }, uAspect: { value: 1 } },
|
|
208
|
+
});
|
|
209
|
+
post.aoMat = new THREE.ShaderMaterial({
|
|
210
|
+
depthTest: false,
|
|
211
|
+
depthWrite: false,
|
|
212
|
+
vertexShader: QUAD_VS,
|
|
213
|
+
fragmentShader: AO_FS,
|
|
214
|
+
uniforms: {
|
|
215
|
+
tDepth: { value: sceneRT.depthTexture },
|
|
216
|
+
uNF: { value: new THREE.Vector2(camera.near, camera.far) },
|
|
217
|
+
uTan: { value: new THREE.Vector2(1, 1) },
|
|
218
|
+
uPx: { value: new THREE.Vector2(0.001, 0.001) },
|
|
219
|
+
uAspect: { value: 1 },
|
|
220
|
+
uAO: { value: 1 },
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
post.rayScene = fullScreen(post.rayMat);
|
|
224
|
+
post.aoScene = fullScreen(post.aoMat);
|
|
225
|
+
post.dofScene = fullScreen(dof.material);
|
|
226
|
+
post.aoCam = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
|
|
227
|
+
|
|
228
|
+
// 1. The scene, then occlusion and shafts from it at half size.
|
|
229
|
+
post.composer.addPass({
|
|
230
|
+
enabled: true,
|
|
231
|
+
needsSwap: false,
|
|
232
|
+
clear: true,
|
|
233
|
+
renderToScreen: false,
|
|
234
|
+
setSize(w, h) {
|
|
235
|
+
sceneRT.setSize(w, h);
|
|
236
|
+
for (const t of [post.aoRT, post.rayRT, post.maskRT, dof.target]) t.setSize(Math.ceil(w / 2), Math.ceil(h / 2));
|
|
237
|
+
sizeHistory(w, h);
|
|
238
|
+
const d = dof.material.uniforms;
|
|
239
|
+
d.uPx.value.set(1 / w, 1 / h);
|
|
240
|
+
d.uDof.value.y = DOF_BLUR * h;
|
|
241
|
+
d.uDof.value.z = DOF_MOST * h;
|
|
242
|
+
mask.uPx.value.set(1 / post.maskRT.width, 1 / post.maskRT.height);
|
|
243
|
+
},
|
|
244
|
+
render(rn) {
|
|
245
|
+
const u = post.final.uniforms;
|
|
246
|
+
rn.setRenderTarget(sceneRT);
|
|
247
|
+
if (taa) jitterLens(sceneRT.width, sceneRT.height);
|
|
248
|
+
rn.render(scene, camera);
|
|
249
|
+
u.uPx.value.set(1 / sceneRT.width, 1 / sceneRT.height);
|
|
250
|
+
u.uHp.value.set(1 / post.aoRT.width, 1 / post.aoRT.height);
|
|
251
|
+
{
|
|
252
|
+
const a = post.aoMat.uniforms;
|
|
253
|
+
a.uPx.value.copy(u.uPx.value);
|
|
254
|
+
a.uTan.value.copy(u.uTan.value);
|
|
255
|
+
a.uAspect.value = u.uAspect.value;
|
|
256
|
+
a.uAO.value = u.uAO.value;
|
|
257
|
+
rn.setRenderTarget(post.aoRT);
|
|
258
|
+
rn.render(post.aoScene, post.aoCam);
|
|
259
|
+
}
|
|
260
|
+
drawParticles(rn, sceneRT);
|
|
261
|
+
if (taa) steadyLens();
|
|
262
|
+
adaptEye(rn, sceneRT.texture);
|
|
263
|
+
u.tEye.value = eye.level;
|
|
264
|
+
u.uEye.value.x = expectedLogLuminance();
|
|
265
|
+
focus(rn);
|
|
266
|
+
if (u.uRay.value > 0.002) {
|
|
267
|
+
rn.setRenderTarget(post.maskRT);
|
|
268
|
+
rn.render(post.maskScene, camera);
|
|
269
|
+
const a = post.rayMat.uniforms;
|
|
270
|
+
a.uSun.value.copy(u.uSun.value);
|
|
271
|
+
a.uAspect.value = u.uAspect.value;
|
|
272
|
+
rn.setRenderTarget(post.rayRT);
|
|
273
|
+
rn.render(post.rayScene, post.aoCam);
|
|
274
|
+
}
|
|
275
|
+
},
|
|
276
|
+
dispose() {},
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// 2. Bloom from the scene target.
|
|
280
|
+
post.bloom = bloomPass(sceneRT);
|
|
281
|
+
post.composer.addPass(post.bloom);
|
|
282
|
+
|
|
283
|
+
// 3. Everything together, graded: to the screen, or where the world is drawn smaller, into a target of its size, which the upscale
|
|
284
|
+
// brings to the screen's.
|
|
285
|
+
const finalMat = new THREE.ShaderMaterial({
|
|
286
|
+
depthFunc: THREE.AlwaysDepth, // it writes the scene's depth to the screen, whatever stood there
|
|
287
|
+
defines: taa ? { TAA: 1 } : {},
|
|
288
|
+
vertexShader: QUAD_VS,
|
|
289
|
+
fragmentShader: FINAL_FS,
|
|
290
|
+
uniforms: {
|
|
291
|
+
tScene: { value: sceneRT.texture },
|
|
292
|
+
tDepth: { value: sceneRT.depthTexture },
|
|
293
|
+
tAO: { value: post.aoRT.texture },
|
|
294
|
+
tRay: { value: post.rayRT.texture },
|
|
295
|
+
...Object.fromEntries(post.bloom.levels.map((l, i) => ['tBloom' + i, { value: l.v.texture }])),
|
|
296
|
+
uBloomW: { value: post.bloom.weights },
|
|
297
|
+
uSun: { value: new THREE.Vector2(0.5, 0.5) },
|
|
298
|
+
uRay: { value: 0 },
|
|
299
|
+
uAspect: { value: 1 },
|
|
300
|
+
uRayCol: { value: new THREE.Color(1, 1, 1) },
|
|
301
|
+
uNF: { value: new THREE.Vector2(camera.near, camera.far) },
|
|
302
|
+
uTan: { value: new THREE.Vector2(1, 1) },
|
|
303
|
+
uPx: { value: new THREE.Vector2(0.001, 0.001) },
|
|
304
|
+
uAO: { value: 1 },
|
|
305
|
+
uHp: { value: new THREE.Vector2(0.002, 0.002) },
|
|
306
|
+
uExposure: { value: 1 },
|
|
307
|
+
tEye: { value: eye.level },
|
|
308
|
+
tDof: { value: dof.target.texture },
|
|
309
|
+
uDof: dof.material.uniforms.uDof,
|
|
310
|
+
uEye: { value: new THREE.Vector3(referenceLogLuminance, adaptationStrength, maxExposureStops) },
|
|
311
|
+
uVig: grade.uVig,
|
|
312
|
+
uSat: grade.uSat,
|
|
313
|
+
uTime: grade.uTime,
|
|
314
|
+
...taaUniforms,
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
const finalScene = fullScreen(finalMat),
|
|
318
|
+
presentScene = fullScreen(present),
|
|
319
|
+
easuScene = fullScreen(upscale.easu),
|
|
320
|
+
rcasScene = fullScreen(upscale.rcas);
|
|
321
|
+
upscale.rcas.uniforms.tDepth.value = present.uniforms.tDepth.value = sceneRT.depthTexture;
|
|
322
|
+
post.final = {
|
|
323
|
+
enabled: true,
|
|
324
|
+
needsSwap: false,
|
|
325
|
+
clear: false,
|
|
326
|
+
renderToScreen: true,
|
|
327
|
+
material: finalMat,
|
|
328
|
+
uniforms: finalMat.uniforms,
|
|
329
|
+
setSize() {}, // sized with the rest by sizePost
|
|
330
|
+
render(rn) {
|
|
331
|
+
if (taa) {
|
|
332
|
+
rn.setRenderTarget(historyInto());
|
|
333
|
+
rn.render(finalScene, post.aoCam);
|
|
334
|
+
present.uniforms.tSrc.value = historyNow().texture;
|
|
335
|
+
}
|
|
336
|
+
rn.setRenderTarget(upscale.low); // the screen, where the world is drawn at its size
|
|
337
|
+
rn.render(taa ? presentScene : finalScene, post.aoCam);
|
|
338
|
+
if (!upscale.low) return;
|
|
339
|
+
rn.setRenderTarget(upscale.full);
|
|
340
|
+
rn.render(easuScene, post.aoCam);
|
|
341
|
+
rn.setRenderTarget(null);
|
|
342
|
+
rn.render(rcasScene, post.aoCam);
|
|
343
|
+
},
|
|
344
|
+
dispose() {},
|
|
345
|
+
};
|
|
346
|
+
post.composer.addPass(post.final);
|
|
347
|
+
}
|
|
348
|
+
// The materials of the passes after the scene, each with the target it is drawn into (null: the screen), for warming their
|
|
349
|
+
// programs before the first frame .
|
|
350
|
+
function postMaterials() {
|
|
351
|
+
if (!post.composer) return [];
|
|
352
|
+
const list = [
|
|
353
|
+
[post.aoMat, post.aoRT],
|
|
354
|
+
[post.rayMat, post.rayRT],
|
|
355
|
+
[post.final.material, post.taa ? historyNow() : upscale.low],
|
|
356
|
+
];
|
|
357
|
+
if (post.taa) list.push([present, upscale.low]);
|
|
358
|
+
for (const l of post.bloom.levels) list.push([l.hMat, l.h], [l.vMat, l.v]);
|
|
359
|
+
list.push(...eyeMaterials());
|
|
360
|
+
list.push([dof.material, dof.target]);
|
|
361
|
+
if (upscale.low) list.push([upscale.easu, upscale.full], [upscale.rcas, null]);
|
|
362
|
+
return list;
|
|
363
|
+
}
|
|
364
|
+
// The bloom pass: bright parts blurred at falling sizes and added back over the picture.
|
|
365
|
+
function bloomPass(sceneRT) {
|
|
366
|
+
const quad = fullScreen(null),
|
|
367
|
+
mesh = quad.children[0],
|
|
368
|
+
cam = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
|
|
369
|
+
const target = () => {
|
|
370
|
+
const t = new THREE.WebGLRenderTarget(2, 2, { type: THREE.HalfFloatType, format: THREE.RGBFormat, depthBuffer: false });
|
|
371
|
+
t.texture.internalFormat = 'R11F_G11F_B10F';
|
|
372
|
+
return t;
|
|
373
|
+
};
|
|
374
|
+
const blur = (k, first) => {
|
|
375
|
+
const coeff = [];
|
|
376
|
+
for (let j = 0; j < k; j++) coeff.push((0.39894 * Math.exp((-0.5 * j * j) / (k * k))) / k);
|
|
377
|
+
return new THREE.ShaderMaterial({
|
|
378
|
+
depthTest: false,
|
|
379
|
+
depthWrite: false,
|
|
380
|
+
vertexShader: QUAD_VS,
|
|
381
|
+
fragmentShader: BLUR_FS,
|
|
382
|
+
uniforms: {
|
|
383
|
+
tSrc: { value: null },
|
|
384
|
+
uStep: { value: new THREE.Vector2() },
|
|
385
|
+
uK: { value: coeff },
|
|
386
|
+
uTaps: { value: k },
|
|
387
|
+
uBright: { value: first },
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
};
|
|
391
|
+
// each level is blurred across (hMat) into h, then down (vMat) into v; only the first blur across reads the scene and keeps its bright part
|
|
392
|
+
const levels = BLOOM.kernels.map((k, i) => {
|
|
393
|
+
const vMat = blur(k, false);
|
|
394
|
+
return { h: target(), v: target(), hMat: i === 0 ? blur(k, true) : vMat, vMat };
|
|
395
|
+
});
|
|
396
|
+
// the levels' weights: strength, times the factor moved towards its mirror by the radius (as Three's composite does)
|
|
397
|
+
const weights = BLOOM.factors.map(f => {
|
|
398
|
+
const w = BLOOM.strength * THREE.MathUtils.lerp(f, 1.2 - f, BLOOM.radius);
|
|
399
|
+
return new THREE.Vector3(w, w, w);
|
|
400
|
+
});
|
|
401
|
+
return {
|
|
402
|
+
enabled: true,
|
|
403
|
+
needsSwap: false,
|
|
404
|
+
clear: true,
|
|
405
|
+
renderToScreen: false,
|
|
406
|
+
levels,
|
|
407
|
+
weights,
|
|
408
|
+
setSize(w, h) {
|
|
409
|
+
let x = Math.round(w / 2),
|
|
410
|
+
y = Math.round(h / 2);
|
|
411
|
+
for (const l of levels) {
|
|
412
|
+
l.h.setSize(x, y);
|
|
413
|
+
l.v.setSize(x, y);
|
|
414
|
+
l.size = [x, y];
|
|
415
|
+
x = Math.round(x / 2);
|
|
416
|
+
y = Math.round(y / 2);
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
render(rn) {
|
|
420
|
+
let from = sceneRT.texture;
|
|
421
|
+
for (const l of levels) {
|
|
422
|
+
let u = l.hMat.uniforms;
|
|
423
|
+
mesh.material = l.hMat;
|
|
424
|
+
u.tSrc.value = from;
|
|
425
|
+
u.uStep.value.set(1 / l.size[0], 0);
|
|
426
|
+
rn.setRenderTarget(l.h);
|
|
427
|
+
rn.render(quad, cam);
|
|
428
|
+
u = l.vMat.uniforms;
|
|
429
|
+
mesh.material = l.vMat;
|
|
430
|
+
u.tSrc.value = l.h.texture;
|
|
431
|
+
u.uStep.value.set(0, 1 / l.size[1]);
|
|
432
|
+
rn.setRenderTarget(l.v);
|
|
433
|
+
rn.render(quad, cam);
|
|
434
|
+
from = l.v.texture;
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
dispose() {
|
|
438
|
+
for (const l of levels) {
|
|
439
|
+
l.h.dispose();
|
|
440
|
+
l.v.dispose();
|
|
441
|
+
l.hMat.dispose();
|
|
442
|
+
l.vMat.dispose();
|
|
443
|
+
}
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
function setPost(on, samples, taa) {
|
|
448
|
+
if (on && (!post.composer || post.samples !== samples || post.taa !== taa)) buildPost(samples, taa);
|
|
449
|
+
post.on = on;
|
|
450
|
+
grade.uGraded.value = on ? 1 : 0;
|
|
451
|
+
}
|
|
452
|
+
// Sizes the passes for a screen of width x height pixels: the scene and everything worked out from it at post.scale of each side, and
|
|
453
|
+
// where that is less than the screen the upscale's targets, which are let go again when the world is drawn at the screen's size.
|
|
454
|
+
function sizePost(width, height) {
|
|
455
|
+
const w = Math.max(1, Math.round(width * post.scale)),
|
|
456
|
+
h = Math.max(1, Math.round(height * post.scale));
|
|
457
|
+
post.drawn = [w, h];
|
|
458
|
+
grade.uScreenPx.value.set(1 / width, 1 / height);
|
|
459
|
+
for (const pass of post.composer.passes) pass.setSize(w, h);
|
|
460
|
+
const smaller = w < width || h < height;
|
|
461
|
+
present.uniforms.uPx.value.set(1 / w, 1 / h);
|
|
462
|
+
present.uniforms.uSharp.value = smaller ? 0 : STEADY_SHARPEN;
|
|
463
|
+
if (smaller !== !!upscale.low) {
|
|
464
|
+
if (smaller) {
|
|
465
|
+
upscale.low = upscaleTarget();
|
|
466
|
+
upscale.full = upscaleTarget();
|
|
467
|
+
} else {
|
|
468
|
+
upscale.low.dispose();
|
|
469
|
+
upscale.full.dispose();
|
|
470
|
+
upscale.low = upscale.full = null;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (!smaller) return;
|
|
474
|
+
upscale.low.setSize(w, h);
|
|
475
|
+
upscale.full.setSize(width, height);
|
|
476
|
+
upscale.easu.uniforms.tSrc.value = upscale.low.texture;
|
|
477
|
+
upscale.easu.uniforms.uSrcSize.value.set(w, h);
|
|
478
|
+
upscale.rcas.uniforms.tSrc.value = upscale.full.texture;
|
|
479
|
+
upscale.rcas.uniforms.uPx.value.set(1 / width, 1 / height);
|
|
480
|
+
}
|
|
481
|
+
// The upscale's targets while they exist, for the reckoning of memory.
|
|
482
|
+
const upscaleTargets = () => [upscale.low, upscale.full];
|
|
483
|
+
// Composite a depth-tested sprite at output resolution. Adjust its alpha for
|
|
484
|
+
// blending over encoded colour after the main grading pass.
|
|
485
|
+
function drawnAtFullSize(sprite) {
|
|
486
|
+
const material = sprite.material;
|
|
487
|
+
material.onBeforeCompile = shader => {
|
|
488
|
+
Object.assign(shader.uniforms, grade);
|
|
489
|
+
shader.fragmentShader = shader.fragmentShader
|
|
490
|
+
.replace('void main() {', `uniform float uGraded; uniform vec2 uScreenPx;\n${GRADE}\nvoid main() {`)
|
|
491
|
+
.replace('#include <tonemapping_fragment>', `#include <tonemapping_fragment>\n${OVER_ENCODED}`);
|
|
492
|
+
};
|
|
493
|
+
material.customProgramCacheKey = () => 'drawn-at-full-size';
|
|
494
|
+
overlay.add(sprite);
|
|
495
|
+
}
|
|
496
|
+
// Draws what is drawn at full size over the picture on the screen.
|
|
497
|
+
function drawOverlay() {
|
|
498
|
+
const clears = renderer.autoClear;
|
|
499
|
+
renderer.autoClear = false;
|
|
500
|
+
renderer.render(overlay, camera);
|
|
501
|
+
renderer.autoClear = clears;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
return {
|
|
505
|
+
drawOverlay,
|
|
506
|
+
drawParticles,
|
|
507
|
+
drawnAtFullSize,
|
|
508
|
+
overlay,
|
|
509
|
+
post,
|
|
510
|
+
postMaterials,
|
|
511
|
+
sceneDepth,
|
|
512
|
+
setPost,
|
|
513
|
+
sizePost,
|
|
514
|
+
upscaleTargets,
|
|
515
|
+
depthOfField,
|
|
516
|
+
temporalAA,
|
|
517
|
+
eyeAdaptation,
|
|
518
|
+
};
|
|
519
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// A point-light shadow has six faces. Cache static casters once, then draw
|
|
2
|
+
// dynamic geometry over that depth on subsequent passes. STILL_LAYER selects
|
|
3
|
+
// the static casters. The revision and membership sum invalidate the cache
|
|
4
|
+
// when a caster moves, changes geometry, appears or disappears.
|
|
5
|
+
const STILL_LAYER = 2,
|
|
6
|
+
STILL_BIT = 1 << STILL_LAYER;
|
|
7
|
+
const still = { kept: false, sum: 0, round: 0 }; // round: which drawing of the kept picture this is
|
|
8
|
+
|
|
9
|
+
// Marks a caster that stands still
|
|
10
|
+
const markStill = object => object.layers.enable(STILL_LAYER);
|
|
11
|
+
const isStill = object => (object.layers.mask & STILL_BIT) !== 0;
|
|
12
|
+
// Where a mesh stands, how it is turned (about any axis) and its size, as one number
|
|
13
|
+
const placeOf = e => e[12] * 1.37 + e[13] * 2.11 + e[14] * 0.59 + e[0] * 5.3 + e[1] * 3.3 + e[2] * 4.1 + e[5] * 1.9 + e[6] * 2.7;
|
|
14
|
+
|
|
15
|
+
// Record a caster during cache generation. On subsequent draws, skip it only
|
|
16
|
+
// when its cached revision still matches the caller-supplied transform or batch revision.
|
|
17
|
+
function keptAway(object, as) {
|
|
18
|
+
if (!still.kept) {
|
|
19
|
+
object.keptIn = still.round;
|
|
20
|
+
object.keptAs = as;
|
|
21
|
+
still.sum += object.id;
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if (object.keptIn !== still.round || object.keptAs !== as) {
|
|
25
|
+
still.sum = NaN; // not in the picture as it stands: drawn now, and the picture drawn again
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
still.sum += object.id;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { isStill, keptAway, markStill, placeOf, still, STILL_LAYER };
|