@bendyline/molen-ambient 0.0.11 → 0.0.13

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-D4HrizHN.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
@@ -730,12 +899,74 @@ var AmbientAircraft = class {
730
899
  * content model (wheels turning and steering), from a pool per type so a spawn never builds new
731
900
  * GPU objects once the pool is warm. Farther cars stay instanced proxies.
732
901
  */
902
+ /**
903
+ * Authored glazing is often nearly clear (about 16% opaque), so from the street a passing car
904
+ * reads as an open frame of pillars and seats. Traffic glass is tinted and glossy instead:
905
+ * darker, at least 60% opaque and smooth enough to pick up the sky. Only traffic: a car the
906
+ * player sits in keeps its authored glass. The visual's materials are its own clones.
907
+ */
908
+ function tintGlass(object) {
909
+ const mesh = object;
910
+ if (!mesh.isMesh) return;
911
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
912
+ if (!material.transparent || !/glass|glazing/i.test(material.name)) continue;
913
+ if (!(material instanceof THREE.MeshStandardMaterial)) continue;
914
+ material.color.multiplyScalar(.3);
915
+ material.opacity = Math.max(material.opacity, .62);
916
+ material.roughness = Math.min(material.roughness, .08);
917
+ }
918
+ }
919
+ /** Buffer residency estimate; textures add their decoded base level plus mip overhead. */
920
+ function modelBytes(object) {
921
+ const buffers = /* @__PURE__ */ new Set();
922
+ const textures = /* @__PURE__ */ new Set();
923
+ object.traverse((node) => {
924
+ const mesh = node;
925
+ if (!mesh.isMesh) return;
926
+ for (const attr of Object.values(mesh.geometry.attributes)) buffers.add(attr.array.buffer);
927
+ if (mesh.geometry.index) buffers.add(mesh.geometry.index.array.buffer);
928
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
929
+ });
930
+ let bytes = [...buffers].reduce((sum, buffer) => sum + buffer.byteLength, 0);
931
+ for (const texture of textures) {
932
+ const image = texture.image;
933
+ bytes += (image?.width ?? 1) * (image?.height ?? 1) * 4 * 4 / 3;
934
+ }
935
+ return bytes;
936
+ }
937
+ function disposeUnbound(model) {
938
+ const geometries = /* @__PURE__ */ new Set();
939
+ const materials = /* @__PURE__ */ new Set();
940
+ const textures = /* @__PURE__ */ new Set();
941
+ model.traverse((node) => {
942
+ const mesh = node;
943
+ if (!mesh.isMesh) return;
944
+ geometries.add(mesh.geometry);
945
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
946
+ materials.add(material);
947
+ for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
948
+ }
949
+ });
950
+ for (const item of [
951
+ ...geometries,
952
+ ...materials,
953
+ ...textures
954
+ ]) item.dispose();
955
+ model.removeFromParent();
956
+ }
733
957
  var VehicleDetail = class {
734
958
  options;
735
959
  group = new THREE.Group();
736
960
  active = /* @__PURE__ */ new Map();
737
961
  idle = /* @__PURE__ */ new Map();
738
- loading = /* @__PURE__ */ new Set();
962
+ loading = /* @__PURE__ */ new Map();
963
+ resident = /* @__PURE__ */ new Map();
964
+ wanted = /* @__PURE__ */ new Set();
965
+ candidates = [];
966
+ clock = 0;
967
+ idleLimit = 4;
968
+ byteLimit = 32 * 1024 * 1024;
969
+ concurrentLimit = 2;
739
970
  missing = /* @__PURE__ */ new Set();
740
971
  max = 8;
741
972
  disposed = false;
@@ -747,6 +978,53 @@ var VehicleDetail = class {
747
978
  setMax(max) {
748
979
  this.max = Math.max(0, max);
749
980
  }
981
+ setCacheBudget(options) {
982
+ for (const value of Object.values(options)) if (value !== void 0 && !Number.isFinite(value)) throw new RangeError("Vehicle cache budgets must be finite");
983
+ if (options.modelBytes !== void 0 && options.modelBytes > this.byteLimit) this.missing.clear();
984
+ if (options.idleModels !== void 0) this.idleLimit = Math.max(0, Math.floor(options.idleModels));
985
+ if (options.modelBytes !== void 0) this.byteLimit = Math.max(0, options.modelBytes);
986
+ if (options.concurrentLoads !== void 0) this.concurrentLimit = Math.max(1, Math.floor(options.concurrentLoads));
987
+ this.prune();
988
+ for (const [id, entry] of [...this.active].reverse()) {
989
+ if (this.cacheStats().bytes <= this.byteLimit) break;
990
+ this.active.delete(id);
991
+ this.release(entry.visual);
992
+ }
993
+ }
994
+ cacheStats() {
995
+ return {
996
+ active: this.active.size,
997
+ idle: this.resident.size - this.active.size,
998
+ bytes: [...this.resident.values()].reduce((sum, entry) => sum + entry.bytes, 0),
999
+ pending: this.loading.size
1000
+ };
1001
+ }
1002
+ release(visual) {
1003
+ if (!this.resident.delete(visual)) return;
1004
+ const textures = /* @__PURE__ */ new Set();
1005
+ visual.object.traverse((node) => {
1006
+ const mesh = node;
1007
+ if (mesh.isMesh) {
1008
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
1009
+ }
1010
+ });
1011
+ for (const texture of textures) texture.dispose();
1012
+ visual.dispose();
1013
+ }
1014
+ prune(reserve = 0) {
1015
+ const idle = [...this.idle.entries()].flatMap(([type, pool]) => pool.map((visual) => ({
1016
+ type,
1017
+ visual
1018
+ }))).sort((a, b) => (this.resident.get(a.visual)?.used ?? 0) - (this.resident.get(b.visual)?.used ?? 0));
1019
+ while (idle.length && (idle.length > this.idleLimit || this.cacheStats().bytes + reserve > this.byteLimit)) {
1020
+ const oldest = idle.shift();
1021
+ if (!oldest) break;
1022
+ const pool = this.idle.get(oldest.type);
1023
+ pool?.splice(pool.indexOf(oldest.visual), 1);
1024
+ if (!pool?.length) this.idle.delete(oldest.type);
1025
+ this.release(oldest.visual);
1026
+ }
1027
+ }
750
1028
  has(id) {
751
1029
  return this.active.has(id);
752
1030
  }
@@ -759,12 +1037,17 @@ var VehicleDetail = class {
759
1037
  const far = this.options.far ?? 65;
760
1038
  const wanted = candidates.filter((c) => c.distance < (this.active.has(c.id) ? far : near) && !this.missing.has(c.type)).sort((a, b) => a.distance - b.distance || (a.id < b.id ? -1 : 1)).slice(0, this.max);
761
1039
  const keep = new Set(wanted.map((c) => c.id));
1040
+ this.candidates = wanted;
1041
+ this.wanted = new Set(wanted.map((c) => c.type));
1042
+ for (const [type, controller] of this.loading) if (!this.wanted.has(type)) controller.abort();
762
1043
  for (const [id, entry] of this.active) if (!keep.has(id) || !exists(id)) {
763
1044
  entry.visual.object.visible = false;
764
1045
  this.active.delete(id);
765
1046
  const pool = this.idle.get(entry.type) ?? [];
766
1047
  pool.push(entry.visual);
767
1048
  this.idle.set(entry.type, pool);
1049
+ const resident = this.resident.get(entry.visual);
1050
+ if (resident) resident.used = ++this.clock;
768
1051
  }
769
1052
  let started = 0;
770
1053
  for (const candidate of wanted) {
@@ -774,10 +1057,11 @@ var VehicleDetail = class {
774
1057
  this.activate(candidate, pooled);
775
1058
  continue;
776
1059
  }
777
- if (this.loading.has(candidate.type) || started >= 1) continue;
1060
+ if (this.loading.has(candidate.type) || this.loading.size >= this.concurrentLimit || started >= 1) continue;
778
1061
  started++;
779
1062
  this.load(candidate.type);
780
1063
  }
1064
+ this.prune();
781
1065
  }
782
1066
  activate(candidate, visual) {
783
1067
  const data = this.options.models.vehicle(candidate.type);
@@ -805,30 +1089,60 @@ var VehicleDetail = class {
805
1089
  this.missing.add(type);
806
1090
  return;
807
1091
  }
808
- this.loading.add(type);
1092
+ const controller = new AbortController();
1093
+ this.loading.set(type, controller);
1094
+ let bound;
809
1095
  try {
810
- const model = await this.options.models.load(type);
1096
+ const model = await this.options.models.load(type, controller.signal);
811
1097
  if (model === void 0) {
812
- this.missing.add(type);
1098
+ if (!controller.signal.aborted) this.missing.add(type);
813
1099
  return;
814
1100
  }
815
- if (this.disposed) return;
1101
+ if (this.disposed || controller.signal.aborted || !this.wanted.has(type)) {
1102
+ disposeUnbound(model);
1103
+ return;
1104
+ }
1105
+ const bytes = modelBytes(model);
1106
+ this.prune(bytes);
1107
+ if (this.cacheStats().bytes + bytes > this.byteLimit) {
1108
+ disposeUnbound(model);
1109
+ if (bytes > this.byteLimit) this.missing.add(type);
1110
+ return;
1111
+ }
1112
+ const originalMaterials = /* @__PURE__ */ new Set();
1113
+ model.traverse((node) => {
1114
+ const mesh = node;
1115
+ if (mesh.isMesh) for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) originalMaterials.add(material);
1116
+ });
816
1117
  const visual = createVehicleVisual(model, data);
1118
+ bound = visual;
1119
+ for (const material of originalMaterials) material.dispose();
1120
+ this.resident.set(visual, {
1121
+ bytes,
1122
+ used: ++this.clock
1123
+ });
817
1124
  visual.object.visible = false;
818
1125
  visual.object.traverse((object) => {
819
1126
  object.userData.walkIgnore = true;
1127
+ tintGlass(object);
820
1128
  });
821
1129
  this.group.add(visual.object);
822
1130
  await this.options.prepare?.(visual.object);
823
- if (this.disposed) {
824
- visual.dispose();
1131
+ if (this.disposed || controller.signal.aborted || !this.wanted.has(type) || this.cacheStats().bytes > this.byteLimit) {
1132
+ this.release(visual);
825
1133
  return;
826
1134
  }
827
- const pool = this.idle.get(type) ?? [];
828
- pool.push(visual);
829
- this.idle.set(type, pool);
1135
+ const candidate = this.candidates.find((c) => c.type === type && !this.active.has(c.id));
1136
+ if (candidate && this.active.size < this.max) this.activate(candidate, visual);
1137
+ else {
1138
+ const pool = this.idle.get(type) ?? [];
1139
+ pool.push(visual);
1140
+ this.idle.set(type, pool);
1141
+ }
1142
+ this.prune();
830
1143
  } catch {
831
- this.missing.add(type);
1144
+ if (bound) this.release(bound);
1145
+ if (!controller.signal.aborted) this.missing.add(type);
832
1146
  } finally {
833
1147
  this.loading.delete(type);
834
1148
  }
@@ -854,8 +1168,8 @@ var VehicleDetail = class {
854
1168
  }
855
1169
  dispose() {
856
1170
  this.disposed = true;
857
- for (const entry of this.active.values()) entry.visual.dispose();
858
- for (const pool of this.idle.values()) for (const visual of pool) visual.dispose();
1171
+ for (const controller of this.loading.values()) controller.abort();
1172
+ for (const visual of this.resident.keys()) this.release(visual);
859
1173
  this.active.clear();
860
1174
  this.idle.clear();
861
1175
  this.group.removeFromParent();
@@ -877,10 +1191,7 @@ function createAmbientRenderer(options) {
877
1191
  const object = new THREE.Group();
878
1192
  object.name = "ambient";
879
1193
  options.parent.add(object);
880
- const prepareSync = (target) => {
881
- options.prepare?.(target);
882
- };
883
- const batches = new ProxyBatches(prepareSync);
1194
+ const batches = new ProxyBatches(options.prepare);
884
1195
  object.add(batches.group);
885
1196
  const detail = options.vehicles !== void 0 ? new VehicleDetail({
886
1197
  models: options.vehicles,
@@ -894,9 +1205,9 @@ function createAmbientRenderer(options) {
894
1205
  far: 1500,
895
1206
  ...options.prepare !== void 0 ? { prepare: options.prepare } : {}
896
1207
  }) : void 0;
897
- const figures = new AmbientFigures(prepareSync);
1208
+ const figures = new AmbientFigures(options.prepare);
898
1209
  object.add(figures.group);
899
- const aircraft = options.aircraft !== void 0 ? new AmbientAircraft(options.aircraft, object, options.prepare) : void 0;
1210
+ const aircraft = options.aircraft !== void 0 ? new AmbientAircraft(options.aircraft, object, options.prepare, options.admission) : void 0;
900
1211
  let budget = {
901
1212
  ...DEFAULT_BUDGET,
902
1213
  ...options.budget
@@ -904,6 +1215,11 @@ function createAmbientRenderer(options) {
904
1215
  const applyBudget = () => {
905
1216
  detail?.setMax(budget.detailedCars);
906
1217
  boatDetail?.setMax(budget.detailedBoats ?? 4);
1218
+ for (const pool of [detail, boatDetail]) pool?.setCacheBudget({
1219
+ ...budget.idleVehicleModels !== void 0 ? { idleModels: budget.idleVehicleModels } : {},
1220
+ ...budget.vehicleModelBytes !== void 0 ? { modelBytes: budget.vehicleModelBytes } : {},
1221
+ ...budget.vehicleLoads !== void 0 ? { concurrentLoads: budget.vehicleLoads } : {}
1222
+ });
907
1223
  figures.setBudget({
908
1224
  skinned: budget.skinnedFigures,
909
1225
  radius: budget.figureRadius,
@@ -1104,8 +1420,41 @@ function wildlifeGeometry(species, tier) {
1104
1420
  source.dispose();
1105
1421
  if (source !== geometry) geometry.dispose();
1106
1422
  };
1107
- const ellipsoid = (center, size, color = body.color, bone = 0) => {
1108
- 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);
1423
+ const ellipsoid = (center, size, color = body.color, bone = 0, pitch = 0) => {
1424
+ 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);
1425
+ };
1426
+ const stripe = (z) => Math.sin(z / l * Math.PI * 2 * 8.5) > .15 ? body.accent : body.color;
1427
+ const barrel = (center, size) => {
1428
+ const rings = tier === 0 ? 14 : 8, sides = tier === 0 ? 8 : 6;
1429
+ const ring = (i) => {
1430
+ const u = -1 + 2 * i / rings, r = Math.sqrt(Math.max(0, 1 - u * u));
1431
+ return Array.from({ length: sides }, (_, k) => {
1432
+ const a = k / sides * Math.PI * 2;
1433
+ return [
1434
+ center[0] + Math.cos(a) * size[0] * r,
1435
+ center[1] + Math.sin(a) * size[1] * r,
1436
+ center[2] + u * size[2]
1437
+ ];
1438
+ });
1439
+ };
1440
+ for (let i = 0; i < rings; i++) {
1441
+ const [a, b] = [ring(i), ring(i + 1)];
1442
+ const positions = [];
1443
+ for (let k = 0; k < sides; k++) {
1444
+ const n = (k + 1) % sides;
1445
+ const [p, q, r, s] = [
1446
+ a[k],
1447
+ b[k],
1448
+ b[n],
1449
+ a[n]
1450
+ ];
1451
+ if (i < rings - 1) positions.push(...p, ...r, ...q);
1452
+ if (i > 0) positions.push(...p, ...s, ...r);
1453
+ }
1454
+ const geometry = new THREE.BufferGeometry();
1455
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
1456
+ add(geometry, new THREE.Matrix4(), stripe(center[2] + (-1 + (2 * i + 1) / rings) * size[2]));
1457
+ }
1109
1458
  };
1110
1459
  const segment = (a, b, r1, r2, color = body.color, bone = 0) => {
1111
1460
  const start = new THREE.Vector3(...a), end = new THREE.Vector3(...b), delta = end.clone().sub(start);
@@ -1228,7 +1577,7 @@ function wildlifeGeometry(species, tier) {
1228
1577
  ]);
1229
1578
  if (flying) {
1230
1579
  const span = l * (detail("raptor") ? 1.65 : insect ? .9 : 1.05);
1231
- wedge([
1580
+ if (insect) wedge([
1232
1581
  [
1233
1582
  side * w * .25,
1234
1583
  y,
@@ -1245,6 +1594,49 @@ function wildlifeGeometry(species, tier) {
1245
1594
  -l * .42
1246
1595
  ]
1247
1596
  ], h * .045, body.accent, wing);
1597
+ else {
1598
+ const lift = h * (detail("raptor") ? .06 : .14);
1599
+ const rootFront = [
1600
+ side * w * .25,
1601
+ y,
1602
+ l * .22
1603
+ ];
1604
+ const rootBack = [
1605
+ side * w * .35,
1606
+ y,
1607
+ -l * .3
1608
+ ];
1609
+ const wrist = [
1610
+ side * span * .48,
1611
+ y + lift,
1612
+ l * .06
1613
+ ];
1614
+ const wristBack = [
1615
+ side * span * .5,
1616
+ y + lift,
1617
+ -l * .24
1618
+ ];
1619
+ const tip = [
1620
+ side * span,
1621
+ y + h * .02,
1622
+ -l * .16
1623
+ ];
1624
+ wedge([
1625
+ rootFront,
1626
+ wrist,
1627
+ rootBack
1628
+ ], h * .045, body.accent, wing);
1629
+ wedge([
1630
+ wrist,
1631
+ wristBack,
1632
+ rootBack
1633
+ ], h * .045, body.accent, wing);
1634
+ wedge([
1635
+ wrist,
1636
+ tip,
1637
+ wristBack
1638
+ ], h * .04, body.accent, wing);
1639
+ }
1248
1640
  if (tier === 0 && !insect) for (let i = 0; i < 3; i++) wedge([
1249
1641
  [
1250
1642
  side * span * (.65 + i * .1),
@@ -1431,10 +1823,11 @@ function wildlifeGeometry(species, tier) {
1431
1823
  -l * 1.3
1432
1824
  ], w * .1, w * .005, body.color, tail);
1433
1825
  } else {
1434
- const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant";
1826
+ const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant", ungulate = body.family === "ungulate";
1435
1827
  const torsoY = h * (hopper ? .57 : low ? .5 : .72);
1436
1828
  const torsoRadius = h * (hopper ? .3 : low ? .31 : elephant ? .32 : .19);
1437
- ellipsoid([
1829
+ const mass = detail("stripes") && tier < 2 ? barrel : ellipsoid;
1830
+ mass([
1438
1831
  0,
1439
1832
  torsoY,
1440
1833
  -l * .04
@@ -1443,6 +1836,26 @@ function wildlifeGeometry(species, tier) {
1443
1836
  torsoRadius,
1444
1837
  l * (hopper ? .28 : .38)
1445
1838
  ]);
1839
+ if (!hopper && tier < 2) {
1840
+ mass([
1841
+ 0,
1842
+ torsoY - torsoRadius * .08,
1843
+ l * .2
1844
+ ], [
1845
+ w * .47,
1846
+ torsoRadius * 1.1,
1847
+ l * .17
1848
+ ]);
1849
+ mass([
1850
+ 0,
1851
+ torsoY + torsoRadius * .06,
1852
+ -l * .27
1853
+ ], [
1854
+ w * .48,
1855
+ torsoRadius * .98,
1856
+ l * .15
1857
+ ]);
1858
+ }
1446
1859
  if (tier === 0) ellipsoid([
1447
1860
  0,
1448
1861
  torsoY - torsoRadius * .48,
@@ -1454,6 +1867,15 @@ function wildlifeGeometry(species, tier) {
1454
1867
  ], elephant ? body.color : body.accent);
1455
1868
  const neckTop = h * (hopper ? .92 : low ? .61 : elephant ? .9 : detail("long-neck") ? 1.38 : 1.1);
1456
1869
  const headZ = l * (hopper ? .3 : .47), headR = h * (elephant ? .18 : low ? .23 : .13);
1870
+ if (detail("palmate") && tier < 2) ellipsoid([
1871
+ 0,
1872
+ torsoY + torsoRadius * .75,
1873
+ l * .19
1874
+ ], [
1875
+ w * .3,
1876
+ torsoRadius * .55,
1877
+ l * .15
1878
+ ], body.color);
1457
1879
  segment([
1458
1880
  0,
1459
1881
  torsoY,
@@ -1463,6 +1885,19 @@ function wildlifeGeometry(species, tier) {
1463
1885
  neckTop - headR * .4,
1464
1886
  headZ - l * .07
1465
1887
  ], w * (elephant ? .4 : low ? .32 : .23), w * (low ? .28 : .17));
1888
+ if (detail("mane") && tier < 2) {
1889
+ const dy = neckTop - headR * .4 - torsoY, dz = headZ - l * .34;
1890
+ const pitch = Math.atan2(-dy, dz), lift = w * .19;
1891
+ ellipsoid([
1892
+ 0,
1893
+ torsoY + dy / 2 + Math.cos(pitch) * lift,
1894
+ l * .27 + dz / 2 + Math.sin(pitch) * lift
1895
+ ], [
1896
+ w * .035,
1897
+ h * .06,
1898
+ Math.hypot(dy, dz) * .55
1899
+ ], body.accent, 0, pitch);
1900
+ }
1466
1901
  const head = joint("head", [
1467
1902
  0,
1468
1903
  neckTop - headR * .6,
@@ -1477,15 +1912,36 @@ function wildlifeGeometry(species, tier) {
1477
1912
  headR,
1478
1913
  l * (elephant || low ? .16 : .12)
1479
1914
  ], body.color, head);
1480
- if (!elephant) ellipsoid([
1481
- 0,
1482
- neckTop - headR * .28,
1483
- headZ + l * .11
1484
- ], [
1485
- w * .17,
1486
- headR * .55,
1487
- l * .1
1488
- ], body.accent, head);
1915
+ if (!elephant) {
1916
+ const long = detail("palmate") ? 1.3 : 1;
1917
+ ellipsoid([
1918
+ 0,
1919
+ neckTop - headR * (ungulate ? .38 : .28),
1920
+ headZ + l * .1 * long
1921
+ ], [
1922
+ w * (detail("palmate") ? .2 : .16),
1923
+ headR * .52,
1924
+ l * .075 * long
1925
+ ], ungulate ? body.color : body.accent, head);
1926
+ if (ungulate && tier === 0) ellipsoid([
1927
+ 0,
1928
+ neckTop - headR * .36,
1929
+ headZ + l * .17 * long
1930
+ ], [
1931
+ w * .12,
1932
+ headR * .3,
1933
+ l * .012
1934
+ ], "#2e2a26", head);
1935
+ if (detail("palmate") && tier < 2) ellipsoid([
1936
+ 0,
1937
+ neckTop - headR * 1.2,
1938
+ headZ - l * .02
1939
+ ], [
1940
+ w * .05,
1941
+ headR * .6,
1942
+ l * .03
1943
+ ], body.color, head);
1944
+ }
1489
1945
  for (const side of [-1, 1]) {
1490
1946
  const earHeight = body.family === "lagomorph" ? h * .45 : hopper ? h * .2 : elephant ? h * .25 : low ? h * .075 : h * .11;
1491
1947
  ellipsoid([
@@ -1496,31 +1952,93 @@ function wildlifeGeometry(species, tier) {
1496
1952
  w * (elephant ? .35 : .1),
1497
1953
  earHeight,
1498
1954
  l * .035
1499
- ], elephant ? body.color : body.accent, head);
1955
+ ], elephant || detail("stripes") ? body.color : body.accent, head);
1500
1956
  eye(side * w * (low ? .3 : .25), neckTop + headR * .1, headZ + l * .09, headR * .1, head);
1501
- if (detail("horns") || detail("antlers")) {
1502
- const base = [
1503
- side * w * .22,
1504
- neckTop + headR * .8,
1505
- headZ
1957
+ const horn = "#817963";
1958
+ const base = [
1959
+ side * w * .2,
1960
+ neckTop + headR * .8,
1961
+ headZ - l * .02
1962
+ ];
1963
+ const along = (a, b, t) => [
1964
+ a[0] + (b[0] - a[0]) * t,
1965
+ a[1] + (b[1] - a[1]) * t,
1966
+ a[2] + (b[2] - a[2]) * t
1967
+ ];
1968
+ if (detail("palmate")) {
1969
+ const root = [
1970
+ side * w * .45,
1971
+ neckTop + h * .08,
1972
+ headZ - l * .05
1973
+ ];
1974
+ if (tier < 2) segment(base, root, w * .05, w * .04, horn, head);
1975
+ const front = [
1976
+ side * w * 1.05,
1977
+ neckTop + h * .2,
1978
+ headZ + l * .02
1979
+ ];
1980
+ const back = [
1981
+ side * w * 1,
1982
+ neckTop + h * .16,
1983
+ headZ - l * .16
1984
+ ];
1985
+ wedge([
1986
+ root,
1987
+ front,
1988
+ back
1989
+ ], h * .025, horn, head);
1990
+ if (tier < 2) for (let i = 0; i < (tier === 0 ? 5 : 2); i++) {
1991
+ const at = along(front, back, (i + .5) / (tier === 0 ? 5 : 2));
1992
+ segment(at, [
1993
+ at[0] + side * w * .12,
1994
+ at[1] + h * .07,
1995
+ at[2]
1996
+ ], w * .025, w * .008, horn, head);
1997
+ }
1998
+ } else if (detail("antlers")) {
1999
+ const bend = [
2000
+ side * w * .5,
2001
+ neckTop + h * .2,
2002
+ headZ - l * .1
1506
2003
  ];
1507
2004
  const tip = [
1508
2005
  side * w * .42,
1509
- neckTop + h * .36,
1510
- headZ - l * .04
2006
+ neckTop + h * .38,
2007
+ headZ - l * .03
1511
2008
  ];
1512
- segment(base, tip, w * .045, w * .013, "#817963", head);
1513
- if (detail("antlers") && tier < 2) for (let branch = 0; branch < (tier === 0 ? 3 : 1); branch++) {
1514
- const t = .3 + branch * .2;
1515
- segment([
1516
- base[0] + (tip[0] - base[0]) * t,
1517
- base[1] + (tip[1] - base[1]) * t,
1518
- base[2] + (tip[2] - base[2]) * t
1519
- ], [
1520
- side * w * (.6 + branch * .1),
1521
- neckTop + h * (.18 + branch * .09),
1522
- headZ + l * .04
1523
- ], w * .027, w * .008, "#817963", head);
2009
+ if (tier === 2) segment(base, tip, w * .045, w * .013, horn, head);
2010
+ else {
2011
+ segment(base, bend, w * .045, w * .032, horn, head);
2012
+ segment(bend, tip, w * .032, w * .012, horn, head);
2013
+ }
2014
+ if (tier < 2) for (let branch = 0; branch < (tier === 0 ? 3 : 1); branch++) {
2015
+ const from = branch === 0 ? along(base, bend, .45) : along(bend, tip, branch === 1 ? .2 : .6);
2016
+ segment(from, [
2017
+ from[0] + side * w * .04,
2018
+ from[1] + h * .11,
2019
+ from[2] + l * .06
2020
+ ], w * .024, w * .008, horn, head);
2021
+ }
2022
+ } else if (detail("straight-horns")) segment(base, [
2023
+ side * w * .16,
2024
+ neckTop + h * .5,
2025
+ headZ - l * .28
2026
+ ], w * .04, w * .01, horn, head);
2027
+ else if (detail("horns")) {
2028
+ const mid = [
2029
+ side * w * .36,
2030
+ neckTop + h * .2,
2031
+ headZ - l * .08
2032
+ ];
2033
+ const tip = [
2034
+ side * w * .26,
2035
+ neckTop + h * .38,
2036
+ headZ - l * .02
2037
+ ];
2038
+ if (tier === 2) segment(base, tip, w * .042, w * .01, horn, head);
2039
+ else {
2040
+ segment(base, mid, w * .042, w * .028, horn, head);
2041
+ segment(mid, tip, w * .028, w * .01, horn, head);
1524
2042
  }
1525
2043
  }
1526
2044
  for (const front of [false, true]) {
@@ -1532,27 +2050,83 @@ function wildlifeGeometry(species, tier) {
1532
2050
  ];
1533
2051
  const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1534
2052
  const footY = hopper && front ? h * .42 : h * .055;
1535
- const knee = [
1536
- pivot[0],
1537
- (pivot[1] + footY) * .5,
1538
- legZ + (hopper && !front ? l * .14 : -l * .02)
1539
- ];
1540
2053
  const radius = w * (elephant ? .2 : hopper && !front ? .22 : low ? .13 : .1);
1541
- segment(pivot, knee, radius, radius * .7, body.color, leg);
1542
- segment(knee, [
1543
- pivot[0],
1544
- footY,
1545
- legZ
1546
- ], radius * .7, radius * .46, body.color, leg);
1547
- if (tier < 2) ellipsoid([
1548
- pivot[0],
1549
- footY,
1550
- legZ + (hopper && !front ? l * .12 : l * .035)
1551
- ], [
1552
- radius * .65,
1553
- Math.max(h * .025, footY * .65),
1554
- l * (hopper && !front ? .16 : .055)
1555
- ], elephant ? body.color : body.accent, leg);
2054
+ if (hopper || elephant || tier === 2) {
2055
+ const knee = [
2056
+ pivot[0],
2057
+ (pivot[1] + footY) * .5,
2058
+ legZ + (hopper && !front ? l * .14 : -l * .02)
2059
+ ];
2060
+ segment(pivot, knee, radius, radius * .7, body.color, leg);
2061
+ segment(knee, [
2062
+ pivot[0],
2063
+ footY,
2064
+ legZ
2065
+ ], radius * .7, radius * .46, body.color, leg);
2066
+ } else {
2067
+ const x = pivot[0];
2068
+ const bends = front ? [
2069
+ [
2070
+ x,
2071
+ torsoY - torsoRadius * .9,
2072
+ legZ - l * .035
2073
+ ],
2074
+ [
2075
+ x,
2076
+ h * (low ? .17 : .3),
2077
+ legZ - l * .005
2078
+ ],
2079
+ [
2080
+ x,
2081
+ footY + h * .035,
2082
+ legZ + l * .008
2083
+ ]
2084
+ ] : [
2085
+ [
2086
+ x,
2087
+ pivot[1] - (pivot[1] - footY) * .38,
2088
+ legZ + l * .07
2089
+ ],
2090
+ [
2091
+ x,
2092
+ h * (low ? .16 : .31),
2093
+ legZ - l * .075
2094
+ ],
2095
+ [
2096
+ x,
2097
+ footY + h * .035,
2098
+ legZ - l * .045
2099
+ ]
2100
+ ];
2101
+ const radii = front ? [
2102
+ 1.45,
2103
+ .95,
2104
+ .6,
2105
+ .5
2106
+ ] : [
2107
+ 1.6,
2108
+ 1.05,
2109
+ .6,
2110
+ .5
2111
+ ];
2112
+ let from = pivot;
2113
+ bends.forEach((to, i) => {
2114
+ segment(from, to, radius * radii[i], radius * radii[i + 1], body.color, leg);
2115
+ from = to;
2116
+ });
2117
+ }
2118
+ if (tier < 2) {
2119
+ const longFoot = (hopper || body.family === "lagomorph") && !front;
2120
+ ellipsoid([
2121
+ pivot[0],
2122
+ footY,
2123
+ legZ + (longFoot ? l * .08 : front ? l * .02 : -l * .03)
2124
+ ], [
2125
+ radius * .65,
2126
+ Math.max(h * .025, hopper && front ? footY * .65 : h * .03),
2127
+ l * (longFoot ? .14 : .055)
2128
+ ], elephant ? body.color : ungulate ? "#3a3530" : body.accent, leg);
2129
+ }
1556
2130
  }
1557
2131
  }
1558
2132
  const tail = joint("tail", [
@@ -1567,17 +2141,35 @@ function wildlifeGeometry(species, tier) {
1567
2141
  -l * .34
1568
2142
  ], [
1569
2143
  0,
1570
- h * 1.12,
1571
- -l * .65
1572
- ], w * .19, w * .16, body.color, tail);
2144
+ h * .62,
2145
+ -l * .6
2146
+ ], w * .19, w * .22, body.color, tail);
1573
2147
  ellipsoid([
1574
2148
  0,
1575
- h * 1,
1576
- -l * .6
2149
+ h * .72,
2150
+ -l * .66
2151
+ ], [
2152
+ w * .34,
2153
+ h * .3,
2154
+ l * .2
2155
+ ], body.color, tail);
2156
+ ellipsoid([
2157
+ 0,
2158
+ h * 1.08,
2159
+ -l * .62
1577
2160
  ], [
1578
2161
  w * .36,
1579
- h * .57,
1580
- l * .27
2162
+ h * .28,
2163
+ l * .2
2164
+ ], body.color, tail);
2165
+ if (tier < 2) ellipsoid([
2166
+ 0,
2167
+ h * 1.28,
2168
+ -l * .45
2169
+ ], [
2170
+ w * .3,
2171
+ h * .17,
2172
+ l * .18
1581
2173
  ], body.color, tail);
1582
2174
  } else if (detail("long-tail") || detail("bushy-tail") || [
1583
2175
  "canid",
@@ -1637,18 +2229,6 @@ function wildlifeGeometry(species, tier) {
1637
2229
  headZ + l * .22
1638
2230
  ], w * .09, w * .04, body.color, trunk);
1639
2231
  }
1640
- if (detail("stripes") && tier < 2) for (let i = 0; i < 6; i++) {
1641
- const z = -l * .25 + i * l * .09;
1642
- for (const side of [-1, 1]) segment([
1643
- side * w * .45,
1644
- torsoY - torsoRadius * .4,
1645
- z
1646
- ], [
1647
- side * w * .4,
1648
- torsoY + torsoRadius * .55,
1649
- z + l * .02
1650
- ], w * .025, w * .018, body.accent);
1651
- }
1652
2232
  }
1653
2233
  const geometry = new THREE.BufferGeometry();
1654
2234
  geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));