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
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// The noise foam is torn by, shared by every water (the sea's surf, a wake) and the beach.
|
|
2
|
+
export const FOAM_GLSL = `/* glsl */
|
|
3
|
+
float seaHash(vec2 c) { c = mod(c, 289.0); return fract(sin(dot(c, vec2(127.1, 311.7))) * 43758.5453); }
|
|
4
|
+
float seaNoise(vec2 x) {
|
|
5
|
+
vec2 i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f);
|
|
6
|
+
return mix(mix(seaHash(i), seaHash(i + vec2(1.0, 0.0)), f.x), mix(seaHash(i + vec2(0.0, 1.0)), seaHash(i + vec2(1.0, 1.0)), f.x), f.y);
|
|
7
|
+
}
|
|
8
|
+
// foam's lace, 0 to 1: threads of foam round holes of clear water, gathered in patches tens of metres across; once the
|
|
9
|
+
// threads would be finer than a pixel, the share of the water they cover (px: metres to a pixel)
|
|
10
|
+
float seaLace(vec2 q, float t, float px) {
|
|
11
|
+
vec2 a = q * 0.55 + vec2(0.07, 0.04) * t;
|
|
12
|
+
float n = seaNoise(a + 1.6 * vec2(seaNoise(a * 0.43 - 0.03 * t), seaNoise(a * 0.43 + 5.2)));
|
|
13
|
+
float n2 = seaNoise(q * 1.7 - vec2(0.05, 0.11) * t);
|
|
14
|
+
// a thread is about a tenth of a metre wide, the finer net's a third of that: each fades to its share of the water
|
|
15
|
+
// while it is still a pixel or two wide, or it crawls and sparkles as the foam drifts
|
|
16
|
+
float coarse = mix(0.2, smoothstep(0.72, 0.93, 1.0 - abs(2.0 * n - 1.0)), 1.0 - smoothstep(0.03, 0.1, px));
|
|
17
|
+
float fine = mix(0.08, smoothstep(0.78, 0.95, 1.0 - abs(2.0 * n2 - 1.0)), 1.0 - smoothstep(0.01, 0.035, px));
|
|
18
|
+
float gathered = mix(0.35, 1.0, smoothstep(0.3, 0.7, seaNoise(q * 0.09 + 3.1)));
|
|
19
|
+
return max(coarse, 0.7 * fine) * gathered;
|
|
20
|
+
}`;
|
|
21
|
+
|
|
22
|
+
// Shared by the water and the beach, so a breaking wave and the swash it sends up the sand keep time: the swell's phase at
|
|
23
|
+
// the water's edge, and its sets. Positions are measured from the land's corner (uSeaO), so the numbers stay small out here,
|
|
24
|
+
// 14 km from the world's middle.
|
|
25
|
+
export const SWELL_GLSL = `/* glsl */
|
|
26
|
+
const float SWELL_W = 0.785; // radians a second: a wave comes in every eight seconds
|
|
27
|
+
const float SHOAL_K = 8.0; // the swell's phase per root metre of depth: its crests close up as the water shoals
|
|
28
|
+
const float SEA_TAU = 6.2831853;
|
|
29
|
+
// how far a crest is put back along the shore, so the waves break in peaks and sections, never in one line
|
|
30
|
+
float swellShift(vec2 q) { return 1.9 * sin(q.x * 0.0123 + 1.3 * sin(q.y * 0.0171)) + 1.2 * sin(q.y * 0.0219 - q.x * 0.0083 + 2.0); }
|
|
31
|
+
// the swell's phase over the shoals, depth metres deep: a crest where it is a whole turn; at the water's edge (depth 0)
|
|
32
|
+
// a crest is a bore reaching the sand
|
|
33
|
+
float swellPhase(vec2 q, float depth, float t) { return SHOAL_K * sqrt(max(depth, 0.0) + 0.03) + t * SWELL_W + swellShift(q); }
|
|
34
|
+
// the height of the set, 0.48 to 1.08: every seventh wave or so is a big one
|
|
35
|
+
float swellSet(vec2 q, float t) { return 0.78 + 0.3 * sin(t * SWELL_W / 7.0 + (q.x + q.y) * 0.0021); }`;
|
|
36
|
+
|
|
37
|
+
// The water's own: its uniforms, and how far a point lies from the nearest wall and how far the moles shelter it.
|
|
38
|
+
export const seaParameters = ({ moleCount, quayCount, moleReach, moleWidth }) => `/* glsl */
|
|
39
|
+
uniform vec4 uSeaO; uniform vec2 uSwellDir;
|
|
40
|
+
uniform vec4 uMole[${moleCount}], uMoleF[${moleCount}], uQuay[${quayCount}];
|
|
41
|
+
${SWELL_GLSL}
|
|
42
|
+
// (metres from p to the face of the nearest mole or quay, the share of the swell the moles let reach p, how far p lies
|
|
43
|
+
// among them: there they, not the city's sheltered water, say how still it is)
|
|
44
|
+
vec3 seaWalls(vec2 p) {
|
|
45
|
+
float d = 1e4, open = 1.0, among = 0.0;
|
|
46
|
+
for (int i = 0; i < ${moleCount}; i++) {
|
|
47
|
+
vec4 M = uMole[i], F = uMoleF[i]; // centre x, z, radius, mouth; the city's turn (sin, cos), half the mouth's angle, in use
|
|
48
|
+
vec2 rel = p - M.xy; float r = length(rel);
|
|
49
|
+
if (F.w < 0.5 || r > M.z + 240.0) continue;
|
|
50
|
+
float a = atan(dot(rel, vec2(F.y, -F.x)), dot(rel, F.xy)); // radians round from straight out to sea
|
|
51
|
+
if (abs(a) > ${moleReach.toFixed(2)}) continue;
|
|
52
|
+
among = max(among, smoothstep(M.z + 240.0, M.z + 120.0, r));
|
|
53
|
+
float pastMouth = abs(a - M.w) - F.z; // radians beyond the mouth's edge: below 0 in the mouth
|
|
54
|
+
if (pastMouth > 0.0) d = min(d, abs(r - M.z) - ${(moleWidth / 2).toFixed(1)});
|
|
55
|
+
if (r < M.z) open *= mix(1.0, 0.35, smoothstep(0.0, 0.1, pastMouth)); // behind the mole, but for what comes in by its mouth
|
|
56
|
+
}
|
|
57
|
+
for (int i = 0; i < ${quayCount}; i++) {
|
|
58
|
+
vec4 Q = uQuay[i]; vec2 ab = Q.zw - Q.xy, ap = p - Q.xy;
|
|
59
|
+
d = min(d, length(ap - ab * clamp(dot(ap, ab) / dot(ab, ab), 0.0, 1.0)));
|
|
60
|
+
}
|
|
61
|
+
return vec3(max(d, 0.0), open, among);
|
|
62
|
+
}`;
|
|
63
|
+
|
|
64
|
+
// In the water's main: bends its waves (g) by the swell and gives the white water (rapid) and how churned the water is (surf).
|
|
65
|
+
// Reads the water's own: vW, bed, dep, dist, g, fp, OW, wv, uDepth, uDepthR, uTime.
|
|
66
|
+
export const SEA_WAVES = `/* glsl */
|
|
67
|
+
vec2 sq = vW.xz - uSeaO.xy;
|
|
68
|
+
float seaPx = length(fwidth(sq)), seaAfar = smoothstep(0.05, 0.3, seaPx); // metres to a pixel; and how far that is from near
|
|
69
|
+
vec3 walls = dist < 2500.0 ? seaWalls(vW.xz) : vec3(1e4, 1.0, 0.0);
|
|
70
|
+
float exposed = mix(clamp(1.0 - bed.b * 1.15, 0.0, 1.0), 1.0, walls.z) * walls.y; // the bed's B is sheltered water (0.84 at most): a harbour lies still
|
|
71
|
+
float setH = swellSet(sq, uTime), offshore = smoothstep(5.0, 7.8, dep);
|
|
72
|
+
// offshore, two trains of long swell running before the wind
|
|
73
|
+
vec2 swell2 = mat2(0.9, 0.435, -0.435, 0.9) * uSwellDir;
|
|
74
|
+
g += (wv(sq, -uSwellDir, 0.074, 0.85, 0.34 * setH) * OW(0.074) + wv(sq, -swell2, 0.11, 1.04, 0.18) * OW(0.11)) * offshore * exposed;
|
|
75
|
+
// over the shoals the crests follow the depth, closing up as it shallows: the phase is SHOAL_K times its root, which
|
|
76
|
+
// spaces them as waves are spaced over a steady slope. Offshore and in still water none of this is worked out.
|
|
77
|
+
float shoals = (1.0 - offshore) * exposed, surf = 0.0, white = 0.0, seaLaceK = 0.0;
|
|
78
|
+
vec2 seaward = -uSwellDir;
|
|
79
|
+
if (shoals > 0.0) {
|
|
80
|
+
vec2 bedUv = uDepthR.xy + vW.xz * uDepthR.zw; // (no mipmaps: the level is always 0)
|
|
81
|
+
vec2 deepens = (vec2(textureLod(uDepth, bedUv + vec2(uDepthR.z * uSeaO.z, 0.0), 0.0).r, textureLod(uDepth, bedUv + vec2(0.0, uDepthR.w * uSeaO.z), 0.0).r) - bed.r) * (10.0 / uSeaO.z);
|
|
82
|
+
float slope = length(deepens), h = max(dep, 0.0), rootH = sqrt(h + 0.03);
|
|
83
|
+
if (slope > 0.002) seaward = deepens / slope;
|
|
84
|
+
float psi = swellPhase(sq, h, uTime);
|
|
85
|
+
float sharp = mix(1.0, 4.5, smoothstep(4.0, 0.8, h)); // round swell, peaked as it shoals
|
|
86
|
+
float crest = exp(sharp * (cos(psi) - 1.0));
|
|
87
|
+
float dpsi = SHOAL_K * min(slope, 0.25) / (2.0 * rootH); // radians a metre, seaward
|
|
88
|
+
float breakAt = (1.6 + 0.6 * sin(sq.x * 0.029 + sq.y * 0.023)) * setH; // the depth the crests break at: deeper for a set's big waves, and in peaks
|
|
89
|
+
surf = smoothstep(breakAt, breakAt * 0.5, h) * shoals;
|
|
90
|
+
float swellH = 0.3 * setH * shoals * mix(1.0, 0.45, surf) * smoothstep(0.0, 0.3, h); // a broken wave runs on as a lower bore
|
|
91
|
+
g -= seaward * (swellH * sharp * sin(psi) * crest * min(dpsi, 2.5)) * OW(dpsi * sharp);
|
|
92
|
+
// white water: the lip as a crest curls, the bore it leaves rolling in, and lace torn from it lying between
|
|
93
|
+
if (surf > 0.0) {
|
|
94
|
+
seaLaceK = seaLace(sq + seaward * (uTime * 0.6), uTime, seaPx);
|
|
95
|
+
float behind = fract(psi / SEA_TAU);
|
|
96
|
+
white = surf * (exp(-behind * 10.0) * (0.65 + 0.35 * seaLaceK) + exp(-behind * 2.2) * seaLaceK * 0.9 + 0.35 * seaLaceK + smoothstep(0.92, 1.0, behind) * 0.5);
|
|
97
|
+
// from afar the breakers' white lines, each a pixel or two, come and go along the whole coast at once: there the white
|
|
98
|
+
// water is held at what it is on average over a wave, as the beach's swash is (SHORE_GLSL)
|
|
99
|
+
white = mix(white, surf * (0.085 + 0.745 * seaLaceK), seaAfar);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
// at a mole or a quay's foot: a ring of foam, bursting out as each crest of the swell strikes; a lap of it in a harbour
|
|
103
|
+
if (walls.x < 60.0) {
|
|
104
|
+
if (surf <= 0.0) seaLaceK = seaLace(sq + seaward * (uTime * 0.6), uTime, seaPx);
|
|
105
|
+
float strike = mix(pow(0.5 + 0.5 * cos(-0.074 * dot(uSwellDir, sq) + 0.85 * uTime), 4.0), 0.27, seaAfar); // its mean, from afar
|
|
106
|
+
float reach = mix(1.2, 13.0, exposed * (0.3 + 0.7 * strike)); // metres the foam spreads from the wall's foot
|
|
107
|
+
float burst = exp(-walls.x / reach) * (0.6 + 0.4 * seaLaceK) * (0.3 + 0.7 * exposed * (0.45 + 0.55 * strike));
|
|
108
|
+
white = max(white, burst + exp(-walls.x / 22.0) * exposed * seaLaceK * 0.8); // and the lace it leaves spreading round it
|
|
109
|
+
}
|
|
110
|
+
rapid = clamp(white, 0.0, 1.0) * mix(1.0, 0.7, 1.0 - OW(1.5));`;
|
|
111
|
+
|
|
112
|
+
// The beach's own colour, in the ground's shader as soon as its baked colour (base) is read: the bake fades the foot of the
|
|
113
|
+
// beach into the grey-green of a lake's bed over the 1.6 m about the sea's level, which on the
|
|
114
|
+
// sea's shore laid a green band along every beach. There the sea's bed is drawn instead: the beach's pale sand into the
|
|
115
|
+
// shallows, darkening and greening with weed only as it deepens. Reads vWP, landUv, uLandR, uSea, uShore; sets base, and past the
|
|
116
|
+
// map's edge under the sea tint's salt and cover too.
|
|
117
|
+
export const shoreBed = ({ beach, lakeBed, weed }) => `/* glsl */
|
|
118
|
+
if (uShore > 0.5 && vWP.y < uSea + 1.4 && texture2D(uSeaDepth, landUv).a > 0.5) { // the sea's bed, never a lake's sunk below the sea (the map's A)
|
|
119
|
+
const vec3 BEACH = vec3(${beach.join(', ')}), LAKE_BED = vec3(${lakeBed.join(', ')}), WEED = vec3(${weed.join(', ')}); // palette values in linear light
|
|
120
|
+
const float OFF_MAP_FADE = 150.0; // metres past the map's edge over which its own bed gives way
|
|
121
|
+
float baked = 1.0 - smoothstep(uSea - 0.2, uSea + 1.4, vWP.y); // how much of the bed's tone the bake laid here
|
|
122
|
+
float under = uSea - vWP.y; // metres of water over the bed
|
|
123
|
+
// wet sand in the shallows, weed on the middle slope, and the bed's own tone by 7 m, the plain bed material's
|
|
124
|
+
vec3 seaBed = mix(BEACH * 0.72, mix(LAKE_BED, WEED, 0.4 * (1.0 - smoothstep(4.5, 7.0, under))), smoothstep(1.2, 6.0, under));
|
|
125
|
+
// past the map's edge its maps are read at the edge, so a cape that meets it ran on as a pale band of sand under the sea
|
|
126
|
+
// to the horizon: under water out there the bed takes the tone the bake lays under all the sea, crust and cover none
|
|
127
|
+
vec2 pastUv = max(-landUv, landUv - 1.0);
|
|
128
|
+
float offMap = smoothstep(0.0, OFF_MAP_FADE, max(pastUv.x / uLandR.z, pastUv.y / uLandR.w)) * (1.0 - smoothstep(uSea - 0.3, uSea + 0.3, vWP.y));
|
|
129
|
+
base = mix(base, LAKE_BED, offMap); tint.a *= 1.0 - offMap; cover *= 1.0 - offMap;
|
|
130
|
+
base += (seaBed - LAKE_BED) * baked;
|
|
131
|
+
}`;
|
|
132
|
+
|
|
133
|
+
// The beach, in the ground's shader after its colour and relief (diffuseColor, gTH) are worked out: where the sea meets the
|
|
134
|
+
// sand, the swash runs up and drains back with a band of foam at its front, the sand under it dark and glassy; below the
|
|
135
|
+
// highest reach of a set's swash the sand lies wet, darker and glossier the lower it is, and at that reach a line of the
|
|
136
|
+
// foam's last lace and the wrack it left. shoreGloss (glassy) and shoreDamp (wet) are for the roughness. Reads vWP, landUv,
|
|
137
|
+
// vd, gWN, uSea, uTime, uLandR, uShore, uSeaDepth (the water's depth map, its B sheltered water), uWind, uNight; a shore with no
|
|
138
|
+
// depth map defines SHORE_EXPOSED, how open to the swell it is (0 to 1), instead.
|
|
139
|
+
export const SHORE_GLSL = `/* glsl */
|
|
140
|
+
#ifndef SHORE_EXPOSED
|
|
141
|
+
#define SHORE_EXPOSED clamp(1.0 - texture2D(uSeaDepth, landUv).b * 1.15, 0.0, 1.0)
|
|
142
|
+
#define SHORE_AT_SEA (texture2D(uSeaDepth, landUv).a > 0.5)
|
|
143
|
+
#else
|
|
144
|
+
#define SHORE_AT_SEA true
|
|
145
|
+
#endif
|
|
146
|
+
float shoreGloss = 0.0, shoreDamp = 0.0;
|
|
147
|
+
float rise = vWP.y - uSea; // metres above the still sea
|
|
148
|
+
if (uShore > 0.5 && rise < 1.0 && rise > -0.3 && SHORE_AT_SEA) { // the sea's swash, never on a lake's bed sunk below it
|
|
149
|
+
// the swash's marks fade out over the last of the block, under the still sea, rather than stop at a line along the shore
|
|
150
|
+
vec3 dry = diffuseColor.rgb; float flatTH = gTH, inSwash = smoothstep(-0.3, -0.1, rise);
|
|
151
|
+
vec2 sq = vWP.xz - uLandR.xy;
|
|
152
|
+
float exposed = SHORE_EXPOSED, px = length(fwidth(sq)); // metres to a pixel
|
|
153
|
+
// seen from afar the swash's running edge is a pixel or less across and its sheet sweeps metres of sand at a time:
|
|
154
|
+
// there its edge is softened and the sheet held nearer its average, so the coast does not pulse
|
|
155
|
+
float afar = smoothstep(0.05, 0.3, px);
|
|
156
|
+
float setH = swellSet(sq, uTime);
|
|
157
|
+
float runUp = 0.05 + 0.42 * setH * exposed; // how high the swash climbs: a lap of a few centimetres in a harbour
|
|
158
|
+
float tau = fract(swellPhase(sq, 0.0, uTime) / SEA_TAU); // how far through its wave: 0 as the bore reaches the sand
|
|
159
|
+
float up = tau < 0.3 ? 1.0 - (1.0 - tau / 0.3) * (1.0 - tau / 0.3) : 1.0 - pow((tau - 0.3) / 0.7, 1.4); // a quick rush up, a slow drain back
|
|
160
|
+
float front = runUp * up + 0.03 * (seaNoise(sq * 0.31) - 0.5) - 0.02, fw = fwidth(rise) + 0.003 + 0.06 * afar * runUp;
|
|
161
|
+
float sheet = 1.0 - smoothstep(front - fw, front + fw, rise); // the sheet of water as it is now; the foam rides its front
|
|
162
|
+
float film = mix(sheet, 1.0 - smoothstep(-0.02, runUp * 0.75, rise), afar * 0.85); // and as it darkens the sand: from afar, the time it covers it
|
|
163
|
+
float lace = seaLace(sq, uTime, px);
|
|
164
|
+
// the foam rides the front up the sand, thick in the rush and thinning as it drains; what it strands lies as lace
|
|
165
|
+
float rush = 1.0 - smoothstep(0.15, 0.55, tau);
|
|
166
|
+
float edge = sheet * smoothstep(front - 0.05 - 0.05 * rush - fw, front - fw * 0.5, rise) * (0.35 + 0.65 * rush) * (0.6 + 0.4 * lace);
|
|
167
|
+
float stranded = sheet * lace * 0.4 * (1.0 - rush) * (1.0 - smoothstep(0.0, 0.6, rise / max(front, 0.05))); // lying low, where the drain left it
|
|
168
|
+
// from afar the foam riding up and down the sand is a white line sweeping the beach every eight seconds, which reads as the
|
|
169
|
+
// whole coast flickering (the owner): there it is held at where it lies on average, a faint band over the swash's middle
|
|
170
|
+
float meanFoam = exp(-abs(rise - 0.5 * runUp) / max(0.4 * runUp, fw)) * (0.15 + 0.15 * lace);
|
|
171
|
+
float foam = clamp(mix(max(edge, stranded), meanFoam, afar) * (0.3 + 0.7 * exposed), 0.0, 1.0);
|
|
172
|
+
// the tide line: as high as a set's big waves reach, ragged along the shore; the sand below it is wet, darkest and
|
|
173
|
+
// glossiest at the bottom, and glassy for a moment where the backwash has just drained off it
|
|
174
|
+
float reach = 0.07 + 0.46 * exposed * (0.9 + 0.2 * seaNoise(sq * 0.045)) + 0.09 * (seaNoise(sq * 0.11 + 7.0) - 0.5) + 0.03 * (seaNoise(sq * 0.6) - 0.5); // meandering
|
|
175
|
+
float wet = 1.0 - smoothstep(reach - 0.1, reach + fw, rise), low = 1.0 - smoothstep(0.0, reach, rise);
|
|
176
|
+
float drained = (1.0 - smoothstep(front, front + 0.1, rise)) * (1.0 - sheet) * (1.0 - rush) * (1.0 - afar);
|
|
177
|
+
// the foot of the beach is already the wet sand of the bed (SHORE_BED): darkened again here, and again under the still sea,
|
|
178
|
+
// it lay along every shore as a dark outline between the dry sand and the shallows, metres wide on a gentle beach
|
|
179
|
+
float notUnder = smoothstep(-0.15, 0.05, rise);
|
|
180
|
+
diffuseColor.rgb *= 1.0 - wet * mix(0.08, 0.16, low) * notUnder;
|
|
181
|
+
shoreDamp = wet * mix(0.6, 1.0, low);
|
|
182
|
+
// the foam's last reach: a line of its dried lace a hand wide, with dark wrack caught in it, gone to a faint line
|
|
183
|
+
// before it is finer than a pixel
|
|
184
|
+
// (measured across the sand, not up it: on a flat beach the rise hardly changes from pixel to pixel however far off)
|
|
185
|
+
float lineW = 0.012 + fw, atLine = exp(-abs(rise - reach - 0.01) / lineW), sharpLine = (1.0 - smoothstep(0.012, 0.04, fw)) * (1.0 - smoothstep(0.02, 0.06, px));
|
|
186
|
+
float wrack = smoothstep(0.66, 0.82, seaNoise(sq * 6.5 + 3.0)) * smoothstep(0.4, 0.7, seaNoise(sq * 0.5)) * sharpLine; // in flecks, gathered in stretches
|
|
187
|
+
float dried = atLine * mix(0.35, 0.4 + 0.6 * smoothstep(0.1, 0.5, seaLace(sq * 1.7, 0.0, px * 1.7)), sharpLine) * exposed;
|
|
188
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.7, 0.69, 0.64), dried * 0.75);
|
|
189
|
+
diffuseColor.rgb *= 1.0 - atLine * wrack * 0.45 * exposed;
|
|
190
|
+
diffuseColor.rgb *= 1.0 - 0.14 * film * notUnder; // sand under a sheet of water is darker,
|
|
191
|
+
shoreGloss = max(film, drained * 0.7); // and glassy, and so for a moment where it has drained
|
|
192
|
+
gTH *= 1.0 - 0.7 * film - 0.25 * wet; // water smooths the grain over
|
|
193
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.78, 0.8, 0.78), foam);
|
|
194
|
+
diffuseColor.rgb = mix(dry, diffuseColor.rgb, inSwash);
|
|
195
|
+
gTH = mix(flatTH, gTH, inSwash);
|
|
196
|
+
shoreGloss *= inSwash;
|
|
197
|
+
shoreDamp *= inSwash;
|
|
198
|
+
}
|
|
199
|
+
// the bed under the shallows: ripples the waves raise in the sand, running along the shore a hand's breadth high, and the
|
|
200
|
+
// sun's light gathered by the waves into a moving net; each gone before it is finer than a pixel
|
|
201
|
+
if (uShore > 0.5 && rise < 0.05 && rise > -4.5 && vd < 70.0 && SHORE_AT_SEA) {
|
|
202
|
+
vec2 sq = vWP.xz - uLandR.xy, down = length(gWN.xz) > 0.004 ? normalize(gWN.xz) : uWind; // down the bed's slope: out to sea
|
|
203
|
+
float across = dot(sq, down), along = dot(sq, vec2(-down.y, down.x));
|
|
204
|
+
float phase = across * 8.5 + 1.8 * sin(along * 0.9 + across * 0.3) + 0.9 * sin(along * 2.3 - across * 0.5);
|
|
205
|
+
float seenR = (1.0 - smoothstep(0.4, 1.2, fwidth(phase))) * (1.0 - smoothstep(30.0, 70.0, vd)) * smoothstep(0.02, 0.4, -rise);
|
|
206
|
+
float ripple = sin(phase) + 0.3 * sin(2.0 * phase + 1.1);
|
|
207
|
+
gTH += ripple * seenR * 0.2;
|
|
208
|
+
diffuseColor.rgb *= 1.0 + 0.07 * ripple * seenR; // silt settles in the troughs
|
|
209
|
+
vec2 cq = sq * 0.9;
|
|
210
|
+
float lines = abs(sin(cq.x + sin(cq.y * 1.3 + uTime * 0.9)) + sin(cq.y * 1.1 + sin(cq.x * 0.8 - uTime * 0.7)));
|
|
211
|
+
float lines2 = abs(sin(cq.x * 1.7 - cq.y * 0.6 + sin(cq.y * 1.9 - uTime * 1.1)) + sin(cq.y * 1.5 + cq.x * 0.4 + sin(cq.x * 1.4 + uTime * 0.8)));
|
|
212
|
+
float caustic = (1.0 - smoothstep(0.0, 0.3, lines)) + (1.0 - smoothstep(0.0, 0.3, lines2)) * 0.7;
|
|
213
|
+
float seenC = (1.0 - smoothstep(0.05, 0.15, length(fwidth(cq)))) * smoothstep(0.03, 0.3, -rise) * (1.0 - smoothstep(1.5, 4.5, -rise));
|
|
214
|
+
diffuseColor.rgb *= 1.0 + caustic * seenC * 0.55 * (1.0 - uNight);
|
|
215
|
+
}`;
|
|
216
|
+
|
|
217
|
+
// The colour of the sea's body: clear and green-blue over pale sand in the shallows, churned where the waves break, dark
|
|
218
|
+
// where it is deep. Reads waterline, surf, uDeep, uLum.
|
|
219
|
+
export const SEA_BODY = `/* glsl */
|
|
220
|
+
body = mix(uDeep, vec3(0.028, 0.15, 0.142) * uLum, exp(-waterline * 0.38));
|
|
221
|
+
body = mix(body, vec3(0.11, 0.15, 0.14) * uLum, surf * 0.45);`;
|
|
222
|
+
// How much of the bed the sea hides: less than the lake does, so the sand shows through the shallows; and all of it once
|
|
223
|
+
// the water is deep. The bed is drawn in two materials that meet along the cells of the ground at 9 m (the plain bed,
|
|
224
|
+
// the submerged bed), and the depth map reads no deeper than 8 m: where the water still let a tenth of the bed through, that seam
|
|
225
|
+
// showed from the air as stair steps along every coast. So the bed fades out from 6 m and is gone just short of 8 m, where
|
|
226
|
+
// every texel of the map round the seam reads (a harbour's basin, 6 m deep, shows its floor as before).
|
|
227
|
+
export const SEA_OPACITY = `/* glsl */
|
|
228
|
+
opacity = mix(0.06, 0.94, 1.0 - exp(-waterline * 0.3 / max(V.y, 0.3)));
|
|
229
|
+
opacity = mix(opacity, 1.0, smoothstep(6.0, 7.8, waterline));`;
|
|
230
|
+
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
// WebGLPrograms also selects variants by object and geometry, independently of material identity.
|
|
4
|
+
function meshKind(object) {
|
|
5
|
+
const { morphAttributes: morph = {}, attributes = {} } = object.geometry ?? {};
|
|
6
|
+
let key = '';
|
|
7
|
+
if (object.isSkinnedMesh) key += ':skin';
|
|
8
|
+
if (object.isInstancedMesh) {
|
|
9
|
+
key += ':instances';
|
|
10
|
+
if (object.instanceColor) key += ':instanceColor';
|
|
11
|
+
if (object.morphTexture) key += ':instanceMorph';
|
|
12
|
+
}
|
|
13
|
+
if (object.isBatchedMesh) key += object._colorsTexture ? ':batchColor' : ':batch';
|
|
14
|
+
if (object.isPoints) key += attributes.uv ? ':pointUV' : ':points';
|
|
15
|
+
if (object.isLine) key += ':line';
|
|
16
|
+
if (object.isSprite) key += object.center ? ':spriteCenter' : ':sprite';
|
|
17
|
+
if (morph.position?.length || morph.normal?.length || morph.color?.length) {
|
|
18
|
+
key += ':morph:' + (morph.position?.length ?? 0) + ':' + (morph.normal?.length ?? 0) + ':' + (morph.color?.length ?? 0);
|
|
19
|
+
}
|
|
20
|
+
if (attributes.uv1) key += ':uv1';
|
|
21
|
+
if (attributes.uv2) key += ':uv2';
|
|
22
|
+
if (attributes.uv3) key += ':uv3';
|
|
23
|
+
if (attributes.tangent) key += ':tangent';
|
|
24
|
+
if (attributes.color?.itemSize === 4) key += ':rgba';
|
|
25
|
+
return key;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function representatives(root) {
|
|
29
|
+
const picked = new Map();
|
|
30
|
+
root.traverse(object => {
|
|
31
|
+
if (!(object.isMesh || object.isPoints || object.isLine || object.isSprite) || !object.material) return;
|
|
32
|
+
const materials = Array.isArray(object.material) ? object.material.map(m => m.uuid).join(',') : object.material.uuid;
|
|
33
|
+
const key = materials + ':' + meshKind(object);
|
|
34
|
+
if (!picked.has(key)) picked.set(key, object);
|
|
35
|
+
});
|
|
36
|
+
return [...picked.values()];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const shadowSide = [THREE.BackSide, THREE.FrontSide, THREE.DoubleSide];
|
|
40
|
+
|
|
41
|
+
// Compile a group in its lighting environment, including material overrides and offscreen passes.
|
|
42
|
+
// All scratch resources and readiness polling belong to this renderer instance.
|
|
43
|
+
export function createShaderWarmup({renderer, scene, camera, postMaterials = () => [], pollIntervalMs = 10}) {
|
|
44
|
+
const target = new THREE.WebGLRenderTarget(1, 1);
|
|
45
|
+
const quad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), []);
|
|
46
|
+
const twins = new Map();
|
|
47
|
+
const pending = new Set();
|
|
48
|
+
const ready = new WeakSet();
|
|
49
|
+
let timer;
|
|
50
|
+
|
|
51
|
+
function poll() {
|
|
52
|
+
const live = new Set(renderer.info.programs);
|
|
53
|
+
for (const entry of pending) {
|
|
54
|
+
for (const program of entry.programs) {
|
|
55
|
+
if (!live.has(program)) entry.programs.delete(program);
|
|
56
|
+
else if (ready.has(program) || program.isReady()) {
|
|
57
|
+
ready.add(program);
|
|
58
|
+
entry.programs.delete(program);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (entry.programs.size) continue;
|
|
62
|
+
pending.delete(entry);
|
|
63
|
+
entry.resolve();
|
|
64
|
+
}
|
|
65
|
+
timer = pending.size ? setTimeout(poll, pollIntervalMs) : undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function begin(traverse, root, view, lit, yieldIfReady = false) {
|
|
69
|
+
const materials = renderer.compile({
|
|
70
|
+
traverse,
|
|
71
|
+
traverseVisible: visit => { if (lit !== root) root.traverseVisible(visit); }
|
|
72
|
+
}, view, lit);
|
|
73
|
+
const programs = new Set();
|
|
74
|
+
for (const m of materials) {
|
|
75
|
+
const variants = renderer.properties.get(m).programs;
|
|
76
|
+
if (variants) for (const program of variants.values()) {
|
|
77
|
+
if (!ready.has(program)) programs.add(program);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (!programs.size && !yieldIfReady) return Promise.resolve();
|
|
81
|
+
return new Promise(resolve => {
|
|
82
|
+
pending.add({programs, resolve});
|
|
83
|
+
if (timer === undefined) timer = setTimeout(poll, 0);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function withTarget(into, work) {
|
|
88
|
+
const previous = renderer.getRenderTarget();
|
|
89
|
+
const face = renderer.getActiveCubeFace();
|
|
90
|
+
const level = renderer.getActiveMipmapLevel();
|
|
91
|
+
renderer.setRenderTarget(into);
|
|
92
|
+
try { return work(); }
|
|
93
|
+
finally { renderer.setRenderTarget(previous, face, level); }
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function compiled(root, view = camera, lit = root) {
|
|
97
|
+
const objects = representatives(root);
|
|
98
|
+
return begin(visit => objects.forEach(visit), root, view, lit);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function compiledInTurn(root, view = camera, lit = scene, into = null) {
|
|
102
|
+
for (const object of representatives(root)) {
|
|
103
|
+
await withTarget(into, () => begin(visit => visit(object), root, view, lit, true));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function warmStandIns(root, standIn, view, into = target, fog, includeMaterialArrays = false) {
|
|
108
|
+
const picked = new Map();
|
|
109
|
+
root.traverse(object => {
|
|
110
|
+
if (!(object.isMesh || object.isPoints || object.isLine) || !object.material) return;
|
|
111
|
+
if (!includeMaterialArrays && Array.isArray(object.material)) return;
|
|
112
|
+
const m = standIn(object);
|
|
113
|
+
if (!m) return;
|
|
114
|
+
const key = [m.uuid, m.side, !!m.map, !!m.alphaMap, m.alphaTest > 0, meshKind(object)].join(':');
|
|
115
|
+
if (!picked.has(key)) picked.set(key, object);
|
|
116
|
+
});
|
|
117
|
+
return withTarget(into, () => {
|
|
118
|
+
const previousFog = scene.fog;
|
|
119
|
+
scene.fog = fog;
|
|
120
|
+
try {
|
|
121
|
+
// Three gathers the target scene's lights once per compile. Submit the entire override pass
|
|
122
|
+
// together, applying mutable stand-ins only while Three prepares their representative.
|
|
123
|
+
return begin(visit => {
|
|
124
|
+
for (const object of picked.values()) {
|
|
125
|
+
const material = object.material;
|
|
126
|
+
try {
|
|
127
|
+
object.material = standIn(object);
|
|
128
|
+
visit(object);
|
|
129
|
+
} finally { object.material = material; }
|
|
130
|
+
}
|
|
131
|
+
}, scene, view, scene);
|
|
132
|
+
} finally { scene.fog = previousFog; }
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function shadowMaterial(object, point) {
|
|
137
|
+
const material = object.material;
|
|
138
|
+
const side = material.shadowSide ?? (renderer.shadowMap.type === THREE.VSMShadowMap ? material.side : shadowSide[material.side]);
|
|
139
|
+
const key = [point, side, !!material.map, !!material.alphaMap, material.alphaTest > 0].join(':');
|
|
140
|
+
let twin = point ? object.customDistanceMaterial : object.customDepthMaterial;
|
|
141
|
+
if (!twin) {
|
|
142
|
+
twin = twins.get(key);
|
|
143
|
+
if (!twin) {
|
|
144
|
+
twin = point ? new THREE.MeshDistanceMaterial() : new THREE.MeshDepthMaterial(
|
|
145
|
+
Number(THREE.REVISION) <= 170 ? {depthPacking: THREE.RGBADepthPacking} : {}
|
|
146
|
+
);
|
|
147
|
+
twins.set(key, twin);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
twin.side = side;
|
|
151
|
+
twin.map = material.map;
|
|
152
|
+
twin.alphaMap = material.alphaMap;
|
|
153
|
+
twin.alphaTest = material.alphaToCoverage ? 0.5 : material.alphaTest;
|
|
154
|
+
twin.displacementMap = material.displacementMap;
|
|
155
|
+
twin.clippingPlanes = material.clippingPlanes;
|
|
156
|
+
twin.clipIntersection = material.clipIntersection;
|
|
157
|
+
twin.clipShadows = material.clipShadows;
|
|
158
|
+
return twin;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function warmShadowPass(root, {waitForPoint = true} = {}) {
|
|
162
|
+
if (!renderer.shadowMap.enabled) return Promise.resolve();
|
|
163
|
+
const depth = warmStandIns(root, o => o.castShadow && shadowMaterial(o, false), camera, target, null);
|
|
164
|
+
let point;
|
|
165
|
+
scene.traverseVisible(o => { if (o.isPointLight && o.castShadow) point = true; });
|
|
166
|
+
const distance = point && warmStandIns(root, o => o.castShadow && shadowMaterial(o, true), camera, target, null);
|
|
167
|
+
for (const twin of twins.values()) {
|
|
168
|
+
twin.map = twin.alphaMap = twin.displacementMap = twin.clippingPlanes = null;
|
|
169
|
+
}
|
|
170
|
+
return waitForPoint ? Promise.all([depth, distance]) : depth;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function warmPostPasses() {
|
|
174
|
+
const screen = [], offscreen = [];
|
|
175
|
+
for (const [material, into] of postMaterials()) (into === null ? screen : offscreen).push(material);
|
|
176
|
+
// Three prepares each material in a material array against the same render target.
|
|
177
|
+
const compilePass = (materials, into) => {
|
|
178
|
+
if (!materials.length) return;
|
|
179
|
+
quad.material = materials;
|
|
180
|
+
return withTarget(into, () => compiled(quad, camera));
|
|
181
|
+
};
|
|
182
|
+
try { return Promise.all([compilePass(offscreen, target), compilePass(screen, null)]); }
|
|
183
|
+
finally { quad.material = []; }
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function dispose() {
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
timer = undefined;
|
|
189
|
+
for (const entry of pending) entry.resolve();
|
|
190
|
+
pending.clear();
|
|
191
|
+
quad.geometry.dispose();
|
|
192
|
+
target.dispose();
|
|
193
|
+
for (const twin of twins.values()) twin.dispose();
|
|
194
|
+
twins.clear();
|
|
195
|
+
}
|
|
196
|
+
return {compiled, compiledInTurn, compilingNow: () => pending.size > 0, warmStandIns, warmShadowPass, warmPostPasses, dispose};
|
|
197
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Vector3 } from 'three';
|
|
2
|
+
|
|
3
|
+
// The snapped center keeps its original depth and moves by whole texels across the shadow map.
|
|
4
|
+
export function createShadowSnap() {
|
|
5
|
+
const worldUp = new Vector3(0, 1, 0);
|
|
6
|
+
const right = new Vector3();
|
|
7
|
+
const up = new Vector3();
|
|
8
|
+
return (out, x, y, z, direction, texelWidth, texelHeight = texelWidth) => {
|
|
9
|
+
right.crossVectors(worldUp, direction);
|
|
10
|
+
if (right.lengthSq() < 1e-8) right.set(1, 0, 0);
|
|
11
|
+
right.normalize();
|
|
12
|
+
up.crossVectors(direction, right);
|
|
13
|
+
const a = Math.round((x * right.x + y * right.y + z * right.z) / texelWidth) * texelWidth;
|
|
14
|
+
const u = Math.round((x * up.x + y * up.y + z * up.z) / texelHeight) * texelHeight;
|
|
15
|
+
const d = x * direction.x + y * direction.y + z * direction.z;
|
|
16
|
+
return out.set(0, 0, 0).addScaledVector(right, a).addScaledVector(up, u).addScaledVector(direction, d);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function fitOrthographicShadow(camera, extent, near, far) {
|
|
21
|
+
camera.left = -extent;
|
|
22
|
+
camera.right = extent;
|
|
23
|
+
camera.top = extent;
|
|
24
|
+
camera.bottom = -extent;
|
|
25
|
+
camera.near = near;
|
|
26
|
+
camera.far = far;
|
|
27
|
+
camera.updateProjectionMatrix();
|
|
28
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import {isStill, keptAway, placeOf, still, STILL_LAYER} from './still-casters.js';
|
|
3
|
+
|
|
4
|
+
const handlers = new WeakMap();
|
|
5
|
+
let installed = 0;
|
|
6
|
+
let previousBefore, previousAfter;
|
|
7
|
+
|
|
8
|
+
// Delegating to previous hooks also preserves application hooks installed before this extension.
|
|
9
|
+
function before(renderer, object, camera, shadowCamera, geometry, material, group) {
|
|
10
|
+
previousBefore.call(this, renderer, object, camera, shadowCamera, geometry, material, group);
|
|
11
|
+
handlers.get(renderer)?.begin(this, shadowCamera, geometry, material);
|
|
12
|
+
}
|
|
13
|
+
function after(renderer, object, camera, shadowCamera, geometry, material, group) {
|
|
14
|
+
handlers.get(renderer)?.end(this, geometry);
|
|
15
|
+
previousAfter.call(this, renderer, object, camera, shadowCamera, geometry, material, group);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function kindOf(o) {
|
|
19
|
+
const morph = o.geometry.morphAttributes;
|
|
20
|
+
if (morph.position || morph.normal || morph.color) return null;
|
|
21
|
+
if (o.isSkinnedMesh) return 'skinned';
|
|
22
|
+
if (o.isInstancedMesh) return o.instanceColor ? 'tinted' : 'instanced';
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Opt in one renderer. Meshes with their own shadow hooks continue to use them.
|
|
27
|
+
export function installShadowDepth(renderer) {
|
|
28
|
+
if (handlers.has(renderer)) throw new Error('Shadow depth hooks already installed for this renderer');
|
|
29
|
+
const materials = {customDepthMaterial: {}, customDistanceMaterial: {}}, entries = new WeakMap();
|
|
30
|
+
const world = new THREE.Matrix4(), instance = new THREE.Matrix4(), sphere = new THREE.Sphere();
|
|
31
|
+
function noneNear(object, at, reach, entry) {
|
|
32
|
+
const matrix = object.instanceMatrix;
|
|
33
|
+
const geometry = object.geometry;
|
|
34
|
+
if (!geometry.boundingSphere) geometry.computeBoundingSphere();
|
|
35
|
+
const bounds = geometry.boundingSphere;
|
|
36
|
+
if (entry.matrix === matrix && entry.version === matrix.version && entry.count === object.count &&
|
|
37
|
+
entry.reach === reach && entry.x === at.x && entry.y === at.y && entry.z === at.z &&
|
|
38
|
+
entry.bounds.equals(bounds) && entry.world.equals(object.matrixWorld)) return entry.away;
|
|
39
|
+
entry.matrix = matrix;
|
|
40
|
+
entry.version = matrix.version;
|
|
41
|
+
entry.count = object.count;
|
|
42
|
+
entry.reach = reach;
|
|
43
|
+
entry.x = at.x; entry.y = at.y; entry.z = at.z;
|
|
44
|
+
(entry.world ??= new THREE.Matrix4()).copy(object.matrixWorld);
|
|
45
|
+
(entry.bounds ??= new THREE.Sphere()).copy(bounds);
|
|
46
|
+
entry.away = true;
|
|
47
|
+
for (let i = 0; i < object.count; ++i) {
|
|
48
|
+
object.getMatrixAt(i, instance);
|
|
49
|
+
world.multiplyMatrices(object.matrixWorld, instance);
|
|
50
|
+
sphere.copy(bounds).applyMatrix4(world);
|
|
51
|
+
if (sphere.center.distanceToSquared(at) <= (reach + sphere.radius) ** 2) {
|
|
52
|
+
entry.away = false;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return entry.away;
|
|
57
|
+
}
|
|
58
|
+
function begin(object, shadowCamera, geometry, material) {
|
|
59
|
+
const point = material.isMeshDistanceMaterial;
|
|
60
|
+
let entry = entries.get(object);
|
|
61
|
+
if (point && isStill(object)) {
|
|
62
|
+
const at = placeOf(object.matrixWorld.elements);
|
|
63
|
+
if (still.kept && object.keptIn === still.round && object.keptAs !== at) object.layers.disable(STILL_LAYER);
|
|
64
|
+
if (keptAway(object, at)) {
|
|
65
|
+
if (!entry) entries.set(object, entry = {});
|
|
66
|
+
entry.range = geometry.drawRange.count;
|
|
67
|
+
geometry.drawRange.count = -1;
|
|
68
|
+
}
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (!(object.isInstancedMesh || object.isSkinnedMesh)) return;
|
|
72
|
+
if (!entry) entries.set(object, entry = {});
|
|
73
|
+
if (object.isInstancedMesh && point && noneNear(object, shadowCamera.position, shadowCamera.far, entry)) {
|
|
74
|
+
entry.countAside = object.count;
|
|
75
|
+
object.count = 0;
|
|
76
|
+
}
|
|
77
|
+
const field = point ? 'customDistanceMaterial' : 'customDepthMaterial';
|
|
78
|
+
if (object[field] !== undefined && object[field] !== entry[field]) return;
|
|
79
|
+
const skin = object.material;
|
|
80
|
+
const kind = kindOf(object);
|
|
81
|
+
if (!kind || Array.isArray(skin) || skin.alphaTest > 0 || skin.alphaToCoverage || skin.displacementMap || skin.clippingPlanes?.length) {
|
|
82
|
+
if (entry[field] && object[field] === entry[field]) object[field] = undefined;
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
let shared = materials[field][kind];
|
|
86
|
+
if (!shared) {
|
|
87
|
+
shared = point ? new THREE.MeshDistanceMaterial() : new THREE.MeshDepthMaterial(
|
|
88
|
+
Number(THREE.REVISION) <= 170 ? {depthPacking: THREE.RGBADepthPacking} : {}
|
|
89
|
+
);
|
|
90
|
+
materials[field][kind] = shared;
|
|
91
|
+
}
|
|
92
|
+
object[field] = entry[field] = shared;
|
|
93
|
+
}
|
|
94
|
+
function end(object, geometry) {
|
|
95
|
+
const entry = entries.get(object);
|
|
96
|
+
if (!entry) return;
|
|
97
|
+
if (entry.range !== undefined) {
|
|
98
|
+
geometry.drawRange.count = entry.range;
|
|
99
|
+
entry.range = undefined;
|
|
100
|
+
}
|
|
101
|
+
if (entry.countAside !== undefined) {
|
|
102
|
+
object.count = entry.countAside;
|
|
103
|
+
entry.countAside = undefined;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (installed++ === 0) {
|
|
107
|
+
previousBefore = THREE.Object3D.prototype.onBeforeShadow;
|
|
108
|
+
previousAfter = THREE.Object3D.prototype.onAfterShadow;
|
|
109
|
+
THREE.Object3D.prototype.onBeforeShadow = before;
|
|
110
|
+
THREE.Object3D.prototype.onAfterShadow = after;
|
|
111
|
+
}
|
|
112
|
+
handlers.set(renderer, {begin, end});
|
|
113
|
+
function dispose(root) {
|
|
114
|
+
if (!handlers.delete(renderer)) return;
|
|
115
|
+
root?.traverse(o => {
|
|
116
|
+
const entry = entries.get(o);
|
|
117
|
+
if (!entry) return;
|
|
118
|
+
for (const field of ['customDepthMaterial','customDistanceMaterial']) {
|
|
119
|
+
if (entry[field] && o[field] === entry[field]) o[field] = undefined;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
for (const group of Object.values(materials)) for (const material of Object.values(group)) material.dispose();
|
|
123
|
+
if (--installed === 0) {
|
|
124
|
+
if (THREE.Object3D.prototype.onBeforeShadow === before) THREE.Object3D.prototype.onBeforeShadow = previousBefore;
|
|
125
|
+
if (THREE.Object3D.prototype.onAfterShadow === after) THREE.Object3D.prototype.onAfterShadow = previousAfter;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return {dispose};
|
|
129
|
+
}
|