@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21

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.
Files changed (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -3,6 +3,7 @@ export type CanonicalValue = null | boolean | string | number | readonly Canonic
3
3
  };
4
4
  export declare function canonicalize(value: unknown): CanonicalValue;
5
5
  export declare function canonicalStringify(value: unknown): string;
6
+ export declare function canonicalParse<T = unknown>(text: string): T;
6
7
  export declare function cloneCanonical<T>(value: T): T;
7
8
  export declare function decodeCanonicalValue<T>(value: CanonicalValue): T;
8
9
  export declare function defaultChecksum(value: unknown): string;
package/dist/canonical.js CHANGED
@@ -6,6 +6,9 @@ export function canonicalize(value) {
6
6
  export function canonicalStringify(value) {
7
7
  return JSON.stringify(canonicalize(value));
8
8
  }
9
+ export function canonicalParse(text) {
10
+ return decodeCanonical(JSON.parse(text));
11
+ }
9
12
  export function cloneCanonical(value) {
10
13
  return decodeCanonical(JSON.parse(canonicalStringify(value)));
11
14
  }
@@ -0,0 +1,10 @@
1
+ export type SyncplayChecksumMode = 'per-tick' | 'interval' | 'sparse' | 'events-only';
2
+ export interface SyncplayChecksumOptions {
3
+ readonly checksumMode?: SyncplayChecksumMode;
4
+ readonly checksumIntervalFrames?: number;
5
+ }
6
+ export interface SyncplayChecksumPolicy {
7
+ readonly checksumMode: SyncplayChecksumMode;
8
+ readonly checksumIntervalFrames: number;
9
+ }
10
+ export declare function parseSyncplayChecksumPolicy(options: SyncplayChecksumOptions, defaultMode?: SyncplayChecksumMode): SyncplayChecksumPolicy;
@@ -0,0 +1,14 @@
1
+ import { SyncplayError } from './errors.js';
2
+ export function parseSyncplayChecksumPolicy(options, defaultMode = 'per-tick') {
3
+ const checksumMode = options.checksumMode ?? defaultMode;
4
+ const checksumIntervalFrames = options.checksumIntervalFrames ?? 1;
5
+ if ((checksumMode !== 'per-tick' && checksumMode !== 'interval'
6
+ && checksumMode !== 'sparse' && checksumMode !== 'events-only')
7
+ || !Number.isSafeInteger(checksumIntervalFrames) || checksumIntervalFrames < 1) {
8
+ throw new SyncplayError('Checksum policy requires a supported mode and a positive frame interval.', {
9
+ code: 'session.checksum-policy-invalid',
10
+ operation: 'configure-checksums',
11
+ });
12
+ }
13
+ return Object.freeze({ checksumMode, checksumIntervalFrames });
14
+ }
@@ -0,0 +1,18 @@
1
+ import type { Point3D } from './physics3d-shared.js';
2
+ export interface HullFace {
3
+ /** Outward unit normal in hull-local space. */
4
+ readonly normal: Point3D;
5
+ /** Plane offset: dot(normal, vertex) === offset for on-plane vertices. */
6
+ readonly offset: number;
7
+ /** Vertex indices, CCW around `normal`. */
8
+ readonly verts: readonly number[];
9
+ }
10
+ export interface SolverHullData {
11
+ /** Local space, deduplicated, lexicographically sorted. */
12
+ readonly vertices: readonly Point3D[];
13
+ /** Outward face planes, verts CCW. */
14
+ readonly faces: readonly HullFace[];
15
+ /** Unique undirected edge directions (normalized, canonical sign). */
16
+ readonly edgeDirs: readonly Point3D[];
17
+ }
18
+ export declare function buildSolverHull(vertices: readonly Point3D[]): SolverHullData;
@@ -0,0 +1,248 @@
1
+ import { crossVec3, dotVec3, scaleVec3, solverSqrt, subVec3, } from './physics3d-shared.js';
2
+ const PLANE_TOL = 1e-9;
3
+ const COPLANAR_TOL = 1e-6;
4
+ const DEDUP_TOL2 = 1e-12;
5
+ const EDGE_TOL = 1e-6;
6
+ function lexCompare(a, b) {
7
+ if (a.x !== b.x) {
8
+ return a.x < b.x ? -1 : 1;
9
+ }
10
+ if (a.y !== b.y) {
11
+ return a.y < b.y ? -1 : 1;
12
+ }
13
+ if (a.z !== b.z) {
14
+ return a.z < b.z ? -1 : 1;
15
+ }
16
+ return 0;
17
+ }
18
+ function dedupSorted(input) {
19
+ const sorted = [...input].sort(lexCompare);
20
+ const out = [];
21
+ for (const v of sorted) {
22
+ const last = out[out.length - 1];
23
+ if (last !== undefined) {
24
+ const dx = v.x - last.x;
25
+ const dy = v.y - last.y;
26
+ const dz = v.z - last.z;
27
+ if (dx * dx + dy * dy + dz * dz <= DEDUP_TOL2) {
28
+ continue;
29
+ }
30
+ }
31
+ out.push({ x: v.x, y: v.y, z: v.z });
32
+ }
33
+ return out;
34
+ }
35
+ function isFinitePoint(v) {
36
+ return Number.isFinite(v.x) && Number.isFinite(v.y) && Number.isFinite(v.z);
37
+ }
38
+ /**
39
+ * Order the on-plane vertex indices CCW around `normal` (right-hand rule) using
40
+ * a signed-angle comparator built from cross/dot sign tests only — no atan2.
41
+ * The polygon is convex, so all points share a consistent winding relative to
42
+ * the centroid; we sort by half-plane then by cross-product sign.
43
+ */
44
+ function orderFaceVerts(indices, verts, normal) {
45
+ /* istanbul ignore next -- a supporting-plane face always carries at least three vertices. */
46
+ if (indices.length <= 2) {
47
+ return [...indices];
48
+ }
49
+ let cx = 0;
50
+ let cy = 0;
51
+ let cz = 0;
52
+ for (const i of indices) {
53
+ cx += verts[i].x;
54
+ cy += verts[i].y;
55
+ cz += verts[i].z;
56
+ }
57
+ const inv = 1 / indices.length;
58
+ const center = { x: cx * inv, y: cy * inv, z: cz * inv };
59
+ // Build an orthonormal basis (u, w) spanning the face plane.
60
+ const ref = subVec3(verts[indices[0]], center);
61
+ const refLen = solverSqrt(dotVec3(ref, ref));
62
+ /* istanbul ignore next -- a face vertex never coincides with its own face centroid, so refLen is always positive. */
63
+ const u = refLen > EDGE_TOL ? scaleVec3(ref, 1 / refLen) : orthonormalAny(normal);
64
+ const w = crossVec3(normal, u);
65
+ const key = (i) => {
66
+ const d = subVec3(verts[i], center);
67
+ const s = dotVec3(d, w);
68
+ const c = dotVec3(d, u);
69
+ // half 0 for upper (s>=0), 1 for lower — upper sweeps first for CCW order.
70
+ return { half: s >= 0 ? 0 : 1, s, c };
71
+ };
72
+ return [...indices].sort((ia, ib) => {
73
+ const a = key(ia);
74
+ const b = key(ib);
75
+ if (a.half !== b.half) {
76
+ return a.half - b.half;
77
+ }
78
+ // Within a half-plane, angle increases as (c descending) via cross sign:
79
+ // cross of the two direction vectors' sign gives relative order.
80
+ const da = subVec3(verts[ia], center);
81
+ const db = subVec3(verts[ib], center);
82
+ const cr = dotVec3(crossVec3(da, db), normal);
83
+ if (cr > EDGE_TOL) {
84
+ return -1;
85
+ }
86
+ /* istanbul ignore else -- an exact cross-product tie needs two same-half face vertices antipodal through the centroid; no convex face produces one (searched over cube + 4/6/8-gon prisms at four phases). The index tie-break stays as the determinism guarantee. */
87
+ if (cr < -EDGE_TOL) {
88
+ return 1;
89
+ }
90
+ /* istanbul ignore next -- see above: the exact tie is unreachable, the tie-break stays for determinism. */
91
+ return ia - ib;
92
+ });
93
+ }
94
+ /* istanbul ignore next -- only reachable from the unreachable refLen guard in orderFaceVerts. */
95
+ function orthonormalAny(n) {
96
+ const ax = Math.abs(n.x);
97
+ const ay = Math.abs(n.y);
98
+ const az = Math.abs(n.z);
99
+ const pick = ax <= ay && ax <= az ? { x: 1, y: 0, z: 0 } : ay <= az ? { x: 0, y: 1, z: 0 } : { x: 0, y: 0, z: 1 };
100
+ const proj = dotVec3(pick, n);
101
+ const t = subVec3(pick, scaleVec3(n, proj));
102
+ const len = solverSqrt(dotVec3(t, t));
103
+ return len > EDGE_TOL ? scaleVec3(t, 1 / len) : { x: 1, y: 0, z: 0 };
104
+ }
105
+ function buildHullUncached(vertices) {
106
+ if (vertices.length < 4 || vertices.length > 16) {
107
+ throw new Error(`hull requires 4-16 finite vertices with non-zero volume, received ${vertices.length}`);
108
+ }
109
+ for (const v of vertices) {
110
+ if (!isFinitePoint(v)) {
111
+ throw new Error('hull requires 4-16 finite vertices with non-zero volume, received non-finite vertex');
112
+ }
113
+ }
114
+ const verts = dedupSorted(vertices);
115
+ if (verts.length < 4) {
116
+ throw new Error('hull requires 4-16 finite vertices with non-zero volume, received degenerate vertex set');
117
+ }
118
+ const n = verts.length;
119
+ let cx = 0;
120
+ let cy = 0;
121
+ let cz = 0;
122
+ for (const v of verts) {
123
+ cx += v.x;
124
+ cy += v.y;
125
+ cz += v.z;
126
+ }
127
+ const centroid = { x: cx / n, y: cy / n, z: cz / n };
128
+ // Enumerate every triple as a candidate supporting plane.
129
+ const candidates = [];
130
+ for (let i = 0; i < n; i += 1) {
131
+ for (let j = i + 1; j < n; j += 1) {
132
+ for (let k = j + 1; k < n; k += 1) {
133
+ const e1 = subVec3(verts[j], verts[i]);
134
+ const e2 = subVec3(verts[k], verts[i]);
135
+ const cr = crossVec3(e1, e2);
136
+ const len2 = dotVec3(cr, cr);
137
+ if (len2 < PLANE_TOL * PLANE_TOL) {
138
+ continue; // collinear triple
139
+ }
140
+ const invLen = 1 / solverSqrt(len2);
141
+ let nx = cr.x * invLen;
142
+ let ny = cr.y * invLen;
143
+ let nz = cr.z * invLen;
144
+ let offset = nx * verts[i].x + ny * verts[i].y + nz * verts[i].z;
145
+ // Orient outward: centroid must lie behind the plane (dot < offset).
146
+ const centroidDot = nx * centroid.x + ny * centroid.y + nz * centroid.z;
147
+ if (centroidDot > offset) {
148
+ nx = -nx;
149
+ ny = -ny;
150
+ nz = -nz;
151
+ offset = -offset;
152
+ }
153
+ // Keep iff all vertices lie on or behind the plane.
154
+ let supporting = true;
155
+ const onPlane = [];
156
+ for (let m = 0; m < n; m += 1) {
157
+ const d = nx * verts[m].x + ny * verts[m].y + nz * verts[m].z - offset;
158
+ if (d > PLANE_TOL) {
159
+ supporting = false;
160
+ break;
161
+ }
162
+ if (d >= -PLANE_TOL) {
163
+ onPlane.push(m);
164
+ }
165
+ }
166
+ if (supporting && onPlane.length >= 3) {
167
+ candidates.push({ nx, ny, nz, offset, onPlane });
168
+ }
169
+ }
170
+ }
171
+ }
172
+ // Merge coplanar candidates (same oriented plane within tolerance).
173
+ const faces = [];
174
+ const usedVerts = new Array(n).fill(false);
175
+ for (const cand of candidates) {
176
+ const existing = faces.find((f) => Math.abs(f.normal.x - cand.nx) <= COPLANAR_TOL
177
+ && Math.abs(f.normal.y - cand.ny) <= COPLANAR_TOL
178
+ && Math.abs(f.normal.z - cand.nz) <= COPLANAR_TOL
179
+ && Math.abs(f.offset - cand.offset) <= COPLANAR_TOL);
180
+ if (existing !== undefined) {
181
+ continue;
182
+ }
183
+ const merged = new Set(cand.onPlane);
184
+ for (let m = 0; m < n; m += 1) {
185
+ const d = cand.nx * verts[m].x + cand.ny * verts[m].y + cand.nz * verts[m].z - cand.offset;
186
+ if (d >= -PLANE_TOL && d <= PLANE_TOL) {
187
+ merged.add(m);
188
+ }
189
+ }
190
+ const normal = { x: cand.nx, y: cand.ny, z: cand.nz };
191
+ const ordered = orderFaceVerts([...merged], verts, normal);
192
+ for (const idx of ordered) {
193
+ usedVerts[idx] = true;
194
+ }
195
+ faces.push({ normal, offset: cand.offset, verts: ordered });
196
+ }
197
+ if (faces.length < 4) {
198
+ throw new Error('hull requires 4-16 finite vertices with non-zero volume, received coplanar vertex set');
199
+ }
200
+ for (let m = 0; m < n; m += 1) {
201
+ if (!usedVerts[m]) {
202
+ throw new Error('hull requires all vertices on the convex boundary, received interior vertex');
203
+ }
204
+ }
205
+ // Sort faces deterministically by outward normal then offset.
206
+ /* istanbul ignore next -- two faces of a convex hull never share a normal, so the offset tie-break never runs; it stays as the determinism guarantee. */
207
+ faces.sort((a, b) => lexCompare(a.normal, b.normal) || (a.offset < b.offset ? -1 : a.offset > b.offset ? 1 : 0));
208
+ // Unique undirected edge directions from face polygon edges.
209
+ const edgeDirs = [];
210
+ for (const face of faces) {
211
+ const fv = face.verts;
212
+ for (let e = 0; e < fv.length; e += 1) {
213
+ const a = verts[fv[e]];
214
+ const b = verts[fv[(e + 1) % fv.length]];
215
+ let dir = subVec3(b, a);
216
+ const len = solverSqrt(dotVec3(dir, dir));
217
+ /* istanbul ignore next -- dedupSorted removes coincident vertices, so no face edge is degenerate. */
218
+ if (len <= EDGE_TOL) {
219
+ continue;
220
+ }
221
+ dir = scaleVec3(dir, 1 / len);
222
+ // Canonical sign: first non-zero component positive.
223
+ if (dir.x < -EDGE_TOL || (Math.abs(dir.x) <= EDGE_TOL && (dir.y < -EDGE_TOL || (Math.abs(dir.y) <= EDGE_TOL && dir.z < 0)))) {
224
+ dir = scaleVec3(dir, -1);
225
+ }
226
+ const dup = edgeDirs.some((d) => Math.abs(d.x - dir.x) <= EDGE_TOL && Math.abs(d.y - dir.y) <= EDGE_TOL && Math.abs(d.z - dir.z) <= EDGE_TOL);
227
+ if (!dup) {
228
+ edgeDirs.push(dir);
229
+ }
230
+ }
231
+ }
232
+ edgeDirs.sort(lexCompare);
233
+ return { vertices: verts, faces, edgeDirs };
234
+ }
235
+ // Memoize by vertex-array identity: hull topology is a pure derived function of
236
+ // the immutable input vertex list (same rationale as quatMat3Cache) — never
237
+ // canonical rollback state, so it is safe to keep outside the frame.
238
+ // rdm-ignore-next-line determinism/no-module-mutable-state: pure derived hull data keyed by immutable input identity
239
+ const hullCache = new WeakMap();
240
+ export function buildSolverHull(vertices) {
241
+ const cached = hullCache.get(vertices);
242
+ if (cached !== undefined) {
243
+ return cached;
244
+ }
245
+ const built = buildHullUncached(vertices);
246
+ hullCache.set(vertices, built);
247
+ return built;
248
+ }
@@ -0,0 +1,140 @@
1
+ import type { Point3D } from './physics3d-shared.js';
2
+ type ColliderHullShape = Readonly<{
3
+ type: 'hull';
4
+ vertices: readonly Point3D[];
5
+ }>;
6
+ type ColliderCompoundChild = Readonly<{
7
+ shape: ColliderHullShape;
8
+ offset: Point3D;
9
+ }>;
10
+ type ColliderCompoundShape = Readonly<{
11
+ type: 'compound';
12
+ children: readonly ColliderCompoundChild[];
13
+ }>;
14
+ export interface ColliderSourceMesh {
15
+ /** Triangle-list vertices; `length` must be a positive multiple of 3. */
16
+ readonly vertices: readonly Point3D[];
17
+ }
18
+ export interface ColliderCookOptions {
19
+ /** Voxel grid resolution along the mesh's longest axis (4..96). */
20
+ readonly resolution?: number;
21
+ /** Upper bound on emitted hull children (1..64). */
22
+ readonly maxHulls?: number;
23
+ /** Vertex cap per emitted hull (8..16); 8 is the voxel-AABB fallback's size. */
24
+ readonly maxHullVertices?: number;
25
+ /** Stop splitting a part once `1 - partVolume/hullVolume` falls under this. */
26
+ readonly concavityTolerance?: number;
27
+ /** Output vertex quantum; must match the runtime's distance quantum or be coarser. */
28
+ readonly quantum?: number;
29
+ /** Candidate split planes evaluated per axis. */
30
+ readonly splitCandidates?: number;
31
+ }
32
+ export interface CookedColliderHull {
33
+ readonly vertexCount: number;
34
+ /** `1 - partVoxelVolume / hullVolume` for the part this hull covers. */
35
+ readonly concavity: number;
36
+ }
37
+ export interface CookedCollider {
38
+ readonly schemaVersion: 1;
39
+ readonly kind: 'deterministic-convex-decomposition';
40
+ readonly shape: ColliderCompoundShape;
41
+ readonly hulls: readonly CookedColliderHull[];
42
+ readonly sourceTriangles: number;
43
+ readonly resolution: number;
44
+ /** Volume-weighted mean concavity across the emitted hulls. */
45
+ readonly concavity: number;
46
+ readonly hash: string;
47
+ }
48
+ interface Lattice {
49
+ /** Grid dimensions in voxels. */
50
+ readonly nx: number;
51
+ readonly ny: number;
52
+ readonly nz: number;
53
+ readonly voxelSize: number;
54
+ readonly originX: number;
55
+ readonly originY: number;
56
+ readonly originZ: number;
57
+ /** Occupied voxel indices, ascending. */
58
+ readonly occupied: readonly number[];
59
+ }
60
+ /** A voxel-index subset of the lattice. */
61
+ interface Part {
62
+ readonly voxels: readonly number[];
63
+ readonly hull: LatticeHull;
64
+ readonly concavity: number;
65
+ }
66
+ /** Convex hull in corner-lattice integer coordinates. */
67
+ interface LatticeHull {
68
+ readonly vertices: readonly (readonly [number, number, number])[];
69
+ readonly faces: readonly (readonly [number, number, number])[];
70
+ /** Exact hull volume in corner-lattice units (voxel = 1 unit³). */
71
+ readonly volume: number;
72
+ }
73
+ export declare function cookConvexDecomposition(source: ColliderSourceMesh, options?: ColliderCookOptions): CookedCollider;
74
+ export declare function assertCookedColliderIsRuntimeValid(cooked: CookedCollider): void;
75
+ declare function voxelize(triangles: readonly (readonly [Point3D, Point3D, Point3D])[], resolution: number): Lattice;
76
+ /**
77
+ * Akenine-Möller triangle/AABB overlap, box centred at `center` with half-width
78
+ * `half`, biased outward by `tol`.
79
+ *
80
+ * The bias is load-bearing, not a fudge. Axis-aligned source faces land exactly
81
+ * on voxel boundaries whenever the voxel size is not a binary fraction (`1/24`
82
+ * is not), so the overlap test is decided at exact tangency and rounding rejects
83
+ * roughly half the cells. That punches holes in the surface shell, the exterior
84
+ * flood fill leaks through them, and the mesh cooks hollow. A conservative
85
+ * rasteriser must resolve tangency as "overlapping".
86
+ */
87
+ declare function triangleIntersectsBox(triangle: readonly [Point3D, Point3D, Point3D], center: Point3D, half: number): boolean;
88
+ declare function makePart(voxels: readonly number[], lattice: Lattice): Part;
89
+ declare function bestSplit(part: Part, lattice: Lattice, splitCandidates: number): readonly [Part, Part] | undefined;
90
+ type IVec = readonly [number, number, number];
91
+ /**
92
+ * Incremental convex hull over integer points. Degenerate inputs (collinear or
93
+ * coplanar) return an empty face set with zero volume; callers treat that as a
94
+ * flat part and fall back to its voxel AABB.
95
+ */
96
+ declare function convexHullLattice(points: readonly IVec[]): LatticeHull;
97
+ declare function collinear(a: IVec, b: IVec, c: IVec): boolean;
98
+ declare function finalizeHull(part: Part, lattice: Lattice, maxHullVertices: number, quantum: number): readonly Point3D[];
99
+ /**
100
+ * Drop the least load-bearing vertex until the cap is met.
101
+ *
102
+ * A vertex's cost is the volume of the cone its incident faces subtend from the
103
+ * hull's centroid — the part of the hull only that vertex carries. Scoring from
104
+ * the current hull's faces costs one hull rebuild per removal rather than one
105
+ * per candidate, which is the difference between a cook that takes a second and
106
+ * one that takes a minute. It is a greedy proxy, and it is allowed to be: the
107
+ * caller re-inflates the survivor until it contains the original hull, so a
108
+ * suboptimal choice costs tightness, never correctness.
109
+ */
110
+ declare function reduceLatticeHull(hull: LatticeHull, maxHullVertices: number): readonly IVec[];
111
+ /**
112
+ * Inflate `hullPoints` about their centroid by the smallest factor that puts
113
+ * every point of `mustContain` back inside. A cooked collider is never smaller
114
+ * than the geometry it stands for.
115
+ */
116
+ declare function inflateToContain(hullPoints: readonly Point3D[], mustContain: readonly Point3D[]): readonly Point3D[];
117
+ interface SupportPlane {
118
+ readonly normal: Point3D;
119
+ /** Signed distance from the centroid to the plane; always > 0. */
120
+ readonly offset: number;
121
+ }
122
+ declare function supportingPlanes(points: readonly Point3D[], centroid: Point3D): readonly SupportPlane[];
123
+ /** Vertices of `points` that survive on the convex boundary, in canonical order. */
124
+ declare function boundaryVertices(points: readonly Point3D[]): readonly Point3D[];
125
+ declare function dedupePoints(points: readonly Point3D[]): readonly Point3D[];
126
+ export declare const __colliderCookingInternals: {
127
+ bestSplit: typeof bestSplit;
128
+ boundaryVertices: typeof boundaryVertices;
129
+ collinear: typeof collinear;
130
+ convexHullLattice: typeof convexHullLattice;
131
+ dedupePoints: typeof dedupePoints;
132
+ finalizeHull: typeof finalizeHull;
133
+ inflateToContain: typeof inflateToContain;
134
+ makePart: typeof makePart;
135
+ reduceLatticeHull: typeof reduceLatticeHull;
136
+ supportingPlanes: typeof supportingPlanes;
137
+ triangleIntersectsBox: typeof triangleIntersectsBox;
138
+ voxelize: typeof voxelize;
139
+ };
140
+ export {};