@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,1156 @@
1
+ /**
2
+ * Deterministic Cloth and Surface Deformables (Row A02).
3
+ *
4
+ * Exposes Position-Based Dynamics (PBD) simulation of stretchable, shearable,
5
+ * bendable cloth and thin-shell surface deformables routed through the compiled
6
+ * C++ WASM core:
7
+ * - Stretch (structural edges), shear (diagonals), and isometric/dihedral bend constraints.
8
+ * - Pin constraints with hard anchoring or soft spring behavior, dynamic two-way coupling.
9
+ * - Long-range tether constraints to eliminate stretching artifacts under gravity.
10
+ * - Thin-surface aerodynamic drag and lift forces from ambient air density and wind vectors.
11
+ * - Telemetry metrics (maximum tension, shear, bending moment, reaction forces, break events).
12
+ * - Full parity with core/native/physics/cloth.cpp compiled into physics-extended.wasm.
13
+ *
14
+ * Strictly marshals data into WASM linear memory and reads results back.
15
+ * Never falls back to a JavaScript solver to ensure bit-identical cross-runtime determinism.
16
+ */
17
+
18
+ import {
19
+ ensureExtendedInstance as extEnsureInstance,
20
+ extendedPhysicsReady as extPhysicsReady,
21
+ } from './extended-wasm-instance.mjs';
22
+ import {
23
+ physicsExtendedWasmModuleBase64,
24
+ } from './physics-extended-wasm-module.generated.mjs';
25
+
26
+ export const CLOTH_ABI_VERSION = 1;
27
+ export const CLOTH_BODY_STRIDE = 44;
28
+ export const CLOTH_BREAK_EVENT_STRIDE = 4;
29
+ export const CLOTH_MAX_BREAK_EVENTS = 64;
30
+
31
+ export const ClothStatus = Object.freeze({
32
+ Success: 0,
33
+ InvalidInput: -1,
34
+ InvalidNodeCount: -2,
35
+ InvalidParams: -3,
36
+ BufferOverflow: -4,
37
+ });
38
+
39
+ export const ClothBreakReason = Object.freeze({
40
+ None: 0,
41
+ TensionExceeded: 1,
42
+ ShearExceeded: 2,
43
+ BendExceeded: 3,
44
+ });
45
+
46
+ export const ClothGridOrientation = Object.freeze({
47
+ XY: 0, // Hanging vertically facing Z
48
+ XZ: 1, // Horizontal table / trampoline facing Y
49
+ YZ: 2, // Hanging vertically facing X
50
+ });
51
+
52
+ // ---------------------------------------------------------------------------
53
+ // Deterministic Vector Math (double precision only)
54
+ // ---------------------------------------------------------------------------
55
+
56
+ export function addClothVec3(a, b) {
57
+ return { x: a.x + b.x, y: a.y + b.y, z: a.z + b.z };
58
+ }
59
+
60
+ export function subClothVec3(a, b) {
61
+ return { x: a.x - b.x, y: a.y - b.y, z: a.z - b.z };
62
+ }
63
+
64
+ export function scaleClothVec3(v, s) {
65
+ return { x: v.x * s, y: v.y * s, z: v.z * s };
66
+ }
67
+
68
+ export function dotClothVec3(a, b) {
69
+ return a.x * b.x + a.y * b.y + a.z * b.z;
70
+ }
71
+
72
+ export function crossClothVec3(a, b) {
73
+ return {
74
+ x: a.y * b.z - a.z * b.y,
75
+ y: a.z * b.x - a.x * b.z,
76
+ z: a.x * b.y - a.y * b.x,
77
+ };
78
+ }
79
+
80
+ export function lengthSquaredClothVec3(v) {
81
+ return dotClothVec3(v, v);
82
+ }
83
+
84
+ export function lengthClothVec3(v) {
85
+ const sq = lengthSquaredClothVec3(v);
86
+ return sq > 0.0 ? Math.sqrt(sq) : 0.0;
87
+ }
88
+
89
+ export function normalizeClothVec3(v) {
90
+ const len = lengthClothVec3(v);
91
+ if (len <= 1e-15) return { x: 0.0, y: 0.0, z: 0.0 };
92
+ const inv = 1.0 / len;
93
+ return { x: v.x * inv, y: v.y * inv, z: v.z * inv };
94
+ }
95
+
96
+ // ---------------------------------------------------------------------------
97
+ // Cloth Simulation Construction and Factories
98
+ // ---------------------------------------------------------------------------
99
+
100
+ export function createClothNode(options = {}) {
101
+ const pos = options.position ?? { x: options.x ?? 0.0, y: options.y ?? 0.0, z: options.z ?? 0.0 };
102
+ const prevPos = options.previousPosition ?? { ...pos };
103
+ const vel = options.velocity ?? { x: options.vx ?? 0.0, y: options.vy ?? 0.0, z: options.vz ?? 0.0 };
104
+ const norm = options.normal ?? { x: 0.0, y: 1.0, z: 0.0 };
105
+
106
+ let invMass = 1.0;
107
+ if (options.inverseMass !== undefined) {
108
+ invMass = options.inverseMass;
109
+ } else if (options.mass !== undefined) {
110
+ invMass = options.mass > 0.0 ? 1.0 / options.mass : 0.0;
111
+ }
112
+
113
+ return {
114
+ position: { x: pos.x, y: pos.y, z: pos.z },
115
+ previousPosition: { x: prevPos.x, y: prevPos.y, z: prevPos.z },
116
+ velocity: { x: vel.x, y: vel.y, z: vel.z },
117
+ normal: { x: norm.x, y: norm.y, z: norm.z },
118
+ inverseMass: invMass,
119
+ };
120
+ }
121
+
122
+ export function createClothStretchConstraint(options = {}) {
123
+ return {
124
+ nodeA: options.nodeA ?? 0,
125
+ nodeB: options.nodeB ?? 1,
126
+ restLength: options.restLength ?? 1.0,
127
+ stiffness: options.stiffness ?? 1.0,
128
+ damping: options.damping ?? 0.01,
129
+ currentLength: options.currentLength ?? (options.restLength ?? 1.0),
130
+ tension: options.tension ?? 0.0,
131
+ broken: Boolean(options.broken),
132
+ };
133
+ }
134
+
135
+ export function createClothShearConstraint(options = {}) {
136
+ return {
137
+ nodeA: options.nodeA ?? 0,
138
+ nodeB: options.nodeB ?? 1,
139
+ restLength: options.restLength ?? 1.4142135623730951,
140
+ stiffness: options.stiffness ?? 0.8,
141
+ damping: options.damping ?? 0.01,
142
+ currentLength: options.currentLength ?? (options.restLength ?? 1.4142135623730951),
143
+ shearForce: options.shearForce ?? 0.0,
144
+ broken: Boolean(options.broken),
145
+ };
146
+ }
147
+
148
+ export function createClothBendConstraint(options = {}) {
149
+ return {
150
+ nodeA: options.nodeA ?? 0,
151
+ nodeB: options.nodeB ?? 1,
152
+ nodeC: options.nodeC !== undefined ? options.nodeC : 4294967295,
153
+ nodeD: options.nodeD !== undefined ? options.nodeD : 4294967295,
154
+ restAngle: options.restAngle ?? 0.0,
155
+ bendStiffness: options.bendStiffness ?? (options.stiffness ?? 0.5),
156
+ currentAngle: options.currentAngle ?? 0.0,
157
+ bendingMoment: options.bendingMoment ?? 0.0,
158
+ broken: Boolean(options.broken),
159
+ };
160
+ }
161
+
162
+ export function createClothFace(options = {}) {
163
+ const norm = options.normal ?? { x: 0.0, y: 1.0, z: 0.0 };
164
+ return {
165
+ nodeA: options.nodeA ?? 0,
166
+ nodeB: options.nodeB ?? 1,
167
+ nodeC: options.nodeC ?? 2,
168
+ restArea: options.restArea ?? 0.5,
169
+ currentArea: options.currentArea ?? (options.restArea ?? 0.5),
170
+ normal: { x: norm.x, y: norm.y, z: norm.z },
171
+ aerodynamicForce: options.aerodynamicForce ?? 0.0,
172
+ };
173
+ }
174
+
175
+ export function createClothPin(options = {}) {
176
+ const local = options.localOffset ?? options.offset ?? { x: 0.0, y: 0.0, z: 0.0 };
177
+ const world = options.worldPosition ?? { ...local };
178
+ return {
179
+ nodeIndex: options.nodeIndex ?? 0,
180
+ bodyIndex: options.bodyIndex !== undefined ? options.bodyIndex : -1,
181
+ localOffset: { x: local.x, y: local.y, z: local.z },
182
+ worldPosition: { x: world.x, y: world.y, z: world.z },
183
+ pinStiffness: options.pinStiffness ?? (options.stiffness ?? 1.0),
184
+ reactionForce: options.reactionForce ?? 0.0,
185
+ active: options.active !== false,
186
+ };
187
+ }
188
+
189
+ export function createClothTether(options = {}) {
190
+ return {
191
+ nodeIndex: options.nodeIndex ?? 0,
192
+ anchorNodeIndex: options.anchorNodeIndex ?? 0,
193
+ maxDistance: options.maxDistance ?? 1.0,
194
+ stiffness: options.stiffness ?? 1.0,
195
+ currentDistance: options.currentDistance ?? (options.maxDistance ?? 1.0),
196
+ tension: options.tension ?? 0.0,
197
+ active: options.active !== false,
198
+ };
199
+ }
200
+
201
+ export function createClothParams(options = {}) {
202
+ return {
203
+ gravityX: options.gravityX ?? options.gravity?.x ?? 0.0,
204
+ gravityY: options.gravityY ?? options.gravity?.y ?? -9.81,
205
+ gravityZ: options.gravityZ ?? options.gravity?.z ?? 0.0,
206
+ damping: options.damping ?? 0.01,
207
+ airDensity: options.airDensity ?? 1.225,
208
+ windX: options.windX ?? options.wind?.x ?? 0.0,
209
+ windY: options.windY ?? options.wind?.y ?? 0.0,
210
+ windZ: options.windZ ?? options.wind?.z ?? 0.0,
211
+ dragCoefficient: options.dragCoefficient ?? 1.2,
212
+ liftCoefficient: options.liftCoefficient ?? 0.8,
213
+ solverIterations: options.solverIterations ?? 10,
214
+ collisionRadius: options.collisionRadius ?? 0.02,
215
+ enableGround: options.enableGround !== false,
216
+ groundY: options.groundY ?? 0.0,
217
+ groundFriction: options.groundFriction ?? 0.3,
218
+ groundRestitution: options.groundRestitution ?? 0.0,
219
+ breakThreshold: options.breakThreshold ?? 1e9,
220
+ tetherLimitRatio: options.tetherLimitRatio ?? 1.15,
221
+ enableTethers: options.enableTethers !== false,
222
+ enableAerodynamics: options.enableAerodynamics !== false,
223
+ };
224
+ }
225
+
226
+ export function createClothSimulation(options = {}) {
227
+ return {
228
+ id: options.id ?? 0,
229
+ params: createClothParams(options.params),
230
+ nodes: Array.isArray(options.nodes) ? options.nodes.map(createClothNode) : [],
231
+ stretchConstraints: Array.isArray(options.stretchConstraints) ? options.stretchConstraints.map(createClothStretchConstraint) : [],
232
+ shearConstraints: Array.isArray(options.shearConstraints) ? options.shearConstraints.map(createClothShearConstraint) : [],
233
+ bendConstraints: Array.isArray(options.bendConstraints) ? options.bendConstraints.map(createClothBendConstraint) : [],
234
+ faces: Array.isArray(options.faces) ? options.faces.map(createClothFace) : [],
235
+ pins: Array.isArray(options.pins) ? options.pins.map(createClothPin) : [],
236
+ tethers: Array.isArray(options.tethers) ? options.tethers.map(createClothTether) : [],
237
+ breakEvents: [],
238
+ maxTension: 0.0,
239
+ maxShear: 0.0,
240
+ maxBend: 0.0,
241
+ totalAerodynamicForce: 0.0,
242
+ };
243
+ }
244
+
245
+ export function addClothPin(cloth, nodeIndex, bodyIndex = -1, localOffset = { x: 0, y: 0, z: 0 }, pinStiffness = 1.0) {
246
+ const pin = createClothPin({
247
+ nodeIndex,
248
+ bodyIndex,
249
+ localOffset,
250
+ worldPosition: { ...localOffset },
251
+ pinStiffness,
252
+ active: true,
253
+ });
254
+ cloth.pins.push(pin);
255
+ return cloth.pins.length - 1;
256
+ }
257
+
258
+ export function addClothTether(cloth, nodeIndex, anchorNodeIndex, maxDistance, stiffness = 1.0) {
259
+ const tether = createClothTether({
260
+ nodeIndex,
261
+ anchorNodeIndex,
262
+ maxDistance,
263
+ stiffness,
264
+ currentDistance: maxDistance,
265
+ tension: 0.0,
266
+ active: true,
267
+ });
268
+ cloth.tethers.push(tether);
269
+ return cloth.tethers.length - 1;
270
+ }
271
+
272
+ /**
273
+ * Generates regular grid topology matching native initClothGrid in C++.
274
+ */
275
+ export function initClothGridData(
276
+ cloth,
277
+ width = 2.0,
278
+ height = 2.0,
279
+ cols = 4,
280
+ rows = 4,
281
+ orientation = ClothGridOrientation.XY,
282
+ totalMass = 1.0,
283
+ stretchStiffness = 0.95,
284
+ shearStiffness = 0.8,
285
+ bendStiffness = 0.5,
286
+ pinTopCorners = true,
287
+ pinTopEdge = false
288
+ ) {
289
+ if (cols < 2 || rows < 2) {
290
+ throw new Error('KINETIX_CLOTH_INVALID_GRID_DIMENSIONS: cols and rows must be >= 2');
291
+ }
292
+
293
+ const totalNodes = cols * rows;
294
+ const dx = width / (cols - 1);
295
+ const dy = height / (rows - 1);
296
+ const nodeMass = totalMass > 0.0 ? totalMass / totalNodes : 1.0;
297
+ const nodeInvMass = 1.0 / nodeMass;
298
+
299
+ cloth.nodes = [];
300
+ cloth.pins = [];
301
+
302
+ for (let r = 0; r < rows; ++r) {
303
+ for (let c = 0; c < cols; ++c) {
304
+ const idx = r * cols + c;
305
+ let pos = { x: 0.0, y: 0.0, z: 0.0 };
306
+ if (orientation === ClothGridOrientation.XY) {
307
+ pos = {
308
+ x: c * dx - width * 0.5,
309
+ y: height * 0.5 - r * dy,
310
+ z: 0.0,
311
+ };
312
+ } else if (orientation === ClothGridOrientation.XZ) {
313
+ pos = {
314
+ x: c * dx - width * 0.5,
315
+ y: 0.0,
316
+ z: r * dy - height * 0.5,
317
+ };
318
+ } else {
319
+ pos = {
320
+ x: 0.0,
321
+ y: height * 0.5 - r * dy,
322
+ z: c * dx - width * 0.5,
323
+ };
324
+ }
325
+
326
+ const isCorner = (r === 0 && (c === 0 || c === cols - 1));
327
+ const isTopEdge = (r === 0);
328
+ const isPinned = (pinTopCorners && isCorner) || (pinTopEdge && isTopEdge);
329
+
330
+ cloth.nodes.push({
331
+ position: { ...pos },
332
+ previousPosition: { ...pos },
333
+ velocity: { x: 0.0, y: 0.0, z: 0.0 },
334
+ normal: { x: 0.0, y: 1.0, z: 0.0 },
335
+ inverseMass: isPinned ? 0.0 : nodeInvMass,
336
+ });
337
+
338
+ if (isPinned) {
339
+ addClothPin(cloth, idx, -1, pos, 1.0);
340
+ }
341
+ }
342
+ }
343
+
344
+ cloth.stretchConstraints = [];
345
+ for (let r = 0; r < rows; ++r) {
346
+ for (let c = 0; c < cols; ++c) {
347
+ const idx = r * cols + c;
348
+ if (c + 1 < cols) {
349
+ cloth.stretchConstraints.push(createClothStretchConstraint({
350
+ nodeA: idx,
351
+ nodeB: idx + 1,
352
+ restLength: dx,
353
+ stiffness: stretchStiffness,
354
+ damping: 0.01,
355
+ currentLength: dx,
356
+ tension: 0.0,
357
+ broken: false,
358
+ }));
359
+ }
360
+ if (r + 1 < rows) {
361
+ cloth.stretchConstraints.push(createClothStretchConstraint({
362
+ nodeA: idx,
363
+ nodeB: idx + cols,
364
+ restLength: dy,
365
+ stiffness: stretchStiffness,
366
+ damping: 0.01,
367
+ currentLength: dy,
368
+ tension: 0.0,
369
+ broken: false,
370
+ }));
371
+ }
372
+ }
373
+ }
374
+
375
+ cloth.shearConstraints = [];
376
+ const diagLen = Math.sqrt(dx * dx + dy * dy);
377
+ for (let r = 0; r + 1 < rows; ++r) {
378
+ for (let c = 0; c + 1 < cols; ++c) {
379
+ const v00 = r * cols + c;
380
+ const v10 = v00 + 1;
381
+ const v01 = v00 + cols;
382
+ const v11 = v01 + 1;
383
+
384
+ cloth.shearConstraints.push(createClothShearConstraint({
385
+ nodeA: v00,
386
+ nodeB: v11,
387
+ restLength: diagLen,
388
+ stiffness: shearStiffness,
389
+ damping: 0.01,
390
+ currentLength: diagLen,
391
+ shearForce: 0.0,
392
+ broken: false,
393
+ }));
394
+ cloth.shearConstraints.push(createClothShearConstraint({
395
+ nodeA: v10,
396
+ nodeB: v01,
397
+ restLength: diagLen,
398
+ stiffness: shearStiffness,
399
+ damping: 0.01,
400
+ currentLength: diagLen,
401
+ shearForce: 0.0,
402
+ broken: false,
403
+ }));
404
+ }
405
+ }
406
+
407
+ cloth.bendConstraints = [];
408
+ for (let r = 0; r < rows; ++r) {
409
+ for (let c = 0; c < cols; ++c) {
410
+ const idx = r * cols + c;
411
+ if (c + 2 < cols) {
412
+ cloth.bendConstraints.push(createClothBendConstraint({
413
+ nodeA: idx,
414
+ nodeB: idx + 2,
415
+ nodeC: idx + 1,
416
+ nodeD: 4294967295,
417
+ restAngle: 0.0,
418
+ bendStiffness: bendStiffness,
419
+ currentAngle: 0.0,
420
+ bendingMoment: 0.0,
421
+ broken: false,
422
+ }));
423
+ }
424
+ if (r + 2 < rows) {
425
+ cloth.bendConstraints.push(createClothBendConstraint({
426
+ nodeA: idx,
427
+ nodeB: idx + 2 * cols,
428
+ nodeC: idx + cols,
429
+ nodeD: 4294967295,
430
+ restAngle: 0.0,
431
+ bendStiffness: bendStiffness,
432
+ currentAngle: 0.0,
433
+ bendingMoment: 0.0,
434
+ broken: false,
435
+ }));
436
+ }
437
+ }
438
+ }
439
+
440
+ cloth.faces = [];
441
+ const faceArea = 0.5 * dx * dy;
442
+ for (let r = 0; r + 1 < rows; ++r) {
443
+ for (let c = 0; c + 1 < cols; ++c) {
444
+ const v00 = r * cols + c;
445
+ const v10 = v00 + 1;
446
+ const v01 = v00 + cols;
447
+ const v11 = v01 + 1;
448
+
449
+ cloth.faces.push(createClothFace({
450
+ nodeA: v00,
451
+ nodeB: v01,
452
+ nodeC: v11,
453
+ restArea: faceArea,
454
+ currentArea: faceArea,
455
+ normal: { x: 0.0, y: 1.0, z: 0.0 },
456
+ aerodynamicForce: 0.0,
457
+ }));
458
+ cloth.faces.push(createClothFace({
459
+ nodeA: v00,
460
+ nodeB: v11,
461
+ nodeC: v10,
462
+ restArea: faceArea,
463
+ currentArea: faceArea,
464
+ normal: { x: 0.0, y: 1.0, z: 0.0 },
465
+ aerodynamicForce: 0.0,
466
+ }));
467
+ }
468
+ }
469
+
470
+ cloth.tethers = [];
471
+ if (pinTopCorners || pinTopEdge) {
472
+ const anchorLeft = 0;
473
+ const anchorRight = cols - 1;
474
+ const tetherRatio = cloth.params?.tetherLimitRatio ?? 1.15;
475
+ for (let r = 1; r < rows; ++r) {
476
+ for (let c = 0; c < cols; ++c) {
477
+ const idx = r * cols + c;
478
+ const anchor = (c < Math.floor(cols / 2)) ? anchorLeft : anchorRight;
479
+ const delta = subClothVec3(cloth.nodes[idx].position, cloth.nodes[anchor].position);
480
+ const restDist = lengthClothVec3(delta);
481
+ addClothTether(cloth, idx, anchor, restDist * tetherRatio, 1.0);
482
+ }
483
+ }
484
+ }
485
+
486
+ return cloth;
487
+ }
488
+
489
+ export function createClothGrid(options = {}) {
490
+ const cloth = createClothSimulation(options);
491
+ initClothGridData(
492
+ cloth,
493
+ options.width ?? 2.0,
494
+ options.height ?? 2.0,
495
+ options.cols ?? 4,
496
+ options.rows ?? 4,
497
+ options.orientation ?? ClothGridOrientation.XY,
498
+ options.totalMass ?? 1.0,
499
+ options.stretchStiffness ?? 0.95,
500
+ options.shearStiffness ?? 0.8,
501
+ options.bendStiffness ?? 0.5,
502
+ options.pinTopCorners !== undefined ? Boolean(options.pinTopCorners) : true,
503
+ options.pinTopEdge !== undefined ? Boolean(options.pinTopEdge) : false
504
+ );
505
+ return cloth;
506
+ }
507
+
508
+ // ---------------------------------------------------------------------------
509
+ // WASM Instance Lifecycle & Memory Scratch
510
+ // ---------------------------------------------------------------------------
511
+
512
+ let cachedInstance = null;
513
+
514
+ function decodeBase64(value) {
515
+ if (typeof atob === 'function') {
516
+ const decoded = atob(value);
517
+ const bytes = new Uint8Array(decoded.length);
518
+ for (let index = 0; index < decoded.length; index += 1) bytes[index] = decoded.charCodeAt(index);
519
+ return bytes;
520
+ }
521
+ if (typeof Buffer !== 'undefined') return Uint8Array.from(Buffer.from(value, 'base64'));
522
+ throw new Error('KINETIX_EXTENDED_PHYSICS_BASE64_UNAVAILABLE');
523
+ }
524
+
525
+ export async function initCloth() {
526
+ if (cachedInstance !== null) return cachedInstance;
527
+ cachedInstance = await extEnsureInstance();
528
+ return cachedInstance;
529
+ }
530
+
531
+ export async function extendedPhysicsReady() {
532
+ const ready = await extPhysicsReady();
533
+ if (cachedInstance === null) {
534
+ cachedInstance = await extEnsureInstance();
535
+ }
536
+ return ready;
537
+ }
538
+
539
+ export async function ensureExtendedInstance() {
540
+ cachedInstance = await extEnsureInstance();
541
+ return cachedInstance;
542
+ }
543
+
544
+ export function clothReady() {
545
+ return cachedInstance !== null;
546
+ }
547
+
548
+ export function setClothWasmInstance(instance) {
549
+ cachedInstance = instance;
550
+ }
551
+
552
+ export function getClothWasmInstance(instanceOverride = null) {
553
+ if (instanceOverride) return instanceOverride;
554
+ if (cachedInstance !== null) return cachedInstance;
555
+
556
+ try {
557
+ const bytes = decodeBase64(physicsExtendedWasmModuleBase64);
558
+ const mod = new WebAssembly.Module(bytes);
559
+ const inst = new WebAssembly.Instance(mod, {});
560
+ inst.exports._initialize?.();
561
+ cachedInstance = inst;
562
+ return cachedInstance;
563
+ } catch (error) {
564
+ const detail = error instanceof Error ? error.message : String(error);
565
+ throw new Error(
566
+ `KINETIX_CLOTH_WASM_UNAVAILABLE: ${detail}. Await extendedPhysicsReady() or ensureExtendedInstance() before calling cloth simulation.`,
567
+ { cause: error }
568
+ );
569
+ }
570
+ }
571
+
572
+ const SCRATCH_MEMORY_START = 8 * 1024 * 1024; // 8 MB
573
+ const SAFE_STACK_POINTER = 4 * 1024 * 1024; // 4 MB (downwards to 2 MB)
574
+
575
+ function align16(value) {
576
+ return (value + 15) & ~15;
577
+ }
578
+
579
+ function withSafeEnvironment(instance, byteLength, fn) {
580
+ const exp = instance.exports;
581
+ const origStack = typeof exp.emscripten_stack_get_current === 'function'
582
+ ? exp.emscripten_stack_get_current()
583
+ : null;
584
+
585
+ if (origStack !== null && typeof exp._emscripten_stack_restore === 'function') {
586
+ exp._emscripten_stack_restore(SAFE_STACK_POINTER);
587
+ }
588
+
589
+ let ptr = 0;
590
+ const useMalloc = typeof exp.malloc === 'function';
591
+
592
+ if (useMalloc) {
593
+ ptr = exp.malloc(byteLength);
594
+ if (!ptr) {
595
+ throw new Error('KINETIX_CLOTH_MALLOC_FAILED');
596
+ }
597
+ } else {
598
+ if (!instance._scratchBase) {
599
+ instance._scratchBase = SCRATCH_MEMORY_START;
600
+ instance._scratchOffset = instance._scratchBase;
601
+ }
602
+ const aligned = align16(byteLength);
603
+ ptr = instance._scratchOffset;
604
+ instance._scratchOffset += aligned;
605
+ if (instance._scratchOffset > exp.memory.buffer.byteLength) {
606
+ const pagesNeeded = Math.ceil((instance._scratchOffset - exp.memory.buffer.byteLength) / 65536);
607
+ exp.memory.grow(pagesNeeded);
608
+ }
609
+ }
610
+
611
+ const prevOffset = instance._scratchOffset;
612
+
613
+ try {
614
+ return fn(ptr);
615
+ } finally {
616
+ if (useMalloc && typeof exp.free === 'function') {
617
+ exp.free(ptr);
618
+ } else if (!useMalloc) {
619
+ instance._scratchOffset = prevOffset;
620
+ }
621
+ if (origStack !== null && typeof exp._emscripten_stack_restore === 'function') {
622
+ exp._emscripten_stack_restore(origStack);
623
+ }
624
+ }
625
+ }
626
+
627
+ // ---------------------------------------------------------------------------
628
+ // Strides, Limits, and Probes Queries
629
+ // ---------------------------------------------------------------------------
630
+
631
+ export function getClothStrides(wasmInstance = null) {
632
+ const instance = getClothWasmInstance(wasmInstance);
633
+ const exp = instance.exports;
634
+ return {
635
+ nodeStride: exp.rundot_kinetix_ext_cloth_node_stride(),
636
+ stretchStride: exp.rundot_kinetix_ext_cloth_stretch_stride(),
637
+ shearStride: exp.rundot_kinetix_ext_cloth_shear_stride(),
638
+ bendStride: exp.rundot_kinetix_ext_cloth_bend_stride(),
639
+ faceStride: exp.rundot_kinetix_ext_cloth_face_stride(),
640
+ pinStride: exp.rundot_kinetix_ext_cloth_pin_stride(),
641
+ tetherStride: exp.rundot_kinetix_ext_cloth_tether_stride(),
642
+ paramsStride: exp.rundot_kinetix_ext_cloth_params_stride(),
643
+ clothStride: exp.rundot_kinetix_ext_cloth_stride(),
644
+ clothSize: exp.rundot_kinetix_ext_cloth_size(),
645
+ };
646
+ }
647
+
648
+ export function getClothLimits(wasmInstance = null) {
649
+ const instance = getClothWasmInstance(wasmInstance);
650
+ const exp = instance.exports;
651
+ return {
652
+ maxNodes: exp.rundot_kinetix_ext_cloth_max_nodes(),
653
+ maxFaces: exp.rundot_kinetix_ext_cloth_max_faces(),
654
+ maxPins: exp.rundot_kinetix_ext_cloth_max_pins(),
655
+ };
656
+ }
657
+
658
+ export function getClothNodeStride(wasmInstance = null) {
659
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_node_stride();
660
+ }
661
+
662
+ export function getClothStretchStride(wasmInstance = null) {
663
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_stretch_stride();
664
+ }
665
+
666
+ export function getClothShearStride(wasmInstance = null) {
667
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_shear_stride();
668
+ }
669
+
670
+ export function getClothBendStride(wasmInstance = null) {
671
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_bend_stride();
672
+ }
673
+
674
+ export function getClothFaceStride(wasmInstance = null) {
675
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_face_stride();
676
+ }
677
+
678
+ export function getClothPinStride(wasmInstance = null) {
679
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_pin_stride();
680
+ }
681
+
682
+ export function getClothTetherStride(wasmInstance = null) {
683
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_tether_stride();
684
+ }
685
+
686
+ export function getClothParamsStride(wasmInstance = null) {
687
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_params_stride();
688
+ }
689
+
690
+ export function getClothMaxNodes(wasmInstance = null) {
691
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_max_nodes();
692
+ }
693
+
694
+ export function getClothMaxFaces(wasmInstance = null) {
695
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_max_faces();
696
+ }
697
+
698
+ export function getClothMaxPins(wasmInstance = null) {
699
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_max_pins();
700
+ }
701
+
702
+ export function getClothProbeSize(wasmInstance = null) {
703
+ return getClothWasmInstance(wasmInstance).exports.rundot_kinetix_ext_cloth_probe_size();
704
+ }
705
+
706
+ export function runClothProbe(scenario = 0, wasmInstance = null) {
707
+ const instance = getClothWasmInstance(wasmInstance);
708
+ const exp = instance.exports;
709
+ const size = exp.rundot_kinetix_ext_cloth_probe_size();
710
+ const ptr = exp.rundot_kinetix_ext_cloth_probe(scenario);
711
+ if (!ptr) {
712
+ throw new Error(`KINETIX_CLOTH_PROBE_FAILED: scenario ${scenario}`);
713
+ }
714
+ const view = new Float64Array(exp.memory.buffer, ptr, size);
715
+ return Float64Array.from(view);
716
+ }
717
+
718
+ export const getClothProbe = runClothProbe;
719
+
720
+ // ---------------------------------------------------------------------------
721
+ // Core Simulation Stepping (WASM Routed)
722
+ // ---------------------------------------------------------------------------
723
+
724
+ /**
725
+ * Low-level C-ABI step taking flat Float64Array buffers.
726
+ */
727
+ export function stepClothRaw(
728
+ nodeData, nodeCount,
729
+ stretchData, stretchCount,
730
+ shearData, shearCount,
731
+ bendData, bendCount,
732
+ faceData, faceCount,
733
+ pinData, pinCount,
734
+ tetherData, tetherCount,
735
+ bodyData, bodyCount,
736
+ paramsData,
737
+ breakEventsData, breakEventCountPtr,
738
+ dt,
739
+ wasmInstance = null
740
+ ) {
741
+ const instance = getClothWasmInstance(wasmInstance);
742
+ const exp = instance.exports;
743
+ return exp.rundot_kinetix_ext_cloth_step(
744
+ nodeData, nodeCount,
745
+ stretchData, stretchCount,
746
+ shearData, shearCount,
747
+ bendData, bendCount,
748
+ faceData, faceCount,
749
+ pinData, pinCount,
750
+ tetherData, tetherCount,
751
+ bodyData, bodyCount,
752
+ paramsData,
753
+ breakEventsData, breakEventCountPtr,
754
+ dt
755
+ );
756
+ }
757
+
758
+ /**
759
+ * Steps the cloth simulation deterministically by dt seconds using the C++ WASM engine.
760
+ * Marshals nodes, constraints, faces, pins, tethers, parameters, and bodies into WASM linear
761
+ * memory, calls rundot_kinetix_ext_cloth_step, and unmarshals the updated physical state back.
762
+ *
763
+ * @param {object} cloth - ClothSimulation object created by createClothSimulation or createClothGrid
764
+ * @param {Array<object>} bodies - Optional rigid bodies for obstacle and pin coupling
765
+ * @param {number} dt - Timestep in seconds (e.g. 1/60)
766
+ * @param {object} wasmInstance - Optional explicit WebAssembly instance override
767
+ * @returns {object} The updated cloth simulation object
768
+ */
769
+ export function stepCloth(cloth, bodies = [], dt = 1.0 / 60.0, wasmInstance = null) {
770
+ if (dt <= 0.0 || !cloth || !Array.isArray(cloth.nodes) || cloth.nodes.length < 3) {
771
+ return cloth;
772
+ }
773
+
774
+ const instance = getClothWasmInstance(wasmInstance);
775
+ const exp = instance.exports;
776
+
777
+ // Stride queries directly from WASM ABI
778
+ const nodeStride = exp.rundot_kinetix_ext_cloth_node_stride();
779
+ const stretchStride = exp.rundot_kinetix_ext_cloth_stretch_stride();
780
+ const shearStride = exp.rundot_kinetix_ext_cloth_shear_stride();
781
+ const bendStride = exp.rundot_kinetix_ext_cloth_bend_stride();
782
+ const faceStride = exp.rundot_kinetix_ext_cloth_face_stride();
783
+ const pinStride = exp.rundot_kinetix_ext_cloth_pin_stride();
784
+ const tetherStride = exp.rundot_kinetix_ext_cloth_tether_stride();
785
+ const paramsStride = exp.rundot_kinetix_ext_cloth_params_stride();
786
+
787
+ // Capacity queries
788
+ const maxNodes = exp.rundot_kinetix_ext_cloth_max_nodes();
789
+ const maxFaces = exp.rundot_kinetix_ext_cloth_max_faces();
790
+ const maxPins = exp.rundot_kinetix_ext_cloth_max_pins();
791
+
792
+ const nodeCount = Math.min(cloth.nodes.length, maxNodes);
793
+ const stretchCount = Array.isArray(cloth.stretchConstraints) ? cloth.stretchConstraints.length : 0;
794
+ const shearCount = Array.isArray(cloth.shearConstraints) ? cloth.shearConstraints.length : 0;
795
+ const bendCount = Array.isArray(cloth.bendConstraints) ? cloth.bendConstraints.length : 0;
796
+ const faceCount = Array.isArray(cloth.faces) ? Math.min(cloth.faces.length, maxFaces) : 0;
797
+ const pinCount = Array.isArray(cloth.pins) ? Math.min(cloth.pins.length, maxPins) : 0;
798
+ const tetherCount = Array.isArray(cloth.tethers) ? cloth.tethers.length : 0;
799
+ const bodyCount = Array.isArray(bodies) ? Math.min(bodies.length, 64) : 0;
800
+
801
+ // Compute memory bytes with 16-byte alignment
802
+ const nodeBytes = align16(nodeCount * nodeStride * 8);
803
+ const stretchBytes = align16(stretchCount * stretchStride * 8);
804
+ const shearBytes = align16(shearCount * shearStride * 8);
805
+ const bendBytes = align16(bendCount * bendStride * 8);
806
+ const faceBytes = align16(faceCount * faceStride * 8);
807
+ const pinBytes = align16(pinCount * pinStride * 8);
808
+ const tetherBytes = align16(tetherCount * tetherStride * 8);
809
+ const bodyBytes = align16(bodyCount * CLOTH_BODY_STRIDE * 8);
810
+ const paramsBytes = align16(paramsStride * 8);
811
+ const breakEventsBytes = align16(CLOTH_MAX_BREAK_EVENTS * CLOTH_BREAK_EVENT_STRIDE * 8);
812
+ const breakCountBytes = 16; // 4 bytes uint32 + padding
813
+
814
+ const totalBytes = nodeBytes + stretchBytes + shearBytes + bendBytes + faceBytes +
815
+ pinBytes + tetherBytes + bodyBytes + paramsBytes +
816
+ breakEventsBytes + breakCountBytes;
817
+
818
+ return withSafeEnvironment(instance, totalBytes, (basePtr) => {
819
+ const memBuf = exp.memory.buffer;
820
+ const f64 = new Float64Array(memBuf);
821
+ const u32 = new Uint32Array(memBuf);
822
+
823
+ let curPtr = basePtr;
824
+ const nodePtr = curPtr; curPtr += nodeBytes;
825
+ const stretchPtr = stretchCount > 0 ? curPtr : 0; curPtr += stretchBytes;
826
+ const shearPtr = shearCount > 0 ? curPtr : 0; curPtr += shearBytes;
827
+ const bendPtr = bendCount > 0 ? curPtr : 0; curPtr += bendBytes;
828
+ const facePtr = faceCount > 0 ? curPtr : 0; curPtr += faceBytes;
829
+ const pinPtr = pinCount > 0 ? curPtr : 0; curPtr += pinBytes;
830
+ const tetherPtr = tetherCount > 0 ? curPtr : 0; curPtr += tetherBytes;
831
+ const bodyPtr = bodyCount > 0 ? curPtr : 0; curPtr += bodyBytes;
832
+ const paramsPtr = curPtr; curPtr += paramsBytes;
833
+ const breakEventsPtr = curPtr; curPtr += breakEventsBytes;
834
+ const breakCountPtr = curPtr;
835
+
836
+ // Marshal Nodes
837
+ for (let i = 0; i < nodeCount; ++i) {
838
+ const n = cloth.nodes[i];
839
+ const o = (nodePtr / 8) + i * nodeStride;
840
+ f64[o + 0] = n.position.x;
841
+ f64[o + 1] = n.position.y;
842
+ f64[o + 2] = n.position.z;
843
+ f64[o + 3] = n.previousPosition ? n.previousPosition.x : n.position.x;
844
+ f64[o + 4] = n.previousPosition ? n.previousPosition.y : n.position.y;
845
+ f64[o + 5] = n.previousPosition ? n.previousPosition.z : n.position.z;
846
+ f64[o + 6] = n.velocity ? n.velocity.x : 0.0;
847
+ f64[o + 7] = n.velocity ? n.velocity.y : 0.0;
848
+ f64[o + 8] = n.velocity ? n.velocity.z : 0.0;
849
+ f64[o + 9] = n.normal ? n.normal.x : 0.0;
850
+ f64[o + 10] = n.normal ? n.normal.y : 1.0;
851
+ f64[o + 11] = n.normal ? n.normal.z : 0.0;
852
+ f64[o + 12] = n.inverseMass !== undefined ? n.inverseMass : (n.mass > 0 ? 1.0 / n.mass : 0.0);
853
+ }
854
+
855
+ // Marshal Stretch Constraints
856
+ if (stretchCount > 0) {
857
+ for (let i = 0; i < stretchCount; ++i) {
858
+ const s = cloth.stretchConstraints[i];
859
+ const o = (stretchPtr / 8) + i * stretchStride;
860
+ f64[o + 0] = s.nodeA;
861
+ f64[o + 1] = s.nodeB;
862
+ f64[o + 2] = s.restLength;
863
+ f64[o + 3] = s.stiffness;
864
+ f64[o + 4] = s.damping ?? 0.01;
865
+ f64[o + 5] = s.currentLength ?? s.restLength;
866
+ f64[o + 6] = s.tension ?? 0.0;
867
+ f64[o + 7] = s.broken ? 1.0 : 0.0;
868
+ }
869
+ }
870
+
871
+ // Marshal Shear Constraints
872
+ if (shearCount > 0) {
873
+ for (let i = 0; i < shearCount; ++i) {
874
+ const sh = cloth.shearConstraints[i];
875
+ const o = (shearPtr / 8) + i * shearStride;
876
+ f64[o + 0] = sh.nodeA;
877
+ f64[o + 1] = sh.nodeB;
878
+ f64[o + 2] = sh.restLength;
879
+ f64[o + 3] = sh.stiffness;
880
+ f64[o + 4] = sh.damping ?? 0.01;
881
+ f64[o + 5] = sh.currentLength ?? sh.restLength;
882
+ f64[o + 6] = sh.shearForce ?? 0.0;
883
+ f64[o + 7] = sh.broken ? 1.0 : 0.0;
884
+ }
885
+ }
886
+
887
+ // Marshal Bend Constraints
888
+ if (bendCount > 0) {
889
+ for (let i = 0; i < bendCount; ++i) {
890
+ const b = cloth.bendConstraints[i];
891
+ const o = (bendPtr / 8) + i * bendStride;
892
+ f64[o + 0] = b.nodeA;
893
+ f64[o + 1] = b.nodeB;
894
+ f64[o + 2] = b.nodeC !== undefined ? b.nodeC : 4294967295;
895
+ f64[o + 3] = b.nodeD !== undefined ? b.nodeD : 4294967295;
896
+ f64[o + 4] = b.restAngle ?? 0.0;
897
+ f64[o + 5] = b.bendStiffness ?? (b.stiffness ?? 0.5);
898
+ f64[o + 6] = b.currentAngle ?? 0.0;
899
+ f64[o + 7] = b.bendingMoment ?? 0.0;
900
+ f64[o + 8] = b.broken ? 1.0 : 0.0;
901
+ }
902
+ }
903
+
904
+ // Marshal Faces
905
+ if (faceCount > 0) {
906
+ for (let i = 0; i < faceCount; ++i) {
907
+ const fc = cloth.faces[i];
908
+ const o = (facePtr / 8) + i * faceStride;
909
+ f64[o + 0] = fc.nodeA;
910
+ f64[o + 1] = fc.nodeB;
911
+ f64[o + 2] = fc.nodeC;
912
+ f64[o + 3] = fc.restArea;
913
+ f64[o + 4] = fc.currentArea ?? fc.restArea;
914
+ f64[o + 5] = fc.normal ? fc.normal.x : 0.0;
915
+ f64[o + 6] = fc.normal ? fc.normal.y : 1.0;
916
+ f64[o + 7] = fc.normal ? fc.normal.z : 0.0;
917
+ f64[o + 8] = fc.aerodynamicForce ? (typeof fc.aerodynamicForce === 'number' ? fc.aerodynamicForce : fc.aerodynamicForce.x) : 0.0;
918
+ }
919
+ }
920
+
921
+ // Marshal Pins
922
+ if (pinCount > 0) {
923
+ for (let i = 0; i < pinCount; ++i) {
924
+ const p = cloth.pins[i];
925
+ const o = (pinPtr / 8) + i * pinStride;
926
+ f64[o + 0] = p.nodeIndex;
927
+ f64[o + 1] = p.bodyIndex !== undefined ? p.bodyIndex : -1;
928
+ f64[o + 2] = p.localOffset ? p.localOffset.x : 0.0;
929
+ f64[o + 3] = p.localOffset ? p.localOffset.y : 0.0;
930
+ f64[o + 4] = p.localOffset ? p.localOffset.z : 0.0;
931
+ f64[o + 5] = p.worldPosition ? p.worldPosition.x : f64[o + 2];
932
+ f64[o + 6] = p.worldPosition ? p.worldPosition.y : f64[o + 3];
933
+ f64[o + 7] = p.worldPosition ? p.worldPosition.z : f64[o + 4];
934
+ f64[o + 8] = p.pinStiffness ?? (p.stiffness ?? 1.0);
935
+ f64[o + 9] = p.reactionForce ?? 0.0;
936
+ f64[o + 10] = p.active !== false ? 1.0 : 0.0;
937
+ }
938
+ }
939
+
940
+ // Marshal Tethers
941
+ if (tetherCount > 0) {
942
+ for (let i = 0; i < tetherCount; ++i) {
943
+ const t = cloth.tethers[i];
944
+ const o = (tetherPtr / 8) + i * tetherStride;
945
+ f64[o + 0] = t.nodeIndex;
946
+ f64[o + 1] = t.anchorNodeIndex;
947
+ f64[o + 2] = t.maxDistance;
948
+ f64[o + 3] = t.stiffness ?? 1.0;
949
+ f64[o + 4] = t.currentDistance ?? t.maxDistance;
950
+ f64[o + 5] = t.tension ?? 0.0;
951
+ f64[o + 6] = t.active !== false ? 1.0 : 0.0;
952
+ }
953
+ }
954
+
955
+ // Marshal Bodies
956
+ if (bodyCount > 0) {
957
+ for (let i = 0; i < bodyCount; ++i) {
958
+ const b = bodies[i];
959
+ const o = (bodyPtr / 8) + i * CLOTH_BODY_STRIDE;
960
+ f64[o + 0] = b.x ?? 0.0;
961
+ f64[o + 1] = b.y ?? 0.0;
962
+ f64[o + 2] = b.z ?? 0.0;
963
+ f64[o + 3] = b.vx ?? 0.0;
964
+ f64[o + 4] = b.vy ?? 0.0;
965
+ f64[o + 5] = b.vz ?? 0.0;
966
+ f64[o + 9] = (b.dynamic || b.kind === 'dynamic') ? 1.0 : 0.0;
967
+ const isSphere = b.shape === 1 || b.shape?.type === 'sphere';
968
+ f64[o + 10] = isSphere ? 1.0 : 0.0;
969
+ f64[o + 18] = b.mass ?? 1.0;
970
+ const q = b.orientation || b;
971
+ f64[o + 22] = q.orientationX ?? q.x ?? 0.0;
972
+ f64[o + 23] = q.orientationY ?? q.y ?? 0.0;
973
+ f64[o + 24] = q.orientationZ ?? q.z ?? 0.0;
974
+ f64[o + 25] = q.orientationW ?? q.w ?? 1.0;
975
+ f64[o + 30] = b.shape0 ?? b.shape?.radius ?? 0.5;
976
+ f64[o + 31] = b.shape1 ?? 0.0;
977
+ f64[o + 32] = b.shape2 ?? 0.0;
978
+ }
979
+ }
980
+
981
+ // Marshal Parameters
982
+ const prOff = paramsPtr / 8;
983
+ const p = cloth.params || {};
984
+ f64[prOff + 0] = p.gravityX ?? p.gravity?.x ?? 0.0;
985
+ f64[prOff + 1] = p.gravityY ?? p.gravity?.y ?? -9.81;
986
+ f64[prOff + 2] = p.gravityZ ?? p.gravity?.z ?? 0.0;
987
+ f64[prOff + 3] = p.damping ?? 0.01;
988
+ f64[prOff + 4] = p.airDensity ?? 1.225;
989
+ f64[prOff + 5] = p.windX ?? p.wind?.x ?? 0.0;
990
+ f64[prOff + 6] = p.windY ?? p.wind?.y ?? 0.0;
991
+ f64[prOff + 7] = p.windZ ?? p.wind?.z ?? 0.0;
992
+ f64[prOff + 8] = p.dragCoefficient ?? 1.2;
993
+ f64[prOff + 9] = p.liftCoefficient ?? 0.8;
994
+ f64[prOff + 10] = p.solverIterations ?? 10;
995
+ f64[prOff + 11] = p.collisionRadius ?? 0.02;
996
+ f64[prOff + 12] = p.enableGround !== false ? 1.0 : 0.0;
997
+ f64[prOff + 13] = p.groundY ?? 0.0;
998
+ f64[prOff + 14] = p.groundFriction ?? 0.3;
999
+ f64[prOff + 15] = p.groundRestitution ?? 0.0;
1000
+ f64[prOff + 16] = p.breakThreshold ?? 1e9;
1001
+ f64[prOff + 17] = p.tetherLimitRatio ?? 1.15;
1002
+ f64[prOff + 18] = p.enableTethers !== false ? 1.0 : 0.0;
1003
+ f64[prOff + 19] = p.enableAerodynamics !== false ? 1.0 : 0.0;
1004
+
1005
+ // Break event count capacity
1006
+ u32[breakCountPtr / 4] = CLOTH_MAX_BREAK_EVENTS;
1007
+
1008
+ // Step cloth through C++ WASM
1009
+ const status = exp.rundot_kinetix_ext_cloth_step(
1010
+ nodePtr, nodeCount,
1011
+ stretchPtr, stretchCount,
1012
+ shearPtr, shearCount,
1013
+ bendPtr, bendCount,
1014
+ facePtr, faceCount,
1015
+ pinPtr, pinCount,
1016
+ tetherPtr, tetherCount,
1017
+ bodyPtr, bodyCount,
1018
+ paramsPtr,
1019
+ breakEventsPtr,
1020
+ breakCountPtr,
1021
+ dt
1022
+ );
1023
+
1024
+ if (status !== ClothStatus.Success) {
1025
+ throw new Error(`KINETIX_CLOTH_STEP_FAILED: status ${status}`);
1026
+ }
1027
+
1028
+ // Read back Nodes
1029
+ for (let i = 0; i < nodeCount; ++i) {
1030
+ const n = cloth.nodes[i];
1031
+ const o = (nodePtr / 8) + i * nodeStride;
1032
+ n.position.x = f64[o + 0];
1033
+ n.position.y = f64[o + 1];
1034
+ n.position.z = f64[o + 2];
1035
+ n.previousPosition.x = f64[o + 3];
1036
+ n.previousPosition.y = f64[o + 4];
1037
+ n.previousPosition.z = f64[o + 5];
1038
+ n.velocity.x = f64[o + 6];
1039
+ n.velocity.y = f64[o + 7];
1040
+ n.velocity.z = f64[o + 8];
1041
+ n.normal.x = f64[o + 9];
1042
+ n.normal.y = f64[o + 10];
1043
+ n.normal.z = f64[o + 11];
1044
+ n.inverseMass = f64[o + 12];
1045
+ }
1046
+
1047
+ // Read back Stretch Constraints
1048
+ let maxTension = 0.0;
1049
+ if (stretchCount > 0) {
1050
+ for (let i = 0; i < stretchCount; ++i) {
1051
+ const s = cloth.stretchConstraints[i];
1052
+ const o = (stretchPtr / 8) + i * stretchStride;
1053
+ s.currentLength = f64[o + 5];
1054
+ s.tension = f64[o + 6];
1055
+ s.broken = f64[o + 7] > 0.5;
1056
+ if (s.tension > maxTension) maxTension = s.tension;
1057
+ }
1058
+ }
1059
+
1060
+ // Read back Shear Constraints
1061
+ let maxShear = 0.0;
1062
+ if (shearCount > 0) {
1063
+ for (let i = 0; i < shearCount; ++i) {
1064
+ const sh = cloth.shearConstraints[i];
1065
+ const o = (shearPtr / 8) + i * shearStride;
1066
+ sh.currentLength = f64[o + 5];
1067
+ sh.shearForce = f64[o + 6];
1068
+ sh.broken = f64[o + 7] > 0.5;
1069
+ if (sh.shearForce > maxShear) maxShear = sh.shearForce;
1070
+ }
1071
+ }
1072
+
1073
+ // Read back Bend Constraints
1074
+ let maxBend = 0.0;
1075
+ if (bendCount > 0) {
1076
+ for (let i = 0; i < bendCount; ++i) {
1077
+ const b = cloth.bendConstraints[i];
1078
+ const o = (bendPtr / 8) + i * bendStride;
1079
+ b.currentAngle = f64[o + 6];
1080
+ b.bendingMoment = f64[o + 7];
1081
+ b.broken = f64[o + 8] > 0.5;
1082
+ if (b.bendingMoment > maxBend) maxBend = b.bendingMoment;
1083
+ }
1084
+ }
1085
+
1086
+ // Read back Faces
1087
+ let totalAerodynamicForce = 0.0;
1088
+ if (faceCount > 0) {
1089
+ for (let i = 0; i < faceCount; ++i) {
1090
+ const fc = cloth.faces[i];
1091
+ const o = (facePtr / 8) + i * faceStride;
1092
+ fc.currentArea = f64[o + 4];
1093
+ fc.normal.x = f64[o + 5];
1094
+ fc.normal.y = f64[o + 6];
1095
+ fc.normal.z = f64[o + 7];
1096
+ fc.aerodynamicForce = f64[o + 8];
1097
+ totalAerodynamicForce += Math.abs(fc.aerodynamicForce);
1098
+ }
1099
+ }
1100
+
1101
+ // Read back Pins
1102
+ if (pinCount > 0) {
1103
+ for (let i = 0; i < pinCount; ++i) {
1104
+ const p = cloth.pins[i];
1105
+ const o = (pinPtr / 8) + i * pinStride;
1106
+ p.worldPosition.x = f64[o + 5];
1107
+ p.worldPosition.y = f64[o + 6];
1108
+ p.worldPosition.z = f64[o + 7];
1109
+ p.reactionForce = f64[o + 9];
1110
+ }
1111
+ }
1112
+
1113
+ // Read back Tethers
1114
+ if (tetherCount > 0) {
1115
+ for (let i = 0; i < tetherCount; ++i) {
1116
+ const t = cloth.tethers[i];
1117
+ const o = (tetherPtr / 8) + i * tetherStride;
1118
+ t.currentDistance = f64[o + 4];
1119
+ t.tension = f64[o + 5];
1120
+ }
1121
+ }
1122
+
1123
+ // Read back coupled Bodies (if dynamic positions were modified)
1124
+ if (bodyCount > 0) {
1125
+ for (let i = 0; i < bodyCount; ++i) {
1126
+ const b = bodies[i];
1127
+ const o = (bodyPtr / 8) + i * CLOTH_BODY_STRIDE;
1128
+ b.x = f64[o + 0];
1129
+ b.y = f64[o + 1];
1130
+ b.z = f64[o + 2];
1131
+ }
1132
+ }
1133
+
1134
+ // Read back Break Events
1135
+ const actualEvents = u32[breakCountPtr / 4];
1136
+ cloth.breakEvents = [];
1137
+ for (let ev = 0; ev < actualEvents; ++ev) {
1138
+ const o = (breakEventsPtr / 8) + ev * CLOTH_BREAK_EVENT_STRIDE;
1139
+ cloth.breakEvents.push({
1140
+ clothId: Math.round(f64[o + 0]),
1141
+ elementIndex: Math.round(f64[o + 1]),
1142
+ reason: Math.round(f64[o + 2]),
1143
+ measuredValue: f64[o + 3],
1144
+ });
1145
+ }
1146
+
1147
+ cloth.maxTension = maxTension;
1148
+ cloth.maxShear = maxShear;
1149
+ cloth.maxBend = maxBend;
1150
+ cloth.totalAerodynamicForce = totalAerodynamicForce;
1151
+
1152
+ return cloth;
1153
+ });
1154
+ }
1155
+
1156
+ export const stepClothSimulation = stepCloth;