@adatechnology/game-urban-three 0.3.16 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,12 +2,25 @@
2
2
  import { isLightBudget } from "@adatechnology/game-render-three";
3
3
  import { discardObject3D } from "@adatechnology/game-shapes-three";
4
4
  import { createPlantProvider as createPlantProvider2, createUrban as createUrban2, createVehicleProvider as createVehicleProvider2, SAMPLE_CELLS } from "@adatechnology/game-urban";
5
- import { shapeGeometries as shapeGeometries3 } from "@adatechnology/game-shapes-three";
5
+ import { shapeGeometries as shapeGeometries5 } from "@adatechnology/game-shapes-three";
6
6
 
7
7
  // src/draw.ts
8
- import { markKitOrigin } from "@adatechnology/game-contracts";
9
- import { LIGHT_PRIORITY } from "@adatechnology/game-render-three";
10
- import { shapeGeometries as shapeGeometries2 } from "@adatechnology/game-shapes-three";
8
+ import { markKitOrigin as markKitOrigin2 } from "@adatechnology/game-contracts";
9
+ import { LIGHT_PRIORITY as LIGHT_PRIORITY2 } from "@adatechnology/game-render-three";
10
+ import { shapeGeometries as shapeGeometries4, standOutCoincident } from "@adatechnology/game-shapes-three";
11
+
12
+ // src/lamp-light.ts
13
+ var LAMP_FLICKER = { dim: 0.15, fast: 23, slow: 3.1, fastAbove: 0.3, slowAbove: 0.85, groups: 4 };
14
+ var TURN = 2 * Math.PI;
15
+ var flickerGroupOf = (phase) => Math.floor((phase % TURN + TURN) % TURN / TURN * LAMP_FLICKER.groups) % LAMP_FLICKER.groups;
16
+ var flickerGroupPhase = (group) => group / LAMP_FLICKER.groups * TURN;
17
+ var flickerLevel = (t, phase) => Math.sin(t * LAMP_FLICKER.fast + phase) > LAMP_FLICKER.fastAbove || Math.sin(t * LAMP_FLICKER.slow + phase) > LAMP_FLICKER.slowAbove ? LAMP_FLICKER.dim : 1;
18
+ function physicalIntensityOf(part, reference = part.y) {
19
+ const foot = Math.max(reference, 0.5);
20
+ const legacyFalloff = part.distance > 0 ? Math.max(0, 1 - foot / part.distance) ** part.decay : 1;
21
+ const window = part.distance > 0 ? Math.max(1e-6, 1 - (foot / part.distance) ** 4) ** 2 : 1;
22
+ return part.intensity * Math.PI * legacyFalloff * foot ** part.decay / window;
23
+ }
11
24
 
12
25
  // src/rounded-box.ts
13
26
  import { shapeGeometries } from "@adatechnology/game-shapes-three";
@@ -82,6 +95,463 @@ function createTextTexture(THREE, document, lines, o) {
82
95
  return texture;
83
96
  }
84
97
 
98
+ // src/networks/draw-networks.ts
99
+ import { markKitOrigin } from "@adatechnology/game-contracts";
100
+ import { LIGHT_PRIORITY } from "@adatechnology/game-render-three";
101
+ import { shapeGeometries as shapeGeometries3 } from "@adatechnology/game-shapes-three";
102
+ import { DEFAULT_POLES } from "@adatechnology/game-urban";
103
+ import { networkLampsOf, slotsOfPole } from "@adatechnology/game-urban/networks";
104
+
105
+ // src/networks/draw-pieces.ts
106
+ import { shapeGeometries as shapeGeometries2 } from "@adatechnology/game-shapes-three";
107
+ var CYLINDER_SEGMENTS = 10;
108
+ function drawPieces(THREE, { masts, boxes, cylinders, specOf }) {
109
+ const T = shapeGeometries2(THREE);
110
+ const owned = [];
111
+ const meshes = [];
112
+ const matrix = new THREE.Matrix4();
113
+ const quaternion = new THREE.Quaternion();
114
+ const position = new THREE.Vector3();
115
+ const scale = new THREE.Vector3();
116
+ const up = new THREE.Vector3(0, 1, 0);
117
+ const axis = new THREE.Vector3();
118
+ const color = new THREE.Color();
119
+ const instanced = (geometry, material, count, name) => {
120
+ owned.push(geometry, material);
121
+ const mesh = new THREE.InstancedMesh(geometry, material, count);
122
+ mesh.name = name;
123
+ mesh.castShadow = true;
124
+ mesh.receiveShadow = true;
125
+ meshes.push(mesh);
126
+ return mesh;
127
+ };
128
+ for (const specId of new Set(masts.map((mast) => mast.specId))) {
129
+ const { mast } = specOf(specId);
130
+ const geometry = new T.CylinderGeometry(mast.radiusTop, mast.radiusBottom, mast.height, mast.segments ?? 4).rotateY(Math.PI / 4).translate(0, mast.height / 2, 0);
131
+ const own = masts.filter((item) => item.specId === specId);
132
+ const mesh = instanced(geometry, new THREE.MeshStandardMaterial({ color: mast.color, roughness: 0.9 }), own.length, `network:masts:${specId}`);
133
+ own.forEach((item, index) => mesh.setMatrixAt(index, matrix.compose(position.set(item.x, item.y, item.z), quaternion.setFromAxisAngle(up, item.ry), scale.set(1, 1, 1))));
134
+ }
135
+ if (boxes.length > 0) {
136
+ const mesh = instanced(new T.BoxGeometry(1, 1, 1), new THREE.MeshStandardMaterial({ color: 16777215, roughness: 0.75 }), boxes.length, "network:hardware");
137
+ boxes.forEach((box, index) => {
138
+ mesh.setMatrixAt(index, matrix.compose(position.set(box.x, box.y, box.z), quaternion.setFromAxisAngle(up, box.ry), scale.set(box.width, box.height, box.depth)));
139
+ mesh.setColorAt(index, color.set(box.color));
140
+ });
141
+ }
142
+ if (cylinders.length > 0) {
143
+ const mesh = instanced(new T.CylinderGeometry(1, 1, 1, CYLINDER_SEGMENTS), new THREE.MeshStandardMaterial({ color: 16777215, roughness: 0.6, metalness: 0.3 }), cylinders.length, "network:cylinders");
144
+ cylinders.forEach((cylinder, index) => {
145
+ quaternion.setFromUnitVectors(up, axis.set(cylinder.axis[0], cylinder.axis[1], cylinder.axis[2]).normalize());
146
+ mesh.setMatrixAt(index, matrix.compose(position.set(cylinder.x, cylinder.y, cylinder.z), quaternion, scale.set(cylinder.radius, cylinder.height, cylinder.radius)));
147
+ mesh.setColorAt(index, color.set(cylinder.color));
148
+ });
149
+ }
150
+ return {
151
+ meshes,
152
+ dispose() {
153
+ for (const mesh of meshes) mesh.dispose();
154
+ for (const item of owned.splice(0)) item.dispose();
155
+ }
156
+ };
157
+ }
158
+
159
+ // src/networks/catenary.ts
160
+ var SPAN_SEGMENTS = 8;
161
+ var TUBE_SIDES = 4;
162
+ function catenaryPoints({ from, to, sag, segments = SPAN_SEGMENTS }) {
163
+ return Array.from({ length: segments + 1 }, (_, index) => {
164
+ const t = index / segments;
165
+ return [from[0] + (to[0] - from[0]) * t, from[1] + (to[1] - from[1]) * t - 4 * sag * t * (1 - t), from[2] + (to[2] - from[2]) * t];
166
+ });
167
+ }
168
+ var emptyMesh = () => ({ positions: [], normals: [], indices: [] });
169
+ var normalize = (v) => {
170
+ const length = Math.hypot(v[0], v[1], v[2]) || 1;
171
+ return [v[0] / length, v[1] / length, v[2] / length];
172
+ };
173
+ var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
174
+ function sectionAxes(tangent) {
175
+ const level = cross(tangent, [0, 1, 0]);
176
+ const side = Math.hypot(...level) < 1e-6 ? [1, 0, 0] : normalize(level);
177
+ return [side, normalize(cross(side, tangent))];
178
+ }
179
+ function appendTube({ mesh, points, radius }) {
180
+ const base = mesh.positions.length / 3;
181
+ const start = mesh.indices.length;
182
+ points.forEach((point, index) => {
183
+ const previous = points[Math.max(index - 1, 0)];
184
+ const next = points[Math.min(index + 1, points.length - 1)];
185
+ const [side, up] = sectionAxes(normalize([next[0] - previous[0], next[1] - previous[1], next[2] - previous[2]]));
186
+ for (let k = 0; k < TUBE_SIDES; k++) {
187
+ const angle = (k + 0.5) / TUBE_SIDES * 2 * Math.PI;
188
+ const normal = [0, 1, 2].map((axis) => Math.cos(angle) * side[axis] + Math.sin(angle) * up[axis]);
189
+ mesh.normals.push(normal[0], normal[1], normal[2]);
190
+ mesh.positions.push(point[0] + normal[0] * radius, point[1] + normal[1] * radius, point[2] + normal[2] * radius);
191
+ }
192
+ });
193
+ for (let ring = 0; ring + 1 < points.length; ring++) {
194
+ for (let k = 0; k < TUBE_SIDES; k++) {
195
+ const a = base + ring * TUBE_SIDES + k;
196
+ const b = base + ring * TUBE_SIDES + (k + 1) % TUBE_SIDES;
197
+ const c = a + TUBE_SIDES;
198
+ const d = b + TUBE_SIDES;
199
+ mesh.indices.push(a, c, b, b, c, d);
200
+ }
201
+ }
202
+ return [start, mesh.indices.length - start];
203
+ }
204
+ var emptyLines = () => ({ positions: [], colors: [], indices: [] });
205
+ function appendLine({ lines, points, color }) {
206
+ const base = lines.positions.length / 3;
207
+ const start = lines.indices.length;
208
+ for (const point of points) {
209
+ lines.positions.push(point[0], point[1], point[2]);
210
+ lines.colors.push(color[0], color[1], color[2]);
211
+ }
212
+ for (let index = 0; index + 1 < points.length; index++) lines.indices.push(base + index, base + index + 1);
213
+ return [start, lines.indices.length - start];
214
+ }
215
+
216
+ // src/networks/pole-frames.ts
217
+ import { distributionHardwareOf } from "@adatechnology/game-urban/networks";
218
+ var LINE_EDGES = /* @__PURE__ */ new Set(["mv_span", "lv_span", "telecom_span"]);
219
+ function worldOf(frame, local) {
220
+ return [frame.x + local[0] * frame.road[0] + local[2] * frame.line[0], frame.y + local[1], frame.z + local[0] * frame.road[1] + local[2] * frame.line[1]];
221
+ }
222
+ var unit = (x, z) => {
223
+ const length = Math.hypot(x, z);
224
+ return length < 1e-9 ? [1, 0] : [x / length, z / length];
225
+ };
226
+ function indexOf(networks) {
227
+ const at = /* @__PURE__ */ new Map();
228
+ for (const edge of networks.edges) {
229
+ for (const end of [edge.from, edge.to]) at.set(end, [...at.get(end) ?? [], edge]);
230
+ }
231
+ return { nodes: new Map(networks.nodes.map((node) => [node.id, node])), at };
232
+ }
233
+ function lineDirectionsOf(index, pole) {
234
+ const out = [];
235
+ for (const edge of index.at.get(pole.id) ?? []) {
236
+ if (!LINE_EDGES.has(edge.kind)) continue;
237
+ const other = index.nodes.get(edge.from === pole.id ? edge.to : edge.from);
238
+ if (!other || other.kind === "transformer") continue;
239
+ const far = other.kind === "splice" ? farEndOf(index, other, pole.id) : other;
240
+ const direction = unit(far.at[0] - pole.at[0], far.at[2] - pole.at[2]);
241
+ if (!out.some((seen) => seen[0] * direction[0] + seen[1] * direction[1] > 0.94)) out.push(direction);
242
+ }
243
+ return out;
244
+ }
245
+ function farEndOf(index, splice, from) {
246
+ const next = (index.at.get(splice.id) ?? []).map((edge) => edge.from === splice.id ? edge.to : edge.from).find((id) => id !== from);
247
+ return next && index.nodes.get(next) || splice;
248
+ }
249
+ function roadOf(index, pole) {
250
+ const lamp = index.nodes.get(`light:${pole.id}`);
251
+ if (lamp) return unit(lamp.at[0] - pole.at[0], lamp.at[2] - pole.at[2]);
252
+ const along = lineDirectionsOf(index, pole)[0] ?? [1, 0];
253
+ const across = [-along[1], along[0]];
254
+ const lot = (index.at.get(pole.id) ?? []).filter((edge) => edge.kind === "service_drop" && edge.from === pole.id).map((edge) => index.nodes.get(edge.to))[0];
255
+ if (!lot) return across;
256
+ const toward = (lot.at[0] - pole.at[0]) * across[0] + (lot.at[2] - pole.at[2]) * across[1];
257
+ return toward > 0 ? [-across[0], -across[1]] : across;
258
+ }
259
+ function cablesAt(index, pole) {
260
+ return Math.max(0, ...(index.at.get(pole.id) ?? []).filter((edge) => edge.kind === "telecom_span").map((edge) => edge.conductors ?? 1));
261
+ }
262
+ function poleFramesOf({ networks, index, specOf }) {
263
+ return networks.nodes.filter((node) => node.kind === "pole").map((node) => {
264
+ const specId = node.spec ?? "concrete_distribution";
265
+ const spec = specOf(specId);
266
+ const road = roadOf(index, node);
267
+ const hardware = distributionHardwareOf(spec, {
268
+ mv: node.carries.includes("power_mv"),
269
+ transformer: index.nodes.has(`transformer:${node.id}`),
270
+ lamp: index.nodes.has(`light:${node.id}`),
271
+ cables: cablesAt(index, node),
272
+ cableGap: networks.rules.layout.telecom.cableGap
273
+ });
274
+ return { id: node.id, specId, spec, x: node.at[0], y: node.at[1], z: node.at[2], ry: Math.atan2(-road[1], road[0]), road, line: [-road[1], road[0]], hardware, guyed: node.guyed === true };
275
+ });
276
+ }
277
+
278
+ // src/networks/wire-runs.ts
279
+ var WIRE_CLASSES = ["mv", "lv", "telecom", "drops", "guys"];
280
+ var MIN_LENGTH = 0.05;
281
+ var CLASS_OF = { mv_span: "mv", lv_span: "lv", telecom_span: "telecom", service_drop: "drops", telecom_drop: "drops" };
282
+ function pointsOn(frame, kind, conductors) {
283
+ const { attachments } = frame.hardware;
284
+ if (kind === "mv_span") return attachments.mv;
285
+ if (kind === "telecom_span") return attachments.telecom.slice(0, conductors);
286
+ if (kind === "telecom_drop") return attachments.telecom.slice(0, 1);
287
+ return [attachments.lv];
288
+ }
289
+ function frameNear({ frames, index }, node, other) {
290
+ const own = frames.get(other);
291
+ if (own) return own;
292
+ const near = (index.at.get(node.id) ?? []).map((edge) => edge.from === node.id ? edge.to : edge.from).find((id) => frames.has(id));
293
+ return near === void 0 ? void 0 : frames.get(near);
294
+ }
295
+ function endOf(ends, edge, id, other) {
296
+ const node = ends.index.nodes.get(id);
297
+ if (!node) return [];
298
+ const conductors = edge.kind === "telecom_span" ? edge.conductors ?? 1 : edge.kind === "mv_span" ? 3 : 1;
299
+ const pole = ends.frames.get(id);
300
+ if (pole) return pointsOn(pole, edge.kind, conductors).map((point) => worldOf(pole, point));
301
+ if (node.kind === "transformer") {
302
+ const host = ends.frames.get(id.slice("transformer:".length));
303
+ const points = host?.hardware.attachments.transformer;
304
+ if (!host || !points) return [];
305
+ return (edge.kind === "mv_span" ? points.mv : [points.lv]).map((point) => worldOf(host, point));
306
+ }
307
+ if (node.kind === "connection") return [node.at];
308
+ const near = frameNear(ends, node, other);
309
+ if (!near) return [node.at];
310
+ const placed = { ...near, x: node.at[0], z: node.at[2], y: 0 };
311
+ return pointsOn(near, edge.kind, conductors).map((point) => worldOf(placed, point));
312
+ }
313
+ function wireRunsOf(ends, edges) {
314
+ const runs = [];
315
+ for (const edge of edges) {
316
+ const wireClass = CLASS_OF[edge.kind];
317
+ if (!wireClass) continue;
318
+ const from = endOf(ends, edge, edge.from, edge.to);
319
+ const to = endOf(ends, edge, edge.to, edge.from);
320
+ const count = Math.min(from.length, to.length);
321
+ if (count === 0) continue;
322
+ const conductors = Array.from({ length: count }, (_, k) => catenaryPoints({ from: from[k], to: to[k], sag: edge.sag ?? 0 }));
323
+ if (Math.hypot(from[0][0] - to[0][0], from[0][1] - to[0][1], from[0][2] - to[0][2]) < MIN_LENGTH) continue;
324
+ runs.push({ id: edge.id, wireClass, conductors });
325
+ }
326
+ return runs;
327
+ }
328
+ var GUY_SHARES = [1, 0.75, 0.5, 0.35];
329
+ function guysOf({ index, frames, surfaceAt }) {
330
+ const out = [];
331
+ for (const frame of frames.values()) {
332
+ const node = index.nodes.get(frame.id);
333
+ if (!frame.guyed || !node) continue;
334
+ lineDirectionsOf(index, node).forEach((pull, k) => {
335
+ const share = GUY_SHARES.find((s) => {
336
+ const [x, z] = [frame.x - pull[0] * frame.spec.guy.reach * s, frame.z - pull[1] * frame.spec.guy.reach * s];
337
+ return surfaceAt === void 0 || surfaceAt(x, z) === "sidewalk";
338
+ });
339
+ if (share === void 0) return;
340
+ const reach = frame.spec.guy.reach * share;
341
+ out.push({ id: `guy:${frame.id}:${k}`, pole: frame.id, from: worldOf(frame, frame.hardware.attachments.guy), at: [frame.x - pull[0] * reach, frame.y, frame.z - pull[1] * reach] });
342
+ });
343
+ }
344
+ return out;
345
+ }
346
+
347
+ // src/networks/draw-wires.ts
348
+ var WIRE_STYLE = {
349
+ mv: { radius: 7e-3, color: 9343639, metalness: 0.5, roughness: 0.5 },
350
+ lv: { radius: 0.016, color: 1776670, metalness: 0, roughness: 0.8 },
351
+ telecom: { radius: 8e-3, color: 1316118, metalness: 0, roughness: 0.85 },
352
+ drops: { radius: 7e-3, color: 1842206, metalness: 0, roughness: 0.8 },
353
+ guys: { radius: 5e-3, color: 8027262, metalness: 0.6, roughness: 0.5 }
354
+ };
355
+ var rgb = (THREE, hex) => {
356
+ const color = new THREE.Color(hex);
357
+ return [color.r, color.g, color.b];
358
+ };
359
+ var distanceToSegment = (x, z, a, b) => {
360
+ const [dx, dz] = [b[0] - a[0], b[2] - a[2]];
361
+ const t = Math.max(0, Math.min(1, ((x - a[0]) * dx + (z - a[2]) * dz) / (dx * dx + dz * dz || 1)));
362
+ return Math.hypot(a[0] + dx * t - x, a[2] + dz * t - z);
363
+ };
364
+ var apply = (attribute, original, ranges, shown) => {
365
+ const array = attribute.array;
366
+ ranges.forEach((range, span) => {
367
+ if (!range) return;
368
+ const [start, count] = range;
369
+ if (shown[span]) array.set(original.subarray(start, start + count), start);
370
+ else array.fill(0, start, start + count);
371
+ });
372
+ attribute.needsUpdate = true;
373
+ };
374
+ function drawWires(THREE, { runs, guys }) {
375
+ const all = [...runs, ...guys.map((guy) => ({ id: guy.id, wireClass: "guys", conductors: [[guy.from, guy.at]] }))];
376
+ const meshes = new Map(WIRE_CLASSES.map((wireClass) => [wireClass, emptyMesh()]));
377
+ const lines = emptyLines();
378
+ const spans = /* @__PURE__ */ new Map();
379
+ for (const run of all) {
380
+ const mesh = meshes.get(run.wireClass);
381
+ const { radius, color } = WIRE_STYLE[run.wireClass];
382
+ const nearRanges = run.conductors.map((points) => appendTube({ mesh, points, radius }));
383
+ const farRanges = run.conductors.map((points) => appendLine({ lines, points, color: rgb(THREE, color) }));
384
+ const span = (ranges) => [ranges[0][0], ranges.reduce((sum, range) => sum + range[1], 0)];
385
+ spans.set(run.id, { wireClass: run.wireClass, near: span(nearRanges), far: span(farRanges) });
386
+ }
387
+ const owned = [];
388
+ const near = WIRE_CLASSES.filter((wireClass) => meshes.get(wireClass).indices.length > 0).map((wireClass) => {
389
+ const data = meshes.get(wireClass);
390
+ const geometry = new THREE.BufferGeometry();
391
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(data.positions, 3));
392
+ geometry.setAttribute("normal", new THREE.Float32BufferAttribute(data.normals, 3));
393
+ geometry.setIndex(new THREE.BufferAttribute(new Uint32Array(data.indices), 1));
394
+ const style = WIRE_STYLE[wireClass];
395
+ const material = new THREE.MeshStandardMaterial({ color: style.color, metalness: style.metalness, roughness: style.roughness });
396
+ owned.push(geometry, material);
397
+ const mesh = new THREE.Mesh(geometry, material);
398
+ mesh.name = `network:${wireClass}`;
399
+ mesh.castShadow = true;
400
+ return mesh;
401
+ });
402
+ const lineGeometry = new THREE.BufferGeometry();
403
+ lineGeometry.setAttribute("position", new THREE.Float32BufferAttribute(lines.positions, 3));
404
+ lineGeometry.setAttribute("color", new THREE.Float32BufferAttribute(lines.colors, 3));
405
+ lineGeometry.setIndex(new THREE.BufferAttribute(new Uint32Array(lines.indices), 1));
406
+ const lineMaterial = new THREE.LineBasicMaterial({ vertexColors: true });
407
+ owned.push(lineGeometry, lineMaterial);
408
+ const far = new THREE.LineSegments(lineGeometry, lineMaterial);
409
+ far.name = "network:far";
410
+ far.visible = false;
411
+ const ids = [...spans.keys()];
412
+ const ends = all.map((run) => [run.conductors[0][0], run.conductors[0][run.conductors[0].length - 1]]);
413
+ const originals = new Map(near.map((mesh) => [mesh, mesh.geometry.index.array.slice()]));
414
+ const farOriginal = lineGeometry.index.array.slice();
415
+ let shown = ids.map(() => true);
416
+ return {
417
+ near,
418
+ far,
419
+ spans,
420
+ setFocus(x, z, distance) {
421
+ const next = ends.map(([a, b]) => distanceToSegment(x, z, a, b) <= distance);
422
+ if (next.every((value, span) => value === shown[span])) return;
423
+ shown = next;
424
+ for (const mesh of near) {
425
+ const wireClass = mesh.name.slice("network:".length);
426
+ apply(mesh.geometry.index, originals.get(mesh), ids.map((id) => spans.get(id).wireClass === wireClass ? spans.get(id).near : void 0), shown);
427
+ mesh.visible = ids.some((id, span) => shown[span] && spans.get(id).wireClass === wireClass);
428
+ }
429
+ apply(lineGeometry.index, farOriginal, ids.map((id) => spans.get(id).far), shown.map((value) => !value));
430
+ far.visible = shown.some((value) => !value);
431
+ },
432
+ dispose() {
433
+ for (const item of owned.splice(0)) item.dispose();
434
+ }
435
+ };
436
+ }
437
+
438
+ // src/networks/pieces.ts
439
+ var SPLICE = { width: 0.16, height: 0.3, depth: 0.42, drop: 0.28, color: 2763822 };
440
+ var COIL = { radius: 0.32, thickness: 0.07, along: 0.75, drop: 0.5, color: 1974049 };
441
+ var ANCHOR = { width: 0.08, height: 0.3, depth: 0.08, sunk: 0.1 };
442
+ var boxIn = (frame, box) => {
443
+ const [x, y, z] = worldOf(frame, [box.x, box.y + box.height / 2, box.z]);
444
+ return { x, y, z, ry: frame.ry, width: box.width, height: box.height, depth: box.depth, color: box.color };
445
+ };
446
+ function piecesOf({ frames, index, guys }) {
447
+ const out = { masts: [], boxes: [], cylinders: [], lenses: [] };
448
+ for (const frame of frames.values()) {
449
+ out.masts.push({ specId: frame.specId, x: frame.x, y: frame.y, z: frame.z, ry: frame.ry });
450
+ for (const box of frame.hardware.boxes) out.boxes.push(boxIn(frame, box));
451
+ for (const cylinder of frame.hardware.cylinders) {
452
+ const [x, y, z] = worldOf(frame, [cylinder.x, cylinder.y + cylinder.height / 2, cylinder.z]);
453
+ out.cylinders.push({ x, y, z, axis: [0, 1, 0], radius: cylinder.radius, height: cylinder.height, color: cylinder.color });
454
+ }
455
+ if (frame.hardware.lens) out.lenses.push({ pole: frame.id, box: boxIn(frame, frame.hardware.lens) });
456
+ }
457
+ for (const guy of guys) {
458
+ const frame = frames.get(guy.pole);
459
+ if (frame) out.boxes.push({ x: guy.at[0], y: guy.at[1] + ANCHOR.height / 2 - ANCHOR.sunk, z: guy.at[2], ry: frame.ry, width: ANCHOR.width, height: ANCHOR.height, depth: ANCHOR.depth, color: frame.spec.guy.color });
460
+ }
461
+ for (const node of index.nodes.values()) {
462
+ if (node.kind !== "splice") continue;
463
+ const pole = (index.at.get(node.id) ?? []).map((edge) => edge.from === node.id ? edge.to : edge.from).map((id) => frames.get(id)).find((frame) => frame !== void 0);
464
+ if (!pole) continue;
465
+ const at = { ...pole, x: node.at[0], z: node.at[2], y: 0 };
466
+ const ox = pole.hardware.attachments.telecom[0]?.[0] ?? 0;
467
+ const [x, , z] = worldOf(at, [ox, 0, 0]);
468
+ out.boxes.push({ x, y: node.at[1] - SPLICE.drop, z, ry: pole.ry, width: SPLICE.width, height: SPLICE.height, depth: SPLICE.depth, color: SPLICE.color });
469
+ const coil = worldOf(at, [ox, node.at[1] - COIL.drop, COIL.along]);
470
+ out.cylinders.push({ x: coil[0], y: coil[1], z: coil[2], axis: [pole.road[0], 0, pole.road[1]], radius: COIL.radius, height: COIL.thickness, color: COIL.color });
471
+ }
472
+ return out;
473
+ }
474
+
475
+ // src/networks/draw-networks.ts
476
+ var ALL_LAMPS_WORK = { lit: 1, flicker: 0 };
477
+ var NEAR_DISTANCE = 90;
478
+ var specResolver = (poles) => {
479
+ return (id) => {
480
+ const def = poles ? poles.get(id).def : DEFAULT_POLES.find((pole) => pole.id === id);
481
+ if (!def || !slotsOfPole({ id, shape: "distribution", spec: def.spec })) throw new Error(`the pole "${id}" a network stands on has no slots (a distribution pole)`);
482
+ return def.spec;
483
+ };
484
+ };
485
+ function drawNetworks(THREE, { networks, options, budget, physicalLights }) {
486
+ const root = markKitOrigin(options.root ?? new THREE.Group(), { owner: "game-urban-three", kind: "networks" });
487
+ const specOf = specResolver(options.poles);
488
+ const index = indexOf(networks);
489
+ const frames = new Map(poleFramesOf({ networks, index, specOf }).map((frame) => [frame.id, frame]));
490
+ const guys = guysOf({ index, frames, ...options.surfaceAt ? { surfaceAt: options.surfaceAt } : {} });
491
+ const wires = drawWires(THREE, { runs: wireRunsOf({ frames, index }, networks.edges), guys });
492
+ const pieces = piecesOf({ frames, index, guys });
493
+ const drawn = drawPieces(THREE, { masts: pieces.masts, boxes: pieces.boxes, cylinders: pieces.cylinders, specOf });
494
+ const lamps = networkLampsOf({ networks, shares: options.lamps ?? ALL_LAMPS_WORK, isNight: options.isNight ?? false });
495
+ const byPole = new Map(lamps.map((lamp) => [lamp.pole, lamp]));
496
+ const lensMaterial = new THREE.MeshBasicMaterial({ color: 16777215 });
497
+ const lenses = new THREE.InstancedMesh(new (shapeGeometries3(THREE)).BoxGeometry(1, 1, 1), lensMaterial, Math.max(pieces.lenses.length, 1));
498
+ lenses.name = "network:lenses";
499
+ lenses.count = pieces.lenses.length;
500
+ const matrix = new THREE.Matrix4();
501
+ const color = new THREE.Color();
502
+ const flickering = [];
503
+ pieces.lenses.forEach((lens, slot) => {
504
+ const { box } = lens;
505
+ lenses.setMatrixAt(slot, matrix.compose(new THREE.Vector3(box.x, box.y, box.z), new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), box.ry), new THREE.Vector3(box.width, box.height, box.depth)));
506
+ const lamp = byPole.get(lens.pole);
507
+ const spec = frames.get(lens.pole).spec;
508
+ lenses.setColorAt(slot, color.set(lamp?.on ? spec.lens.lit : spec.lens.unlit));
509
+ if (lamp?.flicker && lamp.flickerPhase !== void 0) flickering.push({ index: slot, lit: spec.lens.lit, phase: flickerGroupPhase(flickerGroupOf(lamp.flickerPhase)) });
510
+ });
511
+ const lights = [];
512
+ const animators = [];
513
+ for (const lamp of lamps) {
514
+ const frame = frames.get(lamp.pole);
515
+ if (!lamp.on || !frame || !budget) continue;
516
+ const { light } = frame.spec;
517
+ const bulb = new THREE.Object3D();
518
+ bulb.position.set(...worldOf(frame, [light.reach ?? frame.spec.lamp.reach, light.y, light.z ?? 0]));
519
+ root.add(bulb);
520
+ const intensity = physicalLights ? physicalIntensityOf({ intensity: light.intensity, distance: light.distance, decay: 2, y: light.y }) : light.intensity;
521
+ const source = budget.addSource({ owner: "urban.light", priority: LIGHT_PRIORITY.street, color: light.color, intensity, distance: light.distance, decay: 2, position: [0, 0, 0], follow: bulb });
522
+ lights.push({ bulb, lamp, source });
523
+ if (lamp.flicker && lamp.flickerPhase !== void 0) {
524
+ const phase = flickerGroupPhase(flickerGroupOf(lamp.flickerPhase));
525
+ animators.push((t) => source.update({ intensity: intensity * flickerLevel(t, phase) }));
526
+ }
527
+ }
528
+ root.add(...drawn.meshes, ...wires.near, wires.far, lenses);
529
+ const nearDistance = options.nearDistance ?? NEAR_DISTANCE;
530
+ return {
531
+ root,
532
+ lamps,
533
+ lights,
534
+ spans: wires.spans,
535
+ drawCalls: () => ({ near: [...drawn.meshes, ...wires.near, lenses].filter((mesh) => mesh.visible && (!(mesh instanceof THREE.InstancedMesh) || mesh.count > 0)).length, far: wires.far.visible ? 1 : 0 }),
536
+ setFocus: (x, z) => wires.setFocus(x, z, nearDistance),
537
+ animate(t) {
538
+ for (const step of animators) step(t);
539
+ if (flickering.length === 0) return;
540
+ for (const item of flickering) lenses.setColorAt(item.index, color.set(item.lit).multiplyScalar(flickerLevel(t, item.phase)));
541
+ lenses.instanceColor.needsUpdate = true;
542
+ },
543
+ dispose() {
544
+ for (const light of lights) light.source.remove();
545
+ root.parent?.remove(root);
546
+ wires.dispose();
547
+ drawn.dispose();
548
+ lenses.dispose();
549
+ lenses.geometry.dispose();
550
+ lensMaterial.dispose();
551
+ }
552
+ };
553
+ }
554
+
85
555
  // src/draw.ts
86
556
  var DEFAULT_ROUGHNESS = 0.9;
87
557
  var DEFAULT_METALNESS = 0;
@@ -90,8 +560,9 @@ var LIT_WINDOW = 16757854;
90
560
  var DARK_WINDOW = { color: 856599, roughness: 0.3, metalness: 0.5 };
91
561
  var SIGN_FONT = '700 56px "Arial Narrow", Arial, sans-serif';
92
562
  var SIGN_CANVAS = { width: 512, height: 128 };
563
+ var PLANE_LAYER_GAP = 2e-3;
93
564
  function createUrbanThree(THREE, options = {}) {
94
- const T = shapeGeometries2(THREE);
565
+ const T = shapeGeometries4(THREE);
95
566
  const geometries = /* @__PURE__ */ new Map();
96
567
  const materials = /* @__PURE__ */ new Map();
97
568
  const disposables = [];
@@ -106,6 +577,23 @@ function createUrbanThree(THREE, options = {}) {
106
577
  }
107
578
  return material;
108
579
  };
580
+ let animatedLenses = /* @__PURE__ */ new Set();
581
+ const flickeringLens = (color, phase, animators) => {
582
+ const group = flickerGroupOf(phase);
583
+ const key = `f|${color}|${group}`;
584
+ let material = materials.get(key);
585
+ if (!material) {
586
+ material = new THREE.MeshBasicMaterial({ color });
587
+ materials.set(key, material);
588
+ }
589
+ if (!animatedLenses.has(key)) {
590
+ animatedLenses.add(key);
591
+ const lens = material;
592
+ const groupPhase = flickerGroupPhase(group);
593
+ animators.push((t) => lens.color.setHex(color).multiplyScalar(flickerLevel(t, groupPhase)));
594
+ }
595
+ return material;
596
+ };
109
597
  const glowing = (color) => {
110
598
  const key = `b|${color}`;
111
599
  let material = materials.get(key);
@@ -146,18 +634,26 @@ function createUrbanThree(THREE, options = {}) {
146
634
  const aimAt = (node, from, to) => {
147
635
  node.quaternion.setFromUnitVectors(forward, new THREE.Vector3(to.x - from.x, to.y - from.y, to.z - from.z).normalize());
148
636
  };
149
- const sourceOf = (part, bulb, extra) => options.lights.addSource({ owner: "urban.light", priority: LIGHT_PRIORITY.street, color: part.color, intensity: part.intensity, distance: part.distance, decay: part.decay, position: [0, 0, 0], follow: bulb, ...extra });
637
+ const sourceOf = (part, bulb, extra) => options.lights.addSource({ owner: "urban.light", priority: LIGHT_PRIORITY2.street, color: part.color, intensity: part.intensity, distance: part.distance, decay: part.decay, position: [0, 0, 0], follow: bulb, ...extra });
638
+ const pageFlicker = (t, x) => Math.sin(t * 23 + x) > 0.3 || Math.sin(t * 3.1) > 0.85 ? 0.15 : 1;
639
+ const groupedPhaseOf = (part) => options.flickerLenses && part.kind === "point" && part.flickerPhase !== void 0 ? flickerGroupPhase(flickerGroupOf(part.flickerPhase)) : void 0;
150
640
  const drawBudgetedLight = (part, animators, lights) => {
151
641
  const bulb = new THREE.Object3D();
152
642
  bulb.position.set(part.x, part.y, part.z);
153
643
  if (part.kind === "point") {
154
- const source2 = sourceOf(part, bulb, {});
644
+ const intensity2 = options.physicalLights ? physicalIntensityOf(part) : part.intensity;
645
+ const source2 = sourceOf(part, bulb, { intensity: intensity2 });
155
646
  lights.push({ bulb, part, source: source2 });
156
- if (part.flicker) animators.push((t) => source2.update({ intensity: part.intensity * (Math.sin(t * 23 + part.x) > 0.3 || Math.sin(t * 3.1) > 0.85 ? 0.15 : 1) }));
647
+ if (part.flicker) {
648
+ const phase = groupedPhaseOf(part);
649
+ animators.push((t) => source2.update({ intensity: intensity2 * (phase === void 0 ? pageFlicker(t, part.x) : flickerLevel(t, phase)) }));
650
+ }
157
651
  return [bulb];
158
652
  }
159
653
  aimAt(bulb, part, part.target);
160
- const source = sourceOf(part, bulb, { kind: "spot", angle: part.angle, penumbra: part.penumbra });
654
+ const beam = Math.hypot(part.target.x - part.x, part.target.y - part.y, part.target.z - part.z);
655
+ const intensity = options.physicalLights ? physicalIntensityOf(part, beam) : part.intensity;
656
+ const source = sourceOf(part, bulb, { kind: "spot", angle: part.angle, penumbra: part.penumbra, intensity });
161
657
  lights.push({ bulb, part, source });
162
658
  const { sweep } = part;
163
659
  if (sweep) animators.push((t) => aimAt(bulb, part, { x: part.x + Math.sin(t * sweep.speed + part.x) * sweep.radius, y: 0, z: part.z + Math.cos(t * sweep.speed + part.x) * sweep.radius }));
@@ -172,7 +668,8 @@ function createUrbanThree(THREE, options = {}) {
172
668
  return [group];
173
669
  }
174
670
  case "box": {
175
- const mesh = new THREE.Mesh(roundedBox(part.width, part.height, part.depth, part.radius), part.emissive ? glowing(part.color) : standard(part.color, part.style));
671
+ const lens = options.flickerLenses && part.emissive && part.flickerPhase !== void 0 ? flickeringLens(part.color, part.flickerPhase, animators) : void 0;
672
+ const mesh = new THREE.Mesh(roundedBox(part.width, part.height, part.depth, part.radius), lens ?? (part.emissive ? glowing(part.color) : standard(part.color, part.style)));
176
673
  mesh.position.set(part.x, part.y, part.z);
177
674
  if (part.ry) mesh.rotation.y = part.ry;
178
675
  if (part.rz) mesh.rotation.z = part.rz;
@@ -282,8 +779,9 @@ function createUrbanThree(THREE, options = {}) {
282
779
  light2.position.set(part.x, part.y, part.z);
283
780
  lights.push(light2);
284
781
  if (part.flicker) {
782
+ const phase = groupedPhaseOf(part);
285
783
  animators.push((t) => {
286
- light2.intensity = part.intensity * (Math.sin(t * 23 + part.x) > 0.3 || Math.sin(t * 3.1) > 0.85 ? 0.15 : 1);
784
+ light2.intensity = part.intensity * (phase === void 0 ? pageFlicker(t, part.x) : flickerLevel(t, phase));
287
785
  });
288
786
  }
289
787
  return [light2];
@@ -304,7 +802,22 @@ function createUrbanThree(THREE, options = {}) {
304
802
  }
305
803
  }
306
804
  };
307
- const instancedItems = (list) => {
805
+ const layerItems = (list) => {
806
+ const placed = [];
807
+ return list.map((entry) => {
808
+ const quarterTurn = Math.abs(Math.sin(entry.ry * 2)) < 1e-6;
809
+ if (entry.rx || entry.rz || !quarterTurn) return entry;
810
+ const turned = Math.abs(Math.cos(entry.ry)) < 0.5;
811
+ const [halfX, halfZ] = turned ? [entry.depth / 2, entry.width / 2] : [entry.width / 2, entry.depth / 2];
812
+ const rect = { x0: entry.x - halfX, x1: entry.x + halfX, z0: entry.z - halfZ, z1: entry.z + halfZ };
813
+ let lift = 0;
814
+ while (placed.some((other) => Math.abs(other.top - (entry.y + entry.height + lift)) < PLANE_LAYER_GAP / 2 && other.x0 < rect.x1 && rect.x0 < other.x1 && other.z0 < rect.z1 && rect.z0 < other.z1)) lift += PLANE_LAYER_GAP;
815
+ placed.push({ top: entry.y + entry.height + lift, ...rect });
816
+ return lift ? { ...entry, y: entry.y + lift } : entry;
817
+ });
818
+ };
819
+ const instancedItems = (items) => {
820
+ const list = options.standOut === false ? items : layerItems(items);
308
821
  const mesh = new THREE.InstancedMesh(roundedBox(1, 1, 1, ITEM_ROUNDNESS), standard(16777215), list.length);
309
822
  const matrix = new THREE.Matrix4();
310
823
  const quaternion = new THREE.Quaternion();
@@ -338,17 +851,43 @@ function createUrbanThree(THREE, options = {}) {
338
851
  });
339
852
  return mesh;
340
853
  };
854
+ const layerPlanes = (parts) => {
855
+ const placed = [];
856
+ return (part, made) => {
857
+ if (part.shape !== "plane") return;
858
+ const origin = { x: part.x, y: part.y, z: part.z };
859
+ for (let parent = part.parent; parent >= 0; parent = parts[parent].parent) {
860
+ const group = parts[parent];
861
+ if (group.shape !== "group" || group.ry) return;
862
+ origin.x += group.x;
863
+ origin.y += group.y;
864
+ origin.z += group.z;
865
+ }
866
+ const rect = { x0: origin.x - part.width / 2, x1: origin.x + part.width / 2, z0: origin.z - part.depth / 2, z1: origin.z + part.depth / 2 };
867
+ let lift = 0;
868
+ while (placed.some((other) => Math.abs(other.y - (origin.y + lift)) < PLANE_LAYER_GAP / 2 && other.x0 < rect.x1 && rect.x0 < other.x1 && other.z0 < rect.z1 && rect.z0 < other.z1)) lift += PLANE_LAYER_GAP;
869
+ placed.push({ y: origin.y + lift, ...rect });
870
+ made[0].position.y += lift;
871
+ };
872
+ };
341
873
  function draw(scene, drawOptions = {}) {
342
- const root = markKitOrigin(drawOptions.root ?? new THREE.Group(), { owner: "game-urban-three", kind: "urban" });
874
+ const root = markKitOrigin2(drawOptions.root ?? new THREE.Group(), { owner: "game-urban-three", kind: "urban" });
343
875
  const animators = [];
344
876
  const lights = [];
877
+ animatedLenses = /* @__PURE__ */ new Set();
345
878
  const objects = [];
879
+ const layer = options.standOut === false ? () => void 0 : layerPlanes(scene.parts);
346
880
  for (const part of scene.parts) {
347
881
  const made = drawPart(part, animators, lights);
882
+ layer(part, made);
348
883
  objects.push(made);
349
884
  const parent = part.parent < 0 ? root : objects[part.parent][0];
350
885
  parent.add(...made);
351
886
  }
887
+ if (options.standOut === true) {
888
+ let passes = 0;
889
+ while (passes++ < 3 && standOutCoincident(root).left > 0) ;
890
+ }
352
891
  if (scene.items.length) root.add(instancedItems(scene.items));
353
892
  if (scene.windows.lit.length) root.add(instancedWindows(scene.windows.lit, glowing(LIT_WINDOW)));
354
893
  if (scene.windows.dark.length) root.add(instancedWindows(scene.windows.dark, standard(DARK_WINDOW.color, DARK_WINDOW)));
@@ -369,7 +908,8 @@ function createUrbanThree(THREE, options = {}) {
369
908
  }
370
909
  };
371
910
  }
372
- return { THREE, draw };
911
+ const networks = (data, drawOptions = {}) => drawNetworks(THREE, { networks: data, options: drawOptions, ...options.lights ? { budget: options.lights } : {}, ...options.physicalLights ? { physicalLights: true } : {} });
912
+ return { THREE, draw, drawNetworks: networks };
373
913
  }
374
914
 
375
915
  // src/resolvers.ts
@@ -861,7 +1401,7 @@ function createUrbanTestModule(options = {}) {
861
1401
  for (const lamp of entry.drawn.lights) {
862
1402
  const light = "bulb" in lamp ? { node: lamp.bulb, distance: lamp.part.distance, color: lamp.part.color } : lamp.isLight ? { node: lamp, distance: lamp.distance, color: lamp.color } : null;
863
1403
  if (!light) continue;
864
- const reach = new three.Mesh(new (shapeGeometries3(three)).SphereGeometry(Math.min(light.distance || 10, 60), 14, 8), new three.MeshBasicMaterial({ color: light.color ?? 16764006, wireframe: true, transparent: true, opacity: 0.25 }));
1404
+ const reach = new three.Mesh(new (shapeGeometries5(three)).SphereGeometry(Math.min(light.distance || 10, 60), 14, 8), new three.MeshBasicMaterial({ color: light.color ?? 16764006, wireframe: true, transparent: true, opacity: 0.25 }));
865
1405
  reach.position.copy(light.node.position);
866
1406
  mark(light.node.parent ?? entry.root, reach);
867
1407
  }
@@ -981,9 +1521,15 @@ function loadUrbanTestModule(room, options = {}) {
981
1521
  }
982
1522
 
983
1523
  export {
1524
+ LAMP_FLICKER,
1525
+ flickerGroupOf,
1526
+ flickerGroupPhase,
1527
+ flickerLevel,
1528
+ physicalIntensityOf,
984
1529
  createRoundedBox,
985
1530
  createPrism,
986
1531
  createTextTexture,
1532
+ WIRE_STYLE,
987
1533
  createUrbanThree,
988
1534
  createVehicleResolver,
989
1535
  createPlantResolver,
@@ -995,4 +1541,4 @@ export {
995
1541
  createUrbanTestModule,
996
1542
  loadUrbanTestModule
997
1543
  };
998
- //# sourceMappingURL=chunk-XUITKOBO.js.map
1544
+ //# sourceMappingURL=chunk-JTXGLKOL.js.map