roamwright 0.1.1 → 0.4.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/CONTRIBUTING.md +39 -8
- package/README.md +74 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/foliage.js +47 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/index-parts.js +60 -0
- package/geometry/instance-visibility.js +41 -0
- package/geometry/instances.js +23 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/part-ranges.js +18 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +304 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/ground-lookahead.js +29 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +184 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/piece-batch.js +74 -20
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +4 -0
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-budget.js +49 -0
- package/runtime/frame-yield.js +14 -0
- package/runtime/gpu-timing.js +6 -3
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
package/rendering/piece-batch.js
CHANGED
|
@@ -67,7 +67,8 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
67
67
|
const DEPTH_STEP = 1 / 16,
|
|
68
68
|
DEPTH_FROM = -1024,
|
|
69
69
|
PIECES_MOST = 65536;
|
|
70
|
-
|
|
70
|
+
const keyBuffers = []; // one scratch buffer per power of two, shared by all passes
|
|
71
|
+
let _keys;
|
|
71
72
|
// The i-th of `seen` pieces in order, nearest first (farthest first, from the end of the keys, where `far`)
|
|
72
73
|
const nthSeen = (i, seen, far) => _seen[_keys[far ? seen - 1 - i : i] % PIECES_MOST];
|
|
73
74
|
let multi; // the multi-draw extension, or null where the browser has none (the pieces are then drawn one by one)
|
|
@@ -85,6 +86,7 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
85
86
|
this.layout = null; // [name, array type, item size, normalized] of each attribute, from the first piece
|
|
86
87
|
this.indices = null; // an indexed batch keeps its indices here as well, to fill a larger buffer from when it grows
|
|
87
88
|
this.pieces = []; // by id: { vertexStart, vertexCount, start, count, sphere, shown, depth }, or null where one was removed
|
|
89
|
+
this.views = new Map(); // child meshes drawing pieces with alternate materials from these same buffers
|
|
88
90
|
this.spare = []; // ids to give out again
|
|
89
91
|
this.starts = new Int32Array(16); // the pieces after the first that the pass being drawn sees: where each begins (in bytes, if indexed)
|
|
90
92
|
this.counts = new Int32Array(16);
|
|
@@ -117,6 +119,43 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
117
119
|
if (!geometry.boundingSphere) geometry.computeBoundingSphere();
|
|
118
120
|
return this.record({ vertexStart, vertexCount, start, count, sphere: geometry.boundingSphere.clone() });
|
|
119
121
|
}
|
|
122
|
+
// A second material pass over a disjoint subset of this batch, sharing its buffers and
|
|
123
|
+
// transform. Only setView changes membership. Empty views skip drawing altogether.
|
|
124
|
+
materialView(material) {
|
|
125
|
+
const mesh = new THREE.Mesh(this.geometry, material);
|
|
126
|
+
const members = new Set();
|
|
127
|
+
this.views.set(mesh, members);
|
|
128
|
+
mesh.visible = false;
|
|
129
|
+
mesh.matrixAutoUpdate = false;
|
|
130
|
+
mesh.layers.mask = this.layers.mask;
|
|
131
|
+
mesh.castShadow = this.castShadow;
|
|
132
|
+
mesh.receiveShadow = this.receiveShadow;
|
|
133
|
+
mesh.boundingSphere = this.boundingSphere.clone();
|
|
134
|
+
mesh.raycast = this.raycast;
|
|
135
|
+
mesh.onBeforeRender = (_r, _s, viewer) => this.choose(viewer, mesh, members);
|
|
136
|
+
mesh.onAfterRender = (_r, _s, _c, _g, drawnWith) => this.drawRest(drawnWith);
|
|
137
|
+
mesh.onBeforeShadow = (_r, _o, _c, viewer, _g, drawnWith) => this.chooseShadow(viewer, drawnWith, mesh, members);
|
|
138
|
+
mesh.onAfterShadow = (_r, _o, _c, _v, _g, drawnWith) => this.drawRest(drawnWith);
|
|
139
|
+
super.add(mesh);
|
|
140
|
+
return mesh;
|
|
141
|
+
}
|
|
142
|
+
setView(id, mesh = null) {
|
|
143
|
+
const piece = this.pieces[id];
|
|
144
|
+
if (piece.view === mesh) return;
|
|
145
|
+
const members = mesh && this.views.get(mesh);
|
|
146
|
+
if (mesh && !members) throw new Error('Material view belongs to another batch');
|
|
147
|
+
if (piece.view) {
|
|
148
|
+
const previous = this.views.get(piece.view);
|
|
149
|
+
previous.delete(piece);
|
|
150
|
+
piece.view.visible = previous.size > 0;
|
|
151
|
+
}
|
|
152
|
+
piece.view = mesh;
|
|
153
|
+
if (mesh) {
|
|
154
|
+
members.add(piece);
|
|
155
|
+
mesh.visible = true;
|
|
156
|
+
}
|
|
157
|
+
this.changes++;
|
|
158
|
+
}
|
|
120
159
|
// Moves a piece into another batch of the same layout (one drawn in another material), copied from buffer to buffer on the card;
|
|
121
160
|
// returns its id there
|
|
122
161
|
move(id, to) {
|
|
@@ -164,7 +203,7 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
164
203
|
// A piece whose vertices are in place: its id
|
|
165
204
|
record(piece) {
|
|
166
205
|
const id = this.spare.length ? this.spare.pop() : this.pieces.length;
|
|
167
|
-
this.pieces[id] = { ...piece, shown: true, depth: 0 };
|
|
206
|
+
this.pieces[id] = { ...piece, shown: true, depth: 0, view: null };
|
|
168
207
|
this.changes++;
|
|
169
208
|
this.used.vertices += piece.vertexCount;
|
|
170
209
|
if (this.indices) this.used.indices += piece.count;
|
|
@@ -173,6 +212,7 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
173
212
|
}
|
|
174
213
|
remove(id) {
|
|
175
214
|
const piece = this.pieces[id];
|
|
215
|
+
if (piece.view) this.setView(id);
|
|
176
216
|
this.pieces[id] = null;
|
|
177
217
|
this.spare.push(id);
|
|
178
218
|
this.changes++;
|
|
@@ -207,18 +247,22 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
207
247
|
}
|
|
208
248
|
// The bounds of all pieces, by which Three decides whether the batch is in view at all; with no piece it never is
|
|
209
249
|
bound() {
|
|
210
|
-
const all = new THREE.Sphere(
|
|
250
|
+
const all = (this.geometry.boundingSphere ??= new THREE.Sphere());
|
|
251
|
+
all.makeEmpty();
|
|
211
252
|
for (const piece of this.pieces) if (piece) all.union(piece.sphere);
|
|
212
253
|
if (all.isEmpty()) all.radius = -Infinity;
|
|
213
|
-
this.
|
|
214
|
-
|
|
254
|
+
(this.boundingSphere ??= new THREE.Sphere()).copy(all);
|
|
255
|
+
for (const mesh of this.views.keys()) {
|
|
256
|
+
mesh.geometry = this.geometry;
|
|
257
|
+
mesh.boundingSphere.copy(all);
|
|
258
|
+
}
|
|
215
259
|
}
|
|
216
260
|
// Three sorts the opaque things of a pass nearest first by the centre of this sphere, so that what stands in front hides what is
|
|
217
261
|
// behind before it is shaded: the batch is placed where its nearest piece in the viewer's picture stands, as that piece's own mesh
|
|
218
262
|
// was, with a radius that still holds every piece (the sphere is also what Three culls the whole batch by)
|
|
219
|
-
sortBy(nearest) {
|
|
263
|
+
sortBy(nearest, mesh) {
|
|
220
264
|
const all = this.geometry.boundingSphere,
|
|
221
|
-
at =
|
|
265
|
+
at = mesh.boundingSphere;
|
|
222
266
|
at.center.copy(nearest.sphere.center);
|
|
223
267
|
at.radius = all.radius + at.center.distanceTo(all.center);
|
|
224
268
|
}
|
|
@@ -237,9 +281,12 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
237
281
|
// Packs the pieces together, one after another, into buffers `room` times what they fill, once they fill less than `use`
|
|
238
282
|
// of the ones they are in (a buffer only ever grows, and pieces are let go): what each piece holds is copied across on the card,
|
|
239
283
|
// and its indices are written again from the copy kept here; a batch with no piece left gives its room back. Returns whether it did either.
|
|
284
|
+
needsPack(use = this.fit.use) {
|
|
285
|
+
return this.held.vertices >= PACK_LEAST && this.used.vertices < this.held.vertices * use;
|
|
286
|
+
}
|
|
240
287
|
pack() {
|
|
241
288
|
const { vertices: filled, indices: filledIndices } = this.used;
|
|
242
|
-
if (this.
|
|
289
|
+
if (!this.needsPack()) return false;
|
|
243
290
|
if (!filled) {
|
|
244
291
|
this.empty();
|
|
245
292
|
return true;
|
|
@@ -313,6 +360,7 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
313
360
|
}
|
|
314
361
|
gl.bindBuffer(gl.COPY_WRITE_BUFFER, null);
|
|
315
362
|
this.geometry.boundingSphere = old.boundingSphere;
|
|
363
|
+
for (const mesh of this.views.keys()) mesh.geometry = this.geometry;
|
|
316
364
|
this.discard(old);
|
|
317
365
|
}
|
|
318
366
|
// Frees the batch's buffers on the card, once it is let go
|
|
@@ -328,23 +376,26 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
328
376
|
}
|
|
329
377
|
// The pieces a camera sees, nearest first (farthest first in a see-through material): the first becomes the range Three draws,
|
|
330
378
|
// the others are listed for the multi-draw that follows it
|
|
331
|
-
choose(viewer) {
|
|
379
|
+
choose(viewer, mesh = this, pieces = this.pieces) {
|
|
332
380
|
_view.setFromProjectionMatrix(_clip.multiplyMatrices(viewer.projectionMatrix, viewer.matrixWorldInverse).multiply(this.matrixWorld));
|
|
333
381
|
const e = _clip.elements;
|
|
334
382
|
let seen = 0;
|
|
335
|
-
for (const piece of
|
|
336
|
-
if (!piece || !piece.shown || !_view.intersectsSphere(piece.sphere)) continue;
|
|
383
|
+
for (const piece of pieces) {
|
|
384
|
+
if (!piece || (mesh === this && piece.view) || !piece.shown || !_view.intersectsSphere(piece.sphere)) continue;
|
|
337
385
|
const c = piece.sphere.center;
|
|
338
386
|
piece.depth = e[2] * c.x + e[6] * c.y + e[10] * c.z + e[14];
|
|
339
387
|
_seen[seen++] = piece;
|
|
340
388
|
}
|
|
341
|
-
if (seen >
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
389
|
+
if (seen > 1) {
|
|
390
|
+
const size = Math.ceil(Math.log2(seen));
|
|
391
|
+
_keys = keyBuffers[size] ??= new Float64Array(2 ** size);
|
|
392
|
+
for (let i = 0; i < seen; i++) _keys[i] = Math.max(0, Math.floor((_seen[i].depth - DEPTH_FROM) / DEPTH_STEP)) * PIECES_MOST + i;
|
|
393
|
+
_keys.fill(Infinity, seen);
|
|
394
|
+
_keys.sort();
|
|
395
|
+
}
|
|
396
|
+
const far = mesh.material.transparent,
|
|
397
|
+
first = seen > 1 ? nthSeen(0, seen, far) : seen ? _seen[0] : null;
|
|
398
|
+
if (first && camera === viewer && !far) this.sortBy(first, mesh);
|
|
348
399
|
if (first) this.geometry.setDrawRange(first.start, first.count);
|
|
349
400
|
else this.geometry.setDrawRange(0, -1); // a range Three takes for nothing to draw
|
|
350
401
|
this.rest = Math.max(0, seen - 1);
|
|
@@ -394,8 +445,11 @@ export function createPieceBatch({ renderer, camera, markStill = () => {}, isSti
|
|
|
394
445
|
}
|
|
395
446
|
// In a point light's shadow it is drawn only where it casts there, and not where a cached point shadow already contains its current revision
|
|
396
447
|
onBeforeShadow(_renderer, _object, _camera, shadowCamera, _geometry, drawnWith) {
|
|
397
|
-
|
|
398
|
-
|
|
448
|
+
this.chooseShadow(shadowCamera, drawnWith, this, this.pieces);
|
|
449
|
+
}
|
|
450
|
+
chooseShadow(shadowCamera, drawnWith, mesh, pieces) {
|
|
451
|
+
if (!drawnWith.isMeshDistanceMaterial || (this.pointShadow && !(isStill(mesh) && keptAway(mesh, this.changes))))
|
|
452
|
+
this.choose(shadowCamera, mesh, pieces);
|
|
399
453
|
else {
|
|
400
454
|
this.geometry.setDrawRange(0, -1);
|
|
401
455
|
this.rest = 0;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
export function createPlanarReflection({ camera, renderer, scene, drawnSize, waterCells, cellSize, scale = 0.5, minSize = 256, hiddenInterval = 4 }) {
|
|
4
|
+
const mirror = {
|
|
5
|
+
rt: null,
|
|
6
|
+
cam: new THREE.PerspectiveCamera(),
|
|
7
|
+
on: false,
|
|
8
|
+
plane: new THREE.Plane(),
|
|
9
|
+
clip: new THREE.Vector4(),
|
|
10
|
+
q: new THREE.Vector4(),
|
|
11
|
+
frustum: new THREE.Frustum(),
|
|
12
|
+
n: new THREE.Vector3(0, 1, 0),
|
|
13
|
+
o: new THREE.Vector3(),
|
|
14
|
+
a: new THREE.Vector3(),
|
|
15
|
+
b: new THREE.Vector3(),
|
|
16
|
+
rot: new THREE.Matrix4(),
|
|
17
|
+
m: new THREE.Matrix4(),
|
|
18
|
+
};
|
|
19
|
+
const WAVE_X = 0.1, // the waves bend the reflection sideways and up or down by up to this much of the picture's half-size
|
|
20
|
+
WAVE_Y = 0.16,
|
|
21
|
+
IN_FRONT = 0.05, // a corner nearer the mirror camera's plane than this is cut off
|
|
22
|
+
NOTHING = new THREE.Color(-1, -1, -1), // what the mirror holds outside the part drawn: the water takes the plain sky there
|
|
23
|
+
_colour = new THREE.Color(),
|
|
24
|
+
_corner = [new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4(), new THREE.Vector4()],
|
|
25
|
+
_cut = new THREE.Vector4(),
|
|
26
|
+
part = { x0: 0, x1: 0, y0: 0, y1: 0 }; // the part of the picture where water shows, from -1 to 1 each way
|
|
27
|
+
|
|
28
|
+
function findWaterPart(view, level) {
|
|
29
|
+
const M = mirror,
|
|
30
|
+
cells = (M.cells ??= waterCells());
|
|
31
|
+
let x0 = 1,
|
|
32
|
+
x1 = -1,
|
|
33
|
+
y0 = 1,
|
|
34
|
+
y1 = -1;
|
|
35
|
+
for (let c = 0; c < cells.length; c += 2) {
|
|
36
|
+
let left = true, // is the whole cell off one side of the picture?
|
|
37
|
+
right = true,
|
|
38
|
+
below = true,
|
|
39
|
+
above = true,
|
|
40
|
+
ax = 1, // the cell's own box
|
|
41
|
+
bx = -1,
|
|
42
|
+
ay = 1,
|
|
43
|
+
by = -1;
|
|
44
|
+
const take = p => {
|
|
45
|
+
const x = p.x / p.w,
|
|
46
|
+
y = p.y / p.w;
|
|
47
|
+
left &&= x < -1;
|
|
48
|
+
right &&= x > 1;
|
|
49
|
+
below &&= y < -1;
|
|
50
|
+
above &&= y > 1;
|
|
51
|
+
ax = Math.min(ax, x);
|
|
52
|
+
bx = Math.max(bx, x);
|
|
53
|
+
ay = Math.min(ay, y);
|
|
54
|
+
by = Math.max(by, y);
|
|
55
|
+
};
|
|
56
|
+
for (let k = 0; k < 4; k++)
|
|
57
|
+
_corner[k].set(cells[c] + (k === 1 || k === 2 ? cellSize : 0), level, cells[c + 1] + (k > 1 ? cellSize : 0), 1).applyMatrix4(view);
|
|
58
|
+
for (let k = 0; k < 4; k++) {
|
|
59
|
+
const a = _corner[k],
|
|
60
|
+
b = _corner[(k + 1) % 4];
|
|
61
|
+
if (a.w > IN_FRONT) take(a);
|
|
62
|
+
if (a.w > IN_FRONT !== b.w > IN_FRONT) take(_cut.lerpVectors(a, b, (IN_FRONT - a.w) / (b.w - a.w))); // where the edge passes the camera's plane
|
|
63
|
+
}
|
|
64
|
+
if (bx < ax || left || right || below || above) continue;
|
|
65
|
+
x0 = Math.min(x0, ax);
|
|
66
|
+
x1 = Math.max(x1, bx);
|
|
67
|
+
y0 = Math.min(y0, ay);
|
|
68
|
+
y1 = Math.max(y1, by);
|
|
69
|
+
}
|
|
70
|
+
part.x0 = Math.max(-1, x0 - WAVE_X);
|
|
71
|
+
part.x1 = Math.min(1, x1 + WAVE_X);
|
|
72
|
+
part.y0 = Math.max(-1, y0 - WAVE_Y);
|
|
73
|
+
part.y1 = Math.min(1, y1 + WAVE_Y);
|
|
74
|
+
return x1 > x0 && y1 > y0;
|
|
75
|
+
}
|
|
76
|
+
// Fits the mirror's camera and target to `part`: whole pixels of the target, and a projection that fills just those, so each
|
|
77
|
+
// reflected thing lands on the pixel it would have in the whole picture.
|
|
78
|
+
function fitToPart(projection, target) {
|
|
79
|
+
const w = target.width,
|
|
80
|
+
h = target.height,
|
|
81
|
+
px0 = Math.floor(((part.x0 + 1) / 2) * w),
|
|
82
|
+
px1 = Math.ceil(((part.x1 + 1) / 2) * w),
|
|
83
|
+
py0 = Math.floor(((part.y0 + 1) / 2) * h),
|
|
84
|
+
py1 = Math.ceil(((part.y1 + 1) / 2) * h),
|
|
85
|
+
x0 = (2 * px0) / w - 1,
|
|
86
|
+
x1 = (2 * px1) / w - 1,
|
|
87
|
+
y0 = (2 * py0) / h - 1,
|
|
88
|
+
y1 = (2 * py1) / h - 1,
|
|
89
|
+
e = projection.elements;
|
|
90
|
+
for (let col = 0; col < 16; col += 4) {
|
|
91
|
+
e[col] = (2 * e[col] - (x0 + x1) * e[col + 3]) / (x1 - x0);
|
|
92
|
+
e[col + 1] = (2 * e[col + 1] - (y0 + y1) * e[col + 3]) / (y1 - y0);
|
|
93
|
+
}
|
|
94
|
+
target.viewport.set(px0, py0, px1 - px0, py1 - py0);
|
|
95
|
+
target.scissor.set(px0, py0, px1 - px0, py1 - py0);
|
|
96
|
+
}
|
|
97
|
+
function sizeMirror() {
|
|
98
|
+
// half the size the world is drawn at each way: where the quality fit draws it smaller and upscales it, the mirror shrinks with it
|
|
99
|
+
const [w, h] = drawnSize();
|
|
100
|
+
if (mirror.rt) mirror.rt.setSize(Math.max(minSize, Math.round(w * scale)), Math.max(minSize, Math.round(h * scale)));
|
|
101
|
+
}
|
|
102
|
+
function renderMirror(u, wl, k, unseen, frame) {
|
|
103
|
+
const M = mirror, cp = camera.position;
|
|
104
|
+
if (M.off) k = 0; // debug switch, for measuring what the mirror costs
|
|
105
|
+
u.uReflOn.value = k;
|
|
106
|
+
M.on = k > 0.01;
|
|
107
|
+
if (!M.on || (unseen && M.rt && frame % hiddenInterval)) return;
|
|
108
|
+
if (!M.rt) {
|
|
109
|
+
M.rt = new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType, depthBuffer: true });
|
|
110
|
+
u.uRefl.value = M.rt.texture;
|
|
111
|
+
sizeMirror();
|
|
112
|
+
}
|
|
113
|
+
const c = M.cam,
|
|
114
|
+
o = M.o.set(0, wl, 0);
|
|
115
|
+
M.a.subVectors(o, cp).reflect(M.n).negate().add(o);
|
|
116
|
+
c.position.copy(M.a);
|
|
117
|
+
M.rot.extractRotation(camera.matrixWorld);
|
|
118
|
+
M.b.set(0, 0, -1).applyMatrix4(M.rot).add(cp);
|
|
119
|
+
M.a.subVectors(o, M.b).reflect(M.n).negate().add(o);
|
|
120
|
+
c.up.set(0, 1, 0).applyMatrix4(M.rot).reflect(M.n);
|
|
121
|
+
c.lookAt(M.a);
|
|
122
|
+
c.near = camera.near;
|
|
123
|
+
c.far = camera.far;
|
|
124
|
+
c.updateMatrixWorld();
|
|
125
|
+
c.projectionMatrix.copy(camera.projectionMatrix);
|
|
126
|
+
u.uReflM.value
|
|
127
|
+
.set(0.5, 0, 0, 0.5, 0, 0.5, 0, 0.5, 0, 0, 0.5, 0.5, 0, 0, 0, 1)
|
|
128
|
+
.multiply(c.projectionMatrix)
|
|
129
|
+
.multiply(c.matrixWorldInverse);
|
|
130
|
+
const whole = !findWaterPart(M.m.multiplyMatrices(c.projectionMatrix, c.matrixWorldInverse), wl); // no cell found where water was seen: draw it all
|
|
131
|
+
// tilt the near plane onto the water, so nothing under the surface gets into the mirror
|
|
132
|
+
M.plane.setFromNormalAndCoplanarPoint(M.n, M.o.set(0, wl - 0.05, 0)).applyMatrix4(c.matrixWorldInverse);
|
|
133
|
+
const cl = M.clip.set(M.plane.normal.x, M.plane.normal.y, M.plane.normal.z, M.plane.constant),
|
|
134
|
+
pe = c.projectionMatrix.elements,
|
|
135
|
+
q = M.q;
|
|
136
|
+
q.x = (Math.sign(cl.x) + pe[8]) / pe[0];
|
|
137
|
+
q.y = (Math.sign(cl.y) + pe[9]) / pe[5];
|
|
138
|
+
q.z = -1;
|
|
139
|
+
q.w = (1 + pe[10]) / pe[14];
|
|
140
|
+
cl.multiplyScalar(2 / cl.dot(q));
|
|
141
|
+
pe[2] = cl.x;
|
|
142
|
+
pe[6] = cl.y;
|
|
143
|
+
pe[10] = cl.z + 1;
|
|
144
|
+
pe[14] = cl.w;
|
|
145
|
+
const su = renderer.shadowMap.autoUpdate,
|
|
146
|
+
alpha = renderer.getClearAlpha();
|
|
147
|
+
const previous = renderer.getRenderTarget(), face = renderer.getActiveCubeFace(), mip = renderer.getActiveMipmapLevel();
|
|
148
|
+
renderer.getClearColor(_colour);
|
|
149
|
+
renderer.shadowMap.autoUpdate = false;
|
|
150
|
+
try {
|
|
151
|
+
M.rt.scissorTest = false;
|
|
152
|
+
M.rt.viewport.set(0, 0, M.rt.width, M.rt.height);
|
|
153
|
+
renderer.setRenderTarget(M.rt);
|
|
154
|
+
renderer.setClearColor(NOTHING, 1);
|
|
155
|
+
renderer.clear();
|
|
156
|
+
renderer.setClearColor(_colour, alpha);
|
|
157
|
+
if (!whole) {
|
|
158
|
+
fitToPart(c.projectionMatrix, M.rt);
|
|
159
|
+
M.rt.scissorTest = true;
|
|
160
|
+
renderer.setRenderTarget(M.rt);
|
|
161
|
+
}
|
|
162
|
+
renderer.render(scene, c);
|
|
163
|
+
} finally {
|
|
164
|
+
renderer.setClearColor(_colour, alpha);
|
|
165
|
+
renderer.setRenderTarget(previous, face, mip);
|
|
166
|
+
renderer.shadowMap.autoUpdate = su;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function dispose() {
|
|
170
|
+
mirror.rt?.dispose();
|
|
171
|
+
mirror.rt = mirror.cells = null;
|
|
172
|
+
mirror.on = false;
|
|
173
|
+
}
|
|
174
|
+
return { mirror, sizeMirror, renderMirror, dispose };
|
|
175
|
+
}
|
|
@@ -14,7 +14,7 @@ const RAY_FS = `/* glsl */ uniform sampler2D tMask; uniform vec2 uSun; uniform f
|
|
|
14
14
|
gl_FragColor = vec4(acc / 30.0, 0.0, 0.0, 1.0); }`;
|
|
15
15
|
|
|
16
16
|
// Ambient occlusion from the depth buffer alone: corners, eaves, doorways and the ground under things darken. Stored with the depth it was taken at.
|
|
17
|
-
const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx; uniform float uAspect, uAO; varying vec2 vUv;
|
|
17
|
+
const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx; uniform float uAspect, uAO; uniform vec2 uContact; varying vec2 vUv;
|
|
18
18
|
vec3 vp(vec2 uv){ uv = (floor(uv / uPx) + 0.5) * uPx; // every lookup is moved onto the centre of a depth texel, so the position and the depth it is built from always belong together (otherwise thin dark lines cross flat ground, worst on phones)
|
|
19
19
|
float z = uNF.x * uNF.y / ((uNF.y - uNF.x) * texture2D(tDepth, uv).x - uNF.y); return vec3((uv * 2.0 - 1.0) * uTan * -z, z); }
|
|
20
20
|
void main(){ vec2 uv = (floor(vUv / uPx) + 0.5) * uPx; // a half-size pixel's centre is the corner of four depth texels: step onto one, or rounding picks a different one from row to row and draws lines
|
|
@@ -31,6 +31,26 @@ const AO_FS = `/* glsl */ uniform sampler2D tDepth; uniform vec2 uNF, uTan, uPx;
|
|
|
31
31
|
}
|
|
32
32
|
float reach = sr * dist * 2.0 * uTan.y; // how far the samples really reach in the world once the screen's cap has bitten (metres)
|
|
33
33
|
ao = mix(clamp(1.0 - uAO * occ * rad / 14.0 * 2.1, 0.0, 1.0), 1.0, max(smoothstep(120.0, 240.0, dist), 1.0 - smoothstep(0.04, 0.12, reach))); // under a hand's breadth it would shade a nose's own curve: a face seen close was smeared dark beside the nose
|
|
34
|
+
// Four nearer samples resolve local contacts that can fall inside the first spiral tap.
|
|
35
|
+
// Skip subpixel contacts, and vary spacing by depth so the radius remains in world units.
|
|
36
|
+
if (uContact.y > 0.0 && dist > 1.5 && dist < 45.0) {
|
|
37
|
+
float r = uContact.x, screenRadius = min(r / (dist * 2.0 * uTan.y), 0.06);
|
|
38
|
+
float pixels = screenRadius / uPx.y;
|
|
39
|
+
if (pixels > 1.5) {
|
|
40
|
+
float contact = 0.0;
|
|
41
|
+
vec2 direction = vec2(0.923880, 0.382683);
|
|
42
|
+
for (int j = 0; j < 4; j++) {
|
|
43
|
+
vec2 dUV = direction * vec2(1.0 / uAspect, 1.0) * screenRadius;
|
|
44
|
+
direction = vec2(-direction.y, direction.x);
|
|
45
|
+
vec3 delta = vp(uv + dUV) - P;
|
|
46
|
+
float separation = length(delta);
|
|
47
|
+
float horizon = clamp((dot(N, delta) - r * 0.045) / max(separation, r * 0.12), 0.0, 1.0);
|
|
48
|
+
contact += horizon * (1.0 - smoothstep(r * 0.4, r * 2.0, separation));
|
|
49
|
+
}
|
|
50
|
+
float scale = smoothstep(1.5, 3.0, pixels) * smoothstep(1.5, 3.0, dist) * (1.0 - smoothstep(32.0, 45.0, dist));
|
|
51
|
+
ao *= 1.0 - clamp(uContact.y * 0.25 * contact * scale, 0.0, 0.55);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
34
54
|
}
|
|
35
55
|
gl_FragColor = vec4(ao, dist, 0.0, 1.0); }`;
|
|
36
56
|
|
package/rendering/post.js
CHANGED
|
@@ -44,6 +44,8 @@ export function createPostProcessing({
|
|
|
44
44
|
referenceLogLuminance = 0,
|
|
45
45
|
expectedLogLuminance = () => referenceLogLuminance,
|
|
46
46
|
adaptationStrength = 0.35,
|
|
47
|
+
contactRadius = 0.25,
|
|
48
|
+
contactStrength = () => 0,
|
|
47
49
|
maxExposureStops = 0.8,
|
|
48
50
|
eyeAdaptation = createEyeAdaptation(),
|
|
49
51
|
depthOfField = createDepthOfField(),
|
|
@@ -218,6 +220,7 @@ export function createPostProcessing({
|
|
|
218
220
|
uPx: { value: new THREE.Vector2(0.001, 0.001) },
|
|
219
221
|
uAspect: { value: 1 },
|
|
220
222
|
uAO: { value: 1 },
|
|
223
|
+
uContact: { value: new THREE.Vector2(contactRadius, 0) },
|
|
221
224
|
},
|
|
222
225
|
});
|
|
223
226
|
post.rayScene = fullScreen(post.rayMat);
|
|
@@ -254,6 +257,7 @@ export function createPostProcessing({
|
|
|
254
257
|
a.uTan.value.copy(u.uTan.value);
|
|
255
258
|
a.uAspect.value = u.uAspect.value;
|
|
256
259
|
a.uAO.value = u.uAO.value;
|
|
260
|
+
a.uContact.value.y = contactStrength();
|
|
257
261
|
rn.setRenderTarget(post.aoRT);
|
|
258
262
|
rn.render(post.aoScene, post.aoCam);
|
|
259
263
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const RAIN_PARS = `/* glsl */
|
|
2
|
+
uniform float uRain;`;
|
|
3
|
+
|
|
4
|
+
// In the water's main, after its waves (g): adds the rings' slope to g. Reads vW, dist, g, uTime, seaHash and OW.
|
|
5
|
+
const RAIN_GLSL = `/* glsl */
|
|
6
|
+
if (uRain > 0.01 && dist < 40.0) {
|
|
7
|
+
float showers = uRain * (1.0 - smoothstep(22.0, 40.0, dist)) * OW(30.0);
|
|
8
|
+
for (int layer = 0; layer < 2; layer++) {
|
|
9
|
+
float cellsAMetre = layer == 0 ? 1.6 : 2.3; // two grids of different size, so neither shows
|
|
10
|
+
vec2 q = vW.xz * cellsAMetre + float(layer) * vec2(0.5, 0.37), cell = floor(q), f = fract(q) - 0.5;
|
|
11
|
+
float h = seaHash(cell + float(layer) * 91.7), h2 = seaHash(cell.yx + 17.3), h3 = fract(h * 31.7 + h2 * 7.3);
|
|
12
|
+
float age = fract(uTime * (0.8 + 0.7 * h2) + h * 7.0); // a drop strikes the cell about once a second
|
|
13
|
+
float reach = 0.14 + 0.16 * h3; // how far its ring spreads, in cells
|
|
14
|
+
vec2 d = f - (vec2(h, h2) - 0.5) * (0.8 - 2.0 * reach); // from where it struck, so the ring stays in its cell
|
|
15
|
+
float r = length(d) + 1e-4, x = (r - age * reach) * 18.0 * 1.6 / cellsAMetre; // across the ring: 18 radians a cell of the first grid
|
|
16
|
+
float ring = sin(x) * exp(-x * x * 0.06) * (1.0 - age) * (1.0 - age) * (0.6 + 0.4 * h3);
|
|
17
|
+
float struck = step(fract(h * 53.1 + float(layer) * 0.5), uRain * (layer == 0 ? 0.8 : 0.6)); // a lighter shower, fewer drops
|
|
18
|
+
g += d / r * (0.4 * ring * struck * showers);
|
|
19
|
+
}
|
|
20
|
+
}`;
|
|
21
|
+
|
|
22
|
+
export { RAIN_GLSL, RAIN_PARS };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
// Packed earth, wheel ruts and rain-dependent puddles on a road strip.
|
|
4
|
+
const ROAD_COMMON = `/* glsl */ varying vec4 vRoad; varying vec3 vRW; float gPud = 0.0;
|
|
5
|
+
float rh(vec2 p){ vec3 q = fract(vec3(p.xyx) * .1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }
|
|
6
|
+
float rn(vec2 p){ vec2 i = floor(p), f = fract(p); f = f * f * (3.0 - 2.0 * f);
|
|
7
|
+
return mix(mix(rh(i), rh(i + vec2(1, 0)), f.x), mix(rh(i + vec2(0, 1)), rh(i + vec2(1, 1)), f.x), f.y); }
|
|
8
|
+
varying vec3 vVerge; uniform sampler2D uGrit; uniform float uRoadDamp;`;
|
|
9
|
+
const ROAD_SURFACE = `/* glsl */
|
|
10
|
+
float x = vRoad.x, s = vRoad.y, hw = vRoad.z, fine = smoothstep(160.0, 45.0, length(vViewPosition)); // fine grain fades out before it can shimmer
|
|
11
|
+
float nw = mix(0.5, rn(vRW.xz * 1.9), fine), nb = rn(vRW.xz * 0.37), ns = rn(vRW.xz * 0.11 + 7.0);
|
|
12
|
+
float side = x > 0.0 ? 3.1 : 9.4; // each edge meanders on its own
|
|
13
|
+
float edge = hw + (rn(vec2(s * 0.018, side + 1.0)) - 0.5) * 1.4 + (rn(vec2(s * 0.06, side)) - 0.5) * 1.2 + (rn(vec2(s * 0.24, side + 2.0)) - 0.5) * 0.5 + (rn(vec2(s * 0.9, side + 4.0)) - 0.5) * 0.3 * fine - abs(x); // metres inside the travelled edge
|
|
14
|
+
float drift = (rn(vec2(s * 0.02, 1.3)) - 0.5) * 1.3 + (rn(vec2(s * 0.09, 2.3)) - 0.5) * 0.3, gauge = 0.78 + (rn(vec2(s * 0.05, 5.1)) - 0.5) * 0.14; // the ruts wander as a pair
|
|
15
|
+
float c = x - drift, r1 = c - gauge, r2 = c + gauge, rw = 0.08 + 0.08 * ns, deep = 0.18 + 0.3 * rn(vec2(s * 0.07, 6.6));
|
|
16
|
+
float rut = (exp(-r1 * r1 / rw) + exp(-r2 * r2 / rw)) * smoothstep(-0.3, 0.6, edge);
|
|
17
|
+
float crown = (1.0 - smoothstep(gauge * 0.3, gauge * 0.72, abs(c))) * vRoad.w;
|
|
18
|
+
float grass = clamp(crown * smoothstep(0.38, 0.66, nb * 0.75 + nw * 0.25) + (1.0 - smoothstep(-0.2, 0.9, edge)) * smoothstep(0.42, 0.68, nb) * 0.7, 0.0, 1.0);
|
|
19
|
+
gPud = smoothstep(0.08, 0.45, uRoadDamp) * smoothstep(0.35, 0.8, rut) * smoothstep(0.7, 0.76, rn(vec2(s * 0.16, c * 0.6 + 4.0)) * 0.75 + ns * 0.25) * (1.0 - grass); // water lies in the deeper stretches of rut
|
|
20
|
+
float worn = smoothstep(-0.2, 0.9, edge); // the travelled way, packed and dry, against its damp margin
|
|
21
|
+
float near = smoothstep(70.0, 6.0, length(vViewPosition)); // grit, pebbles and dried cracks, at walking distance
|
|
22
|
+
vec3 g1 = texture2D(uGrit, vRW.xz * 0.45).rgb, g2 = texture2D(uGrit, vRW.xz * 0.13 + 0.37).rgb;
|
|
23
|
+
float grit = mix(1.0, (0.72 + 0.56 * g1.r) * (0.86 + 0.28 * g2.r) * (1.0 + 0.28 * g1.g * g2.r * worn) * (1.0 - 0.22 * g1.b * worn * (1.0 - min(rut, 1.0))), near);
|
|
24
|
+
vec3 soil = diffuseColor.rgb * grit * (0.86 + 0.28 * ns) * (0.9 + 0.2 * nw) * mix(0.72, 1.06, worn) * (1.0 - deep * min(rut, 1.0)); // ruts are compacted, wetter and shadowed
|
|
25
|
+
diffuseColor.rgb = mix(soil, vVerge * (0.8 + 0.4 * nw), grass) * (1.0 - 0.45 * gPud);
|
|
26
|
+
diffuseColor.a *= smoothstep(-0.9, 0.5, edge + (nw - 0.5) * 0.8);`;
|
|
27
|
+
const ROAD_SHEEN = `/* glsl */
|
|
28
|
+
#if NUM_HEMI_LIGHTS > 0
|
|
29
|
+
if (gPud > 0.0) { float fr = 0.04 + 0.96 * pow(1.0 - clamp(dot(normal, normalize(vViewPosition)), 0.0, 1.0), 5.0); outgoingLight += gPud * fr * hemisphereLights[0].skyColor * 0.6; }
|
|
30
|
+
#endif
|
|
31
|
+
#include <opaque_fragment>`;
|
|
32
|
+
|
|
33
|
+
export function createRoadMaterial({ grit, dampness = { value: 0 } }) {
|
|
34
|
+
const mat = new THREE.MeshStandardMaterial({
|
|
35
|
+
transparent: true,
|
|
36
|
+
depthWrite: false,
|
|
37
|
+
vertexColors: true,
|
|
38
|
+
roughness: 0.95,
|
|
39
|
+
polygonOffset: true,
|
|
40
|
+
polygonOffsetFactor: -2,
|
|
41
|
+
polygonOffsetUnits: -2,
|
|
42
|
+
});
|
|
43
|
+
mat.onBeforeCompile = sh => {
|
|
44
|
+
sh.uniforms.uGrit = { value: grit };
|
|
45
|
+
sh.uniforms.uRoadDamp = dampness;
|
|
46
|
+
sh.vertexShader = sh.vertexShader
|
|
47
|
+
.replace(
|
|
48
|
+
'#include <common>',
|
|
49
|
+
'#include <common>\nattribute vec4 road; attribute vec3 verge; varying vec4 vRoad; varying vec3 vRW, vVerge;',
|
|
50
|
+
)
|
|
51
|
+
.replace(
|
|
52
|
+
'#include <begin_vertex>',
|
|
53
|
+
'#include <begin_vertex>\nvRoad = road; vVerge = verge; vRW = (modelMatrix * vec4(position, 1.0)).xyz;',
|
|
54
|
+
);
|
|
55
|
+
sh.fragmentShader = sh.fragmentShader
|
|
56
|
+
.replace('#include <common>', '#include <common>\n' + ROAD_COMMON)
|
|
57
|
+
.replace('#include <color_fragment>', '#include <color_fragment>\n' + ROAD_SURFACE)
|
|
58
|
+
.replace('#include <roughnessmap_fragment>', 'float roughnessFactor = mix(roughness, 0.08, gPud);')
|
|
59
|
+
.replace('#include <opaque_fragment>', ROAD_SHEEN);
|
|
60
|
+
};
|
|
61
|
+
mat.customProgramCacheKey = () => 'road-earth';
|
|
62
|
+
return mat;
|
|
63
|
+
}
|