@bendyline/molen-ambient 0.0.12 → 0.0.14

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/dist/client.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { d as registerAmbientSchema, r as AmbientAgent } from "./components-vzvNQczG.mjs";
1
+ import { d as registerAmbientSchema, r as AmbientAgent } from "./components-DJINUkXK.mjs";
2
2
  import { Transform, componentHandle } from "@bendyline/molen-kernel/world";
3
3
  import { createVehicleVisual, vehicleProxyGeometry, vehicleProxyMaterial } from "@bendyline/molen-client/vehicles";
4
4
  import * as THREE from "three";
@@ -170,7 +170,7 @@ var ProxyBatches = class {
170
170
  for (const batch of this.batches.values()) {
171
171
  const mesh = batch.mesh;
172
172
  mesh.count = batch.count;
173
- mesh.visible = batch.count > 0;
173
+ mesh.visible = batch.ready && batch.count > 0;
174
174
  total += batch.count;
175
175
  if (batch.count === 0) continue;
176
176
  mesh.instanceMatrix.clearUpdateRanges();
@@ -199,7 +199,8 @@ var ProxyBatches = class {
199
199
  mesh.count = 0;
200
200
  const batch = {
201
201
  mesh,
202
- count: previous?.count ?? 0
202
+ count: previous?.count ?? 0,
203
+ ready: true
203
204
  };
204
205
  if (previous !== void 0) {
205
206
  for (let i = 0; i < previous.count; i++) {
@@ -214,7 +215,14 @@ var ProxyBatches = class {
214
215
  }
215
216
  this.group.add(mesh);
216
217
  this.batches.set(key, batch);
217
- this.prepare?.(mesh);
218
+ const prepared = this.prepare?.(mesh);
219
+ if (prepared !== void 0 && previous?.ready !== true) {
220
+ batch.ready = false;
221
+ const show = () => {
222
+ batch.ready = true;
223
+ };
224
+ prepared.then(show, show);
225
+ }
218
226
  return batch;
219
227
  }
220
228
  /** Number of instanced meshes (draw calls when all are populated). */
@@ -244,6 +252,22 @@ var ProxyBatches = class {
244
252
  function posed(walker) {
245
253
  return walker.mode === "sit" || walker.anchor !== void 0 && walker.anchor !== "origin";
246
254
  }
255
+ /**
256
+ * Leave a hidden body out of the scene's per-frame world-matrix walk. Pooled bodies wait hidden
257
+ * in the group with their skeletons, and three walks hidden objects too: a pool's bones were
258
+ * recomposed every frame. A move while hidden is applied when the body shows again.
259
+ */
260
+ function skipWhenHidden(object) {
261
+ object.updateMatrixWorld = function(force) {
262
+ if (!this.visible) {
263
+ if (force === true) this.matrixWorldNeedsUpdate = true;
264
+ return;
265
+ }
266
+ THREE.Object3D.prototype.updateMatrixWorld.call(this, force);
267
+ };
268
+ }
269
+ /** Bodies built or swapped per frame (see AmbientFigures.update). */
270
+ const MAX_SWITCHES_PER_FRAME = 2;
247
271
  var AmbientFigures = class {
248
272
  prepare;
249
273
  group = new THREE.Group();
@@ -263,6 +287,8 @@ var AmbientFigures = class {
263
287
  };
264
288
  cursor = 0;
265
289
  frame = 0;
290
+ /** New bodies whose shaders are still being built; they stay hidden until then. */
291
+ preparing = /* @__PURE__ */ new WeakSet();
266
292
  constructor(prepare) {
267
293
  this.prepare = prepare;
268
294
  this.group.name = "ambient:figures";
@@ -292,20 +318,30 @@ var AmbientFigures = class {
292
318
  if (pooled !== void 0) {
293
319
  this.pooled--;
294
320
  this.cache.release(cached.key);
295
- pooled.root.visible = true;
321
+ pooled.root.visible = !this.preparing.has(pooled);
296
322
  return {
297
323
  key: cached.key,
298
324
  mesh: pooled
299
325
  };
300
326
  }
301
327
  const mesh = figureBodyToSkinnedMesh(cached.body, cached.geometry, this.material);
328
+ skipWhenHidden(mesh.root);
302
329
  mesh.mesh.castShadow = tier === 0;
303
330
  mesh.mesh.receiveShadow = true;
304
331
  mesh.root.traverse((object) => {
305
332
  object.userData.walkIgnore = true;
306
333
  });
307
334
  this.group.add(mesh.root);
308
- this.prepare?.(mesh.root);
335
+ const prepared = this.prepare?.(mesh.root);
336
+ if (prepared !== void 0) {
337
+ mesh.root.visible = false;
338
+ this.preparing.add(mesh);
339
+ const show = () => {
340
+ this.preparing.delete(mesh);
341
+ if (!this.pool.get(cached.key)?.includes(mesh)) mesh.root.visible = true;
342
+ };
343
+ prepared.then(show, show);
344
+ }
309
345
  return {
310
346
  key: cached.key,
311
347
  mesh
@@ -332,6 +368,7 @@ var AmbientFigures = class {
332
368
  distance: Math.hypot(w.pos[0] - camera[0], w.pos[1] - camera[1], w.pos[2] - camera[2])
333
369
  })).sort((a, b) => a.distance - b.distance || (a.w.id < b.w.id ? -1 : 1));
334
370
  let skinned = 0;
371
+ let switches = 0;
335
372
  for (const { w, distance } of ranked) {
336
373
  const entry = this.active.get(w.id);
337
374
  let tier = this.tierFor(distance, entry?.tier);
@@ -346,7 +383,8 @@ var AmbientFigures = class {
346
383
  continue;
347
384
  }
348
385
  let current = entry;
349
- if (current === void 0 || current.tier !== tier) {
386
+ if ((current === void 0 || current.tier !== tier) && switches < MAX_SWITCHES_PER_FRAME) {
387
+ switches++;
350
388
  let descriptor = this.descriptors.get(w.figure);
351
389
  if (descriptor === void 0) {
352
390
  try {
@@ -373,6 +411,7 @@ var AmbientFigures = class {
373
411
  };
374
412
  this.active.set(w.id, current);
375
413
  }
414
+ if (current === void 0) continue;
376
415
  current.distance = distance;
377
416
  current.seen = this.frame;
378
417
  if (current.last !== void 0) current.walked += Math.hypot(w.pos[0] - current.last[0], w.pos[2] - current.last[1]);
@@ -521,10 +560,6 @@ function ambientVehicleKind(options = {}) {
521
560
  }
522
561
  //#endregion
523
562
  //#region src/client/aircraft.ts
524
- /**
525
- * Aircraft overhead: content models (propellers or fans turning) when the host supplies them,
526
- * otherwise instanced proxies drawn by the vehicle batches.
527
- */
528
563
  /** Node names the aircraft visual moves or toggles; everything else on an ambient copy is still. */
529
564
  function movingNodes(spec) {
530
565
  const visual = spec.visual;
@@ -539,58 +574,161 @@ function movingNodes(spec) {
539
574
  ]);
540
575
  }
541
576
  /**
542
- * An aircraft overhead is seen from hundreds of meters, so its hundreds of still parts (fuselage,
543
- * wings, canopy frame, cockpit) merge into one mesh per material: a copy costs a draw per
544
- * material plus its moving parts, instead of one per part (281 for the P-51). Moving parts keep
545
- * their nodes and bindings. The visual is built without the interior, which is never animated
546
- * overhead. Exported for tests.
577
+ * A float copy of an attribute (quantized and interleaved ones included), its points moved by
578
+ * `points` or its directions turned by `directions` and renormalized. Read straight from the
579
+ * array: per-component accessors made copying an airliner's parts tens of milliseconds.
580
+ */
581
+ function transformedCopy(attribute, points, directions) {
582
+ const { count, itemSize } = attribute;
583
+ const interleaved = attribute.isInterleavedBufferAttribute;
584
+ const data = interleaved ? attribute.data : void 0;
585
+ const array = data?.array ?? attribute.array;
586
+ const stride = data?.stride ?? itemSize;
587
+ const offset = interleaved ? attribute.offset : 0;
588
+ const divisor = !attribute.normalized ? 1 : array instanceof Int8Array || array instanceof Uint8Array || array instanceof Uint8ClampedArray ? array instanceof Int8Array ? 127 : 255 : array instanceof Int16Array || array instanceof Uint16Array ? array instanceof Int16Array ? 32767 : 65535 : array instanceof Int32Array ? 2147483647 : array instanceof Uint32Array ? 4294967295 : 1;
589
+ const signed = array instanceof Int8Array || array instanceof Int16Array || array instanceof Int32Array;
590
+ const out = new Float32Array(count * itemSize);
591
+ for (let i = 0, o = 0; i < count; i++, o += itemSize) {
592
+ const base = offset + i * stride;
593
+ for (let c = 0; c < itemSize; c++) {
594
+ const value = (array[base + c] ?? 0) / divisor;
595
+ out[o + c] = signed && value < -1 ? -1 : value;
596
+ }
597
+ }
598
+ if (itemSize < 3) return out;
599
+ if (points !== void 0) {
600
+ const e = points.elements;
601
+ for (let o = 0; o < out.length; o += itemSize) {
602
+ const x = out[o] ?? 0;
603
+ const y = out[o + 1] ?? 0;
604
+ const z = out[o + 2] ?? 0;
605
+ out[o] = (e[0] ?? 0) * x + (e[4] ?? 0) * y + (e[8] ?? 0) * z + (e[12] ?? 0);
606
+ out[o + 1] = (e[1] ?? 0) * x + (e[5] ?? 0) * y + (e[9] ?? 0) * z + (e[13] ?? 0);
607
+ out[o + 2] = (e[2] ?? 0) * x + (e[6] ?? 0) * y + (e[10] ?? 0) * z + (e[14] ?? 0);
608
+ }
609
+ } else if (directions !== void 0) {
610
+ const e = directions.elements;
611
+ for (let o = 0; o < out.length; o += itemSize) {
612
+ const x = out[o] ?? 0;
613
+ const y = out[o + 1] ?? 0;
614
+ const z = out[o + 2] ?? 0;
615
+ const nx = (e[0] ?? 0) * x + (e[3] ?? 0) * y + (e[6] ?? 0) * z;
616
+ const ny = (e[1] ?? 0) * x + (e[4] ?? 0) * y + (e[7] ?? 0) * z;
617
+ const nz = (e[2] ?? 0) * x + (e[5] ?? 0) * y + (e[8] ?? 0) * z;
618
+ const length = Math.hypot(nx, ny, nz) || 1;
619
+ out[o] = nx / length;
620
+ out[o + 1] = ny / length;
621
+ out[o + 2] = nz / length;
622
+ }
623
+ }
624
+ return out;
625
+ }
626
+ /**
627
+ * Drop the cabin (the spec's interior nodes) from an ambient copy, which is never seen from
628
+ * inside. Its animated instruments are moving parts the merge keeps: hundreds of separate digits
629
+ * and needles on each aircraft overhead, every one updated and culled in every frame.
547
630
  */
548
- function mergeStaticAircraftParts(model, spec) {
631
+ function withoutCabin(model, spec) {
632
+ const names = new Set(spec.visual.interior?.nodes ?? []);
633
+ if (names.size === 0) return;
634
+ const cabin = [];
635
+ model.traverse((node) => {
636
+ if (names.has(node.name)) cabin.push(node);
637
+ });
638
+ for (const node of cabin) node.removeFromParent();
639
+ }
640
+ /** The merge, yielding after each part so a caller can spread it over several tasks. */
641
+ function* mergeSteps(model, spec) {
642
+ withoutCabin(model, spec);
549
643
  const moving = movingNodes(spec);
550
644
  model.updateMatrixWorld(true);
551
- const toModel = new THREE.Matrix4().copy(model.matrixWorld).invert();
645
+ const roots = [{
646
+ root: model,
647
+ minimum: 1
648
+ }];
649
+ model.traverse((object) => {
650
+ if (object !== model && moving.has(object.name)) roots.push({
651
+ root: object,
652
+ minimum: 2
653
+ });
654
+ });
655
+ for (const { root, minimum } of roots) yield* mergeUnder(root, moving, minimum);
656
+ }
657
+ /**
658
+ * Merge the still meshes under `root`, down to the next moving part, into one mesh per material
659
+ * in `root`'s frame. Materials with fewer than `minimum` meshes stay as they are.
660
+ */
661
+ function* mergeUnder(root, moving, minimum) {
662
+ const toRoot = new THREE.Matrix4().copy(root.matrixWorld).invert();
663
+ const transform = new THREE.Matrix4();
664
+ const normalTransform = new THREE.Matrix3();
552
665
  const groups = /* @__PURE__ */ new Map();
553
- const merged = [];
554
666
  const visit = (object) => {
555
- if (moving.has(object.name)) return;
556
- for (const child of [...object.children]) visit(child);
667
+ if (object !== root && moving.has(object.name)) return;
668
+ for (const child of object.children) visit(child);
669
+ if (object === root) return;
557
670
  const mesh = object;
558
671
  if (!mesh.isMesh || Array.isArray(mesh.material) || mesh.isSkinnedMesh) return;
559
672
  const source = mesh.geometry;
560
673
  if (source.morphAttributes.position !== void 0) return;
561
- const geometry = new THREE.BufferGeometry();
562
- for (const name of [
674
+ const names = [
563
675
  "position",
564
676
  "normal",
565
677
  "color"
566
- ]) {
567
- const attribute = source.getAttribute(name);
568
- if (attribute === void 0) continue;
569
- const copy = new Float32Array(attribute.count * attribute.itemSize);
570
- for (let i = 0; i < attribute.count; i++) for (let c = 0; c < attribute.itemSize; c++) copy[i * attribute.itemSize + c] = attribute.getComponent(i, c);
571
- geometry.setAttribute(name, new THREE.BufferAttribute(copy, attribute.itemSize));
572
- }
573
- if (source.index !== null) geometry.setIndex(source.index.clone());
574
- geometry.applyMatrix4(new THREE.Matrix4().multiplyMatrices(toModel, mesh.matrixWorld));
575
- const key = `${mesh.material.uuid}|${[...Object.keys(geometry.attributes)].sort().join()}|${geometry.index !== null}`;
678
+ ].filter((name) => source.getAttribute(name));
679
+ const key = `${mesh.material.uuid}|${names.sort().join()}|${source.index !== null}`;
576
680
  const group = groups.get(key) ?? {
577
681
  material: mesh.material,
578
- parts: []
682
+ meshes: []
579
683
  };
580
- group.parts.push(geometry);
684
+ group.meshes.push(mesh);
581
685
  groups.set(key, group);
582
- mesh.removeFromParent();
583
686
  };
584
- visit(model);
585
- for (const { material, parts } of groups.values()) {
687
+ visit(root);
688
+ const merged = [];
689
+ for (const { material, meshes } of groups.values()) {
690
+ if (meshes.length < minimum) continue;
691
+ const parts = [];
692
+ for (const mesh of meshes) {
693
+ const source = mesh.geometry;
694
+ transform.multiplyMatrices(toRoot, mesh.matrixWorld);
695
+ normalTransform.getNormalMatrix(transform);
696
+ const geometry = new THREE.BufferGeometry();
697
+ for (const name of [
698
+ "position",
699
+ "normal",
700
+ "color"
701
+ ]) {
702
+ const attribute = source.getAttribute(name);
703
+ if (attribute === void 0) continue;
704
+ const copy = transformedCopy(attribute, name === "position" ? transform : void 0, name === "normal" ? normalTransform : void 0);
705
+ geometry.setAttribute(name, new THREE.BufferAttribute(copy, attribute.itemSize));
706
+ }
707
+ if (source.index !== null) geometry.setIndex(source.index.clone());
708
+ parts.push(geometry);
709
+ mesh.removeFromParent();
710
+ yield;
711
+ }
586
712
  const geometry = mergeGeometries(parts, false);
587
713
  for (const part of parts) part.dispose();
714
+ yield;
588
715
  if (geometry === null) continue;
589
716
  const mesh = new THREE.Mesh(geometry, material);
590
717
  mesh.name = `ambient-merged:${material.name}`;
591
718
  merged.push(mesh);
592
719
  }
593
- for (const mesh of merged) model.add(mesh);
720
+ for (const mesh of merged) root.add(mesh);
721
+ }
722
+ /** Main-thread time (ms) a template merge takes before giving the frame back. */
723
+ const MERGE_SLICE_MS = 3;
724
+ /** The merge in slices: an airliner's still parts held a frame for 40-60 ms in one go. */
725
+ async function mergeStaticAircraftPartsSliced(model, spec) {
726
+ const steps = mergeSteps(model, spec);
727
+ for (;;) {
728
+ const start = performance.now();
729
+ while (performance.now() - start < MERGE_SLICE_MS) if (steps.next().done === true) return;
730
+ await new Promise((resolve) => setTimeout(resolve, 0));
731
+ }
594
732
  }
595
733
  /** Move toward a selection at `time` seconds for full travel; 0, unset or a first sight is instant. */
596
734
  function travel(position, selected, time, dt) {
@@ -613,14 +751,22 @@ var AmbientAircraft = class {
613
751
  models;
614
752
  parent;
615
753
  prepare;
754
+ admission;
616
755
  active = /* @__PURE__ */ new Map();
617
756
  loading = /* @__PURE__ */ new Set();
618
757
  missing = /* @__PURE__ */ new Set();
619
758
  disposed = false;
620
- constructor(models, parent, prepare) {
759
+ /**
760
+ * One parsed and merged model per type. Each aircraft is a clone sharing its geometry and
761
+ * materials: a jet's spawn rings replace distant traffic every few seconds, and parsing and
762
+ * merging the model again for each one held the main thread for tens of milliseconds.
763
+ */
764
+ templates = /* @__PURE__ */ new Map();
765
+ constructor(models, parent, prepare, admission) {
621
766
  this.models = models;
622
767
  this.parent = parent;
623
768
  this.prepare = prepare;
769
+ this.admission = admission;
624
770
  }
625
771
  /** Whether a type draws as a model (otherwise the caller draws a proxy). */
626
772
  modelled(id) {
@@ -662,10 +808,21 @@ var AmbientAircraft = class {
662
808
  }
663
809
  for (const [id, entry] of this.active) if (!seen.has(id)) {
664
810
  entry.visual.object.removeFromParent();
665
- entry.visual.dispose();
666
811
  this.active.delete(id);
667
812
  }
668
813
  }
814
+ template(type, spec) {
815
+ let template = this.templates.get(type);
816
+ if (template === void 0) {
817
+ template = this.models.load(type).then(async (model) => {
818
+ if (model !== void 0) await mergeStaticAircraftPartsSliced(model, spec);
819
+ return model;
820
+ });
821
+ this.templates.set(type, template);
822
+ template.catch(() => this.templates.delete(type));
823
+ }
824
+ return template;
825
+ }
669
826
  async load(id, type) {
670
827
  const spec = this.models.aircraft(type);
671
828
  if (spec === void 0) {
@@ -674,30 +831,36 @@ var AmbientAircraft = class {
674
831
  }
675
832
  this.loading.add(id);
676
833
  try {
677
- const model = await this.models.load(type);
678
- if (model === void 0) {
834
+ const template = await this.template(type, spec);
835
+ if (template === void 0) {
679
836
  this.missing.add(type);
680
837
  return;
681
838
  }
682
839
  if (this.disposed) return;
683
- mergeStaticAircraftParts(model, spec);
684
- const visual = createAircraftVisual(model, {
685
- ...spec,
686
- visual: {
687
- ...spec.visual,
688
- interior: void 0
689
- }
690
- });
691
- visual.object.traverse((object) => {
692
- object.userData.walkIgnore = true;
693
- object.castShadow = false;
694
- });
840
+ const create = () => {
841
+ const created = createAircraftVisual(template.clone(true), {
842
+ ...spec,
843
+ visual: {
844
+ ...spec.visual,
845
+ interior: void 0
846
+ }
847
+ });
848
+ created.object.traverse((object) => {
849
+ object.userData.walkIgnore = true;
850
+ object.castShadow = false;
851
+ });
852
+ return created;
853
+ };
854
+ const visual = this.admission ? await this.admission.run(create, { label: "ambient-aircraft" }) : create();
855
+ if (this.disposed) return;
856
+ visual.object.visible = false;
695
857
  this.parent.add(visual.object);
696
858
  await this.prepare?.(visual.object);
697
859
  if (this.disposed) {
698
- visual.dispose();
860
+ visual.object.removeFromParent();
699
861
  return;
700
862
  }
863
+ visual.object.visible = true;
701
864
  this.active.set(id, {
702
865
  visual,
703
866
  rotor: 0,
@@ -716,11 +879,17 @@ var AmbientAircraft = class {
716
879
  }
717
880
  dispose() {
718
881
  this.disposed = true;
719
- for (const entry of this.active.values()) {
720
- entry.visual.object.removeFromParent();
721
- entry.visual.dispose();
722
- }
882
+ for (const entry of this.active.values()) entry.visual.object.removeFromParent();
723
883
  this.active.clear();
884
+ for (const template of this.templates.values()) template.then((model) => {
885
+ model?.traverse((object) => {
886
+ const mesh = object;
887
+ if (!mesh.isMesh) return;
888
+ mesh.geometry.dispose();
889
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) material.dispose();
890
+ });
891
+ }, () => {});
892
+ this.templates.clear();
724
893
  }
725
894
  };
726
895
  //#endregion
@@ -859,6 +1028,22 @@ var VehicleDetail = class {
859
1028
  has(id) {
860
1029
  return this.active.has(id);
861
1030
  }
1031
+ /**
1032
+ * Pool the models of agents that are gone (call every frame): a car a host takes out of traffic
1033
+ * leaves in the frame its replacement shows, not up to a `select` later as a double.
1034
+ */
1035
+ dropGone(exists) {
1036
+ for (const [id, entry] of this.active) if (!exists(id)) this.idleOut(id, entry);
1037
+ }
1038
+ idleOut(id, entry) {
1039
+ entry.visual.object.visible = false;
1040
+ this.active.delete(id);
1041
+ const pool = this.idle.get(entry.type) ?? [];
1042
+ pool.push(entry.visual);
1043
+ this.idle.set(entry.type, pool);
1044
+ const resident = this.resident.get(entry.visual);
1045
+ if (resident) resident.used = ++this.clock;
1046
+ }
862
1047
  get count() {
863
1048
  return this.active.size;
864
1049
  }
@@ -871,15 +1056,7 @@ var VehicleDetail = class {
871
1056
  this.candidates = wanted;
872
1057
  this.wanted = new Set(wanted.map((c) => c.type));
873
1058
  for (const [type, controller] of this.loading) if (!this.wanted.has(type)) controller.abort();
874
- for (const [id, entry] of this.active) if (!keep.has(id) || !exists(id)) {
875
- entry.visual.object.visible = false;
876
- this.active.delete(id);
877
- const pool = this.idle.get(entry.type) ?? [];
878
- pool.push(entry.visual);
879
- this.idle.set(entry.type, pool);
880
- const resident = this.resident.get(entry.visual);
881
- if (resident) resident.used = ++this.clock;
882
- }
1059
+ for (const [id, entry] of this.active) if (!keep.has(id) || !exists(id)) this.idleOut(id, entry);
883
1060
  let started = 0;
884
1061
  for (const candidate of wanted) {
885
1062
  if (this.active.has(candidate.id)) continue;
@@ -1022,10 +1199,7 @@ function createAmbientRenderer(options) {
1022
1199
  const object = new THREE.Group();
1023
1200
  object.name = "ambient";
1024
1201
  options.parent.add(object);
1025
- const prepareSync = (target) => {
1026
- options.prepare?.(target);
1027
- };
1028
- const batches = new ProxyBatches(prepareSync);
1202
+ const batches = new ProxyBatches(options.prepare);
1029
1203
  object.add(batches.group);
1030
1204
  const detail = options.vehicles !== void 0 ? new VehicleDetail({
1031
1205
  models: options.vehicles,
@@ -1039,9 +1213,9 @@ function createAmbientRenderer(options) {
1039
1213
  far: 1500,
1040
1214
  ...options.prepare !== void 0 ? { prepare: options.prepare } : {}
1041
1215
  }) : void 0;
1042
- const figures = new AmbientFigures(prepareSync);
1216
+ const figures = new AmbientFigures(options.prepare);
1043
1217
  object.add(figures.group);
1044
- const aircraft = options.aircraft !== void 0 ? new AmbientAircraft(options.aircraft, object, options.prepare) : void 0;
1218
+ const aircraft = options.aircraft !== void 0 ? new AmbientAircraft(options.aircraft, object, options.prepare, options.admission) : void 0;
1045
1219
  let budget = {
1046
1220
  ...DEFAULT_BUDGET,
1047
1221
  ...options.budget
@@ -1103,6 +1277,9 @@ function createAmbientRenderer(options) {
1103
1277
  aircraft: 0,
1104
1278
  boats: 0
1105
1279
  };
1280
+ const agentExists = (id) => world.has(id, AmbientAgent);
1281
+ detail?.dropGone(agentExists);
1282
+ boatDetail?.dropGone(agentExists);
1106
1283
  for (const [id, a, t] of world.query(AmbientAgent, Transform)) {
1107
1284
  const agent = a;
1108
1285
  if (agent.kind === "pedestrian") {
@@ -1254,8 +1431,41 @@ function wildlifeGeometry(species, tier) {
1254
1431
  source.dispose();
1255
1432
  if (source !== geometry) geometry.dispose();
1256
1433
  };
1257
- const ellipsoid = (center, size, color = body.color, bone = 0) => {
1258
- add(new THREE.SphereGeometry(1, tier === 0 ? 8 : tier === 1 ? 6 : 4, tier === 0 ? 5 : tier === 1 ? 4 : 3), new THREE.Matrix4().compose(new THREE.Vector3(...center), new THREE.Quaternion(), new THREE.Vector3(...size)), color, bone);
1434
+ const ellipsoid = (center, size, color = body.color, bone = 0, pitch = 0) => {
1435
+ add(new THREE.SphereGeometry(1, tier === 0 ? 8 : tier === 1 ? 6 : 4, tier === 0 ? 5 : tier === 1 ? 4 : 3), new THREE.Matrix4().compose(new THREE.Vector3(...center), new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), pitch), new THREE.Vector3(...size)), color, bone);
1436
+ };
1437
+ const stripe = (z) => Math.sin(z / l * Math.PI * 2 * 8.5) > .15 ? body.accent : body.color;
1438
+ const barrel = (center, size) => {
1439
+ const rings = tier === 0 ? 14 : 8, sides = tier === 0 ? 8 : 6;
1440
+ const ring = (i) => {
1441
+ const u = -1 + 2 * i / rings, r = Math.sqrt(Math.max(0, 1 - u * u));
1442
+ return Array.from({ length: sides }, (_, k) => {
1443
+ const a = k / sides * Math.PI * 2;
1444
+ return [
1445
+ center[0] + Math.cos(a) * size[0] * r,
1446
+ center[1] + Math.sin(a) * size[1] * r,
1447
+ center[2] + u * size[2]
1448
+ ];
1449
+ });
1450
+ };
1451
+ for (let i = 0; i < rings; i++) {
1452
+ const [a, b] = [ring(i), ring(i + 1)];
1453
+ const positions = [];
1454
+ for (let k = 0; k < sides; k++) {
1455
+ const n = (k + 1) % sides;
1456
+ const [p, q, r, s] = [
1457
+ a[k],
1458
+ b[k],
1459
+ b[n],
1460
+ a[n]
1461
+ ];
1462
+ if (i < rings - 1) positions.push(...p, ...r, ...q);
1463
+ if (i > 0) positions.push(...p, ...s, ...r);
1464
+ }
1465
+ const geometry = new THREE.BufferGeometry();
1466
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
1467
+ add(geometry, new THREE.Matrix4(), stripe(center[2] + (-1 + (2 * i + 1) / rings) * size[2]));
1468
+ }
1259
1469
  };
1260
1470
  const segment = (a, b, r1, r2, color = body.color, bone = 0) => {
1261
1471
  const start = new THREE.Vector3(...a), end = new THREE.Vector3(...b), delta = end.clone().sub(start);
@@ -1378,7 +1588,7 @@ function wildlifeGeometry(species, tier) {
1378
1588
  ]);
1379
1589
  if (flying) {
1380
1590
  const span = l * (detail("raptor") ? 1.65 : insect ? .9 : 1.05);
1381
- wedge([
1591
+ if (insect) wedge([
1382
1592
  [
1383
1593
  side * w * .25,
1384
1594
  y,
@@ -1395,6 +1605,49 @@ function wildlifeGeometry(species, tier) {
1395
1605
  -l * .42
1396
1606
  ]
1397
1607
  ], h * .045, body.accent, wing);
1608
+ else {
1609
+ const lift = h * (detail("raptor") ? .06 : .14);
1610
+ const rootFront = [
1611
+ side * w * .25,
1612
+ y,
1613
+ l * .22
1614
+ ];
1615
+ const rootBack = [
1616
+ side * w * .35,
1617
+ y,
1618
+ -l * .3
1619
+ ];
1620
+ const wrist = [
1621
+ side * span * .48,
1622
+ y + lift,
1623
+ l * .06
1624
+ ];
1625
+ const wristBack = [
1626
+ side * span * .5,
1627
+ y + lift,
1628
+ -l * .24
1629
+ ];
1630
+ const tip = [
1631
+ side * span,
1632
+ y + h * .02,
1633
+ -l * .16
1634
+ ];
1635
+ wedge([
1636
+ rootFront,
1637
+ wrist,
1638
+ rootBack
1639
+ ], h * .045, body.accent, wing);
1640
+ wedge([
1641
+ wrist,
1642
+ wristBack,
1643
+ rootBack
1644
+ ], h * .045, body.accent, wing);
1645
+ wedge([
1646
+ wrist,
1647
+ tip,
1648
+ wristBack
1649
+ ], h * .04, body.accent, wing);
1650
+ }
1398
1651
  if (tier === 0 && !insect) for (let i = 0; i < 3; i++) wedge([
1399
1652
  [
1400
1653
  side * span * (.65 + i * .1),
@@ -1581,10 +1834,11 @@ function wildlifeGeometry(species, tier) {
1581
1834
  -l * 1.3
1582
1835
  ], w * .1, w * .005, body.color, tail);
1583
1836
  } else {
1584
- const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant";
1837
+ const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant", ungulate = body.family === "ungulate";
1585
1838
  const torsoY = h * (hopper ? .57 : low ? .5 : .72);
1586
1839
  const torsoRadius = h * (hopper ? .3 : low ? .31 : elephant ? .32 : .19);
1587
- ellipsoid([
1840
+ const mass = detail("stripes") && tier < 2 ? barrel : ellipsoid;
1841
+ mass([
1588
1842
  0,
1589
1843
  torsoY,
1590
1844
  -l * .04
@@ -1593,6 +1847,26 @@ function wildlifeGeometry(species, tier) {
1593
1847
  torsoRadius,
1594
1848
  l * (hopper ? .28 : .38)
1595
1849
  ]);
1850
+ if (!hopper && tier < 2) {
1851
+ mass([
1852
+ 0,
1853
+ torsoY - torsoRadius * .08,
1854
+ l * .2
1855
+ ], [
1856
+ w * .47,
1857
+ torsoRadius * 1.1,
1858
+ l * .17
1859
+ ]);
1860
+ mass([
1861
+ 0,
1862
+ torsoY + torsoRadius * .06,
1863
+ -l * .27
1864
+ ], [
1865
+ w * .48,
1866
+ torsoRadius * .98,
1867
+ l * .15
1868
+ ]);
1869
+ }
1596
1870
  if (tier === 0) ellipsoid([
1597
1871
  0,
1598
1872
  torsoY - torsoRadius * .48,
@@ -1604,6 +1878,15 @@ function wildlifeGeometry(species, tier) {
1604
1878
  ], elephant ? body.color : body.accent);
1605
1879
  const neckTop = h * (hopper ? .92 : low ? .61 : elephant ? .9 : detail("long-neck") ? 1.38 : 1.1);
1606
1880
  const headZ = l * (hopper ? .3 : .47), headR = h * (elephant ? .18 : low ? .23 : .13);
1881
+ if (detail("palmate") && tier < 2) ellipsoid([
1882
+ 0,
1883
+ torsoY + torsoRadius * .75,
1884
+ l * .19
1885
+ ], [
1886
+ w * .3,
1887
+ torsoRadius * .55,
1888
+ l * .15
1889
+ ], body.color);
1607
1890
  segment([
1608
1891
  0,
1609
1892
  torsoY,
@@ -1613,6 +1896,19 @@ function wildlifeGeometry(species, tier) {
1613
1896
  neckTop - headR * .4,
1614
1897
  headZ - l * .07
1615
1898
  ], w * (elephant ? .4 : low ? .32 : .23), w * (low ? .28 : .17));
1899
+ if (detail("mane") && tier < 2) {
1900
+ const dy = neckTop - headR * .4 - torsoY, dz = headZ - l * .34;
1901
+ const pitch = Math.atan2(-dy, dz), lift = w * .19;
1902
+ ellipsoid([
1903
+ 0,
1904
+ torsoY + dy / 2 + Math.cos(pitch) * lift,
1905
+ l * .27 + dz / 2 + Math.sin(pitch) * lift
1906
+ ], [
1907
+ w * .035,
1908
+ h * .06,
1909
+ Math.hypot(dy, dz) * .55
1910
+ ], body.accent, 0, pitch);
1911
+ }
1616
1912
  const head = joint("head", [
1617
1913
  0,
1618
1914
  neckTop - headR * .6,
@@ -1627,15 +1923,36 @@ function wildlifeGeometry(species, tier) {
1627
1923
  headR,
1628
1924
  l * (elephant || low ? .16 : .12)
1629
1925
  ], body.color, head);
1630
- if (!elephant) ellipsoid([
1631
- 0,
1632
- neckTop - headR * .28,
1633
- headZ + l * .11
1634
- ], [
1635
- w * .17,
1636
- headR * .55,
1637
- l * .1
1638
- ], body.accent, head);
1926
+ if (!elephant) {
1927
+ const long = detail("palmate") ? 1.3 : 1;
1928
+ ellipsoid([
1929
+ 0,
1930
+ neckTop - headR * (ungulate ? .38 : .28),
1931
+ headZ + l * .1 * long
1932
+ ], [
1933
+ w * (detail("palmate") ? .2 : .16),
1934
+ headR * .52,
1935
+ l * .075 * long
1936
+ ], ungulate ? body.color : body.accent, head);
1937
+ if (ungulate && tier === 0) ellipsoid([
1938
+ 0,
1939
+ neckTop - headR * .36,
1940
+ headZ + l * .17 * long
1941
+ ], [
1942
+ w * .12,
1943
+ headR * .3,
1944
+ l * .012
1945
+ ], "#2e2a26", head);
1946
+ if (detail("palmate") && tier < 2) ellipsoid([
1947
+ 0,
1948
+ neckTop - headR * 1.2,
1949
+ headZ - l * .02
1950
+ ], [
1951
+ w * .05,
1952
+ headR * .6,
1953
+ l * .03
1954
+ ], body.color, head);
1955
+ }
1639
1956
  for (const side of [-1, 1]) {
1640
1957
  const earHeight = body.family === "lagomorph" ? h * .45 : hopper ? h * .2 : elephant ? h * .25 : low ? h * .075 : h * .11;
1641
1958
  ellipsoid([
@@ -1646,31 +1963,93 @@ function wildlifeGeometry(species, tier) {
1646
1963
  w * (elephant ? .35 : .1),
1647
1964
  earHeight,
1648
1965
  l * .035
1649
- ], elephant ? body.color : body.accent, head);
1966
+ ], elephant || detail("stripes") ? body.color : body.accent, head);
1650
1967
  eye(side * w * (low ? .3 : .25), neckTop + headR * .1, headZ + l * .09, headR * .1, head);
1651
- if (detail("horns") || detail("antlers")) {
1652
- const base = [
1653
- side * w * .22,
1654
- neckTop + headR * .8,
1655
- headZ
1968
+ const horn = "#817963";
1969
+ const base = [
1970
+ side * w * .2,
1971
+ neckTop + headR * .8,
1972
+ headZ - l * .02
1973
+ ];
1974
+ const along = (a, b, t) => [
1975
+ a[0] + (b[0] - a[0]) * t,
1976
+ a[1] + (b[1] - a[1]) * t,
1977
+ a[2] + (b[2] - a[2]) * t
1978
+ ];
1979
+ if (detail("palmate")) {
1980
+ const root = [
1981
+ side * w * .45,
1982
+ neckTop + h * .08,
1983
+ headZ - l * .05
1984
+ ];
1985
+ if (tier < 2) segment(base, root, w * .05, w * .04, horn, head);
1986
+ const front = [
1987
+ side * w * 1.05,
1988
+ neckTop + h * .2,
1989
+ headZ + l * .02
1990
+ ];
1991
+ const back = [
1992
+ side * w * 1,
1993
+ neckTop + h * .16,
1994
+ headZ - l * .16
1995
+ ];
1996
+ wedge([
1997
+ root,
1998
+ front,
1999
+ back
2000
+ ], h * .025, horn, head);
2001
+ if (tier < 2) for (let i = 0; i < (tier === 0 ? 5 : 2); i++) {
2002
+ const at = along(front, back, (i + .5) / (tier === 0 ? 5 : 2));
2003
+ segment(at, [
2004
+ at[0] + side * w * .12,
2005
+ at[1] + h * .07,
2006
+ at[2]
2007
+ ], w * .025, w * .008, horn, head);
2008
+ }
2009
+ } else if (detail("antlers")) {
2010
+ const bend = [
2011
+ side * w * .5,
2012
+ neckTop + h * .2,
2013
+ headZ - l * .1
1656
2014
  ];
1657
2015
  const tip = [
1658
2016
  side * w * .42,
1659
- neckTop + h * .36,
1660
- headZ - l * .04
2017
+ neckTop + h * .38,
2018
+ headZ - l * .03
1661
2019
  ];
1662
- segment(base, tip, w * .045, w * .013, "#817963", head);
1663
- if (detail("antlers") && tier < 2) for (let branch = 0; branch < (tier === 0 ? 3 : 1); branch++) {
1664
- const t = .3 + branch * .2;
1665
- segment([
1666
- base[0] + (tip[0] - base[0]) * t,
1667
- base[1] + (tip[1] - base[1]) * t,
1668
- base[2] + (tip[2] - base[2]) * t
1669
- ], [
1670
- side * w * (.6 + branch * .1),
1671
- neckTop + h * (.18 + branch * .09),
1672
- headZ + l * .04
1673
- ], w * .027, w * .008, "#817963", head);
2020
+ if (tier === 2) segment(base, tip, w * .045, w * .013, horn, head);
2021
+ else {
2022
+ segment(base, bend, w * .045, w * .032, horn, head);
2023
+ segment(bend, tip, w * .032, w * .012, horn, head);
2024
+ }
2025
+ if (tier < 2) for (let branch = 0; branch < (tier === 0 ? 3 : 1); branch++) {
2026
+ const from = branch === 0 ? along(base, bend, .45) : along(bend, tip, branch === 1 ? .2 : .6);
2027
+ segment(from, [
2028
+ from[0] + side * w * .04,
2029
+ from[1] + h * .11,
2030
+ from[2] + l * .06
2031
+ ], w * .024, w * .008, horn, head);
2032
+ }
2033
+ } else if (detail("straight-horns")) segment(base, [
2034
+ side * w * .16,
2035
+ neckTop + h * .5,
2036
+ headZ - l * .28
2037
+ ], w * .04, w * .01, horn, head);
2038
+ else if (detail("horns")) {
2039
+ const mid = [
2040
+ side * w * .36,
2041
+ neckTop + h * .2,
2042
+ headZ - l * .08
2043
+ ];
2044
+ const tip = [
2045
+ side * w * .26,
2046
+ neckTop + h * .38,
2047
+ headZ - l * .02
2048
+ ];
2049
+ if (tier === 2) segment(base, tip, w * .042, w * .01, horn, head);
2050
+ else {
2051
+ segment(base, mid, w * .042, w * .028, horn, head);
2052
+ segment(mid, tip, w * .028, w * .01, horn, head);
1674
2053
  }
1675
2054
  }
1676
2055
  for (const front of [false, true]) {
@@ -1682,27 +2061,83 @@ function wildlifeGeometry(species, tier) {
1682
2061
  ];
1683
2062
  const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1684
2063
  const footY = hopper && front ? h * .42 : h * .055;
1685
- const knee = [
1686
- pivot[0],
1687
- (pivot[1] + footY) * .5,
1688
- legZ + (hopper && !front ? l * .14 : -l * .02)
1689
- ];
1690
2064
  const radius = w * (elephant ? .2 : hopper && !front ? .22 : low ? .13 : .1);
1691
- segment(pivot, knee, radius, radius * .7, body.color, leg);
1692
- segment(knee, [
1693
- pivot[0],
1694
- footY,
1695
- legZ
1696
- ], radius * .7, radius * .46, body.color, leg);
1697
- if (tier < 2) ellipsoid([
1698
- pivot[0],
1699
- footY,
1700
- legZ + (hopper && !front ? l * .12 : l * .035)
1701
- ], [
1702
- radius * .65,
1703
- Math.max(h * .025, footY * .65),
1704
- l * (hopper && !front ? .16 : .055)
1705
- ], elephant ? body.color : body.accent, leg);
2065
+ if (hopper || elephant || tier === 2) {
2066
+ const knee = [
2067
+ pivot[0],
2068
+ (pivot[1] + footY) * .5,
2069
+ legZ + (hopper && !front ? l * .14 : -l * .02)
2070
+ ];
2071
+ segment(pivot, knee, radius, radius * .7, body.color, leg);
2072
+ segment(knee, [
2073
+ pivot[0],
2074
+ footY,
2075
+ legZ
2076
+ ], radius * .7, radius * .46, body.color, leg);
2077
+ } else {
2078
+ const x = pivot[0];
2079
+ const bends = front ? [
2080
+ [
2081
+ x,
2082
+ torsoY - torsoRadius * .9,
2083
+ legZ - l * .035
2084
+ ],
2085
+ [
2086
+ x,
2087
+ h * (low ? .17 : .3),
2088
+ legZ - l * .005
2089
+ ],
2090
+ [
2091
+ x,
2092
+ footY + h * .035,
2093
+ legZ + l * .008
2094
+ ]
2095
+ ] : [
2096
+ [
2097
+ x,
2098
+ pivot[1] - (pivot[1] - footY) * .38,
2099
+ legZ + l * .07
2100
+ ],
2101
+ [
2102
+ x,
2103
+ h * (low ? .16 : .31),
2104
+ legZ - l * .075
2105
+ ],
2106
+ [
2107
+ x,
2108
+ footY + h * .035,
2109
+ legZ - l * .045
2110
+ ]
2111
+ ];
2112
+ const radii = front ? [
2113
+ 1.45,
2114
+ .95,
2115
+ .6,
2116
+ .5
2117
+ ] : [
2118
+ 1.6,
2119
+ 1.05,
2120
+ .6,
2121
+ .5
2122
+ ];
2123
+ let from = pivot;
2124
+ bends.forEach((to, i) => {
2125
+ segment(from, to, radius * radii[i], radius * radii[i + 1], body.color, leg);
2126
+ from = to;
2127
+ });
2128
+ }
2129
+ if (tier < 2) {
2130
+ const longFoot = (hopper || body.family === "lagomorph") && !front;
2131
+ ellipsoid([
2132
+ pivot[0],
2133
+ footY,
2134
+ legZ + (longFoot ? l * .08 : front ? l * .02 : -l * .03)
2135
+ ], [
2136
+ radius * .65,
2137
+ Math.max(h * .025, hopper && front ? footY * .65 : h * .03),
2138
+ l * (longFoot ? .14 : .055)
2139
+ ], elephant ? body.color : ungulate ? "#3a3530" : body.accent, leg);
2140
+ }
1706
2141
  }
1707
2142
  }
1708
2143
  const tail = joint("tail", [
@@ -1717,17 +2152,35 @@ function wildlifeGeometry(species, tier) {
1717
2152
  -l * .34
1718
2153
  ], [
1719
2154
  0,
1720
- h * 1.12,
1721
- -l * .65
1722
- ], w * .19, w * .16, body.color, tail);
2155
+ h * .62,
2156
+ -l * .6
2157
+ ], w * .19, w * .22, body.color, tail);
1723
2158
  ellipsoid([
1724
2159
  0,
1725
- h * 1,
1726
- -l * .6
2160
+ h * .72,
2161
+ -l * .66
2162
+ ], [
2163
+ w * .34,
2164
+ h * .3,
2165
+ l * .2
2166
+ ], body.color, tail);
2167
+ ellipsoid([
2168
+ 0,
2169
+ h * 1.08,
2170
+ -l * .62
1727
2171
  ], [
1728
2172
  w * .36,
1729
- h * .57,
1730
- l * .27
2173
+ h * .28,
2174
+ l * .2
2175
+ ], body.color, tail);
2176
+ if (tier < 2) ellipsoid([
2177
+ 0,
2178
+ h * 1.28,
2179
+ -l * .45
2180
+ ], [
2181
+ w * .3,
2182
+ h * .17,
2183
+ l * .18
1731
2184
  ], body.color, tail);
1732
2185
  } else if (detail("long-tail") || detail("bushy-tail") || [
1733
2186
  "canid",
@@ -1787,18 +2240,6 @@ function wildlifeGeometry(species, tier) {
1787
2240
  headZ + l * .22
1788
2241
  ], w * .09, w * .04, body.color, trunk);
1789
2242
  }
1790
- if (detail("stripes") && tier < 2) for (let i = 0; i < 6; i++) {
1791
- const z = -l * .25 + i * l * .09;
1792
- for (const side of [-1, 1]) segment([
1793
- side * w * .45,
1794
- torsoY - torsoRadius * .4,
1795
- z
1796
- ], [
1797
- side * w * .4,
1798
- torsoY + torsoRadius * .55,
1799
- z + l * .02
1800
- ], w * .025, w * .018, body.accent);
1801
- }
1802
2243
  }
1803
2244
  const geometry = new THREE.BufferGeometry();
1804
2245
  geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));