@bendyline/molen-ambient 0.0.12 → 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-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.
547
580
  */
548
- function mergeStaticAircraftParts(model, spec) {
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.
630
+ */
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
@@ -1022,10 +1191,7 @@ function createAmbientRenderer(options) {
1022
1191
  const object = new THREE.Group();
1023
1192
  object.name = "ambient";
1024
1193
  options.parent.add(object);
1025
- const prepareSync = (target) => {
1026
- options.prepare?.(target);
1027
- };
1028
- const batches = new ProxyBatches(prepareSync);
1194
+ const batches = new ProxyBatches(options.prepare);
1029
1195
  object.add(batches.group);
1030
1196
  const detail = options.vehicles !== void 0 ? new VehicleDetail({
1031
1197
  models: options.vehicles,
@@ -1039,9 +1205,9 @@ function createAmbientRenderer(options) {
1039
1205
  far: 1500,
1040
1206
  ...options.prepare !== void 0 ? { prepare: options.prepare } : {}
1041
1207
  }) : void 0;
1042
- const figures = new AmbientFigures(prepareSync);
1208
+ const figures = new AmbientFigures(options.prepare);
1043
1209
  object.add(figures.group);
1044
- 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;
1045
1211
  let budget = {
1046
1212
  ...DEFAULT_BUDGET,
1047
1213
  ...options.budget
@@ -1254,8 +1420,41 @@ function wildlifeGeometry(species, tier) {
1254
1420
  source.dispose();
1255
1421
  if (source !== geometry) geometry.dispose();
1256
1422
  };
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);
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
+ }
1259
1458
  };
1260
1459
  const segment = (a, b, r1, r2, color = body.color, bone = 0) => {
1261
1460
  const start = new THREE.Vector3(...a), end = new THREE.Vector3(...b), delta = end.clone().sub(start);
@@ -1378,7 +1577,7 @@ function wildlifeGeometry(species, tier) {
1378
1577
  ]);
1379
1578
  if (flying) {
1380
1579
  const span = l * (detail("raptor") ? 1.65 : insect ? .9 : 1.05);
1381
- wedge([
1580
+ if (insect) wedge([
1382
1581
  [
1383
1582
  side * w * .25,
1384
1583
  y,
@@ -1395,6 +1594,49 @@ function wildlifeGeometry(species, tier) {
1395
1594
  -l * .42
1396
1595
  ]
1397
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
+ }
1398
1640
  if (tier === 0 && !insect) for (let i = 0; i < 3; i++) wedge([
1399
1641
  [
1400
1642
  side * span * (.65 + i * .1),
@@ -1581,10 +1823,11 @@ function wildlifeGeometry(species, tier) {
1581
1823
  -l * 1.3
1582
1824
  ], w * .1, w * .005, body.color, tail);
1583
1825
  } else {
1584
- 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";
1585
1827
  const torsoY = h * (hopper ? .57 : low ? .5 : .72);
1586
1828
  const torsoRadius = h * (hopper ? .3 : low ? .31 : elephant ? .32 : .19);
1587
- ellipsoid([
1829
+ const mass = detail("stripes") && tier < 2 ? barrel : ellipsoid;
1830
+ mass([
1588
1831
  0,
1589
1832
  torsoY,
1590
1833
  -l * .04
@@ -1593,6 +1836,26 @@ function wildlifeGeometry(species, tier) {
1593
1836
  torsoRadius,
1594
1837
  l * (hopper ? .28 : .38)
1595
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
+ }
1596
1859
  if (tier === 0) ellipsoid([
1597
1860
  0,
1598
1861
  torsoY - torsoRadius * .48,
@@ -1604,6 +1867,15 @@ function wildlifeGeometry(species, tier) {
1604
1867
  ], elephant ? body.color : body.accent);
1605
1868
  const neckTop = h * (hopper ? .92 : low ? .61 : elephant ? .9 : detail("long-neck") ? 1.38 : 1.1);
1606
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);
1607
1879
  segment([
1608
1880
  0,
1609
1881
  torsoY,
@@ -1613,6 +1885,19 @@ function wildlifeGeometry(species, tier) {
1613
1885
  neckTop - headR * .4,
1614
1886
  headZ - l * .07
1615
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
+ }
1616
1901
  const head = joint("head", [
1617
1902
  0,
1618
1903
  neckTop - headR * .6,
@@ -1627,15 +1912,36 @@ function wildlifeGeometry(species, tier) {
1627
1912
  headR,
1628
1913
  l * (elephant || low ? .16 : .12)
1629
1914
  ], 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);
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
+ }
1639
1945
  for (const side of [-1, 1]) {
1640
1946
  const earHeight = body.family === "lagomorph" ? h * .45 : hopper ? h * .2 : elephant ? h * .25 : low ? h * .075 : h * .11;
1641
1947
  ellipsoid([
@@ -1646,31 +1952,93 @@ function wildlifeGeometry(species, tier) {
1646
1952
  w * (elephant ? .35 : .1),
1647
1953
  earHeight,
1648
1954
  l * .035
1649
- ], elephant ? body.color : body.accent, head);
1955
+ ], elephant || detail("stripes") ? body.color : body.accent, head);
1650
1956
  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
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
1656
2003
  ];
1657
2004
  const tip = [
1658
2005
  side * w * .42,
1659
- neckTop + h * .36,
1660
- headZ - l * .04
2006
+ neckTop + h * .38,
2007
+ headZ - l * .03
1661
2008
  ];
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);
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);
1674
2042
  }
1675
2043
  }
1676
2044
  for (const front of [false, true]) {
@@ -1682,27 +2050,83 @@ function wildlifeGeometry(species, tier) {
1682
2050
  ];
1683
2051
  const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1684
2052
  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
2053
  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);
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
+ }
1706
2130
  }
1707
2131
  }
1708
2132
  const tail = joint("tail", [
@@ -1717,17 +2141,35 @@ function wildlifeGeometry(species, tier) {
1717
2141
  -l * .34
1718
2142
  ], [
1719
2143
  0,
1720
- h * 1.12,
1721
- -l * .65
1722
- ], w * .19, w * .16, body.color, tail);
2144
+ h * .62,
2145
+ -l * .6
2146
+ ], w * .19, w * .22, body.color, tail);
1723
2147
  ellipsoid([
1724
2148
  0,
1725
- h * 1,
1726
- -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
1727
2160
  ], [
1728
2161
  w * .36,
1729
- h * .57,
1730
- 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
1731
2173
  ], body.color, tail);
1732
2174
  } else if (detail("long-tail") || detail("bushy-tail") || [
1733
2175
  "canid",
@@ -1787,18 +2229,6 @@ function wildlifeGeometry(species, tier) {
1787
2229
  headZ + l * .22
1788
2230
  ], w * .09, w * .04, body.color, trunk);
1789
2231
  }
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
2232
  }
1803
2233
  const geometry = new THREE.BufferGeometry();
1804
2234
  geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));