roamwright 0.61.14 → 0.61.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -23
- package/examples/effects.js +19 -13
- package/examples/island.js +21 -14
- package/examples/ruins.js +3 -2
- package/examples/stage.js +0 -6
- package/examples/town-fittings.html +52 -0
- package/examples/town-fittings.js +464 -0
- package/geometry/harbour-kit.js +200 -0
- package/geometry/plant-garden.js +92 -0
- package/geometry/storm-wall.js +98 -0
- package/geometry/town-garden.js +343 -0
- package/index.js +8 -1
- package/package.json +1 -1
- package/rendering/lamp-maps.js +87 -0
- package/rendering/plant-garden.js +51 -0
- package/rendering/scatter-instances.js +12 -0
- package/rendering/storm-wall.js +238 -0
- package/rendering/street-lamps.js +101 -1
- package/tools/beasts-near.cjs +17 -0
- package/tools/beasts-near.test.cjs +36 -1
- package/tools/viewport-check.cjs +56 -21
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Instanced garden plant cell renderer: binds cached multi-lobe garden plant shapes (`createGardenPlantShapes`) to a
|
|
3
|
+
* foliage material across spatial cells so each pass can frustum-cull cells independently.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { instancedMesh } from '../geometry/instances.js';
|
|
9
|
+
import { createGardenPlantShapes } from '../geometry/plant-garden.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Creates a garden plant mesh builder bound to a foliage material and shared cached plant geometries.
|
|
13
|
+
* @param {object} [options] - Geometry and material options.
|
|
14
|
+
* @param {(seed: number, detail?: number) => THREE.BufferGeometry} [options.crownBlob] - Displaced crown lump helper (`createPlantTints().crownBlob`).
|
|
15
|
+
* @param {(hex: number) => (x: number, y: number, z: number, out: number[]) => void} [options.crownTint] - Foliage vertex tint helper (`createPlantTints().crownTint`).
|
|
16
|
+
* @param {THREE.Material | (() => THREE.Material)} [options.material] - Foliage material (or lazy factory called on mesh build).
|
|
17
|
+
* @param {Record<string, number[][]>} [options.lobes] - Lobe definitions per plant kind (`GARDEN_PLANT_LOBES`).
|
|
18
|
+
* @param {number} [options.detail=1] - Icosahedron subdivision detail per lobe.
|
|
19
|
+
* @param {number} [options.color=0xffffff] - Base sRGB hex colour before per-instance tinting.
|
|
20
|
+
* @returns {{
|
|
21
|
+
* plantShape: (kind: string) => THREE.BufferGeometry,
|
|
22
|
+
* plantsMesh: (order: { shape: string, cells: Array<{ matrices: Float32Array, colours: Float32Array }> }) => THREE.Group,
|
|
23
|
+
* geometries: WeakSet<THREE.BufferGeometry>,
|
|
24
|
+
* }} Cached shape builder, cell group builder, and shared-geometry membership set (to protect cached geometries on unload).
|
|
25
|
+
*/
|
|
26
|
+
export function createGardenPlants(options = {}) {
|
|
27
|
+
const { plantShape, geometries } = createGardenPlantShapes(options);
|
|
28
|
+
let fallbackMaterial = null;
|
|
29
|
+
const resolveMaterial =
|
|
30
|
+
typeof options.material === 'function'
|
|
31
|
+
? options.material
|
|
32
|
+
: options.material
|
|
33
|
+
? () => options.material
|
|
34
|
+
: () => (fallbackMaterial ??= new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.9 }));
|
|
35
|
+
|
|
36
|
+
function plantsMesh({ shape, cells }) {
|
|
37
|
+
const group = new THREE.Group();
|
|
38
|
+
const geometry = plantShape(shape);
|
|
39
|
+
const material = resolveMaterial();
|
|
40
|
+
for (const cell of cells) {
|
|
41
|
+
const mesh = instancedMesh(geometry, material, cell);
|
|
42
|
+
mesh.castShadow = mesh.receiveShadow = true;
|
|
43
|
+
mesh.matrixAutoUpdate = false;
|
|
44
|
+
mesh.computeBoundingSphere();
|
|
45
|
+
group.add(mesh);
|
|
46
|
+
}
|
|
47
|
+
return group;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { plantShape, plantsMesh, geometries };
|
|
51
|
+
}
|
|
@@ -38,6 +38,9 @@ function createDefaultFoliageMaterial(roughness) {
|
|
|
38
38
|
* @param {boolean} [options.castShadow=true] - Whether each instanced mesh casts shadows.
|
|
39
39
|
* @param {boolean} [options.receiveShadow=true] - Whether each instanced mesh receives shadows.
|
|
40
40
|
* @param {boolean} [options.stopWhenFull=false] - Stop sampling early once every variant bucket is full.
|
|
41
|
+
* @param {((color: THREE.Color, x: number, y: number, z: number, index: number, bucket: number) => void) | null} [options.colorAt=null]
|
|
42
|
+
* - Writes each placed instance's colour into `color` (from where it stands, its index in its variant and the variant),
|
|
43
|
+
* multiplied into the material's colour; without it the instances carry no colour of their own.
|
|
41
44
|
* @returns {{ meshes: THREE.InstancedMesh[], material: THREE.Material, total: number, dispose: () => void }}
|
|
42
45
|
*/
|
|
43
46
|
export function scatterInstances({
|
|
@@ -55,6 +58,7 @@ export function scatterInstances({
|
|
|
55
58
|
castShadow = true,
|
|
56
59
|
receiveShadow = true,
|
|
57
60
|
stopWhenFull = false,
|
|
61
|
+
colorAt = null,
|
|
58
62
|
} = {}) {
|
|
59
63
|
if (!Array.isArray(geometries) || geometries.length === 0) {
|
|
60
64
|
throw new RangeError('geometries must be a non-empty array of BufferGeometry');
|
|
@@ -74,6 +78,7 @@ export function scatterInstances({
|
|
|
74
78
|
stopWhenFull,
|
|
75
79
|
});
|
|
76
80
|
|
|
81
|
+
const color = new THREE.Color();
|
|
77
82
|
const meshes = geometries.map((geometry, index) => {
|
|
78
83
|
const { count, matrices } = plan.buckets[index];
|
|
79
84
|
const mesh = new THREE.InstancedMesh(geometry, mat, 0);
|
|
@@ -81,6 +86,13 @@ export function scatterInstances({
|
|
|
81
86
|
mesh.count = count;
|
|
82
87
|
mesh.castShadow = castShadow;
|
|
83
88
|
mesh.receiveShadow = receiveShadow;
|
|
89
|
+
if (colorAt && count > 0) {
|
|
90
|
+
mesh.instanceColor = new THREE.InstancedBufferAttribute(new Float32Array((matrices.length / 16) * 3), 3);
|
|
91
|
+
for (let i = 0; i < count; i++) {
|
|
92
|
+
colorAt(color, matrices[i * 16 + 12], matrices[i * 16 + 13], matrices[i * 16 + 14], i, index);
|
|
93
|
+
mesh.setColorAt(i, color);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
84
96
|
if (count > 0) mesh.computeBoundingSphere();
|
|
85
97
|
if (parent) parent.add(mesh);
|
|
86
98
|
return mesh;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Animated layered storm wall effect (sandstorm, dust wall, or blizzard curtain): concentric curved sheets displaced
|
|
3
|
+
* downwind and torn by multi-scale drifting tiling noise, lit from sky and sun by day or night, with an inside-wall
|
|
4
|
+
* dust-density readout for atmospheric thickening.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { stormWallArrays } from '../geometry/storm-wall.js';
|
|
10
|
+
import { createNoise } from '../math/noise.js';
|
|
11
|
+
import { clamp, sstep } from '../math/scalar.js';
|
|
12
|
+
import { pixelTexture } from './textures.js';
|
|
13
|
+
|
|
14
|
+
/** Default linear RGB colours for a sunlit sand-and-dust storm wall. */
|
|
15
|
+
export const STORM_WALL_COLORS = {
|
|
16
|
+
shade: [0.17, 0.09, 0.045],
|
|
17
|
+
lit: [0.95, 0.72, 0.45],
|
|
18
|
+
haze: [0.78, 0.62, 0.46],
|
|
19
|
+
sun: [1, 0.93, 0.82],
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const DEFAULT_NOISE_SCALES = [
|
|
23
|
+
[3, 5, 811],
|
|
24
|
+
[6, 5, 823],
|
|
25
|
+
[12, 4, 839],
|
|
26
|
+
];
|
|
27
|
+
const glslFloat = n => (Number.isInteger(n) ? `${n}.0` : String(n));
|
|
28
|
+
const glslVec3 = ([r, g, b]) => `vec3(${glslFloat(r)}, ${glslFloat(g)}, ${glslFloat(b)})`;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Generates a tileable 3-channel procedural noise CanvasTexture (coarse, medium, and fine fBm) used to tear and billow
|
|
32
|
+
* storm-wall curtains.
|
|
33
|
+
* @param {object} [options]
|
|
34
|
+
* @param {(u: number, v: number, period: number, octaves: number, seed: number) => number} [options.tfbm] - Tiling fBm sampler (`createNoise().tfbm`).
|
|
35
|
+
* @param {number} [options.size=256] - Texture width and height in pixels.
|
|
36
|
+
* @param {number} [options.floor=0.28] - Lower fBm value mapped to 0.
|
|
37
|
+
* @param {number} [options.span=0.44] - fBm range mapped to `[0, 1]`.
|
|
38
|
+
* @param {Array<[number, number, number]>} [options.scales] - Three `[period, octaves, seed]` entries for R, G, B channels.
|
|
39
|
+
* @returns {THREE.CanvasTexture} Repeating RGBA noise texture.
|
|
40
|
+
*/
|
|
41
|
+
export function stormWallNoise({ tfbm, size = 256, floor = 0.28, span = 0.44, scales = DEFAULT_NOISE_SCALES } = {}) {
|
|
42
|
+
const sample = tfbm ?? createNoise(0).tfbm;
|
|
43
|
+
const full = v => 255 * clamp((v - floor) / span, 0, 1);
|
|
44
|
+
const [s0, s1, s2] = scales;
|
|
45
|
+
return pixelTexture(
|
|
46
|
+
size,
|
|
47
|
+
(u, v, o) => {
|
|
48
|
+
o[0] = full(sample(u, v, s0[0], s0[1], s0[2]));
|
|
49
|
+
o[1] = full(sample(u, v, s1[0], s1[1], s1[2]));
|
|
50
|
+
o[2] = full(sample(u, v, s2[0], s2[1], s2[2]));
|
|
51
|
+
},
|
|
52
|
+
{ data: true },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Builds a Three.js `BufferGeometry` for a curved layered storm wall using `stormWallArrays`.
|
|
58
|
+
* @param {object} [options] - Geometry parameters forwarded to `stormWallArrays`.
|
|
59
|
+
* @returns {THREE.BufferGeometry} Indexed geometry with `position`, `uv`, and `aEdge` attributes.
|
|
60
|
+
*/
|
|
61
|
+
export function stormWallGeometry(options = {}) {
|
|
62
|
+
const { position, uv, aEdge, index } = stormWallArrays(options);
|
|
63
|
+
const geo = new THREE.BufferGeometry();
|
|
64
|
+
geo.setAttribute('position', new THREE.BufferAttribute(position, 3));
|
|
65
|
+
geo.setAttribute('uv', new THREE.BufferAttribute(uv, 2));
|
|
66
|
+
geo.setAttribute('aEdge', new THREE.BufferAttribute(aEdge, 2));
|
|
67
|
+
geo.setIndex(new THREE.BufferAttribute(index, 1));
|
|
68
|
+
return geo;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Builds a towering curved wall of wind-borne sand, dust, or snow with animated billowing curtains, daylight/moonlight
|
|
73
|
+
* shading, and an inside-wall density readout.
|
|
74
|
+
* @param {object} [options]
|
|
75
|
+
* @param {THREE.Object3D | null} [options.parent=null] - Optional scene/group to attach the mesh to.
|
|
76
|
+
* @param {number} [options.x=0] - Arc centre world X coordinate in metres.
|
|
77
|
+
* @param {number} [options.z=0] - Arc centre world Z coordinate in metres.
|
|
78
|
+
* @param {number} [options.radius=1200] - Mean arc radius in metres.
|
|
79
|
+
* @param {number} [options.sweep=1.5] - Half-angle of the arc in radians.
|
|
80
|
+
* @param {number} [options.width=150] - Radial thickness across all layers in metres.
|
|
81
|
+
* @param {number} [options.height=300] - Curtain height in metres.
|
|
82
|
+
* @param {[number, number]} [options.wind=[0.88, 0.47]] - Horizontal wind direction `[x, z]`.
|
|
83
|
+
* @param {(x: number, z: number) => number} [options.ground] - Ground height sampler `(x, z) => y` in metres.
|
|
84
|
+
* @param {(u: number, v: number, period: number, octaves: number, seed: number) => number} [options.tfbm] - Tiling fBm sampler.
|
|
85
|
+
* @param {THREE.Camera | null} [options.camera=null] - Viewer camera used to compute inside-wall density.
|
|
86
|
+
* @param {THREE.Color} [options.nightTintColor] - Sky/hemisphere colour blended onto the wall at night.
|
|
87
|
+
* @param {() => number} [options.ambientRatio] - Returns ambient-to-noon light ratio (`0..1`) for moonlight/overcast floors.
|
|
88
|
+
* @param {(inside: number) => void} [options.onInside] - Optional callback invoked each frame with inside-wall density (`0..1`).
|
|
89
|
+
* @param {number} [options.layers=5] - Concentric curtain sheets.
|
|
90
|
+
* @param {number} [options.along=150] - Segments along the arc.
|
|
91
|
+
* @param {number} [options.up=10] - Vertical segments.
|
|
92
|
+
* @param {number} [options.endFade=0.12] - Fraction of arc at each end over which the wall thins and sinks.
|
|
93
|
+
* @param {number} [options.billow=28] - Metres the wall's upper curtains billow in and out.
|
|
94
|
+
* @param {number} [options.nightFloor=0.05] - Minimum fraction of noon light on a moonless night.
|
|
95
|
+
* @param {number} [options.moonTint=0.55] - Blend weight toward `nightTintColor` at night.
|
|
96
|
+
* @param {{ shade?: [number, number, number], lit?: [number, number, number], haze?: [number, number, number], sun?: [number, number, number] }} [options.colors] - Wall linear RGB palette.
|
|
97
|
+
* @param {number} [options.renderOrder=7] - Transparent mesh render order.
|
|
98
|
+
* @param {number | null} [options.layer=null] - Optional Three.js render layer index (`mesh.layers.set(layer)`).
|
|
99
|
+
* @returns {{
|
|
100
|
+
* mesh: THREE.Mesh,
|
|
101
|
+
* material: THREE.ShaderMaterial,
|
|
102
|
+
* geometry: THREE.BufferGeometry,
|
|
103
|
+
* noiseTexture: THREE.CanvasTexture,
|
|
104
|
+
* update: (time: number, day: number, strength?: number) => number,
|
|
105
|
+
* dispose: () => void,
|
|
106
|
+
* }} Storm wall handle and zero-allocation per-frame updater returning inside-wall density in `[0, 1]`.
|
|
107
|
+
*/
|
|
108
|
+
export function createStormWall({
|
|
109
|
+
parent = null,
|
|
110
|
+
x = 0,
|
|
111
|
+
z = 0,
|
|
112
|
+
radius = 1200,
|
|
113
|
+
sweep = 1.5,
|
|
114
|
+
width = 150,
|
|
115
|
+
height = 300,
|
|
116
|
+
wind = [0.88, 0.47],
|
|
117
|
+
ground = () => 0,
|
|
118
|
+
tfbm,
|
|
119
|
+
camera = null,
|
|
120
|
+
nightTintColor = new THREE.Color(0.22, 0.28, 0.45),
|
|
121
|
+
ambientRatio = () => 0,
|
|
122
|
+
onInside = null,
|
|
123
|
+
layers = 5,
|
|
124
|
+
along = 150,
|
|
125
|
+
up = 10,
|
|
126
|
+
endFade = 0.12,
|
|
127
|
+
billow = 28,
|
|
128
|
+
nightFloor = 0.05,
|
|
129
|
+
moonTint = 0.55,
|
|
130
|
+
colors = STORM_WALL_COLORS,
|
|
131
|
+
renderOrder = 7,
|
|
132
|
+
layer = null,
|
|
133
|
+
} = {}) {
|
|
134
|
+
const palette = { ...STORM_WALL_COLORS, ...colors };
|
|
135
|
+
const [sunR, sunG, sunB] = palette.sun;
|
|
136
|
+
const geo = stormWallGeometry({ x, z, radius, sweep, width, height, ground, layers, along, up });
|
|
137
|
+
const noiseTexture = stormWallNoise({ tfbm });
|
|
138
|
+
const mat = new THREE.ShaderMaterial({
|
|
139
|
+
side: THREE.DoubleSide,
|
|
140
|
+
transparent: true,
|
|
141
|
+
depthWrite: false,
|
|
142
|
+
fog: true,
|
|
143
|
+
uniforms: {
|
|
144
|
+
...THREE.UniformsUtils.clone(THREE.UniformsLib.fog),
|
|
145
|
+
uTime: { value: 0 },
|
|
146
|
+
uWind: { value: new THREE.Vector2(wind[0], wind[1]) },
|
|
147
|
+
uCentre: { value: new THREE.Vector2(x, z) }, // a curtain seen edge on fades, or it shows as a flat sheet
|
|
148
|
+
uLight: { value: new THREE.Color() },
|
|
149
|
+
uStrength: { value: 1 },
|
|
150
|
+
uNoise: { value: noiseTexture },
|
|
151
|
+
},
|
|
152
|
+
vertexShader: `/* glsl */
|
|
153
|
+
#include <common>
|
|
154
|
+
#include <fog_pars_vertex>
|
|
155
|
+
attribute vec2 aEdge; uniform float uTime; uniform vec2 uWind, uCentre;
|
|
156
|
+
varying vec2 vSt, vEdge; varying float vDist, vFace;
|
|
157
|
+
void main(){
|
|
158
|
+
vSt = uv; vEdge = aEdge;
|
|
159
|
+
vec3 p = position;
|
|
160
|
+
float lift = uv.y * uv.y;
|
|
161
|
+
p.xz += uWind * (lift * 70.0 + sin(uv.x * 3.1 + uTime * 0.21 + uv.y * 4.0) * 14.0 * uv.y); // the crest leans downwind and billows
|
|
162
|
+
vec3 across = normalize(vec3(position.x - uCentre.x, 0.0, position.z - uCentre.y)); // the curtain faces out from the arc's middle
|
|
163
|
+
// the wall billows out and in, along it and across it, more the higher it stands: where the ring turns away from the viewer
|
|
164
|
+
// its side is then a ragged edge, not the straight upright line of a sheet seen edge on
|
|
165
|
+
float b = uv.x * 0.9 + uTime * 0.03;
|
|
166
|
+
p.xz += (across.xz * (sin(b * 1.7 + uv.y * 5.0) + 0.6 * sin(b * 3.9 - uv.y * 9.0)) +
|
|
167
|
+
vec2(-across.z, across.x) * (sin(b * 2.3 - uv.y * 6.5) + 0.5 * sin(b * 5.1 + uv.y * 11.0))) * ${billow.toFixed(1)} * (0.25 + uv.y);
|
|
168
|
+
vec4 mvPosition = modelViewMatrix * vec4(p, 1.0); vDist = length(mvPosition.xyz);
|
|
169
|
+
vFace = abs(dot(normalize(normalMatrix * across), mvPosition.xyz / vDist));
|
|
170
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
171
|
+
#include <fog_vertex>
|
|
172
|
+
}`,
|
|
173
|
+
fragmentShader: `/* glsl */
|
|
174
|
+
#include <common>
|
|
175
|
+
#include <fog_pars_fragment>
|
|
176
|
+
uniform float uTime, uStrength; uniform vec3 uLight; uniform sampler2D uNoise;
|
|
177
|
+
varying vec2 vSt, vEdge; varying float vDist, vFace;
|
|
178
|
+
void main(){
|
|
179
|
+
float t = uTime;
|
|
180
|
+
float tall = texture2D(uNoise, vec2(vSt.x * 0.045 + vEdge.y * 0.3, 0.3 + t * 0.002)).r; // how high the wall stands here: towers and saddles along it
|
|
181
|
+
vec2 rise = vec2(vSt.x, vSt.y * 0.42 - t * 0.016); // the sand streams upward and along
|
|
182
|
+
float n1 = texture2D(uNoise, rise * vec2(0.12, 0.5) + vec2(-t * 0.006, 0.0)).r;
|
|
183
|
+
float n2 = texture2D(uNoise, rise * vec2(0.37, 1.3) + vec2(-t * 0.014, n1 * 0.25)).g;
|
|
184
|
+
float n3 = texture2D(uNoise, rise * vec2(1.2, 3.4) + vec2(-t * 0.03, n2 * 0.2)).b;
|
|
185
|
+
float n = n1 * 0.5 + n2 * 0.32 + n3 * 0.18;
|
|
186
|
+
// the same billows a little higher up: where they are thinner above, the billow's top faces the sky and takes the light
|
|
187
|
+
vec2 above = rise + vec2(0.0, 0.035);
|
|
188
|
+
float m1 = texture2D(uNoise, above * vec2(0.12, 0.5) + vec2(-t * 0.006, 0.0)).r;
|
|
189
|
+
float m2 = texture2D(uNoise, above * vec2(0.37, 1.3) + vec2(-t * 0.014, m1 * 0.25)).g;
|
|
190
|
+
float relief = clamp((n1 * 0.5 + n2 * 0.32) - (m1 * 0.5 + m2 * 0.32), -0.2, 0.2) * 2.5;
|
|
191
|
+
float end = smoothstep(0.0, ${endFade} * 1.5, vEdge.x + (n2 - 0.5) * ${endFade} * 0.5); // the arc's ends thin away and sink, ragged
|
|
192
|
+
float top = (0.42 + 0.56 * tall) * mix(0.3, 1.0, end), h = vSt.y / top;
|
|
193
|
+
float crest = 1.0 - smoothstep(0.62 + 0.3 * n2, 1.0 + 0.12 * n3, h); // torn into plumes at the top
|
|
194
|
+
float body = smoothstep(0.18, 0.62, n + (1.0 - h) * 0.75); // dense at the foot, in rags above
|
|
195
|
+
// where the arc turns edge on to the viewer its curtains fade, raggedly by the billows, or the wall ends in a straight line
|
|
196
|
+
float a = crest * body * smoothstep(0.0, ${endFade}, vEdge.x) * end * smoothstep(12.0, 90.0, vDist) * smoothstep(0.03, 0.4, vFace * (0.4 + 1.2 * n)) * uStrength * (0.3 + 0.24 * vEdge.y);
|
|
197
|
+
float lit = smoothstep(0.2, 0.85, n * 0.55 + h * 0.35 + relief * 0.5); // the billows' tops catch the light; the hollows under them are in shade
|
|
198
|
+
vec3 col = mix(${glslVec3(palette.shade)}, ${glslVec3(palette.lit)}, lit) * (0.82 + 0.3 * n3) * uLight;
|
|
199
|
+
col = mix(col, ${glslVec3(palette.haze)} * uLight, (1.0 - vEdge.y) * 0.3); // the curtains deeper in are paler with the dust before them
|
|
200
|
+
gl_FragColor = vec4(col, a);
|
|
201
|
+
#include <tonemapping_fragment>
|
|
202
|
+
#include <colorspace_fragment>
|
|
203
|
+
#include <fog_fragment>
|
|
204
|
+
}`,
|
|
205
|
+
});
|
|
206
|
+
const mesh = new THREE.Mesh(geo, mat);
|
|
207
|
+
mesh.renderOrder = renderOrder;
|
|
208
|
+
mesh.frustumCulled = false;
|
|
209
|
+
if (layer !== null) mesh.layers.set(layer);
|
|
210
|
+
if (parent) parent.add(mesh);
|
|
211
|
+
|
|
212
|
+
const u = mat.uniforms;
|
|
213
|
+
function update(time, day, strength = 1) {
|
|
214
|
+
const k = u.uStrength.value + clamp(strength - u.uStrength.value, -0.02, 0.02);
|
|
215
|
+
u.uStrength.value = k;
|
|
216
|
+
u.uTime.value = time;
|
|
217
|
+
u.uLight.value
|
|
218
|
+
.setRGB(sunR, sunG, sunB)
|
|
219
|
+
.lerp(nightTintColor, moonTint * (1 - day))
|
|
220
|
+
.multiplyScalar(Math.max(nightFloor + (1 - nightFloor) * day, ambientRatio()));
|
|
221
|
+
let inside = 0;
|
|
222
|
+
if (camera) {
|
|
223
|
+
const off = Math.abs(Math.hypot(camera.position.x - x, camera.position.z - z) - radius);
|
|
224
|
+
inside = sstep(width * 0.9, width * 0.25, off) * sstep(height, height * 0.6, camera.position.y) * k;
|
|
225
|
+
}
|
|
226
|
+
if (onInside) onInside(inside);
|
|
227
|
+
return inside;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function dispose() {
|
|
231
|
+
if (mesh.parent) mesh.parent.remove(mesh);
|
|
232
|
+
geo.dispose();
|
|
233
|
+
mat.dispose();
|
|
234
|
+
noiseTexture.dispose();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return { mesh, material: mat, geometry: geo, noiseTexture, update, dispose };
|
|
238
|
+
}
|
|
@@ -5,8 +5,10 @@ import * as THREE from 'three';
|
|
|
5
5
|
import { GB, geometryOf } from '../geometry/builder.js';
|
|
6
6
|
import { indexParts } from '../geometry/index-parts.js';
|
|
7
7
|
import { C, TRS } from '../geometry/primitives.js';
|
|
8
|
-
import {
|
|
8
|
+
import { litShare } from '../math/lit-share.js';
|
|
9
|
+
import { sstep, TAU } from '../math/scalar.js';
|
|
9
10
|
import { createLightPoints } from './light-points.js';
|
|
11
|
+
import { falloffAt, pixelTexture } from './textures.js';
|
|
10
12
|
|
|
11
13
|
/** The iron of a lamp's post and lantern, and its burner. */
|
|
12
14
|
export const LAMP_IRON = { post: 0x1b1f20, burner: 0x2a2622 };
|
|
@@ -164,3 +166,101 @@ export function createStreetLamps({
|
|
|
164
166
|
points.renderOrder = 8;
|
|
165
167
|
return { posts, mantles, panes, points };
|
|
166
168
|
}
|
|
169
|
+
|
|
170
|
+
/** Default radial alpha falloff for a distant city sky-glow dome sprite: `[radius share, opacity]`. */
|
|
171
|
+
export const CITY_GLOW_FALLOFF = [
|
|
172
|
+
[0, 1],
|
|
173
|
+
[0.4, 0.35],
|
|
174
|
+
[1, 0],
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Builds an additive distant sky-glow dome sprite positioned above a lit town so its night glow reads from afar.
|
|
179
|
+
* @param {object} options
|
|
180
|
+
* @param {THREE.Color | number} options.color - Glow tint colour.
|
|
181
|
+
* @param {[number, number]} [options.size=[640, 300]] - Sprite width and height in metres.
|
|
182
|
+
* @param {[number, number, number]} [options.position=[0, 78, 0]] - World position `[x, y, z]` in metres.
|
|
183
|
+
* @param {number} [options.resolution=128] - Procedural texture side length in pixels.
|
|
184
|
+
* @param {number} [options.middle=78/128] - Vertical centre share (`0..1`) where the dome is brightest.
|
|
185
|
+
* @param {number[][]} [options.falloff=CITY_GLOW_FALLOFF] - Radial opacity falloff stops.
|
|
186
|
+
* @param {number} [options.renderOrder=7] - Render order for the additive sprite.
|
|
187
|
+
* @returns {THREE.Sprite} Configured glow sprite starting at opacity 0.
|
|
188
|
+
*/
|
|
189
|
+
export function createCityGlow({
|
|
190
|
+
color,
|
|
191
|
+
size = [640, 300],
|
|
192
|
+
position = [0, 78, 0],
|
|
193
|
+
resolution = 128,
|
|
194
|
+
middle = 78 / 128,
|
|
195
|
+
falloff = CITY_GLOW_FALLOFF,
|
|
196
|
+
renderOrder = 7,
|
|
197
|
+
}) {
|
|
198
|
+
const dome = pixelTexture(
|
|
199
|
+
resolution,
|
|
200
|
+
(u, v, out) => {
|
|
201
|
+
out[0] = out[1] = out[2] = 255;
|
|
202
|
+
out[3] = 255 * falloffAt(falloff, (Math.hypot(u - 0.5, v - middle) * resolution - 2) / (resolution / 2 - 4));
|
|
203
|
+
},
|
|
204
|
+
{ clamp: true },
|
|
205
|
+
);
|
|
206
|
+
const sprite = new THREE.Sprite(
|
|
207
|
+
new THREE.SpriteMaterial({
|
|
208
|
+
map: dome,
|
|
209
|
+
color,
|
|
210
|
+
blending: THREE.AdditiveBlending,
|
|
211
|
+
depthWrite: false,
|
|
212
|
+
fog: false,
|
|
213
|
+
transparent: true,
|
|
214
|
+
opacity: 0,
|
|
215
|
+
}),
|
|
216
|
+
);
|
|
217
|
+
sprite.scale.set(size[0], size[1], 1);
|
|
218
|
+
sprite.position.set(position[0], position[1], position[2]);
|
|
219
|
+
sprite.renderOrder = renderOrder;
|
|
220
|
+
return sprite;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Creates a zero-allocation per-frame controller for street-lamp night uniforms, distant city glow fade, wall-wash
|
|
225
|
+
* visibility and near-lamp mesh culling.
|
|
226
|
+
* @param {object} options
|
|
227
|
+
* @param {{ night: { value: number }, litShare: { value: number }, time: { value: number }, sky: { value: THREE.Color } }} options.uniforms - Shared night/window/clock/sky shader uniforms.
|
|
228
|
+
* @param {THREE.Camera} options.camera - Active viewer camera.
|
|
229
|
+
* @param {{ x: number, z: number }} options.center - Town centre `[x, z]` in metres for distance culling and glow fade.
|
|
230
|
+
* @param {{ glow?: THREE.Sprite | null, posts?: THREE.Object3D | null, panes?: THREE.Object3D | null, mantles?: THREE.Object3D | null, wallWash?: { visible: boolean } | null }} options.lamps - Lamp object holder (fields may be populated lazily after build).
|
|
231
|
+
* @param {number} [options.skyScale=0.55] - Horizon colour multiplier fed into `uniforms.sky`.
|
|
232
|
+
* @param {[number, number]} [options.nightRange=[0.12, 0.75]] - Smoothstep input range converting `night + dusk * duskWeight` into `uniforms.night`.
|
|
233
|
+
* @param {number} [options.duskWeight=0.35] - Contribution of dusk to the lamp night factor.
|
|
234
|
+
* @param {number} [options.glowPeak=0.42] - Maximum opacity of the distant city glow sprite at full night.
|
|
235
|
+
* @param {[number, number]} [options.glowFade=[240, 600]] - Distance range in metres over which the city glow fades in.
|
|
236
|
+
* @param {number} [options.nearRange=420] - Distance in metres within which lamp posts, panes and mantles stay visible.
|
|
237
|
+
* @returns {(night: number, dusk: number, time: number, elapsed: number, horizon: THREE.Color) => number} Zero-allocation per-frame updater returning the computed night factor in `[0, 1]`.
|
|
238
|
+
*/
|
|
239
|
+
export function createStreetLampNight({
|
|
240
|
+
uniforms,
|
|
241
|
+
camera,
|
|
242
|
+
center,
|
|
243
|
+
lamps,
|
|
244
|
+
skyScale = 0.55,
|
|
245
|
+
nightRange = [0.12, 0.75],
|
|
246
|
+
duskWeight = 0.35,
|
|
247
|
+
glowPeak = 0.42,
|
|
248
|
+
glowFade = [240, 600],
|
|
249
|
+
nearRange = 420,
|
|
250
|
+
}) {
|
|
251
|
+
return function updateStreetLampNight(night, dusk, time, elapsed, horizon) {
|
|
252
|
+
const nightLevel = sstep(nightRange[0], nightRange[1], night + dusk * duskWeight);
|
|
253
|
+
uniforms.night.value = nightLevel;
|
|
254
|
+
uniforms.litShare.value = litShare(nightLevel, time);
|
|
255
|
+
uniforms.time.value = elapsed;
|
|
256
|
+
uniforms.sky.value.copy(horizon).multiplyScalar(skyScale);
|
|
257
|
+
if (lamps.wallWash) lamps.wallWash.visible = nightLevel > 0;
|
|
258
|
+
if (lamps.glow) {
|
|
259
|
+
const dc = Math.hypot(camera.position.x - center.x, camera.position.z - center.z);
|
|
260
|
+
lamps.glow.material.opacity = nightLevel * glowPeak * sstep(glowFade[0], glowFade[1], dc);
|
|
261
|
+
lamps.glow.visible = lamps.glow.material.opacity > 0;
|
|
262
|
+
lamps.posts.visible = lamps.panes.visible = lamps.mantles.visible = dc < nearRange;
|
|
263
|
+
}
|
|
264
|
+
return nightLevel;
|
|
265
|
+
};
|
|
266
|
+
}
|
package/tools/beasts-near.cjs
CHANGED
|
@@ -204,10 +204,19 @@ function settleHerdBeastInPage({ debug, crowdIndex, memberIndex, hour }) {
|
|
|
204
204
|
*
|
|
205
205
|
* @param {{ debug: string, actorId: string, hour: number | null }} arg
|
|
206
206
|
*/
|
|
207
|
+
// When a scene's script first shows an actor (a `[seconds, 'show', actorId]` step of its `steps`), or null if it never does.
|
|
208
|
+
function sceneShowTimeInPage({ debug, sceneId, actorId }) {
|
|
209
|
+
const ev = window[debug].CONFIG.events.find(e => e.id === sceneId);
|
|
210
|
+
const step = (ev?.steps || ev?.script || []).find(s => Array.isArray(s) && s[1] === 'show' && s[2] === actorId);
|
|
211
|
+
return step ? step[0] : null;
|
|
212
|
+
}
|
|
213
|
+
|
|
207
214
|
function settleActorInPage({ debug, actorId, hour }) {
|
|
208
215
|
const app = window[debug];
|
|
209
216
|
const a = app.actors?.[actorId];
|
|
210
217
|
if (!a?.group) return null;
|
|
218
|
+
// An actor a scene has not shown yet stands where it was built (the world's origin, under the land): nothing real to frame.
|
|
219
|
+
if (a.active === false) return { hidden: true };
|
|
211
220
|
const { THREE } = app;
|
|
212
221
|
if (app.director) app.director.paused = true;
|
|
213
222
|
app.env.flow = false;
|
|
@@ -395,6 +404,13 @@ async function runBeastsNear(
|
|
|
395
404
|
actorId,
|
|
396
405
|
hour: opts.hourExplicit ? opts.hour : null,
|
|
397
406
|
});
|
|
407
|
+
if (target?.hidden) {
|
|
408
|
+
const shownAt = await page.evaluate(sceneShowTimeInPage, { debug: driver.debug, sceneId: meta.id, actorId });
|
|
409
|
+
throw new Error(
|
|
410
|
+
`${actorId} is not shown at ${targetAt}s of ${meta.id}` +
|
|
411
|
+
(Number.isFinite(shownAt) ? `: it comes on at ${shownAt}s, so ask --at ${Math.ceil(shownAt + 1)} or later` : ''),
|
|
412
|
+
);
|
|
413
|
+
}
|
|
398
414
|
if (target) {
|
|
399
415
|
saved.push(
|
|
400
416
|
...(await captureTargetViews(page, driver, target, {
|
|
@@ -493,6 +509,7 @@ module.exports = {
|
|
|
493
509
|
parseBeastsNearArgs,
|
|
494
510
|
poseCameraAndDrawInPage,
|
|
495
511
|
runBeastsNear,
|
|
512
|
+
sceneShowTimeInPage,
|
|
496
513
|
settleActorInPage,
|
|
497
514
|
settleHerdBeastInPage,
|
|
498
515
|
};
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
const assert = require('node:assert/strict');
|
|
3
3
|
const test = require('node:test');
|
|
4
|
-
const {
|
|
4
|
+
const {
|
|
5
|
+
frameBeastView,
|
|
6
|
+
locateHerdBeastInPage,
|
|
7
|
+
parseBeastsNearArgs,
|
|
8
|
+
poseCameraAndDrawInPage,
|
|
9
|
+
sceneShowTimeInPage,
|
|
10
|
+
settleActorInPage,
|
|
11
|
+
} = require('./beasts-near.cjs');
|
|
5
12
|
|
|
6
13
|
test('parseBeastsNearArgs parses kinds, scene beat options, and flags cleanly', () => {
|
|
7
14
|
const parsed = parseBeastsNearArgs([
|
|
@@ -103,3 +110,31 @@ test('locateHerdBeastInPage and poseCameraAndDrawInPage talk strictly through wi
|
|
|
103
110
|
delete globalThis.window;
|
|
104
111
|
}
|
|
105
112
|
});
|
|
113
|
+
|
|
114
|
+
test('a scene actor not shown yet is refused, with the second its script shows it', () => {
|
|
115
|
+
const debug = 'BEASTS_TEST';
|
|
116
|
+
globalThis.window = {
|
|
117
|
+
[debug]: {
|
|
118
|
+
actors: { hound: { group: {}, active: false } },
|
|
119
|
+
CONFIG: {
|
|
120
|
+
events: [
|
|
121
|
+
{
|
|
122
|
+
id: 'hunt',
|
|
123
|
+
script: [
|
|
124
|
+
[0, 'hour', 6],
|
|
125
|
+
[12.5, 'show', 'hound'],
|
|
126
|
+
[14, 'show', 'crow'],
|
|
127
|
+
],
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
try {
|
|
134
|
+
assert.deepEqual(settleActorInPage({ debug, actorId: 'hound', hour: null }), { hidden: true });
|
|
135
|
+
assert.equal(sceneShowTimeInPage({ debug, sceneId: 'hunt', actorId: 'hound' }), 12.5);
|
|
136
|
+
assert.equal(sceneShowTimeInPage({ debug, sceneId: 'hunt', actorId: 'wolf' }), null);
|
|
137
|
+
} finally {
|
|
138
|
+
delete globalThis.window;
|
|
139
|
+
}
|
|
140
|
+
});
|
package/tools/viewport-check.cjs
CHANGED
|
@@ -20,6 +20,11 @@ const assert = require('node:assert/strict');
|
|
|
20
20
|
* @param {string} [options.fullscreenButtonSelector='#b-full'] Selector for the fullscreen toggle button.
|
|
21
21
|
* @param {string} [options.turnedClass='turned'] Body class used for rotated phone landscape layout.
|
|
22
22
|
* @param {string} [options.touchNonPhoneSelector='body.touch:not(.phone)'] Selector patched to simulate stale tablet CSS width.
|
|
23
|
+
* @param {{ output?: number, scene?: number }} [options.phoneScale={ output: 1, scene: 1 }] The share of each side the
|
|
24
|
+
* phone's Medium preset draws at: the output of its render limit, and the scene texture of the output (each within a
|
|
25
|
+
* pixel of rounding).
|
|
26
|
+
* @param {{ width: number, height: number }} [options.phoneLimit={ width: 1920, height: 1080 }] The most pixels the phone's
|
|
27
|
+
* output is drawn at, before `phoneScale`; a device with more is fitted into it.
|
|
23
28
|
* @returns {Promise<void>}
|
|
24
29
|
*/
|
|
25
30
|
async function runViewportCheck(
|
|
@@ -33,6 +38,8 @@ async function runViewportCheck(
|
|
|
33
38
|
fullscreenButtonSelector = '#b-full',
|
|
34
39
|
turnedClass = 'turned',
|
|
35
40
|
touchNonPhoneSelector = 'body.touch:not(.phone)',
|
|
41
|
+
phoneScale = {},
|
|
42
|
+
phoneLimit = { width: 1920, height: 1080 },
|
|
36
43
|
} = {},
|
|
37
44
|
) {
|
|
38
45
|
const { BASE, GPU, chromium, shell, debug, enter, applyStorage, routeUrl } = driver;
|
|
@@ -135,35 +142,63 @@ async function runViewportCheck(
|
|
|
135
142
|
await picture(1920, 1080);
|
|
136
143
|
await page.close();
|
|
137
144
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
145
|
+
// Checks the output is `width` x `height` (or, given a `scale` `{ output, scene }`, the device's pixels of that size fitted
|
|
146
|
+
// into the phone's limit drawn at `scale.output` of each side) and the scene texture `scale.scene` of the output.
|
|
147
|
+
async function checkTouchViewport(tab, width, height, scale = null) {
|
|
148
|
+
const fit = scale
|
|
149
|
+
? Math.min(1, (phoneLimit.width * (scale.output ?? 1)) / width, (phoneLimit.height * (scale.output ?? 1)) / height)
|
|
150
|
+
: 1;
|
|
151
|
+
const outW = Math.floor(width * fit);
|
|
152
|
+
const outH = Math.floor(height * fit);
|
|
153
|
+
const sceneW = Math.round(outW * (scale?.scene ?? 1));
|
|
154
|
+
const sceneH = Math.round(outH * (scale?.scene ?? 1));
|
|
155
|
+
await tab
|
|
156
|
+
.waitForFunction(
|
|
157
|
+
([d, w, h, sw, sh]) => {
|
|
158
|
+
const { renderer, post } = window[d];
|
|
159
|
+
const gl = renderer.getContext();
|
|
160
|
+
const bounds = renderer.domElement.getBoundingClientRect();
|
|
161
|
+
const near = (a, b) => Math.abs(a - b) <= 1;
|
|
162
|
+
return (
|
|
163
|
+
near(gl.drawingBufferWidth, w) &&
|
|
164
|
+
near(gl.drawingBufferHeight, h) &&
|
|
165
|
+
near(post.sceneRT.width, sw) &&
|
|
166
|
+
near(post.sceneRT.height, sh) &&
|
|
167
|
+
Math.abs(bounds.width - innerWidth) < 1 &&
|
|
168
|
+
Math.abs(bounds.height - innerHeight) < 1
|
|
169
|
+
);
|
|
170
|
+
},
|
|
171
|
+
[debug, outW, outH, sceneW, sceneH],
|
|
172
|
+
{ timeout: 12000 },
|
|
173
|
+
)
|
|
174
|
+
.catch(async error => {
|
|
175
|
+
console.error(
|
|
176
|
+
await tab.evaluate(d => {
|
|
177
|
+
const { renderer, post } = window[d];
|
|
178
|
+
const gl = renderer.getContext();
|
|
179
|
+
const bounds = renderer.domElement.getBoundingClientRect();
|
|
180
|
+
return {
|
|
181
|
+
buffer: [gl.drawingBufferWidth, gl.drawingBufferHeight],
|
|
182
|
+
scene: [post.sceneRT.width, post.sceneRT.height],
|
|
183
|
+
bounds: [bounds.width, bounds.height],
|
|
184
|
+
viewport: [innerWidth, innerHeight],
|
|
185
|
+
ratio: renderer.getPixelRatio(),
|
|
186
|
+
};
|
|
187
|
+
}, debug),
|
|
151
188
|
);
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
);
|
|
156
|
-
console.log(width + ' x ' + height + ' scene and output filling the touch viewport');
|
|
189
|
+
throw error;
|
|
190
|
+
});
|
|
191
|
+
console.log(`${outW} x ${outH} output and ${sceneW} x ${sceneH} scene filling the touch viewport`);
|
|
157
192
|
}
|
|
158
193
|
|
|
159
194
|
const phone = await browser.newPage({ viewport: { width: 430, height: 932 }, deviceScaleFactor: 3, hasTouch: true, isMobile: true });
|
|
160
195
|
await applyStorage(phone, { quality: 'medium' });
|
|
161
196
|
await enter(phone, pageUrl, { waitUntil: 'load', direct: true });
|
|
162
|
-
await checkTouchViewport(phone,
|
|
197
|
+
await checkTouchViewport(phone, 2796, 1290, phoneScale); // 932 x 430 at 3x, turned
|
|
163
198
|
await phone.setViewportSize({ width: 742, height: 430 });
|
|
164
|
-
await checkTouchViewport(phone,
|
|
199
|
+
await checkTouchViewport(phone, 2226, 1290, phoneScale);
|
|
165
200
|
await phone.setViewportSize({ width: 600, height: 300 });
|
|
166
|
-
await checkTouchViewport(phone, 1800, 900);
|
|
201
|
+
await checkTouchViewport(phone, 1800, 900, phoneScale);
|
|
167
202
|
await phone.close();
|
|
168
203
|
|
|
169
204
|
const tablet = await browser.newPage({
|