@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,1285 @@
1
+ /**
2
+ * Volume soft bodies (Row A03 capability).
3
+ *
4
+ * Implements volumetric tetrahedral soft body dynamics:
5
+ * - Volume / shape preservation constraints
6
+ * - Hydrostatic & inflation pressure forces
7
+ * - Corotational elasticity & shape matching
8
+ * - Deterministic plastic deformation (yield threshold & plastic flow rate)
9
+ * - Inverted-element restoration impulses
10
+ * - Rigid body anchor coupling and ground collision
11
+ *
12
+ * All computation is marshaled directly to and from compiled C++ WebAssembly
13
+ * (physics-extended.wasm). No physics logic is reimplemented in JavaScript.
14
+ */
15
+
16
+ import {
17
+ ensureExtendedInstance as extEnsureInstance,
18
+ extendedPhysicsReady as extPhysicsReady,
19
+ } from './extended-wasm-instance.mjs';
20
+ import {
21
+ physicsExtendedWasmModuleBase64,
22
+ } from './physics-extended-wasm-module.generated.mjs';
23
+
24
+ export const SOFT_BODY_ABI_VERSION = 1;
25
+
26
+ export const SoftBodyStatus = Object.freeze({
27
+ Success: 0,
28
+ InvalidInput: -1,
29
+ InvalidNodeCount: -2,
30
+ InvalidTetCount: -3,
31
+ InvalidParams: -4,
32
+ BufferOverflow: -5,
33
+ });
34
+
35
+ export const SoftBodyProbeScenario = Object.freeze({
36
+ VolumePreservation: 0,
37
+ PressureInflation: 1,
38
+ InversionHandling: 2,
39
+ Plasticity: 3,
40
+ });
41
+
42
+ export const wasmEntryPointsUsed = Object.freeze([
43
+ 'rundot_kinetix_ext_soft_body_abi_version',
44
+ 'rundot_kinetix_ext_soft_body_node_stride',
45
+ 'rundot_kinetix_ext_soft_body_tet_stride',
46
+ 'rundot_kinetix_ext_soft_body_face_stride',
47
+ 'rundot_kinetix_ext_soft_body_edge_stride',
48
+ 'rundot_kinetix_ext_soft_body_anchor_stride',
49
+ 'rundot_kinetix_ext_soft_body_params_stride',
50
+ 'rundot_kinetix_ext_soft_body_max_nodes',
51
+ 'rundot_kinetix_ext_soft_body_max_tetrahedra',
52
+ 'rundot_kinetix_ext_soft_body_max_edges',
53
+ 'rundot_kinetix_ext_soft_body_max_faces',
54
+ 'rundot_kinetix_ext_soft_body_size',
55
+ 'rundot_kinetix_ext_soft_body_stride',
56
+ 'rundot_kinetix_ext_soft_body_step',
57
+ 'rundot_kinetix_ext_soft_body_compute_volume',
58
+ 'rundot_kinetix_ext_soft_body_solve_volume',
59
+ 'rundot_kinetix_ext_soft_body_apply_pressure',
60
+ 'rundot_kinetix_ext_soft_body_handle_inversion',
61
+ 'rundot_kinetix_ext_soft_body_probe_size',
62
+ 'rundot_kinetix_ext_soft_body_probe',
63
+ ]);
64
+
65
+ // ---------------------------------------------------------------------------
66
+ // WASM Instance Management & Memory Setup
67
+ // ---------------------------------------------------------------------------
68
+
69
+ let cachedInstance = null;
70
+
71
+ function decodeBase64(value) {
72
+ if (typeof atob === 'function') {
73
+ const decoded = atob(value);
74
+ const bytes = new Uint8Array(decoded.length);
75
+ for (let i = 0; i < decoded.length; i += 1) bytes[i] = decoded.charCodeAt(i);
76
+ return bytes;
77
+ }
78
+ if (typeof Buffer !== 'undefined') return Uint8Array.from(Buffer.from(value, 'base64'));
79
+ throw new Error('KINETIX_EXTENDED_PHYSICS_BASE64_UNAVAILABLE: Base64 decoder unavailable');
80
+ }
81
+
82
+ export async function extendedPhysicsReady() {
83
+ const ready = await extPhysicsReady();
84
+ if (cachedInstance === null) {
85
+ cachedInstance = await extEnsureInstance();
86
+ }
87
+ return ready;
88
+ }
89
+
90
+ export async function ensureExtendedInstance() {
91
+ cachedInstance = await extEnsureInstance();
92
+ return cachedInstance;
93
+ }
94
+
95
+ export async function initSoftBodyWasm() {
96
+ if (cachedInstance !== null) return cachedInstance;
97
+ cachedInstance = await extEnsureInstance();
98
+ return cachedInstance;
99
+ }
100
+
101
+ export function setExtendedInstance(instance) {
102
+ cachedInstance = instance;
103
+ }
104
+
105
+ export function softBodyReady() {
106
+ return cachedInstance !== null;
107
+ }
108
+
109
+ export function getExtendedInstance(instanceOverride = null) {
110
+ if (instanceOverride) return instanceOverride;
111
+ if (cachedInstance !== null) return cachedInstance;
112
+
113
+ try {
114
+ const bytes = decodeBase64(physicsExtendedWasmModuleBase64);
115
+ const mod = new WebAssembly.Module(bytes);
116
+ const inst = new WebAssembly.Instance(mod, {});
117
+ inst.exports._initialize?.();
118
+ cachedInstance = inst;
119
+ return cachedInstance;
120
+ } catch (error) {
121
+ const detail = error instanceof Error ? error.message : String(error);
122
+ throw new Error(
123
+ `KINETIX_SOFT_BODY_WASM_UNAVAILABLE: ${detail}. Await extendedPhysicsReady() or ensureExtendedInstance() before calling soft body solver.`,
124
+ { cause: error }
125
+ );
126
+ }
127
+ }
128
+
129
+ // ---------------------------------------------------------------------------
130
+ // Dynamic Strides & Limits (Queried from WASM ABI, never hardcoded)
131
+ // ---------------------------------------------------------------------------
132
+
133
+ export function getSoftBodyAbiVersion(instanceOverride = null) {
134
+ const instance = getExtendedInstance(instanceOverride);
135
+ return instance.exports.rundot_kinetix_ext_soft_body_abi_version();
136
+ }
137
+
138
+ export function getSoftBodyStrides(instanceOverride = null) {
139
+ const instance = getExtendedInstance(instanceOverride);
140
+ const exp = instance.exports;
141
+ return {
142
+ abiVersion: exp.rundot_kinetix_ext_soft_body_abi_version(),
143
+ nodeStride: exp.rundot_kinetix_ext_soft_body_node_stride(),
144
+ tetStride: exp.rundot_kinetix_ext_soft_body_tet_stride(),
145
+ faceStride: exp.rundot_kinetix_ext_soft_body_face_stride(),
146
+ edgeStride: exp.rundot_kinetix_ext_soft_body_edge_stride(),
147
+ anchorStride: exp.rundot_kinetix_ext_soft_body_anchor_stride(),
148
+ paramsStride: exp.rundot_kinetix_ext_soft_body_params_stride(),
149
+ size: exp.rundot_kinetix_ext_soft_body_size(),
150
+ stride: exp.rundot_kinetix_ext_soft_body_stride(),
151
+ };
152
+ }
153
+
154
+ export function getSoftBodyLimits(instanceOverride = null) {
155
+ const instance = getExtendedInstance(instanceOverride);
156
+ const exp = instance.exports;
157
+ return {
158
+ maxNodes: exp.rundot_kinetix_ext_soft_body_max_nodes(),
159
+ maxTetrahedra: exp.rundot_kinetix_ext_soft_body_max_tetrahedra(),
160
+ maxEdges: exp.rundot_kinetix_ext_soft_body_max_edges(),
161
+ maxFaces: exp.rundot_kinetix_ext_soft_body_max_faces(),
162
+ };
163
+ }
164
+
165
+ // ---------------------------------------------------------------------------
166
+ // Scratch Buffer Memory Management
167
+ // ---------------------------------------------------------------------------
168
+
169
+ const SCRATCH_MEMORY_START = 8 * 1024 * 1024; // 8 MB safe offset
170
+
171
+ function align16(value) {
172
+ return (value + 15) & ~15;
173
+ }
174
+
175
+ function allocateScratchBuffer(instance, byteLength) {
176
+ const exp = instance.exports;
177
+ if (typeof exp.malloc === 'function') {
178
+ const ptr = exp.malloc(byteLength);
179
+ if (ptr) {
180
+ return {
181
+ ptr,
182
+ free() {
183
+ if (typeof exp.free === 'function') exp.free(ptr);
184
+ },
185
+ };
186
+ }
187
+ }
188
+
189
+ const memory = exp.memory;
190
+ const alignedBytes = align16(byteLength);
191
+ if (!instance._softBodyScratchBase) {
192
+ instance._softBodyScratchBase = Math.max(memory.buffer.byteLength, SCRATCH_MEMORY_START);
193
+ instance._softBodyScratchOffset = instance._softBodyScratchBase;
194
+ }
195
+ const neededBytes = instance._softBodyScratchOffset + alignedBytes;
196
+ if (neededBytes > memory.buffer.byteLength) {
197
+ const pages = Math.ceil((neededBytes - memory.buffer.byteLength) / 65536);
198
+ memory.grow(pages);
199
+ }
200
+ const ptr = instance._softBodyScratchOffset;
201
+ instance._softBodyScratchOffset += alignedBytes;
202
+ return {
203
+ ptr,
204
+ free() {
205
+ instance._softBodyScratchOffset = ptr;
206
+ },
207
+ };
208
+ }
209
+
210
+ function withScratchMemory(instance, byteLength, fn) {
211
+ const buffer = allocateScratchBuffer(instance, byteLength);
212
+ try {
213
+ return fn(buffer.ptr);
214
+ } finally {
215
+ buffer.free();
216
+ }
217
+ }
218
+
219
+ // ---------------------------------------------------------------------------
220
+ // Vector math primitives (deterministic double-precision)
221
+ // ---------------------------------------------------------------------------
222
+
223
+ export function addVec3(a, b) {
224
+ return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
225
+ }
226
+
227
+ export function subVec3(a, b) {
228
+ return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
229
+ }
230
+
231
+ export function scaleVec3(v, s) {
232
+ return { x: v.x * s, y: v.y * s, z: v.z * s };
233
+ }
234
+
235
+ export function dotVec3(a, b) {
236
+ return a.x * b.x + a.y * b.y + a.z * b.z;
237
+ }
238
+
239
+ export function crossVec3(a, b) {
240
+ return {
241
+ x: a.y * b.z - a.z * b.y,
242
+ y: a.z * b.x - a.x * b.z,
243
+ z: a.x * b.y - a.y * b.x,
244
+ };
245
+ }
246
+
247
+ export function lengthVec3(v) {
248
+ const d = dotVec3(v, v);
249
+ return d <= 0.0 ? 0.0 : Math.sqrt(d);
250
+ }
251
+
252
+ // ---------------------------------------------------------------------------
253
+ // High-Level Data Model & Factory Functions
254
+ // ---------------------------------------------------------------------------
255
+
256
+ export function createSoftBody(options = {}) {
257
+ const p = options.params || {};
258
+ return {
259
+ id: options.id ?? 0,
260
+ params: {
261
+ gravityX: p.gravityX ?? 0.0,
262
+ gravityY: p.gravityY ?? -9.81,
263
+ gravityZ: p.gravityZ ?? 0.0,
264
+ damping: p.damping ?? 0.005,
265
+ collisionRadius: p.collisionRadius ?? 0.05,
266
+ groundY: p.groundY ?? 0.0,
267
+ groundFriction: p.groundFriction ?? 0.3,
268
+ groundRestitution: p.groundRestitution ?? 0.1,
269
+ enableGround: p.enableGround !== false,
270
+ pressure: p.pressure ?? 0.0,
271
+ pressureStiffness: p.pressureStiffness ?? 100.0,
272
+ volumePreservationStiffness: p.volumePreservationStiffness ?? 1.0,
273
+ shapeMatchingStiffness: p.shapeMatchingStiffness ?? 0.0,
274
+ edgeYieldThreshold: p.edgeYieldThreshold ?? 0.0,
275
+ edgePlasticityRate: p.edgePlasticityRate ?? 0.0,
276
+ inversionThreshold: p.inversionThreshold ?? 0.0,
277
+ inversionRestorationStiffness: p.inversionRestorationStiffness ?? 0.8,
278
+ solverIterations: p.solverIterations ?? 10,
279
+ },
280
+ nodes: (options.nodes || []).map((n) => ({
281
+ position: {
282
+ x: n.position ? n.position.x : (n.x ?? 0.0),
283
+ y: n.position ? n.position.y : (n.y ?? 0.0),
284
+ z: n.position ? n.position.z : (n.z ?? 0.0),
285
+ },
286
+ velocity: {
287
+ x: n.velocity ? n.velocity.x : (n.vx ?? 0.0),
288
+ y: n.velocity ? n.velocity.y : (n.vy ?? 0.0),
289
+ z: n.velocity ? n.velocity.z : (n.vz ?? 0.0),
290
+ },
291
+ previousPosition: {
292
+ x: n.previousPosition ? n.previousPosition.x : (n.position ? n.position.x : (n.x ?? 0.0)),
293
+ y: n.previousPosition ? n.previousPosition.y : (n.position ? n.position.y : (n.y ?? 0.0)),
294
+ z: n.previousPosition ? n.previousPosition.z : (n.position ? n.position.z : (n.z ?? 0.0)),
295
+ },
296
+ inverseMass: n.inverseMass ?? 1.0,
297
+ })),
298
+ tetrahedra: (options.tetrahedra || []).map((t) => ({
299
+ node0: t.node0 ?? (t.nodes ? t.nodes[0] : 0),
300
+ node1: t.node1 ?? (t.nodes ? t.nodes[1] : 1),
301
+ node2: t.node2 ?? (t.nodes ? t.nodes[2] : 2),
302
+ node3: t.node3 ?? (t.nodes ? t.nodes[3] : 3),
303
+ restVolume: t.restVolume ?? 0.0,
304
+ currentVolume: t.currentVolume ?? (t.restVolume ?? 0.0),
305
+ volumeStiffness: t.volumeStiffness ?? 1.0,
306
+ inverted: Boolean(t.inverted),
307
+ })),
308
+ faces: (options.faces || []).map((f) => ({
309
+ nodeA: f.nodeA ?? (f.nodes ? f.nodes[0] : 0),
310
+ nodeB: f.nodeB ?? (f.nodes ? f.nodes[1] : 1),
311
+ nodeC: f.nodeC ?? (f.nodes ? f.nodes[2] : 2),
312
+ area: f.area ?? 0.0,
313
+ normal: {
314
+ x: f.normal ? f.normal.x : 0.0,
315
+ y: f.normal ? f.normal.y : 1.0,
316
+ z: f.normal ? f.normal.z : 0.0,
317
+ },
318
+ pressureMultiplier: f.pressureMultiplier ?? 1.0,
319
+ })),
320
+ edges: (options.edges || []).map((e) => ({
321
+ nodeA: e.nodeA ?? (e.nodes ? e.nodes[0] : 0),
322
+ nodeB: e.nodeB ?? (e.nodes ? e.nodes[1] : 1),
323
+ restLength: e.restLength ?? 1.0,
324
+ currentLength: e.currentLength ?? (e.restLength ?? 1.0),
325
+ stretchStiffness: e.stretchStiffness ?? 500.0,
326
+ plasticStrain: e.plasticStrain ?? 0.0,
327
+ yieldThreshold: e.yieldThreshold ?? 0.0,
328
+ plasticityRate: e.plasticityRate ?? 0.0,
329
+ })),
330
+ anchors: (options.anchors || []).map((a) => ({
331
+ nodeIndex: a.nodeIndex ?? 0,
332
+ bodyIndex: a.bodyIndex ?? -1,
333
+ localAttachment: {
334
+ x: a.localAttachment ? a.localAttachment.x : 0.0,
335
+ y: a.localAttachment ? a.localAttachment.y : 0.0,
336
+ z: a.localAttachment ? a.localAttachment.z : 0.0,
337
+ },
338
+ worldAttachment: {
339
+ x: a.worldAttachment ? a.worldAttachment.x : 0.0,
340
+ y: a.worldAttachment ? a.worldAttachment.y : 0.0,
341
+ z: a.worldAttachment ? a.worldAttachment.z : 0.0,
342
+ },
343
+ reactionForce: {
344
+ x: a.reactionForce ? a.reactionForce.x : 0.0,
345
+ y: a.reactionForce ? a.reactionForce.y : 0.0,
346
+ z: a.reactionForce ? a.reactionForce.z : 0.0,
347
+ },
348
+ active: a.active !== false,
349
+ })),
350
+ totalRestVolume: 0.0,
351
+ currentTotalVolume: 0.0,
352
+ invertedCount: 0,
353
+ };
354
+ }
355
+
356
+ export function addSoftBodyNode(sim, node) {
357
+ const entry = {
358
+ position: {
359
+ x: node.position ? node.position.x : (node.x ?? 0.0),
360
+ y: node.position ? node.position.y : (node.y ?? 0.0),
361
+ z: node.position ? node.position.z : (node.z ?? 0.0),
362
+ },
363
+ velocity: {
364
+ x: node.velocity ? node.velocity.x : (node.vx ?? 0.0),
365
+ y: node.velocity ? node.velocity.y : (node.vy ?? 0.0),
366
+ z: node.velocity ? node.velocity.z : (node.vz ?? 0.0),
367
+ },
368
+ previousPosition: {
369
+ x: node.previousPosition ? node.previousPosition.x : (node.position ? node.position.x : (node.x ?? 0.0)),
370
+ y: node.previousPosition ? node.previousPosition.y : (node.position ? node.position.y : (node.y ?? 0.0)),
371
+ z: node.previousPosition ? node.previousPosition.z : (node.position ? node.position.z : (node.z ?? 0.0)),
372
+ },
373
+ inverseMass: node.inverseMass ?? 1.0,
374
+ };
375
+ sim.nodes.push(entry);
376
+ return sim.nodes.length - 1;
377
+ }
378
+
379
+ export function addSoftBodyTetrahedron(sim, tet) {
380
+ const entry = {
381
+ node0: tet.node0 ?? (tet.nodes ? tet.nodes[0] : 0),
382
+ node1: tet.node1 ?? (tet.nodes ? tet.nodes[1] : 1),
383
+ node2: tet.node2 ?? (tet.nodes ? tet.nodes[2] : 2),
384
+ node3: tet.node3 ?? (tet.nodes ? tet.nodes[3] : 3),
385
+ restVolume: tet.restVolume ?? 0.0,
386
+ currentVolume: tet.currentVolume ?? (tet.restVolume ?? 0.0),
387
+ volumeStiffness: tet.volumeStiffness ?? 1.0,
388
+ inverted: Boolean(tet.inverted),
389
+ };
390
+ sim.tetrahedra.push(entry);
391
+ return sim.tetrahedra.length - 1;
392
+ }
393
+
394
+ export function addSoftBodyFace(sim, face) {
395
+ const entry = {
396
+ nodeA: face.nodeA ?? (face.nodes ? face.nodes[0] : 0),
397
+ nodeB: face.nodeB ?? (face.nodes ? face.nodes[1] : 1),
398
+ nodeC: face.nodeC ?? (face.nodes ? face.nodes[2] : 2),
399
+ area: face.area ?? 0.0,
400
+ normal: {
401
+ x: face.normal ? face.normal.x : 0.0,
402
+ y: face.normal ? face.normal.y : 1.0,
403
+ z: face.normal ? face.normal.z : 0.0,
404
+ },
405
+ pressureMultiplier: face.pressureMultiplier ?? 1.0,
406
+ };
407
+ sim.faces.push(entry);
408
+ return sim.faces.length - 1;
409
+ }
410
+
411
+ export function addSoftBodyEdge(sim, edge) {
412
+ const entry = {
413
+ nodeA: edge.nodeA ?? (edge.nodes ? edge.nodes[0] : 0),
414
+ nodeB: edge.nodeB ?? (edge.nodes ? edge.nodes[1] : 1),
415
+ restLength: edge.restLength ?? 1.0,
416
+ currentLength: edge.currentLength ?? (edge.restLength ?? 1.0),
417
+ stretchStiffness: edge.stretchStiffness ?? 500.0,
418
+ plasticStrain: edge.plasticStrain ?? 0.0,
419
+ yieldThreshold: edge.yieldThreshold ?? 0.0,
420
+ plasticityRate: edge.plasticityRate ?? 0.0,
421
+ };
422
+ sim.edges.push(entry);
423
+ return sim.edges.length - 1;
424
+ }
425
+
426
+ export function addSoftBodyAnchor(sim, anchor) {
427
+ const entry = {
428
+ nodeIndex: anchor.nodeIndex ?? 0,
429
+ bodyIndex: anchor.bodyIndex ?? -1,
430
+ localAttachment: {
431
+ x: anchor.localAttachment ? anchor.localAttachment.x : 0.0,
432
+ y: anchor.localAttachment ? anchor.localAttachment.y : 0.0,
433
+ z: anchor.localAttachment ? anchor.localAttachment.z : 0.0,
434
+ },
435
+ worldAttachment: {
436
+ x: anchor.worldAttachment ? anchor.worldAttachment.x : 0.0,
437
+ y: anchor.worldAttachment ? anchor.worldAttachment.y : 0.0,
438
+ z: anchor.worldAttachment ? anchor.worldAttachment.z : 0.0,
439
+ },
440
+ reactionForce: {
441
+ x: anchor.reactionForce ? anchor.reactionForce.x : 0.0,
442
+ y: anchor.reactionForce ? anchor.reactionForce.y : 0.0,
443
+ z: anchor.reactionForce ? anchor.reactionForce.z : 0.0,
444
+ },
445
+ active: anchor.active !== false,
446
+ };
447
+ sim.anchors.push(entry);
448
+ return sim.anchors.length - 1;
449
+ }
450
+
451
+ /**
452
+ * Creates a procedural 5-tetrahedron cube soft body matching C++ createSoftBodyCube.
453
+ */
454
+ export function createSoftBodyCube(options = {}) {
455
+ const center = options.center || { x: 0.0, y: 1.0, z: 0.0 };
456
+ const h = typeof options.halfExtents === 'number'
457
+ ? { x: options.halfExtents, y: options.halfExtents, z: options.halfExtents }
458
+ : (options.halfExtents || { x: 0.5, y: 0.5, z: 0.5 });
459
+ const totalMass = options.totalMass ?? 1.0;
460
+ const nodeMass = totalMass > 0.0 ? totalMass / 8.0 : 0.125;
461
+ const invMass = nodeMass > 0.0 ? 1.0 / nodeMass : 1.0;
462
+ const stretchStiffness = options.stretchStiffness ?? 500.0;
463
+ const volumeStiffness = options.volumeStiffness ?? 1.0;
464
+
465
+ const hx = h.x;
466
+ const hy = h.y;
467
+ const hz = h.z;
468
+
469
+ const corners = [
470
+ [-hx, -hy, -hz],
471
+ [ hx, -hy, -hz],
472
+ [ hx, hy, -hz],
473
+ [-hx, hy, -hz],
474
+ [-hx, -hy, hz],
475
+ [ hx, -hy, hz],
476
+ [ hx, hy, hz],
477
+ [-hx, hy, hz],
478
+ ];
479
+
480
+ const nodes = [];
481
+ for (let i = 0; i < 8; ++i) {
482
+ nodes.push({
483
+ position: {
484
+ x: center.x + corners[i][0],
485
+ y: center.y + corners[i][1],
486
+ z: center.z + corners[i][2],
487
+ },
488
+ velocity: { x: 0.0, y: 0.0, z: 0.0 },
489
+ previousPosition: {
490
+ x: center.x + corners[i][0],
491
+ y: center.y + corners[i][1],
492
+ z: center.z + corners[i][2],
493
+ },
494
+ inverseMass: invMass,
495
+ });
496
+ }
497
+
498
+ // 5-tetrahedron decomposition
499
+ const tetIndices = [
500
+ [0, 1, 3, 5],
501
+ [1, 2, 3, 6],
502
+ [0, 4, 5, 7],
503
+ [3, 5, 6, 7],
504
+ [0, 3, 5, 7],
505
+ ];
506
+
507
+ const tetrahedra = [];
508
+ for (let t = 0; t < 5; ++t) {
509
+ const idx = tetIndices[t];
510
+ const p0 = nodes[idx[0]].position;
511
+ const p1 = nodes[idx[1]].position;
512
+ const p2 = nodes[idx[2]].position;
513
+ const p3 = nodes[idx[3]].position;
514
+ const d1 = subVec3(p1, p0);
515
+ const d2 = subVec3(p2, p0);
516
+ const d3 = subVec3(p3, p0);
517
+ let v = dotVec3(d1, crossVec3(d2, d3)) / 6.0;
518
+ let n1 = idx[1];
519
+ let n2 = idx[2];
520
+ if (v < 0.0) {
521
+ n1 = idx[2];
522
+ n2 = idx[1];
523
+ v = -v;
524
+ }
525
+ tetrahedra.push({
526
+ node0: idx[0],
527
+ node1: n1,
528
+ node2: n2,
529
+ node3: idx[3],
530
+ restVolume: v,
531
+ currentVolume: v,
532
+ volumeStiffness,
533
+ inverted: false,
534
+ });
535
+ }
536
+
537
+ // 12 boundary surface triangles (facing outward)
538
+ const faceIndices = [
539
+ [0, 2, 1], [0, 3, 2], // -Z
540
+ [4, 5, 6], [4, 6, 7], // +Z
541
+ [0, 1, 5], [0, 5, 4], // -Y
542
+ [2, 3, 7], [2, 7, 6], // +Y
543
+ [0, 4, 7], [0, 7, 3], // -X
544
+ [1, 2, 6], [1, 6, 5], // +X
545
+ ];
546
+
547
+ const faces = [];
548
+ for (let f = 0; f < 12; ++f) {
549
+ const idx = faceIndices[f];
550
+ const pA = nodes[idx[0]].position;
551
+ const pB = nodes[idx[1]].position;
552
+ const pC = nodes[idx[2]].position;
553
+ const normalCross = crossVec3(subVec3(pB, pA), subVec3(pC, pA));
554
+ const doubleArea = lengthVec3(normalCross);
555
+ const area = 0.5 * doubleArea;
556
+ const normal = doubleArea > 1e-15 ? scaleVec3(normalCross, 1.0 / doubleArea) : { x: 0.0, y: 1.0, z: 0.0 };
557
+ faces.push({
558
+ nodeA: idx[0],
559
+ nodeB: idx[1],
560
+ nodeC: idx[2],
561
+ area,
562
+ normal,
563
+ pressureMultiplier: 1.0,
564
+ });
565
+ }
566
+
567
+ // 18 edges
568
+ const edgeIndices = [
569
+ [0, 1], [1, 2], [2, 3], [3, 0], // -Z face
570
+ [4, 5], [5, 6], [6, 7], [7, 4], // +Z face
571
+ [0, 4], [1, 5], [2, 6], [3, 7], // connecting pillars
572
+ [0, 2], [1, 3], [4, 6], [5, 7], // face diagonals
573
+ [0, 5], [3, 5], // internal diagonals
574
+ ];
575
+
576
+ const edges = [];
577
+ for (let e = 0; e < 18; ++e) {
578
+ const idx = edgeIndices[e];
579
+ const len = lengthVec3(subVec3(nodes[idx[1]].position, nodes[idx[0]].position));
580
+ edges.push({
581
+ nodeA: idx[0],
582
+ nodeB: idx[1],
583
+ restLength: len,
584
+ currentLength: len,
585
+ stretchStiffness,
586
+ plasticStrain: 0.0,
587
+ yieldThreshold: options.params?.edgeYieldThreshold ?? 0.0,
588
+ plasticityRate: options.params?.edgePlasticityRate ?? 0.0,
589
+ });
590
+ }
591
+
592
+ return createSoftBody({
593
+ params: options.params,
594
+ nodes,
595
+ tetrahedra,
596
+ faces,
597
+ edges,
598
+ });
599
+ }
600
+
601
+ // ---------------------------------------------------------------------------
602
+ // Low-Level / Raw WASM Marshaling Functions
603
+ // ---------------------------------------------------------------------------
604
+
605
+ export function stepSoftBodyRaw(
606
+ nodeData, nodeCount,
607
+ tetData, tetCount,
608
+ faceData, faceCount,
609
+ edgeData, edgeCount,
610
+ anchorData, anchorCount,
611
+ bodyData, bodyCount,
612
+ paramsData,
613
+ dt,
614
+ instanceOverride = null
615
+ ) {
616
+ const instance = getExtendedInstance(instanceOverride);
617
+ const exp = instance.exports;
618
+ const strides = getSoftBodyStrides(instance);
619
+
620
+ const nodeDoubles = nodeCount * strides.nodeStride;
621
+ const tetDoubles = tetCount * strides.tetStride;
622
+ const faceDoubles = faceCount * strides.faceStride;
623
+ const edgeDoubles = edgeCount * strides.edgeStride;
624
+ const anchorDoubles = anchorCount * strides.anchorStride;
625
+ const bodyStride = 44;
626
+ const bodyDoubles = bodyCount * bodyStride;
627
+ const paramsDoubles = strides.paramsStride;
628
+
629
+ const totalDoubles =
630
+ nodeDoubles + tetDoubles + faceDoubles + edgeDoubles +
631
+ anchorDoubles + bodyDoubles + paramsDoubles;
632
+
633
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
634
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
635
+ let offset = 0;
636
+
637
+ const nodePtr = basePtr + offset * 8;
638
+ if (nodeData) {
639
+ for (let i = 0; i < nodeDoubles; ++i) mem[offset + i] = nodeData[i];
640
+ }
641
+ offset += nodeDoubles;
642
+
643
+ const tetPtr = tetCount > 0 ? (basePtr + offset * 8) : 0;
644
+ if (tetData && tetCount > 0) {
645
+ for (let i = 0; i < tetDoubles; ++i) mem[offset + i] = tetData[i];
646
+ }
647
+ offset += tetDoubles;
648
+
649
+ const facePtr = faceCount > 0 ? (basePtr + offset * 8) : 0;
650
+ if (faceData && faceCount > 0) {
651
+ for (let i = 0; i < faceDoubles; ++i) mem[offset + i] = faceData[i];
652
+ }
653
+ offset += faceDoubles;
654
+
655
+ const edgePtr = edgeCount > 0 ? (basePtr + offset * 8) : 0;
656
+ if (edgeData && edgeCount > 0) {
657
+ for (let i = 0; i < edgeDoubles; ++i) mem[offset + i] = edgeData[i];
658
+ }
659
+ offset += edgeDoubles;
660
+
661
+ const anchorPtr = anchorCount > 0 ? (basePtr + offset * 8) : 0;
662
+ if (anchorData && anchorCount > 0) {
663
+ for (let i = 0; i < anchorDoubles; ++i) mem[offset + i] = anchorData[i];
664
+ }
665
+ offset += anchorDoubles;
666
+
667
+ const bodyPtr = bodyCount > 0 ? (basePtr + offset * 8) : 0;
668
+ if (bodyData && bodyCount > 0) {
669
+ for (let i = 0; i < bodyDoubles; ++i) mem[offset + i] = bodyData[i];
670
+ }
671
+ offset += bodyDoubles;
672
+
673
+ const paramsPtr = basePtr + offset * 8;
674
+ if (paramsData) {
675
+ for (let i = 0; i < paramsDoubles; ++i) mem[offset + i] = paramsData[i];
676
+ }
677
+
678
+ const status = exp.rundot_kinetix_ext_soft_body_step(
679
+ nodePtr, nodeCount,
680
+ tetPtr, tetCount,
681
+ facePtr, faceCount,
682
+ edgePtr, edgeCount,
683
+ anchorPtr, anchorCount,
684
+ bodyPtr, bodyCount,
685
+ paramsPtr,
686
+ dt
687
+ );
688
+
689
+ if (status === SoftBodyStatus.Success) {
690
+ // Copy outputs back to typed array buffers if provided
691
+ if (nodeData) {
692
+ for (let i = 0; i < nodeDoubles; ++i) nodeData[i] = mem[i];
693
+ }
694
+ let readOff = nodeDoubles;
695
+ if (tetData && tetCount > 0) {
696
+ for (let i = 0; i < tetDoubles; ++i) tetData[i] = mem[readOff + i];
697
+ }
698
+ readOff += tetDoubles;
699
+ if (faceData && faceCount > 0) {
700
+ for (let i = 0; i < faceDoubles; ++i) faceData[i] = mem[readOff + i];
701
+ }
702
+ readOff += faceDoubles;
703
+ if (edgeData && edgeCount > 0) {
704
+ for (let i = 0; i < edgeDoubles; ++i) edgeData[i] = mem[readOff + i];
705
+ }
706
+ readOff += edgeDoubles;
707
+ if (anchorData && anchorCount > 0) {
708
+ for (let i = 0; i < anchorDoubles; ++i) anchorData[i] = mem[readOff + i];
709
+ }
710
+ }
711
+
712
+ return status;
713
+ });
714
+ }
715
+
716
+ export function computeSoftBodyVolumeRaw(nodeData, nodeCount, tetData, tetCount, instanceOverride = null) {
717
+ const instance = getExtendedInstance(instanceOverride);
718
+ const exp = instance.exports;
719
+ const strides = getSoftBodyStrides(instance);
720
+
721
+ const nodeDoubles = nodeCount * strides.nodeStride;
722
+ const tetDoubles = tetCount * strides.tetStride;
723
+ const totalDoubles = nodeDoubles + tetDoubles;
724
+
725
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
726
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
727
+ for (let i = 0; i < nodeDoubles; ++i) mem[i] = nodeData[i];
728
+ for (let i = 0; i < tetDoubles; ++i) mem[nodeDoubles + i] = tetData[i];
729
+
730
+ const nodePtr = basePtr;
731
+ const tetPtr = basePtr + nodeDoubles * 8;
732
+ return exp.rundot_kinetix_ext_soft_body_compute_volume(nodePtr, nodeCount, tetPtr, tetCount);
733
+ });
734
+ }
735
+
736
+ export function solveSoftBodyVolumeRaw(nodeData, nodeCount, tetData, tetCount, volumeStiffness, dt, instanceOverride = null) {
737
+ const instance = getExtendedInstance(instanceOverride);
738
+ const exp = instance.exports;
739
+ const strides = getSoftBodyStrides(instance);
740
+
741
+ const nodeDoubles = nodeCount * strides.nodeStride;
742
+ const tetDoubles = tetCount * strides.tetStride;
743
+ const totalDoubles = nodeDoubles + tetDoubles;
744
+
745
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
746
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
747
+ for (let i = 0; i < nodeDoubles; ++i) mem[i] = nodeData[i];
748
+ for (let i = 0; i < tetDoubles; ++i) mem[nodeDoubles + i] = tetData[i];
749
+
750
+ const nodePtr = basePtr;
751
+ const tetPtr = basePtr + nodeDoubles * 8;
752
+ const status = exp.rundot_kinetix_ext_soft_body_solve_volume(
753
+ nodePtr, nodeCount, tetPtr, tetCount, volumeStiffness, dt
754
+ );
755
+ if (status === 0) {
756
+ for (let i = 0; i < nodeDoubles; ++i) nodeData[i] = mem[i];
757
+ for (let i = 0; i < tetDoubles; ++i) tetData[i] = mem[nodeDoubles + i];
758
+ }
759
+ return status;
760
+ });
761
+ }
762
+
763
+ export function applySoftBodyPressureRaw(nodeData, nodeCount, faceData, faceCount, pressure, dt, instanceOverride = null) {
764
+ const instance = getExtendedInstance(instanceOverride);
765
+ const exp = instance.exports;
766
+ const strides = getSoftBodyStrides(instance);
767
+
768
+ const nodeDoubles = nodeCount * strides.nodeStride;
769
+ const faceDoubles = faceCount * strides.faceStride;
770
+ const totalDoubles = nodeDoubles + faceDoubles;
771
+
772
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
773
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
774
+ for (let i = 0; i < nodeDoubles; ++i) mem[i] = nodeData[i];
775
+ for (let i = 0; i < faceDoubles; ++i) mem[nodeDoubles + i] = faceData[i];
776
+
777
+ const nodePtr = basePtr;
778
+ const facePtr = basePtr + nodeDoubles * 8;
779
+ const status = exp.rundot_kinetix_ext_soft_body_apply_pressure(
780
+ nodePtr, nodeCount, facePtr, faceCount, pressure, dt
781
+ );
782
+ if (status === 0) {
783
+ for (let i = 0; i < nodeDoubles; ++i) nodeData[i] = mem[i];
784
+ }
785
+ return status;
786
+ });
787
+ }
788
+
789
+ export function handleSoftBodyInversionRaw(nodeData, nodeCount, tetData, tetCount, threshold, stiffness, instanceOverride = null) {
790
+ const instance = getExtendedInstance(instanceOverride);
791
+ const exp = instance.exports;
792
+ const strides = getSoftBodyStrides(instance);
793
+
794
+ const nodeDoubles = nodeCount * strides.nodeStride;
795
+ const tetDoubles = tetCount * strides.tetStride;
796
+ const totalDoubles = nodeDoubles + tetDoubles;
797
+
798
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
799
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
800
+ for (let i = 0; i < nodeDoubles; ++i) mem[i] = nodeData[i];
801
+ for (let i = 0; i < tetDoubles; ++i) mem[nodeDoubles + i] = tetData[i];
802
+
803
+ const nodePtr = basePtr;
804
+ const tetPtr = basePtr + nodeDoubles * 8;
805
+ const invertedCount = exp.rundot_kinetix_ext_soft_body_handle_inversion(
806
+ nodePtr, nodeCount, tetPtr, tetCount, threshold, stiffness
807
+ );
808
+ for (let i = 0; i < nodeDoubles; ++i) nodeData[i] = mem[i];
809
+ for (let i = 0; i < tetDoubles; ++i) tetData[i] = mem[nodeDoubles + i];
810
+ return invertedCount;
811
+ });
812
+ }
813
+
814
+ export function probeSoftBodyRaw(scenario, instanceOverride = null) {
815
+ const instance = getExtendedInstance(instanceOverride);
816
+ const exp = instance.exports;
817
+ const size = exp.rundot_kinetix_ext_soft_body_probe_size();
818
+ const ptr = exp.rundot_kinetix_ext_soft_body_probe(scenario);
819
+ const result = new Float64Array(size);
820
+ const mem = new Float64Array(exp.memory.buffer, ptr, size);
821
+ for (let i = 0; i < size; ++i) result[i] = mem[i];
822
+ return result;
823
+ }
824
+
825
+ // ---------------------------------------------------------------------------
826
+ // High-Level Simulation Interface (Structured Objects <-> WASM Marshaling)
827
+ // ---------------------------------------------------------------------------
828
+
829
+ export function stepSoftBody(sim, dt, bodies = [], instanceOverride = null) {
830
+ const instance = getExtendedInstance(instanceOverride);
831
+ const exp = instance.exports;
832
+ const strides = getSoftBodyStrides(instance);
833
+
834
+ const nodeCount = sim.nodes.length;
835
+ const tetCount = sim.tetrahedra.length;
836
+ const faceCount = sim.faces.length;
837
+ const edgeCount = sim.edges.length;
838
+ const anchorCount = sim.anchors.length;
839
+ const bodyCount = bodies ? bodies.length : 0;
840
+ const bodyStride = 44;
841
+
842
+ const nodeDoubles = nodeCount * strides.nodeStride;
843
+ const tetDoubles = tetCount * strides.tetStride;
844
+ const faceDoubles = faceCount * strides.faceStride;
845
+ const edgeDoubles = edgeCount * strides.edgeStride;
846
+ const anchorDoubles = anchorCount * strides.anchorStride;
847
+ const bodyDoubles = bodyCount * bodyStride;
848
+ const paramsDoubles = strides.paramsStride;
849
+
850
+ const totalDoubles =
851
+ nodeDoubles + tetDoubles + faceDoubles + edgeDoubles +
852
+ anchorDoubles + bodyDoubles + paramsDoubles;
853
+
854
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
855
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
856
+ let offset = 0;
857
+
858
+ // Pack nodes
859
+ const nodePtr = basePtr + offset * 8;
860
+ for (let i = 0; i < nodeCount; ++i) {
861
+ const n = sim.nodes[i];
862
+ const off = offset + i * strides.nodeStride;
863
+ mem[off + 0] = n.position.x;
864
+ mem[off + 1] = n.position.y;
865
+ mem[off + 2] = n.position.z;
866
+ mem[off + 3] = n.velocity.x;
867
+ mem[off + 4] = n.velocity.y;
868
+ mem[off + 5] = n.velocity.z;
869
+ mem[off + 6] = n.inverseMass;
870
+ mem[off + 7] = n.previousPosition.x;
871
+ mem[off + 8] = n.previousPosition.y;
872
+ mem[off + 9] = n.previousPosition.z;
873
+ }
874
+ offset += nodeDoubles;
875
+
876
+ // Pack tetrahedra
877
+ const tetPtr = tetCount > 0 ? (basePtr + offset * 8) : 0;
878
+ if (tetCount > 0) {
879
+ for (let t = 0; t < tetCount; ++t) {
880
+ const tet = sim.tetrahedra[t];
881
+ const off = offset + t * strides.tetStride;
882
+ mem[off + 0] = tet.node0;
883
+ mem[off + 1] = tet.node1;
884
+ mem[off + 2] = tet.node2;
885
+ mem[off + 3] = tet.node3;
886
+ mem[off + 4] = tet.restVolume;
887
+ mem[off + 5] = tet.currentVolume;
888
+ mem[off + 6] = tet.volumeStiffness;
889
+ mem[off + 7] = tet.inverted ? 1.0 : 0.0;
890
+ }
891
+ }
892
+ offset += tetDoubles;
893
+
894
+ // Pack faces
895
+ const facePtr = faceCount > 0 ? (basePtr + offset * 8) : 0;
896
+ if (faceCount > 0) {
897
+ for (let f = 0; f < faceCount; ++f) {
898
+ const fc = sim.faces[f];
899
+ const off = offset + f * strides.faceStride;
900
+ mem[off + 0] = fc.nodeA;
901
+ mem[off + 1] = fc.nodeB;
902
+ mem[off + 2] = fc.nodeC;
903
+ mem[off + 3] = fc.area;
904
+ mem[off + 4] = fc.normal.x;
905
+ mem[off + 5] = fc.normal.y;
906
+ mem[off + 6] = fc.normal.z;
907
+ mem[off + 7] = fc.pressureMultiplier;
908
+ }
909
+ }
910
+ offset += faceDoubles;
911
+
912
+ // Pack edges
913
+ const edgePtr = edgeCount > 0 ? (basePtr + offset * 8) : 0;
914
+ if (edgeCount > 0) {
915
+ for (let e = 0; e < edgeCount; ++e) {
916
+ const ed = sim.edges[e];
917
+ const off = offset + e * strides.edgeStride;
918
+ mem[off + 0] = ed.nodeA;
919
+ mem[off + 1] = ed.nodeB;
920
+ mem[off + 2] = ed.restLength;
921
+ mem[off + 3] = ed.currentLength;
922
+ mem[off + 4] = ed.stretchStiffness;
923
+ mem[off + 5] = ed.plasticStrain;
924
+ mem[off + 6] = ed.yieldThreshold;
925
+ mem[off + 7] = ed.plasticityRate;
926
+ }
927
+ }
928
+ offset += edgeDoubles;
929
+
930
+ // Pack anchors
931
+ const anchorPtr = anchorCount > 0 ? (basePtr + offset * 8) : 0;
932
+ if (anchorCount > 0) {
933
+ for (let a = 0; a < anchorCount; ++a) {
934
+ const an = sim.anchors[a];
935
+ const off = offset + a * strides.anchorStride;
936
+ mem[off + 0] = an.nodeIndex;
937
+ mem[off + 1] = an.bodyIndex;
938
+ mem[off + 2] = an.localAttachment.x;
939
+ mem[off + 3] = an.localAttachment.y;
940
+ mem[off + 4] = an.localAttachment.z;
941
+ mem[off + 5] = an.worldAttachment.x;
942
+ mem[off + 6] = an.worldAttachment.y;
943
+ mem[off + 7] = an.worldAttachment.z;
944
+ mem[off + 8] = an.reactionForce.x;
945
+ mem[off + 9] = an.reactionForce.y;
946
+ mem[off + 10] = an.reactionForce.z;
947
+ mem[off + 11] = an.active ? 1.0 : 0.0;
948
+ }
949
+ }
950
+ offset += anchorDoubles;
951
+
952
+ // Pack rigid bodies
953
+ const bodyPtr = bodyCount > 0 ? (basePtr + offset * 8) : 0;
954
+ if (bodyCount > 0) {
955
+ for (let b = 0; b < bodyCount; ++b) {
956
+ const bd = bodies[b];
957
+ const off = offset + b * bodyStride;
958
+ mem[off + 0] = bd.x ?? 0.0;
959
+ mem[off + 1] = bd.y ?? 0.0;
960
+ mem[off + 2] = bd.z ?? 0.0;
961
+ mem[off + 3] = bd.vx ?? 0.0;
962
+ mem[off + 4] = bd.vy ?? 0.0;
963
+ mem[off + 5] = bd.vz ?? 0.0;
964
+ mem[off + 9] = bd.dynamic !== false ? 1.0 : 0.0;
965
+ mem[off + 18] = bd.mass ?? 1.0;
966
+ mem[off + 22] = bd.orientationX ?? 0.0;
967
+ mem[off + 23] = bd.orientationY ?? 0.0;
968
+ mem[off + 24] = bd.orientationZ ?? 0.0;
969
+ mem[off + 25] = bd.orientationW ?? 1.0;
970
+ }
971
+ }
972
+ offset += bodyDoubles;
973
+
974
+ // Pack params
975
+ const paramsPtr = basePtr + offset * 8;
976
+ const p = sim.params;
977
+ mem[offset + 0] = p.gravityX;
978
+ mem[offset + 1] = p.gravityY;
979
+ mem[offset + 2] = p.gravityZ;
980
+ mem[offset + 3] = p.damping;
981
+ mem[offset + 4] = p.collisionRadius;
982
+ mem[offset + 5] = p.groundY;
983
+ mem[offset + 6] = p.groundFriction;
984
+ mem[offset + 7] = p.groundRestitution;
985
+ mem[offset + 8] = p.enableGround ? 1.0 : 0.0;
986
+ mem[offset + 9] = p.pressure;
987
+ mem[offset + 10] = p.pressureStiffness;
988
+ mem[offset + 11] = p.volumePreservationStiffness;
989
+ mem[offset + 12] = p.shapeMatchingStiffness;
990
+ mem[offset + 13] = p.edgeYieldThreshold;
991
+ mem[offset + 14] = p.edgePlasticityRate;
992
+ mem[offset + 15] = p.inversionThreshold;
993
+ mem[offset + 16] = p.inversionRestorationStiffness;
994
+ mem[offset + 17] = p.solverIterations;
995
+
996
+ const status = exp.rundot_kinetix_ext_soft_body_step(
997
+ nodePtr, nodeCount,
998
+ tetPtr, tetCount,
999
+ facePtr, faceCount,
1000
+ edgePtr, edgeCount,
1001
+ anchorPtr, anchorCount,
1002
+ bodyPtr, bodyCount,
1003
+ paramsPtr,
1004
+ dt
1005
+ );
1006
+
1007
+ if (status === SoftBodyStatus.Success) {
1008
+ // Unpack nodes
1009
+ for (let i = 0; i < nodeCount; ++i) {
1010
+ const off = i * strides.nodeStride;
1011
+ const n = sim.nodes[i];
1012
+ n.position.x = mem[off + 0];
1013
+ n.position.y = mem[off + 1];
1014
+ n.position.z = mem[off + 2];
1015
+ n.velocity.x = mem[off + 3];
1016
+ n.velocity.y = mem[off + 4];
1017
+ n.velocity.z = mem[off + 5];
1018
+ n.inverseMass = mem[off + 6];
1019
+ n.previousPosition.x = mem[off + 7];
1020
+ n.previousPosition.y = mem[off + 8];
1021
+ n.previousPosition.z = mem[off + 9];
1022
+ }
1023
+
1024
+ // Unpack tetrahedra
1025
+ let totalCurrentVol = 0.0;
1026
+ let invertedCount = 0;
1027
+ let readOff = nodeDoubles;
1028
+ if (tetCount > 0) {
1029
+ for (let t = 0; t < tetCount; ++t) {
1030
+ const off = readOff + t * strides.tetStride;
1031
+ const tet = sim.tetrahedra[t];
1032
+ tet.currentVolume = mem[off + 5];
1033
+ tet.inverted = mem[off + 7] > 0.5;
1034
+ totalCurrentVol += tet.currentVolume;
1035
+ if (tet.inverted) invertedCount++;
1036
+ }
1037
+ }
1038
+ sim.currentTotalVolume = totalCurrentVol;
1039
+ sim.invertedCount = invertedCount;
1040
+ readOff += tetDoubles;
1041
+
1042
+ // Unpack faces
1043
+ if (faceCount > 0) {
1044
+ for (let f = 0; f < faceCount; ++f) {
1045
+ const off = readOff + f * strides.faceStride;
1046
+ const fc = sim.faces[f];
1047
+ fc.area = mem[off + 3];
1048
+ fc.normal.x = mem[off + 4];
1049
+ fc.normal.y = mem[off + 5];
1050
+ fc.normal.z = mem[off + 6];
1051
+ }
1052
+ }
1053
+ readOff += faceDoubles;
1054
+
1055
+ // Unpack edges
1056
+ if (edgeCount > 0) {
1057
+ for (let e = 0; e < edgeCount; ++e) {
1058
+ const off = readOff + e * strides.edgeStride;
1059
+ const ed = sim.edges[e];
1060
+ ed.restLength = mem[off + 2];
1061
+ ed.currentLength = mem[off + 3];
1062
+ ed.plasticStrain = mem[off + 5];
1063
+ }
1064
+ }
1065
+ readOff += edgeDoubles;
1066
+
1067
+ // Unpack anchors
1068
+ if (anchorCount > 0) {
1069
+ for (let a = 0; a < anchorCount; ++a) {
1070
+ const off = readOff + a * strides.anchorStride;
1071
+ const an = sim.anchors[a];
1072
+ an.worldAttachment.x = mem[off + 5];
1073
+ an.worldAttachment.y = mem[off + 6];
1074
+ an.worldAttachment.z = mem[off + 7];
1075
+ an.reactionForce.x = mem[off + 8];
1076
+ an.reactionForce.y = mem[off + 9];
1077
+ an.reactionForce.z = mem[off + 10];
1078
+ }
1079
+ }
1080
+ }
1081
+
1082
+ return status;
1083
+ });
1084
+ }
1085
+
1086
+ export function computeSoftBodyVolume(sim, instanceOverride = null) {
1087
+ const instance = getExtendedInstance(instanceOverride);
1088
+ const exp = instance.exports;
1089
+ const strides = getSoftBodyStrides(instance);
1090
+
1091
+ const nodeCount = sim.nodes.length;
1092
+ const tetCount = sim.tetrahedra.length;
1093
+ const nodeDoubles = nodeCount * strides.nodeStride;
1094
+ const tetDoubles = tetCount * strides.tetStride;
1095
+ const totalDoubles = nodeDoubles + tetDoubles;
1096
+
1097
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
1098
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
1099
+ for (let i = 0; i < nodeCount; ++i) {
1100
+ const n = sim.nodes[i];
1101
+ const off = i * strides.nodeStride;
1102
+ mem[off + 0] = n.position.x;
1103
+ mem[off + 1] = n.position.y;
1104
+ mem[off + 2] = n.position.z;
1105
+ }
1106
+ for (let t = 0; t < tetCount; ++t) {
1107
+ const tet = sim.tetrahedra[t];
1108
+ const off = nodeDoubles + t * strides.tetStride;
1109
+ mem[off + 0] = tet.node0;
1110
+ mem[off + 1] = tet.node1;
1111
+ mem[off + 2] = tet.node2;
1112
+ mem[off + 3] = tet.node3;
1113
+ }
1114
+
1115
+ const nodePtr = basePtr;
1116
+ const tetPtr = basePtr + nodeDoubles * 8;
1117
+ return exp.rundot_kinetix_ext_soft_body_compute_volume(nodePtr, nodeCount, tetPtr, tetCount);
1118
+ });
1119
+ }
1120
+
1121
+ export function solveSoftBodyVolume(sim, volumeStiffness, dt, instanceOverride = null) {
1122
+ const instance = getExtendedInstance(instanceOverride);
1123
+ const exp = instance.exports;
1124
+ const strides = getSoftBodyStrides(instance);
1125
+
1126
+ const nodeCount = sim.nodes.length;
1127
+ const tetCount = sim.tetrahedra.length;
1128
+ const nodeDoubles = nodeCount * strides.nodeStride;
1129
+ const tetDoubles = tetCount * strides.tetStride;
1130
+ const totalDoubles = nodeDoubles + tetDoubles;
1131
+
1132
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
1133
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
1134
+ for (let i = 0; i < nodeCount; ++i) {
1135
+ const n = sim.nodes[i];
1136
+ const off = i * strides.nodeStride;
1137
+ mem[off + 0] = n.position.x;
1138
+ mem[off + 1] = n.position.y;
1139
+ mem[off + 2] = n.position.z;
1140
+ mem[off + 6] = n.inverseMass;
1141
+ }
1142
+ for (let t = 0; t < tetCount; ++t) {
1143
+ const tet = sim.tetrahedra[t];
1144
+ const off = nodeDoubles + t * strides.tetStride;
1145
+ mem[off + 0] = tet.node0;
1146
+ mem[off + 1] = tet.node1;
1147
+ mem[off + 2] = tet.node2;
1148
+ mem[off + 3] = tet.node3;
1149
+ mem[off + 4] = tet.restVolume;
1150
+ mem[off + 5] = tet.currentVolume;
1151
+ mem[off + 6] = tet.volumeStiffness;
1152
+ }
1153
+
1154
+ const nodePtr = basePtr;
1155
+ const tetPtr = basePtr + nodeDoubles * 8;
1156
+ const status = exp.rundot_kinetix_ext_soft_body_solve_volume(
1157
+ nodePtr, nodeCount, tetPtr, tetCount, volumeStiffness, dt
1158
+ );
1159
+
1160
+ if (status === 0) {
1161
+ for (let i = 0; i < nodeCount; ++i) {
1162
+ const off = i * strides.nodeStride;
1163
+ sim.nodes[i].position.x = mem[off + 0];
1164
+ sim.nodes[i].position.y = mem[off + 1];
1165
+ sim.nodes[i].position.z = mem[off + 2];
1166
+ }
1167
+ for (let t = 0; t < tetCount; ++t) {
1168
+ const off = nodeDoubles + t * strides.tetStride;
1169
+ sim.tetrahedra[t].currentVolume = mem[off + 5];
1170
+ }
1171
+ }
1172
+ return status;
1173
+ });
1174
+ }
1175
+
1176
+ export function applySoftBodyPressure(sim, pressure, dt, instanceOverride = null) {
1177
+ const instance = getExtendedInstance(instanceOverride);
1178
+ const exp = instance.exports;
1179
+ const strides = getSoftBodyStrides(instance);
1180
+
1181
+ const nodeCount = sim.nodes.length;
1182
+ const faceCount = sim.faces.length;
1183
+ const nodeDoubles = nodeCount * strides.nodeStride;
1184
+ const faceDoubles = faceCount * strides.faceStride;
1185
+ const totalDoubles = nodeDoubles + faceDoubles;
1186
+
1187
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
1188
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
1189
+ for (let i = 0; i < nodeCount; ++i) {
1190
+ const n = sim.nodes[i];
1191
+ const off = i * strides.nodeStride;
1192
+ mem[off + 0] = n.position.x;
1193
+ mem[off + 1] = n.position.y;
1194
+ mem[off + 2] = n.position.z;
1195
+ mem[off + 6] = n.inverseMass;
1196
+ }
1197
+ for (let f = 0; f < faceCount; ++f) {
1198
+ const fc = sim.faces[f];
1199
+ const off = nodeDoubles + f * strides.faceStride;
1200
+ mem[off + 0] = fc.nodeA;
1201
+ mem[off + 1] = fc.nodeB;
1202
+ mem[off + 2] = fc.nodeC;
1203
+ mem[off + 3] = fc.area;
1204
+ mem[off + 4] = fc.normal.x;
1205
+ mem[off + 5] = fc.normal.y;
1206
+ mem[off + 6] = fc.normal.z;
1207
+ mem[off + 7] = fc.pressureMultiplier;
1208
+ }
1209
+
1210
+ const nodePtr = basePtr;
1211
+ const facePtr = basePtr + nodeDoubles * 8;
1212
+ const status = exp.rundot_kinetix_ext_soft_body_apply_pressure(
1213
+ nodePtr, nodeCount, facePtr, faceCount, pressure, dt
1214
+ );
1215
+
1216
+ if (status === 0) {
1217
+ for (let i = 0; i < nodeCount; ++i) {
1218
+ const off = i * strides.nodeStride;
1219
+ sim.nodes[i].position.x = mem[off + 0];
1220
+ sim.nodes[i].position.y = mem[off + 1];
1221
+ sim.nodes[i].position.z = mem[off + 2];
1222
+ }
1223
+ }
1224
+ return status;
1225
+ });
1226
+ }
1227
+
1228
+ export function handleSoftBodyInversion(sim, threshold = 0.0, stiffness = 0.8, instanceOverride = null) {
1229
+ const instance = getExtendedInstance(instanceOverride);
1230
+ const exp = instance.exports;
1231
+ const strides = getSoftBodyStrides(instance);
1232
+
1233
+ const nodeCount = sim.nodes.length;
1234
+ const tetCount = sim.tetrahedra.length;
1235
+ const nodeDoubles = nodeCount * strides.nodeStride;
1236
+ const tetDoubles = tetCount * strides.tetStride;
1237
+ const totalDoubles = nodeDoubles + tetDoubles;
1238
+
1239
+ return withScratchMemory(instance, totalDoubles * 8, (basePtr) => {
1240
+ const mem = new Float64Array(exp.memory.buffer, basePtr, totalDoubles);
1241
+ for (let i = 0; i < nodeCount; ++i) {
1242
+ const n = sim.nodes[i];
1243
+ const off = i * strides.nodeStride;
1244
+ mem[off + 0] = n.position.x;
1245
+ mem[off + 1] = n.position.y;
1246
+ mem[off + 2] = n.position.z;
1247
+ mem[off + 6] = n.inverseMass;
1248
+ }
1249
+ for (let t = 0; t < tetCount; ++t) {
1250
+ const tet = sim.tetrahedra[t];
1251
+ const off = nodeDoubles + t * strides.tetStride;
1252
+ mem[off + 0] = tet.node0;
1253
+ mem[off + 1] = tet.node1;
1254
+ mem[off + 2] = tet.node2;
1255
+ mem[off + 3] = tet.node3;
1256
+ mem[off + 4] = tet.restVolume;
1257
+ mem[off + 5] = tet.currentVolume;
1258
+ mem[off + 7] = tet.inverted ? 1.0 : 0.0;
1259
+ }
1260
+
1261
+ const nodePtr = basePtr;
1262
+ const tetPtr = basePtr + nodeDoubles * 8;
1263
+ const invertedCount = exp.rundot_kinetix_ext_soft_body_handle_inversion(
1264
+ nodePtr, nodeCount, tetPtr, tetCount, threshold, stiffness
1265
+ );
1266
+
1267
+ for (let i = 0; i < nodeCount; ++i) {
1268
+ const off = i * strides.nodeStride;
1269
+ sim.nodes[i].position.x = mem[off + 0];
1270
+ sim.nodes[i].position.y = mem[off + 1];
1271
+ sim.nodes[i].position.z = mem[off + 2];
1272
+ }
1273
+ for (let t = 0; t < tetCount; ++t) {
1274
+ const off = nodeDoubles + t * strides.tetStride;
1275
+ sim.tetrahedra[t].currentVolume = mem[off + 5];
1276
+ sim.tetrahedra[t].inverted = mem[off + 7] > 0.5;
1277
+ }
1278
+ sim.invertedCount = invertedCount >= 0 ? invertedCount : 0;
1279
+ return invertedCount;
1280
+ });
1281
+ }
1282
+
1283
+ export function probeSoftBody(scenario, instanceOverride = null) {
1284
+ return probeSoftBodyRaw(scenario, instanceOverride);
1285
+ }