roamwright 0.61.16 → 0.61.18
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 +25 -24
- package/examples/land-features.js +130 -9
- package/examples/land.html +2 -1
- package/examples/land.js +17 -2
- package/examples/ruins.html +3 -2
- package/examples/ruins.js +184 -37
- package/examples/world-map.html +162 -0
- package/examples/world-map.js +475 -0
- package/geometry/farm-props.js +59 -0
- package/geometry/field-map.js +147 -0
- package/geometry/instances.js +40 -0
- package/geometry/plant-tuft.js +138 -0
- package/geometry/ruins-breach.js +329 -0
- package/geometry/ruins-crater.js +269 -0
- package/geometry/ruins-masonry.js +323 -0
- package/geometry/ruins-walls.js +272 -0
- package/geometry/strata-mesa.js +114 -0
- package/geometry/strata-rocks.js +73 -2
- package/index.js +109 -1
- package/package.json +1 -1
- package/rendering/contact-shadows.js +56 -0
- package/rendering/crowd-lifecycle.js +80 -0
- package/rendering/farm-plough.js +145 -0
- package/rendering/map-glyphs.js +494 -0
- package/rendering/map-input.js +143 -0
- package/rendering/map-labels.js +181 -0
- package/rendering/map-relief.js +330 -0
- package/rendering/map-signs.js +124 -0
- package/rendering/place-names.js +408 -0
- package/rendering/plant-tuft.js +190 -0
- package/rendering/portrait-studio.js +213 -0
- package/rendering/ruins-fittings.js +199 -0
- package/rendering/ruins-mound.js +103 -0
- package/rendering/screen-pick.js +75 -0
- package/runtime/build-freshness.js +133 -0
- package/runtime/compass-strip.js +232 -0
- package/runtime/context-lost.js +108 -0
- package/runtime/doorways.js +98 -0
- package/runtime/hud-frame.js +74 -0
- package/runtime/map-relief.js +236 -0
- package/runtime/map-spot.js +120 -0
- package/runtime/map-view.js +229 -0
- package/runtime/panel-shell.js +90 -0
- package/runtime/relation-graph.js +163 -0
- package/runtime/stage-frame.js +263 -0
- package/runtime/url-router.js +162 -0
- package/runtime/way-back.js +71 -0
- package/runtime/whereabouts.js +210 -0
- package/runtime/world-controls.js +265 -0
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Floating 3D world-space place-name sprites with staggered canvas creation and next-frame GPU
|
|
3
|
+
* texture upload (preventing first-frame hitches), priority + distance sorting with zero per-frame
|
|
4
|
+
* allocations, HUD-margin and other-label avoidance, terrain/skyline occlusion, and smooth opacity fading.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import * as THREE from 'three';
|
|
8
|
+
import { clamp, sstep } from '../math/scalar.js';
|
|
9
|
+
import { DEFAULT_MAP_FONT } from './map-glyphs.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Default `[maxRange, minRange, heightAboveGround, scale, priority]` tuples for open-land place kinds.
|
|
13
|
+
* @type {Readonly<Record<string, readonly [number, number, number, number, number]>>}
|
|
14
|
+
*/
|
|
15
|
+
export const DEFAULT_PLACE_TAG_STYLES = Object.freeze({
|
|
16
|
+
city: [6000, 150, 78, 1.2, 0],
|
|
17
|
+
region: [760, 240, 110, 1.1, 1],
|
|
18
|
+
landmark: [260, 34, 30, 0.95, 2],
|
|
19
|
+
district: [230, 60, 40, 0.9, 3],
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default `[maxRange, minRange, heightAboveGround, scale, priority]` tuples inside compact spaces.
|
|
24
|
+
* @type {Readonly<Record<string, readonly [number, number, number, number, number]>>}
|
|
25
|
+
*/
|
|
26
|
+
export const DEFAULT_SMALL_TAG_STYLES = Object.freeze({
|
|
27
|
+
landmark: [150, 4, 7, 0.9, 2],
|
|
28
|
+
district: [170, 22, 13, 0.9, 3],
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const HALO = 'rgba(4,6,8,.9)';
|
|
32
|
+
const TEXT_W = 468;
|
|
33
|
+
|
|
34
|
+
function edged(ctx, stroke) {
|
|
35
|
+
ctx.shadowBlur = 22;
|
|
36
|
+
ctx.strokeStyle = 'rgba(4,6,8,.7)';
|
|
37
|
+
ctx.lineWidth = 18;
|
|
38
|
+
stroke();
|
|
39
|
+
ctx.shadowBlur = 4;
|
|
40
|
+
ctx.strokeStyle = 'rgba(4,6,8,1)';
|
|
41
|
+
ctx.lineWidth = 6;
|
|
42
|
+
stroke();
|
|
43
|
+
ctx.shadowBlur = 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function drawDoorwayMark(ctx, x, y) {
|
|
47
|
+
const trace = () => {
|
|
48
|
+
ctx.beginPath();
|
|
49
|
+
ctx.moveTo(x - 9, y + 15);
|
|
50
|
+
ctx.lineTo(x - 9, y - 4);
|
|
51
|
+
ctx.arc(x, y - 4, 9, Math.PI, 0);
|
|
52
|
+
ctx.lineTo(x + 9, y + 15);
|
|
53
|
+
};
|
|
54
|
+
edged(ctx, () => {
|
|
55
|
+
trace();
|
|
56
|
+
ctx.stroke();
|
|
57
|
+
});
|
|
58
|
+
ctx.strokeStyle = '#ffc27a';
|
|
59
|
+
ctx.lineWidth = 3.5;
|
|
60
|
+
trace();
|
|
61
|
+
ctx.stroke();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const goesAfter = (a, b) => a.pr > b.pr || (a.pr === b.pr && a.d > b.d);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a world-space place-name sprite controller.
|
|
68
|
+
*
|
|
69
|
+
* @param {object} options
|
|
70
|
+
* @param {THREE.PerspectiveCamera} options.camera Active world camera.
|
|
71
|
+
* @param {THREE.WebGLRenderer} options.renderer Active WebGL renderer (for next-frame `initTexture`).
|
|
72
|
+
* @param {() => ReadonlyArray<object>} options.getLocations Returns all world locations.
|
|
73
|
+
* @param {() => object} options.getSpace Returns the active space (`{ id, flyable, ground(x, z), origin }`).
|
|
74
|
+
* @param {() => Record<string, object>} options.getSpaces Returns the map of all spaces by id.
|
|
75
|
+
* @param {(id: string) => boolean} options.isBuilt Returns true when space `id` is built.
|
|
76
|
+
* @param {() => number} [options.getNight] Night factor `0..1` (0 = full day, 1 = night).
|
|
77
|
+
* @param {() => boolean} options.isShown Returns true when place names should be visible.
|
|
78
|
+
* @param {() => { width: number, height: number }} options.getViewport Live stage viewport in CSS pixels.
|
|
79
|
+
* @param {() => { top: number, rightLeft: number, rightBottom: number }} options.getHudBounds HUD exclusion bounds in CSS pixels.
|
|
80
|
+
* @param {(x0: number, y0: number, x1: number, y1: number) => boolean} [options.isBlocked] Extra screen-space occlusion test (page pixels: left, top, right, bottom).
|
|
81
|
+
* @param {(x: number, z: number, space: object) => number} [options.skylineBoost] Extra roofline height in metres along sight lines (e.g. inside a city).
|
|
82
|
+
* @param {(sprite: THREE.Sprite) => void} [options.onSpriteCreated] Called when a sprite is created (for example to attach it to an overlay scene).
|
|
83
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
84
|
+
* @param {Record<string, readonly [number, number, number, number, number]>} [options.tagStyles=DEFAULT_PLACE_TAG_STYLES]
|
|
85
|
+
* @param {Record<string, readonly [number, number, number, number, number]>} [options.smallTagStyles=DEFAULT_SMALL_TAG_STYLES]
|
|
86
|
+
* @param {string} [options.defaultSpaceId='world']
|
|
87
|
+
* @param {number} [options.maxVisible=6] Maximum simultaneous visible names.
|
|
88
|
+
* @param {number} [options.tagsPerFrame=4] New tag sprites built per frame during warm-up.
|
|
89
|
+
* @param {number} [options.nameHeight=0.036] Sprite height as a share of unit frustum height.
|
|
90
|
+
* @param {number} [options.nameMinPx=28] Minimum height in CSS pixels.
|
|
91
|
+
* @param {number} [options.dayLift=0.7] Luminance boost by day so white lettering reads against a bright sky.
|
|
92
|
+
* @param {number} [options.hudLeftPx=120] Left HUD margin in CSS pixels.
|
|
93
|
+
* @param {object} [options.host=globalThis]
|
|
94
|
+
* @returns {{ tags: Array<object>, update: (dt: number) => void, refresh: () => void, dispose: () => void }}
|
|
95
|
+
*/
|
|
96
|
+
export function createPlaceNames({
|
|
97
|
+
camera,
|
|
98
|
+
renderer,
|
|
99
|
+
getLocations,
|
|
100
|
+
getSpace,
|
|
101
|
+
getSpaces,
|
|
102
|
+
isBuilt,
|
|
103
|
+
getNight = () => 0,
|
|
104
|
+
isShown,
|
|
105
|
+
getViewport,
|
|
106
|
+
getHudBounds,
|
|
107
|
+
isBlocked = () => false,
|
|
108
|
+
skylineBoost = () => 0,
|
|
109
|
+
onSpriteCreated = () => {},
|
|
110
|
+
font = DEFAULT_MAP_FONT,
|
|
111
|
+
tagStyles = DEFAULT_PLACE_TAG_STYLES,
|
|
112
|
+
smallTagStyles = DEFAULT_SMALL_TAG_STYLES,
|
|
113
|
+
defaultSpaceId = 'world',
|
|
114
|
+
maxVisible = 6,
|
|
115
|
+
tagsPerFrame = 4,
|
|
116
|
+
nameHeight = 0.036,
|
|
117
|
+
nameMinPx = 28,
|
|
118
|
+
dayLift = 0.7,
|
|
119
|
+
hudLeftPx = 120,
|
|
120
|
+
host = globalThis,
|
|
121
|
+
}) {
|
|
122
|
+
const _v = new THREE.Vector3();
|
|
123
|
+
const tags = [];
|
|
124
|
+
const tagBox = new Float32Array(maxVisible * 2);
|
|
125
|
+
const named = new Set();
|
|
126
|
+
let measure = null;
|
|
127
|
+
let drawnLast = null;
|
|
128
|
+
let unnamed = [];
|
|
129
|
+
let listed = false;
|
|
130
|
+
|
|
131
|
+
function tagText(loc) {
|
|
132
|
+
const cap = loc.kind === 'region' || loc.kind === 'city';
|
|
133
|
+
const text = cap ? loc.name.toUpperCase().split('').join('\u200a') : loc.name;
|
|
134
|
+
const f = (loc.kind === 'district' ? 'italic ' : '') + (cap ? '400 38px ' : '400 44px ') + font;
|
|
135
|
+
measure ??= host.document.createElement('canvas').getContext('2d');
|
|
136
|
+
measure.font = f;
|
|
137
|
+
return { text, font: f, w: measure.measureText(text).width };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function tagKind(loc, small) {
|
|
141
|
+
if (!small) {
|
|
142
|
+
const T = tagStyles[loc.kind];
|
|
143
|
+
return loc.kind === 'landmark' && loc.h ? [T[0], T[1], loc.h, T[3], T[4]] : T;
|
|
144
|
+
}
|
|
145
|
+
const [range, nearest, h, size, pr] = smallTagStyles[loc.kind];
|
|
146
|
+
return [loc.range ?? range, nearest, loc.h ?? h, size, pr];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function nameable(loc) {
|
|
150
|
+
const landId = loc.space ?? defaultSpaceId;
|
|
151
|
+
if (!isBuilt(landId)) return false;
|
|
152
|
+
const own = getSpaces()[landId];
|
|
153
|
+
return own.flyable ? !!(tagStyles[loc.kind] && loc.x !== undefined) : !!(smallTagStyles[loc.kind] && loc.off);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function drawTag(t) {
|
|
157
|
+
const loc = t.loc;
|
|
158
|
+
const c = t.sp.material.map.image;
|
|
159
|
+
const g = c.getContext('2d');
|
|
160
|
+
const { text, font: f, w } = tagText(loc);
|
|
161
|
+
g.textAlign = 'center';
|
|
162
|
+
g.textBaseline = 'middle';
|
|
163
|
+
g.lineJoin = 'round';
|
|
164
|
+
g.shadowColor = HALO;
|
|
165
|
+
g.font = f;
|
|
166
|
+
g.save();
|
|
167
|
+
g.translate(256, 44);
|
|
168
|
+
if (w > TEXT_W) g.scale(TEXT_W / w, 1);
|
|
169
|
+
edged(g, () => g.strokeText(text, 0, 0));
|
|
170
|
+
g.fillStyle = loc.kind === 'landmark' ? '#f3ecd9' : loc.kind === 'district' ? '#cfe9e2' : '#ffffff';
|
|
171
|
+
g.fillText(text, 0, 0);
|
|
172
|
+
g.restore();
|
|
173
|
+
if (loc.enter) drawDoorwayMark(g, Math.max(18, 256 - Math.min(w, TEXT_W) / 2 - 26), 44);
|
|
174
|
+
if (loc.kind === 'landmark') {
|
|
175
|
+
edged(g, () => g.strokeRect(256 - 1.5, 74, 3, 18));
|
|
176
|
+
g.fillStyle = '#62e6cb';
|
|
177
|
+
g.fillRect(256 - 1.5, 74, 3, 18);
|
|
178
|
+
}
|
|
179
|
+
t.sp.material.map.needsUpdate = true;
|
|
180
|
+
t.drawn = true;
|
|
181
|
+
drawnLast = t;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function toCard() {
|
|
185
|
+
if (!drawnLast) return;
|
|
186
|
+
renderer.initTexture(drawnLast.sp.material.map);
|
|
187
|
+
drawnLast.onCard = true;
|
|
188
|
+
drawnLast = null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function makeTag(loc) {
|
|
192
|
+
const land = loc.space ?? defaultSpaceId;
|
|
193
|
+
const own = getSpaces()[land];
|
|
194
|
+
const small = !own.flyable;
|
|
195
|
+
const T = tagKind(loc, small);
|
|
196
|
+
const c = host.document.createElement('canvas');
|
|
197
|
+
c.width = 512;
|
|
198
|
+
c.height = 96;
|
|
199
|
+
loc.tw = Math.min(1, tagText(loc).w / TEXT_W);
|
|
200
|
+
const tex = new THREE.CanvasTexture(c);
|
|
201
|
+
tex.colorSpace = THREE.SRGBColorSpace;
|
|
202
|
+
tex.anisotropy = 4;
|
|
203
|
+
const sp = new THREE.Sprite(
|
|
204
|
+
new THREE.SpriteMaterial({
|
|
205
|
+
map: tex,
|
|
206
|
+
transparent: true,
|
|
207
|
+
depthWrite: false,
|
|
208
|
+
depthTest: small,
|
|
209
|
+
fog: false,
|
|
210
|
+
opacity: 0,
|
|
211
|
+
sizeAttenuation: false,
|
|
212
|
+
}),
|
|
213
|
+
);
|
|
214
|
+
sp.center.set(0.5, 0);
|
|
215
|
+
sp.scale.set((nameHeight * T[3] * 512) / 96, nameHeight * T[3], 1);
|
|
216
|
+
sp.renderOrder = 19;
|
|
217
|
+
sp.visible = false;
|
|
218
|
+
onSpriteCreated(sp);
|
|
219
|
+
const x = small ? own.origin.x + loc.off[0] : loc.x;
|
|
220
|
+
const z = small ? own.origin.z + loc.off[1] : loc.z;
|
|
221
|
+
return {
|
|
222
|
+
loc,
|
|
223
|
+
sp,
|
|
224
|
+
land,
|
|
225
|
+
T,
|
|
226
|
+
x,
|
|
227
|
+
z,
|
|
228
|
+
a: 0,
|
|
229
|
+
want: 0,
|
|
230
|
+
d: 0,
|
|
231
|
+
tw: loc.tw,
|
|
232
|
+
y: own.ground(x, z) + T[2],
|
|
233
|
+
pr: T[4],
|
|
234
|
+
drawn: false,
|
|
235
|
+
onCard: false,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function isHidden(t, sp) {
|
|
240
|
+
const cx = camera.position.x;
|
|
241
|
+
const cy = camera.position.y;
|
|
242
|
+
const cz = camera.position.z;
|
|
243
|
+
const tx = t.x;
|
|
244
|
+
const tz = t.z;
|
|
245
|
+
const ty = t.y - t.T[2] + 6;
|
|
246
|
+
const dx = tx - cx;
|
|
247
|
+
const dz = tz - cz;
|
|
248
|
+
const L = Math.sqrt(dx * dx + dz * dz);
|
|
249
|
+
const n = Math.min(24, Math.ceil(L / 25));
|
|
250
|
+
for (let i = 1; i < n; i++) {
|
|
251
|
+
const u = i / n;
|
|
252
|
+
if (u * L > L - 45) break;
|
|
253
|
+
const x = cx + (tx - cx) * u;
|
|
254
|
+
const z = cz + (tz - cz) * u;
|
|
255
|
+
const sky = sp.ground(x, z) + skylineBoost(x, z, sp);
|
|
256
|
+
if (cy + (ty - cy) * u < sky) return true;
|
|
257
|
+
}
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function listUnnamed() {
|
|
262
|
+
const cx = camera.position.x;
|
|
263
|
+
const cz = camera.position.z;
|
|
264
|
+
const far = loc => Math.hypot((loc.x ?? cx) - cx, (loc.z ?? cz) - cz);
|
|
265
|
+
unnamed = getLocations()
|
|
266
|
+
.filter(loc => !named.has(loc) && nameable(loc))
|
|
267
|
+
.sort((a, b) => far(b) - far(a));
|
|
268
|
+
listed = true;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function addTags() {
|
|
272
|
+
for (let k = 0; k < tagsPerFrame && unnamed.length; k++) {
|
|
273
|
+
const loc = unnamed.pop();
|
|
274
|
+
named.add(loc);
|
|
275
|
+
tags.push(makeTag(loc));
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function sortTags() {
|
|
280
|
+
for (let i = 1; i < tags.length; i++) {
|
|
281
|
+
const t = tags[i];
|
|
282
|
+
let j = i - 1;
|
|
283
|
+
for (; j >= 0 && goesAfter(tags[j], t); j--) tags[j + 1] = tags[j];
|
|
284
|
+
tags[j + 1] = t;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function update(dt) {
|
|
289
|
+
const show = isShown();
|
|
290
|
+
if (!listed) {
|
|
291
|
+
if (!show) return;
|
|
292
|
+
listUnnamed();
|
|
293
|
+
}
|
|
294
|
+
if (unnamed.length) addTags();
|
|
295
|
+
const sp = getSpace();
|
|
296
|
+
const cx = camera.position.x;
|
|
297
|
+
const cy = camera.position.y;
|
|
298
|
+
const cz = camera.position.z;
|
|
299
|
+
const asp = camera.aspect;
|
|
300
|
+
const vp = getViewport();
|
|
301
|
+
const hud = getHudBounds();
|
|
302
|
+
const halfW = vp.width / 2;
|
|
303
|
+
const halfH = vp.height / 2;
|
|
304
|
+
const hudTop = 1 - (2 * hud.top) / vp.height;
|
|
305
|
+
const hudLeft = -1 + (2 * hudLeftPx) / vp.width;
|
|
306
|
+
const hudRight = -1 + (2 * hud.rightLeft) / vp.width;
|
|
307
|
+
const hudRightBottom = 1 - (2 * hud.rightBottom) / vp.height;
|
|
308
|
+
const lens = 2 * Math.tan((camera.fov * Math.PI) / 360);
|
|
309
|
+
const grow = Math.max(1, nameMinPx / ((nameHeight / lens) * vp.height));
|
|
310
|
+
toCard();
|
|
311
|
+
let n = 0;
|
|
312
|
+
let drawnNow = false;
|
|
313
|
+
for (let i = 0; i < tags.length; i++) {
|
|
314
|
+
const t = tags[i];
|
|
315
|
+
const T = t.T;
|
|
316
|
+
const dx = t.x - cx;
|
|
317
|
+
const dy = t.y - cy;
|
|
318
|
+
const dz = t.z - cz;
|
|
319
|
+
const d = (t.d = Math.sqrt(dx * dx + dy * dy + dz * dz));
|
|
320
|
+
t.want = show && t.land === sp.id ? sstep(T[1] * 0.6, T[1], d) * (1 - sstep(T[0] * 0.72, T[0], d)) : 0;
|
|
321
|
+
}
|
|
322
|
+
if (show) {
|
|
323
|
+
sortTags();
|
|
324
|
+
for (let i = 0; i < tags.length; i++) {
|
|
325
|
+
const t = tags[i];
|
|
326
|
+
if (t.want < 0.02) continue;
|
|
327
|
+
_v.set(t.x, t.y, t.z).project(camera);
|
|
328
|
+
const tall = (2 * nameHeight * t.T[3] * grow) / lens;
|
|
329
|
+
const half = (nameHeight * t.T[3] * grow * t.tw * TEXT_W) / 96 / (lens * asp);
|
|
330
|
+
if (
|
|
331
|
+
_v.z > 1 ||
|
|
332
|
+
Math.abs(_v.x) > 1.1 ||
|
|
333
|
+
Math.abs(_v.y) > 1.1 ||
|
|
334
|
+
_v.y + tall > hudTop ||
|
|
335
|
+
(_v.x < hudLeft && _v.y > -0.25) ||
|
|
336
|
+
(_v.x + half > hudRight && _v.y + tall > hudRightBottom)
|
|
337
|
+
) {
|
|
338
|
+
t.want = 0;
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (t.pr >= 2 && t.d > 120 && isHidden(t, sp)) {
|
|
342
|
+
t.want = 0;
|
|
343
|
+
continue;
|
|
344
|
+
}
|
|
345
|
+
const bx0 = (_v.x - half + 1) * halfW;
|
|
346
|
+
const by0 = (1 - (_v.y + tall)) * halfH;
|
|
347
|
+
const bx1 = (_v.x + half + 1) * halfW;
|
|
348
|
+
const by1 = (1 - _v.y) * halfH;
|
|
349
|
+
let ok = n < maxVisible && !isBlocked(bx0, by0, bx1, by1);
|
|
350
|
+
for (let j = 0; j < n && ok; j++) {
|
|
351
|
+
if (Math.abs(tagBox[j * 2] - _v.x) * Math.max(asp, 0.9) < 0.36 * grow && Math.abs(tagBox[j * 2 + 1] - _v.y) < 0.11 * grow) {
|
|
352
|
+
ok = false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (!ok) {
|
|
356
|
+
t.want = 0;
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
tagBox[n * 2] = _v.x;
|
|
360
|
+
tagBox[n * 2 + 1] = _v.y;
|
|
361
|
+
n++;
|
|
362
|
+
if (!t.onCard) {
|
|
363
|
+
t.want = 0;
|
|
364
|
+
if (!t.drawn && !drawnNow) {
|
|
365
|
+
drawTag(t);
|
|
366
|
+
drawnNow = true;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const lift = 1 + dayLift * (1 - getNight());
|
|
372
|
+
const k = Math.min(1, dt * 5);
|
|
373
|
+
const fit = clamp(asp / 0.95, 0.72, 1);
|
|
374
|
+
for (let i = 0; i < tags.length; i++) {
|
|
375
|
+
const t = tags[i];
|
|
376
|
+
t.a += (t.want - t.a) * k;
|
|
377
|
+
const vis = t.a > 0.02;
|
|
378
|
+
if (t.sp.visible !== vis) t.sp.visible = vis;
|
|
379
|
+
if (vis) {
|
|
380
|
+
const h = nameHeight * t.T[3] * fit * grow;
|
|
381
|
+
t.sp.material.opacity = t.a;
|
|
382
|
+
t.sp.material.color.setScalar(lift);
|
|
383
|
+
t.sp.position.set(t.x, t.y, t.z);
|
|
384
|
+
t.sp.scale.set((h * 512) / 96, h, 1);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return {
|
|
390
|
+
tags,
|
|
391
|
+
update,
|
|
392
|
+
refresh() {
|
|
393
|
+
if (listed) listUnnamed();
|
|
394
|
+
},
|
|
395
|
+
dispose() {
|
|
396
|
+
for (let i = 0; i < tags.length; i++) {
|
|
397
|
+
tags[i].sp.material.map?.dispose();
|
|
398
|
+
tags[i].sp.material.dispose();
|
|
399
|
+
tags[i].sp.parent?.remove(tags[i].sp);
|
|
400
|
+
}
|
|
401
|
+
tags.length = 0;
|
|
402
|
+
named.clear();
|
|
403
|
+
unnamed.length = 0;
|
|
404
|
+
drawnLast = null;
|
|
405
|
+
listed = false;
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flowering and wind-swaying grass tuft material and pressed-cover wiring: opens flower petals and
|
|
3
|
+
* grain ears from `aFlower` instance attributes, sways blades with `WIND_SWAY_GLSL`, presses grass
|
|
4
|
+
* flat inside active scene discs, and fades tufts smoothly at single- or multi-ring patch edges.
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as THREE from 'three';
|
|
9
|
+
import { DEFAULT_FLOWER_LOOKS, FLOWER_KINDS, TUFT_SHAPE, tuftGeometry as buildTuftGeometry } from '../geometry/plant-tuft.js';
|
|
10
|
+
import { WIND_SWAY_GLSL } from '../motion/wind.js';
|
|
11
|
+
import { clearInstancesInDiscs, createPressedDiscs, pressedDiscsPars, pressedStandingGLSL } from './pressed-cover.js';
|
|
12
|
+
import { thinSurfaceLight } from './surface-light.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Default shader tuning constants for {@link createTuftMaterial} and {@link createGrassTuft}.
|
|
16
|
+
*/
|
|
17
|
+
export const TUFT_SHADER = Object.freeze({
|
|
18
|
+
/** Narrowed stem width as a fraction of the blade it came from. */
|
|
19
|
+
stemNarrow: 0.3,
|
|
20
|
+
/** Camera distance in metres within which flower heads keep true world size. */
|
|
21
|
+
headTrue: 5,
|
|
22
|
+
/** Distance span in metres over which flower heads double in apparent size. */
|
|
23
|
+
headGrow: 8,
|
|
24
|
+
/** Maximum apparent flower-head size multiplier at distance. */
|
|
25
|
+
headMost: 2.5,
|
|
26
|
+
/** Forward-scattering backlit glow strength against a low sun. */
|
|
27
|
+
glow: 1.1,
|
|
28
|
+
/** Default pressed-disc clearing radii for low cover shrubs (`clearInstancesInDiscs`). */
|
|
29
|
+
cleared: Object.freeze({ fromRadii: 1.2, toRadii: 1.7 }),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
function buildFlowerUniforms(looks, tallest) {
|
|
33
|
+
const table = (fn, size) => looks.map(fn).map(a => (size === 3 ? new THREE.Vector3(...a) : new THREE.Vector4(...a)));
|
|
34
|
+
return {
|
|
35
|
+
uFlowerA: { value: table(k => [k.stem, k.r, k.cup, k.wide], 4) },
|
|
36
|
+
uFlowerB: { value: table(k => [k.petals, k.spread, k.nod, k.over * tallest], 4) },
|
|
37
|
+
uHeart: { value: table(k => k.heart, 3) },
|
|
38
|
+
uRim: { value: table(k => k.tip, 3) },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildFlowerVertexGLSL(cfg) {
|
|
43
|
+
return /* glsl */ `
|
|
44
|
+
float fKind = floor(aFlower * 0.25 + 0.01), fHeads = aFlower - fKind * 4.0;
|
|
45
|
+
if (fKind > 0.5 && aStem.x > 0.5 && aStem.x < fHeads + 0.5) {
|
|
46
|
+
int k = int(fKind) - 1;
|
|
47
|
+
vec4 fa = uFlowerA[k], fb = uFlowerB[k];
|
|
48
|
+
float sx = length(instanceMatrix[0].xyz), sy = length(instanceMatrix[1].xyz);
|
|
49
|
+
float fh = fract(sin(dot(instanceMatrix[3].xz, vec2(12.9898, 78.233)) + aStem.x * 7.31) * 43758.5453);
|
|
50
|
+
float seen = 1.0 + clamp((length(ip - uCam) - ${cfg.headTrue.toFixed(1)}) / ${cfg.headGrow.toFixed(1)}, 0.0, ${(cfg.headMost - 1).toFixed(1)});
|
|
51
|
+
float lift = max(fa.x / sy, fb.w) * (0.92 + 0.16 * fh) / aStem.y;
|
|
52
|
+
if (aPetal.w < 0.5) {
|
|
53
|
+
transformed.xz = mix(aStem.zw, transformed.xz, ${cfg.stemNarrow.toFixed(2)});
|
|
54
|
+
transformed.y *= lift;
|
|
55
|
+
} else {
|
|
56
|
+
vec2 lean = transformed.xz - aStem.zw; lean /= max(length(lean), 1e-4);
|
|
57
|
+
vec3 up = normalize(vec3(lean.x * fb.z, 1.0, lean.y * fb.z)),
|
|
58
|
+
ax = normalize(cross(up, vec3(0.0, 0.0, 1.0))), bx = cross(up, ax);
|
|
59
|
+
float ang = aPetal.x * (fb.y > 0.0 ? 2.4 : 6.2832 / fb.x) + fh * 6.2832,
|
|
60
|
+
r = aPetal.x < fb.x ? fa.y * (0.85 + 0.3 * fh) : 0.0;
|
|
61
|
+
vec3 dir = cos(ang) * ax + sin(ang) * bx,
|
|
62
|
+
off = r * aPetal.y * (dir * cos(fa.z) + up * sin(fa.z)) + cross(up, dir) * aPetal.z * fa.w * r - up * aPetal.x * fb.y;
|
|
63
|
+
transformed = vec3(transformed.x, transformed.y * lift, transformed.z) + off * seen / vec3(sx, sy, sx);
|
|
64
|
+
vColor.rgb = mix(uHeart[k], uRim[k], smoothstep(0.12, 0.8, aPetal.y)) * (0.85 + 0.3 * fh);
|
|
65
|
+
vTip = 0.33;
|
|
66
|
+
}
|
|
67
|
+
}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Create a flowering, wind-swaying, trodden-aware grass tuft `MeshStandardMaterial` compatible with
|
|
72
|
+
* `createTuftPatch`.
|
|
73
|
+
*
|
|
74
|
+
* @param {{ value: THREE.Vector3 }} uCam - Patch centre / camera position uniform.
|
|
75
|
+
* @param {{ value: number }} uR - Single-ring patch outer radius uniform in metres.
|
|
76
|
+
* @param {boolean} [ringed=false] - When true, reads per-instance `aReach` `[inner, outer]` fade radii.
|
|
77
|
+
* @param {object} [options]
|
|
78
|
+
* @param {{ value: number }} [options.uTime] - Elapsed clock uniform in seconds.
|
|
79
|
+
* @param {{ value: number }} [options.uSunLight] - Sun intensity uniform (`0..1`).
|
|
80
|
+
* @param {{ value: THREE.Vector3 }} [options.uWind] - World wind vector uniform.
|
|
81
|
+
* @param {ReturnType<typeof createPressedDiscs>} [options.pressed] - Shared pressed-cover discs handle.
|
|
82
|
+
* @param {readonly object[]} [options.looks=DEFAULT_FLOWER_LOOKS] - Flower and seed-head descriptor table.
|
|
83
|
+
* @param {number} [options.tallest=TUFT_SHAPE.tallest] - Tallest blade height in tuft-local metres.
|
|
84
|
+
* @param {(sky: number, sun: number, glow: number, where: string) => string} [options.lightThrough] - Backlighting GLSL snippet builder.
|
|
85
|
+
* @param {Partial<typeof TUFT_SHADER>} [options.shader] - Overrides to {@link TUFT_SHADER}.
|
|
86
|
+
* @param {object} [options.flowerUniforms] - Prebuilt flower table uniform map (reused across materials).
|
|
87
|
+
* @returns {THREE.MeshStandardMaterial}
|
|
88
|
+
*/
|
|
89
|
+
export function createTuftMaterial(uCam, uR, ringed = false, options = {}) {
|
|
90
|
+
const cfg = options.shader ? { ...TUFT_SHADER, ...options.shader } : TUFT_SHADER;
|
|
91
|
+
const looks = options.looks ?? DEFAULT_FLOWER_LOOKS;
|
|
92
|
+
const tallest = options.tallest ?? TUFT_SHAPE.tallest;
|
|
93
|
+
const flowerU = options.flowerUniforms ?? buildFlowerUniforms(looks, tallest);
|
|
94
|
+
const pressed = options.pressed ?? createPressedDiscs(8);
|
|
95
|
+
const uTime = options.uTime ?? { value: 0 };
|
|
96
|
+
const uSunLight = options.uSunLight ?? { value: 1 };
|
|
97
|
+
const uWind = options.uWind ?? { value: new THREE.Vector3(1, 0, 0) };
|
|
98
|
+
const lightThrough =
|
|
99
|
+
options.lightThrough ??
|
|
100
|
+
((sky, sun, glow, where) => thinSurfaceLight({ sky, sun, glow, where, sunlight: 'uSunLight', shadowed: false }));
|
|
101
|
+
const kinds = looks.length;
|
|
102
|
+
const flowerVertex = buildFlowerVertexGLSL(cfg);
|
|
103
|
+
|
|
104
|
+
const mat = new THREE.MeshStandardMaterial({ roughness: 1, side: THREE.DoubleSide });
|
|
105
|
+
mat.onBeforeCompile = sh => {
|
|
106
|
+
Object.assign(sh.uniforms, flowerU, pressed.uniforms, { uTime, uSunLight, uWind, uCam, uR });
|
|
107
|
+
sh.vertexShader = sh.vertexShader
|
|
108
|
+
.replace(
|
|
109
|
+
'#include <common>',
|
|
110
|
+
`#include <common>
|
|
111
|
+
uniform float uTime, uR; uniform vec3 uCam, uWind; ${pressedDiscsPars(pressed)} varying float vTip; attribute float aFlower; attribute vec4 aStem, aPetal;${ringed ? ' attribute vec2 aReach;' : ''}
|
|
112
|
+
${WIND_SWAY_GLSL}
|
|
113
|
+
uniform vec4 uFlowerA[${kinds}], uFlowerB[${kinds}]; uniform vec3 uHeart[${kinds}], uRim[${kinds}];`,
|
|
114
|
+
)
|
|
115
|
+
.replace(
|
|
116
|
+
'#include <begin_vertex>',
|
|
117
|
+
`/* glsl */
|
|
118
|
+
#include <begin_vertex>
|
|
119
|
+
vec3 ip = instanceMatrix[3].xyz; float gd = length((ip - uCam) * vec3(1.0, 1.48, 1.0)); vTip = position.y;${flowerVertex}
|
|
120
|
+
// wind sway: blades lean downwind, more in a gust, and waves run across the grass
|
|
121
|
+
transformed.xz += windSway(ip, transformed.y, 0.05, 0.05, 0.0);
|
|
122
|
+
// trodden: pressed nearly flat where people stand and the camera looks through
|
|
123
|
+
${pressedStandingGLSL(pressed, 'ip.xz')}
|
|
124
|
+
transformed.y *= mix(0.1, 1.0, stands);
|
|
125
|
+
transformed *= 1.0 - smoothstep(${ringed ? 'aReach.y * 0.55, aReach.y' : 'uR * 0.55, uR'}, gd)${ringed ? ' * (aReach.x > 0.0 ? smoothstep(aReach.x * 0.55, aReach.x, gd) : 1.0)' : ''};`,
|
|
126
|
+
);
|
|
127
|
+
sh.fragmentShader = sh.fragmentShader
|
|
128
|
+
.replace('#include <common>', '#include <common>\nvarying float vTip; uniform float uSunLight;')
|
|
129
|
+
.replace('#include <map_fragment>', 'diffuseColor.rgb *= mix(0.62, 1.34, clamp(vTip * 1.6, 0.0, 1.0));')
|
|
130
|
+
.replace(
|
|
131
|
+
'#include <normal_fragment_begin>',
|
|
132
|
+
'#include <normal_fragment_begin>\n normal = normalize((viewMatrix * vec4(0.0, 1.0, 0.0, 0.0)).xyz);',
|
|
133
|
+
)
|
|
134
|
+
.replace('#include <opaque_fragment>', lightThrough(0, 0, cfg.glow, 'clamp(vTip * 1.6, 0.2, 1.0)'));
|
|
135
|
+
};
|
|
136
|
+
mat.customProgramCacheKey = () => 'grass-one-wind-trodden' + (ringed ? '-ringed' : '');
|
|
137
|
+
return mat;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Create a world-bound grass tuft toolkit sharing one set of pressed-cover discs, flower uniform
|
|
142
|
+
* tables, wind/time/sun uniforms and low-cover clearing hook.
|
|
143
|
+
*
|
|
144
|
+
* @param {object} [options]
|
|
145
|
+
* @param {number} [options.seed=5] - Deterministic seed for `tuftGeometry()`.
|
|
146
|
+
* @param {readonly object[]} [options.looks=DEFAULT_FLOWER_LOOKS] - Flower and seed-head descriptor table.
|
|
147
|
+
* @param {Partial<typeof TUFT_SHAPE>} [options.shape] - Overrides to `TUFT_SHAPE`.
|
|
148
|
+
* @param {Partial<typeof TUFT_SHADER>} [options.shader] - Overrides to {@link TUFT_SHADER}.
|
|
149
|
+
* @param {number} [options.maxPressed=8] - Maximum pressed-cover discs.
|
|
150
|
+
* @param {{ value: number }} [options.uTime] - Elapsed clock uniform in seconds.
|
|
151
|
+
* @param {{ value: number }} [options.uSunLight] - Sun intensity uniform (`0..1`).
|
|
152
|
+
* @param {{ value: THREE.Vector3 }} [options.uWind] - World wind vector uniform.
|
|
153
|
+
* @param {(sky: number, sun: number, glow: number, where: string) => string} [options.lightThrough] - Backlighting GLSL snippet builder.
|
|
154
|
+
* @returns {{
|
|
155
|
+
* FLOWER: typeof FLOWER_KINDS,
|
|
156
|
+
* pressedDiscs: ReturnType<typeof createPressedDiscs>,
|
|
157
|
+
* pressGrass: (count: number) => THREE.Vector3[],
|
|
158
|
+
* clearedInScenes: <T extends THREE.Material>(mat: T) => T,
|
|
159
|
+
* tuftGeometry: () => THREE.BufferGeometry,
|
|
160
|
+
* tuftMaterial: (uCam: { value: THREE.Vector3 }, uR: { value: number }, ringed?: boolean) => THREE.MeshStandardMaterial,
|
|
161
|
+
* }}
|
|
162
|
+
*/
|
|
163
|
+
export function createGrassTuft(options = {}) {
|
|
164
|
+
const looks = options.looks ?? DEFAULT_FLOWER_LOOKS;
|
|
165
|
+
const tallest = options.shape?.tallest ?? TUFT_SHAPE.tallest;
|
|
166
|
+
const cfg = options.shader ? { ...TUFT_SHADER, ...options.shader } : TUFT_SHADER;
|
|
167
|
+
const flowerUniforms = buildFlowerUniforms(looks, tallest);
|
|
168
|
+
const pressedDiscs = options.pressed ?? createPressedDiscs(options.maxPressed ?? 8);
|
|
169
|
+
const cleared = cfg.cleared ?? TUFT_SHADER.cleared;
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
FLOWER: FLOWER_KINDS,
|
|
173
|
+
pressedDiscs,
|
|
174
|
+
pressGrass: count => pressedDiscs.count(count),
|
|
175
|
+
clearedInScenes: mat => clearInstancesInDiscs(mat, pressedDiscs, cleared),
|
|
176
|
+
tuftGeometry: () => buildTuftGeometry({ seed: options.seed ?? 5, shape: options.shape }),
|
|
177
|
+
tuftMaterial: (uCam, uR, ringed = false) =>
|
|
178
|
+
createTuftMaterial(uCam, uR, ringed, {
|
|
179
|
+
uTime: options.uTime,
|
|
180
|
+
uSunLight: options.uSunLight,
|
|
181
|
+
uWind: options.uWind,
|
|
182
|
+
pressed: pressedDiscs,
|
|
183
|
+
looks,
|
|
184
|
+
tallest,
|
|
185
|
+
lightThrough: options.lightThrough,
|
|
186
|
+
shader: cfg,
|
|
187
|
+
flowerUniforms,
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
}
|