@adatechnology/game-vehicles-three 0.9.9 → 0.9.10

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.
@@ -1,5 +1,5 @@
1
1
  // src/test-area/module.ts
2
- import { DEFERRED_PRIORITY, DEFERRED_QUEUE_ID, footprintBlocker } from "@adatechnology/game-contracts";
2
+ import { DEFERRED_PRIORITY, DEFERRED_QUEUE_ID as DEFERRED_QUEUE_ID2, footprintBlocker } from "@adatechnology/game-contracts";
3
3
  import { discardObject3D } from "@adatechnology/game-shapes-three";
4
4
  import { buildVehicleItem, crackedGlassMaterial, createCreaseQueue, createVehicleDestructible as createVehicleDestructible2, createVehicleKit } from "@adatechnology/game-vehicles";
5
5
  import { isRenderPools } from "@adatechnology/game-render-three";
@@ -332,12 +332,360 @@ function markDamageables(c, damageables) {
332
332
  }
333
333
  }
334
334
 
335
+ // src/collider-fit.ts
336
+ import { fitOrientedBox, fitOrientedBoxes, hugs } from "@adatechnology/game-shapes";
337
+ var FIT = 0.01;
338
+ var MIN_HALF = 5e-3;
339
+ var COARSEN_ABOVE = 60;
340
+ var COARSE_CELL = 8e-3;
341
+ var COARSE_MIN_TRIANGLES = 12;
342
+ var IDENTITY_AXES = [1, 0, 0, 0, 1, 0, 0, 0, 1];
343
+ var CELL_BIAS = 4096;
344
+ var CELL_SPAN = 8192;
345
+ var soupOf = () => ({ vertices: [], indices: [], placed: /* @__PURE__ */ new Map() });
346
+ var addTriangle = (soup, source, positions, corners, matrix) => {
347
+ let placed = soup.placed.get(source);
348
+ if (!placed) soup.placed.set(source, placed = /* @__PURE__ */ new Map());
349
+ for (const corner of corners) {
350
+ let at = placed.get(corner);
351
+ if (at === void 0) {
352
+ const x = positions[corner * 3], y = positions[corner * 3 + 1], z = positions[corner * 3 + 2];
353
+ at = soup.vertices.length / 3;
354
+ placed.set(corner, at);
355
+ soup.vertices.push(
356
+ matrix[0] * x + matrix[4] * y + matrix[8] * z + matrix[12],
357
+ matrix[1] * x + matrix[5] * y + matrix[9] * z + matrix[13],
358
+ matrix[2] * x + matrix[6] * y + matrix[10] * z + matrix[14]
359
+ );
360
+ }
361
+ soup.indices.push(at);
362
+ }
363
+ };
364
+ var cellKey = (x, y, z, cell) => ((Math.floor(x / cell) + CELL_BIAS) * CELL_SPAN + (Math.floor(y / cell) + CELL_BIAS)) * CELL_SPAN + (Math.floor(z / cell) + CELL_BIAS);
365
+ var coarsen = (soup) => {
366
+ const count2 = soup.vertices.length / 3;
367
+ const cells = /* @__PURE__ */ new Map();
368
+ const sums = [];
369
+ const counts = [];
370
+ const to = new Uint32Array(count2);
371
+ for (let i = 0; i < count2; i++) {
372
+ const x = soup.vertices[i * 3], y = soup.vertices[i * 3 + 1], z = soup.vertices[i * 3 + 2];
373
+ const key = cellKey(x, y, z, COARSE_CELL);
374
+ let at = cells.get(key);
375
+ if (at === void 0) {
376
+ at = counts.length;
377
+ cells.set(key, at);
378
+ counts.push(0);
379
+ sums.push(0, 0, 0);
380
+ }
381
+ to[i] = at;
382
+ counts[at] = counts[at] + 1;
383
+ sums[at * 3] = sums[at * 3] + x;
384
+ sums[at * 3 + 1] = sums[at * 3 + 1] + y;
385
+ sums[at * 3 + 2] = sums[at * 3 + 2] + z;
386
+ }
387
+ const best = new Float64Array(counts.length).fill(Infinity);
388
+ const vertices = new Array(counts.length * 3).fill(0);
389
+ for (let i = 0; i < count2; i++) {
390
+ const at = to[i];
391
+ const n = counts[at];
392
+ const x = soup.vertices[i * 3], y = soup.vertices[i * 3 + 1], z = soup.vertices[i * 3 + 2];
393
+ const d = (x - sums[at * 3] / n) ** 2 + (y - sums[at * 3 + 1] / n) ** 2 + (z - sums[at * 3 + 2] / n) ** 2;
394
+ if (d >= best[at]) continue;
395
+ best[at] = d;
396
+ vertices[at * 3] = x;
397
+ vertices[at * 3 + 1] = y;
398
+ vertices[at * 3 + 2] = z;
399
+ }
400
+ const indices = [];
401
+ const seen = /* @__PURE__ */ new Set();
402
+ const stride = counts.length;
403
+ for (let i = 0; i + 2 < soup.indices.length; i += 3) {
404
+ const a = to[soup.indices[i]], b = to[soup.indices[i + 1]], c = to[soup.indices[i + 2]];
405
+ if (a === b || b === c || a === c) continue;
406
+ const lo = Math.min(a, b, c), hi = Math.max(a, b, c);
407
+ const key = (lo * stride + (a + b + c - lo - hi)) * stride + hi;
408
+ if (seen.has(key)) continue;
409
+ seen.add(key);
410
+ indices.push(a, b, c);
411
+ }
412
+ return { vertices, indices, placed: /* @__PURE__ */ new Map() };
413
+ };
414
+ var axesOf = ([x, y, z, w]) => [
415
+ 1 - 2 * (y * y + z * z),
416
+ 2 * (x * y + z * w),
417
+ 2 * (x * z - y * w),
418
+ 2 * (x * y - z * w),
419
+ 1 - 2 * (x * x + z * z),
420
+ 2 * (y * z + x * w),
421
+ 2 * (x * z + y * w),
422
+ 2 * (y * z - x * w),
423
+ 1 - 2 * (x * x + y * y)
424
+ ];
425
+ var tightened = (points, box) => {
426
+ const axes = axesOf(box.rotation);
427
+ const low = [Infinity, Infinity, Infinity];
428
+ const high = [-Infinity, -Infinity, -Infinity];
429
+ for (let i = 0; i < points.length; i += 3) {
430
+ for (let k = 0; k < 3; k++) {
431
+ const t = points[i] * axes[k * 3] + points[i + 1] * axes[k * 3 + 1] + points[i + 2] * axes[k * 3 + 2];
432
+ if (t < low[k]) low[k] = t;
433
+ if (t > high[k]) high[k] = t;
434
+ }
435
+ }
436
+ const mid = [0, 1, 2].map((k) => (low[k] + high[k]) / 2);
437
+ const center = [0, 1, 2].map((k) => mid[0] * axes[k] + mid[1] * axes[3 + k] + mid[2] * axes[6 + k]);
438
+ const half = [0, 1, 2].map((k) => Math.max(MIN_HALF, (high[k] - low[k]) / 2));
439
+ return { center, half, rotation: box.rotation };
440
+ };
441
+ var isOneBox = (soup) => fitOrientedBoxes(soup.vertices, soup.indices).length <= 1;
442
+ var colliderOf = (id, key, soup) => {
443
+ const points = soup.vertices;
444
+ if (points.length < 9) return null;
445
+ const low = [Infinity, Infinity, Infinity];
446
+ const high = [-Infinity, -Infinity, -Infinity];
447
+ for (let i = 0; i < points.length; i += 3) {
448
+ for (let k = 0; k < 3; k++) {
449
+ const v = points[i + k];
450
+ if (v < low[k]) low[k] = v;
451
+ if (v > high[k]) high[k] = v;
452
+ }
453
+ }
454
+ const center = [(low[0] + high[0]) / 2, (low[1] + high[1]) / 2, (low[2] + high[2]) / 2];
455
+ const half = [(high[0] - low[0]) / 2, (high[1] - low[1]) / 2, (high[2] - low[2]) / 2];
456
+ const held = half.map((h) => Math.max(MIN_HALF, h));
457
+ let oneBox;
458
+ const isHeldByOneBox = () => oneBox ?? (oneBox = isOneBox(soup));
459
+ if (hugs(points, { center, half, axes: IDENTITY_AXES, tolerance: [FIT, FIT, FIT] }) && isHeldByOneBox()) return { id, key, center, half: held, rotation: [0, 0, 0, 1] };
460
+ const turned = fitOrientedBox(points);
461
+ if (turned && hugs(points, { center: turned.center, half: turned.half, axes: axesOf(turned.rotation), tolerance: [FIT, FIT, FIT] }) && isHeldByOneBox()) return { id, key, ...tightened(points, turned) };
462
+ const coarse = soup.indices.length / 3 > COARSEN_ABOVE ? coarsen(soup) : soup;
463
+ const kept = coarse.indices.length >= COARSE_MIN_TRIANGLES * 3 ? coarse : soup;
464
+ return { id, key, center, half: held, rotation: [0, 0, 0, 1], mesh: { vertices: new Float32Array(kept.vertices), indices: new Uint32Array(kept.indices) } };
465
+ };
466
+
467
+ // src/colliders.ts
468
+ var SHELL_ROLES = /* @__PURE__ */ new Set(["panel", "fender", "roof", "frame", "glass", "pillar", "door", "lid", "hood", "bumper"]);
469
+ var GONE = /* @__PURE__ */ new Set(["broken", "melted", "ashes"]);
470
+ var MIN_AREA = 1e-9;
471
+ var CHUNK = 0.8;
472
+ var CELL_BIAS2 = 512;
473
+ var CELL_SPAN2 = 1024;
474
+ var PART_SPAN = 2 ** 30;
475
+ var DEFAULT_BUDGET = Infinity;
476
+ var isDrawn = (mesh, stop) => {
477
+ if (!mesh.isMesh || !mesh.geometry || mesh.userData.ghost || mesh.userData.helper) return false;
478
+ if (!mesh.geometry.getAttribute("position")) return false;
479
+ for (let node = mesh; node; node = node.parent) {
480
+ if (node.visible === false || node.userData.noSurfaceRay) return false;
481
+ if (node === stop) break;
482
+ }
483
+ return true;
484
+ };
485
+ var shellPartAt = (shell, hub, x, y, z) => {
486
+ let best = hub;
487
+ let bestVolume = Infinity;
488
+ for (const part of shell) {
489
+ const { center, size } = part.box;
490
+ if (Math.abs(x - center[0]) > size[0] / 2 || Math.abs(y - center[1]) > size[1] / 2 || Math.abs(z - center[2]) > size[2] / 2) continue;
491
+ const volume = size[0] * size[1] * size[2];
492
+ if (volume < bestVolume) [best, bestVolume] = [part.id, volume];
493
+ }
494
+ return best;
495
+ };
496
+ var isNotDrawn = (positions, [a, b, c]) => {
497
+ const ux = positions[b * 3] - positions[a * 3], uy = positions[b * 3 + 1] - positions[a * 3 + 1], uz = positions[b * 3 + 2] - positions[a * 3 + 2];
498
+ const wx = positions[c * 3] - positions[a * 3], wy = positions[c * 3 + 1] - positions[a * 3 + 1], wz = positions[c * 3 + 2] - positions[a * 3 + 2];
499
+ const area = Math.hypot(uy * wz - uz * wy, uz * wx - ux * wz, ux * wy - uy * wx);
500
+ return !(area > MIN_AREA);
501
+ };
502
+ var sameMatrix = (a, b) => {
503
+ for (let i = 0; i < 16; i++) if (a[i] !== b[i]) return false;
504
+ return true;
505
+ };
506
+ var createVehicleColliders = (THREE, vehicle, frame, options = {}) => {
507
+ const entries = /* @__PURE__ */ new Map();
508
+ const pieces = /* @__PURE__ */ new Map();
509
+ const partIndex = new Map(vehicle.model.parts.map((part, index) => [part.id, index]));
510
+ const isShown2 = new Map(vehicle.model.parts.map((part) => [part.id, part.visual]));
511
+ const shell = vehicle.model.parts.filter((part) => SHELL_ROLES.has(part.role));
512
+ const toFrame = new THREE.Matrix4();
513
+ const toVehicle = new THREE.Matrix4();
514
+ const toShell = new THREE.Matrix4();
515
+ const placed = new THREE.Vector3();
516
+ const matrix = new THREE.Matrix4();
517
+ let pending = false;
518
+ const release = (entry) => {
519
+ for (const key of entry.chunks.keys()) {
520
+ const piece = pieces.get(key);
521
+ if (!piece) continue;
522
+ piece.entries.delete(entry);
523
+ piece.dirty = true;
524
+ }
525
+ };
526
+ const touch = (entry, only) => {
527
+ for (const key of entry.chunks.keys()) if (!only || only.has(key)) {
528
+ const piece = pieces.get(key);
529
+ if (piece) piece.dirty = true;
530
+ }
531
+ };
532
+ const assign = (mesh, entry, budget) => {
533
+ toShell.multiplyMatrices(toVehicle, mesh.matrixWorld);
534
+ const positions = entry.positions;
535
+ const index = entry.indexArray;
536
+ const owner = entry.owner;
537
+ const e = toShell.elements;
538
+ for (; entry.assigned < entry.triangles && budget.left > 0; entry.assigned++, budget.left--) {
539
+ const t = entry.assigned * 3;
540
+ const a = index ? index[t] : t, b = index ? index[t + 1] : t + 1, c = index ? index[t + 2] : t + 2;
541
+ placed.set(
542
+ (positions[a * 3] + positions[b * 3] + positions[c * 3]) / 3,
543
+ (positions[a * 3 + 1] + positions[b * 3 + 1] + positions[c * 3 + 1]) / 3,
544
+ (positions[a * 3 + 2] + positions[b * 3 + 2] + positions[c * 3 + 2]) / 3
545
+ );
546
+ const x = e[0] * placed.x + e[4] * placed.y + e[8] * placed.z + e[12];
547
+ const y = e[1] * placed.x + e[5] * placed.y + e[9] * placed.z + e[13];
548
+ const z = e[2] * placed.x + e[6] * placed.y + e[10] * placed.z + e[14];
549
+ const part = owner ?? shellPartAt(shell, vehicle.model.hub, x, y, z);
550
+ const cell = ((Math.floor(x / CHUNK) + CELL_BIAS2) * CELL_SPAN2 + (Math.floor(y / CHUNK) + CELL_BIAS2)) * CELL_SPAN2 + (Math.floor(z / CHUNK) + CELL_BIAS2);
551
+ entry.pieceOf[entry.assigned] = (partIndex.get(part) ?? 0) * PART_SPAN + cell;
552
+ }
553
+ if (entry.assigned < entry.triangles) return false;
554
+ for (let t = 0; t < entry.triangles; t++) {
555
+ const key = entry.pieceOf[t];
556
+ let list = entry.chunks.get(key);
557
+ if (!list) entry.chunks.set(key, list = []);
558
+ list.push(t);
559
+ }
560
+ for (const key of entry.chunks.keys()) {
561
+ let piece = pieces.get(key);
562
+ if (!piece) {
563
+ 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 });
565
+ }
566
+ piece.entries.add(entry);
567
+ piece.dirty = true;
568
+ }
569
+ entry.snapshot = new Float32Array(positions);
570
+ return true;
571
+ };
572
+ const follow = (entry, position) => {
573
+ const array = entry.positions;
574
+ const moved = /* @__PURE__ */ new Set();
575
+ const changed = new Uint8Array(array.length / 3);
576
+ for (let i = 0; i < array.length; i++) {
577
+ if (array[i] === entry.snapshot[i]) continue;
578
+ changed[Math.floor(i / 3)] = 1;
579
+ entry.snapshot[i] = array[i];
580
+ }
581
+ const index = entry.indexArray;
582
+ for (let t = 0; t < entry.triangles; t++) {
583
+ const a = index ? index[t * 3] : t * 3, b = index ? index[t * 3 + 1] : t * 3 + 1, c = index ? index[t * 3 + 2] : t * 3 + 2;
584
+ if (changed[a] || changed[b] || changed[c]) moved.add(entry.pieceOf[t]);
585
+ }
586
+ entry.version = position.version;
587
+ touch(entry, moved);
588
+ };
589
+ const read = (mesh, owner, budget) => {
590
+ const geometry = mesh.geometry;
591
+ const position = geometry.getAttribute("position");
592
+ const indexAttribute = geometry.getIndex();
593
+ const triangles = Math.floor((indexAttribute ? indexAttribute.count : position.count) / 3);
594
+ matrix.multiplyMatrices(toFrame, mesh.matrixWorld);
595
+ let entry = entries.get(mesh);
596
+ if (entry && (entry.geometry !== geometry || entry.owner !== owner || entry.positions !== position.array || entry.indexArray !== indexAttribute?.array || entry.triangles !== triangles)) {
597
+ release(entry);
598
+ entries.delete(mesh);
599
+ entry = void 0;
600
+ }
601
+ if (!entry) {
602
+ entry = { geometry, owner, positions: position.array, indexArray: indexAttribute?.array, triangles, matrix: [...matrix.elements], version: position.version, snapshot: new Float32Array(0), pieceOf: new Float64Array(triangles), assigned: 0, chunks: /* @__PURE__ */ new Map() };
603
+ entries.set(mesh, entry);
604
+ }
605
+ if (entry.assigned < entry.triangles) {
606
+ if (!assign(mesh, entry, budget)) pending = true;
607
+ return;
608
+ }
609
+ if (!sameMatrix(entry.matrix, matrix.elements)) {
610
+ entry.matrix = [...matrix.elements];
611
+ touch(entry);
612
+ }
613
+ if (position.version !== entry.version) follow(entry, position);
614
+ };
615
+ const rebuild = (piece, budget) => {
616
+ const soup = soupOf();
617
+ for (const entry of piece.entries) {
618
+ for (const t of entry.chunks.get(piece.key) ?? []) {
619
+ const index = entry.indexArray;
620
+ budget.left--;
621
+ const corners = index ? [index[t * 3], index[t * 3 + 1], index[t * 3 + 2]] : [t * 3, t * 3 + 1, t * 3 + 2];
622
+ if (isNotDrawn(entry.positions, corners)) continue;
623
+ addTriangle(soup, entry, entry.positions, corners, entry.matrix);
624
+ }
625
+ }
626
+ piece.collider = colliderOf(piece.part, `${piece.part}#${piece.key % PART_SPAN}`, soup);
627
+ piece.dirty = false;
628
+ };
629
+ const make = () => {
630
+ const root = vehicle.vehicle.vehicle;
631
+ frame.updateWorldMatrix(true, true);
632
+ root.updateWorldMatrix(true, true);
633
+ toFrame.copy(frame.matrixWorld).invert();
634
+ toVehicle.copy(root.matrixWorld).invert();
635
+ const state = new Map(vehicle.state().parts.map((part) => [part.id, part]));
636
+ const isGone = (id) => {
637
+ const part = state.get(id);
638
+ return part !== void 0 && (GONE.has(part.status) || part.group !== 0);
639
+ };
640
+ const budget = { left: options.budget ?? DEFAULT_BUDGET };
641
+ pending = false;
642
+ const live = /* @__PURE__ */ new Map();
643
+ const claimed = /* @__PURE__ */ new Set();
644
+ for (const part of vehicle.model.parts) {
645
+ for (const object of vehicle.objectsOf(part.id)) {
646
+ object.traverse((node) => {
647
+ const mesh = node;
648
+ if (claimed.has(mesh)) return;
649
+ claimed.add(mesh);
650
+ if (!isGone(part.id) && isDrawn(mesh, root)) live.set(mesh, part.id);
651
+ });
652
+ }
653
+ }
654
+ root.traverse((node) => {
655
+ const mesh = node;
656
+ if (!claimed.has(mesh) && isDrawn(mesh, root)) live.set(mesh, null);
657
+ });
658
+ for (const [mesh, entry] of entries) {
659
+ if (live.has(mesh)) continue;
660
+ release(entry);
661
+ entries.delete(mesh);
662
+ }
663
+ for (const [mesh, owner] of live) read(mesh, owner, budget);
664
+ const out = [];
665
+ for (const [key, piece] of pieces) {
666
+ if (piece.entries.size === 0) {
667
+ pieces.delete(key);
668
+ continue;
669
+ }
670
+ if (piece.dirty) {
671
+ if (budget.left <= 0) pending = true;
672
+ else rebuild(piece, budget);
673
+ }
674
+ if (piece.collider && !(isGone(piece.part) && isShown2.get(piece.part))) out.push(piece.collider);
675
+ }
676
+ return out;
677
+ };
678
+ return { pieces: make, isPending: () => pending };
679
+ };
680
+ var vehicleColliders = (THREE, vehicle, frame) => createVehicleColliders(THREE, vehicle, frame).pieces();
681
+
335
682
  // src/vehicle-lod.ts
336
- import { markKitOrigin as markKitOrigin2 } from "@adatechnology/game-contracts";
337
- import { createStaticBatcher } from "@adatechnology/game-render-three";
683
+ import { DEFERRED_QUEUE_ID, markKitOrigin as markKitOrigin2 } from "@adatechnology/game-contracts";
684
+ import { BATCHED_LAYER, createStaticBatchQueue, createStaticBatcher } from "@adatechnology/game-render-three";
338
685
  var VEHICLE_LOD_DISTANCE = 14;
339
- var BATCH_BUDGET_MS = 2;
686
+ var HIT_BATCH_VERTICES = 16384;
340
687
  var VEHICLE_REBATCH_AFTER_MS = 1500;
688
+ var createVehicleLodQueue = () => createStaticBatchQueue({ id: DEFERRED_QUEUE_ID.VEHICLE_LOD_BATCH });
341
689
  var isShown = (object, top) => {
342
690
  for (let node = object; node && node !== top; node = node.parent) if (node.visible === false) return false;
343
691
  return true;
@@ -345,7 +693,9 @@ var isShown = (object, top) => {
345
693
  function createVehicleLod(kit, context, root, full, options = {}) {
346
694
  const THREE = kit.THREE;
347
695
  const now = options.now ?? (() => performance.now());
696
+ const queue = options.queue ?? createVehicleLodQueue();
348
697
  let changedAt = Number.NEGATIVE_INFINITY;
698
+ let isRestless = false;
349
699
  let isUploaded = false;
350
700
  let modelKey = "";
351
701
  const culled = /* @__PURE__ */ new Map();
@@ -378,6 +728,9 @@ function createVehicleLod(kit, context, root, full, options = {}) {
378
728
  let isFused = false;
379
729
  let isLocked = false;
380
730
  let batcher = null;
731
+ const rejoined = [];
732
+ let givenBack = 0;
733
+ let offered = 0;
381
734
  let shadow = null;
382
735
  const castsShadow = /* @__PURE__ */ new Set();
383
736
  const shadowProxyOf = (fused) => {
@@ -407,15 +760,27 @@ function createVehicleLod(kit, context, root, full, options = {}) {
407
760
  root.add(proxy);
408
761
  return proxy;
409
762
  };
763
+ const lockedBatcher = (include) => {
764
+ const made = createStaticBatcher(THREE, full, { queue, budgetMs: 2, maxVertices: HIT_BATCH_VERTICES, ...include ? { include } : {} });
765
+ made.group.traverse((object) => object.userData.skipLite = true);
766
+ made.group.userData.noSurfaceRay = true;
767
+ markKitOrigin2(made.group, { owner: "game-vehicles-three", kind: "vehicle", id: root.name });
768
+ return made;
769
+ };
410
770
  const batchLocked = () => {
411
- batcher = createStaticBatcher(THREE, full, { budgetMs: BATCH_BUDGET_MS });
412
- batcher.group.traverse((object) => object.userData.skipLite = true);
413
- batcher.group.userData.noSurfaceRay = true;
414
- markKitOrigin2(batcher.group, { owner: "game-vehicles-three", kind: "vehicle", id: root.name });
771
+ batcher = lockedBatcher();
772
+ };
773
+ const isOnItsOwn = (mesh) => mesh.layers.mask !== 1 << BATCHED_LAYER && mesh.parent?.name !== "static-batches";
774
+ const disposeRejoined = () => {
775
+ for (const made of rejoined) made.dispose();
776
+ rejoined.length = 0;
415
777
  };
416
778
  const drawFull = () => {
417
779
  if (!batcher && isLocked) {
418
- if (now() - changedAt >= VEHICLE_REBATCH_AFTER_MS) batchLocked();
780
+ if (!isRestless || now() - changedAt >= VEHICLE_REBATCH_AFTER_MS) {
781
+ isRestless = false;
782
+ batchLocked();
783
+ }
419
784
  return;
420
785
  }
421
786
  if (!batcher) {
@@ -429,17 +794,28 @@ function createVehicleLod(kit, context, root, full, options = {}) {
429
794
  castsShadow.add(object);
430
795
  object.castShadow = false;
431
796
  });
432
- batcher = createStaticBatcher(THREE, full, { budgetMs: BATCH_BUDGET_MS });
797
+ batcher = createStaticBatcher(THREE, full, { queue, budgetMs: 2 });
433
798
  batcher.group.traverse((object) => object.userData.skipLite = true);
434
799
  batcher.group.userData.noSurfaceRay = true;
435
800
  markKitOrigin2(batcher.group, { owner: "game-vehicles-three", kind: "vehicle", id: root.name });
436
801
  return;
437
802
  }
438
803
  if (!batcher) return;
439
- if (batcher.update() > 0) changedAt = now();
440
- if (batcher.stats().batched === 0 && batcher.stats().released > 0) {
804
+ let changed = batcher.update();
805
+ for (const made of rejoined) changed += made.update();
806
+ givenBack += changed;
807
+ if (changed > 0) changedAt = now();
808
+ const { batched, released } = batcher.stats();
809
+ const rejoinedBatched = rejoined.reduce((sum, made) => sum + made.stats().batched, 0);
810
+ if (batched === 0 && released > 0 && rejoinedBatched === 0) {
441
811
  batcher.dispose();
442
812
  batcher = null;
813
+ disposeRejoined();
814
+ givenBack = offered = 0;
815
+ isRestless = true;
816
+ } else if (isLocked && givenBack - offered >= 2 && now() - changedAt >= VEHICLE_REBATCH_AFTER_MS) {
817
+ offered = givenBack;
818
+ rejoined.push(lockedBatcher(isOnItsOwn));
443
819
  }
444
820
  };
445
821
  const build = () => {
@@ -483,6 +859,7 @@ function createVehicleLod(kit, context, root, full, options = {}) {
483
859
  isLocked || (isLocked = touched);
484
860
  if (keyOf() !== modelKey) {
485
861
  changedAt = now();
862
+ isRestless = true;
486
863
  api.dispose();
487
864
  armUpload();
488
865
  }
@@ -503,6 +880,8 @@ function createVehicleLod(kit, context, root, full, options = {}) {
503
880
  show(false);
504
881
  batcher?.dispose();
505
882
  batcher = null;
883
+ disposeRejoined();
884
+ givenBack = offered = 0;
506
885
  for (const mesh of castsShadow) mesh.castShadow = true;
507
886
  castsShadow.clear();
508
887
  shadow?.removeFromParent();
@@ -1060,7 +1439,7 @@ var hornOfGroup = (group) => group === "motorcycles" ? "horn_bike" : TRUCK_GROUP
1060
1439
 
1061
1440
  // src/test-area/types.ts
1062
1441
  var defaultEffectIds = {
1063
- ignite: "vehicle_fire",
1442
+ ignite: "volumetric_fire",
1064
1443
  explode: "car_fuel_tank",
1065
1444
  glass: "glass_burst",
1066
1445
  sparks: "impact_metal",
@@ -1400,7 +1779,9 @@ var VEHICLES_AREA_ID = "vehicles_area";
1400
1779
  var DEFAULT_TEST_VEHICLES = ["compact_hatch", "midsize_sedan", "midsize_pickup", "sport_bike", "box_truck", "city_bus", "battle_tank", "attack_helicopter"];
1401
1780
  var ITEM_OF = { compact_hatch: "hatch", midsize_sedan: "sedan", midsize_pickup: "pickup", sport_bike: "sport_bike", box_truck: "box_truck", city_bus: "city_bus", battle_tank: "battle_tank", attack_helicopter: "attack_helicopter" };
1402
1781
  var itemId = (key) => ITEM_OF[key] ?? key;
1782
+ var COLLIDER_BUDGET = 25e3;
1403
1783
  var LANE = { z: 11, from: -12, wall: 13 };
1784
+ var CRASH_WALL = { length: 6, height: 2, thickness: 0.15 };
1404
1785
  var SPARK_EVERY_S = 0.08;
1405
1786
  var TYRE_DAMAGE = /* @__PURE__ */ new Set(["flat_tyre", "torn_tyre", "run_flat"]);
1406
1787
  var LEAF_REST_RADIANS = 0.02;
@@ -1492,6 +1873,9 @@ function createVehiclesTestModule(options = {}) {
1492
1873
  const kit = r.userData.kit;
1493
1874
  const d = createVehicleDestructible2(kit, r.userData.vehicle, { id, frame: root, name: text.items[itemKey] ?? key, creaseQueue });
1494
1875
  tapEffects(d);
1876
+ const colliders = createVehicleColliders(kit.THREE, d, root, { budget: COLLIDER_BUDGET });
1877
+ d.colliders = colliders.pieces;
1878
+ d.collidersPending = colliders.isPending;
1495
1879
  const setPartPose = d.setPartPose?.bind(d);
1496
1880
  {
1497
1881
  const rests = new Map(d.describe().parts.map((part) => [part.id, part.rotation ?? [0, 0, 0]]));
@@ -1647,7 +2031,10 @@ function createVehiclesTestModule(options = {}) {
1647
2031
  stopEngine();
1648
2032
  if (!lane) return;
1649
2033
  lane.group.parent?.remove(lane.group);
1650
- lane.wall.parent?.remove(lane.wall);
2034
+ if (lane.wall) {
2035
+ lane.wall.parent?.remove(lane.wall);
2036
+ discardObject3D(lane.wall);
2037
+ }
1651
2038
  lane = null;
1652
2039
  };
1653
2040
  const startLane = () => {
@@ -1656,10 +2043,14 @@ function createVehiclesTestModule(options = {}) {
1656
2043
  const kit = kitFor(three);
1657
2044
  const vehicles = createVehiclesThree(three, { kit, fleet });
1658
2045
  const built = vehicles.build("compact_hatch", { quality: settings.damage === "none" ? "lite" : "high", seed: settings.seed });
1659
- const wall = new three.Mesh(new three.BoxGeometry(0.6, 2, 6), new three.MeshStandardMaterial({ color: 7829367 }));
1660
- wall.position.set(LANE.wall + 0.3, 1, LANE.z);
2046
+ const wall = options.wall ? options.wall(three, CRASH_WALL) : null;
1661
2047
  const scene = host.scene;
1662
- scene.add(built.group, wall);
2048
+ scene.add(built.group);
2049
+ if (wall) {
2050
+ wall.rotation.y = Math.PI / 2;
2051
+ wall.position.set(LANE.wall + CRASH_WALL.thickness / 2, 0, LANE.z);
2052
+ scene.add(wall);
2053
+ }
1663
2054
  const mode = settings.driving === "semi" || settings.damage !== "none" ? "semi" : "arcade";
1664
2055
  const damage = settings.damage === "none" || !built.context ? null : createLaneDamage({ kit, context: built.context, frame: built.group, damage: settings.damage });
1665
2056
  const surfaces = createSurfaces2();
@@ -1795,9 +2186,10 @@ function createVehiclesTestModule(options = {}) {
1795
2186
  }
1796
2187
  };
1797
2188
  const creaseQueue = createCreaseQueue();
2189
+ const lodQueue = createVehicleLodQueue();
1798
2190
  const updateLod = (entry) => {
1799
2191
  if (!host || settings.quality === "lite") return;
1800
- entry.lod ?? (entry.lod = createVehicleLod(entry.root.userData.kit, entry.item.vehicle, entry.root, entry.root.children[0]));
2192
+ entry.lod ?? (entry.lod = createVehicleLod(entry.root.userData.kit, entry.item.vehicle, entry.root, entry.root.children[0], { queue: lodQueue }));
1801
2193
  const at = host.player().position;
1802
2194
  const distance = Math.hypot(at[0] - entry.root.position.x, at[2] - entry.root.position.z);
1803
2195
  const c = entry.item.vehicle;
@@ -1807,6 +2199,7 @@ function createVehiclesTestModule(options = {}) {
1807
2199
  const frame = (dt) => {
1808
2200
  for (const [id, entry] of placed) {
1809
2201
  if (!entry.root.parent) {
2202
+ entry.lod?.dispose();
1810
2203
  placed.delete(id);
1811
2204
  continue;
1812
2205
  }
@@ -1913,7 +2306,8 @@ function createVehiclesTestModule(options = {}) {
1913
2306
  const stopChange = lent.bench?.onChange(onChange);
1914
2307
  const stopFrame = lent.onFrame(frame);
1915
2308
  const stopBlocker = lent.addBlocker?.(blocked);
1916
- const stopCrease = lent.defer?.(creaseQueue.deferredQueue(), DEFERRED_PRIORITY[DEFERRED_QUEUE_ID.VEHICLE_CREASE]);
2309
+ const stopCrease = lent.defer?.(creaseQueue.deferredQueue(), DEFERRED_PRIORITY[DEFERRED_QUEUE_ID2.VEHICLE_CREASE]);
2310
+ const stopLodBatch = lent.defer?.(lodQueue.deferredQueue(), DEFERRED_PRIORITY[DEFERRED_QUEUE_ID2.VEHICLE_LOD_BATCH]);
1917
2311
  if (isRenderPools(lent.pools)) lent.pools.materials.register("vehicles.cracked-glass", () => crackedGlassMaterial(three, new three.DataTexture(new Uint8Array(4), 1, 1)));
1918
2312
  lent.audio?.register?.(LIBRARY_BANK);
1919
2313
  lent.audio?.register?.(VEHICLE_SOUND_BANK);
@@ -1923,6 +2317,7 @@ function createVehiclesTestModule(options = {}) {
1923
2317
  stopFrame();
1924
2318
  stopBlocker?.();
1925
2319
  stopCrease?.();
2320
+ stopLodBatch?.();
1926
2321
  cabins.clear(effects());
1927
2322
  clearHelpers();
1928
2323
  clearLane();
@@ -1947,7 +2342,7 @@ import { createVehicleCamera, defaultEye, mirrorPoses } from "@adatechnology/gam
1947
2342
  import { markKitOrigin as markKitOrigin3 } from "@adatechnology/game-contracts";
1948
2343
  import { CABIN_BANK, LIBRARY_BANK as LIBRARY_BANK2, RADIO_BANK } from "@adatechnology/game-audio";
1949
2344
  import { createSemiSimDriver as createSemiSimDriver4 } from "@adatechnology/game-vehicles-driving";
1950
- import { certify, createProvingTests, startRun, statusOf, subjectSpec as subjectSpec2, withResult } from "@adatechnology/game-vehicles-proving";
2345
+ import { certify, createProvingTests, startRun, STEPS_PER_TICK, statusOf, subjectSpec as subjectSpec2, withResult } from "@adatechnology/game-vehicles-proving";
1951
2346
  import { discardObject3D as discardObject3D3 } from "@adatechnology/game-shapes-three";
1952
2347
 
1953
2348
  // src/test-area/proving/cockpit.ts
@@ -2324,6 +2719,7 @@ function createVehicleLamps(THREE, built, options = {}) {
2324
2719
  const has = { head: lamps.some((l) => l.kind === "headlight"), tail: lamps.some((l) => l.kind === "taillight"), fog: lamps.some((l) => l.kind === "fog"), indicators: false, beacon: beacons.length > 0 };
2325
2720
  const originals = /* @__PURE__ */ new Map();
2326
2721
  const copies = /* @__PURE__ */ new Map();
2722
+ const copyOf = /* @__PURE__ */ new Map();
2327
2723
  const base = /* @__PURE__ */ new Map();
2328
2724
  const own = (mesh) => {
2329
2725
  const mat = mesh.material;
@@ -2332,6 +2728,7 @@ function createVehicleLamps(THREE, built, options = {}) {
2332
2728
  const copy = mat.clone();
2333
2729
  originals.set(mesh, mat);
2334
2730
  copies.set(copy, mat);
2731
+ copyOf.set(mesh, copy);
2335
2732
  base.set(copy, mat.emissiveIntensity ?? 1);
2336
2733
  mesh.material = copy;
2337
2734
  return copy;
@@ -2467,13 +2864,12 @@ function createVehicleLamps(THREE, built, options = {}) {
2467
2864
  for (const a of anchors) a.removeFromParent();
2468
2865
  anchors.length = 0;
2469
2866
  const giveBack = (m) => {
2470
- const copy = m.material;
2471
2867
  const original = originals.get(m);
2472
2868
  if (!original) return;
2473
2869
  originals.delete(m);
2474
- if (!copies.has(copy)) return;
2475
- m.material = original;
2476
- if (copy && !Array.isArray(copy)) copy.dispose?.();
2870
+ if (copies.has(m.material)) m.material = original;
2871
+ copyOf.get(m)?.dispose?.();
2872
+ copyOf.delete(m);
2477
2873
  };
2478
2874
  for (const b of beacons) giveBack(b.mesh);
2479
2875
  for (const l of lamps) {
@@ -2486,6 +2882,7 @@ function createVehicleLamps(THREE, built, options = {}) {
2486
2882
  }
2487
2883
  originals.clear();
2488
2884
  copies.clear();
2885
+ copyOf.clear();
2489
2886
  base.clear();
2490
2887
  }
2491
2888
  };
@@ -3173,6 +3570,7 @@ function createProvingModule(options) {
3173
3570
  let misted = false;
3174
3571
  let lastGear = null;
3175
3572
  let driveClock = 0;
3573
+ let boardHalfSecond = 0;
3176
3574
  let queue = [];
3177
3575
  let open = false;
3178
3576
  let lastWrap = 0;
@@ -3442,6 +3840,7 @@ function createProvingModule(options) {
3442
3840
  if (!car) return;
3443
3841
  parking = null;
3444
3842
  run = startRun(test, car.subject);
3843
+ boardHalfSecond = 0;
3445
3844
  setSurface(run.surface);
3446
3845
  if (id === "turn_circle") run.driver.place(0, TRACK.start, 0);
3447
3846
  rig.vehicle?.reset();
@@ -3785,7 +4184,7 @@ function createProvingModule(options) {
3785
4184
  adjust[ADJUST_KEYS[change.id]] = Number(change.value);
3786
4185
  break;
3787
4186
  case "time_scale":
3788
- settings.timeScale = Number(change.value);
4187
+ settings.timeScale = Math.max(1, Math.round(Number(change.value)));
3789
4188
  break;
3790
4189
  case "gearbox":
3791
4190
  gearbox = change.value === "manual" ? "manual" : "automatic";
@@ -3808,7 +4207,6 @@ function createProvingModule(options) {
3808
4207
  let speed = 0;
3809
4208
  let steer = 0;
3810
4209
  if (run) {
3811
- run.step(dt * settings.timeScale);
3812
4210
  const s = run.driver.state;
3813
4211
  pose(s, s.steerAngle);
3814
4212
  speed = s.u;
@@ -3864,7 +4262,8 @@ function createProvingModule(options) {
3864
4262
  lastGear = geared.gear;
3865
4263
  drawBoard();
3866
4264
  } else if (!geared) lastGear = null;
3867
- if (run && Math.floor(run.time * 2) !== Math.floor((run.time - dt * settings.timeScale) * 2) || driving && Math.floor(driveClock * 2) !== Math.floor((driveClock - dt) * 2)) drawBoard();
4265
+ if (run && Math.floor(run.time * 2) !== boardHalfSecond || driving && Math.floor(driveClock * 2) !== Math.floor((driveClock - dt) * 2)) drawBoard();
4266
+ if (run) boardHalfSecond = Math.floor(run.time * 2);
3868
4267
  if (driving) driveClock += dt;
3869
4268
  };
3870
4269
  const speedNow = () => Math.abs(run?.driver.state.u ?? driving?.driver.state.u ?? parking?.driver.state.u ?? 0) * 3.6;
@@ -3951,6 +4350,7 @@ function createProvingModule(options) {
3951
4350
  lent.audio?.preload?.(VEHICLE_PRELOAD_SOUNDS);
3952
4351
  const stopChange = lent.bench?.onChange(onChange);
3953
4352
  const stopFrame = lent.onFrame(frame);
4353
+ const stopRunTick = lent.onFrame(() => run?.stepFixed(STEPS_PER_TICK * settings.timeScale), { phase: "fixedSimulate" });
3954
4354
  const stopClear = lent.onClear?.(() => {
3955
4355
  open = false;
3956
4356
  teardown();
@@ -3959,6 +4359,7 @@ function createProvingModule(options) {
3959
4359
  return () => {
3960
4360
  stopChange?.();
3961
4361
  stopFrame();
4362
+ stopRunTick();
3962
4363
  stopClear?.();
3963
4364
  teardown();
3964
4365
  lent.setViewCamera?.(null);
@@ -4005,7 +4406,10 @@ export {
4005
4406
  createEngineBudget,
4006
4407
  slideOfGrip,
4007
4408
  createVehicleSound,
4409
+ createVehicleColliders,
4410
+ vehicleColliders,
4008
4411
  VEHICLE_LOD_DISTANCE,
4412
+ createVehicleLodQueue,
4009
4413
  createVehicleLod,
4010
4414
  VEHICLE_SCENARIO_IDS,
4011
4415
  PAGE_SCENARIO_ALIASES,
@@ -4030,4 +4434,4 @@ export {
4030
4434
  createProvingModule,
4031
4435
  loadProvingModule
4032
4436
  };
4033
- //# sourceMappingURL=chunk-CCKLH536.js.map
4437
+ //# sourceMappingURL=chunk-GEB4YOV7.js.map