@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
@@ -0,0 +1,1499 @@
1
+ import { carveSweptQuad } from './voxel-swept-quad.mjs';
2
+
3
+
4
+ import { requirePhysicsExports, physicsWorldAbiExports } from './wasm-instance.mjs';
5
+
6
+ export const VOXEL_ABI_VERSION = 2;
7
+ export const VOXEL_MAX_DIMENSION = 1024;
8
+ export const VOXEL_BOX_CHILD_STRIDE = 14;
9
+ export const VOXEL_COOK_STATS_STRIDE = 17;
10
+ export const VOXEL_SUBCHUNK_STRIDE = 10;
11
+ export const VOXEL_RAY_HIT_STRIDE = 12;
12
+ export const VOXEL_SEAM_RESULT_STRIDE = 20;
13
+ export const VOXEL_MASS_PROPERTIES_STRIDE = 11;
14
+
15
+ export const VoxelStatus = Object.freeze({
16
+ Success: 0,
17
+ NoOccupiedVoxels: 1,
18
+ Unchanged: 2,
19
+ InvalidDimensions: -1,
20
+ InvalidCellSize: -2,
21
+ NullBuffer: -3,
22
+ BufferOverflow: -4,
23
+ InvalidOperation: -5,
24
+ IncompatibleGrids: -6,
25
+ });
26
+
27
+ export const VoxelCarveShapeType = Object.freeze({
28
+ Box: 0,
29
+ Sphere: 1,
30
+ Capsule: 2,
31
+ SweptQuad: 3,
32
+ });
33
+
34
+ export const VoxelCarveOp = Object.freeze({
35
+ Carve: 0,
36
+ Fill: 1,
37
+ });
38
+
39
+ export const VoxelAnchorFaceMask = Object.freeze({
40
+ AnchorNone: 0,
41
+ AnchorNegX: 1 << 0, // 1
42
+ AnchorPosX: 1 << 1, // 2
43
+ AnchorNegY: 1 << 2, // 4
44
+ AnchorPosY: 1 << 3, // 8
45
+ AnchorNegZ: 1 << 4, // 16
46
+ AnchorPosZ: 1 << 5, // 32
47
+ AnchorAll: 0x3f, // 63
48
+ });
49
+
50
+ export const VoxelChildFlags = Object.freeze({
51
+ ChildFlagNone: 0,
52
+ ChildFlagPreserved: 1 << 0, // 1
53
+ ChildFlagRecooked: 1 << 1, // 2
54
+ ChildFlagSevered: 1 << 2, // 4
55
+ });
56
+
57
+ export const wasmEntryPointsUsed = Object.freeze([
58
+ 'rundot_kinetix_ext_voxel_abi_version',
59
+ 'rundot_kinetix_ext_voxel_child_stride',
60
+ 'rundot_kinetix_ext_voxel_stats_stride',
61
+ 'rundot_kinetix_ext_voxel_subchunk_stride',
62
+ 'rundot_kinetix_ext_voxel_ray_hit_stride',
63
+ 'rundot_kinetix_ext_voxel_seam_result_stride',
64
+ 'rundot_kinetix_ext_voxel_mass_properties_stride',
65
+ 'rundot_kinetix_ext_voxel_mass_properties',
66
+ 'rundot_kinetix_ext_voxel_scratch_size',
67
+ 'rundot_kinetix_ext_voxel_cook_chunk',
68
+ 'rundot_kinetix_ext_voxel_carve',
69
+ 'rundot_kinetix_ext_voxel_incremental_cook',
70
+ 'rundot_kinetix_ext_voxel_split_components',
71
+ 'rundot_kinetix_ext_voxel_incremental_slice',
72
+ 'rundot_kinetix_ext_voxel_raycast_dda',
73
+ 'rundot_kinetix_ext_voxel_seam_query',
74
+ ]);
75
+
76
+ export async function ensureVoxelInstance() {
77
+ await physicsWorldAbiExports();
78
+ return getVoxelInstance();
79
+ }
80
+
81
+ export async function voxelPhysicsReady() {
82
+ await ensureVoxelInstance();
83
+ return true;
84
+ }
85
+
86
+ export function getVoxelInstance(instanceOverride = null) {
87
+ if (instanceOverride !== null) throw new Error('KINETIX_VOXEL_INSTANCE_OVERRIDE_UNSUPPORTED');
88
+ const exports = requirePhysicsExports();
89
+ if (exports.rundot_kinetix_ext_voxel_abi_version?.() !== VOXEL_ABI_VERSION
90
+ || exports.rundot_kinetix_ext_voxel_child_stride?.() !== VOXEL_BOX_CHILD_STRIDE
91
+ || exports.rundot_kinetix_ext_voxel_stats_stride?.() !== VOXEL_COOK_STATS_STRIDE
92
+ || exports.rundot_kinetix_ext_voxel_subchunk_stride?.() !== VOXEL_SUBCHUNK_STRIDE
93
+ || exports.rundot_kinetix_ext_voxel_ray_hit_stride?.() !== VOXEL_RAY_HIT_STRIDE
94
+ || exports.rundot_kinetix_ext_voxel_seam_result_stride?.() !== VOXEL_SEAM_RESULT_STRIDE
95
+ || exports.rundot_kinetix_ext_voxel_mass_properties_stride?.() !== VOXEL_MASS_PROPERTIES_STRIDE) {
96
+ throw new Error('KINETIX_VOXEL_WASM_ABI_MISMATCH');
97
+ }
98
+ return { exports };
99
+ }
100
+
101
+ function withVoxelScratch(instance, byteCount, callback) {
102
+ if (!Number.isSafeInteger(byteCount) || byteCount <= 0 || byteCount > 0x7ffffff0) {
103
+ throw new Error('KINETIX_VOXEL_BUFFER_SIZE_INVALID');
104
+ }
105
+ const exports = instance.exports;
106
+ const pointer = exports.rundot_kinetix_world_buffer_allocate(Math.ceil(byteCount / 16) * 16);
107
+ if (!Number.isInteger(pointer) || pointer <= 0) throw new Error('KINETIX_VOXEL_OUT_OF_MEMORY');
108
+ try {
109
+ return callback(pointer);
110
+ } finally {
111
+ exports.rundot_kinetix_world_buffer_free(pointer);
112
+ }
113
+ }
114
+
115
+ function requireInteger(value, min, max, field) {
116
+ if (!Number.isInteger(value) || value < min || value > max) {
117
+ throw new Error(`KINETIX_VOXEL_INVALID_ARGUMENT ${field}`);
118
+ }
119
+ }
120
+
121
+ function requireFinite(value, field) {
122
+ if (!Number.isFinite(value)) throw new Error(`KINETIX_VOXEL_INVALID_ARGUMENT ${field}`);
123
+ }
124
+
125
+ function validateVector(value, field, integer = false) {
126
+ for (const axis of ['x', 'y', 'z']) {
127
+ if (integer) requireInteger(value?.[axis], -2147482623, 2147482623, `${field}.${axis}`);
128
+ else requireFinite(value?.[axis], `${field}.${axis}`);
129
+ }
130
+ }
131
+
132
+ function validateGrid(dimX, dimY, dimZ, cellSize) {
133
+ for (const [field, value] of Object.entries({ dimX, dimY, dimZ })) {
134
+ requireInteger(value, 1, VOXEL_MAX_DIMENSION, field);
135
+ }
136
+ if (!Number.isFinite(cellSize) || cellSize <= 0
137
+ || !Number.isFinite(cellSize * Math.max(dimX, dimY, dimZ))) {
138
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT cellSize');
139
+ }
140
+ const totalCells = dimX * dimY * dimZ;
141
+ if (totalCells * 6 > 0x7ffffff0) throw new Error('KINETIX_VOXEL_BUFFER_SIZE_INVALID');
142
+ return totalCells;
143
+ }
144
+
145
+ function validateChunk(chunk) {
146
+ const total = validateGrid(chunk?.dimX, chunk?.dimY, chunk?.dimZ, chunk?.cellSize);
147
+ if (!(chunk.occupancy instanceof Uint8Array) || chunk.occupancy.length !== total) {
148
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT occupancy');
149
+ }
150
+ }
151
+
152
+ function validateBounds(bounds, field) {
153
+ for (const axis of ['X', 'Y', 'Z']) {
154
+ requireInteger(bounds?.[`min${axis}`], -2147482623, 2147482623, `${field}.min${axis}`);
155
+ requireInteger(bounds?.[`max${axis}`], -2147482623, 2147482623, `${field}.max${axis}`);
156
+ if (bounds[`min${axis}`] > bounds[`max${axis}`]) {
157
+ throw new Error(`KINETIX_VOXEL_INVALID_ARGUMENT ${field}`);
158
+ }
159
+ }
160
+ }
161
+
162
+ function validateChildren(children) {
163
+ if (!Array.isArray(children)) throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT children');
164
+ const ids = new Set();
165
+ for (const child of children) {
166
+ requireInteger(child?.childId, 0, 0xfffffffe, 'childId');
167
+ if (ids.has(child.childId)) throw new Error('KINETIX_VOXEL_DUPLICATE_CHILD_ID');
168
+ ids.add(child.childId);
169
+ validateVector(child.halfExtents, 'halfExtents');
170
+ if (child.halfExtents.x <= 0 || child.halfExtents.y <= 0 || child.halfExtents.z <= 0) {
171
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT halfExtents');
172
+ }
173
+ validateVector(child.offset, 'offset');
174
+ validateBounds(child.cellBounds, 'cellBounds');
175
+ requireInteger(child.flags, 0, 7, 'flags');
176
+ }
177
+ for (let first = 0; first < children.length; first += 1) {
178
+ const a = children[first];
179
+ for (let second = first + 1; second < children.length; second += 1) {
180
+ const b = children[second];
181
+ if (a.offset.x - a.halfExtents.x < b.offset.x + b.halfExtents.x
182
+ && b.offset.x - b.halfExtents.x < a.offset.x + a.halfExtents.x
183
+ && a.offset.y - a.halfExtents.y < b.offset.y + b.halfExtents.y
184
+ && b.offset.y - b.halfExtents.y < a.offset.y + a.halfExtents.y
185
+ && a.offset.z - a.halfExtents.z < b.offset.z + b.halfExtents.z
186
+ && b.offset.z - b.halfExtents.z < a.offset.z + a.halfExtents.z) {
187
+ throw new Error('KINETIX_VOXEL_OVERLAPPING_CHILDREN');
188
+ }
189
+ }
190
+ }
191
+ }
192
+
193
+ function validateCookOptions(options, capacity, children = []) {
194
+ requireInteger(capacity, 1, 0x7fffffff, 'capacity');
195
+ const minimumId = children.reduce((next, child) => Math.max(next, child.childId + 1), 0);
196
+ const next = options.nextChildId ?? minimumId;
197
+ requireInteger(next, minimumId, 0xffffffff - capacity, 'nextChildId');
198
+ return next;
199
+ }
200
+
201
+ function validateSplitOptions(options) {
202
+ requireInteger(options.maxSubChunks ?? 1024, 1, 0x7fffffff, 'maxSubChunks');
203
+ if (options.maxSubOccupancyBytes !== undefined) {
204
+ requireInteger(options.maxSubOccupancyBytes, 1, 0x7fffffff, 'maxSubOccupancyBytes');
205
+ }
206
+ requireInteger(options.anchorFacesMask ?? 0, 0, 63, 'anchorFacesMask');
207
+ }
208
+
209
+ function validateCarveRegion(region) {
210
+ if (region?.op !== 'carve' && region?.op !== 'fill' && region?.op !== 0 && region?.op !== 1) {
211
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT op');
212
+ }
213
+ switch (region.shapeType) {
214
+ case 'box': case 0:
215
+ validateBounds(region.box, 'box');
216
+ break;
217
+ case 'sphere': case 1:
218
+ validateVector(region.sphereCenter, 'sphereCenter');
219
+ requireFinite(region.sphereRadius, 'sphereRadius');
220
+ if (region.sphereRadius < 0 || !Number.isFinite(region.sphereRadius ** 2)) {
221
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT sphereRadius');
222
+ }
223
+ break;
224
+ case 'capsule': case 2:
225
+ validateVector(region.capsuleP0, 'capsuleP0');
226
+ validateVector(region.capsuleP1, 'capsuleP1');
227
+ requireFinite(region.capsuleRadius, 'capsuleRadius');
228
+ if (region.capsuleRadius < 0 || !Number.isFinite(region.capsuleRadius ** 2)) {
229
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT capsuleRadius');
230
+ }
231
+ break;
232
+ case 'swept_quad': case 3:
233
+ break;
234
+ default:
235
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT shapeType');
236
+ }
237
+ }
238
+
239
+ function requireVoxelStatus(status) {
240
+ if (status !== 0 && status !== 1 && status !== 2) {
241
+ throw new Error(`KINETIX_VOXEL_NATIVE_FAILURE status=${status}`);
242
+ }
243
+ }
244
+
245
+ export function getVoxelAbiVersion(instance = null) {
246
+ const inst = getVoxelInstance(instance);
247
+ return inst.exports.rundot_kinetix_ext_voxel_abi_version();
248
+ }
249
+
250
+ export function getVoxelChildStride(instance = null) {
251
+ const inst = getVoxelInstance(instance);
252
+ return inst.exports.rundot_kinetix_ext_voxel_child_stride();
253
+ }
254
+
255
+ export function getVoxelStatsStride(instance = null) {
256
+ const inst = getVoxelInstance(instance);
257
+ return inst.exports.rundot_kinetix_ext_voxel_stats_stride();
258
+ }
259
+
260
+ export function getVoxelSubChunkStride(instance = null) {
261
+ const inst = getVoxelInstance(instance);
262
+ return inst.exports.rundot_kinetix_ext_voxel_subchunk_stride();
263
+ }
264
+
265
+ export function getVoxelRayHitStride(instance = null) {
266
+ const inst = getVoxelInstance(instance);
267
+ return inst.exports.rundot_kinetix_ext_voxel_ray_hit_stride();
268
+ }
269
+
270
+ export function getVoxelSeamResultStride(instance = null) {
271
+ const inst = getVoxelInstance(instance);
272
+ return inst.exports.rundot_kinetix_ext_voxel_seam_result_stride();
273
+ }
274
+
275
+
276
+
277
+ export function getVoxelMaxDimension() { return VOXEL_MAX_DIMENSION; }
278
+
279
+ export function getVoxelScratchSize(dimX, dimY, dimZ, instance = null) {
280
+ const inst = getVoxelInstance(instance);
281
+ return inst.exports.rundot_kinetix_ext_voxel_scratch_size(dimX, dimY, dimZ);
282
+ }
283
+
284
+ export function unpackBoxChild(f64, offset = 0) {
285
+ return {
286
+ childId: Math.trunc(f64[offset + 0]),
287
+ halfExtents: {
288
+ x: f64[offset + 1],
289
+ y: f64[offset + 2],
290
+ z: f64[offset + 3],
291
+ },
292
+ offset: {
293
+ x: f64[offset + 4],
294
+ y: f64[offset + 5],
295
+ z: f64[offset + 6],
296
+ },
297
+ cellBounds: {
298
+ minX: Math.trunc(f64[offset + 7]),
299
+ minY: Math.trunc(f64[offset + 8]),
300
+ minZ: Math.trunc(f64[offset + 9]),
301
+ maxX: Math.trunc(f64[offset + 10]),
302
+ maxY: Math.trunc(f64[offset + 11]),
303
+ maxZ: Math.trunc(f64[offset + 12]),
304
+ },
305
+ flags: Math.trunc(f64[offset + 13]),
306
+ };
307
+ }
308
+
309
+ export function packBoxChild(child, f64, offset = 0) {
310
+ f64[offset + 0] = Number(child.childId ?? 0);
311
+ f64[offset + 1] = Number(child.halfExtents?.x ?? child.halfX ?? 0.0);
312
+ f64[offset + 2] = Number(child.halfExtents?.y ?? child.halfY ?? 0.0);
313
+ f64[offset + 3] = Number(child.halfExtents?.z ?? child.halfZ ?? 0.0);
314
+ f64[offset + 4] = Number(child.offset?.x ?? child.offsetX ?? 0.0);
315
+ f64[offset + 5] = Number(child.offset?.y ?? child.offsetY ?? 0.0);
316
+ f64[offset + 6] = Number(child.offset?.z ?? child.offsetZ ?? 0.0);
317
+ f64[offset + 7] = Number(child.cellBounds?.minX ?? 0);
318
+ f64[offset + 8] = Number(child.cellBounds?.minY ?? 0);
319
+ f64[offset + 9] = Number(child.cellBounds?.minZ ?? 0);
320
+ f64[offset + 10] = Number(child.cellBounds?.maxX ?? 0);
321
+ f64[offset + 11] = Number(child.cellBounds?.maxY ?? 0);
322
+ f64[offset + 12] = Number(child.cellBounds?.maxZ ?? 0);
323
+ f64[offset + 13] = Number(child.flags ?? 0);
324
+ }
325
+
326
+ export function unpackCookStats(f64, offset = 0) {
327
+ return {
328
+ totalBoxes: Math.trunc(f64[offset + 0]),
329
+ preservedBoxes: Math.trunc(f64[offset + 1]),
330
+ recookedBoxes: Math.trunc(f64[offset + 2]),
331
+ retiredBoxes: Math.trunc(f64[offset + 3]),
332
+ cellsEvaluated: Math.trunc(f64[offset + 4]),
333
+ dirtyAABB: {
334
+ minX: Math.trunc(f64[offset + 5]),
335
+ minY: Math.trunc(f64[offset + 6]),
336
+ minZ: Math.trunc(f64[offset + 7]),
337
+ maxX: Math.trunc(f64[offset + 8]),
338
+ maxY: Math.trunc(f64[offset + 9]),
339
+ maxZ: Math.trunc(f64[offset + 10]),
340
+ },
341
+ expandedAABB: {
342
+ minX: Math.trunc(f64[offset + 11]),
343
+ minY: Math.trunc(f64[offset + 12]),
344
+ minZ: Math.trunc(f64[offset + 13]),
345
+ maxX: Math.trunc(f64[offset + 14]),
346
+ maxY: Math.trunc(f64[offset + 15]),
347
+ maxZ: Math.trunc(f64[offset + 16]),
348
+ },
349
+ };
350
+ }
351
+
352
+ export function packCookStats(stats, f64, offset = 0) {
353
+ f64[offset + 0] = Number(stats.totalBoxes ?? 0);
354
+ f64[offset + 1] = Number(stats.preservedBoxes ?? 0);
355
+ f64[offset + 2] = Number(stats.recookedBoxes ?? 0);
356
+ f64[offset + 3] = Number(stats.retiredBoxes ?? 0);
357
+ f64[offset + 4] = Number(stats.cellsEvaluated ?? 0);
358
+ f64[offset + 5] = Number(stats.dirtyAABB?.minX ?? 0);
359
+ f64[offset + 6] = Number(stats.dirtyAABB?.minY ?? 0);
360
+ f64[offset + 7] = Number(stats.dirtyAABB?.minZ ?? 0);
361
+ f64[offset + 8] = Number(stats.dirtyAABB?.maxX ?? 0);
362
+ f64[offset + 9] = Number(stats.dirtyAABB?.maxY ?? 0);
363
+ f64[offset + 10] = Number(stats.dirtyAABB?.maxZ ?? 0);
364
+ f64[offset + 11] = Number(stats.expandedAABB?.minX ?? 0);
365
+ f64[offset + 12] = Number(stats.expandedAABB?.minY ?? 0);
366
+ f64[offset + 13] = Number(stats.expandedAABB?.minZ ?? 0);
367
+ f64[offset + 14] = Number(stats.expandedAABB?.maxX ?? 0);
368
+ f64[offset + 15] = Number(stats.expandedAABB?.maxY ?? 0);
369
+ f64[offset + 16] = Number(stats.expandedAABB?.maxZ ?? 0);
370
+ }
371
+
372
+ export function unpackSubChunk(f64, offset = 0) {
373
+ return {
374
+ subChunkId: Math.trunc(f64[offset + 0]),
375
+ offsetCells: {
376
+ x: Math.trunc(f64[offset + 1]),
377
+ y: Math.trunc(f64[offset + 2]),
378
+ z: Math.trunc(f64[offset + 3]),
379
+ },
380
+ dimX: Math.trunc(f64[offset + 4]),
381
+ dimY: Math.trunc(f64[offset + 5]),
382
+ dimZ: Math.trunc(f64[offset + 6]),
383
+ cellSize: f64[offset + 7],
384
+ occupiedCount: Math.trunc(f64[offset + 8]),
385
+ anchored: f64[offset + 9] !== 0.0,
386
+ };
387
+ }
388
+
389
+ export function packSubChunk(subChunk, f64, offset = 0) {
390
+ f64[offset + 0] = Number(subChunk.subChunkId ?? 0);
391
+ f64[offset + 1] = Number(subChunk.offsetCells?.x ?? subChunk.offsetX ?? 0);
392
+ f64[offset + 2] = Number(subChunk.offsetCells?.y ?? subChunk.offsetY ?? 0);
393
+ f64[offset + 3] = Number(subChunk.offsetCells?.z ?? subChunk.offsetZ ?? 0);
394
+ f64[offset + 4] = Number(subChunk.dimX ?? 0);
395
+ f64[offset + 5] = Number(subChunk.dimY ?? 0);
396
+ f64[offset + 6] = Number(subChunk.dimZ ?? 0);
397
+ f64[offset + 7] = Number(subChunk.cellSize ?? 1.0);
398
+ f64[offset + 8] = Number(subChunk.occupiedCount ?? 0);
399
+ f64[offset + 9] = subChunk.anchored ? 1.0 : 0.0;
400
+ }
401
+
402
+ export function unpackRaycastHit(f64, offset = 0) {
403
+ return {
404
+ hit: f64[offset + 0] !== 0.0,
405
+ distance: f64[offset + 1],
406
+ voxelCoords: {
407
+ x: Math.trunc(f64[offset + 2]),
408
+ y: Math.trunc(f64[offset + 3]),
409
+ z: Math.trunc(f64[offset + 4]),
410
+ },
411
+ point: {
412
+ x: f64[offset + 5],
413
+ y: f64[offset + 6],
414
+ z: f64[offset + 7],
415
+ },
416
+ normal: {
417
+ x: f64[offset + 8],
418
+ y: f64[offset + 9],
419
+ z: f64[offset + 10],
420
+ },
421
+ childIndex: Math.trunc(f64[offset + 11]),
422
+ };
423
+ }
424
+
425
+ export function packRaycastHit(hit, f64, offset = 0) {
426
+ f64[offset + 0] = hit.hit ? 1.0 : 0.0;
427
+ f64[offset + 1] = Number(hit.distance ?? 0.0);
428
+ f64[offset + 2] = Number(hit.voxelCoords?.x ?? 0);
429
+ f64[offset + 3] = Number(hit.voxelCoords?.y ?? 0);
430
+ f64[offset + 4] = Number(hit.voxelCoords?.z ?? 0);
431
+ f64[offset + 5] = Number(hit.point?.x ?? 0.0);
432
+ f64[offset + 6] = Number(hit.point?.y ?? 0.0);
433
+ f64[offset + 7] = Number(hit.point?.z ?? 0.0);
434
+ f64[offset + 8] = Number(hit.normal?.x ?? 0.0);
435
+ f64[offset + 9] = Number(hit.normal?.y ?? 0.0);
436
+ f64[offset + 10] = Number(hit.normal?.z ?? 0.0);
437
+ f64[offset + 11] = Number(hit.childIndex ?? -1);
438
+ }
439
+
440
+ export function unpackSeamResult(f64, offset = 0) {
441
+ return {
442
+ hasSeam: f64[offset + 0] !== 0.0,
443
+ seamAxis: Math.trunc(f64[offset + 1]),
444
+ seamCoord: Math.trunc(f64[offset + 2]),
445
+ sharedFaceCount: Math.trunc(f64[offset + 3]),
446
+ exposedFaceCountA: Math.trunc(f64[offset + 4]),
447
+ exposedFaceCountB: Math.trunc(f64[offset + 5]),
448
+ seamArea: f64[offset + 6],
449
+ seamCentroid: {
450
+ x: f64[offset + 7],
451
+ y: f64[offset + 8],
452
+ z: f64[offset + 9],
453
+ },
454
+ seamNormal: {
455
+ x: f64[offset + 10],
456
+ y: f64[offset + 11],
457
+ z: f64[offset + 12],
458
+ },
459
+ isWatertight: f64[offset + 13] !== 0.0,
460
+ seamBounds: {
461
+ minX: Math.trunc(f64[offset + 14]),
462
+ minY: Math.trunc(f64[offset + 15]),
463
+ minZ: Math.trunc(f64[offset + 16]),
464
+ maxX: Math.trunc(f64[offset + 17]),
465
+ maxY: Math.trunc(f64[offset + 18]),
466
+ maxZ: Math.trunc(f64[offset + 19]),
467
+ },
468
+ };
469
+ }
470
+
471
+ export function packSeamResult(result, f64, offset = 0) {
472
+ f64[offset + 0] = result.hasSeam ? 1.0 : 0.0;
473
+ f64[offset + 1] = Number(result.seamAxis ?? -1);
474
+ f64[offset + 2] = Number(result.seamCoord ?? 0);
475
+ f64[offset + 3] = Number(result.sharedFaceCount ?? 0);
476
+ f64[offset + 4] = Number(result.exposedFaceCountA ?? 0);
477
+ f64[offset + 5] = Number(result.exposedFaceCountB ?? 0);
478
+ f64[offset + 6] = Number(result.seamArea ?? 0.0);
479
+ f64[offset + 7] = Number(result.seamCentroid?.x ?? 0.0);
480
+ f64[offset + 8] = Number(result.seamCentroid?.y ?? 0.0);
481
+ f64[offset + 9] = Number(result.seamCentroid?.z ?? 0.0);
482
+ f64[offset + 10] = Number(result.seamNormal?.x ?? 0.0);
483
+ f64[offset + 11] = Number(result.seamNormal?.y ?? 0.0);
484
+ f64[offset + 12] = Number(result.seamNormal?.z ?? 0.0);
485
+ f64[offset + 13] = result.isWatertight ? 1.0 : 0.0;
486
+ f64[offset + 14] = Number(result.seamBounds?.minX ?? 0);
487
+ f64[offset + 15] = Number(result.seamBounds?.minY ?? 0);
488
+ f64[offset + 16] = Number(result.seamBounds?.minZ ?? 0);
489
+ f64[offset + 17] = Number(result.seamBounds?.maxX ?? 0);
490
+ f64[offset + 18] = Number(result.seamBounds?.maxY ?? 0);
491
+ f64[offset + 19] = Number(result.seamBounds?.maxZ ?? 0);
492
+ }
493
+
494
+ export function cookVoxelChunk(chunk, options = {}, instanceOverride = null) {
495
+ validateChunk(chunk);
496
+ const instance = getVoxelInstance(instanceOverride);
497
+ const exp = instance.exports;
498
+
499
+ const dimX = Math.trunc(chunk.dimX);
500
+ const dimY = Math.trunc(chunk.dimY);
501
+ const dimZ = Math.trunc(chunk.dimZ);
502
+ const cellSize = Number(chunk.cellSize ?? 1.0);
503
+ const totalCells = dimX * dimY * dimZ;
504
+
505
+ const childStride = exp.rundot_kinetix_ext_voxel_child_stride();
506
+ const maxChildren = options.maxChildren ?? Math.min(totalCells, 32768);
507
+ const nextChildIdInput = validateCookOptions(options, maxChildren);
508
+
509
+ const occupancyBytes = totalCells;
510
+ const outChildrenBytes = maxChildren * childStride * 8;
511
+ const outCountBytes = 4;
512
+ const nextIdBytes = 4;
513
+ const scratchBytes = exp.rundot_kinetix_ext_voxel_scratch_size(dimX, dimY, dimZ);
514
+
515
+ const totalBufferBytes = occupancyBytes + 16 + outChildrenBytes + 16 + outCountBytes + nextIdBytes + 16 + scratchBytes + 64;
516
+
517
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
518
+ let memBuf = exp.memory.buffer;
519
+
520
+ let currPtr = basePtr;
521
+ const occPtr = currPtr;
522
+ currPtr += occupancyBytes;
523
+
524
+ currPtr = (currPtr + 15) & ~15;
525
+ const outChildrenPtr = currPtr;
526
+ currPtr += outChildrenBytes;
527
+
528
+ currPtr = (currPtr + 15) & ~15;
529
+ const outCountPtr = currPtr;
530
+ currPtr += outCountBytes;
531
+
532
+ const nextIdPtr = currPtr;
533
+ currPtr += nextIdBytes;
534
+
535
+ currPtr = (currPtr + 15) & ~15;
536
+ const scratchPtr = currPtr;
537
+
538
+ const occView = new Uint8Array(memBuf, occPtr, occupancyBytes);
539
+ occView.set(chunk.occupancy);
540
+
541
+ new Uint32Array(memBuf, outCountPtr, 1)[0] = 0;
542
+ new Uint32Array(memBuf, nextIdPtr, 1)[0] = nextChildIdInput;
543
+
544
+ const status = exp.rundot_kinetix_ext_voxel_cook_chunk(
545
+ dimX, dimY, dimZ, cellSize,
546
+ occPtr,
547
+ outChildrenPtr,
548
+ maxChildren,
549
+ outCountPtr,
550
+ nextIdPtr,
551
+ scratchPtr
552
+ );
553
+ requireVoxelStatus(status);
554
+ memBuf = exp.memory.buffer;
555
+
556
+ const childCount = new Uint32Array(memBuf, outCountPtr, 1)[0];
557
+ const updatedNextChildId = new Uint32Array(memBuf, nextIdPtr, 1)[0];
558
+
559
+ const outF64 = new Float64Array(memBuf, outChildrenPtr, childCount * childStride);
560
+ const children = new Array(childCount);
561
+ for (let i = 0; i < childCount; i++) {
562
+ children[i] = unpackBoxChild(outF64, i * childStride);
563
+ }
564
+
565
+ return {
566
+ status,
567
+ children,
568
+ childCount,
569
+ nextChildId: updatedNextChildId,
570
+ };
571
+ });
572
+ }
573
+
574
+ export function carveVoxelChunk(chunk, carveRegion, options = {}, instanceOverride = null) {
575
+ validateChunk(chunk);
576
+ validateCarveRegion(carveRegion);
577
+ const instance = getVoxelInstance(instanceOverride);
578
+ if (carveRegion.shapeType === 'swept_quad' || carveRegion.shapeType === VoxelCarveShapeType.SweptQuad) {
579
+ if (options === null || typeof options !== 'object' || Array.isArray(options)) {
580
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT options');
581
+ }
582
+ return carveSweptQuad(chunk, carveRegion, options);
583
+ }
584
+ const exp = instance.exports;
585
+
586
+ const dimX = Math.trunc(chunk.dimX);
587
+ const dimY = Math.trunc(chunk.dimY);
588
+ const dimZ = Math.trunc(chunk.dimZ);
589
+ const cellSize = Number(chunk.cellSize ?? 1.0);
590
+ const totalCells = dimX * dimY * dimZ;
591
+
592
+ let shapeType = VoxelCarveShapeType.Box;
593
+ if (typeof carveRegion.shapeType === 'number') {
594
+ shapeType = carveRegion.shapeType;
595
+ } else if (typeof carveRegion.shapeType === 'string') {
596
+ const lower = carveRegion.shapeType.toLowerCase();
597
+ if (lower === 'sphere') shapeType = VoxelCarveShapeType.Sphere;
598
+ else if (lower === 'capsule') shapeType = VoxelCarveShapeType.Capsule;
599
+ }
600
+
601
+ let op = VoxelCarveOp.Carve;
602
+ if (typeof carveRegion.op === 'number') {
603
+ op = carveRegion.op;
604
+ } else if (typeof carveRegion.op === 'string') {
605
+ const lower = carveRegion.op.toLowerCase();
606
+ if (lower === 'fill') op = VoxelCarveOp.Fill;
607
+ }
608
+
609
+ const occupancyBytes = totalCells;
610
+ const carveParamsBytes = 19 * 8; // 19 doubles
611
+ const dirtyAABBBytes = 6 * 4; // 6 int32s
612
+ const changedBytes = 4; // 1 int32
613
+
614
+ const totalBufferBytes = occupancyBytes + 16 + carveParamsBytes + 16 + dirtyAABBBytes + changedBytes + 32;
615
+
616
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
617
+ let memBuf = exp.memory.buffer;
618
+
619
+ let currPtr = basePtr;
620
+ const occPtr = currPtr;
621
+ currPtr += occupancyBytes;
622
+
623
+ currPtr = (currPtr + 15) & ~15;
624
+ const paramsPtr = currPtr;
625
+ currPtr += carveParamsBytes;
626
+
627
+ currPtr = (currPtr + 15) & ~15;
628
+ const dirtyPtr = currPtr;
629
+ currPtr += dirtyAABBBytes;
630
+
631
+ const changedPtr = currPtr;
632
+
633
+ const occView = new Uint8Array(memBuf, occPtr, occupancyBytes);
634
+ occView.set(chunk.occupancy);
635
+
636
+ const box = carveRegion.box ?? {};
637
+ const sphereCenter = carveRegion.sphereCenter ?? {};
638
+ const capsuleP0 = carveRegion.capsuleP0 ?? {};
639
+ const capsuleP1 = carveRegion.capsuleP1 ?? {};
640
+
641
+ const pView = new Float64Array(memBuf, paramsPtr, 19);
642
+ pView[0] = shapeType;
643
+ pView[1] = op;
644
+ pView[2] = Number(box.minX ?? 0);
645
+ pView[3] = Number(box.minY ?? 0);
646
+ pView[4] = Number(box.minZ ?? 0);
647
+ pView[5] = Number(box.maxX ?? 0);
648
+ pView[6] = Number(box.maxY ?? 0);
649
+ pView[7] = Number(box.maxZ ?? 0);
650
+ pView[8] = Number(sphereCenter.x ?? 0.0);
651
+ pView[9] = Number(sphereCenter.y ?? 0.0);
652
+ pView[10] = Number(sphereCenter.z ?? 0.0);
653
+ pView[11] = Number(carveRegion.sphereRadius ?? 0.0);
654
+ pView[12] = Number(capsuleP0.x ?? 0.0);
655
+ pView[13] = Number(capsuleP0.y ?? 0.0);
656
+ pView[14] = Number(capsuleP0.z ?? 0.0);
657
+ pView[15] = Number(capsuleP1.x ?? 0.0);
658
+ pView[16] = Number(capsuleP1.y ?? 0.0);
659
+ pView[17] = Number(capsuleP1.z ?? 0.0);
660
+ pView[18] = Number(carveRegion.capsuleRadius ?? 0.0);
661
+
662
+ new Int32Array(memBuf, dirtyPtr, 6).fill(0);
663
+ new Int32Array(memBuf, changedPtr, 1)[0] = 0;
664
+
665
+ const status = exp.rundot_kinetix_ext_voxel_carve(
666
+ dimX, dimY, dimZ, cellSize,
667
+ occPtr,
668
+ paramsPtr,
669
+ dirtyPtr,
670
+ changedPtr
671
+ );
672
+ requireVoxelStatus(status);
673
+ memBuf = exp.memory.buffer;
674
+
675
+ const changedVal = new Int32Array(memBuf, changedPtr, 1)[0] !== 0;
676
+ const dirtyArr = new Int32Array(memBuf, dirtyPtr, 6);
677
+ const dirtyAABB = {
678
+ minX: dirtyArr[0],
679
+ minY: dirtyArr[1],
680
+ minZ: dirtyArr[2],
681
+ maxX: dirtyArr[3],
682
+ maxY: dirtyArr[4],
683
+ maxZ: dirtyArr[5],
684
+ };
685
+
686
+ const updatedOccupancy = new Uint8Array(totalCells);
687
+ updatedOccupancy.set(new Uint8Array(exp.memory.buffer, occPtr, occupancyBytes));
688
+ if (chunk.occupancy instanceof Uint8Array && chunk.occupancy.length >= totalCells && options.mutateInPlace !== false) {
689
+ chunk.occupancy.set(updatedOccupancy);
690
+ }
691
+
692
+ return {
693
+ status,
694
+ changed: changedVal,
695
+ dirtyAABB,
696
+ occupancy: updatedOccupancy,
697
+ };
698
+ });
699
+ }
700
+
701
+ export function incrementalCookVoxel(chunk, existingChildren, dirtyAABB, options = {}, instanceOverride = null) {
702
+ validateChunk(chunk);
703
+ validateChildren(existingChildren);
704
+ validateBounds(dirtyAABB, 'dirtyAABB');
705
+ const instance = getVoxelInstance(instanceOverride);
706
+ const exp = instance.exports;
707
+
708
+ const dimX = Math.trunc(chunk.dimX);
709
+ const dimY = Math.trunc(chunk.dimY);
710
+ const dimZ = Math.trunc(chunk.dimZ);
711
+ const cellSize = Number(chunk.cellSize ?? 1.0);
712
+ const totalCells = dimX * dimY * dimZ;
713
+
714
+ const childStride = exp.rundot_kinetix_ext_voxel_child_stride();
715
+ const statsStride = exp.rundot_kinetix_ext_voxel_stats_stride();
716
+
717
+ const existingCount = existingChildren.length;
718
+ const maxOutChildren = options.maxChildren ?? Math.min(totalCells + existingCount, 32768);
719
+ const nextChildIdInput = validateCookOptions(options, maxOutChildren, existingChildren);
720
+
721
+ const occupancyBytes = totalCells;
722
+ const existingBytes = existingCount * childStride * 8;
723
+ const dirtyBytes = 6 * 4;
724
+ const outChildrenBytes = maxOutChildren * childStride * 8;
725
+ const outCountBytes = 4;
726
+ const nextIdBytes = 4;
727
+ const statsBytes = statsStride * 8;
728
+ const scratchBytes = exp.rundot_kinetix_ext_voxel_scratch_size(dimX, dimY, dimZ);
729
+
730
+ const totalBufferBytes = occupancyBytes + 16 + existingBytes + 16 + dirtyBytes + 16 + outChildrenBytes + 16 +
731
+ outCountBytes + nextIdBytes + 16 + statsBytes + 16 + scratchBytes + 64;
732
+
733
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
734
+ let memBuf = exp.memory.buffer;
735
+
736
+ let currPtr = basePtr;
737
+ const occPtr = currPtr;
738
+ currPtr += occupancyBytes;
739
+
740
+ currPtr = (currPtr + 15) & ~15;
741
+ const existPtr = currPtr;
742
+ currPtr += existingBytes;
743
+
744
+ currPtr = (currPtr + 15) & ~15;
745
+ const dirtyPtr = currPtr;
746
+ currPtr += dirtyBytes;
747
+
748
+ currPtr = (currPtr + 15) & ~15;
749
+ const outChildrenPtr = currPtr;
750
+ currPtr += outChildrenBytes;
751
+
752
+ currPtr = (currPtr + 15) & ~15;
753
+ const outCountPtr = currPtr;
754
+ currPtr += outCountBytes;
755
+
756
+ const nextIdPtr = currPtr;
757
+ currPtr += nextIdBytes;
758
+
759
+ currPtr = (currPtr + 15) & ~15;
760
+ const statsPtr = currPtr;
761
+ currPtr += statsBytes;
762
+
763
+ currPtr = (currPtr + 15) & ~15;
764
+ const scratchPtr = currPtr;
765
+
766
+ const occView = new Uint8Array(memBuf, occPtr, occupancyBytes);
767
+ occView.set(chunk.occupancy);
768
+
769
+ const existView = new Float64Array(memBuf, existPtr, existingCount * childStride);
770
+ for (let i = 0; i < existingCount; i++) {
771
+ packBoxChild(existingChildren[i], existView, i * childStride);
772
+ }
773
+
774
+ const dirtyView = new Int32Array(memBuf, dirtyPtr, 6);
775
+ if (Array.isArray(dirtyAABB)) {
776
+ dirtyView.set(dirtyAABB.slice(0, 6));
777
+ } else {
778
+ dirtyView[0] = Number(dirtyAABB?.minX ?? 0);
779
+ dirtyView[1] = Number(dirtyAABB?.minY ?? 0);
780
+ dirtyView[2] = Number(dirtyAABB?.minZ ?? 0);
781
+ dirtyView[3] = Number(dirtyAABB?.maxX ?? 0);
782
+ dirtyView[4] = Number(dirtyAABB?.maxY ?? 0);
783
+ dirtyView[5] = Number(dirtyAABB?.maxZ ?? 0);
784
+ }
785
+
786
+ new Uint32Array(memBuf, outCountPtr, 1)[0] = 0;
787
+ new Uint32Array(memBuf, nextIdPtr, 1)[0] = nextChildIdInput;
788
+
789
+ const status = exp.rundot_kinetix_ext_voxel_incremental_cook(
790
+ dimX, dimY, dimZ, cellSize,
791
+ occPtr,
792
+ existPtr,
793
+ existingCount,
794
+ dirtyPtr,
795
+ outChildrenPtr,
796
+ maxOutChildren,
797
+ outCountPtr,
798
+ nextIdPtr,
799
+ statsPtr,
800
+ scratchPtr
801
+ );
802
+ requireVoxelStatus(status);
803
+ memBuf = exp.memory.buffer;
804
+
805
+ const childCount = new Uint32Array(memBuf, outCountPtr, 1)[0];
806
+ const updatedNextChildId = new Uint32Array(memBuf, nextIdPtr, 1)[0];
807
+
808
+ const outF64 = new Float64Array(memBuf, outChildrenPtr, childCount * childStride);
809
+ const children = new Array(childCount);
810
+ for (let i = 0; i < childCount; i++) {
811
+ children[i] = unpackBoxChild(outF64, i * childStride);
812
+ }
813
+
814
+ const statsF64 = new Float64Array(memBuf, statsPtr, statsStride);
815
+ const stats = unpackCookStats(statsF64, 0);
816
+
817
+ return {
818
+ status,
819
+ children,
820
+ childCount,
821
+ nextChildId: updatedNextChildId,
822
+ stats,
823
+ };
824
+ });
825
+ }
826
+
827
+ export function splitVoxelComponents(chunk, options = {}, instanceOverride = null) {
828
+ validateChunk(chunk);
829
+ validateSplitOptions(options);
830
+ const instance = getVoxelInstance(instanceOverride);
831
+ const exp = instance.exports;
832
+
833
+ const dimX = Math.trunc(chunk.dimX);
834
+ const dimY = Math.trunc(chunk.dimY);
835
+ const dimZ = Math.trunc(chunk.dimZ);
836
+ const cellSize = Number(chunk.cellSize ?? 1.0);
837
+ const totalCells = dimX * dimY * dimZ;
838
+
839
+ const subChunkStride = exp.rundot_kinetix_ext_voxel_subchunk_stride();
840
+ const maxSubChunks = options.maxSubChunks ?? 1024;
841
+ const maxSubOccBytes = options.maxSubOccupancyBytes ?? Math.max(totalCells * 2, 4096);
842
+ const anchorFacesMask = options.anchorFacesMask ?? VoxelAnchorFaceMask.AnchorNone;
843
+
844
+ const occupancyBytes = totalCells;
845
+ const outSubChunksBytes = maxSubChunks * subChunkStride * 8;
846
+ const outCountBytes = 4;
847
+ const outOccUsedBytes = 4;
848
+ const scratchBytes = exp.rundot_kinetix_ext_voxel_scratch_size(dimX, dimY, dimZ);
849
+
850
+ const totalBufferBytes = occupancyBytes + 16 + outSubChunksBytes + 16 + outCountBytes + 16 +
851
+ maxSubOccBytes + 16 + outOccUsedBytes + 16 + scratchBytes + 64;
852
+
853
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
854
+ let memBuf = exp.memory.buffer;
855
+
856
+ let currPtr = basePtr;
857
+ const occPtr = currPtr;
858
+ currPtr += occupancyBytes;
859
+
860
+ currPtr = (currPtr + 15) & ~15;
861
+ const outSubChunksPtr = currPtr;
862
+ currPtr += outSubChunksBytes;
863
+
864
+ currPtr = (currPtr + 15) & ~15;
865
+ const outCountPtr = currPtr;
866
+ currPtr += outCountBytes;
867
+
868
+ currPtr = (currPtr + 15) & ~15;
869
+ const outSubOccPtr = currPtr;
870
+ currPtr += maxSubOccBytes;
871
+
872
+ currPtr = (currPtr + 15) & ~15;
873
+ const outOccUsedPtr = currPtr;
874
+ currPtr += outOccUsedBytes;
875
+
876
+ currPtr = (currPtr + 15) & ~15;
877
+ const scratchPtr = currPtr;
878
+
879
+ const occView = new Uint8Array(memBuf, occPtr, occupancyBytes);
880
+ occView.set(chunk.occupancy);
881
+
882
+ new Uint32Array(memBuf, outCountPtr, 1)[0] = 0;
883
+ new Uint32Array(memBuf, outOccUsedPtr, 1)[0] = 0;
884
+
885
+ const status = exp.rundot_kinetix_ext_voxel_split_components(
886
+ dimX, dimY, dimZ, cellSize,
887
+ occPtr,
888
+ anchorFacesMask,
889
+ outSubChunksPtr,
890
+ maxSubChunks,
891
+ outCountPtr,
892
+ outSubOccPtr,
893
+ maxSubOccBytes,
894
+ outOccUsedPtr,
895
+ scratchPtr
896
+ );
897
+ requireVoxelStatus(status);
898
+ memBuf = exp.memory.buffer;
899
+
900
+ const subChunkCount = new Uint32Array(memBuf, outCountPtr, 1)[0];
901
+ const occBytesUsed = new Uint32Array(memBuf, outOccUsedPtr, 1)[0];
902
+
903
+ const scF64 = new Float64Array(memBuf, outSubChunksPtr, subChunkCount * subChunkStride);
904
+ const subOccRaw = new Uint8Array(memBuf, outSubOccPtr, occBytesUsed);
905
+
906
+ const subChunks = new Array(subChunkCount);
907
+ let occOffset = 0;
908
+ for (let i = 0; i < subChunkCount; i++) {
909
+ const sc = unpackSubChunk(scF64, i * subChunkStride);
910
+ const scTotal = sc.dimX * sc.dimY * sc.dimZ;
911
+ if (occOffset + scTotal <= occBytesUsed) {
912
+ sc.occupancy = new Uint8Array(subOccRaw.buffer, subOccRaw.byteOffset + occOffset, scTotal).slice();
913
+ occOffset += scTotal;
914
+ } else {
915
+ throw new Error('KINETIX_VOXEL_NATIVE_OUTPUT_INVALID occupancy');
916
+ }
917
+ subChunks[i] = sc;
918
+ }
919
+
920
+ return {
921
+ status,
922
+ subChunks,
923
+ subChunkCount,
924
+ subOccupancyBytesUsed: occBytesUsed,
925
+ rawSubOccupancies: new Uint8Array(subOccRaw),
926
+ };
927
+ });
928
+ }
929
+
930
+ export function incrementalSliceCompound(parentChildren, subChunks, options = {}, instanceOverride = null) {
931
+ validateChildren(parentChildren);
932
+ if (!Array.isArray(subChunks)) throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT subChunks');
933
+ for (const subChunk of subChunks) {
934
+ validateChunk(subChunk);
935
+ validateVector(subChunk.offsetCells, 'offsetCells', true);
936
+ requireInteger(subChunk.subChunkId, 0, 0xffffffff, 'subChunkId');
937
+ requireInteger(subChunk.occupiedCount, 0, subChunk.occupancy.length, 'occupiedCount');
938
+ if (typeof subChunk.anchored !== 'boolean') throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT anchored');
939
+ }
940
+ const instance = getVoxelInstance(instanceOverride);
941
+ const exp = instance.exports;
942
+
943
+ const childStride = exp.rundot_kinetix_ext_voxel_child_stride();
944
+ const subChunkStride = exp.rundot_kinetix_ext_voxel_subchunk_stride();
945
+
946
+ const parentCount = parentChildren.length;
947
+ const scCount = subChunks.length;
948
+ const cellSize = Number(options.cellSize ?? (subChunks[0]?.cellSize ?? 1.0));
949
+ const maxOutSubChildren = options.maxOutSubChildren ?? Math.max(parentCount * 4, 4096);
950
+ const nextChildIdInput = validateCookOptions(options, maxOutSubChildren, parentChildren);
951
+ if (!Number.isFinite(cellSize) || cellSize <= 0 || subChunks.some((chunk) => chunk.cellSize !== cellSize)) {
952
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT cellSize');
953
+ }
954
+
955
+ const totalSubOccBytes = subChunks.reduce((total, chunk) => total + chunk.occupancy.length, 0);
956
+
957
+ const pChildrenBytes = parentCount * childStride * 8;
958
+ const scBytes = scCount * subChunkStride * 8;
959
+ const subOccBytes = Math.max(totalSubOccBytes, 16);
960
+ const outChildrenBytes = maxOutSubChildren * childStride * 8;
961
+ const outCountsBytes = scCount * 4;
962
+ const nextIdBytes = 4;
963
+ const presBytes = 4;
964
+ const recBytes = 4;
965
+ const scratchBytes = Math.max(16, ...subChunks.map((chunk) => chunk.occupancy.length));
966
+
967
+ const totalBufferBytes = pChildrenBytes + 16 + scBytes + 16 + subOccBytes + 16 + outChildrenBytes + 16 +
968
+ outCountsBytes + 16 + nextIdBytes + presBytes + recBytes + 16 + scratchBytes + 64;
969
+
970
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
971
+ let memBuf = exp.memory.buffer;
972
+
973
+ let currPtr = basePtr;
974
+ const pChildrenPtr = currPtr;
975
+ currPtr += pChildrenBytes;
976
+
977
+ currPtr = (currPtr + 15) & ~15;
978
+ const scPtr = currPtr;
979
+ currPtr += scBytes;
980
+
981
+ currPtr = (currPtr + 15) & ~15;
982
+ const subOccPtr = currPtr;
983
+ currPtr += subOccBytes;
984
+
985
+ currPtr = (currPtr + 15) & ~15;
986
+ const outChildrenPtr = currPtr;
987
+ currPtr += outChildrenBytes;
988
+
989
+ currPtr = (currPtr + 15) & ~15;
990
+ const outCountsPtr = currPtr;
991
+ currPtr += outCountsBytes;
992
+
993
+ const nextIdPtr = currPtr;
994
+ currPtr += nextIdBytes;
995
+
996
+ const presPtr = currPtr;
997
+ currPtr += presBytes;
998
+
999
+ const recPtr = currPtr;
1000
+ currPtr += recBytes;
1001
+
1002
+ currPtr = (currPtr + 15) & ~15;
1003
+ const scratchPtr = currPtr;
1004
+
1005
+ const pView = new Float64Array(memBuf, pChildrenPtr, parentCount * childStride);
1006
+ for (let i = 0; i < parentCount; i++) {
1007
+ packBoxChild(parentChildren[i], pView, i * childStride);
1008
+ }
1009
+
1010
+ const scView = new Float64Array(memBuf, scPtr, scCount * subChunkStride);
1011
+ const subOccView = new Uint8Array(memBuf, subOccPtr, subOccBytes);
1012
+ let occOffset = 0;
1013
+
1014
+ for (let i = 0; i < scCount; i++) {
1015
+ const sc = subChunks[i];
1016
+ packSubChunk(sc, scView, i * subChunkStride);
1017
+ const scCells = sc.dimX * sc.dimY * sc.dimZ;
1018
+ if (sc.occupancy instanceof Uint8Array) {
1019
+ subOccView.set(sc.occupancy.subarray(0, scCells), occOffset);
1020
+ }
1021
+ occOffset += scCells;
1022
+ }
1023
+
1024
+
1025
+ new Uint32Array(memBuf, outCountsPtr, scCount).fill(0);
1026
+ new Uint32Array(memBuf, nextIdPtr, 1)[0] = nextChildIdInput;
1027
+ new Uint32Array(memBuf, presPtr, 1)[0] = 0;
1028
+ new Uint32Array(memBuf, recPtr, 1)[0] = 0;
1029
+
1030
+ const status = exp.rundot_kinetix_ext_voxel_incremental_slice(
1031
+ pChildrenPtr,
1032
+ parentCount,
1033
+ cellSize,
1034
+ scPtr,
1035
+ scCount,
1036
+ subOccPtr,
1037
+ outChildrenPtr,
1038
+ maxOutSubChildren,
1039
+ outCountsPtr,
1040
+ nextIdPtr,
1041
+ presPtr,
1042
+ recPtr,
1043
+ scratchPtr
1044
+ );
1045
+ requireVoxelStatus(status);
1046
+ memBuf = exp.memory.buffer;
1047
+
1048
+ const counts = Array.from(new Uint32Array(memBuf, outCountsPtr, scCount));
1049
+ const preservedTotal = new Uint32Array(memBuf, presPtr, 1)[0];
1050
+ const recookedTotal = new Uint32Array(memBuf, recPtr, 1)[0];
1051
+ const updatedNextChildId = new Uint32Array(memBuf, nextIdPtr, 1)[0];
1052
+
1053
+ const totalOutBoxes = counts.reduce((sum, c) => sum + c, 0);
1054
+ const outF64 = new Float64Array(memBuf, outChildrenPtr, totalOutBoxes * childStride);
1055
+
1056
+ const subChildren = new Array(scCount);
1057
+ let boxCursor = 0;
1058
+ for (let s = 0; s < scCount; s++) {
1059
+ const c = counts[s];
1060
+ const list = new Array(c);
1061
+ for (let b = 0; b < c; b++) {
1062
+ list[b] = unpackBoxChild(outF64, (boxCursor + b) * childStride);
1063
+ }
1064
+ subChildren[s] = list;
1065
+ boxCursor += c;
1066
+ }
1067
+
1068
+ const flatSubChildren = new Array(totalOutBoxes);
1069
+ for (let i = 0; i < totalOutBoxes; i++) {
1070
+ flatSubChildren[i] = unpackBoxChild(outF64, i * childStride);
1071
+ }
1072
+
1073
+ return {
1074
+ status,
1075
+ subChildren,
1076
+ flatSubChildren,
1077
+ subChildCounts: counts,
1078
+ preservedTotal,
1079
+ recookedTotal,
1080
+ nextChildId: updatedNextChildId,
1081
+ };
1082
+ });
1083
+ }
1084
+
1085
+ export function raycastVoxelDDA(chunk, query, instanceOverride = null) {
1086
+ validateChunk(chunk);
1087
+ validateVector(query?.origin, 'origin');
1088
+ validateVector(query?.direction, 'direction');
1089
+ const directionLength = Math.hypot(query.direction.x, query.direction.y, query.direction.z);
1090
+ if (!Number.isFinite(directionLength) || directionLength <= 0
1091
+ || !Number.isFinite(query.maxDistance) || query.maxDistance < 0
1092
+ || !Number.isFinite(query.maxDistance / chunk.cellSize)
1093
+ || ['x', 'y', 'z'].some((axis) => !Number.isFinite(query.origin[axis] / chunk.cellSize))) {
1094
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT ray');
1095
+ }
1096
+ const instance = getVoxelInstance(instanceOverride);
1097
+ const exp = instance.exports;
1098
+
1099
+ const dimX = Math.trunc(chunk.dimX);
1100
+ const dimY = Math.trunc(chunk.dimY);
1101
+ const dimZ = Math.trunc(chunk.dimZ);
1102
+ const cellSize = Number(chunk.cellSize ?? 1.0);
1103
+ const totalCells = dimX * dimY * dimZ;
1104
+
1105
+ const hitStride = exp.rundot_kinetix_ext_voxel_ray_hit_stride();
1106
+
1107
+ const occupancyBytes = totalCells;
1108
+ const rayParamsBytes = 7 * 8;
1109
+ const outHitBytes = hitStride * 8;
1110
+
1111
+ const totalBufferBytes = occupancyBytes + 16 + rayParamsBytes + 16 + outHitBytes + 32;
1112
+
1113
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
1114
+ let memBuf = exp.memory.buffer;
1115
+
1116
+ let currPtr = basePtr;
1117
+ const occPtr = currPtr;
1118
+ currPtr += occupancyBytes;
1119
+
1120
+ currPtr = (currPtr + 15) & ~15;
1121
+ const paramsPtr = currPtr;
1122
+ currPtr += rayParamsBytes;
1123
+
1124
+ currPtr = (currPtr + 15) & ~15;
1125
+ const outHitPtr = currPtr;
1126
+
1127
+ const occView = new Uint8Array(memBuf, occPtr, occupancyBytes);
1128
+ occView.set(chunk.occupancy);
1129
+
1130
+ const ox = Number(query.origin?.x ?? query.x ?? 0.0);
1131
+ const oy = Number(query.origin?.y ?? query.y ?? 0.0);
1132
+ const oz = Number(query.origin?.z ?? query.z ?? 0.0);
1133
+ const dx = Number(query.direction?.x ?? query.dx ?? 0.0);
1134
+ const dy = Number(query.direction?.y ?? query.dy ?? 0.0);
1135
+ const dz = Number(query.direction?.z ?? query.dz ?? 1.0);
1136
+ const maxDist = Number(query.maxDistance ?? 1e30);
1137
+
1138
+ const rView = new Float64Array(memBuf, paramsPtr, 7);
1139
+ rView[0] = ox;
1140
+ rView[1] = oy;
1141
+ rView[2] = oz;
1142
+ rView[3] = dx;
1143
+ rView[4] = dy;
1144
+ rView[5] = dz;
1145
+ rView[6] = maxDist;
1146
+
1147
+ const hitF64 = new Float64Array(memBuf, outHitPtr, hitStride);
1148
+ hitF64.fill(0);
1149
+
1150
+ const status = exp.rundot_kinetix_ext_voxel_raycast_dda(
1151
+ dimX, dimY, dimZ, cellSize,
1152
+ occPtr,
1153
+ paramsPtr,
1154
+ outHitPtr
1155
+ );
1156
+ requireVoxelStatus(status);
1157
+ memBuf = exp.memory.buffer;
1158
+
1159
+ const hit = unpackRaycastHit(new Float64Array(memBuf, outHitPtr, hitStride), 0);
1160
+
1161
+ return {
1162
+ status,
1163
+ hit,
1164
+ };
1165
+ });
1166
+ }
1167
+
1168
+ export function queryVoxelSeam(chunkA, offsetA, chunkB, offsetB, options = {}, instanceOverride = null) {
1169
+ validateChunk(chunkA);
1170
+ validateChunk(chunkB);
1171
+ validateVector(offsetA, 'offsetA', true);
1172
+ validateVector(offsetB, 'offsetB', true);
1173
+ requireInteger(options.cutPlaneAxis ?? -1, -1, 2, 'cutPlaneAxis');
1174
+ requireInteger(options.cutCoordinate ?? 0, -2147482623, 2147482623, 'cutCoordinate');
1175
+ const instance = getVoxelInstance(instanceOverride);
1176
+ const exp = instance.exports;
1177
+
1178
+ const dimAX = Math.trunc(chunkA.dimX);
1179
+ const dimAY = Math.trunc(chunkA.dimY);
1180
+ const dimAZ = Math.trunc(chunkA.dimZ);
1181
+ const cellA = Number(chunkA.cellSize ?? 1.0);
1182
+ const totalA = dimAX * dimAY * dimAZ;
1183
+
1184
+ const dimBX = Math.trunc(chunkB.dimX);
1185
+ const dimBY = Math.trunc(chunkB.dimY);
1186
+ const dimBZ = Math.trunc(chunkB.dimZ);
1187
+ const cellB = Number(chunkB.cellSize ?? 1.0);
1188
+ const totalB = dimBX * dimBY * dimBZ;
1189
+
1190
+ const seamResultStride = exp.rundot_kinetix_ext_voxel_seam_result_stride();
1191
+
1192
+ const occABytes = totalA;
1193
+ const offABytes = 3 * 4;
1194
+ const occBBytes = totalB;
1195
+ const offBBytes = 3 * 4;
1196
+ const outResultBytes = seamResultStride * 8;
1197
+
1198
+ const cutAxis = options.cutPlaneAxis ?? -1;
1199
+ const cutCoord = options.cutCoordinate ?? 0;
1200
+
1201
+ const totalBufferBytes = occABytes + 16 + offABytes + 16 + occBBytes + 16 + offBBytes + 16 + outResultBytes + 32;
1202
+
1203
+ return withVoxelScratch(instance, totalBufferBytes, (basePtr) => {
1204
+ let memBuf = exp.memory.buffer;
1205
+
1206
+ let currPtr = basePtr;
1207
+ const occAPtr = currPtr;
1208
+ currPtr += occABytes;
1209
+
1210
+ currPtr = (currPtr + 15) & ~15;
1211
+ const offAPtr = currPtr;
1212
+ currPtr += offABytes;
1213
+
1214
+ currPtr = (currPtr + 15) & ~15;
1215
+ const occBPtr = currPtr;
1216
+ currPtr += occBBytes;
1217
+
1218
+ currPtr = (currPtr + 15) & ~15;
1219
+ const offBPtr = currPtr;
1220
+ currPtr += offBBytes;
1221
+
1222
+ currPtr = (currPtr + 15) & ~15;
1223
+ const outResultPtr = currPtr;
1224
+
1225
+ const occAView = new Uint8Array(memBuf, occAPtr, occABytes);
1226
+ if (chunkA.occupancy instanceof Uint8Array) {
1227
+ occAView.set(chunkA.occupancy.subarray(0, totalA));
1228
+ } else if (Array.isArray(chunkA.occupancy) || ArrayBuffer.isView(chunkA.occupancy)) {
1229
+ for (let i = 0; i < totalA; i++) occAView[i] = chunkA.occupancy[i] ? 1 : 0;
1230
+ } else {
1231
+ occAView.fill(0);
1232
+ }
1233
+
1234
+ const offAView = new Int32Array(memBuf, offAPtr, 3);
1235
+ if (Array.isArray(offsetA)) {
1236
+ offAView.set(offsetA.slice(0, 3));
1237
+ } else {
1238
+ offAView[0] = Number(offsetA?.x ?? 0);
1239
+ offAView[1] = Number(offsetA?.y ?? 0);
1240
+ offAView[2] = Number(offsetA?.z ?? 0);
1241
+ }
1242
+
1243
+ const occBView = new Uint8Array(memBuf, occBPtr, occBBytes);
1244
+ if (chunkB.occupancy instanceof Uint8Array) {
1245
+ occBView.set(chunkB.occupancy.subarray(0, totalB));
1246
+ } else if (Array.isArray(chunkB.occupancy) || ArrayBuffer.isView(chunkB.occupancy)) {
1247
+ for (let i = 0; i < totalB; i++) occBView[i] = chunkB.occupancy[i] ? 1 : 0;
1248
+ } else {
1249
+ occBView.fill(0);
1250
+ }
1251
+
1252
+ const offBView = new Int32Array(memBuf, offBPtr, 3);
1253
+ if (Array.isArray(offsetB)) {
1254
+ offBView.set(offsetB.slice(0, 3));
1255
+ } else {
1256
+ offBView[0] = Number(offsetB?.x ?? 0);
1257
+ offBView[1] = Number(offsetB?.y ?? 0);
1258
+ offBView[2] = Number(offsetB?.z ?? 0);
1259
+ }
1260
+
1261
+ const outF64 = new Float64Array(memBuf, outResultPtr, seamResultStride);
1262
+ outF64.fill(0);
1263
+
1264
+ const status = exp.rundot_kinetix_ext_voxel_seam_query(
1265
+ dimAX, dimAY, dimAZ, cellA, occAPtr, offAPtr,
1266
+ dimBX, dimBY, dimBZ, cellB, occBPtr, offBPtr,
1267
+ cutAxis, cutCoord,
1268
+ outResultPtr
1269
+ );
1270
+ requireVoxelStatus(status);
1271
+ memBuf = exp.memory.buffer;
1272
+
1273
+ const result = unpackSeamResult(new Float64Array(memBuf, outResultPtr, seamResultStride), 0);
1274
+
1275
+ return {
1276
+ status,
1277
+ result,
1278
+ };
1279
+ });
1280
+ }
1281
+
1282
+ export function createVoxelChunk(dimX, dimY, dimZ, cellSize = 1.0, occupancy = null) {
1283
+ const total = validateGrid(dimX, dimY, dimZ, cellSize);
1284
+ if (occupancy !== null && (!(occupancy instanceof Uint8Array) || occupancy.length !== total)) {
1285
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT occupancy');
1286
+ }
1287
+ return {
1288
+ dimX, dimY, dimZ, cellSize,
1289
+ occupancy: occupancy ?? new Uint8Array(total),
1290
+ };
1291
+ }
1292
+
1293
+ export function getVoxelCell(chunk, x, y, z) {
1294
+ validateChunk(chunk);
1295
+ requireInteger(x, 0, chunk.dimX - 1, 'x');
1296
+ requireInteger(y, 0, chunk.dimY - 1, 'y');
1297
+ requireInteger(z, 0, chunk.dimZ - 1, 'z');
1298
+ return chunk.occupancy[x + chunk.dimX * (y + chunk.dimY * z)] ? 1 : 0;
1299
+ }
1300
+
1301
+ export function setVoxelCell(chunk, x, y, z, solid) {
1302
+ validateChunk(chunk);
1303
+ requireInteger(x, 0, chunk.dimX - 1, 'x');
1304
+ requireInteger(y, 0, chunk.dimY - 1, 'y');
1305
+ requireInteger(z, 0, chunk.dimZ - 1, 'z');
1306
+ if (typeof solid !== 'boolean') throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT solid');
1307
+ chunk.occupancy[x + chunk.dimX * (y + chunk.dimY * z)] = solid ? 1 : 0;
1308
+ return true;
1309
+ }
1310
+
1311
+ export function createVoxelCompoundShape(children) {
1312
+ validateChildren(children);
1313
+ if (children.length === 0) throw new Error('KINETIX_VOXEL_COMPOUND_EMPTY');
1314
+ return {
1315
+ type: 'voxel_compound',
1316
+ children: children.map((child) => ({
1317
+ shape: { type: 'box', halfX: child.halfExtents.x, halfY: child.halfExtents.y, halfZ: child.halfExtents.z },
1318
+ offset: { ...child.offset },
1319
+ })),
1320
+ };
1321
+ }
1322
+
1323
+ export function computeVoxelMassProperties(chunk, mass, originOffset = { x: 0, y: 0, z: 0 }) {
1324
+ validateChunk(chunk);
1325
+ validateVector(originOffset, 'originOffset');
1326
+ if (!Number.isFinite(mass) || mass <= 0) throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT mass');
1327
+ const instance = getVoxelInstance();
1328
+ const exp = instance.exports;
1329
+ const outputBytes = VOXEL_MASS_PROPERTIES_STRIDE * 8;
1330
+ return withVoxelScratch(instance, chunk.occupancy.length + 16 + outputBytes, (pointer) => {
1331
+ const outputPointer = (pointer + chunk.occupancy.length + 15) & ~15;
1332
+ new Uint8Array(exp.memory.buffer, pointer, chunk.occupancy.length).set(chunk.occupancy);
1333
+ const status = exp.rundot_kinetix_ext_voxel_mass_properties(
1334
+ chunk.dimX, chunk.dimY, chunk.dimZ, chunk.cellSize, pointer, mass,
1335
+ originOffset.x, originOffset.y, originOffset.z, outputPointer);
1336
+ requireVoxelStatus(status);
1337
+ if (status === VoxelStatus.NoOccupiedVoxels) return { status, occupiedCount: 0 };
1338
+ const values = new Float64Array(exp.memory.buffer, outputPointer, VOXEL_MASS_PROPERTIES_STRIDE);
1339
+ return {
1340
+ status,
1341
+ occupiedCount: values[0],
1342
+ mass: values[1],
1343
+ centerOfMass: { x: values[2], y: values[3], z: values[4] },
1344
+ inertia: { xx: values[5], yy: values[6], zz: values[7], xy: values[8], xz: values[9], yz: values[10] },
1345
+ };
1346
+ });
1347
+ }
1348
+
1349
+ export function cookVoxelBody(chunk, mass, options = {}) {
1350
+ validateChunk(chunk);
1351
+ const originOffset = options.originOffset ?? { x: 0, y: 0, z: 0 };
1352
+ const source = { ...chunk, occupancy: chunk.occupancy.slice() };
1353
+ const physical = computeVoxelMassProperties(source, mass, originOffset);
1354
+ const cooked = cookVoxelChunk(source, options);
1355
+ if (physical.status === VoxelStatus.NoOccupiedVoxels) return { ...cooked, status: physical.status, occupiedCount: 0 };
1356
+ const children = cooked.children.map((child) => ({
1357
+ shape: { type: 'box', halfX: child.halfExtents.x, halfY: child.halfExtents.y, halfZ: child.halfExtents.z },
1358
+ offset: {
1359
+ x: child.offset.x + originOffset.x,
1360
+ y: child.offset.y + originOffset.y,
1361
+ z: child.offset.z + originOffset.z,
1362
+ },
1363
+ }));
1364
+ return {
1365
+ ...cooked,
1366
+ occupiedCount: physical.occupiedCount,
1367
+ body: {
1368
+ shape: { type: 'voxel_compound', children },
1369
+ mass: physical.mass,
1370
+ centerOfMass: physical.centerOfMass,
1371
+ inertia: physical.inertia,
1372
+ },
1373
+ };
1374
+ }
1375
+
1376
+ export function carveAndIncrementalCook(chunk, existingChildren, carveRegion, options = {}, instanceOverride = null) {
1377
+ validateChildren(existingChildren);
1378
+ const capacity = options.maxChildren ?? Math.min(chunk.occupancy.length + existingChildren.length, 32768);
1379
+ const nextChildId = validateCookOptions(options, capacity, existingChildren);
1380
+ const carveResult = carveVoxelChunk(chunk, carveRegion, { ...options, mutateInPlace: false }, instanceOverride);
1381
+ if (!carveResult.changed) {
1382
+ return {
1383
+ status: VoxelStatus.Unchanged,
1384
+ changed: false,
1385
+ children: existingChildren,
1386
+ childCount: existingChildren.length,
1387
+ nextChildId,
1388
+ carveResult,
1389
+ stats: null,
1390
+ };
1391
+ }
1392
+
1393
+ const cookResult = incrementalCookVoxel(
1394
+ { ...chunk, occupancy: carveResult.occupancy },
1395
+ existingChildren,
1396
+ carveResult.dirtyAABB,
1397
+ options,
1398
+ instanceOverride
1399
+ );
1400
+
1401
+ if (options.mutateInPlace !== false) chunk.occupancy.set(carveResult.occupancy);
1402
+
1403
+ return {
1404
+ status: cookResult.status,
1405
+ changed: carveResult.changed,
1406
+ children: cookResult.children,
1407
+ childCount: cookResult.childCount,
1408
+ nextChildId: cookResult.nextChildId,
1409
+ stats: cookResult.stats,
1410
+ carveResult,
1411
+ };
1412
+ }
1413
+
1414
+ export function carveAndIncrementalSlice(parentChunk, parentChildren, carveRegion, options = {}, instanceOverride = null) {
1415
+ validateChildren(parentChildren);
1416
+ const capacity = options.maxOutSubChildren ?? Math.max(parentChildren.length * 4, 4096);
1417
+ const nextChildId = validateCookOptions(options, capacity, parentChildren);
1418
+ const carveResult = carveVoxelChunk(parentChunk, carveRegion, { ...options, mutateInPlace: false }, instanceOverride);
1419
+ const splitResult = splitVoxelComponents({ ...parentChunk, occupancy: carveResult.occupancy }, options, instanceOverride);
1420
+
1421
+ if (splitResult.subChunkCount <= 0) {
1422
+ if (options.mutateInPlace !== false) parentChunk.occupancy.set(carveResult.occupancy);
1423
+ return {
1424
+ status: VoxelStatus.NoOccupiedVoxels,
1425
+ carveResult,
1426
+ splitResult,
1427
+ subChildren: [],
1428
+ flatSubChildren: [],
1429
+ subChildCounts: [],
1430
+ preservedTotal: 0,
1431
+ recookedTotal: 0,
1432
+ nextChildId,
1433
+ };
1434
+ }
1435
+
1436
+ const sliceResult = incrementalSliceCompound(
1437
+ parentChildren,
1438
+ splitResult.subChunks,
1439
+ {
1440
+ ...options,
1441
+ cellSize: parentChunk.cellSize,
1442
+ },
1443
+ instanceOverride
1444
+ );
1445
+
1446
+ if (options.mutateInPlace !== false) parentChunk.occupancy.set(carveResult.occupancy);
1447
+
1448
+ return {
1449
+ status: sliceResult.status,
1450
+ carveResult,
1451
+ splitResult,
1452
+ ...sliceResult,
1453
+ };
1454
+ }
1455
+
1456
+ // Material IDs are metadata. This operation does not change occupancy or collision geometry.
1457
+ export function paintVoxelChunk(chunk, materials, region, materialId, options = {}) {
1458
+ validateChunk(chunk);
1459
+ requireInteger(materialId, 0, 0xffffffff, 'materialId');
1460
+ if (!Array.isArray(materials) || materials.length !== chunk.occupancy.length) {
1461
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT materials');
1462
+ }
1463
+ for (let index = 0; index < materials.length; index += 1) {
1464
+ const descriptor = Object.getOwnPropertyDescriptor(materials, String(index));
1465
+ if (descriptor === undefined || !Object.hasOwn(descriptor, 'value')) {
1466
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT material cell');
1467
+ }
1468
+ requireInteger(descriptor.value, 0, 0xffffffff, 'material cell');
1469
+ }
1470
+ if (options === null || typeof options !== 'object' || Array.isArray(options)
1471
+ || (options.paintEmpty !== undefined && typeof options.paintEmpty !== 'boolean')) {
1472
+ throw new Error('KINETIX_VOXEL_INVALID_ARGUMENT paint options');
1473
+ }
1474
+ const selection = carveVoxelChunk({ ...chunk, occupancy: new Uint8Array(chunk.occupancy.length) },
1475
+ { ...region, op: 'fill' }, { mutateInPlace: false }).occupancy;
1476
+ let updated = null;
1477
+ const dirty = { minX: chunk.dimX, minY: chunk.dimY, minZ: chunk.dimZ, maxX: -1, maxY: -1, maxZ: -1 };
1478
+ for (let z = 0; z < chunk.dimZ; z += 1) {
1479
+ for (let y = 0; y < chunk.dimY; y += 1) {
1480
+ for (let x = 0; x < chunk.dimX; x += 1) {
1481
+ const index = x + chunk.dimX * (y + chunk.dimY * z);
1482
+ if (!selection[index] || (!chunk.occupancy[index] && options.paintEmpty !== true)
1483
+ || materials[index] === materialId) continue;
1484
+ if (updated === null) updated = Array.from({ length: materials.length }, (_, cell) => materials[cell]);
1485
+ updated[index] = materialId;
1486
+ dirty.minX = Math.min(dirty.minX, x); dirty.maxX = Math.max(dirty.maxX, x);
1487
+ dirty.minY = Math.min(dirty.minY, y); dirty.maxY = Math.max(dirty.maxY, y);
1488
+ dirty.minZ = Math.min(dirty.minZ, z); dirty.maxZ = Math.max(dirty.maxZ, z);
1489
+ }
1490
+ }
1491
+ }
1492
+ return Object.freeze({
1493
+ status: 0,
1494
+ changed: updated !== null,
1495
+ materials: updated === null ? materials : Object.freeze(updated),
1496
+ dirtyAABB: Object.freeze(updated === null
1497
+ ? { minX: 0, minY: 0, minZ: 0, maxX: 0, maxY: 0, maxZ: 0 } : dirty),
1498
+ });
1499
+ }