roamwright 0.61.13 → 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 -22
- package/examples/ruins.html +60 -0
- package/examples/ruins.js +326 -0
- package/examples/town-fittings.html +52 -0
- package/examples/town-fittings.js +464 -0
- package/geometry/harbour-kit.js +200 -0
- package/geometry/paved-road.js +443 -0
- package/geometry/plant-garden.js +92 -0
- package/geometry/ruins-hill.js +392 -0
- package/geometry/ruins-mound.js +484 -0
- package/geometry/ruins-rubble.js +161 -0
- package/geometry/storm-wall.js +98 -0
- package/geometry/strata-rocks.js +270 -0
- package/geometry/town-garden.js +343 -0
- package/index.js +37 -1
- package/package.json +1 -1
- package/rendering/lamp-maps.js +87 -0
- package/rendering/paved-ground.js +218 -0
- package/rendering/paved-road.js +353 -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,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Procedural paved-ground shader material: stone-by-stone ringed flagstone plazas, coursed flagstone
|
|
3
|
+
* streets with central foot-wear bands, and cobblestone fills with optional beaten-earth patches and
|
|
4
|
+
* after-rain standing puddles. Sub-pixel stones, joints and crowns anti-alias smoothly via `fwidth`.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { createNoise } from '../math/noise.js';
|
|
10
|
+
import { createHash } from '../math/random.js';
|
|
11
|
+
import { createSurfaceTextures, fillPixels } from '../math/surface-textures.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Default uniform slot limits for ringed squares, coursed street strips and rain puddles in
|
|
15
|
+
* {@link createPavedGroundMaterial}.
|
|
16
|
+
*/
|
|
17
|
+
export const PAVED_GROUND_LIMITS = Object.freeze({
|
|
18
|
+
rings: 2,
|
|
19
|
+
strips: 4,
|
|
20
|
+
pools: 12,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function buildPavingGLSL({ rings, strips, pools }) {
|
|
24
|
+
return `/* glsl */
|
|
25
|
+
vec2 lp = vWP.xz - uPaveO; // metres in the space's frame
|
|
26
|
+
vec2 fw = fwidth(lp); float pxM = max(fw.x, fw.y); // how many metres a pixel spans here
|
|
27
|
+
vec2 q = vec2(0.0), sz = vec2(1.0); float edgeM = 1e3, wear = 0.0, kind = -1.0;
|
|
28
|
+
for (int i = 0; i < ${rings}; i++) { // a square's flags in rings round its centre
|
|
29
|
+
vec4 R = uRing[i]; vec2 d = lp - R.xy; float r = length(d);
|
|
30
|
+
if (kind < 0.0 && R.z > 0.0 && r < R.z) {
|
|
31
|
+
float ring = floor(r / R.w), n = ring < 0.5 ? 1.0 : max(6.0, floor(6.2832 * (ring + 0.5) / 1.2)); // flags about 1.2 times as long as a ring is wide
|
|
32
|
+
float a = atan(d.y, d.x) / 6.2832 + 0.5;
|
|
33
|
+
q = vec2(a * n + fract(ring * 0.618), r / R.w); sz = vec2(6.2832 * max(r, R.w) / n, R.w);
|
|
34
|
+
edgeM = R.z - r; kind = 0.0;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
for (int i = 0; i < ${strips}; i++) { // a street's flags in courses across it, the joints broken from one course to the next
|
|
38
|
+
vec4 A = uStripA[i], B = uStripB[i];
|
|
39
|
+
if (kind < 0.0 && A.z > A.x && lp.x > A.x && lp.x < A.z && lp.y > A.y && lp.y < A.w) {
|
|
40
|
+
vec2 st = B.z > 0.5 ? lp : lp.yx; // across, along
|
|
41
|
+
vec2 lo = B.z > 0.5 ? A.xy : A.yx, hi = B.z > 0.5 ? A.zw : A.wz;
|
|
42
|
+
float course = floor(st.y / B.y);
|
|
43
|
+
q = vec2(st.x / B.x + 0.5 * mod(course, 2.0) + 0.23 * fract(course * 0.37), st.y / B.y); sz = B.xy;
|
|
44
|
+
edgeM = min(min(st.x - lo.x, hi.x - st.x), min(st.y - lo.y, hi.y - st.y));
|
|
45
|
+
wear = B.w > 0.0 ? 1.0 - smoothstep(0.0, B.w, abs(st.x - (lo.x + hi.x) * 0.5)) : 0.0;
|
|
46
|
+
kind = 1.0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (kind < 0.0) { // cobbles: rounded setts in rows, each row set its own way, each stone its own length
|
|
50
|
+
float rowH = uFill.x * 0.85, row = floor(lp.y / rowH), rr = fract(sin(row * 91.7) * 4375.85), k = 0.85 + 0.3 * rr;
|
|
51
|
+
float cx = lp.x / uFill.x * k + rr * 7.0;
|
|
52
|
+
cx += 0.22 * sin(cx * 1.9 + rr * 6.0); // stones of a row longer and shorter
|
|
53
|
+
q = vec2(cx, lp.y / rowH); sz = vec2(uFill.x / k, rowH); kind = 2.0;
|
|
54
|
+
}
|
|
55
|
+
vec2 id = floor(q), f = fract(q) - 0.5;
|
|
56
|
+
float rnd = fract(sin(dot(id, vec2(12.9898, 78.233))) * 43758.5453), rnd2 = fract(rnd * 17.31 + 0.13);
|
|
57
|
+
// metres from the stone's outline (a rounded rectangle; cobbles round, flags nearly square-cornered) to here
|
|
58
|
+
float corner = kind > 1.5 ? 0.32 * min(sz.x, sz.y) : 0.012;
|
|
59
|
+
vec2 dd = abs(f * sz) - (sz * 0.5 - corner);
|
|
60
|
+
float gapM = min(corner - length(max(dd, 0.0)) - min(max(dd.x, dd.y), 0.0), edgeM);
|
|
61
|
+
float crack = 0.0;
|
|
62
|
+
if (kind < 1.5 && rnd > 0.92) { // a cracked flag: a hairline across it at its own angle, kinked once
|
|
63
|
+
float ca = rnd2 * 6.2832; vec2 cf = f * sz; float t = dot(cf, vec2(-sin(ca), cos(ca)));
|
|
64
|
+
float dM = abs(dot(cf, vec2(cos(ca), sin(ca))) + 0.06 * abs(t - (rnd - 0.96) * 6.0) - 0.05);
|
|
65
|
+
crack = 1.0 - smoothstep(0.001, 0.001 + pxM, dM);
|
|
66
|
+
}
|
|
67
|
+
float jointW = kind > 1.5 ? 0.012 : 0.009; // half a joint's width: cobbles sit in wider joints of sand
|
|
68
|
+
float fade = smoothstep(0.12, 0.45, pxM / min(sz.x, sz.y)); // 1 once a stone is a few pixels across: tones and joints to their mean
|
|
69
|
+
float joint = max(1.0 - smoothstep(jointW - pxM * 0.5, jointW + pxM * 0.5, gapM), crack * 0.4) * (1.0 - fade);
|
|
70
|
+
float jointMean = clamp(2.0 * (jointW + corner * 0.3) * (sz.x + sz.y) / (sz.x * sz.y), 0.0, 0.5);
|
|
71
|
+
// the stone: its own tone, a few warmer or greyer, mottled with patina, grimed along its arrises, darker and smoother
|
|
72
|
+
// where feet have worn it, freckled close up; stains spread across many stones where people stand and water stood
|
|
73
|
+
vec2 sgx = dFdx(lp), sgy = dFdy(lp); // graded by the ground, not by the jump in id at a joint
|
|
74
|
+
float speck = textureGrad(map, lp * 0.71 + id * 0.37, sgx * 0.71, sgy * 0.71).r, mott = textureGrad(map, lp * 0.19 + id * 0.61, sgx * 0.19, sgy * 0.19).g;
|
|
75
|
+
vec3 tone = vec3(0.8 + 0.34 * rnd) * mix(vec3(1.0), rnd2 < 0.5 ? vec3(1.07, 1.0, 0.9) : vec3(0.93, 0.97, 1.04), abs(rnd2 - 0.5) * 1.8);
|
|
76
|
+
tone *= (0.84 + 0.32 * mott) * (0.85 + 0.3 * speck * (1.0 - smoothstep(0.004, 0.02, pxM))) * (1.0 - 0.18 * (1.0 - smoothstep(0.0, 0.07, gapM)));
|
|
77
|
+
tone = mix(tone, vec3(1.0), fade) * (1.0 - 0.13 * wear) * (1.0 - 0.22 * smoothstep(0.55, 0.85, texture2D(map, lp * 0.043 + 0.5).r));
|
|
78
|
+
vec3 dirt = vec3(0.5, 0.47, 0.42) * (1.0 - 0.3 * uFill.z); // as shares of the ground's vertex colour, which three.js multiplies in next
|
|
79
|
+
diffuseColor.rgb *= mix(tone, dirt, joint + fade * jointMean);
|
|
80
|
+
// where the cobbles between the blocks are broken up, beaten earth shows through
|
|
81
|
+
float earth = kind > 1.5 ? smoothstep(0.45, 0.75, texture2D(map, lp * 0.023).g + 0.4 * uFill.y) * uFill.y : 0.0;
|
|
82
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.92, 0.86, 0.76) * (0.8 + 0.4 * speck), earth);
|
|
83
|
+
vec3 g1 = texture2D(map, vWP.xz / 9.0).rgb, g2 = texture2D(map, vWP.xz * 0.019 + 0.37).rgb; // the broad grain every ground has
|
|
84
|
+
diffuseColor.rgb *= mix(vec3(1.0), g1 * g2 * 2.3, 0.55 + 0.45 * fade);
|
|
85
|
+
// a stone's crown: flags flat with worn arrises, cobbles domed; the joints sit lower
|
|
86
|
+
float inside = clamp(gapM / (0.5 * min(sz.x, sz.y)), 0.0, 1.0);
|
|
87
|
+
float crown = kind > 1.5 ? 0.014 * (1.0 - (1.0 - inside) * (1.0 - inside)) : 0.004 * smoothstep(0.0, 0.05, gapM);
|
|
88
|
+
float dist = length(vViewPosition);
|
|
89
|
+
pvH = (crown - 0.006 * joint) * (1.0 - fade) * (1.0 - earth) * (1.0 - smoothstep(25.0, 60.0, dist));
|
|
90
|
+
pvRough = mix(0.86 - 0.18 * wear, 0.97, max(joint, earth));
|
|
91
|
+
// after rain: a film of water on the stone, water standing in the joints, in the low places and in the plan's puddles,
|
|
92
|
+
// whose edges wander and which fill the joints round them; all of it glossy, the puddles a dark mirror
|
|
93
|
+
if (uFill.z > 0.0) {
|
|
94
|
+
float pool = smoothstep(0.6, 0.72, texture2D(map, lp * 0.031 + 0.21).b * 0.7 + texture2D(map, lp * 0.11).g * 0.3) * 0.6;
|
|
95
|
+
float edge = (texture2D(map, lp * 0.37).r - 0.5) * 0.5;
|
|
96
|
+
for (int i = 0; i < ${pools}; i++) {
|
|
97
|
+
vec4 P = uPools[i];
|
|
98
|
+
if (P.z > 0.0) pool = max(pool, 1.0 - smoothstep(0.78, 1.0, length((lp - P.xy) / (P.zw * 0.5)) + edge));
|
|
99
|
+
}
|
|
100
|
+
pvWet = max(max(joint, pool), 0.55 + 0.45 * (1.0 - speck)) * uFill.z;
|
|
101
|
+
diffuseColor.rgb *= 1.0 - 0.45 * pvWet - 0.3 * pool;
|
|
102
|
+
pvRough = mix(pvRough, mix(0.22, 0.03, max(joint, pool)), uFill.z);
|
|
103
|
+
pvH *= 1.0 - pool;
|
|
104
|
+
pvPool = pool;
|
|
105
|
+
}
|
|
106
|
+
// the crowns' many slopes, once too small to draw, widen the gloss instead of leaving a mirror (less so on wet stone,
|
|
107
|
+
// whose film is smooth whatever lies under it: there a far light draws a path across the street, not a pale sheet)
|
|
108
|
+
pvRough = mix(pvRough, max(pvRough, mix(0.5, 0.3, uFill.z)), max(fade, 0.7 * smoothstep(8.0, 45.0, dist)) * (1.0 - pvPool));
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Screen-space derivative bump tilt from the procedural stone crown height `pvH`.
|
|
113
|
+
const PAVING_BUMP = `/* glsl */
|
|
114
|
+
{ vec3 sx = dFdx(-vViewPosition), sy = dFdy(-vViewPosition), r1 = cross(sy, normal), r2 = cross(normal, sx);
|
|
115
|
+
float det = dot(sx, r1); vec3 nb = abs(det) * normal - sign(det) * (dFdx(pvH) * r1 + dFdy(pvH) * r2);
|
|
116
|
+
if (dot(nb, nb) > 1e-12) normal = normalize(nb); }`;
|
|
117
|
+
|
|
118
|
+
// Grazing Fresnel sky reflection on wet stone films and standing rain puddles.
|
|
119
|
+
const WET_SHEEN = `/* glsl */
|
|
120
|
+
if (pvWet > 0.0) {
|
|
121
|
+
#ifdef USE_FOG
|
|
122
|
+
vec3 sky = fogColor;
|
|
123
|
+
#else
|
|
124
|
+
vec3 sky = vec3(0.1, 0.13, 0.2);
|
|
125
|
+
#endif
|
|
126
|
+
float fres = 0.02 + 0.98 * pow(1.0 - saturate(dot(normal, normalize(vViewPosition))), 5.0);
|
|
127
|
+
outgoingLight += sky * fres * mix(0.5 * pvWet, 1.0, pvPool);
|
|
128
|
+
}`;
|
|
129
|
+
|
|
130
|
+
let fallbackGrain = null;
|
|
131
|
+
function defaultGrainTexture() {
|
|
132
|
+
if (fallbackGrain) return fallbackGrain;
|
|
133
|
+
const size = 256;
|
|
134
|
+
const { groundGrain } = createSurfaceTextures({ noise: createNoise(41), hash2: createHash(41) });
|
|
135
|
+
const data = fillPixels({ size, make: groundGrain });
|
|
136
|
+
const tex = new THREE.DataTexture(data, size, size);
|
|
137
|
+
tex.colorSpace = THREE.SRGBColorSpace;
|
|
138
|
+
tex.wrapS = tex.wrapT = THREE.RepeatWrapping;
|
|
139
|
+
tex.generateMipmaps = true;
|
|
140
|
+
tex.minFilter = THREE.LinearMipmapLinearFilter;
|
|
141
|
+
tex.magFilter = THREE.LinearFilter;
|
|
142
|
+
tex.needsUpdate = true;
|
|
143
|
+
return (fallbackGrain = tex);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Create a procedural paved-ground `MeshStandardMaterial` drawing concentric flagstone rings,
|
|
148
|
+
* coursed flagstone street strips, cobblestone fills, optional beaten-earth wear and wet puddles
|
|
149
|
+
* in the space's local `(x, z)` frame. Vertex colours supply each area's base stone hue.
|
|
150
|
+
*
|
|
151
|
+
* @param {object} [options]
|
|
152
|
+
* @param {{ x: number, z: number }} [options.origin] - World-space origin `{ x, z }` of the paving plan in metres.
|
|
153
|
+
* @param {import('three').Texture} [options.map] - Tileable RGB noise/grain texture (generated procedurally if omitted).
|
|
154
|
+
* @param {Array<{ x: number, z: number, r: number, ring: number }>} [options.rings=[]] - Ringed flagstone plazas (`r` radius, `ring` radial width, metres).
|
|
155
|
+
* @param {Array<{ x0: number, z0: number, x1: number, z1: number, len: number, course: number, along?: 'x' | 'z', wear?: number }>} [options.strips=[]] - Coursed flagstone street rectangles.
|
|
156
|
+
* @param {number} [options.cobble=0.16] - Cobblestone width in metres outside rings and strips.
|
|
157
|
+
* @param {number} [options.earth=0] - Amount of beaten earth showing through broken cobbles (`0` to `1`).
|
|
158
|
+
* @param {number} [options.wet=0] - Rain wetness factor (`0` dry, `1` wet after rain).
|
|
159
|
+
* @param {Array<[number, number, number, number]>} [options.pools=[]] - Elliptical rain puddles `[x, z, across, along]` in metres.
|
|
160
|
+
* @param {Partial<typeof PAVED_GROUND_LIMITS>} [options.limits] - Override uniform array capacities (`rings`, `strips`, `pools`).
|
|
161
|
+
* @returns {import('three').MeshStandardMaterial}
|
|
162
|
+
*/
|
|
163
|
+
export function createPavedGroundMaterial({
|
|
164
|
+
origin = { x: 0, z: 0 },
|
|
165
|
+
map,
|
|
166
|
+
rings = [],
|
|
167
|
+
strips = [],
|
|
168
|
+
cobble = 0.16,
|
|
169
|
+
earth = 0,
|
|
170
|
+
wet = 0,
|
|
171
|
+
pools = [],
|
|
172
|
+
limits = PAVED_GROUND_LIMITS,
|
|
173
|
+
} = {}) {
|
|
174
|
+
const cap = { ...PAVED_GROUND_LIMITS, ...limits };
|
|
175
|
+
const ring = Array.from({ length: cap.rings }, (_, i) => {
|
|
176
|
+
const r = rings[i];
|
|
177
|
+
return r ? new THREE.Vector4(r.x, r.z, r.r, r.r / Math.max(1, Math.round(r.r / r.ring))) : new THREE.Vector4();
|
|
178
|
+
});
|
|
179
|
+
const stripA = Array.from({ length: cap.strips }, (_, i) => {
|
|
180
|
+
const s = strips[i];
|
|
181
|
+
return s ? new THREE.Vector4(s.x0, s.z0, s.x1, s.z1) : new THREE.Vector4();
|
|
182
|
+
});
|
|
183
|
+
const stripB = Array.from({ length: cap.strips }, (_, i) => {
|
|
184
|
+
const s = strips[i];
|
|
185
|
+
return s ? new THREE.Vector4(s.len, s.course, s.along === 'z' ? 1 : 0, s.wear ?? 0) : new THREE.Vector4();
|
|
186
|
+
});
|
|
187
|
+
const pool = Array.from({ length: cap.pools }, (_, i) => new THREE.Vector4(...(pools[i] ?? [0, 0, 0, 0])));
|
|
188
|
+
const uniforms = {
|
|
189
|
+
uPaveO: { value: new THREE.Vector2(origin.x, origin.z) },
|
|
190
|
+
uRing: { value: ring },
|
|
191
|
+
uStripA: { value: stripA },
|
|
192
|
+
uStripB: { value: stripB },
|
|
193
|
+
uPools: { value: pool },
|
|
194
|
+
uFill: { value: new THREE.Vector4(cobble, earth, wet, 0) },
|
|
195
|
+
};
|
|
196
|
+
const pavingGLSL = buildPavingGLSL(cap);
|
|
197
|
+
const isDefaultCap =
|
|
198
|
+
cap.rings === PAVED_GROUND_LIMITS.rings && cap.strips === PAVED_GROUND_LIMITS.strips && cap.pools === PAVED_GROUND_LIMITS.pools;
|
|
199
|
+
const cacheKey = isDefaultCap ? 'paved-ground' : `paved-ground:${cap.rings}:${cap.strips}:${cap.pools}`;
|
|
200
|
+
const m = new THREE.MeshStandardMaterial({ vertexColors: true, map: map ?? defaultGrainTexture(), roughness: 0.9, metalness: 0 });
|
|
201
|
+
m.onBeforeCompile = shader => {
|
|
202
|
+
Object.assign(shader.uniforms, uniforms);
|
|
203
|
+
shader.vertexShader = shader.vertexShader
|
|
204
|
+
.replace('#include <common>', '#include <common>\nvarying vec3 vWP;')
|
|
205
|
+
.replace('#include <begin_vertex>', '#include <begin_vertex>\nvWP = (modelMatrix * vec4(position, 1.0)).xyz;');
|
|
206
|
+
shader.fragmentShader = shader.fragmentShader
|
|
207
|
+
.replace(
|
|
208
|
+
'#include <common>',
|
|
209
|
+
`#include <common>\nvarying vec3 vWP; uniform vec2 uPaveO; uniform vec4 uRing[${cap.rings}], uStripA[${cap.strips}], uStripB[${cap.strips}], uPools[${cap.pools}], uFill;\nfloat pvH = 0.0, pvRough = 0.9, pvWet = 0.0, pvPool = 0.0;`,
|
|
210
|
+
)
|
|
211
|
+
.replace('#include <map_fragment>', pavingGLSL)
|
|
212
|
+
.replace('#include <roughnessmap_fragment>', '#include <roughnessmap_fragment>\n roughnessFactor = pvRough;')
|
|
213
|
+
.replace('#include <normal_fragment_maps>', '#include <normal_fragment_maps>\n' + PAVING_BUMP)
|
|
214
|
+
.replace('#include <opaque_fragment>', WET_SHEEN + '\n#include <opaque_fragment>');
|
|
215
|
+
};
|
|
216
|
+
m.customProgramCacheKey = () => cacheKey;
|
|
217
|
+
return m;
|
|
218
|
+
}
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scene binding for paved stone highways and roadside cedar avenues: distance-dissolved near/far
|
|
3
|
+
* flagstone paving, stone milestones and arches, and stretched instanced tree avenues with per-tree
|
|
4
|
+
* LOD dissolve handover.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { GB, geometryView } from '../geometry/builder.js';
|
|
10
|
+
import {
|
|
11
|
+
AVENUE_CEDAR,
|
|
12
|
+
PAVED_ROAD,
|
|
13
|
+
buildAvenueCedar,
|
|
14
|
+
buildFarRoadPaving,
|
|
15
|
+
buildRoadArch,
|
|
16
|
+
buildRoadMilestones,
|
|
17
|
+
buildRoadPaving,
|
|
18
|
+
planRoadAvenue,
|
|
19
|
+
roadLine,
|
|
20
|
+
} from '../geometry/paved-road.js';
|
|
21
|
+
import { TRS } from '../geometry/primitives.js';
|
|
22
|
+
import { createHash, mulberry32 } from '../math/random.js';
|
|
23
|
+
import { TAU } from '../math/scalar.js';
|
|
24
|
+
import { detailLens } from './detail-range.js';
|
|
25
|
+
import { handOverOf, handOverShadow, reachOf, twinMaterial } from './material-lod.js';
|
|
26
|
+
import { createShotCuts } from './shot-cuts.js';
|
|
27
|
+
|
|
28
|
+
const _tone = new THREE.Color();
|
|
29
|
+
const _drift = [1, 1, 1];
|
|
30
|
+
const farPavingMaterials = new WeakMap();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Return a cached far-road paving material derived from `baseMaterial` via `reachOf(..., beyond = true)`
|
|
34
|
+
* with negative polygon offset so the distant strip never z-fights the underlying terrain.
|
|
35
|
+
*
|
|
36
|
+
* @param {import('three').Material} baseMaterial - Base stone material.
|
|
37
|
+
* @param {{ value: number }} reach - Distance uniform in metres at which near paving yields to the far strip.
|
|
38
|
+
* @param {number} [band=30] - Dissolve band depth in metres around `reach`.
|
|
39
|
+
* @returns {import('three').Material}
|
|
40
|
+
*/
|
|
41
|
+
export function farRoadPavingMaterial(baseMaterial, reach, band = 30) {
|
|
42
|
+
let byReach = farPavingMaterials.get(baseMaterial);
|
|
43
|
+
if (!byReach) farPavingMaterials.set(baseMaterial, (byReach = new Map()));
|
|
44
|
+
let byBand = byReach.get(reach);
|
|
45
|
+
if (!byBand) byReach.set(reach, (byBand = new Map()));
|
|
46
|
+
let mat = byBand.get(band);
|
|
47
|
+
if (!mat) {
|
|
48
|
+
mat = reachOf(baseMaterial, reach, true, band);
|
|
49
|
+
mat.polygonOffset = true;
|
|
50
|
+
mat.polygonOffsetFactor = mat.polygonOffsetUnits = -2;
|
|
51
|
+
byBand.set(band, mat);
|
|
52
|
+
}
|
|
53
|
+
return mat;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Create a zero-allocation per-frame updater that dissolves each tree in a roadside avenue smoothly
|
|
58
|
+
* between its near-detail stretch mesh and its single far instanced mesh, snapping immediately on
|
|
59
|
+
* camera cuts and holding steady during scenes.
|
|
60
|
+
*
|
|
61
|
+
* @param {object} options
|
|
62
|
+
* @param {Array<{ x: number, y: number, z: number, jitter: number, closeShot?: boolean }>} options.trees - Avenue tree records.
|
|
63
|
+
* @param {Float32Array} options.shares - Shared per-tree near blend weights (`0` far to `1` near).
|
|
64
|
+
* @param {import('three').InstancedMesh} options.far - Single instanced mesh carrying every far tree.
|
|
65
|
+
* @param {import('three').InstancedMesh[]} options.nearMeshes - Stretches of near instanced meshes reading subarrays of `shares`.
|
|
66
|
+
* @param {import('three').PerspectiveCamera} options.camera - Active scene camera.
|
|
67
|
+
* @param {ReturnType<typeof createShotCuts>} [options.cuts] - Shot-cut detector (created automatically if omitted).
|
|
68
|
+
* @param {() => number} [options.getReach] - Returns the base near-tree distance in metres (default `() => 240`).
|
|
69
|
+
* @param {() => boolean} [options.isPreparing] - Returns true while preparing a view so LODs snap immediately.
|
|
70
|
+
* @param {number} [options.stretch] - Trees per near stretch (defaults to `AVENUE_CEDAR.stretch`).
|
|
71
|
+
* @param {number} [options.hold] - Hysteresis distance in metres (defaults to `AVENUE_CEDAR.hold`).
|
|
72
|
+
* @param {number} [options.handover] - Dissolve duration in seconds (defaults to `AVENUE_CEDAR.handover`).
|
|
73
|
+
* @param {number} [options.closeMinReach=12] - Minimum near reach in metres for trees marked `closeShot`.
|
|
74
|
+
* @param {number} [options.detailFov=68] - Reference vertical FOV in degrees for telephoto reach scaling.
|
|
75
|
+
* @returns {(dt: number) => void} Per-frame update callback.
|
|
76
|
+
*/
|
|
77
|
+
export function createAvenueHandover({
|
|
78
|
+
trees,
|
|
79
|
+
shares,
|
|
80
|
+
far,
|
|
81
|
+
nearMeshes,
|
|
82
|
+
camera,
|
|
83
|
+
cuts,
|
|
84
|
+
getReach = () => 240,
|
|
85
|
+
isPreparing = () => false,
|
|
86
|
+
stretch = AVENUE_CEDAR.stretch,
|
|
87
|
+
hold = AVENUE_CEDAR.hold,
|
|
88
|
+
handover = AVENUE_CEDAR.handover,
|
|
89
|
+
closeMinReach = 12,
|
|
90
|
+
detailFov = 68,
|
|
91
|
+
}) {
|
|
92
|
+
const wanted = new Uint8Array(trees.length);
|
|
93
|
+
const eye = camera.position;
|
|
94
|
+
const shotCuts =
|
|
95
|
+
cuts ??
|
|
96
|
+
createShotCuts({
|
|
97
|
+
camera,
|
|
98
|
+
jumpMetres: AVENUE_CEDAR.cut,
|
|
99
|
+
jumpMetresInScene: 4,
|
|
100
|
+
turnInSceneOnly: true,
|
|
101
|
+
lensInSceneOnly: true,
|
|
102
|
+
});
|
|
103
|
+
return dt => {
|
|
104
|
+
const lens = detailLens(camera, detailFov);
|
|
105
|
+
const reach = getReach() * lens;
|
|
106
|
+
const cut = shotCuts.poll() || isPreparing();
|
|
107
|
+
const frozen = shotCuts.inScene && !cut;
|
|
108
|
+
const step = dt / handover;
|
|
109
|
+
let any = false;
|
|
110
|
+
for (let k = 0; k < nearMeshes.length; k++) {
|
|
111
|
+
const first = k * stretch;
|
|
112
|
+
const end = Math.min(trees.length, first + stretch);
|
|
113
|
+
let moved = false;
|
|
114
|
+
let shown = false;
|
|
115
|
+
for (let i = first; i < end; i++) {
|
|
116
|
+
if (!frozen) {
|
|
117
|
+
const t = trees[i];
|
|
118
|
+
const d = Math.hypot(eye.x - t.x, eye.y - t.y, eye.z - t.z);
|
|
119
|
+
const treeReach = (t.closeShot ? Math.max(closeMinReach, reach) : reach) * t.jitter;
|
|
120
|
+
wanted[i] = d < treeReach + (wanted[i] ? hold : -hold) ? 1 : 0;
|
|
121
|
+
}
|
|
122
|
+
const was = shares[i];
|
|
123
|
+
const now = cut ? wanted[i] : wanted[i] ? Math.min(1, was + step) : Math.max(0, was - step);
|
|
124
|
+
if (now !== was) {
|
|
125
|
+
shares[i] = now;
|
|
126
|
+
moved = true;
|
|
127
|
+
}
|
|
128
|
+
if (now > 0) shown = true;
|
|
129
|
+
}
|
|
130
|
+
const mesh = nearMeshes[k];
|
|
131
|
+
mesh.visible = shown;
|
|
132
|
+
if (moved) {
|
|
133
|
+
mesh.geometry.attributes.aNear.needsUpdate = true;
|
|
134
|
+
any = true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (any) far.geometry.attributes.aNear.needsUpdate = true;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Lay a roadside avenue of instanced cedars along both sides of a paved road line: one far instanced
|
|
143
|
+
* mesh plus range-culled near stretches sharing per-tree `aNear` blend weights, wired to
|
|
144
|
+
* {@link createAvenueHandover}.
|
|
145
|
+
*
|
|
146
|
+
* @param {object} options
|
|
147
|
+
* @param {ReturnType<typeof roadLine>} options.line - Sampled road line.
|
|
148
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler `(x, z) => y` in metres.
|
|
149
|
+
* @param {import('three').Object3D} options.parent - Group receiving the avenue meshes.
|
|
150
|
+
* @param {() => number} [options.random=Math.random] - Repeatable RNG for per-tree scale, yaw and tint.
|
|
151
|
+
* @param {number} options.offset - Lateral distance in metres from road centre to each tree row.
|
|
152
|
+
* @param {number} options.treeSpacing - Longitudinal spacing in metres between trees on one side.
|
|
153
|
+
* @param {import('three').Material} [options.foliageMaterial] - Base vertex-coloured foliage material.
|
|
154
|
+
* @param {number} [options.cedarSeed=319] - Seed for the shared near and far cedar geometry pair.
|
|
155
|
+
* @param {(x: number, z: number, salt: number) => number} [options.hash] - Deterministic 2D hash in `[0, 1)`.
|
|
156
|
+
* @param {(x: number, z: number, bright: number, out: number[]) => void} [options.standColourDrift] - Optional colour drift hook mutating `out`.
|
|
157
|
+
* @param {(x: number, z: number, hx: number, hz: number) => void} [options.addCollider] - Optional trunk collider callback.
|
|
158
|
+
* @param {import('three').PerspectiveCamera} [options.camera] - Active camera (when omitted, all trees stay on far LOD until updated).
|
|
159
|
+
* @param {ReturnType<typeof createShotCuts>} [options.cuts] - Optional shot-cut detector.
|
|
160
|
+
* @param {() => number} [options.getReach] - Base near-tree reach in metres.
|
|
161
|
+
* @param {() => boolean} [options.isPreparing] - Returns true while preparing a scene or view.
|
|
162
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to `PAVED_ROAD`.
|
|
163
|
+
* @param {Partial<typeof AVENUE_CEDAR>} [options.cedarShape] - Overrides to `AVENUE_CEDAR`.
|
|
164
|
+
* @returns {{ trees: Array<object>, hand: (dt: number) => void, far: import('three').InstancedMesh, nearMeshes: import('three').InstancedMesh[] }}
|
|
165
|
+
*/
|
|
166
|
+
export function layRoadAvenue({
|
|
167
|
+
line,
|
|
168
|
+
ground,
|
|
169
|
+
parent,
|
|
170
|
+
random = Math.random,
|
|
171
|
+
offset,
|
|
172
|
+
treeSpacing,
|
|
173
|
+
foliageMaterial,
|
|
174
|
+
cedarSeed = 319,
|
|
175
|
+
hash = createHash(cedarSeed),
|
|
176
|
+
standColourDrift,
|
|
177
|
+
addCollider = () => {},
|
|
178
|
+
camera,
|
|
179
|
+
cuts,
|
|
180
|
+
getReach,
|
|
181
|
+
isPreparing,
|
|
182
|
+
shape,
|
|
183
|
+
cedarShape,
|
|
184
|
+
}) {
|
|
185
|
+
const roadCfg = shape ? { ...PAVED_ROAD, ...shape } : PAVED_ROAD;
|
|
186
|
+
const cedarCfg = cedarShape ? { ...AVENUE_CEDAR, ...cedarShape } : AVENUE_CEDAR;
|
|
187
|
+
const trees = planRoadAvenue(line, ground, offset, treeSpacing, { endMargin: roadCfg.archBack + 12 });
|
|
188
|
+
const baseFoliage =
|
|
189
|
+
foliageMaterial ?? new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.95, flatShading: true, side: THREE.DoubleSide });
|
|
190
|
+
const near = buildAvenueCedar(mulberry32(cedarSeed), false, { shape: cedarCfg });
|
|
191
|
+
const material = twinMaterial(baseFoliage, 'tinted');
|
|
192
|
+
const shares = new Float32Array(trees.length);
|
|
193
|
+
const farGeometry = geometryView(buildAvenueCedar(mulberry32(cedarSeed), true, { shape: cedarCfg }));
|
|
194
|
+
const far = new THREE.InstancedMesh(farGeometry, handOverOf(material, true), trees.length);
|
|
195
|
+
const nearMaterial = handOverOf(material);
|
|
196
|
+
const nearShadow = handOverShadow();
|
|
197
|
+
const nearMeshes = [];
|
|
198
|
+
farGeometry.setAttribute('aNear', new THREE.InstancedBufferAttribute(shares, 1).setUsage(THREE.DynamicDrawUsage));
|
|
199
|
+
trees.forEach((t, i) => {
|
|
200
|
+
const k = 0.92 + random() * 0.2;
|
|
201
|
+
const sx = k * (0.94 + random() * 0.12);
|
|
202
|
+
const sz = k * (0.94 + random() * 0.12);
|
|
203
|
+
const yaw = random() * TAU;
|
|
204
|
+
const coolNeedle = random() >= 0.5;
|
|
205
|
+
const brightDraw = random();
|
|
206
|
+
t.tall = k;
|
|
207
|
+
t.girth = (sx + sz) / 2;
|
|
208
|
+
t.jitter = 0.8 + 0.4 * hash(Math.round(t.x * 9), Math.round(t.z * 9), 457);
|
|
209
|
+
if (standColourDrift) standColourDrift(t.x, t.z, brightDraw, _drift);
|
|
210
|
+
else {
|
|
211
|
+
_drift[0] = _drift[1] = _drift[2] = 1;
|
|
212
|
+
}
|
|
213
|
+
_tone.setHex(coolNeedle ? 0xe0ecd8 : 0xffffff);
|
|
214
|
+
_tone.r *= _drift[0];
|
|
215
|
+
_tone.g *= _drift[1];
|
|
216
|
+
_tone.b *= _drift[2];
|
|
217
|
+
far.setMatrixAt(i, TRS(t.x, t.y - cedarCfg.sink, t.z, sx, k, sz, yaw));
|
|
218
|
+
far.setColorAt(i, _tone);
|
|
219
|
+
addCollider(t.x, t.z, 0.5, 0.5);
|
|
220
|
+
});
|
|
221
|
+
far.customDepthMaterial = handOverShadow(true);
|
|
222
|
+
far.castShadow = far.receiveShadow = true;
|
|
223
|
+
far.computeBoundingSphere();
|
|
224
|
+
parent.add(far);
|
|
225
|
+
for (let first = 0; first < trees.length; first += cedarCfg.stretch) {
|
|
226
|
+
const stretchTrees = trees.slice(first, first + cedarCfg.stretch);
|
|
227
|
+
const nearGeometry = geometryView(near);
|
|
228
|
+
const nearMesh = new THREE.InstancedMesh(nearGeometry, nearMaterial, stretchTrees.length);
|
|
229
|
+
nearGeometry.setAttribute(
|
|
230
|
+
'aNear',
|
|
231
|
+
new THREE.InstancedBufferAttribute(shares.subarray(first, first + stretchTrees.length), 1).setUsage(THREE.DynamicDrawUsage),
|
|
232
|
+
);
|
|
233
|
+
stretchTrees.forEach((_, i) => {
|
|
234
|
+
for (let e = 0; e < 16; e++) nearMesh.instanceMatrix.array[i * 16 + e] = far.instanceMatrix.array[(first + i) * 16 + e];
|
|
235
|
+
far.getColorAt(first + i, _tone);
|
|
236
|
+
nearMesh.setColorAt(i, _tone);
|
|
237
|
+
});
|
|
238
|
+
nearMesh.customDepthMaterial = nearShadow;
|
|
239
|
+
nearMesh.castShadow = nearMesh.receiveShadow = true;
|
|
240
|
+
nearMesh.computeBoundingSphere();
|
|
241
|
+
nearMesh.visible = false;
|
|
242
|
+
parent.add(nearMesh);
|
|
243
|
+
nearMeshes.push(nearMesh);
|
|
244
|
+
}
|
|
245
|
+
const hand = camera
|
|
246
|
+
? createAvenueHandover({
|
|
247
|
+
trees,
|
|
248
|
+
shares,
|
|
249
|
+
far,
|
|
250
|
+
nearMeshes,
|
|
251
|
+
camera,
|
|
252
|
+
cuts,
|
|
253
|
+
getReach,
|
|
254
|
+
isPreparing,
|
|
255
|
+
stretch: cedarCfg.stretch,
|
|
256
|
+
hold: cedarCfg.hold,
|
|
257
|
+
handover: cedarCfg.handover,
|
|
258
|
+
})
|
|
259
|
+
: () => {};
|
|
260
|
+
return { trees, hand, far, nearMeshes };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Lay a complete paved stone road over a terrain: near flagstone courses and raised kerbs handing
|
|
265
|
+
* over by distance to a far paving strip, milestones, an optional spanning stone arch, and an
|
|
266
|
+
* optional roadside avenue of cedars.
|
|
267
|
+
*
|
|
268
|
+
* @param {object} options
|
|
269
|
+
* @param {{ pts: number[][], w: number, trees?: number }} options.road - Road spline points `[[x, z], ...]`, width `w` in metres and optional `trees` spacing in metres.
|
|
270
|
+
* @param {(x: number, z: number) => number} options.ground - Terrain height sampler `(x, z) => y` in metres.
|
|
271
|
+
* @param {import('three').Object3D} options.parent - Group receiving the road meshes.
|
|
272
|
+
* @param {() => number} [options.random=Math.random] - Repeatable RNG for stones, milestones, arch and avenue trees.
|
|
273
|
+
* @param {import('three').Material} [options.stoneMaterial] - Base stone material for paving, milestones and arch.
|
|
274
|
+
* @param {import('three').Material} [options.foliageMaterial] - Base foliage material for avenue cedars.
|
|
275
|
+
* @param {{ value: number }} [options.pavingReach] - Uniform `{ value: metres }` where near flags hand over to the far strip (default `{ value: 220 }`).
|
|
276
|
+
* @param {number} [options.pavingBand=30] - Dissolve band depth in metres around `pavingReach`.
|
|
277
|
+
* @param {boolean} [options.arch=false] - Whether to erect a monumental spanning arch near the road's end.
|
|
278
|
+
* @param {number} [options.avenueOffset] - Lateral offset in metres to each tree row (defaults to `road.w / 2 + kerb + treeOff`).
|
|
279
|
+
* @param {number} [options.cedarSeed=319] - Seed for the avenue cedar geometry pair.
|
|
280
|
+
* @param {(x: number, z: number, salt: number) => number} [options.hash] - Deterministic 2D hash for tree LOD jitter.
|
|
281
|
+
* @param {(x: number, z: number, bright: number, out: number[]) => void} [options.standColourDrift] - Optional colour drift hook for avenue trees.
|
|
282
|
+
* @param {(x: number, z: number, hx: number, hz: number, rot?: number) => void} [options.addCollider] - Optional collider callback for arch piers and tree trunks.
|
|
283
|
+
* @param {import('three').PerspectiveCamera} [options.camera] - Active camera for avenue LOD handover.
|
|
284
|
+
* @param {ReturnType<typeof createShotCuts>} [options.cuts] - Optional shot-cut detector for avenue handover.
|
|
285
|
+
* @param {() => number} [options.getReach] - Base near-tree reach in metres.
|
|
286
|
+
* @param {() => boolean} [options.isPreparing] - Returns true while preparing a scene or view.
|
|
287
|
+
* @param {typeof import('../geometry/builder.js').GB} [options.Builder] - Geometry builder class.
|
|
288
|
+
* @param {Partial<typeof PAVED_ROAD>} [options.shape] - Overrides to `PAVED_ROAD`.
|
|
289
|
+
* @param {Partial<typeof AVENUE_CEDAR>} [options.cedarShape] - Overrides to `AVENUE_CEDAR`.
|
|
290
|
+
* @returns {{ line: ReturnType<typeof roadLine>, avenueOffset: number, trees: Array<object> | null, hand: ((dt: number) => void) | null }}
|
|
291
|
+
*/
|
|
292
|
+
export function layPavedRoad({
|
|
293
|
+
road,
|
|
294
|
+
ground,
|
|
295
|
+
parent,
|
|
296
|
+
random = Math.random,
|
|
297
|
+
stoneMaterial,
|
|
298
|
+
foliageMaterial,
|
|
299
|
+
pavingReach = { value: 220 },
|
|
300
|
+
pavingBand = 30,
|
|
301
|
+
arch = false,
|
|
302
|
+
avenueOffset,
|
|
303
|
+
cedarSeed = 319,
|
|
304
|
+
hash,
|
|
305
|
+
standColourDrift,
|
|
306
|
+
addCollider = () => {},
|
|
307
|
+
camera,
|
|
308
|
+
cuts,
|
|
309
|
+
getReach,
|
|
310
|
+
isPreparing,
|
|
311
|
+
Builder,
|
|
312
|
+
shape,
|
|
313
|
+
cedarShape,
|
|
314
|
+
}) {
|
|
315
|
+
const cfg = shape ? { ...PAVED_ROAD, ...shape } : PAVED_ROAD;
|
|
316
|
+
const BuilderClass = Builder ?? GB;
|
|
317
|
+
const stone = stoneMaterial ?? new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.92, metalness: 0 });
|
|
318
|
+
const line = roadLine(ground, road.pts, cfg.spacing);
|
|
319
|
+
const paving = new BuilderClass();
|
|
320
|
+
const works = new BuilderClass();
|
|
321
|
+
buildRoadPaving(paving, random, line, road.w, { shape: cfg });
|
|
322
|
+
buildRoadMilestones(works, random, line, road.w, { shape: cfg });
|
|
323
|
+
if (arch) buildRoadArch(works, random, line, road.w, { addCollider, shape: cfg });
|
|
324
|
+
paving.mesh(reachOf(stone, pavingReach, false, pavingBand), parent);
|
|
325
|
+
buildFarRoadPaving(line, road.w, { Builder: BuilderClass, shape: cfg }).mesh(
|
|
326
|
+
farRoadPavingMaterial(stone, pavingReach, pavingBand),
|
|
327
|
+
parent,
|
|
328
|
+
false,
|
|
329
|
+
);
|
|
330
|
+
works.mesh(stone, parent);
|
|
331
|
+
const offset = avenueOffset ?? road.w / 2 + cfg.kerb + cfg.treeOff;
|
|
332
|
+
if (!road.trees) return { line, avenueOffset: offset, trees: null, hand: null };
|
|
333
|
+
const { trees, hand } = layRoadAvenue({
|
|
334
|
+
line,
|
|
335
|
+
ground,
|
|
336
|
+
parent,
|
|
337
|
+
random,
|
|
338
|
+
offset,
|
|
339
|
+
treeSpacing: road.trees,
|
|
340
|
+
foliageMaterial,
|
|
341
|
+
cedarSeed,
|
|
342
|
+
hash,
|
|
343
|
+
standColourDrift,
|
|
344
|
+
addCollider,
|
|
345
|
+
camera,
|
|
346
|
+
cuts,
|
|
347
|
+
getReach,
|
|
348
|
+
isPreparing,
|
|
349
|
+
shape: cfg,
|
|
350
|
+
cedarShape,
|
|
351
|
+
});
|
|
352
|
+
return { line, avenueOffset: offset, trees, hand };
|
|
353
|
+
}
|
|
@@ -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
|
+
}
|