@bendyline/molen-ambient 0.0.10 → 0.0.12

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-vzvNQczG.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";
@@ -730,12 +730,74 @@ var AmbientAircraft = class {
730
730
  * content model (wheels turning and steering), from a pool per type so a spawn never builds new
731
731
  * GPU objects once the pool is warm. Farther cars stay instanced proxies.
732
732
  */
733
+ /**
734
+ * Authored glazing is often nearly clear (about 16% opaque), so from the street a passing car
735
+ * reads as an open frame of pillars and seats. Traffic glass is tinted and glossy instead:
736
+ * darker, at least 60% opaque and smooth enough to pick up the sky. Only traffic: a car the
737
+ * player sits in keeps its authored glass. The visual's materials are its own clones.
738
+ */
739
+ function tintGlass(object) {
740
+ const mesh = object;
741
+ if (!mesh.isMesh) return;
742
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
743
+ if (!material.transparent || !/glass|glazing/i.test(material.name)) continue;
744
+ if (!(material instanceof THREE.MeshStandardMaterial)) continue;
745
+ material.color.multiplyScalar(.3);
746
+ material.opacity = Math.max(material.opacity, .62);
747
+ material.roughness = Math.min(material.roughness, .08);
748
+ }
749
+ }
750
+ /** Buffer residency estimate; textures add their decoded base level plus mip overhead. */
751
+ function modelBytes(object) {
752
+ const buffers = /* @__PURE__ */ new Set();
753
+ const textures = /* @__PURE__ */ new Set();
754
+ object.traverse((node) => {
755
+ const mesh = node;
756
+ if (!mesh.isMesh) return;
757
+ for (const attr of Object.values(mesh.geometry.attributes)) buffers.add(attr.array.buffer);
758
+ if (mesh.geometry.index) buffers.add(mesh.geometry.index.array.buffer);
759
+ 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);
760
+ });
761
+ let bytes = [...buffers].reduce((sum, buffer) => sum + buffer.byteLength, 0);
762
+ for (const texture of textures) {
763
+ const image = texture.image;
764
+ bytes += (image?.width ?? 1) * (image?.height ?? 1) * 4 * 4 / 3;
765
+ }
766
+ return bytes;
767
+ }
768
+ function disposeUnbound(model) {
769
+ const geometries = /* @__PURE__ */ new Set();
770
+ const materials = /* @__PURE__ */ new Set();
771
+ const textures = /* @__PURE__ */ new Set();
772
+ model.traverse((node) => {
773
+ const mesh = node;
774
+ if (!mesh.isMesh) return;
775
+ geometries.add(mesh.geometry);
776
+ for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
777
+ materials.add(material);
778
+ for (const value of Object.values(material)) if (value instanceof THREE.Texture) textures.add(value);
779
+ }
780
+ });
781
+ for (const item of [
782
+ ...geometries,
783
+ ...materials,
784
+ ...textures
785
+ ]) item.dispose();
786
+ model.removeFromParent();
787
+ }
733
788
  var VehicleDetail = class {
734
789
  options;
735
790
  group = new THREE.Group();
736
791
  active = /* @__PURE__ */ new Map();
737
792
  idle = /* @__PURE__ */ new Map();
738
- loading = /* @__PURE__ */ new Set();
793
+ loading = /* @__PURE__ */ new Map();
794
+ resident = /* @__PURE__ */ new Map();
795
+ wanted = /* @__PURE__ */ new Set();
796
+ candidates = [];
797
+ clock = 0;
798
+ idleLimit = 4;
799
+ byteLimit = 32 * 1024 * 1024;
800
+ concurrentLimit = 2;
739
801
  missing = /* @__PURE__ */ new Set();
740
802
  max = 8;
741
803
  disposed = false;
@@ -747,6 +809,53 @@ var VehicleDetail = class {
747
809
  setMax(max) {
748
810
  this.max = Math.max(0, max);
749
811
  }
812
+ setCacheBudget(options) {
813
+ for (const value of Object.values(options)) if (value !== void 0 && !Number.isFinite(value)) throw new RangeError("Vehicle cache budgets must be finite");
814
+ if (options.modelBytes !== void 0 && options.modelBytes > this.byteLimit) this.missing.clear();
815
+ if (options.idleModels !== void 0) this.idleLimit = Math.max(0, Math.floor(options.idleModels));
816
+ if (options.modelBytes !== void 0) this.byteLimit = Math.max(0, options.modelBytes);
817
+ if (options.concurrentLoads !== void 0) this.concurrentLimit = Math.max(1, Math.floor(options.concurrentLoads));
818
+ this.prune();
819
+ for (const [id, entry] of [...this.active].reverse()) {
820
+ if (this.cacheStats().bytes <= this.byteLimit) break;
821
+ this.active.delete(id);
822
+ this.release(entry.visual);
823
+ }
824
+ }
825
+ cacheStats() {
826
+ return {
827
+ active: this.active.size,
828
+ idle: this.resident.size - this.active.size,
829
+ bytes: [...this.resident.values()].reduce((sum, entry) => sum + entry.bytes, 0),
830
+ pending: this.loading.size
831
+ };
832
+ }
833
+ release(visual) {
834
+ if (!this.resident.delete(visual)) return;
835
+ const textures = /* @__PURE__ */ new Set();
836
+ visual.object.traverse((node) => {
837
+ const mesh = node;
838
+ if (mesh.isMesh) {
839
+ 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);
840
+ }
841
+ });
842
+ for (const texture of textures) texture.dispose();
843
+ visual.dispose();
844
+ }
845
+ prune(reserve = 0) {
846
+ const idle = [...this.idle.entries()].flatMap(([type, pool]) => pool.map((visual) => ({
847
+ type,
848
+ visual
849
+ }))).sort((a, b) => (this.resident.get(a.visual)?.used ?? 0) - (this.resident.get(b.visual)?.used ?? 0));
850
+ while (idle.length && (idle.length > this.idleLimit || this.cacheStats().bytes + reserve > this.byteLimit)) {
851
+ const oldest = idle.shift();
852
+ if (!oldest) break;
853
+ const pool = this.idle.get(oldest.type);
854
+ pool?.splice(pool.indexOf(oldest.visual), 1);
855
+ if (!pool?.length) this.idle.delete(oldest.type);
856
+ this.release(oldest.visual);
857
+ }
858
+ }
750
859
  has(id) {
751
860
  return this.active.has(id);
752
861
  }
@@ -759,12 +868,17 @@ var VehicleDetail = class {
759
868
  const far = this.options.far ?? 65;
760
869
  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
870
  const keep = new Set(wanted.map((c) => c.id));
871
+ this.candidates = wanted;
872
+ this.wanted = new Set(wanted.map((c) => c.type));
873
+ for (const [type, controller] of this.loading) if (!this.wanted.has(type)) controller.abort();
762
874
  for (const [id, entry] of this.active) if (!keep.has(id) || !exists(id)) {
763
875
  entry.visual.object.visible = false;
764
876
  this.active.delete(id);
765
877
  const pool = this.idle.get(entry.type) ?? [];
766
878
  pool.push(entry.visual);
767
879
  this.idle.set(entry.type, pool);
880
+ const resident = this.resident.get(entry.visual);
881
+ if (resident) resident.used = ++this.clock;
768
882
  }
769
883
  let started = 0;
770
884
  for (const candidate of wanted) {
@@ -774,10 +888,11 @@ var VehicleDetail = class {
774
888
  this.activate(candidate, pooled);
775
889
  continue;
776
890
  }
777
- if (this.loading.has(candidate.type) || started >= 1) continue;
891
+ if (this.loading.has(candidate.type) || this.loading.size >= this.concurrentLimit || started >= 1) continue;
778
892
  started++;
779
893
  this.load(candidate.type);
780
894
  }
895
+ this.prune();
781
896
  }
782
897
  activate(candidate, visual) {
783
898
  const data = this.options.models.vehicle(candidate.type);
@@ -805,30 +920,60 @@ var VehicleDetail = class {
805
920
  this.missing.add(type);
806
921
  return;
807
922
  }
808
- this.loading.add(type);
923
+ const controller = new AbortController();
924
+ this.loading.set(type, controller);
925
+ let bound;
809
926
  try {
810
- const model = await this.options.models.load(type);
927
+ const model = await this.options.models.load(type, controller.signal);
811
928
  if (model === void 0) {
812
- this.missing.add(type);
929
+ if (!controller.signal.aborted) this.missing.add(type);
813
930
  return;
814
931
  }
815
- if (this.disposed) return;
932
+ if (this.disposed || controller.signal.aborted || !this.wanted.has(type)) {
933
+ disposeUnbound(model);
934
+ return;
935
+ }
936
+ const bytes = modelBytes(model);
937
+ this.prune(bytes);
938
+ if (this.cacheStats().bytes + bytes > this.byteLimit) {
939
+ disposeUnbound(model);
940
+ if (bytes > this.byteLimit) this.missing.add(type);
941
+ return;
942
+ }
943
+ const originalMaterials = /* @__PURE__ */ new Set();
944
+ model.traverse((node) => {
945
+ const mesh = node;
946
+ if (mesh.isMesh) for (const material of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) originalMaterials.add(material);
947
+ });
816
948
  const visual = createVehicleVisual(model, data);
949
+ bound = visual;
950
+ for (const material of originalMaterials) material.dispose();
951
+ this.resident.set(visual, {
952
+ bytes,
953
+ used: ++this.clock
954
+ });
817
955
  visual.object.visible = false;
818
956
  visual.object.traverse((object) => {
819
957
  object.userData.walkIgnore = true;
958
+ tintGlass(object);
820
959
  });
821
960
  this.group.add(visual.object);
822
961
  await this.options.prepare?.(visual.object);
823
- if (this.disposed) {
824
- visual.dispose();
962
+ if (this.disposed || controller.signal.aborted || !this.wanted.has(type) || this.cacheStats().bytes > this.byteLimit) {
963
+ this.release(visual);
825
964
  return;
826
965
  }
827
- const pool = this.idle.get(type) ?? [];
828
- pool.push(visual);
829
- this.idle.set(type, pool);
966
+ const candidate = this.candidates.find((c) => c.type === type && !this.active.has(c.id));
967
+ if (candidate && this.active.size < this.max) this.activate(candidate, visual);
968
+ else {
969
+ const pool = this.idle.get(type) ?? [];
970
+ pool.push(visual);
971
+ this.idle.set(type, pool);
972
+ }
973
+ this.prune();
830
974
  } catch {
831
- this.missing.add(type);
975
+ if (bound) this.release(bound);
976
+ if (!controller.signal.aborted) this.missing.add(type);
832
977
  } finally {
833
978
  this.loading.delete(type);
834
979
  }
@@ -854,8 +999,8 @@ var VehicleDetail = class {
854
999
  }
855
1000
  dispose() {
856
1001
  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();
1002
+ for (const controller of this.loading.values()) controller.abort();
1003
+ for (const visual of this.resident.keys()) this.release(visual);
859
1004
  this.active.clear();
860
1005
  this.idle.clear();
861
1006
  this.group.removeFromParent();
@@ -904,6 +1049,11 @@ function createAmbientRenderer(options) {
904
1049
  const applyBudget = () => {
905
1050
  detail?.setMax(budget.detailedCars);
906
1051
  boatDetail?.setMax(budget.detailedBoats ?? 4);
1052
+ for (const pool of [detail, boatDetail]) pool?.setCacheBudget({
1053
+ ...budget.idleVehicleModels !== void 0 ? { idleModels: budget.idleVehicleModels } : {},
1054
+ ...budget.vehicleModelBytes !== void 0 ? { modelBytes: budget.vehicleModelBytes } : {},
1055
+ ...budget.vehicleLoads !== void 0 ? { concurrentLoads: budget.vehicleLoads } : {}
1056
+ });
907
1057
  figures.setBudget({
908
1058
  skinned: budget.skinnedFigures,
909
1059
  radius: budget.figureRadius,
@@ -1066,6 +1216,750 @@ function createAmbientRenderer(options) {
1066
1216
  };
1067
1217
  }
1068
1218
  //#endregion
1219
+ //#region src/client/wildlife-geometry.ts
1220
+ /** Faceted, opaque, bounded animal families. One skinned draw per animal; shared recipe geometry. */
1221
+ /** +Z is the head/front. All geometry is baked in bind pose and skin weights are rigid. */
1222
+ function wildlifeGeometry(species, tier) {
1223
+ const positions = [], normals = [], colors = [], skin = [], weights = [];
1224
+ const joints = [{
1225
+ name: "body",
1226
+ pivot: [
1227
+ 0,
1228
+ 0,
1229
+ 0
1230
+ ]
1231
+ }];
1232
+ const body = species.body, h = body.height, l = body.length, w = body.width;
1233
+ const detail = (name) => body.details.includes(name);
1234
+ const joint = (name, pivot) => {
1235
+ joints.push({
1236
+ name,
1237
+ pivot
1238
+ });
1239
+ return joints.length - 1;
1240
+ };
1241
+ const add = (geometry, matrix, color, bone = 0) => {
1242
+ const source = geometry.index === null ? geometry : geometry.toNonIndexed();
1243
+ source.applyMatrix4(matrix);
1244
+ source.computeVertexNormals();
1245
+ const p = source.getAttribute("position"), n = source.getAttribute("normal");
1246
+ const c = new THREE.Color(color);
1247
+ for (let i = 0; i < p.count; i++) {
1248
+ positions.push(p.getX(i), p.getY(i), p.getZ(i));
1249
+ normals.push(n.getX(i), n.getY(i), n.getZ(i));
1250
+ colors.push(c.r, c.g, c.b);
1251
+ skin.push(bone, 0, 0, 0);
1252
+ weights.push(1, 0, 0, 0);
1253
+ }
1254
+ source.dispose();
1255
+ if (source !== geometry) geometry.dispose();
1256
+ };
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);
1259
+ };
1260
+ const segment = (a, b, r1, r2, color = body.color, bone = 0) => {
1261
+ const start = new THREE.Vector3(...a), end = new THREE.Vector3(...b), delta = end.clone().sub(start);
1262
+ add(new THREE.CylinderGeometry(r2, r1, delta.length(), tier === 0 ? 6 : 4, 1, false), new THREE.Matrix4().compose(start.add(end).multiplyScalar(.5), new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 1, 0), delta.normalize()), new THREE.Vector3(1, 1, 1)), color, bone);
1263
+ };
1264
+ const wedge = (points, thickness, color, bone) => {
1265
+ const [a, b, c] = points;
1266
+ if (a === void 0 || b === void 0 || c === void 0) return;
1267
+ const vertices = [
1268
+ a,
1269
+ b,
1270
+ c,
1271
+ [
1272
+ a[0],
1273
+ a[1] + thickness,
1274
+ a[2]
1275
+ ],
1276
+ [
1277
+ b[0],
1278
+ b[1] + thickness,
1279
+ b[2]
1280
+ ],
1281
+ [
1282
+ c[0],
1283
+ c[1] + thickness,
1284
+ c[2]
1285
+ ]
1286
+ ];
1287
+ const indices = [
1288
+ 0,
1289
+ 2,
1290
+ 1,
1291
+ 3,
1292
+ 4,
1293
+ 5,
1294
+ 0,
1295
+ 1,
1296
+ 4,
1297
+ 0,
1298
+ 4,
1299
+ 3,
1300
+ 1,
1301
+ 2,
1302
+ 5,
1303
+ 1,
1304
+ 5,
1305
+ 4,
1306
+ 2,
1307
+ 0,
1308
+ 3,
1309
+ 2,
1310
+ 3,
1311
+ 5
1312
+ ];
1313
+ const geometry = new THREE.BufferGeometry();
1314
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(vertices.flat(), 3));
1315
+ geometry.setIndex(indices);
1316
+ add(geometry, new THREE.Matrix4(), color, bone);
1317
+ };
1318
+ const eye = (x, y, z, radius, bone = 0) => {
1319
+ if (tier !== 0) return;
1320
+ ellipsoid([
1321
+ x,
1322
+ y,
1323
+ z
1324
+ ], [
1325
+ radius,
1326
+ radius,
1327
+ radius * .55
1328
+ ], "#272a26", bone);
1329
+ };
1330
+ if (body.family === "bird" || body.family === "insect") {
1331
+ const insect = body.family === "insect";
1332
+ const flying = species.motion === "fly";
1333
+ const y = flying ? 0 : detail("wader") ? h * .66 : h * .48;
1334
+ ellipsoid([
1335
+ 0,
1336
+ y,
1337
+ 0
1338
+ ], [
1339
+ w * .5,
1340
+ h * (detail("wader") ? .19 : .3),
1341
+ l * .38
1342
+ ]);
1343
+ const headY = y + h * (detail("wader") ? .23 : .21), headZ = l * .36;
1344
+ if (detail("wader")) segment([
1345
+ 0,
1346
+ y,
1347
+ l * .2
1348
+ ], [
1349
+ 0,
1350
+ headY,
1351
+ headZ
1352
+ ], w * .16, w * .12);
1353
+ ellipsoid([
1354
+ 0,
1355
+ headY,
1356
+ headZ
1357
+ ], [
1358
+ w * .32,
1359
+ h * .17,
1360
+ l * .17
1361
+ ], body.accent);
1362
+ segment([
1363
+ 0,
1364
+ headY,
1365
+ l * .46
1366
+ ], [
1367
+ 0,
1368
+ headY - h * .035,
1369
+ l * (detail("wader") ? .87 : .66)
1370
+ ], w * .1, w * .012, "#a2966e");
1371
+ eye(-w * .27, headY + h * .035, l * .45, h * .045);
1372
+ eye(w * .27, headY + h * .035, l * .45, h * .045);
1373
+ for (const side of [-1, 1]) {
1374
+ const wing = joint(side < 0 ? "wing.l" : "wing.r", [
1375
+ side * w * .25,
1376
+ y,
1377
+ l * .04
1378
+ ]);
1379
+ if (flying) {
1380
+ const span = l * (detail("raptor") ? 1.65 : insect ? .9 : 1.05);
1381
+ wedge([
1382
+ [
1383
+ side * w * .25,
1384
+ y,
1385
+ l * .25
1386
+ ],
1387
+ [
1388
+ side * span,
1389
+ y - h * .08,
1390
+ -l * .1
1391
+ ],
1392
+ [
1393
+ side * w * .4,
1394
+ y,
1395
+ -l * .42
1396
+ ]
1397
+ ], h * .045, body.accent, wing);
1398
+ if (tier === 0 && !insect) for (let i = 0; i < 3; i++) wedge([
1399
+ [
1400
+ side * span * (.65 + i * .1),
1401
+ y - h * .05,
1402
+ -l * .04
1403
+ ],
1404
+ [
1405
+ side * span * (.82 + i * .1),
1406
+ y - h * .08,
1407
+ -l * (.23 + i * .03)
1408
+ ],
1409
+ [
1410
+ side * span * (.61 + i * .1),
1411
+ y,
1412
+ -l * .22
1413
+ ]
1414
+ ], h * .025, body.color, wing);
1415
+ } else ellipsoid([
1416
+ side * w * .38,
1417
+ y + h * .05,
1418
+ -l * .03
1419
+ ], [
1420
+ w * .16,
1421
+ h * .2,
1422
+ l * .34
1423
+ ], body.accent, wing);
1424
+ if (!flying && species.motion !== "swim") {
1425
+ const leg = joint(side < 0 ? "leg.l" : "leg.r", [
1426
+ side * w * .2,
1427
+ y,
1428
+ 0
1429
+ ]);
1430
+ segment([
1431
+ side * w * .2,
1432
+ y,
1433
+ 0
1434
+ ], [
1435
+ side * w * .2,
1436
+ h * .03,
1437
+ l * .04
1438
+ ], w * .045, w * .03, "#655f4a", leg);
1439
+ segment([
1440
+ side * w * .2,
1441
+ h * .03,
1442
+ l * .04
1443
+ ], [
1444
+ side * w * .2,
1445
+ h * .03,
1446
+ l * .18
1447
+ ], w * .03, w * .025, "#655f4a", leg);
1448
+ }
1449
+ }
1450
+ wedge([
1451
+ [
1452
+ -w * .22,
1453
+ y,
1454
+ -l * .3
1455
+ ],
1456
+ [
1457
+ w * .22,
1458
+ y,
1459
+ -l * .3
1460
+ ],
1461
+ [
1462
+ 0,
1463
+ y - h * .08,
1464
+ -l * (detail("long-tail") ? 1 : .64)
1465
+ ]
1466
+ ], h * .05, body.accent, 0);
1467
+ } else if (body.family === "fish") {
1468
+ ellipsoid([
1469
+ 0,
1470
+ 0,
1471
+ 0
1472
+ ], [
1473
+ w * .5,
1474
+ h * .48,
1475
+ l * .4
1476
+ ]);
1477
+ const tail = joint("tail", [
1478
+ 0,
1479
+ 0,
1480
+ -l * .32
1481
+ ]);
1482
+ ellipsoid([
1483
+ 0,
1484
+ 0,
1485
+ -l * .5
1486
+ ], [
1487
+ w * .08,
1488
+ h * .65,
1489
+ l * .13
1490
+ ], body.accent, tail);
1491
+ for (const side of [-1, 1]) wedge([
1492
+ [
1493
+ side * w * .2,
1494
+ 0,
1495
+ l * .1
1496
+ ],
1497
+ [
1498
+ side * w,
1499
+ -h * .12,
1500
+ -l * .18
1501
+ ],
1502
+ [
1503
+ side * w * .25,
1504
+ 0,
1505
+ -l * .2
1506
+ ]
1507
+ ], h * .04, body.accent, 0);
1508
+ eye(-w * .4, h * .14, l * .25, h * .09);
1509
+ eye(w * .4, h * .14, l * .25, h * .09);
1510
+ } else if (body.family === "reptile") {
1511
+ ellipsoid([
1512
+ 0,
1513
+ h * .48,
1514
+ -l * .05
1515
+ ], [
1516
+ w * .5,
1517
+ h * .28,
1518
+ l * .36
1519
+ ]);
1520
+ const head = joint("head", [
1521
+ 0,
1522
+ h * .44,
1523
+ l * .28
1524
+ ]);
1525
+ ellipsoid([
1526
+ 0,
1527
+ h * .43,
1528
+ l * .39
1529
+ ], [
1530
+ w * .34,
1531
+ h * .2,
1532
+ l * .18
1533
+ ], body.color, head);
1534
+ for (const side of [-1, 1]) {
1535
+ eye(side * w * .29, h * .54, l * .42, h * .045, head);
1536
+ for (const front of [false, true]) {
1537
+ const z = (front ? 1 : -1) * l * .22;
1538
+ const pivot = [
1539
+ side * w * .35,
1540
+ h * .4,
1541
+ z
1542
+ ];
1543
+ const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1544
+ const knee = [
1545
+ side * w * .85,
1546
+ h * .2,
1547
+ z + (front ? -1 : 1) * l * .09
1548
+ ];
1549
+ const foot = [
1550
+ side * w * .98,
1551
+ h * .04,
1552
+ z + (front ? 1 : -1) * l * .1
1553
+ ];
1554
+ segment(pivot, knee, w * .12, w * .08, body.color, leg);
1555
+ segment(knee, foot, w * .08, w * .035, body.color, leg);
1556
+ if (tier < 2) ellipsoid(foot, [
1557
+ w * .12,
1558
+ h * .025,
1559
+ l * .055
1560
+ ], body.accent, leg);
1561
+ }
1562
+ }
1563
+ const tail = joint("tail", [
1564
+ 0,
1565
+ h * .4,
1566
+ -l * .34
1567
+ ]);
1568
+ const bend = [
1569
+ w * .12,
1570
+ h * .14,
1571
+ -l * .77
1572
+ ];
1573
+ segment([
1574
+ 0,
1575
+ h * .4,
1576
+ -l * .34
1577
+ ], bend, w * .22, w * .1, body.color, tail);
1578
+ segment(bend, [
1579
+ w * .32,
1580
+ h * .06,
1581
+ -l * 1.3
1582
+ ], w * .1, w * .005, body.color, tail);
1583
+ } else {
1584
+ const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant";
1585
+ const torsoY = h * (hopper ? .57 : low ? .5 : .72);
1586
+ const torsoRadius = h * (hopper ? .3 : low ? .31 : elephant ? .32 : .19);
1587
+ ellipsoid([
1588
+ 0,
1589
+ torsoY,
1590
+ -l * .04
1591
+ ], [
1592
+ w * .5,
1593
+ torsoRadius,
1594
+ l * (hopper ? .28 : .38)
1595
+ ]);
1596
+ if (tier === 0) ellipsoid([
1597
+ 0,
1598
+ torsoY - torsoRadius * .48,
1599
+ l * .04
1600
+ ], [
1601
+ w * .39,
1602
+ torsoRadius * .62,
1603
+ l * .29
1604
+ ], elephant ? body.color : body.accent);
1605
+ const neckTop = h * (hopper ? .92 : low ? .61 : elephant ? .9 : detail("long-neck") ? 1.38 : 1.1);
1606
+ const headZ = l * (hopper ? .3 : .47), headR = h * (elephant ? .18 : low ? .23 : .13);
1607
+ segment([
1608
+ 0,
1609
+ torsoY,
1610
+ l * .27
1611
+ ], [
1612
+ 0,
1613
+ neckTop - headR * .4,
1614
+ headZ - l * .07
1615
+ ], w * (elephant ? .4 : low ? .32 : .23), w * (low ? .28 : .17));
1616
+ const head = joint("head", [
1617
+ 0,
1618
+ neckTop - headR * .6,
1619
+ headZ - l * .06
1620
+ ]);
1621
+ ellipsoid([
1622
+ 0,
1623
+ neckTop,
1624
+ headZ
1625
+ ], [
1626
+ w * (elephant ? .4 : low ? .34 : .28),
1627
+ headR,
1628
+ l * (elephant || low ? .16 : .12)
1629
+ ], 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);
1639
+ for (const side of [-1, 1]) {
1640
+ const earHeight = body.family === "lagomorph" ? h * .45 : hopper ? h * .2 : elephant ? h * .25 : low ? h * .075 : h * .11;
1641
+ ellipsoid([
1642
+ side * w * (elephant ? .52 : .22),
1643
+ neckTop + headR * (elephant ? .15 : .8),
1644
+ headZ - l * .04
1645
+ ], [
1646
+ w * (elephant ? .35 : .1),
1647
+ earHeight,
1648
+ l * .035
1649
+ ], elephant ? body.color : body.accent, head);
1650
+ 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
1656
+ ];
1657
+ const tip = [
1658
+ side * w * .42,
1659
+ neckTop + h * .36,
1660
+ headZ - l * .04
1661
+ ];
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);
1674
+ }
1675
+ }
1676
+ for (const front of [false, true]) {
1677
+ const legZ = (front ? 1 : -1) * l * (hopper ? .15 : .25);
1678
+ const pivot = [
1679
+ side * w * .34,
1680
+ torsoY - torsoRadius * .2,
1681
+ legZ
1682
+ ];
1683
+ const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1684
+ 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
+ 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);
1706
+ }
1707
+ }
1708
+ const tail = joint("tail", [
1709
+ 0,
1710
+ torsoY,
1711
+ -l * .34
1712
+ ]);
1713
+ if (body.family === "rodent" && detail("bushy-tail")) {
1714
+ segment([
1715
+ 0,
1716
+ torsoY,
1717
+ -l * .34
1718
+ ], [
1719
+ 0,
1720
+ h * 1.12,
1721
+ -l * .65
1722
+ ], w * .19, w * .16, body.color, tail);
1723
+ ellipsoid([
1724
+ 0,
1725
+ h * 1,
1726
+ -l * .6
1727
+ ], [
1728
+ w * .36,
1729
+ h * .57,
1730
+ l * .27
1731
+ ], body.color, tail);
1732
+ } else if (detail("long-tail") || detail("bushy-tail") || [
1733
+ "canid",
1734
+ "feline",
1735
+ "elephant"
1736
+ ].includes(body.family)) {
1737
+ const end = [
1738
+ 0,
1739
+ elephant ? h * .12 : hopper ? h * .08 : torsoY * .55,
1740
+ -l * (elephant ? .44 : detail("long-tail") ? 1.1 : .88)
1741
+ ];
1742
+ segment([
1743
+ 0,
1744
+ torsoY,
1745
+ -l * .34
1746
+ ], end, w * (detail("bushy-tail") ? .2 : hopper ? .18 : .06), w * .025, body.color, tail);
1747
+ if (detail("bushy-tail")) ellipsoid([
1748
+ 0,
1749
+ torsoY * .72,
1750
+ -l * .68
1751
+ ], [
1752
+ w * .22,
1753
+ h * .14,
1754
+ l * .2
1755
+ ], body.accent, tail);
1756
+ } else if (body.family !== "rodent") ellipsoid([
1757
+ 0,
1758
+ torsoY,
1759
+ -l * .39
1760
+ ], [
1761
+ w * .1,
1762
+ h * .05,
1763
+ l * .05
1764
+ ], body.accent, tail);
1765
+ if (elephant) {
1766
+ const trunk = joint("trunk", [
1767
+ 0,
1768
+ neckTop - headR * .3,
1769
+ headZ + l * .11
1770
+ ]);
1771
+ segment([
1772
+ 0,
1773
+ neckTop - headR * .3,
1774
+ headZ + l * .11
1775
+ ], [
1776
+ 0,
1777
+ h * .38,
1778
+ headZ + l * .17
1779
+ ], w * .14, w * .09, body.color, trunk);
1780
+ segment([
1781
+ 0,
1782
+ h * .38,
1783
+ headZ + l * .17
1784
+ ], [
1785
+ 0,
1786
+ h * .16,
1787
+ headZ + l * .22
1788
+ ], w * .09, w * .04, body.color, trunk);
1789
+ }
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
+ }
1803
+ const geometry = new THREE.BufferGeometry();
1804
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
1805
+ geometry.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3));
1806
+ geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
1807
+ geometry.setAttribute("skinIndex", new THREE.Uint16BufferAttribute(skin, 4));
1808
+ geometry.setAttribute("skinWeight", new THREE.Float32BufferAttribute(weights, 4));
1809
+ geometry.computeBoundingBox();
1810
+ geometry.computeBoundingSphere();
1811
+ return {
1812
+ geometry,
1813
+ joints,
1814
+ triangles: positions.length / 9
1815
+ };
1816
+ }
1817
+ //#endregion
1818
+ //#region src/client/wildlife.ts
1819
+ /** One opaque skinned draw per visible animal. Geometry is shared and released on disposal. */
1820
+ var WildlifeRenderer = class {
1821
+ prepare;
1822
+ group = new THREE.Group();
1823
+ material = new THREE.MeshStandardMaterial({
1824
+ vertexColors: true,
1825
+ roughness: .85,
1826
+ metalness: 0,
1827
+ flatShading: true
1828
+ });
1829
+ cache = /* @__PURE__ */ new Map();
1830
+ active = /* @__PURE__ */ new Map();
1831
+ frame = 0;
1832
+ cursor = 0;
1833
+ budget = {
1834
+ animals: 32,
1835
+ radius: 180,
1836
+ posesPerFrame: 16
1837
+ };
1838
+ constructor(prepare) {
1839
+ this.prepare = prepare;
1840
+ this.group.name = "ambient:wildlife";
1841
+ }
1842
+ setBudget(budget) {
1843
+ this.budget = {
1844
+ ...this.budget,
1845
+ ...budget
1846
+ };
1847
+ }
1848
+ get count() {
1849
+ return this.active.size;
1850
+ }
1851
+ get triangles() {
1852
+ let count = 0;
1853
+ for (const animal of this.active.values()) count += animal.geometry.triangles;
1854
+ return count;
1855
+ }
1856
+ remove(id, animal) {
1857
+ animal.mesh.skeleton.dispose();
1858
+ animal.root.removeFromParent();
1859
+ this.active.delete(id);
1860
+ }
1861
+ update(agents, species, camera, seconds, appearance) {
1862
+ this.frame++;
1863
+ const visible = agents.map((agent) => ({
1864
+ agent,
1865
+ distance: Math.hypot(agent.position[0] - camera[0], agent.position[1] - camera[1], agent.position[2] - camera[2])
1866
+ })).filter(({ distance }) => distance < this.budget.radius).sort((a, b) => a.distance - b.distance || (a.agent.id < b.agent.id ? -1 : 1)).slice(0, this.budget.animals);
1867
+ const pose = [];
1868
+ for (const { agent, distance } of visible) {
1869
+ const base = species.get(agent.species);
1870
+ const recipe = base === void 0 ? void 0 : appearance?.(agent, base) ?? base;
1871
+ if (recipe === void 0 || recipe.body.family === "insect" && distance > 10 || recipe.body.height < .1 && distance > 25) continue;
1872
+ const tier = distance < 35 ? 0 : distance < 100 ? 1 : 2;
1873
+ const key = `${recipe.id}@${recipe.version}/${recipe.body.color}/${tier}`;
1874
+ let animal = this.active.get(agent.id);
1875
+ if (animal?.key !== key) {
1876
+ if (animal !== void 0) this.remove(agent.id, animal);
1877
+ let geometry = this.cache.get(key);
1878
+ if (geometry === void 0) {
1879
+ geometry = wildlifeGeometry(recipe, tier);
1880
+ this.cache.set(key, geometry);
1881
+ }
1882
+ const mesh = new THREE.SkinnedMesh(geometry.geometry, this.material);
1883
+ const bones = geometry.joints.map((joint) => {
1884
+ const bone = new THREE.Bone();
1885
+ bone.name = joint.name;
1886
+ bone.position.set(...joint.pivot);
1887
+ mesh.add(bone);
1888
+ return bone;
1889
+ });
1890
+ mesh.bind(new THREE.Skeleton(bones));
1891
+ mesh.castShadow = tier === 0;
1892
+ mesh.receiveShadow = true;
1893
+ const root = new THREE.Group();
1894
+ root.add(mesh);
1895
+ root.userData.walkIgnore = true;
1896
+ root.traverse((object) => {
1897
+ object.userData.walkIgnore = true;
1898
+ });
1899
+ this.group.add(root);
1900
+ this.prepare?.(root);
1901
+ animal = {
1902
+ root,
1903
+ mesh,
1904
+ bones,
1905
+ geometry,
1906
+ key,
1907
+ seen: this.frame
1908
+ };
1909
+ this.active.set(agent.id, animal);
1910
+ }
1911
+ animal.seen = this.frame;
1912
+ animal.root.position.set(...agent.position);
1913
+ animal.root.rotation.y = agent.heading;
1914
+ pose.push({
1915
+ animal,
1916
+ agent,
1917
+ species: recipe
1918
+ });
1919
+ }
1920
+ for (const [id, animal] of this.active) if (animal.seen !== this.frame) this.remove(id, animal);
1921
+ const count = Math.min(pose.length, this.budget.posesPerFrame);
1922
+ const near = Math.ceil(count / 2), remaining = pose.length - near;
1923
+ for (let i = 0; i < count; i++) {
1924
+ const entry = pose[i < near ? i : near + (this.cursor + i - near) % remaining];
1925
+ if (entry === void 0) continue;
1926
+ const { animal, agent, species: recipe } = entry;
1927
+ const gait = agent.phase * Math.PI * 2;
1928
+ const flying = recipe.motion === "fly";
1929
+ const flapping = Math.sin(seconds * (recipe.body.family === "insect" ? 45 : 6 / Math.max(.3, recipe.body.length)) + agent.seed % 100);
1930
+ animal.root.position.y = agent.position[1] + (recipe.motion === "hop" && agent.speed > .01 ? Math.abs(Math.sin(gait)) * recipe.body.height * .12 : 0);
1931
+ for (let index = 0; index < animal.bones.length; index++) {
1932
+ const bone = animal.bones[index];
1933
+ if (bone === void 0) continue;
1934
+ const name = animal.geometry.joints[index]?.name ?? "";
1935
+ bone.rotation.set(0, 0, 0);
1936
+ if (name.startsWith("leg.") && agent.speed > .01) {
1937
+ const reverse = name.includes("rear") !== name.endsWith(".r");
1938
+ bone.rotation.x = Math.sin(gait + (recipe.motion === "hop" ? 0 : reverse ? Math.PI : 0)) * (recipe.motion === "hop" ? .22 : .27);
1939
+ } else if (name.startsWith("wing.") && flying) bone.rotation.z = flapping * (name.endsWith(".l") ? -.55 : .55);
1940
+ else if (name === "tail") bone.rotation.y = Math.sin(recipe.motion === "swim" ? seconds * 5 : gait) * .15;
1941
+ else if (name === "head" && agent.speed < .01) bone.rotation.x = Math.sin(seconds * .4 + agent.seed % 30) * .08;
1942
+ else if (name === "trunk") bone.rotation.x = Math.sin(seconds * .7) * .06;
1943
+ }
1944
+ }
1945
+ if (remaining > 0) this.cursor = (this.cursor + count - near) % remaining;
1946
+ if (this.cache.size > 96) {
1947
+ const used = new Set([...this.active.values()].map((animal) => animal.key));
1948
+ for (const [key, geometry] of this.cache) if (!used.has(key)) {
1949
+ geometry.geometry.dispose();
1950
+ this.cache.delete(key);
1951
+ }
1952
+ }
1953
+ }
1954
+ dispose() {
1955
+ for (const [id, animal] of this.active) this.remove(id, animal);
1956
+ for (const geometry of this.cache.values()) geometry.geometry.dispose();
1957
+ this.cache.clear();
1958
+ this.material.dispose();
1959
+ this.group.removeFromParent();
1960
+ }
1961
+ };
1962
+ //#endregion
1069
1963
  //#region src/client.ts
1070
1964
  /**
1071
1965
  * @bendyline/molen-ambient/client — rendering for ambient NPCs: instanced vehicle proxies with
@@ -1074,4 +1968,4 @@ function createAmbientRenderer(options) {
1074
1968
  */
1075
1969
  registerAmbientSchema();
1076
1970
  //#endregion
1077
- export { AmbientFigures, ProxyBatches, ambientVehicleKind, createAmbientRenderer, proxyGeometry, proxyHeight, proxyShapeFor };
1971
+ export { AmbientFigures, ProxyBatches, WildlifeRenderer, ambientVehicleKind, createAmbientRenderer, proxyGeometry, proxyHeight, proxyShapeFor, wildlifeGeometry };