@threenative/physics 0.3.21 → 0.3.22

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/index.js CHANGED
@@ -1229,6 +1229,24 @@ function nativeBodyShape(shape) {
1229
1229
  z: shape.z
1230
1230
  };
1231
1231
  }
1232
+ if (shape.kind === "convexHull") {
1233
+ if (shape.vertices === void 0)
1234
+ throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: convexHull requires vertices");
1235
+ return {
1236
+ collisionLayer: shape.collisionLayer,
1237
+ collisionMask: shape.collisionMask,
1238
+ kind: shape.kind,
1239
+ sensor: shape.sensor,
1240
+ vertices: shape.vertices,
1241
+ x: shape.x,
1242
+ y: shape.y,
1243
+ z: shape.z
1244
+ };
1245
+ }
1246
+ return primitiveShape(shape);
1247
+ }
1248
+ function nativeQueryShape(shape) {
1249
+ if (shape.kind === "convexHull" || shape.kind === "trimesh") return nativeBodyShape(shape);
1232
1250
  return primitiveShape(shape);
1233
1251
  }
1234
1252
  function opaqueNativeShape(shape) {
@@ -1504,6 +1522,7 @@ function createNativePhysicsSimulation(raw, version) {
1504
1522
  if (raw.setBodyTransform === void 0)
1505
1523
  throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
1506
1524
  raw.setBodyTransform(id, position);
1525
+ bodyTransformsDirty = true;
1507
1526
  },
1508
1527
  // Keep the optional raw members: an older runtime must throw rather than accept a call and
1509
1528
  // drop the actuation, which would make the same game diverge by platform.
@@ -1637,7 +1656,7 @@ function createNativePhysicsSimulation(raw, version) {
1637
1656
  maxResults: query.maxResults,
1638
1657
  position: query.position,
1639
1658
  rotation: query.rotation,
1640
- shape: primitiveShape(query.shape)
1659
+ shape: nativeQueryShape(query.shape)
1641
1660
  });
1642
1661
  if (!Array.isArray(hits) || hits.length > query.maxResults)
1643
1662
  throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
@@ -1815,6 +1834,7 @@ installPhysicsSimulationBackend(createNativePhysicsBackend(nativePhysicsHost));
1815
1834
  // src/native/wasm-host.ts
1816
1835
  var EXPORTS = [
1817
1836
  "tn_physics_add_body",
1837
+ "tn_physics_add_convex_hull_body",
1818
1838
  "tn_physics_add_heightfield_body",
1819
1839
  "tn_physics_add_trimesh_body",
1820
1840
  "tn_physics_alloc",
@@ -1828,6 +1848,7 @@ var EXPORTS = [
1828
1848
  "tn_physics_destroy",
1829
1849
  "tn_physics_drain_collision_events",
1830
1850
  "tn_physics_free",
1851
+ "tn_physics_intersect_mesh_shape",
1831
1852
  "tn_physics_intersect_point",
1832
1853
  "tn_physics_intersect_ray",
1833
1854
  "tn_physics_intersect_shape",
@@ -1849,6 +1870,13 @@ var EXPORTS = [
1849
1870
  "tn_physics_step",
1850
1871
  "tn_physics_version"
1851
1872
  ];
1873
+ var QUERY_SHAPE_TYPES = {
1874
+ box: 0,
1875
+ capsule: 2,
1876
+ convexHull: 5,
1877
+ sphere: 1,
1878
+ trimesh: 3
1879
+ };
1852
1880
  var MAX_EXACT_FLOAT_ID = 16777215;
1853
1881
  var F32_MAX = 34028234663852886e22;
1854
1882
  var ACTUATION_OK = 1;
@@ -1990,6 +2018,7 @@ function createWasmPhysicsHost(exports) {
1990
2018
  let nextVehicleId = 0;
1991
2019
  const first = { bytes: 0, ptr: 0 };
1992
2020
  const second = { bytes: 0, ptr: 0 };
2021
+ const third = { bytes: 0, ptr: 0 };
1993
2022
  const ensure = (scratch, bytes) => {
1994
2023
  if (scratch.bytes >= bytes) return scratch.ptr;
1995
2024
  const size = Math.max(64, bytes, scratch.bytes * 2);
@@ -2000,6 +2029,19 @@ function createWasmPhysicsHost(exports) {
2000
2029
  scratch.bytes = size;
2001
2030
  return scratch.ptr;
2002
2031
  };
2032
+ const meshBuffers = (shape, trimesh) => {
2033
+ const { vertices, indices } = shape;
2034
+ if (!(vertices instanceof Float32Array)) throw INVALID;
2035
+ if (!trimesh) return [copyIn(second, vertices), vertices.length, 0, 0];
2036
+ if (!(indices instanceof Uint32Array)) throw INVALID;
2037
+ return [copyIn(second, vertices), vertices.length, copyIn(third, indices), indices.length];
2038
+ };
2039
+ const queryVolume = (shape) => {
2040
+ const type = QUERY_SHAPE_TYPES[shape.kind];
2041
+ if (type === void 0 || !Object.hasOwn(QUERY_SHAPE_TYPES, shape.kind))
2042
+ throw INVALID;
2043
+ return type === 3 || type === 5 ? { mesh: meshBuffers(shape, type === 3), type } : { type };
2044
+ };
2003
2045
  const live = () => {
2004
2046
  if (simulation === 0) throw new Error("physics simulation is disposed");
2005
2047
  };
@@ -2065,9 +2107,14 @@ function createWasmPhysicsHost(exports) {
2065
2107
  value.type
2066
2108
  );
2067
2109
  const shape = fields(value.shape);
2068
- const shapeType = ["box", "sphere", "capsule", "trimesh", "heightfield"].indexOf(
2069
- shape.kind
2070
- );
2110
+ const shapeType = [
2111
+ "box",
2112
+ "sphere",
2113
+ "capsule",
2114
+ "trimesh",
2115
+ "heightfield",
2116
+ "convexHull"
2117
+ ].indexOf(shape.kind);
2071
2118
  if (typeof value.type !== "string" || bodyType < 0) throw INVALID;
2072
2119
  if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
2073
2120
  u32[r2] = nextId;
@@ -2117,6 +2164,15 @@ function createWasmPhysicsHost(exports) {
2117
2164
  sy,
2118
2165
  sz
2119
2166
  );
2167
+ } else if (shapeType === 5) {
2168
+ const { vertices } = shape;
2169
+ if (!(vertices instanceof Float32Array)) throw INVALID;
2170
+ added = wasm.tn_physics_add_convex_hull_body(
2171
+ simulation,
2172
+ record,
2173
+ copyIn(first, vertices),
2174
+ vertices.length
2175
+ );
2120
2176
  } else added = wasm.tn_physics_add_body(simulation, record);
2121
2177
  } catch (error) {
2122
2178
  throw fail(error, "physics body options are invalid");
@@ -2580,11 +2636,13 @@ function createWasmPhysicsHost(exports) {
2580
2636
  if (query === void 0) throw new Error("intersectShape requires query options");
2581
2637
  let pointer;
2582
2638
  let limit;
2639
+ let mesh;
2583
2640
  try {
2584
2641
  const value = fields(query);
2585
2642
  const shape = fields(value.shape);
2586
- const shapeType = ["box", "sphere", "capsule"].indexOf(shape.kind);
2587
- if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
2643
+ const volume = queryVolume(shape);
2644
+ const shapeType = volume.type;
2645
+ mesh = volume.mesh;
2588
2646
  limit = maxResults(value);
2589
2647
  pointer = ensure(first, limit * QUERY_HIT_BYTES);
2590
2648
  views();
@@ -2600,7 +2658,7 @@ function createWasmPhysicsHost(exports) {
2600
2658
  } catch (error) {
2601
2659
  throw fail(error, "intersectShape query options are invalid");
2602
2660
  }
2603
- const count = wasm.tn_physics_intersect_shape(simulation, record, pointer, limit);
2661
+ const count = mesh === void 0 ? wasm.tn_physics_intersect_shape(simulation, record, pointer, limit) : wasm.tn_physics_intersect_mesh_shape(simulation, record, ...mesh, pointer, limit);
2604
2662
  if (count < 0) throw new Error("intersectShape query was rejected");
2605
2663
  return queryHits(pointer, count);
2606
2664
  },
@@ -2629,7 +2687,7 @@ function createWasmPhysicsHost(exports) {
2629
2687
  if (simulation === 0) return;
2630
2688
  wasm.tn_physics_destroy(simulation);
2631
2689
  simulation = 0;
2632
- for (const scratch of [first, second]) {
2690
+ for (const scratch of [first, second, third]) {
2633
2691
  if (scratch.bytes > 0) wasm.tn_physics_free(scratch.ptr, scratch.bytes);
2634
2692
  scratch.bytes = 0;
2635
2693
  scratch.ptr = 0;
@@ -7,11 +7,11 @@ import 'recast-navigation';
7
7
  import '@threenative/core/world';
8
8
 
9
9
  interface INativeShapeDescriptor {
10
- readonly kind: "box" | "capsule" | "heightfield" | "sphere" | "trimesh";
10
+ readonly kind: "box" | "capsule" | "convexHull" | "heightfield" | "sphere" | "trimesh";
11
11
  readonly x: number;
12
12
  readonly y: number;
13
13
  readonly z: number;
14
- /** Present for `trimesh` only: a flat xyz vertex buffer and a flat triangle-index buffer. */
14
+ /** `trimesh` and `convexHull`: a flat xyz vertex buffer; `trimesh` adds a flat triangle-index buffer. */
15
15
  readonly vertices?: Float32Array;
16
16
  readonly indices?: Uint32Array;
17
17
  /** Present for `heightfield` only: column-major samples, the grid and its full extent. */
@@ -349,6 +349,24 @@ function nativeBodyShape(shape) {
349
349
  z: shape.z
350
350
  };
351
351
  }
352
+ if (shape.kind === "convexHull") {
353
+ if (shape.vertices === void 0)
354
+ throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: convexHull requires vertices");
355
+ return {
356
+ collisionLayer: shape.collisionLayer,
357
+ collisionMask: shape.collisionMask,
358
+ kind: shape.kind,
359
+ sensor: shape.sensor,
360
+ vertices: shape.vertices,
361
+ x: shape.x,
362
+ y: shape.y,
363
+ z: shape.z
364
+ };
365
+ }
366
+ return primitiveShape(shape);
367
+ }
368
+ function nativeQueryShape(shape) {
369
+ if (shape.kind === "convexHull" || shape.kind === "trimesh") return nativeBodyShape(shape);
352
370
  return primitiveShape(shape);
353
371
  }
354
372
  function opaqueNativeShape(shape) {
@@ -624,6 +642,7 @@ function createNativePhysicsSimulation(raw, version) {
624
642
  if (raw.setBodyTransform === void 0)
625
643
  throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
626
644
  raw.setBodyTransform(id, position);
645
+ bodyTransformsDirty = true;
627
646
  },
628
647
  // Keep the optional raw members: an older runtime must throw rather than accept a call and
629
648
  // drop the actuation, which would make the same game diverge by platform.
@@ -757,7 +776,7 @@ function createNativePhysicsSimulation(raw, version) {
757
776
  maxResults: query.maxResults,
758
777
  position: query.position,
759
778
  rotation: query.rotation,
760
- shape: primitiveShape(query.shape)
779
+ shape: nativeQueryShape(query.shape)
761
780
  });
762
781
  if (!Array.isArray(hits) || hits.length > query.maxResults)
763
782
  throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threenative/physics",
3
- "version": "0.3.21",
3
+ "version": "0.3.22",
4
4
  "license": "MIT",
5
5
  "description": "Thin Rapier physics bindings for ThreeNative games.",
6
6
  "keywords": [
@@ -56,7 +56,7 @@
56
56
  "recast-navigation": "0.43.1"
57
57
  },
58
58
  "peerDependencies": {
59
- "@threenative/core": ">=0.3.21 <0.4.0",
59
+ "@threenative/core": ">=0.3.22 <0.4.0",
60
60
  "three": ">=0.187.0 <0.188.0"
61
61
  },
62
62
  "devDependencies": {
@@ -66,7 +66,7 @@
66
66
  "tsup": "8.5.1",
67
67
  "typescript": "npm:@typescript/typescript6@6.0.2",
68
68
  "@typescript/native": "npm:typescript@7.0.2",
69
- "@threenative/core": "0.3.21"
69
+ "@threenative/core": "0.3.22"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsup --config tsup.config.ts && publint",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "rustc": "rustc 1.96.0 (ac68faa20 2026-05-25)",
3
3
  "files": {
4
- "threenative_native_physics.wasm": "917c1c510b43f102576acd477062e7863688c3c8200978372bf9b359a15284b2"
4
+ "threenative_native_physics.wasm": "cfd0f82671aa3802bb3e1589492077d2a71ee0fd7cc23cf18f7dd22b57dc3dd3"
5
5
  }
6
6
  }