roamwright 0.61.14 → 0.61.15
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/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/storm-wall.js +238 -0
- package/rendering/street-lamps.js +101 -1
|
@@ -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
|
+
}
|