@bendyline/molen-ambient 0.0.9 → 0.0.11

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,9 +1,9 @@
1
- import { d as registerAmbientSchema, r as AmbientAgent } from "./components-CupqU6fR.mjs";
1
+ import { d as registerAmbientSchema, r as AmbientAgent } from "./components-D4HrizHN.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";
5
5
  import { mergeGeometries } from "three/addons/utils/BufferGeometryUtils.js";
6
- import { FigureGeometryCache, applyPose, evaluatePose, figureBodyToSkinnedMesh, figureSeed, resolveFigureDescriptor, strideRateFor } from "@bendyline/molen-figures/client";
6
+ import { FigureGeometryCache, applyPose, evaluatePose, figureBodyToSkinnedMesh, figureSeed, resolveFigureDescriptor, strideRateFor, worldToFigure } from "@bendyline/molen-figures/client";
7
7
  import { createAircraftVisual } from "@bendyline/molen-client/aircraft";
8
8
  //#region src/client/proxies.ts
9
9
  /**
@@ -69,6 +69,7 @@ function aircraftGeometry({ length, width }) {
69
69
  }
70
70
  /** The shape to draw for an agent kind and its dimensions. */
71
71
  function proxyShapeFor(kind, length) {
72
+ if (kind === "boat") return "boat";
72
73
  if (kind === "train") return "rail";
73
74
  if (kind === "aircraft") return "aircraft";
74
75
  return length > 7.5 ? "bus" : "car";
@@ -77,6 +78,7 @@ function proxyShapeFor(kind, length) {
77
78
  function proxyHeight(shape, length, width, type) {
78
79
  if (shape === "bus") return 3.1;
79
80
  if (shape === "rail") return 3.6;
81
+ if (shape === "boat") return Math.max(2, length * .18);
80
82
  if (shape === "aircraft") return length * .25;
81
83
  if (/van/.test(type) || width >= 1.98) return 2;
82
84
  if (/suv|pickup/.test(type)) return 1.75;
@@ -100,7 +102,17 @@ function proxyGeometry(shape, dims) {
100
102
  });
101
103
  else if (shape === "bus") geometry = busGeometry(d);
102
104
  else if (shape === "rail") geometry = railGeometry(d);
103
- else geometry = aircraftGeometry(d);
105
+ else if (shape === "boat") {
106
+ const hull = box(0, d.height * .08, 0, d.width, d.height * .25, d.length, WHITE);
107
+ const position = hull.getAttribute("position");
108
+ for (let i = 0; i < position.count; i++) if (position.getZ(i) > 0) position.setX(i, position.getX(i) * .15);
109
+ hull.computeVertexNormals();
110
+ geometry = merged([
111
+ hull,
112
+ box(0, d.height * .42, -d.length * .2, d.width * .65, d.height * .45, d.length * .4, WHITE),
113
+ box(0, d.height * .5, -d.length * .2, d.width * .66, d.height * .15, d.length * .36, GLASS)
114
+ ]);
115
+ } else geometry = aircraftGeometry(d);
104
116
  cache.set(key, geometry);
105
117
  return geometry;
106
118
  }
@@ -224,8 +236,14 @@ var ProxyBatches = class {
224
236
  * Pedestrians as procedural figures, posed without the kernel figures systems (so a main-thread
225
237
  * host never loads the script host): gait comes from each agent's speed and the distance it has
226
238
  * walked, which keeps feet planted across lane changes. Meshes are pooled per body key, detail
227
- * tiers follow distance with hysteresis, and pose evaluation is budgeted per frame.
239
+ * tiers follow distance with hysteresis, and pose evaluation is budgeted per frame. A walker can
240
+ * also be seated (`mode: 'sit'`, `anchor: 'eye'` for a vehicle seat) or turn its head toward a
241
+ * point (`lookAt`), which is how a host draws its own characters with the same bodies.
228
242
  */
243
+ /** Whether a walker's pose depends on more than its gait, so a rigid stand-in would be wrong. */
244
+ function posed(walker) {
245
+ return walker.mode === "sit" || walker.anchor !== void 0 && walker.anchor !== "origin";
246
+ }
229
247
  var AmbientFigures = class {
230
248
  prepare;
231
249
  group = new THREE.Group();
@@ -319,6 +337,7 @@ var AmbientFigures = class {
319
337
  let tier = this.tierFor(distance, entry?.tier);
320
338
  if (tier !== void 0 && tier < 2) if (skinned >= this.budget.skinned) tier = 2;
321
339
  else skinned++;
340
+ if (tier === 2 && posed(w)) tier = void 0;
322
341
  if (tier === void 0) {
323
342
  if (entry !== void 0) {
324
343
  this.releaseMesh(entry.key, entry.mesh);
@@ -376,23 +395,40 @@ var AmbientFigures = class {
376
395
  const rest = animated.slice(nearest);
377
396
  for (let i = 0; i < budget - nearest && rest.length > 0 && i < rest.length; i++) chosen.push(rest[(this.cursor + i) % rest.length]);
378
397
  if (rest.length > 0) this.cursor = (this.cursor + budget - nearest) % rest.length;
398
+ const picked = new Set(chosen.map(([id]) => id));
399
+ for (const pair of rest) {
400
+ const walker = byId.get(pair[0]);
401
+ if (pair[1].pose === void 0 && !picked.has(pair[0]) && walker !== void 0 && posed(walker)) chosen.push(pair);
402
+ }
379
403
  for (const [id, entry] of chosen) {
380
404
  const walker = byId.get(id);
381
405
  if (walker === void 0) continue;
382
- const moving = walker.speed > .05;
406
+ const seated = walker.mode === "sit";
407
+ const moving = !seated && walker.speed > .05 && walker.mode !== "idle";
383
408
  const strideRate = moving ? strideRateFor(walker.speed, entry.rig.legLength) : 0;
384
409
  const stride = strideRate > 0 ? walker.speed / strideRate : 1;
385
410
  const state = {
386
- mode: moving ? "walk" : "idle",
387
- speed: walker.speed,
411
+ mode: seated ? "sit" : moving ? walker.mode === "run" ? "run" : "walk" : "idle",
412
+ speed: moving ? walker.speed : 0,
388
413
  strideRate,
389
414
  phaseAt: moving ? entry.walked / stride % 1 : 0,
390
415
  phaseAtTick: tick,
391
416
  modeAtTick: walker.since
392
417
  };
418
+ const lookTarget = walker.lookAt !== void 0 ? worldToFigure({
419
+ pos: [...walker.pos],
420
+ rot: [...walker.rot]
421
+ }, [
422
+ walker.lookAt[0],
423
+ walker.lookAt[1],
424
+ walker.lookAt[2]
425
+ ]) : void 0;
393
426
  entry.pose = evaluatePose(entry.rig, state, tick, {
394
427
  tickRate,
395
- seed: entry.seed
428
+ seed: entry.seed,
429
+ riding: walker.riding,
430
+ ...lookTarget !== void 0 ? { lookTarget } : {},
431
+ ...walker.anchor !== void 0 && walker.anchor !== "origin" ? { anchor: walker.anchor } : {}
396
432
  }, entry.pose);
397
433
  applyPose(entry.pose, entry.mesh.bones);
398
434
  }
@@ -498,7 +534,8 @@ function movingNodes(spec) {
498
534
  ...visual.flaps.map((binding) => binding.node),
499
535
  ...visual.ailerons.map((binding) => binding.node),
500
536
  ...visual.controlStick !== void 0 ? [visual.controlStick.node] : [],
501
- ...visual.collective !== void 0 ? [visual.collective.node] : []
537
+ ...visual.collective !== void 0 ? [visual.collective.node] : [],
538
+ ...visual.animation?.bindings.map((binding) => binding.node) ?? []
502
539
  ]);
503
540
  }
504
541
  /**
@@ -555,6 +592,13 @@ function mergeStaticAircraftParts(model, spec) {
555
592
  }
556
593
  for (const mesh of merged) model.add(mesh);
557
594
  }
595
+ /** Move toward a selection at `time` seconds for full travel; 0, unset or a first sight is instant. */
596
+ function travel(position, selected, time, dt) {
597
+ const target = selected ? 1 : 0;
598
+ if (time === void 0 || time <= 0 || Number.isNaN(position)) return target;
599
+ const step = dt / time;
600
+ return position + Math.max(-step, Math.min(step, target - position));
601
+ }
558
602
  const INPUT = {
559
603
  power: .8,
560
604
  pitch: 0,
@@ -593,6 +637,9 @@ var AmbientAircraft = class {
593
637
  continue;
594
638
  }
595
639
  entry.rotor = (entry.rotor + dt * 40) % (Math.PI * 2);
640
+ const airplane = entry.spec.airplane;
641
+ entry.gear = travel(entry.gear, plane.descending, airplane?.gearTransitTime, dt);
642
+ entry.flaps = travel(entry.flaps, plane.descending, airplane?.flapTransitTime, dt);
596
643
  const object = entry.visual.object;
597
644
  object.position.set(plane.pos[0] ?? 0, plane.pos[1] ?? 0, plane.pos[2] ?? 0);
598
645
  object.quaternion.set(plane.rot[0] ?? 0, plane.rot[1] ?? 0, plane.rot[2] ?? 0, plane.rot[3] ?? 1);
@@ -604,7 +651,9 @@ var AmbientAircraft = class {
604
651
  pitch: 0,
605
652
  roll: 0,
606
653
  rpm: 2400,
607
- verticalSpeed: 0
654
+ verticalSpeed: 0,
655
+ gearPosition: entry.gear,
656
+ flapPosition: entry.flaps
608
657
  }, {
609
658
  ...INPUT,
610
659
  gear: plane.descending,
@@ -651,7 +700,10 @@ var AmbientAircraft = class {
651
700
  }
652
701
  this.active.set(id, {
653
702
  visual,
654
- rotor: 0
703
+ rotor: 0,
704
+ gear: NaN,
705
+ flaps: NaN,
706
+ spec
655
707
  });
656
708
  } catch {
657
709
  this.missing.add(type);
@@ -813,6 +865,7 @@ var VehicleDetail = class {
813
865
  //#region src/client/renderer.ts
814
866
  const DEFAULT_BUDGET = {
815
867
  detailedCars: 8,
868
+ detailedBoats: 4,
816
869
  skinnedFigures: 32,
817
870
  figureRadius: 120,
818
871
  posesPerFrame: 20
@@ -834,6 +887,13 @@ function createAmbientRenderer(options) {
834
887
  parent: object,
835
888
  ...options.prepare !== void 0 ? { prepare: options.prepare } : {}
836
889
  }) : void 0;
890
+ const boatDetail = options.vehicles !== void 0 ? new VehicleDetail({
891
+ models: options.vehicles,
892
+ parent: object,
893
+ near: 1200,
894
+ far: 1500,
895
+ ...options.prepare !== void 0 ? { prepare: options.prepare } : {}
896
+ }) : void 0;
837
897
  const figures = new AmbientFigures(prepareSync);
838
898
  object.add(figures.group);
839
899
  const aircraft = options.aircraft !== void 0 ? new AmbientAircraft(options.aircraft, object, options.prepare) : void 0;
@@ -843,6 +903,7 @@ function createAmbientRenderer(options) {
843
903
  };
844
904
  const applyBudget = () => {
845
905
  detail?.setMax(budget.detailedCars);
906
+ boatDetail?.setMax(budget.detailedBoats ?? 4);
846
907
  figures.setBudget({
847
908
  skinned: budget.skinnedFigures,
848
909
  radius: budget.figureRadius,
@@ -867,8 +928,10 @@ function createAmbientRenderer(options) {
867
928
  pedestrians: 0,
868
929
  trains: 0,
869
930
  aircraft: 0,
931
+ boats: 0,
870
932
  proxies: 0,
871
933
  detailedCars: 0,
934
+ detailedBoats: 0,
872
935
  figures: 0,
873
936
  drawBatches: 0
874
937
  };
@@ -882,11 +945,13 @@ function createAmbientRenderer(options) {
882
945
  const walkers = [];
883
946
  const planes = [];
884
947
  const candidates = [];
948
+ const boatCandidates = [];
885
949
  const next = {
886
950
  cars: 0,
887
951
  pedestrians: 0,
888
952
  trains: 0,
889
- aircraft: 0
953
+ aircraft: 0,
954
+ boats: 0
890
955
  };
891
956
  for (const [id, a, t] of world.query(AmbientAgent, Transform)) {
892
957
  const agent = a;
@@ -906,6 +971,7 @@ function createAmbientRenderer(options) {
906
971
  }
907
972
  if (agent.kind === "car") next.cars++;
908
973
  else if (agent.kind === "train") next.trains++;
974
+ else if (agent.kind === "boat") next.boats++;
909
975
  else next.aircraft++;
910
976
  if (agent.hidden === true) continue;
911
977
  if (agent.kind === "aircraft") {
@@ -919,6 +985,19 @@ function createAmbientRenderer(options) {
919
985
  });
920
986
  if (aircraft?.modelled(id) === true) continue;
921
987
  }
988
+ if (agent.kind === "boat") {
989
+ const distance = Math.hypot(t.pos[0] - camera[0], t.pos[1] - camera[1], t.pos[2] - camera[2]);
990
+ boatCandidates.push({
991
+ id,
992
+ type: agent.type,
993
+ color: agent.color ?? "#d8d8d4",
994
+ distance
995
+ });
996
+ if (boatDetail?.has(id) === true) {
997
+ boatDetail.update(id, t.pos, t.rot, agent.speed, dt);
998
+ continue;
999
+ }
1000
+ }
922
1001
  if (agent.kind === "car" || agent.kind === "train") {
923
1002
  const distance = Math.max(0, Math.hypot(t.pos[0] - camera[0], t.pos[1] - camera[1], t.pos[2] - camera[2]) - agent.length / 2);
924
1003
  if (distance < 80) candidates.push({
@@ -954,11 +1033,13 @@ function createAmbientRenderer(options) {
954
1033
  if (detail !== void 0 && elapsed - lastSelect >= .2) {
955
1034
  lastSelect = elapsed;
956
1035
  detail.select(candidates, (id) => world.exists(id));
1036
+ boatDetail?.select(boatCandidates, (id) => world.exists(id));
957
1037
  }
958
1038
  counts = {
959
1039
  ...next,
960
1040
  proxies,
961
1041
  detailedCars: detail?.count ?? 0,
1042
+ detailedBoats: boatDetail?.count ?? 0,
962
1043
  figures: figures.count,
963
1044
  drawBatches: batches.meshCount
964
1045
  };
@@ -976,6 +1057,7 @@ function createAmbientRenderer(options) {
976
1057
  stats: () => counts,
977
1058
  dispose() {
978
1059
  detail?.dispose();
1060
+ boatDetail?.dispose();
979
1061
  aircraft?.dispose();
980
1062
  figures.dispose();
981
1063
  batches.dispose();
@@ -984,6 +1066,750 @@ function createAmbientRenderer(options) {
984
1066
  };
985
1067
  }
986
1068
  //#endregion
1069
+ //#region src/client/wildlife-geometry.ts
1070
+ /** Faceted, opaque, bounded animal families. One skinned draw per animal; shared recipe geometry. */
1071
+ /** +Z is the head/front. All geometry is baked in bind pose and skin weights are rigid. */
1072
+ function wildlifeGeometry(species, tier) {
1073
+ const positions = [], normals = [], colors = [], skin = [], weights = [];
1074
+ const joints = [{
1075
+ name: "body",
1076
+ pivot: [
1077
+ 0,
1078
+ 0,
1079
+ 0
1080
+ ]
1081
+ }];
1082
+ const body = species.body, h = body.height, l = body.length, w = body.width;
1083
+ const detail = (name) => body.details.includes(name);
1084
+ const joint = (name, pivot) => {
1085
+ joints.push({
1086
+ name,
1087
+ pivot
1088
+ });
1089
+ return joints.length - 1;
1090
+ };
1091
+ const add = (geometry, matrix, color, bone = 0) => {
1092
+ const source = geometry.index === null ? geometry : geometry.toNonIndexed();
1093
+ source.applyMatrix4(matrix);
1094
+ source.computeVertexNormals();
1095
+ const p = source.getAttribute("position"), n = source.getAttribute("normal");
1096
+ const c = new THREE.Color(color);
1097
+ for (let i = 0; i < p.count; i++) {
1098
+ positions.push(p.getX(i), p.getY(i), p.getZ(i));
1099
+ normals.push(n.getX(i), n.getY(i), n.getZ(i));
1100
+ colors.push(c.r, c.g, c.b);
1101
+ skin.push(bone, 0, 0, 0);
1102
+ weights.push(1, 0, 0, 0);
1103
+ }
1104
+ source.dispose();
1105
+ if (source !== geometry) geometry.dispose();
1106
+ };
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);
1109
+ };
1110
+ const segment = (a, b, r1, r2, color = body.color, bone = 0) => {
1111
+ const start = new THREE.Vector3(...a), end = new THREE.Vector3(...b), delta = end.clone().sub(start);
1112
+ 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);
1113
+ };
1114
+ const wedge = (points, thickness, color, bone) => {
1115
+ const [a, b, c] = points;
1116
+ if (a === void 0 || b === void 0 || c === void 0) return;
1117
+ const vertices = [
1118
+ a,
1119
+ b,
1120
+ c,
1121
+ [
1122
+ a[0],
1123
+ a[1] + thickness,
1124
+ a[2]
1125
+ ],
1126
+ [
1127
+ b[0],
1128
+ b[1] + thickness,
1129
+ b[2]
1130
+ ],
1131
+ [
1132
+ c[0],
1133
+ c[1] + thickness,
1134
+ c[2]
1135
+ ]
1136
+ ];
1137
+ const indices = [
1138
+ 0,
1139
+ 2,
1140
+ 1,
1141
+ 3,
1142
+ 4,
1143
+ 5,
1144
+ 0,
1145
+ 1,
1146
+ 4,
1147
+ 0,
1148
+ 4,
1149
+ 3,
1150
+ 1,
1151
+ 2,
1152
+ 5,
1153
+ 1,
1154
+ 5,
1155
+ 4,
1156
+ 2,
1157
+ 0,
1158
+ 3,
1159
+ 2,
1160
+ 3,
1161
+ 5
1162
+ ];
1163
+ const geometry = new THREE.BufferGeometry();
1164
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(vertices.flat(), 3));
1165
+ geometry.setIndex(indices);
1166
+ add(geometry, new THREE.Matrix4(), color, bone);
1167
+ };
1168
+ const eye = (x, y, z, radius, bone = 0) => {
1169
+ if (tier !== 0) return;
1170
+ ellipsoid([
1171
+ x,
1172
+ y,
1173
+ z
1174
+ ], [
1175
+ radius,
1176
+ radius,
1177
+ radius * .55
1178
+ ], "#272a26", bone);
1179
+ };
1180
+ if (body.family === "bird" || body.family === "insect") {
1181
+ const insect = body.family === "insect";
1182
+ const flying = species.motion === "fly";
1183
+ const y = flying ? 0 : detail("wader") ? h * .66 : h * .48;
1184
+ ellipsoid([
1185
+ 0,
1186
+ y,
1187
+ 0
1188
+ ], [
1189
+ w * .5,
1190
+ h * (detail("wader") ? .19 : .3),
1191
+ l * .38
1192
+ ]);
1193
+ const headY = y + h * (detail("wader") ? .23 : .21), headZ = l * .36;
1194
+ if (detail("wader")) segment([
1195
+ 0,
1196
+ y,
1197
+ l * .2
1198
+ ], [
1199
+ 0,
1200
+ headY,
1201
+ headZ
1202
+ ], w * .16, w * .12);
1203
+ ellipsoid([
1204
+ 0,
1205
+ headY,
1206
+ headZ
1207
+ ], [
1208
+ w * .32,
1209
+ h * .17,
1210
+ l * .17
1211
+ ], body.accent);
1212
+ segment([
1213
+ 0,
1214
+ headY,
1215
+ l * .46
1216
+ ], [
1217
+ 0,
1218
+ headY - h * .035,
1219
+ l * (detail("wader") ? .87 : .66)
1220
+ ], w * .1, w * .012, "#a2966e");
1221
+ eye(-w * .27, headY + h * .035, l * .45, h * .045);
1222
+ eye(w * .27, headY + h * .035, l * .45, h * .045);
1223
+ for (const side of [-1, 1]) {
1224
+ const wing = joint(side < 0 ? "wing.l" : "wing.r", [
1225
+ side * w * .25,
1226
+ y,
1227
+ l * .04
1228
+ ]);
1229
+ if (flying) {
1230
+ const span = l * (detail("raptor") ? 1.65 : insect ? .9 : 1.05);
1231
+ wedge([
1232
+ [
1233
+ side * w * .25,
1234
+ y,
1235
+ l * .25
1236
+ ],
1237
+ [
1238
+ side * span,
1239
+ y - h * .08,
1240
+ -l * .1
1241
+ ],
1242
+ [
1243
+ side * w * .4,
1244
+ y,
1245
+ -l * .42
1246
+ ]
1247
+ ], h * .045, body.accent, wing);
1248
+ if (tier === 0 && !insect) for (let i = 0; i < 3; i++) wedge([
1249
+ [
1250
+ side * span * (.65 + i * .1),
1251
+ y - h * .05,
1252
+ -l * .04
1253
+ ],
1254
+ [
1255
+ side * span * (.82 + i * .1),
1256
+ y - h * .08,
1257
+ -l * (.23 + i * .03)
1258
+ ],
1259
+ [
1260
+ side * span * (.61 + i * .1),
1261
+ y,
1262
+ -l * .22
1263
+ ]
1264
+ ], h * .025, body.color, wing);
1265
+ } else ellipsoid([
1266
+ side * w * .38,
1267
+ y + h * .05,
1268
+ -l * .03
1269
+ ], [
1270
+ w * .16,
1271
+ h * .2,
1272
+ l * .34
1273
+ ], body.accent, wing);
1274
+ if (!flying && species.motion !== "swim") {
1275
+ const leg = joint(side < 0 ? "leg.l" : "leg.r", [
1276
+ side * w * .2,
1277
+ y,
1278
+ 0
1279
+ ]);
1280
+ segment([
1281
+ side * w * .2,
1282
+ y,
1283
+ 0
1284
+ ], [
1285
+ side * w * .2,
1286
+ h * .03,
1287
+ l * .04
1288
+ ], w * .045, w * .03, "#655f4a", leg);
1289
+ segment([
1290
+ side * w * .2,
1291
+ h * .03,
1292
+ l * .04
1293
+ ], [
1294
+ side * w * .2,
1295
+ h * .03,
1296
+ l * .18
1297
+ ], w * .03, w * .025, "#655f4a", leg);
1298
+ }
1299
+ }
1300
+ wedge([
1301
+ [
1302
+ -w * .22,
1303
+ y,
1304
+ -l * .3
1305
+ ],
1306
+ [
1307
+ w * .22,
1308
+ y,
1309
+ -l * .3
1310
+ ],
1311
+ [
1312
+ 0,
1313
+ y - h * .08,
1314
+ -l * (detail("long-tail") ? 1 : .64)
1315
+ ]
1316
+ ], h * .05, body.accent, 0);
1317
+ } else if (body.family === "fish") {
1318
+ ellipsoid([
1319
+ 0,
1320
+ 0,
1321
+ 0
1322
+ ], [
1323
+ w * .5,
1324
+ h * .48,
1325
+ l * .4
1326
+ ]);
1327
+ const tail = joint("tail", [
1328
+ 0,
1329
+ 0,
1330
+ -l * .32
1331
+ ]);
1332
+ ellipsoid([
1333
+ 0,
1334
+ 0,
1335
+ -l * .5
1336
+ ], [
1337
+ w * .08,
1338
+ h * .65,
1339
+ l * .13
1340
+ ], body.accent, tail);
1341
+ for (const side of [-1, 1]) wedge([
1342
+ [
1343
+ side * w * .2,
1344
+ 0,
1345
+ l * .1
1346
+ ],
1347
+ [
1348
+ side * w,
1349
+ -h * .12,
1350
+ -l * .18
1351
+ ],
1352
+ [
1353
+ side * w * .25,
1354
+ 0,
1355
+ -l * .2
1356
+ ]
1357
+ ], h * .04, body.accent, 0);
1358
+ eye(-w * .4, h * .14, l * .25, h * .09);
1359
+ eye(w * .4, h * .14, l * .25, h * .09);
1360
+ } else if (body.family === "reptile") {
1361
+ ellipsoid([
1362
+ 0,
1363
+ h * .48,
1364
+ -l * .05
1365
+ ], [
1366
+ w * .5,
1367
+ h * .28,
1368
+ l * .36
1369
+ ]);
1370
+ const head = joint("head", [
1371
+ 0,
1372
+ h * .44,
1373
+ l * .28
1374
+ ]);
1375
+ ellipsoid([
1376
+ 0,
1377
+ h * .43,
1378
+ l * .39
1379
+ ], [
1380
+ w * .34,
1381
+ h * .2,
1382
+ l * .18
1383
+ ], body.color, head);
1384
+ for (const side of [-1, 1]) {
1385
+ eye(side * w * .29, h * .54, l * .42, h * .045, head);
1386
+ for (const front of [false, true]) {
1387
+ const z = (front ? 1 : -1) * l * .22;
1388
+ const pivot = [
1389
+ side * w * .35,
1390
+ h * .4,
1391
+ z
1392
+ ];
1393
+ const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1394
+ const knee = [
1395
+ side * w * .85,
1396
+ h * .2,
1397
+ z + (front ? -1 : 1) * l * .09
1398
+ ];
1399
+ const foot = [
1400
+ side * w * .98,
1401
+ h * .04,
1402
+ z + (front ? 1 : -1) * l * .1
1403
+ ];
1404
+ segment(pivot, knee, w * .12, w * .08, body.color, leg);
1405
+ segment(knee, foot, w * .08, w * .035, body.color, leg);
1406
+ if (tier < 2) ellipsoid(foot, [
1407
+ w * .12,
1408
+ h * .025,
1409
+ l * .055
1410
+ ], body.accent, leg);
1411
+ }
1412
+ }
1413
+ const tail = joint("tail", [
1414
+ 0,
1415
+ h * .4,
1416
+ -l * .34
1417
+ ]);
1418
+ const bend = [
1419
+ w * .12,
1420
+ h * .14,
1421
+ -l * .77
1422
+ ];
1423
+ segment([
1424
+ 0,
1425
+ h * .4,
1426
+ -l * .34
1427
+ ], bend, w * .22, w * .1, body.color, tail);
1428
+ segment(bend, [
1429
+ w * .32,
1430
+ h * .06,
1431
+ -l * 1.3
1432
+ ], w * .1, w * .005, body.color, tail);
1433
+ } else {
1434
+ const hopper = body.family === "hopper", low = ["rodent", "lagomorph"].includes(body.family), elephant = body.family === "elephant";
1435
+ const torsoY = h * (hopper ? .57 : low ? .5 : .72);
1436
+ const torsoRadius = h * (hopper ? .3 : low ? .31 : elephant ? .32 : .19);
1437
+ ellipsoid([
1438
+ 0,
1439
+ torsoY,
1440
+ -l * .04
1441
+ ], [
1442
+ w * .5,
1443
+ torsoRadius,
1444
+ l * (hopper ? .28 : .38)
1445
+ ]);
1446
+ if (tier === 0) ellipsoid([
1447
+ 0,
1448
+ torsoY - torsoRadius * .48,
1449
+ l * .04
1450
+ ], [
1451
+ w * .39,
1452
+ torsoRadius * .62,
1453
+ l * .29
1454
+ ], elephant ? body.color : body.accent);
1455
+ const neckTop = h * (hopper ? .92 : low ? .61 : elephant ? .9 : detail("long-neck") ? 1.38 : 1.1);
1456
+ const headZ = l * (hopper ? .3 : .47), headR = h * (elephant ? .18 : low ? .23 : .13);
1457
+ segment([
1458
+ 0,
1459
+ torsoY,
1460
+ l * .27
1461
+ ], [
1462
+ 0,
1463
+ neckTop - headR * .4,
1464
+ headZ - l * .07
1465
+ ], w * (elephant ? .4 : low ? .32 : .23), w * (low ? .28 : .17));
1466
+ const head = joint("head", [
1467
+ 0,
1468
+ neckTop - headR * .6,
1469
+ headZ - l * .06
1470
+ ]);
1471
+ ellipsoid([
1472
+ 0,
1473
+ neckTop,
1474
+ headZ
1475
+ ], [
1476
+ w * (elephant ? .4 : low ? .34 : .28),
1477
+ headR,
1478
+ l * (elephant || low ? .16 : .12)
1479
+ ], 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);
1489
+ for (const side of [-1, 1]) {
1490
+ const earHeight = body.family === "lagomorph" ? h * .45 : hopper ? h * .2 : elephant ? h * .25 : low ? h * .075 : h * .11;
1491
+ ellipsoid([
1492
+ side * w * (elephant ? .52 : .22),
1493
+ neckTop + headR * (elephant ? .15 : .8),
1494
+ headZ - l * .04
1495
+ ], [
1496
+ w * (elephant ? .35 : .1),
1497
+ earHeight,
1498
+ l * .035
1499
+ ], elephant ? body.color : body.accent, head);
1500
+ 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
1506
+ ];
1507
+ const tip = [
1508
+ side * w * .42,
1509
+ neckTop + h * .36,
1510
+ headZ - l * .04
1511
+ ];
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);
1524
+ }
1525
+ }
1526
+ for (const front of [false, true]) {
1527
+ const legZ = (front ? 1 : -1) * l * (hopper ? .15 : .25);
1528
+ const pivot = [
1529
+ side * w * .34,
1530
+ torsoY - torsoRadius * .2,
1531
+ legZ
1532
+ ];
1533
+ const leg = joint(`leg.${front ? "front" : "rear"}.${side < 0 ? "l" : "r"}`, pivot);
1534
+ 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
+ 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);
1556
+ }
1557
+ }
1558
+ const tail = joint("tail", [
1559
+ 0,
1560
+ torsoY,
1561
+ -l * .34
1562
+ ]);
1563
+ if (body.family === "rodent" && detail("bushy-tail")) {
1564
+ segment([
1565
+ 0,
1566
+ torsoY,
1567
+ -l * .34
1568
+ ], [
1569
+ 0,
1570
+ h * 1.12,
1571
+ -l * .65
1572
+ ], w * .19, w * .16, body.color, tail);
1573
+ ellipsoid([
1574
+ 0,
1575
+ h * 1,
1576
+ -l * .6
1577
+ ], [
1578
+ w * .36,
1579
+ h * .57,
1580
+ l * .27
1581
+ ], body.color, tail);
1582
+ } else if (detail("long-tail") || detail("bushy-tail") || [
1583
+ "canid",
1584
+ "feline",
1585
+ "elephant"
1586
+ ].includes(body.family)) {
1587
+ const end = [
1588
+ 0,
1589
+ elephant ? h * .12 : hopper ? h * .08 : torsoY * .55,
1590
+ -l * (elephant ? .44 : detail("long-tail") ? 1.1 : .88)
1591
+ ];
1592
+ segment([
1593
+ 0,
1594
+ torsoY,
1595
+ -l * .34
1596
+ ], end, w * (detail("bushy-tail") ? .2 : hopper ? .18 : .06), w * .025, body.color, tail);
1597
+ if (detail("bushy-tail")) ellipsoid([
1598
+ 0,
1599
+ torsoY * .72,
1600
+ -l * .68
1601
+ ], [
1602
+ w * .22,
1603
+ h * .14,
1604
+ l * .2
1605
+ ], body.accent, tail);
1606
+ } else if (body.family !== "rodent") ellipsoid([
1607
+ 0,
1608
+ torsoY,
1609
+ -l * .39
1610
+ ], [
1611
+ w * .1,
1612
+ h * .05,
1613
+ l * .05
1614
+ ], body.accent, tail);
1615
+ if (elephant) {
1616
+ const trunk = joint("trunk", [
1617
+ 0,
1618
+ neckTop - headR * .3,
1619
+ headZ + l * .11
1620
+ ]);
1621
+ segment([
1622
+ 0,
1623
+ neckTop - headR * .3,
1624
+ headZ + l * .11
1625
+ ], [
1626
+ 0,
1627
+ h * .38,
1628
+ headZ + l * .17
1629
+ ], w * .14, w * .09, body.color, trunk);
1630
+ segment([
1631
+ 0,
1632
+ h * .38,
1633
+ headZ + l * .17
1634
+ ], [
1635
+ 0,
1636
+ h * .16,
1637
+ headZ + l * .22
1638
+ ], w * .09, w * .04, body.color, trunk);
1639
+ }
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
+ }
1653
+ const geometry = new THREE.BufferGeometry();
1654
+ geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
1655
+ geometry.setAttribute("normal", new THREE.Float32BufferAttribute(normals, 3));
1656
+ geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
1657
+ geometry.setAttribute("skinIndex", new THREE.Uint16BufferAttribute(skin, 4));
1658
+ geometry.setAttribute("skinWeight", new THREE.Float32BufferAttribute(weights, 4));
1659
+ geometry.computeBoundingBox();
1660
+ geometry.computeBoundingSphere();
1661
+ return {
1662
+ geometry,
1663
+ joints,
1664
+ triangles: positions.length / 9
1665
+ };
1666
+ }
1667
+ //#endregion
1668
+ //#region src/client/wildlife.ts
1669
+ /** One opaque skinned draw per visible animal. Geometry is shared and released on disposal. */
1670
+ var WildlifeRenderer = class {
1671
+ prepare;
1672
+ group = new THREE.Group();
1673
+ material = new THREE.MeshStandardMaterial({
1674
+ vertexColors: true,
1675
+ roughness: .85,
1676
+ metalness: 0,
1677
+ flatShading: true
1678
+ });
1679
+ cache = /* @__PURE__ */ new Map();
1680
+ active = /* @__PURE__ */ new Map();
1681
+ frame = 0;
1682
+ cursor = 0;
1683
+ budget = {
1684
+ animals: 32,
1685
+ radius: 180,
1686
+ posesPerFrame: 16
1687
+ };
1688
+ constructor(prepare) {
1689
+ this.prepare = prepare;
1690
+ this.group.name = "ambient:wildlife";
1691
+ }
1692
+ setBudget(budget) {
1693
+ this.budget = {
1694
+ ...this.budget,
1695
+ ...budget
1696
+ };
1697
+ }
1698
+ get count() {
1699
+ return this.active.size;
1700
+ }
1701
+ get triangles() {
1702
+ let count = 0;
1703
+ for (const animal of this.active.values()) count += animal.geometry.triangles;
1704
+ return count;
1705
+ }
1706
+ remove(id, animal) {
1707
+ animal.mesh.skeleton.dispose();
1708
+ animal.root.removeFromParent();
1709
+ this.active.delete(id);
1710
+ }
1711
+ update(agents, species, camera, seconds, appearance) {
1712
+ this.frame++;
1713
+ const visible = agents.map((agent) => ({
1714
+ agent,
1715
+ distance: Math.hypot(agent.position[0] - camera[0], agent.position[1] - camera[1], agent.position[2] - camera[2])
1716
+ })).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);
1717
+ const pose = [];
1718
+ for (const { agent, distance } of visible) {
1719
+ const base = species.get(agent.species);
1720
+ const recipe = base === void 0 ? void 0 : appearance?.(agent, base) ?? base;
1721
+ if (recipe === void 0 || recipe.body.family === "insect" && distance > 10 || recipe.body.height < .1 && distance > 25) continue;
1722
+ const tier = distance < 35 ? 0 : distance < 100 ? 1 : 2;
1723
+ const key = `${recipe.id}@${recipe.version}/${recipe.body.color}/${tier}`;
1724
+ let animal = this.active.get(agent.id);
1725
+ if (animal?.key !== key) {
1726
+ if (animal !== void 0) this.remove(agent.id, animal);
1727
+ let geometry = this.cache.get(key);
1728
+ if (geometry === void 0) {
1729
+ geometry = wildlifeGeometry(recipe, tier);
1730
+ this.cache.set(key, geometry);
1731
+ }
1732
+ const mesh = new THREE.SkinnedMesh(geometry.geometry, this.material);
1733
+ const bones = geometry.joints.map((joint) => {
1734
+ const bone = new THREE.Bone();
1735
+ bone.name = joint.name;
1736
+ bone.position.set(...joint.pivot);
1737
+ mesh.add(bone);
1738
+ return bone;
1739
+ });
1740
+ mesh.bind(new THREE.Skeleton(bones));
1741
+ mesh.castShadow = tier === 0;
1742
+ mesh.receiveShadow = true;
1743
+ const root = new THREE.Group();
1744
+ root.add(mesh);
1745
+ root.userData.walkIgnore = true;
1746
+ root.traverse((object) => {
1747
+ object.userData.walkIgnore = true;
1748
+ });
1749
+ this.group.add(root);
1750
+ this.prepare?.(root);
1751
+ animal = {
1752
+ root,
1753
+ mesh,
1754
+ bones,
1755
+ geometry,
1756
+ key,
1757
+ seen: this.frame
1758
+ };
1759
+ this.active.set(agent.id, animal);
1760
+ }
1761
+ animal.seen = this.frame;
1762
+ animal.root.position.set(...agent.position);
1763
+ animal.root.rotation.y = agent.heading;
1764
+ pose.push({
1765
+ animal,
1766
+ agent,
1767
+ species: recipe
1768
+ });
1769
+ }
1770
+ for (const [id, animal] of this.active) if (animal.seen !== this.frame) this.remove(id, animal);
1771
+ const count = Math.min(pose.length, this.budget.posesPerFrame);
1772
+ const near = Math.ceil(count / 2), remaining = pose.length - near;
1773
+ for (let i = 0; i < count; i++) {
1774
+ const entry = pose[i < near ? i : near + (this.cursor + i - near) % remaining];
1775
+ if (entry === void 0) continue;
1776
+ const { animal, agent, species: recipe } = entry;
1777
+ const gait = agent.phase * Math.PI * 2;
1778
+ const flying = recipe.motion === "fly";
1779
+ const flapping = Math.sin(seconds * (recipe.body.family === "insect" ? 45 : 6 / Math.max(.3, recipe.body.length)) + agent.seed % 100);
1780
+ animal.root.position.y = agent.position[1] + (recipe.motion === "hop" && agent.speed > .01 ? Math.abs(Math.sin(gait)) * recipe.body.height * .12 : 0);
1781
+ for (let index = 0; index < animal.bones.length; index++) {
1782
+ const bone = animal.bones[index];
1783
+ if (bone === void 0) continue;
1784
+ const name = animal.geometry.joints[index]?.name ?? "";
1785
+ bone.rotation.set(0, 0, 0);
1786
+ if (name.startsWith("leg.") && agent.speed > .01) {
1787
+ const reverse = name.includes("rear") !== name.endsWith(".r");
1788
+ bone.rotation.x = Math.sin(gait + (recipe.motion === "hop" ? 0 : reverse ? Math.PI : 0)) * (recipe.motion === "hop" ? .22 : .27);
1789
+ } else if (name.startsWith("wing.") && flying) bone.rotation.z = flapping * (name.endsWith(".l") ? -.55 : .55);
1790
+ else if (name === "tail") bone.rotation.y = Math.sin(recipe.motion === "swim" ? seconds * 5 : gait) * .15;
1791
+ else if (name === "head" && agent.speed < .01) bone.rotation.x = Math.sin(seconds * .4 + agent.seed % 30) * .08;
1792
+ else if (name === "trunk") bone.rotation.x = Math.sin(seconds * .7) * .06;
1793
+ }
1794
+ }
1795
+ if (remaining > 0) this.cursor = (this.cursor + count - near) % remaining;
1796
+ if (this.cache.size > 96) {
1797
+ const used = new Set([...this.active.values()].map((animal) => animal.key));
1798
+ for (const [key, geometry] of this.cache) if (!used.has(key)) {
1799
+ geometry.geometry.dispose();
1800
+ this.cache.delete(key);
1801
+ }
1802
+ }
1803
+ }
1804
+ dispose() {
1805
+ for (const [id, animal] of this.active) this.remove(id, animal);
1806
+ for (const geometry of this.cache.values()) geometry.geometry.dispose();
1807
+ this.cache.clear();
1808
+ this.material.dispose();
1809
+ this.group.removeFromParent();
1810
+ }
1811
+ };
1812
+ //#endregion
987
1813
  //#region src/client.ts
988
1814
  /**
989
1815
  * @bendyline/molen-ambient/client — rendering for ambient NPCs: instanced vehicle proxies with
@@ -992,4 +1818,4 @@ function createAmbientRenderer(options) {
992
1818
  */
993
1819
  registerAmbientSchema();
994
1820
  //#endregion
995
- export { AmbientFigures, ProxyBatches, ambientVehicleKind, createAmbientRenderer, proxyGeometry, proxyHeight, proxyShapeFor };
1821
+ export { AmbientFigures, ProxyBatches, WildlifeRenderer, ambientVehicleKind, createAmbientRenderer, proxyGeometry, proxyHeight, proxyShapeFor, wildlifeGeometry };