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,119 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import {still, STILL_LAYER} from './still-casters.js';
|
|
3
|
+
|
|
4
|
+
// Cache the static layer of one point shadow. Mark its casters and install the matching
|
|
5
|
+
// shadow depth hooks (custom batch casters may implement keptAway in their own hooks).
|
|
6
|
+
export function createStaticPointShadow(renderer) {
|
|
7
|
+
const shadowMap = renderer.shadowMap;
|
|
8
|
+
const renderShadows = shadowMap.render;
|
|
9
|
+
const only = [];
|
|
10
|
+
const view = new THREE.Camera();
|
|
11
|
+
view.layers.set(STILL_LAYER);
|
|
12
|
+
const origin = new THREE.Vector3(NaN, NaN, NaN), current = new THREE.Vector3();
|
|
13
|
+
let light, map, picture, cachedSum, cachedNear, cachedFar, cachedType, stale = true;
|
|
14
|
+
let previousClear;
|
|
15
|
+
|
|
16
|
+
function framebuffer(target, face) {
|
|
17
|
+
const fb = renderer.properties.get(target).__webglFramebuffer;
|
|
18
|
+
return target.isWebGLCubeRenderTarget ? fb[face] : fb;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function copyFace(source, destination, face) {
|
|
22
|
+
const gl = renderer.getContext();
|
|
23
|
+
const {width, height} = destination;
|
|
24
|
+
gl.bindFramebuffer(gl.READ_FRAMEBUFFER, framebuffer(source, face));
|
|
25
|
+
gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, framebuffer(destination, face));
|
|
26
|
+
gl.blitFramebuffer(0, 0, width, height, 0, 0, width, height, gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT, gl.NEAREST);
|
|
27
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer(map, renderer.getActiveCubeFace()));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function clear(color, depth, stencil) {
|
|
31
|
+
if (renderer.getRenderTarget() === map) copyFace(picture, map, renderer.getActiveCubeFace());
|
|
32
|
+
else previousClear.call(renderer, color, depth, stencil);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function capture(scene) {
|
|
36
|
+
const shadow = light.shadow;
|
|
37
|
+
still.round++;
|
|
38
|
+
still.sum = 0;
|
|
39
|
+
renderShadows.call(shadowMap, only, scene, view);
|
|
40
|
+
const next = shadow.map;
|
|
41
|
+
if (!next) return false;
|
|
42
|
+
if (next !== map || next.width !== picture?.width || next.height !== picture?.height) {
|
|
43
|
+
picture?.dispose();
|
|
44
|
+
if (next.isWebGLCubeRenderTarget) {
|
|
45
|
+
picture = new THREE.WebGLCubeRenderTarget(next.width);
|
|
46
|
+
const depth = next.depthTexture;
|
|
47
|
+
picture.depthTexture = new depth.constructor(next.width, depth.type);
|
|
48
|
+
picture.depthTexture.format = depth.format;
|
|
49
|
+
picture.depthTexture.compareFunction = depth.compareFunction;
|
|
50
|
+
picture.depthTexture.minFilter = depth.minFilter;
|
|
51
|
+
picture.depthTexture.magFilter = depth.magFilter;
|
|
52
|
+
} else picture = new THREE.WebGLRenderTarget(next.width, next.height);
|
|
53
|
+
renderer.initRenderTarget(picture);
|
|
54
|
+
}
|
|
55
|
+
map = next;
|
|
56
|
+
const target = renderer.getRenderTarget(), face = renderer.getActiveCubeFace(), level = renderer.getActiveMipmapLevel();
|
|
57
|
+
try {
|
|
58
|
+
renderer.setRenderTarget(map);
|
|
59
|
+
for (let f = 0; f < (map.isWebGLCubeRenderTarget ? 6 : 1); f++) copyFace(map, picture, f);
|
|
60
|
+
} finally { renderer.setRenderTarget(target, face, level); }
|
|
61
|
+
origin.copy(current);
|
|
62
|
+
cachedNear = shadow.camera.near;
|
|
63
|
+
cachedFar = shadow.camera.far;
|
|
64
|
+
cachedType = shadowMap.type;
|
|
65
|
+
cachedSum = still.sum;
|
|
66
|
+
stale = false;
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function shadows(lights, scene, camera) {
|
|
71
|
+
const shadow = light.shadow;
|
|
72
|
+
const requested = this.needsUpdate;
|
|
73
|
+
let usable = this.enabled && (this.autoUpdate || requested) && (shadow.autoUpdate || shadow.needsUpdate) &&
|
|
74
|
+
lights.includes(light) && this.type !== THREE.VSMShadowMap && !(shadow.camera.layers.mask & ~1);
|
|
75
|
+
// The membership counters belong to a single light. Full draws remain correct for other point lights.
|
|
76
|
+
if (usable) for (const other of lights) {
|
|
77
|
+
if (other !== light && other.isPointLight) {
|
|
78
|
+
stale = true;
|
|
79
|
+
usable = false;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!usable) { renderShadows.call(this, lights, scene, camera); return; }
|
|
84
|
+
current.setFromMatrixPosition(light.matrixWorld);
|
|
85
|
+
if (stale || map !== shadow.map || !origin.equals(current) || cachedNear !== shadow.camera.near ||
|
|
86
|
+
cachedFar !== (light.distance || shadow.camera.far) || cachedType !== this.type ||
|
|
87
|
+
(map.isWebGLCubeRenderTarget && map.width !== shadow.mapSize.x)) {
|
|
88
|
+
const captured = capture(scene);
|
|
89
|
+
shadow.needsUpdate = true;
|
|
90
|
+
this.needsUpdate = requested;
|
|
91
|
+
if (!captured) { renderShadows.call(this, lights, scene, camera); return; }
|
|
92
|
+
}
|
|
93
|
+
previousClear = renderer.clear;
|
|
94
|
+
still.kept = true;
|
|
95
|
+
still.sum = 0;
|
|
96
|
+
renderer.clear = clear;
|
|
97
|
+
try {
|
|
98
|
+
renderShadows.call(this, lights, scene, camera);
|
|
99
|
+
stale = still.sum !== cachedSum;
|
|
100
|
+
} finally {
|
|
101
|
+
renderer.clear = previousClear;
|
|
102
|
+
still.kept = false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function keep(lightToCache) {
|
|
107
|
+
if (!lightToCache?.isPointLight) throw new TypeError('Expected a point light');
|
|
108
|
+
light = only[0] = lightToCache;
|
|
109
|
+
stale = true;
|
|
110
|
+
shadowMap.render = shadows;
|
|
111
|
+
}
|
|
112
|
+
function dispose() {
|
|
113
|
+
if (shadowMap.render === shadows) shadowMap.render = renderShadows;
|
|
114
|
+
picture?.dispose();
|
|
115
|
+
picture = map = light = null;
|
|
116
|
+
only.length = 0;
|
|
117
|
+
}
|
|
118
|
+
return {keep, stale: () => stale, dispose};
|
|
119
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { MeshStandardMaterial, MeshDepthMaterial, RGBADepthPacking, Color } from 'three';
|
|
2
|
+
|
|
3
|
+
import { terrainReliefGLSL } from './terrain-rock.js';
|
|
4
|
+
|
|
5
|
+
const CLIFF_FRAG_HEAD = `/* glsl */
|
|
6
|
+
uniform vec3 uDark, uLight, uIron, uShale, uDust, uLichen, uCrust, uSnow;
|
|
7
|
+
uniform sampler2D uRockTex; uniform float uSnowline;
|
|
8
|
+
varying vec3 vWP, vWN; varying vec4 vRock; varying float vYY; float gH = 0.5; float gSnow = 0.0;
|
|
9
|
+
vec3 rockTri(vec3 p, vec3 bw, float s, float o) { return texture2D(uRockTex, p.zy * s + o).rgb * bw.x + texture2D(uRockTex, p.xz * s + o).rgb * bw.y + texture2D(uRockTex, p.xy * s + o).rgb * bw.z; }`;
|
|
10
|
+
const CLIFF_FRAG = `/* glsl */
|
|
11
|
+
#include <color_fragment>
|
|
12
|
+
{
|
|
13
|
+
vec3 wn = normalize(vWN), bw = abs(wn); bw *= bw; bw *= bw; bw /= bw.x + bw.y + bw.z;
|
|
14
|
+
float vd = length(vViewPosition), nearK = 1.0 - smoothstep(10.0, 55.0, vd), midK = 1.0 - smoothstep(60.0, 320.0, vd);
|
|
15
|
+
float px = length(fwidth(vWP)); // metres to a pixel
|
|
16
|
+
// each scale's relief is let go before its grain is finer than a pixel, or its slope taken pixel to pixel lies as a stipple
|
|
17
|
+
float k1 = 1.0 - smoothstep(0.003, 0.009, px), k2 = 1.0 - smoothstep(0.017, 0.05, px), k3 = 1.0 - smoothstep(0.1, 0.3, px);
|
|
18
|
+
vec3 f3 = rockTri(vWP, bw, 0.041, 0.57), f2 = vec3(0.5, 0.0, f3.b), f1 = vec3(0.5, 0.0, 0.5); // the finer scales only where they show
|
|
19
|
+
if (midK > 0.0) f2 = mix(f2, rockTri(vWP, bw, 0.23, 0.31), midK);
|
|
20
|
+
if (nearK > 0.0) f1 = rockTri(vWP, bw, 1.37, 0.0);
|
|
21
|
+
float t = vRock.x, hard = vRock.y, cav = vRock.z, m = vRock.w;
|
|
22
|
+
vec3 rock = mix(uDark, uLight, t); // grey-brown to pale buff, bed by bed
|
|
23
|
+
rock = mix(rock, uIron, smoothstep(0.8, 0.93, t) * 0.75); // a few beds stained with iron
|
|
24
|
+
rock = mix(rock, uShale, smoothstep(0.16, 0.04, t) * 0.7); // and a few dark ones
|
|
25
|
+
float lam = sin(vYY * 7.3 + f2.r * 4.0) * 0.5 + 0.5;
|
|
26
|
+
rock *= 1.0 - 0.1 * (1.0 - hard) * lam * midK * (1.0 - smoothstep(0.08, 0.2, fwidth(vYY))); // soft beds split into thin laminae
|
|
27
|
+
float gl = max(dot(vColor.rgb, vec3(0.3, 0.55, 0.15)), 0.02);
|
|
28
|
+
rock *= mix(vec3(1.0), clamp(vColor.rgb / gl, 0.7, 1.4), 0.35); // the hue of the ground round it
|
|
29
|
+
rock *= (0.68 + 0.64 * mix(0.5, f1.r, nearK)) * (0.78 + 0.44 * f2.r) * (0.82 + 0.36 * f3.r);
|
|
30
|
+
rock *= 1.0 - 0.16 * f2.g * nearK - 0.16 * f1.g * nearK; // cracks, seen from near
|
|
31
|
+
// Weathering follows the rock grain in fixed world space, including on a curving face.
|
|
32
|
+
float streak = mix(f3.b, f2.b, 0.35);
|
|
33
|
+
float top = smoothstep(0.52, 0.86, wn.y);
|
|
34
|
+
rock *= mix(0.8 + 0.4 * smoothstep(0.3, 0.7, streak), 1.0, smoothstep(0.25, 0.55, wn.y));
|
|
35
|
+
rock *= 1.0 - 0.55 * cav;
|
|
36
|
+
vec3 dust = mix(uDust, vColor.rgb, 0.3) * (0.75 + 0.5 * f2.r);
|
|
37
|
+
rock = mix(rock, dust, top * (0.35 + 0.35 * f3.g) * (1.0 - 0.4 * hard)); // dust and grit on the ledges
|
|
38
|
+
float lich = smoothstep(0.58, 0.78, f3.b * 0.55 + f2.b * 0.45 + 0.1 * top) * (0.35 + 0.65 * top);
|
|
39
|
+
rock = mix(rock, uLichen * (0.6 + 0.8 * f1.r), lich * 0.42); // grey-green lichen, thickest on the tops
|
|
40
|
+
rock = mix(rock, uCrust * (0.7 + 0.5 * f1.r), smoothstep(0.83, 0.92, f2.b) * smoothstep(0.4, 0.7, f3.b) * 0.5 * nearK); // and a few orange crusts
|
|
41
|
+
float foot = 1.0 - smoothstep(0.12, 0.5, m);
|
|
42
|
+
rock = mix(rock, vColor.rgb * (0.85 + 0.45 * f2.r) * 1.35, foot * 0.8); // where it rises out of the ground
|
|
43
|
+
float snowline = uSnowline + (f3.g - 0.5) * 60.0;
|
|
44
|
+
gSnow = smoothstep(snowline, snowline + 30.0, vWP.y) * smoothstep(0.66, 0.86, wn.y + (f2.r - 0.5) * 0.25) * 0.9;
|
|
45
|
+
rock = mix(rock, uSnow * (0.92 + 0.1 * f1.r), gSnow); // snow lies on the high ledges
|
|
46
|
+
rock *= rockShade(vWP.xz); // the clouds' shadows, as on the ground below
|
|
47
|
+
diffuseColor.rgb = rock;
|
|
48
|
+
gH = ((f1.r - 0.5) - f1.g * 1.2) * 0.35 * nearK * k1 + ((f2.r - 0.5) - f2.g * nearK) * 0.9 * midK * k2 + (f3.r - 0.5) * 0.6 * k3 - cav * 0.8;
|
|
49
|
+
gH *= 1.0 - gSnow * 0.8;
|
|
50
|
+
}`;
|
|
51
|
+
const CLIFF_BUMP = `/* glsl */
|
|
52
|
+
#include <normal_fragment_maps>
|
|
53
|
+
normal = terrainReliefNormal(vViewPosition, normal, gH, 2.2, vec2(40.0, 260.0));`;
|
|
54
|
+
// Positions and normals are expressed in the same terrain frame as the sample coordinates.
|
|
55
|
+
const STATIC_VERTEX = `attribute vec4 aRock; attribute float aYY;
|
|
56
|
+
vec3 cP, cN; float cHide;
|
|
57
|
+
void cliffMorph() { cP = position; cN = normal; cHide = 0.0; }`;
|
|
58
|
+
const FULL_LIGHT = 'float rockShade(vec2 xz) { return 1.0; }';
|
|
59
|
+
|
|
60
|
+
export function createStrataMaterial({ texture, palette, snowline = 1e7, shading = FULL_LIGHT, vertex = STATIC_VERTEX, uniforms = {} }) {
|
|
61
|
+
const head = vertex + '\nvarying vec3 vWP, vWN; varying vec4 vRock; varying float vYY;';
|
|
62
|
+
const m = new MeshStandardMaterial({ vertexColors: true, roughness: 0.93 });
|
|
63
|
+
const colors = {};
|
|
64
|
+
for (const key of ['dark', 'light', 'iron', 'shale', 'dust', 'lichen', 'crust', 'snow']) {
|
|
65
|
+
const rgb = palette[key];
|
|
66
|
+
if (!rgb || rgb.length !== 3 || !rgb.every(Number.isFinite)) throw new TypeError('Expected a linear RGB triple for ' + key);
|
|
67
|
+
colors['u' + key[0].toUpperCase() + key.slice(1)] = { value: new Color(...rgb) };
|
|
68
|
+
}
|
|
69
|
+
m.onBeforeCompile = sh => {
|
|
70
|
+
Object.assign(sh.uniforms, uniforms, colors, { uRockTex: { value: texture }, uSnowline: { value: snowline } });
|
|
71
|
+
sh.vertexShader = sh.vertexShader
|
|
72
|
+
.replace('#include <common>', '#include <common>\n' + head)
|
|
73
|
+
.replace('#include <beginnormal_vertex>', 'cliffMorph(); vec3 objectNormal = cN;')
|
|
74
|
+
.replace('#include <begin_vertex>', 'vec3 transformed = cP; vWP = cP; vWN = cN; vRock = aRock; vYY = aYY;')
|
|
75
|
+
.replace('#include <project_vertex>', '#include <project_vertex>\nif (cHide > 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);');
|
|
76
|
+
sh.fragmentShader = sh.fragmentShader
|
|
77
|
+
.replace('#include <common>', '#include <common>\n' + shading + '\n' + CLIFF_FRAG_HEAD + '\n' + terrainReliefGLSL)
|
|
78
|
+
.replace('#include <color_fragment>', CLIFF_FRAG)
|
|
79
|
+
.replace('#include <roughnessmap_fragment>', '#include <roughnessmap_fragment>\nroughnessFactor = mix(roughnessFactor, 0.62, gSnow);')
|
|
80
|
+
.replace('#include <normal_fragment_maps>', CLIFF_BUMP);
|
|
81
|
+
};
|
|
82
|
+
m.customProgramCacheKey = () => 'strata:' + vertex + ':' + shading;
|
|
83
|
+
const depth = new MeshDepthMaterial({ depthPacking: RGBADepthPacking });
|
|
84
|
+
depth.onBeforeCompile = sh => {
|
|
85
|
+
Object.assign(sh.uniforms, uniforms);
|
|
86
|
+
sh.vertexShader = sh.vertexShader
|
|
87
|
+
.replace('#include <common>', '#include <common>\n' + vertex)
|
|
88
|
+
.replace('#include <begin_vertex>', 'cliffMorph(); vec3 transformed = cP;')
|
|
89
|
+
.replace('#include <project_vertex>', '#include <project_vertex>\nif (cHide > 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);');
|
|
90
|
+
};
|
|
91
|
+
depth.customProgramCacheKey = () => 'strata-depth:' + vertex;
|
|
92
|
+
return { material: m, depth };
|
|
93
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Flow-local coordinates are in metres. Skew and warp the ripples so the crests
|
|
2
|
+
// stay in short, staggered patches as they travel downstream.
|
|
3
|
+
export const STREAM_FLOW_GLSL = /* glsl */ `
|
|
4
|
+
float streamWaveDetail(float footprint, float frequency) {
|
|
5
|
+
return 1.0 - smoothstep(1.05, 2.8, footprint * frequency);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
vec4 streamFlow(vec3 flow, float acrossMetres, float depth, float travel, float distanceToEye, float footprint) {
|
|
9
|
+
vec2 q = vec2(acrossMetres, flow.y - travel);
|
|
10
|
+
footprint = max(footprint, max(length(dFdx(q)), length(dFdy(q))));
|
|
11
|
+
vec2 broad = vec2(0.78, 0.24), middle = vec2(-1.35, 0.43), fine = vec2(2.4, 1.22);
|
|
12
|
+
vec2 broadWarp = vec2(0.33, -0.22), middleWarp = vec2(0.38, 0.28), fineWarp = vec2(-0.58, 0.63);
|
|
13
|
+
float a = dot(q, broadWarp), b = dot(q, middleWarp), c = dot(q, fineWarp);
|
|
14
|
+
float broadPhase = dot(q, broad) + 1.55 * sin(a);
|
|
15
|
+
float middlePhase = dot(q, middle) + 1.35 * cos(b);
|
|
16
|
+
float finePhase = dot(q, fine) + 0.9 * sin(c);
|
|
17
|
+
vec2 broadRate = broad + 1.55 * cos(a) * broadWarp;
|
|
18
|
+
vec2 middleRate = middle - 1.35 * sin(b) * middleWarp;
|
|
19
|
+
vec2 fineRate = fine + 0.9 * cos(c) * fineWarp;
|
|
20
|
+
float broadDetail = streamWaveDetail(footprint, length(broadRate));
|
|
21
|
+
float middleDetail = streamWaveDetail(footprint, length(middleRate));
|
|
22
|
+
float fineDetail = streamWaveDetail(footprint, length(fineRate));
|
|
23
|
+
float packet = 0.5 + 0.5 * sin(dot(q, vec2(0.23, -0.19)) + 0.9 * sin(b));
|
|
24
|
+
vec2 slope = 0.085 * broadRate * cos(broadPhase) * broadDetail * (0.38 + 0.62 * packet)
|
|
25
|
+
+ 0.048 * middleRate * cos(middlePhase) * middleDetail * (1.0 - 0.55 * packet)
|
|
26
|
+
+ 0.016 * fineRate * cos(finePhase) * fineDetail;
|
|
27
|
+
float fleck = smoothstep(0.66, 0.98, sin(middlePhase)) * smoothstep(0.54, 0.95, packet)
|
|
28
|
+
* (0.08 + 0.32 * flow.z) * middleDetail;
|
|
29
|
+
float broken = smoothstep(0.32, 0.84, 0.55 * packet + 0.45 * (0.5 + 0.5 * sin(broadPhase)));
|
|
30
|
+
float even = max(smoothstep(45.0, 220.0, distanceToEye), 1.0 - broadDetail);
|
|
31
|
+
float rapid = smoothstep(0.28, 0.8, flow.z) * mix(broken, 0.38, even)
|
|
32
|
+
* max(1.0 - 0.5 * flow.x * flow.x, 0.0) * smoothstep(0.3, 1.6, depth);
|
|
33
|
+
float bank = 1.0 - 0.65 * smoothstep(0.62, 1.0, abs(flow.x));
|
|
34
|
+
return vec4(slope * bank, fleck * bank, rapid);
|
|
35
|
+
}
|
|
36
|
+
`;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const wetSurface = ({ dampness, darkening, roughness, sheen }) => `/* glsl */
|
|
2
|
+
if (${dampness} > 0.0) { float up = clamp(dot(normal, normalize((viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz)), 0.0, 1.0); diffuseColor.rgb *= 1.0 - ${dampness} * ${darkening}; roughnessFactor = mix(roughnessFactor, ${roughness}, ${dampness} * up * ${sheen}); }`;
|
|
3
|
+
|
|
4
|
+
// Backlighting from the first hemisphere and directional light, with optional forward scattering.
|
|
5
|
+
export const thinSurfaceLight = ({ sky, sun, glow = 0, where = '1.0', sunlight = '1.0' }) => `/* glsl */
|
|
6
|
+
vec3 through = vec3(0.0);
|
|
7
|
+
#if NUM_HEMI_LIGHTS > 0
|
|
8
|
+
through += hemisphereLights[0].skyColor * max(-dot(normal, hemisphereLights[0].direction), 0.0) * ${sky.toFixed(2)};
|
|
9
|
+
#endif
|
|
10
|
+
#if NUM_DIR_LIGHTS > 0
|
|
11
|
+
through += directionalLights[0].color * max(-dot(normal, directionalLights[0].direction), 0.0) * ${sun.toFixed(2)};
|
|
12
|
+
#endif
|
|
13
|
+
outgoingLight += diffuseColor.rgb * through;${glow ? sunGlow(glow, where, sunlight) : ''}
|
|
14
|
+
#include <opaque_fragment>`;
|
|
15
|
+
const sunGlow = (glow, where, sunlight) => `
|
|
16
|
+
#if NUM_DIR_LIGHTS > 0
|
|
17
|
+
{
|
|
18
|
+
vec3 toEye = normalize(vViewPosition), hue = diffuseColor.rgb / max(max(diffuseColor.r, diffuseColor.g), 0.04);
|
|
19
|
+
float against = max(-dot(toEye, directionalLights[0].direction), 0.0), edge = 1.0 - abs(dot(normal, toEye));
|
|
20
|
+
outgoingLight += hue * directionalLights[0].color * (0.3 * pow(against, 4.0) + pow(against, 20.0)) * edge * edge * edge * ${sunlight} * ${glow.toFixed(2)} * (${where});
|
|
21
|
+
}
|
|
22
|
+
#endif`;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { MeshStandardMaterial } from 'three';
|
|
2
|
+
|
|
3
|
+
export const projectedVaryings = 'varying vec3 vL; varying vec3 vLN; varying vec3 vHalf; varying float vSeed; varying vec2 vCW;';
|
|
4
|
+
|
|
5
|
+
// Project unit-instance geometry in local metres and track world XZ after deformation.
|
|
6
|
+
function projectSurfaceVertex(shader, { declarations = '', deformInstance = '', assign = '' } = {}) {
|
|
7
|
+
shader.vertexShader = shader.vertexShader.replace('#include <common>', '#include <common>\n' + projectedVaryings + declarations).replace(
|
|
8
|
+
'#include <begin_vertex>',
|
|
9
|
+
`/* glsl */
|
|
10
|
+
#include <begin_vertex>
|
|
11
|
+
#ifdef USE_INSTANCING
|
|
12
|
+
vec3 iS = vec3(length(instanceMatrix[0].xyz), length(instanceMatrix[1].xyz), length(instanceMatrix[2].xyz));
|
|
13
|
+
vL = position * iS; vHalf = iS * 0.5;
|
|
14
|
+
vSeed = fract(instanceMatrix[3].x * 0.137 + instanceMatrix[3].z * 0.291) * 8.0;
|
|
15
|
+
${deformInstance}
|
|
16
|
+
#else
|
|
17
|
+
vL = position; vHalf = vec3(1000.0); vSeed = 0.0;
|
|
18
|
+
#endif
|
|
19
|
+
vLN = normal;${assign}`,
|
|
20
|
+
);
|
|
21
|
+
shader.vertexShader = shader.vertexShader.replace(
|
|
22
|
+
'#include <project_vertex>',
|
|
23
|
+
'#include <project_vertex>\n{ vec4 cw = vec4(transformed, 1.0);\n#ifdef USE_INSTANCING\ncw = instanceMatrix * cw;\n#endif\nvCW = (modelMatrix * cw).xz; }',
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Multiplies the standard material colour and sets the height used for relief.
|
|
28
|
+
export const triplanarColor = ({ texture, scale, gain, height }) => `/* glsl */
|
|
29
|
+
vec3 bw = abs(vLN); bw *= bw; bw /= (bw.x + bw.y + bw.z);
|
|
30
|
+
vec3 tx = texture2D(${texture}, vL.zy * ${scale}).rgb * bw.x + texture2D(${texture}, vL.xz * ${scale}).rgb * bw.y + texture2D(${texture}, vL.xy * ${scale}).rgb * bw.z;
|
|
31
|
+
${height} = dot(tx, vec3(0.333));
|
|
32
|
+
diffuseColor.rgb *= tx * ${gain};`;
|
|
33
|
+
|
|
34
|
+
export const surfaceRelief = ({ height, strength, fadeStart, fadeEnd }) => `/* glsl */
|
|
35
|
+
{ // relief from the procedural height ${height}, fading with distance
|
|
36
|
+
float bs = ${strength} * (1.0 - smoothstep(${fadeStart}, ${fadeEnd}, length(vViewPosition)));
|
|
37
|
+
if (bs > 0.01) {
|
|
38
|
+
vec3 sgx = dFdx(-vViewPosition), sgy = dFdy(-vViewPosition); float lx = length(sgx), ly = length(sgy);
|
|
39
|
+
if (lx > 1e-7 && ly > 1e-7) {
|
|
40
|
+
sgx /= lx; sgy /= ly;
|
|
41
|
+
vec3 r1 = cross(sgy, normal), r2 = cross(normal, sgx); float det = dot(sgx, r1);
|
|
42
|
+
vec3 nb = abs(det) * normal - bs * sign(det) * (dFdx(${height}) * r1 + dFdy(${height}) * r2); float l2 = dot(nb, nb);
|
|
43
|
+
if (l2 > 1e-8) normal = nb * inversesqrt(l2); // never normalise a zero vector: one bad pixel becomes a black block after bloom
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}`;
|
|
47
|
+
|
|
48
|
+
// A standard material with metre-scale projection. Uniform wrappers are shared across compilations.
|
|
49
|
+
export function createProjectedMaterial(
|
|
50
|
+
parameters,
|
|
51
|
+
{ cacheKey, uniforms, vertex, declarations = projectedVaryings, color, normal, finish },
|
|
52
|
+
) {
|
|
53
|
+
if (typeof cacheKey !== 'string' || !cacheKey) throw new TypeError('A projected material needs a shader variant key');
|
|
54
|
+
const material = new MeshStandardMaterial(parameters);
|
|
55
|
+
material.customProgramCacheKey = () => cacheKey;
|
|
56
|
+
material.onBeforeCompile = (shader, renderer) => {
|
|
57
|
+
Object.assign(shader.uniforms, uniforms);
|
|
58
|
+
projectSurfaceVertex(shader, vertex);
|
|
59
|
+
shader.fragmentShader = shader.fragmentShader.replace('#include <common>', '#include <common>\n' + declarations);
|
|
60
|
+
if (color) shader.fragmentShader = shader.fragmentShader.replace('#include <map_fragment>', color);
|
|
61
|
+
if (normal)
|
|
62
|
+
shader.fragmentShader = shader.fragmentShader.replace(
|
|
63
|
+
'#include <normal_fragment_maps>',
|
|
64
|
+
'#include <normal_fragment_maps>\n' + normal,
|
|
65
|
+
);
|
|
66
|
+
finish?.(shader, renderer);
|
|
67
|
+
};
|
|
68
|
+
return material;
|
|
69
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Fragment shading for angular debris on a heightfield apron. Use a world position in
|
|
2
|
+
// metres and its derivatives, and the mean spacing of the stones. X is signed facet brightness, Y relief.
|
|
3
|
+
export const talusGLSL = /* glsl */ `
|
|
4
|
+
vec2 talusFragment(vec2 metres, vec2 dx, vec2 dy, float spacing, float seed) {
|
|
5
|
+
mat2 basis = mat2(0.868, -0.497, 0.497, 0.868) / spacing;
|
|
6
|
+
vec2 q = basis * metres;
|
|
7
|
+
vec2 footprint = abs(basis * dx) + abs(basis * dy);
|
|
8
|
+
float resolved = 1.0 - smoothstep(0.12, 0.45, max(footprint.x, footprint.y));
|
|
9
|
+
q.x += 0.5 * mod(floor(q.y), 2.0);
|
|
10
|
+
vec2 cell = floor(q), p = fract(q) - 0.5;
|
|
11
|
+
vec3 h = fract(vec3(cell.xyx) * vec3(0.1031, 0.11369, 0.13787) + seed);
|
|
12
|
+
h += dot(h, h.yzx + 19.19);
|
|
13
|
+
h = fract((h.xxy + h.yzz) * h.zyx);
|
|
14
|
+
p -= (h.xy - 0.5) * 0.14;
|
|
15
|
+
float angle = h.z * 6.28318;
|
|
16
|
+
p = mat2(cos(angle), -sin(angle), sin(angle), cos(angle)) * p;
|
|
17
|
+
float halfWidth = mix(0.13, 0.32, h.y);
|
|
18
|
+
vec2 shape = p / vec2(halfWidth, halfWidth * mix(0.5, 0.9, h.x));
|
|
19
|
+
// Two chipped corners and a slanted top distinguish a fragment from a round pebble.
|
|
20
|
+
float edge = max(max(abs(shape.x), abs(shape.y)), max(dot(shape, vec2(0.77, 0.88)), dot(shape, vec2(-0.74, -0.80))));
|
|
21
|
+
float aa = min(0.8, max(0.025, max(footprint.x, footprint.y) / halfWidth));
|
|
22
|
+
float inside = (1.0 - smoothstep(1.0 - aa, 1.0 + aa, edge)) * resolved;
|
|
23
|
+
float crest = max(0.0, 1.0 - edge);
|
|
24
|
+
float facet = mix(-0.32, 0.36, smoothstep(-aa, aa, shape.x * 0.8 + shape.y * 0.2 + h.z - 0.5));
|
|
25
|
+
return inside * vec2(facet + (h.x - 0.5) * 0.34, 0.28 + 0.72 * crest);
|
|
26
|
+
}`;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// World-space rock for a heightfield. The horizontal components of the surface normal
|
|
2
|
+
// weight fixed vertical projections; changing the normal never changes the coordinates.
|
|
3
|
+
export const terrainRockGLSL = /* glsl */ `
|
|
4
|
+
vec3 rockFaceSample(sampler2D grain, vec3 p, float xWeight) {
|
|
5
|
+
return mix(texture2D(grain, p.xy).rgb, texture2D(grain, p.zy + vec2(0.37, 0.19)).rgb, xWeight);
|
|
6
|
+
}
|
|
7
|
+
float rockBed(float height, float frequency, float phase, float footprint) {
|
|
8
|
+
return 0.5 + 0.5 * sin(height * frequency + phase) *
|
|
9
|
+
(1.0 - smoothstep(0.2, 0.5, footprint * frequency));
|
|
10
|
+
}
|
|
11
|
+
vec4 terrainRock(sampler2D grain, vec3 p, vec3 normal, vec3 darkBed, vec3 lightBed, vec3 iron, vec3 lime) {
|
|
12
|
+
vec2 weights = normal.xz * normal.xz;
|
|
13
|
+
weights *= weights;
|
|
14
|
+
float across = weights.x / max(weights.x + weights.y, 0.00001);
|
|
15
|
+
vec3 broad = rockFaceSample(grain, p * 0.021, across);
|
|
16
|
+
vec3 broken = rockFaceSample(grain, p * 0.13, across);
|
|
17
|
+
vec3 fine = rockFaceSample(grain, p * 0.68, across);
|
|
18
|
+
// Warped beds follow the height through corners. Individual grains keep roughly the same size in both axes.
|
|
19
|
+
float height = p.y + (texture2D(grain, p.xz * 0.0031).r - 0.5) * 22.0
|
|
20
|
+
+ (broad.g - 0.5) * 3.5 + (broken.b - 0.5) * 1.5;
|
|
21
|
+
float footprint = fwidth(height) * 0.15915;
|
|
22
|
+
float major = rockBed(height, 0.83, 0.0, footprint), minor = rockBed(height, 2.57, 1.3, footprint);
|
|
23
|
+
float stained = rockBed(height, 0.29, 0.7, footprint), lamina = rockBed(height, 7.3, 0.0, footprint);
|
|
24
|
+
vec3 colour = mix(darkBed, lightBed, major * 0.6 + minor * 0.4);
|
|
25
|
+
colour = mix(colour, iron, smoothstep(0.62, 0.95, stained) * 0.55);
|
|
26
|
+
colour = mix(colour, lime, smoothstep(0.75, 1.0, minor * major) * 0.5);
|
|
27
|
+
colour *= (0.92 + 0.12 * lamina) * (0.85 + 0.3 * fine.r) * (0.78 + 0.44 * broad.b) * (0.72 + 0.56 * broken.g);
|
|
28
|
+
return vec4(colour, minor * 0.4 + lamina * 0.12 + broken.g * 0.75 + fine.r * 0.3);
|
|
29
|
+
}`;
|
|
30
|
+
|
|
31
|
+
// Fragment normal for a scalar height. Derivatives are evaluated across the entire quad before the grazing-angle test.
|
|
32
|
+
export const terrainReliefGLSL = /* glsl */ `
|
|
33
|
+
vec3 terrainReliefNormal(vec3 viewToEye, vec3 normal, float height, float strength, vec2 fadeMetres) {
|
|
34
|
+
vec3 sx = dFdx(-viewToEye), sy = dFdy(-viewToEye);
|
|
35
|
+
float gx = dFdx(height), gy = dFdy(height);
|
|
36
|
+
float bump = strength * (1.0 - smoothstep(fadeMetres.x, fadeMetres.y, length(viewToEye))) *
|
|
37
|
+
smoothstep(0.06, 0.4, abs(dot(normal, normalize(viewToEye))));
|
|
38
|
+
float lx = length(sx), ly = length(sy);
|
|
39
|
+
if (bump > 0.01 && lx > 1e-7 && ly > 1e-7) {
|
|
40
|
+
sx /= lx; sy /= ly;
|
|
41
|
+
vec3 r1 = cross(sy, normal), r2 = cross(normal, sx);
|
|
42
|
+
float det = dot(sx, r1);
|
|
43
|
+
vec3 tilted = abs(det) * normal - bump * sign(det) * (gx * r1 + gy * r2);
|
|
44
|
+
float squared = dot(tilted, tilted);
|
|
45
|
+
if (squared > 1e-8) return tilted * inversesqrt(squared);
|
|
46
|
+
}
|
|
47
|
+
return normal;
|
|
48
|
+
}`;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { onCardOnly } from './textures.js';
|
|
3
|
+
|
|
4
|
+
// Each instance owns the startup queue for one renderer. Fill functions return RGBA bytes.
|
|
5
|
+
export function createTextureFills({ definitions, fillPixels, request, available = () => !!request }) {
|
|
6
|
+
const pending = [],
|
|
7
|
+
waiting = [];
|
|
8
|
+
let asking = false,
|
|
9
|
+
started = false; // once the first frame has been drawn: a texture asked for later is filled here, so it is never drawn empty
|
|
10
|
+
|
|
11
|
+
// Relative cost passed to the application’s scheduler.
|
|
12
|
+
const fillCost = name => definitions[name].cost ?? 1;
|
|
13
|
+
// An empty texture of a fill by name, repeating unless clamped, in sRGB unless it holds data, as `pixelTexture` makes one, and what
|
|
14
|
+
// puts pixels in it. A fill with a `depth` is a block: a texture in three dimensions.
|
|
15
|
+
function emptyTexture(name, opts) {
|
|
16
|
+
const { size, depth } = definitions[name],
|
|
17
|
+
texture = depth ? new THREE.Data3DTexture(null, size, size, depth) : new THREE.DataTexture(null, size, size);
|
|
18
|
+
texture.wrapS = texture.wrapT = texture.wrapR = opts.clamp ? THREE.ClampToEdgeWrapping : THREE.RepeatWrapping;
|
|
19
|
+
texture.colorSpace = opts.data ? THREE.NoColorSpace : THREE.SRGBColorSpace;
|
|
20
|
+
texture.anisotropy = opts.aniso || 4;
|
|
21
|
+
texture.magFilter = THREE.LinearFilter;
|
|
22
|
+
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
23
|
+
texture.generateMipmaps = true;
|
|
24
|
+
const fill = pixels => {
|
|
25
|
+
texture.image.data = new Uint8Array(pixels.buffer, pixels.byteOffset, pixels.byteLength);
|
|
26
|
+
texture.needsUpdate = true;
|
|
27
|
+
onCardOnly(texture); // the card keeps it, with its mipmaps
|
|
28
|
+
};
|
|
29
|
+
return { texture, fill };
|
|
30
|
+
}
|
|
31
|
+
// The texture of a fill by name.
|
|
32
|
+
function filledTexture(name, opts = {}) {
|
|
33
|
+
const { texture, fill } = emptyTexture(name, opts),
|
|
34
|
+
ask = () => pending.push(request(name, fillCost(name)).then(fill, () => fill(fillPixels(name))));
|
|
35
|
+
if (!available() || started) fill(fillPixels(name));
|
|
36
|
+
else if (asking) ask();
|
|
37
|
+
else waiting.push(ask);
|
|
38
|
+
return texture;
|
|
39
|
+
}
|
|
40
|
+
// Sends the textures made so far to the workers, and every one made after this at once.
|
|
41
|
+
function fillTextures() {
|
|
42
|
+
asking = true;
|
|
43
|
+
for (const ask of waiting.splice(0)) ask();
|
|
44
|
+
}
|
|
45
|
+
// Kept when every texture asked for so far has its pixels: the first frame waits for it.
|
|
46
|
+
function texturesFilled() {
|
|
47
|
+
fillTextures();
|
|
48
|
+
started = true;
|
|
49
|
+
return Promise.all(pending.splice(0));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// A texture asked for in the world, by something built ahead of need, that is not drawn before `filled()`: filled on the pool's
|
|
53
|
+
// workers, or here by `fillNow()` if it is wanted before they answer.
|
|
54
|
+
function filledLater(name, opts = {}) {
|
|
55
|
+
const { texture, fill } = emptyTexture(name, opts);
|
|
56
|
+
let done = false;
|
|
57
|
+
const fillOnce = pixels => {
|
|
58
|
+
if (done) return;
|
|
59
|
+
done = true;
|
|
60
|
+
fill(pixels);
|
|
61
|
+
};
|
|
62
|
+
const fillNow = () => done || fillOnce(fillPixels(name));
|
|
63
|
+
if (available()) request(name, fillCost(name)).then(fillOnce, fillNow);
|
|
64
|
+
else fillNow();
|
|
65
|
+
return { texture, filled: () => done, fillNow };
|
|
66
|
+
}
|
|
67
|
+
return { filledLater, filledTexture, fillTextures, texturesFilled };
|
|
68
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Fit the drawing buffer to a pixel-area budget while preserving its aspect.
|
|
2
|
+
export function fittedPixelRatio(width, height, requested, maxPixels = Infinity) {
|
|
3
|
+
return Math.min(requested, Math.sqrt(maxPixels / (width * height)));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// Project one world unit at unit depth with a perspective camera's vertical field of view.
|
|
7
|
+
export function perspectivePointScale(height, fovDegrees) {
|
|
8
|
+
return height / (2 * Math.tan(fovDegrees * Math.PI / 360));
|
|
9
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
|
|
3
|
+
export function createWakes({ slots = 6, minSpeed = 0.25, reach = 450, ease = 4 } = {}) {
|
|
4
|
+
if (!Number.isInteger(slots) || slots < 1) throw new RangeError('Wake slot count must be a positive integer');
|
|
5
|
+
const wakeUniforms = {
|
|
6
|
+
uWake: { value: Array.from({ length: slots }, () => new THREE.Vector4()) }, // her middle x, z; her velocity x, z in m/s
|
|
7
|
+
uWakeS: { value: Array.from({ length: slots }, () => new THREE.Vector4()) }, // her length, beam in metres; 1 while in use
|
|
8
|
+
};
|
|
9
|
+
const hulls = []; // { obj, length, beam, x, z, vx, vz, known, d2 }
|
|
10
|
+
|
|
11
|
+
// Follows the hull carried by obj (her middle at its origin), `length` and `beam` metres, for as long as the page lives.
|
|
12
|
+
function followWake(obj, length, beam = length * 0.3) {
|
|
13
|
+
const hull = { obj, length, beam, x: 0, z: 0, vx: 0, vz: 0, known: false, d2: Infinity };
|
|
14
|
+
hulls.push(hull);
|
|
15
|
+
return () => {
|
|
16
|
+
const i = hulls.indexOf(hull);
|
|
17
|
+
if (i >= 0) hulls.splice(i, 1);
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// Whether obj is in the scene and nothing holding it is hidden.
|
|
21
|
+
function shown(obj) {
|
|
22
|
+
let o = obj;
|
|
23
|
+
for (; o.parent; o = o.parent) if (!o.visible) return false;
|
|
24
|
+
return o.isScene === true;
|
|
25
|
+
}
|
|
26
|
+
// Each frame: how fast each hull is moving, measured from where her frame was last drawn; the nearest under way fill the slots.
|
|
27
|
+
function updateWakes(dt, eye) {
|
|
28
|
+
for (let i = 0; i < hulls.length; i++) {
|
|
29
|
+
const h = hulls[i],
|
|
30
|
+
e = h.obj.matrixWorld.elements;
|
|
31
|
+
h.d2 = Infinity;
|
|
32
|
+
if (!shown(h.obj)) {
|
|
33
|
+
h.known = false;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (h.known && dt > 0) {
|
|
37
|
+
const k = Math.min(1, dt * ease);
|
|
38
|
+
h.vx += ((e[12] - h.x) / dt - h.vx) * k;
|
|
39
|
+
h.vz += ((e[14] - h.z) / dt - h.vz) * k;
|
|
40
|
+
} else h.vx = h.vz = 0;
|
|
41
|
+
h.x = e[12];
|
|
42
|
+
h.z = e[14];
|
|
43
|
+
h.known = true;
|
|
44
|
+
const d2 = (h.x - eye.x) ** 2 + (h.z - eye.z) ** 2;
|
|
45
|
+
if (d2 < reach * reach && h.vx * h.vx + h.vz * h.vz > minSpeed * minSpeed) h.d2 = d2;
|
|
46
|
+
}
|
|
47
|
+
const at = wakeUniforms.uWake.value,
|
|
48
|
+
size = wakeUniforms.uWakeS.value;
|
|
49
|
+
for (let slot = 0; slot < slots; slot++) {
|
|
50
|
+
let best = null;
|
|
51
|
+
for (let i = 0; i < hulls.length; i++) if (hulls[i].d2 < (best ? best.d2 : Infinity)) best = hulls[i];
|
|
52
|
+
if (!best) {
|
|
53
|
+
size[slot].w = 0;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
at[slot].set(best.x, best.z, best.vx, best.vz);
|
|
57
|
+
size[slot].set(best.length, best.beam, 0, 1);
|
|
58
|
+
best.d2 = Infinity; // taken
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// In the water's main, after its waves (g) are worked out: adds each wake's slope to g and its foam to the white water
|
|
63
|
+
// (rapid). Reads vW, g, rapid, uTime, and the water's fade of a wave by its fineness (OW).
|
|
64
|
+
const WAKE_GLSL = `/* glsl */
|
|
65
|
+
float wakePx = length(fwidth(vW.xz)); // taken before the loop, where every pixel runs it
|
|
66
|
+
for (int i = 0; i < ${slots} && uWakeS[0].w > 0.5; i++) { // the slots fill in order: none if the first is empty
|
|
67
|
+
vec4 H = uWake[i], S = uWakeS[i];
|
|
68
|
+
if (S.w < 0.5) continue;
|
|
69
|
+
vec2 rel = vW.xz - H.xy; float L = S.x, speed = length(H.zw);
|
|
70
|
+
if (dot(rel, rel) > L * L * 144.0) continue; // twelve of her lengths
|
|
71
|
+
vec2 fwd = H.zw / speed, side = vec2(-fwd.y, fwd.x);
|
|
72
|
+
float fromBow = L * 0.5 - dot(rel, fwd), across = dot(rel, side), off = abs(across), sgn = sign(across);
|
|
73
|
+
if (fromBow < -L * 0.3) continue;
|
|
74
|
+
float under = smoothstep(0.25, 1.5, speed), back = max(fromBow, 0.0);
|
|
75
|
+
float k = 9.81 / max(speed * speed, 0.6); // waves that keep pace with her: 2 pi s^2 / g apart
|
|
76
|
+
float arm = off - back * 0.354; // metres outside the arms of the V (19.5 degrees)
|
|
77
|
+
float spread = 0.7 + back * 0.09, envelope = exp(-arm * arm / (spread * spread)) * exp(-back / (L * 3.0)) * under;
|
|
78
|
+
// along each arm a short train of crests, feathered at 55 degrees to her track
|
|
79
|
+
float phase = k * 1.5 * (back * 0.819 + off * 0.574);
|
|
80
|
+
vec2 dPhase = k * 1.5 * (-fwd * 0.819 + side * sgn * 0.574);
|
|
81
|
+
g -= dPhase * (0.022 * envelope * sin(phase)) * smoothstep(0.0, L * 0.3, back) * OW(k * 1.5);
|
|
82
|
+
// inside the V, crests across her track
|
|
83
|
+
float inside = smoothstep(0.5, -1.5, arm) * exp(-back / (L * 3.0)) * under * smoothstep(0.0, L, back);
|
|
84
|
+
g += fwd * (0.012 * inside * k * sin(k * back)) * OW(k);
|
|
85
|
+
// white water: folding along her sides from the bow, and churned behind her stern, spreading as it falls astern
|
|
86
|
+
vec2 hullAt = vec2(-dot(rel, fwd) / (L * 0.5), across / (S.y * 0.5));
|
|
87
|
+
float bowWave = exp(-(length(hullAt) - 1.0) * 5.0) * smoothstep(0.9, 1.0, length(hullAt)) * smoothstep(-0.3, 0.7, -hullAt.x) * smoothstep(0.6, 2.0, speed);
|
|
88
|
+
float astern = back - L, aft = max(astern, 0.0), wide = S.y * (0.35 + aft / (L * 2.5));
|
|
89
|
+
float churn = exp(-off * off / (wide * wide)) * exp(-aft / (L * 1.4)) * smoothstep(-L * 0.5, 0.0, astern) * under;
|
|
90
|
+
float lace = seaLace(vW.xz * 2.0, uTime, wakePx * 2.0);
|
|
91
|
+
float stern = churn * mix(0.85, lace * 0.8, smoothstep(0.0, L * 0.8, aft)); // solid at her stern, breaking into lace astern
|
|
92
|
+
float armFoam = envelope * exp(-back / L) * lace * 0.6; // the bow wave's white running out along the arms a little way
|
|
93
|
+
rapid = max(rapid, clamp(bowWave * (0.55 + 0.45 * lace) + stern + armFoam, 0.0, 1.0));
|
|
94
|
+
}`;
|
|
95
|
+
const WAKE_PARS = `/* glsl */
|
|
96
|
+
uniform vec4 uWake[${slots}], uWakeS[${slots}];`;
|
|
97
|
+
|
|
98
|
+
return { followWake, updateWakes, WAKE_GLSL, WAKE_PARS, wakeUniforms };
|
|
99
|
+
}
|