@adatechnology/game-vehicles-three 0.10.1 → 0.11.1

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/README.md CHANGED
@@ -19,6 +19,13 @@ car.triangles // 19286 · car.drawCalls // 6 · car.damageables // metal, glass,
19
19
  - The colour comes from `color` or from `seed` through the palette of the vehicle's group (`colorFromSeed`).
20
20
  - Tested against the team's page on three r160 (triangles, draw calls, size, per-class vertices, damageable boxes) and against `DIMS` for the 69 vehicles.
21
21
 
22
+ ## Collision hulls
23
+
24
+ `createVehicleColliders(THREE, vehicle, frame, { hulls: true, maxHulls })` gives the engine the convex hulls of a vehicle (`hulls()`) next to the boxes of `pieces()`. The division of a car into hulls is the same for every car of a model, so it is made once per model:
25
+
26
+ - `planCache: createHullPlanCache()` shares the hulls of the intact cars of a model (key: the vehicle key, its seed key and its build state, or `planKey`); the second car of a model is given the same hull objects in under 2 ms. A dented car neither gives nor takes. `cache.dispose()` lets everything go.
27
+ - `workers` (a `WorkerCoordinator` of `@adatechnology/game-workers`) sends the division of the first car of a model to the `vehicles.hull-plan` kind (`@adatechnology/game-vehicles-three/worker-kinds`, three-free; list `hullPlanKind` in the worker file's `installWorkerHost` and `warm([hullPlanKind])` at load). Until it is back `hulls()` answers an empty list and `isHullPending()` is true: the car keeps the boxes of `pieces()`. The call after the answer installs the plan, so call `hulls()` in the fixed step. Without `workers` the division is made in the call, with the same bytes.
28
+
22
29
  ## Test area
23
30
 
24
31
  `createVehiclesTestModule({ locale, document?, three?, vehicles?, effects? })` is a `TestableModule` for the shared test room (`room.loadModule`, or `loadVehiclesTestModule(room)`, which learns the room's three.js to compute the door, hood and trunk clips). It lays the eight vehicles of the Frota test, each with its own `DestructibleImpl`, and brings 13 scenarios: the eleven of the team's test page, literal (`vehicles_9mm_windshield`, `vehicles_shotgun_door`, `vehicles_ram_front`, `vehicles_engine_fire`, `vehicles_fuel_tank`, `vehicles_unbolt_wheel`, `vehicles_tear_bumper`, `vehicles_break_mirror`, `vehicles_kill_battery`, `vehicles_rollover`, `vehicles_melt`; the page ids are in `PAGE_SCENARIO_ALIASES`), plus `vehicles_drive_arcade` and `vehicles_drive_semi` for `@adatechnology/game-vehicles-driving`.
@@ -28,3 +35,13 @@ The bench sets the quality (full or light mode), the paint by seed and palette,
28
35
  ## Rain under the roof
29
36
 
30
37
  `createVehicleRainCovers()` keeps the host's rain (`ModuleHost.rainCover`) told what each vehicle in the scene roofs over: `track(id, destructible, frame)`, then `sync(host.rainCover, dt)` every frame and `clear()` when the module leaves (the vehicles test area does it). The cover comes from `createVehicleRainCover` of `game-vehicles`: the cabin box and roof of a car, the body of a bus or truck; an open pickup bed, bikes, boats, planes, helicopters and the tractor roof nothing. A torn-off roof, a broken windshield or pane and a through-hole of 30 cm or more are openings; a moving car only turns its rectangle (the damage state is read every 0.4 s) and the version the far curtain caches grows every metre or so.
38
+
39
+ ## Crash parts on the physics engine (`createCrashParts`, spec 008 T6.9)
40
+
41
+ `createCrashParts({ THREE, world, vehicle, frame, colliders, mass, onPartLost? })` builds a mutable compound of the car's hulls (`colliders.hulls()`, each child tagged with the id of its part) and keeps it in step with the drawing. `shape()` gives the chassis shape; `bind(chassis)` sets the kit's mass, inertia and centre of mass and listens to the contacts; call `step()` after every physics step.
42
+
43
+ - A part that comes off (`vehicle.disassemble`, or a blow) leaves the compound and becomes a body with its own hulls, mass (`PartDef.mass`), box inertia and the velocity of its point on the chassis; the chassis gives up that mass (`massOf(chassis) + parts = the kit's mass`). Nothing is flung (`flying` stays empty), and the part is not born inside anything (pushed out of fixed geometry, at most 3 times).
44
+ - Level-2 damage: each step the strongest contact row of every hull tag becomes a `crashVehicle` (point, direction, approach speed, both masses, width of the part's box, `source` = part id).
45
+ - At rest (chassis under 0.05 m/s for 30 steps) the hulls of what was dented are made again and the changed ones go to the engine with `setChildShape`. A hull that touches something may only move inward, and outward by 2 mm at most (`holdWithin`), until the contact ends.
46
+ - `stats()` reports `released`, `pushedOut`, `settles`, `remade`, `held`, `unplaced`, `blows`, `events`, `hitParts` and `children`; `dispose()` removes every part body. Wheels and lamps without a hull use the drawn box; the interior is spec 009.
47
+ - The hull cache by model (`createHullPlanCache`) and the worker kind `vehicles.hull-plan` are described above; a dented car leaves the cache.
@@ -332,6 +332,9 @@ function markDamageables(c, damageables) {
332
332
  }
333
333
  }
334
334
 
335
+ // src/colliders.ts
336
+ import { hullOf, hullPlanKind, leavesOf, newHullPlan, planHulls, unitsOf } from "@adatechnology/game-vehicles";
337
+
335
338
  // src/collider-fit.ts
336
339
  import { fitOrientedBox, fitOrientedBoxes, hugs } from "@adatechnology/game-shapes";
337
340
  var FIT = 0.01;
@@ -467,11 +470,14 @@ var colliderOf = (id, key, soup) => {
467
470
  // src/colliders.ts
468
471
  var SHELL_ROLES = /* @__PURE__ */ new Set(["panel", "fender", "roof", "frame", "glass", "pillar", "door", "lid", "hood", "bumper"]);
469
472
  var GONE = /* @__PURE__ */ new Set(["broken", "melted", "ashes"]);
473
+ var SOLID_ROLES = /* @__PURE__ */ new Set(["light", "mirror", "engine", "radiator", "fuel_tank", "oil_pan", "battery", "alternator", "gearbox", "steering", "hitch"]);
474
+ var WHEEL_ROLES = /* @__PURE__ */ new Set(["rim", "tyre", "brakes", "shocks"]);
470
475
  var MIN_AREA = 1e-9;
471
476
  var CHUNK = 0.8;
472
477
  var CELL_BIAS2 = 512;
473
478
  var CELL_SPAN2 = 1024;
474
479
  var PART_SPAN = 2 ** 30;
480
+ var JOB_NUMBERS_PER_CALL = 2e5;
475
481
  var DEFAULT_BUDGET = Infinity;
476
482
  var isDrawn = (mesh, stop) => {
477
483
  if (!mesh.isMesh || !mesh.geometry || mesh.userData.ghost || mesh.userData.helper) return false;
@@ -499,6 +505,18 @@ var isNotDrawn = (positions, [a, b, c]) => {
499
505
  const area = Math.hypot(uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx);
500
506
  return !(area > MIN_AREA);
501
507
  };
508
+ var float64Of = (numbers) => {
509
+ const out = new Float64Array(numbers.length);
510
+ for (let i = 0; i < out.length; i++) out[i] = numbers[i];
511
+ return out;
512
+ };
513
+ var uint32Of = (numbers) => {
514
+ const out = new Uint32Array(numbers.length);
515
+ for (let i = 0; i < out.length; i++) out[i] = numbers[i];
516
+ return out;
517
+ };
518
+ var pieceOfJob = (key, piece) => ({ key, vertices: float64Of(piece.soup.vertices), indices: uint32Of(piece.soup.indices) });
519
+ var signatureOfSolid = (members) => members.map((piece) => `${piece.key}.${piece.version}`).join(",");
502
520
  var sameMatrix = (a, b) => {
503
521
  for (let i = 0; i < 16; i++) if (a[i] !== b[i]) return false;
504
522
  return true;
@@ -508,6 +526,8 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
508
526
  const pieces = /* @__PURE__ */ new Map();
509
527
  const partIndex = new Map(vehicle.model.parts.map((part, index) => [part.id, index]));
510
528
  const isShown2 = new Map(vehicle.model.parts.map((part) => [part.id, part.visual]));
529
+ const isSolid = new Map(vehicle.model.parts.map((part) => [part.id, SOLID_ROLES.has(part.role)]));
530
+ const isWheel = new Map(vehicle.model.parts.map((part) => [part.id, WHEEL_ROLES.has(part.role)]));
511
531
  const shell = vehicle.model.parts.filter((part) => SHELL_ROLES.has(part.role));
512
532
  const toFrame = new THREE.Matrix4();
513
533
  const toVehicle = new THREE.Matrix4();
@@ -515,6 +535,15 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
515
535
  const placed = new THREE.Vector3();
516
536
  const matrix = new THREE.Matrix4();
517
537
  let pending = false;
538
+ let shown = [];
539
+ let hullSources = /* @__PURE__ */ new Map();
540
+ let plan = newHullPlan();
541
+ let isDivided = false;
542
+ let hullGroups = /* @__PURE__ */ new Map();
543
+ let shared;
544
+ let flight;
545
+ let isDamaged = false;
546
+ let building;
518
547
  const release = (entry) => {
519
548
  for (const key of entry.chunks.keys()) {
520
549
  const piece = pieces.get(key);
@@ -561,7 +590,7 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
561
590
  let piece = pieces.get(key);
562
591
  if (!piece) {
563
592
  const part = vehicle.model.parts[Math.floor(key / PART_SPAN)].id;
564
- pieces.set(key, piece = { key, part, entries: /* @__PURE__ */ new Set(), dirty: true, collider: null });
593
+ pieces.set(key, piece = { key, part, entries: /* @__PURE__ */ new Set(), dirty: true, collider: null, soup: null, version: 0, hulls: [], hullVersion: -1 });
565
594
  }
566
595
  piece.entries.add(entry);
567
596
  piece.dirty = true;
@@ -575,6 +604,7 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
575
604
  const changed = new Uint8Array(array.length / 3);
576
605
  for (let i = 0; i < array.length; i++) {
577
606
  if (array[i] === entry.snapshot[i]) continue;
607
+ isDamaged = true;
578
608
  changed[Math.floor(i / 3)] = 1;
579
609
  entry.snapshot[i] = array[i];
580
610
  }
@@ -624,6 +654,8 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
624
654
  }
625
655
  }
626
656
  piece.collider = colliderOf(piece.part, `${piece.part}#${piece.key % PART_SPAN}`, soup);
657
+ piece.soup = options.hulls ? soup : null;
658
+ piece.version++;
627
659
  piece.dirty = false;
628
660
  };
629
661
  const make = () => {
@@ -635,7 +667,9 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
635
667
  const state = new Map(vehicle.state().parts.map((part) => [part.id, part]));
636
668
  const isGone = (id) => {
637
669
  const part = state.get(id);
638
- return part !== void 0 && (GONE.has(part.status) || part.group !== 0);
670
+ const isOff = part !== void 0 && (GONE.has(part.status) || part.group !== 0);
671
+ if (isOff) isDamaged = true;
672
+ return isOff;
639
673
  };
640
674
  const budget = { left: options.budget ?? DEFAULT_BUDGET };
641
675
  pending = false;
@@ -662,6 +696,7 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
662
696
  }
663
697
  for (const [mesh, owner] of live) read(mesh, owner, budget);
664
698
  const out = [];
699
+ shown = [];
665
700
  for (const [key, piece] of pieces) {
666
701
  if (piece.entries.size === 0) {
667
702
  pieces.delete(key);
@@ -671,13 +706,190 @@ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
671
706
  if (budget.left <= 0) pending = true;
672
707
  else rebuild(piece, budget);
673
708
  }
674
- if (piece.collider && !(isGone(piece.part) && isShown2.get(piece.part))) out.push(piece.collider);
709
+ if (piece.collider && !(isGone(piece.part) && isShown2.get(piece.part))) {
710
+ out.push(piece.collider);
711
+ shown.push(piece);
712
+ }
713
+ }
714
+ return out;
715
+ };
716
+ const nameOf = (piece) => `${piece.part}#${piece.key % PART_SPAN}`;
717
+ const hullsOfPiece = (piece) => {
718
+ if (piece.hullVersion === piece.version) return piece.hulls;
719
+ const made = [];
720
+ for (const unit of unitsOf(nameOf(piece), piece.soup, plan.taken.get(nameOf(piece)))) {
721
+ for (const leaf of leavesOf(unit.key, unit.soup, plan.cuts.get(unit.key) ?? null)) {
722
+ const points = hullOf(leaf.soup);
723
+ if (points) made.push({ hull: { id: piece.part, key: leaf.key, points: new Float32Array(points) }, drawn: leaf.soup.vertices });
724
+ }
725
+ }
726
+ piece.hulls = made;
727
+ piece.hullVersion = piece.version;
728
+ return piece.hulls;
729
+ };
730
+ const jobOf = (thin, solids) => ({
731
+ pieces: thin.map((piece) => pieceOfJob(nameOf(piece), piece)),
732
+ solids: [...solids].flatMap(([part, members]) => members.map((piece) => pieceOfJob(part, piece))),
733
+ maxHulls: (options.maxHulls ?? Infinity) - solids.size
734
+ });
735
+ const signatureOf = (thin, solids) => `${options.maxHulls ?? Infinity}|${thin.map((piece) => `${nameOf(piece)}:${piece.soup.vertices.length}`).join(",")}|${[...solids].map(([part, members]) => `${part}:${members.map((piece) => `${piece.key}.${piece.soup.vertices.length}`).join("+")}`).join(",")}`;
736
+ const modelKeyOf = () => {
737
+ if (options.planKey) return options.planKey;
738
+ const context = vehicle.vehicle;
739
+ return context.key === void 0 ? void 0 : `${context.key}|${context.seedKey ?? context.key}|${context.state ?? ""}`;
740
+ };
741
+ const install = (result, thin, solids, signature, modelKey) => {
742
+ const owners = new Map(thin.map((piece) => [nameOf(piece), piece]));
743
+ const made = /* @__PURE__ */ new Map();
744
+ for (const leaf of result.leaves) {
745
+ const name = leaf.unit.split("~")[0];
746
+ const owner = owners.get(name);
747
+ if (!owner) continue;
748
+ const list = made.get(name) ?? [];
749
+ list.push({ hull: { id: owner.part, key: leaf.path ? `${leaf.unit}:${leaf.path}` : leaf.unit, points: leaf.points }, drawn: leaf.vertices });
750
+ made.set(name, list);
751
+ }
752
+ plan = { cuts: new Map(result.cuts), taken: new Map(result.taken.map(([unit, numbers]) => [unit, new Set(numbers)])) };
753
+ for (const piece of thin) {
754
+ piece.hulls = made.get(nameOf(piece)) ?? [];
755
+ piece.hullVersion = piece.version;
756
+ }
757
+ for (const solid of result.solids) {
758
+ const members = solids.get(solid.key);
759
+ if (members) hullGroups.set(solid.key, { signature: signatureOfSolid(members), hulls: solid.points.length > 0 ? [{ id: solid.key, key: solid.key, points: solid.points }] : [] });
760
+ }
761
+ isDivided = true;
762
+ if (modelKey && options.planCache && !isDamaged) {
763
+ shared = { signature, plan, pieces: made, solids: new Map(hullGroups) };
764
+ options.planCache.set(modelKey, shared);
765
+ }
766
+ };
767
+ const adopt = (from, thin) => {
768
+ plan = from.plan;
769
+ for (const piece of thin) {
770
+ piece.hulls = from.pieces.get(nameOf(piece)) ?? [];
771
+ piece.hullVersion = piece.version;
772
+ }
773
+ hullGroups = new Map(from.solids);
774
+ shared = from;
775
+ isDivided = true;
776
+ options.planCache.noteReused();
777
+ };
778
+ const divide = (thin, solids) => {
779
+ const signature = signatureOf(thin, solids);
780
+ const modelKey = modelKeyOf();
781
+ const cache = options.planCache;
782
+ if (!flight) {
783
+ const known2 = !isDamaged && modelKey && cache ? cache.get(modelKey) : void 0;
784
+ if (known2 && known2.signature === signature) {
785
+ adopt(known2, thin);
786
+ building = void 0;
787
+ return true;
788
+ }
789
+ if (!options.workers) {
790
+ cache?.noteDivided();
791
+ install(planHulls(jobOf(thin, solids)), thin, solids, signature, modelKey);
792
+ return true;
793
+ }
794
+ const going = !isDamaged && modelKey ? cache?.flight(modelKey) : void 0;
795
+ const isGoing = going !== void 0 && going.signature === signature;
796
+ if (!isGoing && !hasJobBuilt(thin, solids, signature)) return false;
797
+ const promise = isGoing ? going.promise : options.workers.run(hullPlanKind, { pieces: building.pieces, solids: building.solids, maxHulls: (options.maxHulls ?? Infinity) - solids.size });
798
+ if (!isGoing) {
799
+ cache?.noteDivided();
800
+ if (modelKey && !isDamaged) cache?.setFlight(modelKey, { signature, promise });
801
+ }
802
+ building = void 0;
803
+ const mine = { signature, job: () => jobOf(thin, solids), failed: false };
804
+ flight = mine;
805
+ promise.then((result) => {
806
+ mine.result = result;
807
+ }, () => {
808
+ mine.failed = true;
809
+ }).finally(() => {
810
+ if (modelKey && cache?.flight(modelKey)?.promise === promise) cache.clearFlight(modelKey);
811
+ });
812
+ }
813
+ if (!flight.result && !flight.failed) return false;
814
+ const arrived = flight;
815
+ flight = void 0;
816
+ if (arrived.signature !== signature) return divide(thin, solids);
817
+ const known = !isDamaged && modelKey && cache ? cache.get(modelKey) : void 0;
818
+ if (arrived.failed && known && known.signature === signature) adopt(known, thin);
819
+ else install(arrived.result ?? planHulls(arrived.job()), thin, solids, signature, modelKey);
820
+ return true;
821
+ };
822
+ const hasJobBuilt = (thin, solids, signature) => {
823
+ if (!building || building.signature !== signature) building = { signature, pieces: [], solids: [] };
824
+ const solidPieces = [...solids].flatMap(([part, members]) => members.map((piece) => [part, piece]));
825
+ let room = JOB_NUMBERS_PER_CALL;
826
+ while (building.pieces.length < thin.length && room > 0) {
827
+ const piece = thin[building.pieces.length];
828
+ building.pieces.push(pieceOfJob(nameOf(piece), piece));
829
+ room -= piece.soup.vertices.length + piece.soup.indices.length;
830
+ }
831
+ while (building.pieces.length === thin.length && building.solids.length < solidPieces.length && room > 0) {
832
+ const [part, piece] = solidPieces[building.solids.length];
833
+ building.solids.push(pieceOfJob(part, piece));
834
+ room -= piece.soup.vertices.length + piece.soup.indices.length;
835
+ }
836
+ return building.pieces.length === thin.length && building.solids.length === solidPieces.length;
837
+ };
838
+ const hullOfSolid = (part, members) => {
839
+ const signature = signatureOfSolid(members);
840
+ let made = hullGroups.get(part);
841
+ if (!made || made.signature !== signature) {
842
+ const vertices = [];
843
+ const indices = [];
844
+ for (const piece of members) {
845
+ const offset = vertices.length / 3;
846
+ for (const v of piece.soup.vertices) vertices.push(v);
847
+ for (const i of piece.soup.indices) indices.push(i + offset);
848
+ }
849
+ const points = hullOf({ vertices, indices });
850
+ made = { signature, hulls: points ? [{ id: part, key: part, points: new Float32Array(points) }] : [] };
851
+ hullGroups.set(part, made);
852
+ }
853
+ return made.hulls;
854
+ };
855
+ const hulls = () => {
856
+ if (!options.hulls) throw new Error("createVehicleColliders: hulls() needs options.hulls");
857
+ make();
858
+ hullSources = /* @__PURE__ */ new Map();
859
+ const out = [];
860
+ const solids = /* @__PURE__ */ new Map();
861
+ const thin = [];
862
+ for (const piece of shown) {
863
+ if (!piece.soup || isWheel.get(piece.part)) continue;
864
+ if (!isSolid.get(piece.part)) {
865
+ thin.push(piece);
866
+ continue;
867
+ }
868
+ const list = solids.get(piece.part);
869
+ if (list) list.push(piece);
870
+ else solids.set(piece.part, [piece]);
871
+ }
872
+ if (!isDivided && !pending && !divide(thin, solids)) return out;
873
+ for (const piece of thin) {
874
+ for (const { hull, drawn } of hullsOfPiece(piece)) {
875
+ out.push(hull);
876
+ hullSources.set(hull, [drawn]);
877
+ }
878
+ }
879
+ for (const id of hullGroups.keys()) if (!solids.has(id)) hullGroups.delete(id);
880
+ for (const [part, members] of solids) {
881
+ for (const hull of hullOfSolid(part, members)) {
882
+ out.push(hull);
883
+ hullSources.set(hull, members.map((piece) => piece.soup.vertices));
884
+ }
675
885
  }
676
886
  return out;
677
887
  };
678
- return { pieces: make, isPending: () => pending };
888
+ const drawnPointsOf = (hull) => (hullSources.get(hull) ?? []).flatMap((vertices) => Array.from(vertices));
889
+ return { pieces: make, isPending: () => pending, hulls, isHullPending: () => flight !== void 0 || building !== void 0, drawnPointsOf };
679
890
  };
680
891
  var vehicleColliders = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame).pieces();
892
+ var vehicleHulls = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame, { hulls: true }).hulls();
681
893
 
682
894
  // src/vehicle-lod.ts
683
895
  import { DEFERRED_QUEUE_ID, markKitOrigin as markKitOrigin2 } from "@adatechnology/game-contracts";
@@ -4461,6 +4673,7 @@ export {
4461
4673
  createVehicleSound,
4462
4674
  createVehicleColliders,
4463
4675
  vehicleColliders,
4676
+ vehicleHulls,
4464
4677
  VEHICLE_LOD_DISTANCE,
4465
4678
  createVehicleLodQueue,
4466
4679
  createVehicleLod,
@@ -4488,4 +4701,4 @@ export {
4488
4701
  createProvingModule,
4489
4702
  loadProvingModule
4490
4703
  };
4491
- //# sourceMappingURL=chunk-5T52WAQS.js.map
4704
+ //# sourceMappingURL=chunk-TZ23KYPZ.js.map