zhuda-laya-engine 0.0.0-stage → 2.1.0

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 (740) hide show
  1. package/AGENTS.md +236 -0
  2. package/CHANGELOG.md +431 -0
  3. package/LICENSE +41 -0
  4. package/README.md +247 -2
  5. package/assets/branding/layaair-tribute-mark.svg +5 -0
  6. package/assets/branding/zhuda-layaair-mark.svg +34 -0
  7. package/assets/prefabs/CoinFountainVfx.lh +197 -0
  8. package/assets/prefabs/ConfirmActionButton.lh +87 -0
  9. package/assets/prefabs/LevelUpAuraVfx.lh +136 -0
  10. package/assets/prefabs/MineExplosionParticle.lh +352 -0
  11. package/assets/prefabs/NineSliceDialogFrame.lh +171 -0
  12. package/assets/prefabs/SwordSlashTrailVfx.lh +117 -0
  13. package/assets/prefabs/TacticalHudBar.lh +134 -0
  14. package/assets/prefabs/TelemetryDashboard.lh +87 -0
  15. package/assets/prefabs/UnityCrystalGuardian.lh +144 -0
  16. package/assets/prefabs/ZhudaSpine42Mascot.lh +183 -0
  17. package/assets/scenes/MinesweeperStage.ls +1180 -0
  18. package/dist/asset-calibration-report.json +969 -0
  19. package/dist/build-manifest.json +254 -0
  20. package/dist/sdk/zhuda-laya-csg.cjs +710 -0
  21. package/dist/sdk/zhuda-laya-csg.esm.js +683 -0
  22. package/dist/sdk/zhuda-laya-csg.min.cjs +7 -0
  23. package/dist/sdk/zhuda-laya-csg.min.esm.js +7 -0
  24. package/dist/sdk/zhuda-laya-demo.cjs +5990 -0
  25. package/dist/sdk/zhuda-laya-demo.esm.js +5956 -0
  26. package/dist/sdk/zhuda-laya-demo.min.cjs +12 -0
  27. package/dist/sdk/zhuda-laya-demo.min.esm.js +12 -0
  28. package/dist/sdk/zhuda-laya-engine.cjs +52628 -0
  29. package/dist/sdk/zhuda-laya-engine.esm.js +51848 -0
  30. package/dist/sdk/zhuda-laya-engine.min.cjs +2710 -0
  31. package/dist/sdk/zhuda-laya-engine.min.esm.js +2710 -0
  32. package/docs/3d-acceptance.md +47 -0
  33. package/docs/3d-integration.md +190 -0
  34. package/docs/README.md +112 -0
  35. package/docs/SUPPORT.md +21 -0
  36. package/docs/agent-development.md +219 -0
  37. package/docs/animation-control.md +213 -0
  38. package/docs/asset-loading.md +97 -0
  39. package/docs/assets/reference-games.jpg +0 -0
  40. package/docs/assets/solana-qr.png +0 -0
  41. package/docs/assets/sponsor-pass.svg +32 -0
  42. package/docs/audio.md +319 -0
  43. package/docs/backend-delivery.md +218 -0
  44. package/docs/basis-textures.md +94 -0
  45. package/docs/browser-sdk-releases.json +204 -0
  46. package/docs/browser-sdk.md +187 -0
  47. package/docs/cinematics.md +267 -0
  48. package/docs/cloth.md +280 -0
  49. package/docs/combat-runtime.md +82 -0
  50. package/docs/consumer-route.md +169 -0
  51. package/docs/consumer-tooling.md +518 -0
  52. package/docs/crowd-runtime.md +195 -0
  53. package/docs/developer-tooling.md +191 -0
  54. package/docs/film-scenes.md +1504 -0
  55. package/docs/filmic-rendering.md +743 -0
  56. package/docs/flythrough.md +89 -0
  57. package/docs/gpu-depth.md +207 -0
  58. package/docs/grass.md +127 -0
  59. package/docs/hdr-environment.md +76 -0
  60. package/docs/instanced-meshes.md +137 -0
  61. package/docs/layout-and-media.md +37 -0
  62. package/docs/layout-wireframe.md +80 -0
  63. package/docs/lighting-depth.md +185 -0
  64. package/docs/minigame-packaging.md +79 -0
  65. package/docs/model-editor.md +66 -0
  66. package/docs/model-lod.md +122 -0
  67. package/docs/model-pipeline.md +74 -0
  68. package/docs/model-showcase.md +365 -0
  69. package/docs/overview.md +183 -0
  70. package/docs/plans/README.md +133 -0
  71. package/docs/plans/TASK-TEMPLATE.md +64 -0
  72. package/docs/plans/TASK-spine38-isolated-topology-20261106.md +89 -0
  73. package/docs/plans/TEMPLATE.md +44 -0
  74. package/docs/plans/VERIFICATION-TEMPLATE.md +45 -0
  75. package/docs/plans/asset-packaging.md +140 -0
  76. package/docs/plans/cinematic-production/task.md +97 -0
  77. package/docs/plans/cinematic-production/verification.md +49 -0
  78. package/docs/plans/consumer-delivery-followups.md +129 -0
  79. package/docs/plans/desktop-and-agent-gates/hardening-task.md +85 -0
  80. package/docs/plans/desktop-and-agent-gates/hardening-verification.md +109 -0
  81. package/docs/plans/desktop-and-agent-gates/task.md +94 -0
  82. package/docs/plans/desktop-and-agent-gates/verification.md +60 -0
  83. package/docs/plans/prefab-toolchain.md +73 -0
  84. package/docs/plans/preview-runtime-hardening.md +68 -0
  85. package/docs/platform-adapters.md +113 -0
  86. package/docs/post-quality.md +242 -0
  87. package/docs/prefab-editor.md +194 -0
  88. package/docs/prefab-library.md +94 -0
  89. package/docs/prefab-workflow.md +75 -0
  90. package/docs/procedural-assets.md +463 -0
  91. package/docs/procedural-prefabs.md +133 -0
  92. package/docs/quality-route.md +386 -0
  93. package/docs/release-policy.md +79 -0
  94. package/docs/routes/README.md +180 -0
  95. package/docs/routes/TEMPLATE.md +54 -0
  96. package/docs/routes/action-adventure-3d-minigame.md +238 -0
  97. package/docs/routes/apocalypse-survival-3d.md +219 -0
  98. package/docs/routes/aqua-freeze-2d-minigame.md +183 -0
  99. package/docs/routes/astral-minimal-film-look.md +141 -0
  100. package/docs/routes/cascade-slot.md +93 -0
  101. package/docs/routes/clip-free-garments.md +229 -0
  102. package/docs/routes/drop-merge-2d-minigame.md +200 -0
  103. package/docs/routes/dungeon-line-slot.md +87 -0
  104. package/docs/routes/film-as-data.md +131 -0
  105. package/docs/routes/film-fight-that-lands.md +198 -0
  106. package/docs/routes/film-ink-lines.md +120 -0
  107. package/docs/routes/floating-island.md +229 -0
  108. package/docs/routes/game-2d.md +168 -0
  109. package/docs/routes/game-rules-first.md +89 -0
  110. package/docs/routes/game-to-prefabs.md +249 -0
  111. package/docs/routes/go-game-server.md +95 -0
  112. package/docs/routes/good-looking-3d-scene.md +99 -0
  113. package/docs/routes/high-quality-character.md +263 -0
  114. package/docs/routes/humanoid-from-shapes.md +97 -0
  115. package/docs/routes/hunting-action-3d.md +76 -0
  116. package/docs/routes/idle-gacha-2d.md +67 -0
  117. package/docs/routes/interactive-film-viewer.md +141 -0
  118. package/docs/routes/isometric-mystery-2d.md +217 -0
  119. package/docs/routes/key-art-to-animated-title.md +132 -0
  120. package/docs/routes/laya-ide-project-cli-web.md +125 -0
  121. package/docs/routes/layered-cloth-on-body.md +240 -0
  122. package/docs/routes/low-poly-character-from-shapes.md +475 -0
  123. package/docs/routes/match-three-2d.md +64 -0
  124. package/docs/routes/merge-2d.md +59 -0
  125. package/docs/routes/mini-game-build.md +80 -0
  126. package/docs/routes/mobile-always-landscape.md +149 -0
  127. package/docs/routes/mobile-web-fullscreen.md +255 -0
  128. package/docs/routes/model-from-dcc.md +106 -0
  129. package/docs/routes/new-game-project.md +100 -0
  130. package/docs/routes/onboarding-tutorial-2d.md +54 -0
  131. package/docs/routes/online-lockstep-match.md +473 -0
  132. package/docs/routes/outdoor-landscape.md +134 -0
  133. package/docs/routes/outfit-swap-3d.md +144 -0
  134. package/docs/routes/papercraft-diorama.md +186 -0
  135. package/docs/routes/platform-fighter-2d.md +235 -0
  136. package/docs/routes/platformer-2d.md +69 -0
  137. package/docs/routes/pole-rescue-arcade-3d.md +198 -0
  138. package/docs/routes/pose-atlas-motion.md +188 -0
  139. package/docs/routes/prototype-to-center.md +162 -0
  140. package/docs/routes/raft-brawl-3d-minigame.md +205 -0
  141. package/docs/routes/rhythm-2d.md +82 -0
  142. package/docs/routes/rigged-phone-character.md +120 -0
  143. package/docs/routes/screen-fit-game-box.md +127 -0
  144. package/docs/routes/shared-islands.md +195 -0
  145. package/docs/routes/side-view-duel.md +266 -0
  146. package/docs/routes/single-html-page.md +83 -0
  147. package/docs/routes/smooth-character-motion.md +162 -0
  148. package/docs/routes/spine-character.md +175 -0
  149. package/docs/routes/story-counting-2d.md +219 -0
  150. package/docs/routes/survivors-2d.md +60 -0
  151. package/docs/routes/third-person-town-3d.md +240 -0
  152. package/docs/routes/tower-defence-3d.md +67 -0
  153. package/docs/routes/ui-test-by-probe.md +219 -0
  154. package/docs/routes/underwater.md +138 -0
  155. package/docs/routes/visual-content-prefab.md +101 -0
  156. package/docs/runtime-hardening.md +108 -0
  157. package/docs/shader-warm-up.md +125 -0
  158. package/docs/spring-bones.md +79 -0
  159. package/docs/stylized-shading.md +171 -0
  160. package/docs/terrain-shader.md +130 -0
  161. package/docs/vrm.md +128 -0
  162. package/docs/walking.md +97 -0
  163. package/docs/webgpu.md +225 -0
  164. package/docs/white-label-themes.md +212 -0
  165. package/docs/zh-CN/README.md +130 -0
  166. package/examples/browser-sdk/2d-webgpu.html +46 -0
  167. package/examples/browser-sdk/2d.html +46 -0
  168. package/examples/browser-sdk/3d-gltf-webgpu.html +50 -0
  169. package/examples/browser-sdk/3d-gltf.html +50 -0
  170. package/examples/browser-sdk/3d-webgpu.html +46 -0
  171. package/examples/browser-sdk/3d.html +46 -0
  172. package/examples/cinematic-capture/Main.ts +294 -0
  173. package/examples/cinematic-capture/README.md +27 -0
  174. package/examples/model-inspector/README.md +15 -0
  175. package/examples/model-inspector/asset.gltf +158 -0
  176. package/examples/windows-webgpu/README.md +57 -0
  177. package/examples/windows-webgpu/bundle.mjs +122 -0
  178. package/examples/windows-webgpu/main.mjs +302 -0
  179. package/examples/windows-webgpu/package-lock.json +2353 -0
  180. package/examples/windows-webgpu/package.json +40 -0
  181. package/examples/windows-webgpu/squirrel.mjs +66 -0
  182. package/package.json +259 -4
  183. package/packages/ci-gate/export-film.mjs +347 -0
  184. package/packages/ci-gate/verify-sequence.mjs +285 -0
  185. package/packages/compiler/agent-contract.mjs +545 -0
  186. package/packages/compiler/ast-scanner.mjs +254 -0
  187. package/packages/compiler/backend-loader.mjs +62 -0
  188. package/packages/compiler/branding-assets.mjs +32 -0
  189. package/packages/compiler/branding.mjs +324 -0
  190. package/packages/compiler/browser-auto-example.mjs +31 -0
  191. package/packages/compiler/browser-model-example.mjs +53 -0
  192. package/packages/compiler/browser-process.mjs +344 -0
  193. package/packages/compiler/browser-sdk-example.mjs +50 -0
  194. package/packages/compiler/browser-sdk.mjs +342 -0
  195. package/packages/compiler/chrome-host.mjs +196 -0
  196. package/packages/compiler/cli.mjs +22 -0
  197. package/packages/compiler/compiler-runtime.mjs +41 -0
  198. package/packages/compiler/consumer-accept.mjs +89 -0
  199. package/packages/compiler/consumer-build.mjs +641 -0
  200. package/packages/compiler/consumer-guards.mjs +422 -0
  201. package/packages/compiler/consumer-publication.mjs +56 -0
  202. package/packages/compiler/consumer-scripts.mjs +134 -0
  203. package/packages/compiler/consumer-starter-2d.mjs +54 -0
  204. package/packages/compiler/consumer-starter-minigame.mjs +30 -0
  205. package/packages/compiler/editor-http.mjs +210 -0
  206. package/packages/compiler/editor-request.ts +12 -0
  207. package/packages/compiler/film-host-client.ts +249 -0
  208. package/packages/compiler/flythrough-file.mjs +144 -0
  209. package/packages/compiler/json-locate.mjs +85 -0
  210. package/packages/compiler/layout-contract.mjs +472 -0
  211. package/packages/compiler/minigame-bootstrap.mjs +24 -0
  212. package/packages/compiler/minigame-bridge.mjs +66 -0
  213. package/packages/compiler/minigame-guards.mjs +107 -0
  214. package/packages/compiler/minigame-toolchain.mjs +241 -0
  215. package/packages/compiler/model-crown.mjs +173 -0
  216. package/packages/compiler/model-editor-client.ts +809 -0
  217. package/packages/compiler/model-editor.mjs +242 -0
  218. package/packages/compiler/model-lod.mjs +547 -0
  219. package/packages/compiler/prefab-check.mjs +288 -0
  220. package/packages/compiler/prefab-checks.mjs +74 -0
  221. package/packages/compiler/prefab-dependencies.mjs +102 -0
  222. package/packages/compiler/prefab-editor-client.ts +950 -0
  223. package/packages/compiler/prefab-editor-preview.ts +89 -0
  224. package/packages/compiler/prefab-editor-server.mjs +585 -0
  225. package/packages/compiler/prefab-editor.mjs +43 -0
  226. package/packages/compiler/prefab-environment.mjs +107 -0
  227. package/packages/compiler/prefab-lifecycle.mjs +60 -0
  228. package/packages/compiler/prefab-preview.mjs +159 -0
  229. package/packages/compiler/prefab-watch.mjs +459 -0
  230. package/packages/compiler/runtime3d-types.mjs +91 -0
  231. package/packages/compiler/stage-probe.mjs +557 -0
  232. package/packages/compiler/visual-asset-audit.mjs +609 -0
  233. package/packages/compiler/visual-contract-audit.mjs +173 -0
  234. package/packages/compiler/webgpu-loader.mjs +153 -0
  235. package/packages/compiler/zhuda-build.mjs +714 -0
  236. package/packages/compiler/zhuda-doctor.mjs +186 -0
  237. package/packages/compiler/zhuda-init.mjs +353 -0
  238. package/packages/compiler/zhuda-layout.mjs +163 -0
  239. package/packages/compiler/zhuda-minigame.mjs +636 -0
  240. package/packages/core/BenchmarkEngine.ts +2 -0
  241. package/packages/core/BitmapFontContract.ts +159 -0
  242. package/packages/core/DeclarativeSDK.ts +686 -0
  243. package/packages/core/IframeSoundAdapter.ts +1824 -0
  244. package/packages/core/NoSleepAdapter.ts +302 -0
  245. package/packages/core/PixelCalibrationEngine.ts +602 -0
  246. package/packages/core/Prefab.ts +314 -0
  247. package/packages/core/RealmGuards.ts +111 -0
  248. package/packages/core/ResponsiveLayout.ts +149 -0
  249. package/packages/core/RuntimeGuards.ts +261 -0
  250. package/packages/core/SpineCharacter.ts +243 -0
  251. package/packages/core/SpineVisualContract.ts +238 -0
  252. package/packages/core/SplashSignal.ts +72 -0
  253. package/packages/core/StaticAssetPipeline.ts +3 -0
  254. package/packages/core/TopologyGraph.ts +693 -0
  255. package/packages/core/UnityBridgePipeline.ts +2 -0
  256. package/packages/core/VideoSurface.ts +75 -0
  257. package/packages/core/ViewportMatrixEngine.ts +237 -0
  258. package/packages/core/VisualFocusContract.ts +76 -0
  259. package/packages/core/VisualMigrationPipeline.ts +245 -0
  260. package/packages/core/WebViewSurface.ts +116 -0
  261. package/packages/core/audio/OfflineMix.ts +186 -0
  262. package/packages/core/audio/Spatial.ts +312 -0
  263. package/packages/core/audio/StreamVoice.ts +161 -0
  264. package/packages/core/csg/Bsp.ts +310 -0
  265. package/packages/core/csg/index.ts +589 -0
  266. package/packages/core/effects/Effect2D.ts +286 -0
  267. package/packages/core/effects/Effect3D.ts +213 -0
  268. package/packages/core/effects/EffectBudget.ts +695 -0
  269. package/packages/core/effects/EffectDirector.ts +254 -0
  270. package/packages/core/effects/EffectPresets.ts +843 -0
  271. package/packages/core/effects/EffectPresets2D.ts +1011 -0
  272. package/packages/core/effects/EffectSprites.ts +143 -0
  273. package/packages/core/effects/ParticleEventLink.ts +53 -0
  274. package/packages/core/effects/README.md +108 -0
  275. package/packages/core/effects/VfxMaterial.ts +253 -0
  276. package/packages/core/film/BakeKey.ts +21 -0
  277. package/packages/core/film/ColorValues.ts +44 -0
  278. package/packages/core/film/FilmLint.ts +353 -0
  279. package/packages/core/film/FilmPrefab.ts +82 -0
  280. package/packages/core/film/FilmReview.ts +84 -0
  281. package/packages/core/film/FilmScene.ts +3442 -0
  282. package/packages/core/film/FilmStage.ts +2148 -0
  283. package/packages/core/film/Flythrough.ts +169 -0
  284. package/packages/core/film/FlythroughReport.ts +326 -0
  285. package/packages/core/film/FrameMetrics.ts +167 -0
  286. package/packages/core/film/LiveLook.ts +242 -0
  287. package/packages/core/film/LookQuality.ts +94 -0
  288. package/packages/core/film/Projection.ts +74 -0
  289. package/packages/core/film/SceneLook.ts +363 -0
  290. package/packages/core/film/index.ts +12 -0
  291. package/packages/core/index.ts +48 -0
  292. package/packages/core/particles/EffectEnvelope.ts +52 -0
  293. package/packages/core/particles/LayaParticleRenderer.ts +182 -0
  294. package/packages/core/particles/LayaParticleRenderer3D.ts +218 -0
  295. package/packages/core/particles/ParticleExtras.ts +163 -0
  296. package/packages/core/particles/ParticleMesh3D.ts +156 -0
  297. package/packages/core/particles/ParticleSystem.ts +324 -0
  298. package/packages/core/particles/ParticleSystem3D.ts +411 -0
  299. package/packages/core/particles/ParticleTypes.ts +230 -0
  300. package/packages/core/particles/ParticleTypes3D.ts +115 -0
  301. package/packages/core/particles/README.md +181 -0
  302. package/packages/core/particles/RibbonPath3D.ts +234 -0
  303. package/packages/core/particles/RibbonTrail.ts +145 -0
  304. package/packages/core/particles/RibbonTrail3D.ts +166 -0
  305. package/packages/core/particles/index.ts +12 -0
  306. package/packages/core/prefabs/PrefabLibrary.ts +692 -0
  307. package/packages/core/prefabs/PrefabTree3D.ts +291 -0
  308. package/packages/core/prefabs/index.ts +10 -0
  309. package/packages/core/procedural/Geometry.ts +1871 -0
  310. package/packages/core/procedural/Hand.ts +1510 -0
  311. package/packages/core/procedural/Isosurface.ts +1035 -0
  312. package/packages/core/procedural/ModuleHarness.ts +180 -0
  313. package/packages/core/procedural/Noise.ts +123 -0
  314. package/packages/core/procedural/PhotoMaterial.ts +283 -0
  315. package/packages/core/procedural/ProceduralAssetProvider.ts +105 -0
  316. package/packages/core/procedural/ProceduralMaterial.ts +184 -0
  317. package/packages/core/procedural/PropFactory.ts +307 -0
  318. package/packages/core/procedural/Random.ts +70 -0
  319. package/packages/core/procedural/SynthAudio.ts +221 -0
  320. package/packages/core/procedural/TerrainMesh.ts +207 -0
  321. package/packages/core/procedural/TextureRecipe.ts +464 -0
  322. package/packages/core/procedural/TriplanarMaterial.ts +169 -0
  323. package/packages/core/procedural/Variation.ts +94 -0
  324. package/packages/core/procedural/index.ts +15 -0
  325. package/packages/core/runtime3d/ActionFrames.ts +245 -0
  326. package/packages/core/runtime3d/AnimationBlend.ts +282 -0
  327. package/packages/core/runtime3d/AnimationBlendMath.ts +98 -0
  328. package/packages/core/runtime3d/AssetProvider.ts +775 -0
  329. package/packages/core/runtime3d/AssetShowcase.ts +190 -0
  330. package/packages/core/runtime3d/Assets.ts +801 -0
  331. package/packages/core/runtime3d/AsyncDeadline.ts +53 -0
  332. package/packages/core/runtime3d/AsyncOperation.ts +39 -0
  333. package/packages/core/runtime3d/BackendChoice.ts +359 -0
  334. package/packages/core/runtime3d/BakedCrowd.ts +575 -0
  335. package/packages/core/runtime3d/BasisTexture.ts +657 -0
  336. package/packages/core/runtime3d/CameraCollision.ts +69 -0
  337. package/packages/core/runtime3d/CameraEffects.ts +752 -0
  338. package/packages/core/runtime3d/CameraRig.ts +70 -0
  339. package/packages/core/runtime3d/CharacterController.ts +272 -0
  340. package/packages/core/runtime3d/CinematicCapture.ts +457 -0
  341. package/packages/core/runtime3d/Clocks.ts +724 -0
  342. package/packages/core/runtime3d/Cloth.ts +183 -0
  343. package/packages/core/runtime3d/Clouds.ts +462 -0
  344. package/packages/core/runtime3d/ColorLut.ts +351 -0
  345. package/packages/core/runtime3d/CombatQueries.ts +240 -0
  346. package/packages/core/runtime3d/ComputePass.ts +582 -0
  347. package/packages/core/runtime3d/CrowdPresentation.ts +368 -0
  348. package/packages/core/runtime3d/DebugDraw.ts +567 -0
  349. package/packages/core/runtime3d/DebugStats.ts +35 -0
  350. package/packages/core/runtime3d/Decals.ts +309 -0
  351. package/packages/core/runtime3d/DensityVolume.ts +238 -0
  352. package/packages/core/runtime3d/DistanceBricks.ts +349 -0
  353. package/packages/core/runtime3d/Dither.ts +199 -0
  354. package/packages/core/runtime3d/DrawWarmUp.ts +217 -0
  355. package/packages/core/runtime3d/EdgeSmoothing.ts +108 -0
  356. package/packages/core/runtime3d/EnvironmentProbe.ts +656 -0
  357. package/packages/core/runtime3d/Filmic.ts +973 -0
  358. package/packages/core/runtime3d/Foliage.ts +367 -0
  359. package/packages/core/runtime3d/GltfIntake.ts +914 -0
  360. package/packages/core/runtime3d/GltfSecondUv.ts +153 -0
  361. package/packages/core/runtime3d/GraphicsGuard.ts +171 -0
  362. package/packages/core/runtime3d/Grass.ts +1080 -0
  363. package/packages/core/runtime3d/GroundTruthAO.ts +360 -0
  364. package/packages/core/runtime3d/HalfFloat.ts +44 -0
  365. package/packages/core/runtime3d/HdrEnvironment.ts +588 -0
  366. package/packages/core/runtime3d/Ik.ts +392 -0
  367. package/packages/core/runtime3d/InputBuffer.ts +165 -0
  368. package/packages/core/runtime3d/InstancedMesh.ts +460 -0
  369. package/packages/core/runtime3d/Laya342Adapters.ts +1095 -0
  370. package/packages/core/runtime3d/Laya342GltfAdapter.ts +751 -0
  371. package/packages/core/runtime3d/LensFinish.ts +153 -0
  372. package/packages/core/runtime3d/LensFlare.ts +490 -0
  373. package/packages/core/runtime3d/LightFog.ts +1008 -0
  374. package/packages/core/runtime3d/LightingDepth.ts +544 -0
  375. package/packages/core/runtime3d/Lightmap.ts +238 -0
  376. package/packages/core/runtime3d/Math3D.ts +84 -0
  377. package/packages/core/runtime3d/MeshQuery.ts +547 -0
  378. package/packages/core/runtime3d/MeshoptModelDecoder.ts +354 -0
  379. package/packages/core/runtime3d/MiniGameSplash.ts +70 -0
  380. package/packages/core/runtime3d/ModelAnimationSampler.ts +97 -0
  381. package/packages/core/runtime3d/ModelCompatibility.ts +890 -0
  382. package/packages/core/runtime3d/ModelDecoder.ts +294 -0
  383. package/packages/core/runtime3d/ModelInspector.ts +226 -0
  384. package/packages/core/runtime3d/ModelLod.ts +163 -0
  385. package/packages/core/runtime3d/ModelPlacements.ts +426 -0
  386. package/packages/core/runtime3d/ObjectMotion.ts +217 -0
  387. package/packages/core/runtime3d/PhysicalCamera.ts +190 -0
  388. package/packages/core/runtime3d/PointShadow.ts +500 -0
  389. package/packages/core/runtime3d/PointShadowSchedule.ts +201 -0
  390. package/packages/core/runtime3d/Pool.ts +120 -0
  391. package/packages/core/runtime3d/ProbeVolume.ts +695 -0
  392. package/packages/core/runtime3d/QualityGovernor.ts +385 -0
  393. package/packages/core/runtime3d/QualityMemory.ts +200 -0
  394. package/packages/core/runtime3d/RenderScale.ts +356 -0
  395. package/packages/core/runtime3d/RenderSpaces.ts +105 -0
  396. package/packages/core/runtime3d/Renderer.ts +377 -0
  397. package/packages/core/runtime3d/ResultPlayback.ts +239 -0
  398. package/packages/core/runtime3d/Runtime.ts +507 -0
  399. package/packages/core/runtime3d/RuntimeDriver.ts +127 -0
  400. package/packages/core/runtime3d/SceneLifecycle.ts +211 -0
  401. package/packages/core/runtime3d/SceneMutation.ts +107 -0
  402. package/packages/core/runtime3d/SceneQueries.ts +191 -0
  403. package/packages/core/runtime3d/ScreenReflections.ts +595 -0
  404. package/packages/core/runtime3d/SelectionOutline.ts +389 -0
  405. package/packages/core/runtime3d/ShaderErrors.ts +104 -0
  406. package/packages/core/runtime3d/ShaderRegistry.ts +34 -0
  407. package/packages/core/runtime3d/ShaderWarmUp.ts +473 -0
  408. package/packages/core/runtime3d/ShadowUpdates.ts +287 -0
  409. package/packages/core/runtime3d/SkinBinding.ts +235 -0
  410. package/packages/core/runtime3d/Smaa.ts +386 -0
  411. package/packages/core/runtime3d/SmaaTables.ts +244 -0
  412. package/packages/core/runtime3d/SpringBones.ts +184 -0
  413. package/packages/core/runtime3d/SurfaceBuffer.ts +76 -0
  414. package/packages/core/runtime3d/Surfaces.ts +1105 -0
  415. package/packages/core/runtime3d/Themes.ts +344 -0
  416. package/packages/core/runtime3d/Timeline.ts +362 -0
  417. package/packages/core/runtime3d/ToneCurves.ts +292 -0
  418. package/packages/core/runtime3d/TransformGizmo.ts +539 -0
  419. package/packages/core/runtime3d/TriangleMask.ts +250 -0
  420. package/packages/core/runtime3d/Underwater.ts +778 -0
  421. package/packages/core/runtime3d/Utf8.ts +87 -0
  422. package/packages/core/runtime3d/Vrm.ts +773 -0
  423. package/packages/core/runtime3d/VrmDocument.ts +1830 -0
  424. package/packages/core/runtime3d/WalkGrid.ts +471 -0
  425. package/packages/core/runtime3d/WebGPU342Adapters.ts +280 -0
  426. package/packages/core/runtime3d/index.ts +147 -0
  427. package/packages/core/shaders/Laya342ShaderTemplates.ts +6 -0
  428. package/packages/core/shaders/LitSurfaceMaterial.ts +1076 -0
  429. package/packages/core/shaders/LitSurfaceTemplates.ts +13 -0
  430. package/packages/core/shaders/ReflectiveSurface.ts +285 -0
  431. package/packages/core/shaders/ShaderMaterials.ts +387 -0
  432. package/packages/core/shaders/ToonMaterial.ts +559 -0
  433. package/packages/core/shaders/WaterRipples.ts +170 -0
  434. package/packages/core/shaders/WaterSurface.ts +480 -0
  435. package/packages/core/shaders/ZhudaCommon.ts +109 -0
  436. package/packages/core/shaders/index.ts +6 -0
  437. package/packages/core/simulation/Cloth.ts +1541 -0
  438. package/packages/core/simulation/ParticleCore.ts +498 -0
  439. package/packages/core/simulation/SpringBones.ts +553 -0
  440. package/packages/core/simulation/index.ts +3 -0
  441. package/packages/core/terrain/Heightfield.ts +385 -0
  442. package/packages/core/terrain/Terrain2D.ts +324 -0
  443. package/packages/core/terrain/Terrain3D.ts +452 -0
  444. package/packages/core/terrain/TerrainShell.ts +94 -0
  445. package/packages/core/terrain/index.ts +3 -0
  446. package/packages/demo/BenchmarkEngine.ts +184 -0
  447. package/packages/demo/PixelCalibrationEngine.ts +36 -0
  448. package/packages/demo/ProceduralStarter.ts +312 -0
  449. package/packages/demo/StaticAssetPipeline.ts +1631 -0
  450. package/packages/demo/UnityBridgePipeline.ts +485 -0
  451. package/packages/demo/index.ts +6 -0
  452. package/packages/mcp-server/arguments.mjs +81 -0
  453. package/packages/mcp-server/corpus.mjs +627 -0
  454. package/packages/mcp-server/film-host.mjs +627 -0
  455. package/packages/mcp-server/film-session.mjs +95 -0
  456. package/packages/mcp-server/film-tools.mjs +460 -0
  457. package/packages/mcp-server/index.mjs +931 -0
  458. package/packages/mcp-server/quality-route.mjs +332 -0
  459. package/packages/mcp-server/roots.mjs +72 -0
  460. package/packages/mcp-server/runtime.mjs +81 -0
  461. package/packages/types/BitmapFontContract.d.ts +19 -0
  462. package/packages/types/DeclarativeSDK.d.ts +137 -0
  463. package/packages/types/IframeSoundAdapter.d.ts +173 -0
  464. package/packages/types/NoSleepAdapter.d.ts +50 -0
  465. package/packages/types/PixelCalibrationEngine.d.ts +83 -0
  466. package/packages/types/Prefab.d.ts +11 -0
  467. package/packages/types/RealmGuards.d.ts +26 -0
  468. package/packages/types/ResponsiveLayout.d.ts +10 -0
  469. package/packages/types/RuntimeGuards.d.ts +55 -0
  470. package/packages/types/SpineCharacter.d.ts +94 -0
  471. package/packages/types/SpineVisualContract.d.ts +50 -0
  472. package/packages/types/SplashSignal.d.ts +27 -0
  473. package/packages/types/TopologyGraph.d.ts +76 -0
  474. package/packages/types/VideoSurface.d.ts +21 -0
  475. package/packages/types/ViewportMatrixEngine.d.ts +27 -0
  476. package/packages/types/VisualFocusContract.d.ts +32 -0
  477. package/packages/types/VisualMigrationPipeline.d.ts +53 -0
  478. package/packages/types/WebViewSurface.d.ts +26 -0
  479. package/packages/types/audio/OfflineMix.d.ts +10 -0
  480. package/packages/types/audio/Spatial.d.ts +63 -0
  481. package/packages/types/audio/StreamVoice.d.ts +31 -0
  482. package/packages/types/browser.d.ts +44 -0
  483. package/packages/types/csg/Bsp.d.ts +85 -0
  484. package/packages/types/csg/index.d.ts +81 -0
  485. package/packages/types/demo/BenchmarkEngine.d.ts +61 -0
  486. package/packages/types/demo/PixelCalibrationEngine.d.ts +13 -0
  487. package/packages/types/demo/ProceduralStarter.d.ts +11 -0
  488. package/packages/types/demo/StaticAssetPipeline.d.ts +87 -0
  489. package/packages/types/demo/UnityBridgePipeline.d.ts +49 -0
  490. package/packages/types/demo/index.d.ts +7 -0
  491. package/packages/types/editor-preview.d.ts +27 -0
  492. package/packages/types/effects/Effect2D.d.ts +65 -0
  493. package/packages/types/effects/Effect3D.d.ts +42 -0
  494. package/packages/types/effects/EffectBudget.d.ts +179 -0
  495. package/packages/types/effects/EffectDirector.d.ts +68 -0
  496. package/packages/types/effects/EffectPresets.d.ts +27 -0
  497. package/packages/types/effects/EffectPresets2D.d.ts +32 -0
  498. package/packages/types/effects/EffectSprites.d.ts +23 -0
  499. package/packages/types/effects/ParticleEventLink.d.ts +16 -0
  500. package/packages/types/effects/VfxMaterial.d.ts +52 -0
  501. package/packages/types/film/BakeKey.d.ts +9 -0
  502. package/packages/types/film/ColorValues.d.ts +25 -0
  503. package/packages/types/film/FilmLint.d.ts +40 -0
  504. package/packages/types/film/FilmPrefab.d.ts +22 -0
  505. package/packages/types/film/FilmReview.d.ts +27 -0
  506. package/packages/types/film/FilmScene.d.ts +1214 -0
  507. package/packages/types/film/FilmStage.d.ts +237 -0
  508. package/packages/types/film/Flythrough.d.ts +44 -0
  509. package/packages/types/film/FlythroughReport.d.ts +132 -0
  510. package/packages/types/film/FrameMetrics.d.ts +49 -0
  511. package/packages/types/film/LiveLook.d.ts +60 -0
  512. package/packages/types/film/LookQuality.d.ts +24 -0
  513. package/packages/types/film/Projection.d.ts +20 -0
  514. package/packages/types/film/SceneLook.d.ts +122 -0
  515. package/packages/types/film/index.d.ts +13 -0
  516. package/packages/types/index.d.ts +49 -0
  517. package/packages/types/particles/EffectEnvelope.d.ts +18 -0
  518. package/packages/types/particles/LayaParticleRenderer.d.ts +30 -0
  519. package/packages/types/particles/LayaParticleRenderer3D.d.ts +29 -0
  520. package/packages/types/particles/ParticleExtras.d.ts +50 -0
  521. package/packages/types/particles/ParticleMesh3D.d.ts +24 -0
  522. package/packages/types/particles/ParticleSystem.d.ts +52 -0
  523. package/packages/types/particles/ParticleSystem3D.d.ts +69 -0
  524. package/packages/types/particles/ParticleTypes.d.ts +89 -0
  525. package/packages/types/particles/ParticleTypes3D.d.ts +56 -0
  526. package/packages/types/particles/RibbonPath3D.d.ts +32 -0
  527. package/packages/types/particles/RibbonTrail.d.ts +38 -0
  528. package/packages/types/particles/RibbonTrail3D.d.ts +22 -0
  529. package/packages/types/particles/index.d.ts +13 -0
  530. package/packages/types/prefabs/PrefabLibrary.d.ts +176 -0
  531. package/packages/types/prefabs/PrefabTree3D.d.ts +59 -0
  532. package/packages/types/prefabs/index.d.ts +4 -0
  533. package/packages/types/procedural/Geometry.d.ts +252 -0
  534. package/packages/types/procedural/Hand.d.ts +150 -0
  535. package/packages/types/procedural/Isosurface.d.ts +181 -0
  536. package/packages/types/procedural/ModuleHarness.d.ts +90 -0
  537. package/packages/types/procedural/Noise.d.ts +15 -0
  538. package/packages/types/procedural/PhotoMaterial.d.ts +49 -0
  539. package/packages/types/procedural/ProceduralAssetProvider.d.ts +31 -0
  540. package/packages/types/procedural/ProceduralMaterial.d.ts +63 -0
  541. package/packages/types/procedural/PropFactory.d.ts +94 -0
  542. package/packages/types/procedural/Random.d.ts +26 -0
  543. package/packages/types/procedural/SynthAudio.d.ts +49 -0
  544. package/packages/types/procedural/TerrainMesh.d.ts +55 -0
  545. package/packages/types/procedural/TextureRecipe.d.ts +125 -0
  546. package/packages/types/procedural/TriplanarMaterial.d.ts +27 -0
  547. package/packages/types/procedural/Variation.d.ts +23 -0
  548. package/packages/types/procedural/index.d.ts +16 -0
  549. package/packages/types/runtime3d/ActionFrames.d.ts +62 -0
  550. package/packages/types/runtime3d/AnimationBlend.d.ts +57 -0
  551. package/packages/types/runtime3d/AnimationBlendMath.d.ts +27 -0
  552. package/packages/types/runtime3d/AssetProvider.d.ts +167 -0
  553. package/packages/types/runtime3d/AssetShowcase.d.ts +44 -0
  554. package/packages/types/runtime3d/Assets.d.ts +166 -0
  555. package/packages/types/runtime3d/AsyncDeadline.d.ts +6 -0
  556. package/packages/types/runtime3d/AsyncOperation.d.ts +3 -0
  557. package/packages/types/runtime3d/BackendChoice.d.ts +130 -0
  558. package/packages/types/runtime3d/BakedCrowd.d.ts +156 -0
  559. package/packages/types/runtime3d/BasisTexture.d.ts +169 -0
  560. package/packages/types/runtime3d/CameraCollision.d.ts +20 -0
  561. package/packages/types/runtime3d/CameraEffects.d.ts +134 -0
  562. package/packages/types/runtime3d/CameraRig.d.ts +27 -0
  563. package/packages/types/runtime3d/CharacterController.d.ts +71 -0
  564. package/packages/types/runtime3d/CinematicCapture.d.ts +103 -0
  565. package/packages/types/runtime3d/Clocks.d.ts +200 -0
  566. package/packages/types/runtime3d/Cloth.d.ts +57 -0
  567. package/packages/types/runtime3d/Clouds.d.ts +69 -0
  568. package/packages/types/runtime3d/ColorLut.d.ts +73 -0
  569. package/packages/types/runtime3d/CombatQueries.d.ts +50 -0
  570. package/packages/types/runtime3d/ComputePass.d.ts +134 -0
  571. package/packages/types/runtime3d/CrowdPresentation.d.ts +104 -0
  572. package/packages/types/runtime3d/DebugDraw.d.ts +161 -0
  573. package/packages/types/runtime3d/DebugStats.d.ts +24 -0
  574. package/packages/types/runtime3d/Decals.d.ts +65 -0
  575. package/packages/types/runtime3d/DensityVolume.d.ts +39 -0
  576. package/packages/types/runtime3d/DistanceBricks.d.ts +97 -0
  577. package/packages/types/runtime3d/Dither.d.ts +78 -0
  578. package/packages/types/runtime3d/DrawWarmUp.d.ts +93 -0
  579. package/packages/types/runtime3d/EdgeSmoothing.d.ts +10 -0
  580. package/packages/types/runtime3d/EnvironmentProbe.d.ts +139 -0
  581. package/packages/types/runtime3d/Filmic.d.ts +243 -0
  582. package/packages/types/runtime3d/Foliage.d.ts +95 -0
  583. package/packages/types/runtime3d/GltfIntake.d.ts +236 -0
  584. package/packages/types/runtime3d/GltfSecondUv.d.ts +54 -0
  585. package/packages/types/runtime3d/GraphicsGuard.d.ts +18 -0
  586. package/packages/types/runtime3d/Grass.d.ts +226 -0
  587. package/packages/types/runtime3d/GroundTruthAO.d.ts +64 -0
  588. package/packages/types/runtime3d/HalfFloat.d.ts +13 -0
  589. package/packages/types/runtime3d/HdrEnvironment.d.ts +144 -0
  590. package/packages/types/runtime3d/Ik.d.ts +123 -0
  591. package/packages/types/runtime3d/InputBuffer.d.ts +49 -0
  592. package/packages/types/runtime3d/InstancedMesh.d.ts +116 -0
  593. package/packages/types/runtime3d/Laya342Adapters.d.ts +306 -0
  594. package/packages/types/runtime3d/Laya342GltfAdapter.d.ts +105 -0
  595. package/packages/types/runtime3d/LensFinish.d.ts +38 -0
  596. package/packages/types/runtime3d/LensFlare.d.ts +114 -0
  597. package/packages/types/runtime3d/LightFog.d.ts +190 -0
  598. package/packages/types/runtime3d/LightingDepth.d.ts +128 -0
  599. package/packages/types/runtime3d/Lightmap.d.ts +55 -0
  600. package/packages/types/runtime3d/Math3D.d.ts +19 -0
  601. package/packages/types/runtime3d/MeshQuery.d.ts +107 -0
  602. package/packages/types/runtime3d/MeshoptModelDecoder.d.ts +34 -0
  603. package/packages/types/runtime3d/MiniGameSplash.d.ts +4 -0
  604. package/packages/types/runtime3d/ModelAnimationSampler.d.ts +26 -0
  605. package/packages/types/runtime3d/ModelCompatibility.d.ts +169 -0
  606. package/packages/types/runtime3d/ModelDecoder.d.ts +66 -0
  607. package/packages/types/runtime3d/ModelInspector.d.ts +77 -0
  608. package/packages/types/runtime3d/ModelLod.d.ts +61 -0
  609. package/packages/types/runtime3d/ModelPlacements.d.ts +77 -0
  610. package/packages/types/runtime3d/ObjectMotion.d.ts +43 -0
  611. package/packages/types/runtime3d/PhysicalCamera.d.ts +51 -0
  612. package/packages/types/runtime3d/PointShadow.d.ts +71 -0
  613. package/packages/types/runtime3d/PointShadowSchedule.d.ts +74 -0
  614. package/packages/types/runtime3d/Pool.d.ts +33 -0
  615. package/packages/types/runtime3d/ProbeVolume.d.ts +123 -0
  616. package/packages/types/runtime3d/QualityGovernor.d.ts +201 -0
  617. package/packages/types/runtime3d/QualityMemory.d.ts +83 -0
  618. package/packages/types/runtime3d/RenderScale.d.ts +69 -0
  619. package/packages/types/runtime3d/RenderSpaces.d.ts +44 -0
  620. package/packages/types/runtime3d/Renderer.d.ts +62 -0
  621. package/packages/types/runtime3d/ResultPlayback.d.ts +70 -0
  622. package/packages/types/runtime3d/Runtime.d.ts +136 -0
  623. package/packages/types/runtime3d/RuntimeDriver.d.ts +42 -0
  624. package/packages/types/runtime3d/SceneLifecycle.d.ts +84 -0
  625. package/packages/types/runtime3d/SceneMutation.d.ts +34 -0
  626. package/packages/types/runtime3d/SceneQueries.d.ts +71 -0
  627. package/packages/types/runtime3d/ScreenReflections.d.ts +78 -0
  628. package/packages/types/runtime3d/SelectionOutline.d.ts +68 -0
  629. package/packages/types/runtime3d/ShaderErrors.d.ts +27 -0
  630. package/packages/types/runtime3d/ShaderRegistry.d.ts +10 -0
  631. package/packages/types/runtime3d/ShaderWarmUp.d.ts +162 -0
  632. package/packages/types/runtime3d/ShadowUpdates.d.ts +125 -0
  633. package/packages/types/runtime3d/SkinBinding.d.ts +53 -0
  634. package/packages/types/runtime3d/Smaa.d.ts +60 -0
  635. package/packages/types/runtime3d/SmaaTables.d.ts +12 -0
  636. package/packages/types/runtime3d/SpringBones.d.ts +69 -0
  637. package/packages/types/runtime3d/SurfaceBuffer.d.ts +28 -0
  638. package/packages/types/runtime3d/Surfaces.d.ts +326 -0
  639. package/packages/types/runtime3d/Themes.d.ts +70 -0
  640. package/packages/types/runtime3d/Timeline.d.ts +88 -0
  641. package/packages/types/runtime3d/ToneCurves.d.ts +59 -0
  642. package/packages/types/runtime3d/TransformGizmo.d.ts +139 -0
  643. package/packages/types/runtime3d/TriangleMask.d.ts +84 -0
  644. package/packages/types/runtime3d/Underwater.d.ts +120 -0
  645. package/packages/types/runtime3d/Utf8.d.ts +12 -0
  646. package/packages/types/runtime3d/Vrm.d.ts +120 -0
  647. package/packages/types/runtime3d/VrmDocument.d.ts +381 -0
  648. package/packages/types/runtime3d/WalkGrid.d.ts +94 -0
  649. package/packages/types/runtime3d/WebGPU342Adapters.d.ts +27 -0
  650. package/packages/types/runtime3d/index.d.ts +102 -0
  651. package/packages/types/shaders/Laya342ShaderTemplates.d.ts +3 -0
  652. package/packages/types/shaders/LitSurfaceMaterial.d.ts +257 -0
  653. package/packages/types/shaders/LitSurfaceTemplates.d.ts +7 -0
  654. package/packages/types/shaders/ReflectiveSurface.d.ts +58 -0
  655. package/packages/types/shaders/ShaderMaterials.d.ts +60 -0
  656. package/packages/types/shaders/ToonMaterial.d.ts +176 -0
  657. package/packages/types/shaders/WaterRipples.d.ts +48 -0
  658. package/packages/types/shaders/WaterSurface.d.ts +110 -0
  659. package/packages/types/shaders/ZhudaCommon.d.ts +31 -0
  660. package/packages/types/shaders/index.d.ts +7 -0
  661. package/packages/types/simulation/Cloth.d.ts +381 -0
  662. package/packages/types/simulation/ParticleCore.d.ts +125 -0
  663. package/packages/types/simulation/SpringBones.d.ts +107 -0
  664. package/packages/types/simulation/index.d.ts +4 -0
  665. package/packages/types/terrain/Heightfield.d.ts +67 -0
  666. package/packages/types/terrain/Terrain2D.d.ts +81 -0
  667. package/packages/types/terrain/Terrain3D.d.ts +80 -0
  668. package/packages/types/terrain/TerrainShell.d.ts +8 -0
  669. package/packages/types/terrain/index.d.ts +4 -0
  670. package/packages/types/vendor/LayaAir.runtime.d.ts +97440 -0
  671. package/packages/types/vendor/REPAIRS.md +30 -0
  672. package/packages/types/vendor/restored-3.4.2.json +142 -0
  673. package/packages/types/vendor/spine.runtime.d.ts +1507 -0
  674. package/packages/types/vendor/wechat-media.d.ts +877 -0
  675. package/packages/types/zhuda-2d-ui.d.ts +244 -0
  676. package/packages/types/zhuda-3d-physics.d.ts +34 -0
  677. package/packages/types/zhuda-audio.d.ts +128 -0
  678. package/packages/types/zhuda-bootstrap.d.ts +100 -0
  679. package/packages/types/zhuda-branding.d.ts +6 -0
  680. package/packages/types/zhuda-core.d.ts +46 -0
  681. package/packages/types/zhuda-declarative.d.ts +81 -0
  682. package/packages/types/zhuda-effects.d.ts +302 -0
  683. package/packages/types/zhuda-layout.d.ts +96 -0
  684. package/packages/types/zhuda-particles.d.ts +477 -0
  685. package/packages/types/zhuda-spine42.d.ts +23 -0
  686. package/packages/types/zhuda-static-assets.d.ts +389 -0
  687. package/packages/types/zhuda-video.d.ts +24 -0
  688. package/packages/types/zhuda-visual-contract.d.ts +52 -0
  689. package/packages/types/zhuda-webview.d.ts +43 -0
  690. package/vendor/basis/LICENSE.txt +55 -0
  691. package/vendor/basis/README.md +25 -0
  692. package/vendor/basis/basis_transcoder.js +19 -0
  693. package/vendor/basis/basis_transcoder.wasm +0 -0
  694. package/vendor/laya-3.4.2/WEBGPU-LICENSES.txt +1259 -0
  695. package/vendor/laya-3.4.2/libs/laya.ani.js +1 -0
  696. package/vendor/laya-3.4.2/libs/laya.box2D.js +1209 -0
  697. package/vendor/laya-3.4.2/libs/laya.bridge.js +2273 -0
  698. package/vendor/laya-3.4.2/libs/laya.bullet.js +1 -0
  699. package/vendor/laya-3.4.2/libs/laya.core.js +1 -0
  700. package/vendor/laya-3.4.2/libs/laya.d3.js +1 -0
  701. package/vendor/laya-3.4.2/libs/laya.device.js +1 -0
  702. package/vendor/laya-3.4.2/libs/laya.gltf.js +1 -0
  703. package/vendor/laya-3.4.2/libs/laya.ik.js +1 -0
  704. package/vendor/laya-3.4.2/libs/laya.light2D.js +1 -0
  705. package/vendor/laya-3.4.2/libs/laya.line2D.js +1 -0
  706. package/vendor/laya-3.4.2/libs/laya.native-spine42.js +7672 -0
  707. package/vendor/laya-3.4.2/libs/laya.navMesh2d.js +1 -0
  708. package/vendor/laya-3.4.2/libs/laya.navMesh3d.js +1 -0
  709. package/vendor/laya-3.4.2/libs/laya.navMeshCommon.js +1 -0
  710. package/vendor/laya-3.4.2/libs/laya.particle.js +1 -0
  711. package/vendor/laya-3.4.2/libs/laya.particle3D.js +1 -0
  712. package/vendor/laya-3.4.2/libs/laya.particleCommon.js +1 -0
  713. package/vendor/laya-3.4.2/libs/laya.physics2D.js +3774 -0
  714. package/vendor/laya-3.4.2/libs/laya.physics3D.js +1 -0
  715. package/vendor/laya-3.4.2/libs/laya.postProcess.js +1 -0
  716. package/vendor/laya-3.4.2/libs/laya.spine.js +1 -0
  717. package/vendor/laya-3.4.2/libs/laya.spine3D.js +1 -0
  718. package/vendor/laya-3.4.2/libs/laya.tilemap.js +1 -0
  719. package/vendor/laya-3.4.2/libs/laya.trail2D.js +1 -0
  720. package/vendor/laya-3.4.2/libs/laya.trail3D.js +1 -0
  721. package/vendor/laya-3.4.2/libs/laya.trailCommon.js +1 -0
  722. package/vendor/laya-3.4.2/libs/laya.ui.js +1 -0
  723. package/vendor/laya-3.4.2/libs/laya.ui2.js +9904 -0
  724. package/vendor/laya-3.4.2/libs/laya.vfx.js +6754 -0
  725. package/vendor/laya-3.4.2/libs/laya.webgl_2D.js +1 -0
  726. package/vendor/laya-3.4.2/libs/laya.webgl_3D.js +1 -0
  727. package/vendor/laya-3.4.2/libs/laya.webgpu_2D.js +18576 -0
  728. package/vendor/laya-3.4.2/libs/laya.webgpu_3D.js +6729 -0
  729. package/vendor/laya-3.4.2/libs/nagabind.js +395 -0
  730. package/vendor/laya-3.4.2/libs/nagabind_bg.wasm +0 -0
  731. package/vendor/laya-3.4.2/libs/shader_compiler_web.js +22 -0
  732. package/vendor/laya-3.4.2/libs/shader_compiler_web.wasm +0 -0
  733. package/vendor/laya-3.4.2/libs/spine-core-3.8.js +1 -0
  734. package/vendor/laya-3.4.2/libs/spine-core-4.2.js +10216 -0
  735. package/vendor/laya-3.4.2/webgpu-provenance.json +41 -0
  736. package/vendor/meshoptimizer/LICENSE.txt +21 -0
  737. package/vendor/meshoptimizer/README.md +13 -0
  738. package/vendor/threejs-post/LICENSE.txt +53 -0
  739. package/vendor/threejs-post/README.md +54 -0
  740. package/zhuda.config.json +22 -0
@@ -0,0 +1,1541 @@
1
+ /**
2
+ * Cloth: particles held apart by distance constraints, moved by Verlet
3
+ * integration in fixed steps, kept out of bodies, hung from pins and pulled
4
+ * by a hand. A game's cloth, not a measured fabric.
5
+ *
6
+ * Plain arithmetic: no LayaAir, no timer, no random numbers. Time comes in
7
+ * as deltas of the engine's clock (`ZhudaRuntimeSession.layers`); the same
8
+ * deltas give the same cloth to the last bit. `ZhudaCloth` (runtime3d)
9
+ * draws one through a `ZhudaDynamicMesh`.
10
+ */
11
+ import {
12
+ ZhudaFixedStep,
13
+ validateZhudaCollider,
14
+ zhudaColliderDepth,
15
+ prepareZhudaTable,
16
+ type ZhudaCollider,
17
+ type ZhudaSimVec3,
18
+ type ZhudaTablePrepared,
19
+ } from './ParticleCore.ts';
20
+
21
+ /** What a constraint holds: the weave (0), the weave's diagonals (1), or the cloth flat (2). */
22
+ export const ZHUDA_CLOTH_STRUCTURAL = 0;
23
+ export const ZHUDA_CLOTH_SHEAR = 1;
24
+ export const ZHUDA_CLOTH_BEND = 2;
25
+ /**
26
+ * A span (3): two particles of one thread, several apart, that may come
27
+ * nearer but get no farther apart than the thread between them is long.
28
+ * It stretches nothing and holds nothing in shape; it passes a pull along
29
+ * a thread several particles at a time, where the weave's own constraints
30
+ * pass it one particle a round and leave a heavy cloth stretched.
31
+ */
32
+ export const ZHUDA_CLOTH_SPAN = 3;
33
+ /**
34
+ * A particle less than this inside a body is on it: a micron. Single
35
+ * precision leaves a particle put on a surface that far in or out, and it
36
+ * is not moved again for it.
37
+ */
38
+ const ON_THE_SKIN = 1e-6;
39
+ /** How much longer than its thread a span lets two particles get. */
40
+ const SPAN_SLACK = 1.01;
41
+
42
+ /** A cloth at rest: its particles, how they are held together, and the mesh that is drawn. */
43
+ export interface ZhudaClothShape {
44
+ /** Three numbers a particle, metres. */
45
+ particles: Float32Array;
46
+ /** Pairs of particles kept a distance apart: `a[i]` and `b[i]` at `rest[i]` metres, of `kind[i]`. */
47
+ constraints: { a: Uint32Array; b: Uint32Array; rest: Float32Array; kind: Uint8Array };
48
+ /** The drawn mesh: which particle each vertex is. A seam has two vertices for one particle. */
49
+ particleOf: Uint32Array;
50
+ /** Two numbers a vertex. */
51
+ uvs: Float32Array;
52
+ /** Triangles of vertices, front faces wound as `ZhudaDynamicMesh` draws them. */
53
+ indices: Uint16Array;
54
+ /** A grid's or a tube's particles across and down; absent for a cloth made from a mesh. */
55
+ columns?: number;
56
+ rows?: number;
57
+ }
58
+
59
+ const finite3 = (v: unknown): v is [number, number, number] =>
60
+ Array.isArray(v) && v.length === 3 && v.every(Number.isFinite);
61
+ const whole = (v: unknown, min: number, max: number) =>
62
+ Number.isInteger(v) && (v as number) >= min && (v as number) <= max;
63
+ const positive = (v: unknown, max: number) =>
64
+ Number.isFinite(v) && (v as number) > 0 && (v as number) <= max;
65
+ const unit = (v: ZhudaSimVec3, name: string): [number, number, number] => {
66
+ const l = Math.hypot(v[0], v[1], v[2]);
67
+ if (!(l > 1e-9)) throw Error(`${name}: a direction, not zero`);
68
+ return [v[0] / l, v[1] / l, v[2] / l];
69
+ };
70
+
71
+ interface ConstraintList {
72
+ a: number[];
73
+ b: number[];
74
+ rest: number[];
75
+ kind: number[];
76
+ }
77
+ function link(list: ConstraintList, particles: Float32Array, a: number, b: number, kind: number) {
78
+ const rest = Math.hypot(
79
+ particles[a * 3] - particles[b * 3],
80
+ particles[a * 3 + 1] - particles[b * 3 + 1],
81
+ particles[a * 3 + 2] - particles[b * 3 + 2],
82
+ );
83
+ // Two particles in one place cannot be kept apart in any direction.
84
+ if (!(rest > 1e-9)) return;
85
+ list.a.push(a);
86
+ list.b.push(b);
87
+ list.rest.push(rest);
88
+ list.kind.push(kind);
89
+ }
90
+ const packed = (list: ConstraintList) => ({
91
+ a: Uint32Array.from(list.a),
92
+ b: Uint32Array.from(list.b),
93
+ rest: Float32Array.from(list.rest),
94
+ kind: Uint8Array.from(list.kind),
95
+ });
96
+
97
+ /** A sheet or a tube of `columns` by `rows` particles, with the constraints of a woven cloth. */
98
+ function lattice(
99
+ columns: number,
100
+ rows: number,
101
+ wrap: boolean,
102
+ place: (column: number, row: number) => [number, number, number],
103
+ ): ZhudaClothShape {
104
+ const particles = new Float32Array(columns * rows * 3);
105
+ for (let r = 0; r < rows; r++)
106
+ for (let c = 0; c < columns; c++) particles.set(place(c, r), (r * columns + c) * 3);
107
+ // Across: a tube's last column is held to its first.
108
+ const across = (c: number) => (wrap ? ((c % columns) + columns) % columns : c),
109
+ last = wrap ? columns : columns - 1;
110
+ const at = (c: number, r: number) => r * columns + across(c);
111
+ const list: ConstraintList = { a: [], b: [], rest: [], kind: [] };
112
+ for (let r = 0; r < rows; r++)
113
+ for (let c = 0; c < columns; c++) {
114
+ if (c < last) link(list, particles, at(c, r), at(c + 1, r), ZHUDA_CLOTH_STRUCTURAL);
115
+ if (r < rows - 1) link(list, particles, at(c, r), at(c, r + 1), ZHUDA_CLOTH_STRUCTURAL);
116
+ }
117
+ for (let r = 0; r < rows - 1; r++)
118
+ for (let c = 0; c < last; c++) {
119
+ link(list, particles, at(c, r), at(c + 1, r + 1), ZHUDA_CLOTH_SHEAR);
120
+ link(list, particles, at(c + 1, r), at(c, r + 1), ZHUDA_CLOTH_SHEAR);
121
+ }
122
+ const lastBend = wrap ? columns : columns - 2;
123
+ for (let r = 0; r < rows; r++)
124
+ for (let c = 0; c < columns; c++) {
125
+ if (c < lastBend && (!wrap || columns > 4))
126
+ link(list, particles, at(c, r), at(c + 2, r), ZHUDA_CLOTH_BEND);
127
+ if (r < rows - 2) link(list, particles, at(c, r), at(c, r + 2), ZHUDA_CLOTH_BEND);
128
+ }
129
+ // Spans along each thread, four, eight and sixteen particles apart, as long as the thread between.
130
+ const thread = (path: number[]) => {
131
+ let far = 0;
132
+ for (let i = 1; i < path.length; i++)
133
+ far += Math.hypot(
134
+ particles[path[i] * 3] - particles[path[i - 1] * 3],
135
+ particles[path[i] * 3 + 1] - particles[path[i - 1] * 3 + 1],
136
+ particles[path[i] * 3 + 2] - particles[path[i - 1] * 3 + 2],
137
+ );
138
+ if (!(far > 1e-9)) return;
139
+ list.a.push(path[0]);
140
+ list.b.push(path[path.length - 1]);
141
+ list.rest.push(far);
142
+ list.kind.push(ZHUDA_CLOTH_SPAN);
143
+ };
144
+ for (const apart of [4, 8, 16]) {
145
+ // Round a tube only while the thread between is the shorter way round.
146
+ const lastAcross = wrap ? (apart * 2 <= columns ? columns : 0) : columns - apart;
147
+ for (let r = 0; r < rows; r++)
148
+ for (let c = 0; c < lastAcross; c++)
149
+ thread(Array.from({ length: apart + 1 }, (_, k) => at(c + k, r)));
150
+ for (let r = 0; r < rows - apart; r++)
151
+ for (let c = 0; c < columns; c++)
152
+ thread(Array.from({ length: apart + 1 }, (_, k) => at(c, r + k)));
153
+ }
154
+ // The mesh: a tube has one more column of vertices than of particles, for the seam.
155
+ const meshColumns = wrap ? columns + 1 : columns;
156
+ const particleOf = new Uint32Array(meshColumns * rows),
157
+ uvs = new Float32Array(meshColumns * rows * 2);
158
+ for (let r = 0; r < rows; r++)
159
+ for (let c = 0; c < meshColumns; c++) {
160
+ const v = r * meshColumns + c;
161
+ particleOf[v] = at(c, r);
162
+ uvs[v * 2] = c / (meshColumns - 1);
163
+ uvs[v * 2 + 1] = r / (rows - 1);
164
+ }
165
+ const indices = new Uint16Array((meshColumns - 1) * (rows - 1) * 6);
166
+ let k = 0;
167
+ for (let r = 0; r < rows - 1; r++)
168
+ for (let c = 0; c < meshColumns - 1; c++) {
169
+ const v00 = r * meshColumns + c,
170
+ v10 = v00 + 1,
171
+ v01 = v00 + meshColumns,
172
+ v11 = v01 + 1;
173
+ indices.set([v00, v10, v11, v00, v11, v01], k);
174
+ k += 6;
175
+ }
176
+ return { particles, constraints: packed(list), particleOf, uvs, indices, columns, rows };
177
+ }
178
+
179
+ export interface ZhudaClothGridOptions {
180
+ /** Particles across, 2 to 256. */
181
+ columns: number;
182
+ /** Particles down, 2 to 256. */
183
+ rows: number;
184
+ /** Metres across and down. */
185
+ width: number;
186
+ height: number;
187
+ /** Where the first particle (the top left corner) is. Default: the sheet's top edge centred on the origin. */
188
+ origin?: ZhudaSimVec3;
189
+ /** Which way the columns run (default +x) and which way the rows run (default -y: a sheet hanging down). */
190
+ right?: ZhudaSimVec3;
191
+ down?: ZhudaSimVec3;
192
+ }
193
+
194
+ /**
195
+ * A flat sheet. Particle `row * columns + column`; row 0 is the top edge.
196
+ * Its front faces `cross(right, -down)` (+z for a sheet hanging in x and y).
197
+ */
198
+ export function createZhudaClothGrid(options: ZhudaClothGridOptions): ZhudaClothShape {
199
+ const { columns, rows, width, height } = options;
200
+ if (!whole(columns, 2, 256)) throw Error('Invalid columns: a whole number, 2 to 256');
201
+ if (!whole(rows, 2, 256)) throw Error('Invalid rows: a whole number, 2 to 256');
202
+ if (columns * rows > 65535) throw Error('A cloth holds at most 65535 particles');
203
+ if (!positive(width, 1000)) throw Error('Invalid width: metres, more than 0');
204
+ if (!positive(height, 1000)) throw Error('Invalid height: metres, more than 0');
205
+ const right = unit(options.right ?? [1, 0, 0], 'right'),
206
+ down = unit(options.down ?? [0, -1, 0], 'down');
207
+ const origin = options.origin ?? [
208
+ (-right[0] * width) / 2,
209
+ (-right[1] * width) / 2,
210
+ (-right[2] * width) / 2,
211
+ ];
212
+ if (!finite3(origin)) throw Error('Invalid origin: three finite numbers');
213
+ return lattice(columns, rows, false, (c, r) => {
214
+ const u = (c / (columns - 1)) * width,
215
+ v = (r / (rows - 1)) * height;
216
+ return [
217
+ origin[0] + right[0] * u + down[0] * v,
218
+ origin[1] + right[1] * u + down[1] * v,
219
+ origin[2] + right[2] * u + down[2] * v,
220
+ ];
221
+ });
222
+ }
223
+
224
+ export interface ZhudaClothTubeOptions {
225
+ /** Particles round, 4 to 256. */
226
+ columns: number;
227
+ /** Rings, 2 to 256. */
228
+ rows: number;
229
+ /** The middle of the first ring (a skirt's waist). */
230
+ center: ZhudaSimVec3;
231
+ /** Which way the rings follow one another (default -y: down). */
232
+ axis?: ZhudaSimVec3;
233
+ /** Where a ring's first particle is, seen from its middle (default +z). */
234
+ forward?: ZhudaSimVec3;
235
+ /** The first ring's radius and the last ring's, metres. */
236
+ topRadius: number;
237
+ bottomRadius: number;
238
+ /** Metres from the first ring to the last, along the axis. */
239
+ length: number;
240
+ }
241
+
242
+ /**
243
+ * A tube closed round: a skirt, a sleeve, a lampshade. Particle `row *
244
+ * columns + column`; its mesh has one more column of vertices, for the
245
+ * seam. Its front faces outward.
246
+ */
247
+ export function createZhudaClothTube(options: ZhudaClothTubeOptions): ZhudaClothShape {
248
+ const { columns, rows, topRadius, bottomRadius } = options;
249
+ if (!whole(columns, 4, 256)) throw Error('Invalid columns: a whole number, 4 to 256');
250
+ if (!whole(rows, 2, 256)) throw Error('Invalid rows: a whole number, 2 to 256');
251
+ if ((columns + 1) * rows > 65535) throw Error('A cloth holds at most 65535 vertices');
252
+ if (!positive(topRadius, 1000)) throw Error('Invalid topRadius: metres, more than 0');
253
+ if (!positive(bottomRadius, 1000)) throw Error('Invalid bottomRadius: metres, more than 0');
254
+ if (!positive(options.length, 1000)) throw Error('Invalid length: metres, more than 0');
255
+ if (!finite3(options.center)) throw Error('Invalid center: three finite numbers');
256
+ const axis = unit(options.axis ?? [0, -1, 0], 'axis');
257
+ let forward = unit(options.forward ?? [0, 0, 1], 'forward');
258
+ // Square to the axis, whatever was given.
259
+ const along = forward[0] * axis[0] + forward[1] * axis[1] + forward[2] * axis[2];
260
+ forward = unit(
261
+ [forward[0] - axis[0] * along, forward[1] - axis[1] * along, forward[2] - axis[2] * along],
262
+ 'forward (it may not lie along the axis)',
263
+ );
264
+ // Round the axis so that the front of each triangle faces outward.
265
+ const side = [
266
+ forward[1] * axis[2] - forward[2] * axis[1],
267
+ forward[2] * axis[0] - forward[0] * axis[2],
268
+ forward[0] * axis[1] - forward[1] * axis[0],
269
+ ];
270
+ const { center } = options;
271
+ return lattice(columns, rows, true, (c, r) => {
272
+ const t = r / (rows - 1),
273
+ radius = topRadius + (bottomRadius - topRadius) * t,
274
+ angle = (c / columns) * 2 * Math.PI,
275
+ x = Math.cos(angle) * radius,
276
+ y = Math.sin(angle) * radius,
277
+ d = t * options.length;
278
+ return [
279
+ center[0] + forward[0] * x + side[0] * y + axis[0] * d,
280
+ center[1] + forward[1] * x + side[1] * y + axis[1] * d,
281
+ center[2] + forward[2] * x + side[2] * y + axis[2] * d,
282
+ ];
283
+ });
284
+ }
285
+
286
+ export interface ZhudaClothMeshSource {
287
+ /** Three numbers a vertex. */
288
+ positions: ArrayLike<number>;
289
+ /** Triangles. */
290
+ indices: ArrayLike<number>;
291
+ /** Two numbers a vertex; zeros when left out. */
292
+ uvs?: ArrayLike<number>;
293
+ /** Vertices nearer than this are one particle (a seam, a hard edge), metres. Default 1e-5. */
294
+ weld?: number;
295
+ }
296
+
297
+ /**
298
+ * A cloth from a mesh of one's own: every edge of a triangle is held at its
299
+ * length, and across every edge two triangles share, the two far corners
300
+ * are held apart (so the cloth resists folding). Vertices in one place
301
+ * become one particle.
302
+ */
303
+ export function createZhudaClothFromMesh(source: ZhudaClothMeshSource): ZhudaClothShape {
304
+ const { positions, indices } = source;
305
+ const count = positions.length / 3;
306
+ if (!Number.isInteger(count) || count < 3 || count > 65535)
307
+ throw Error('Invalid positions: three numbers a vertex, 3 to 65535 vertices');
308
+ if (!indices.length || indices.length % 3) throw Error('Invalid indices: three a triangle');
309
+ for (let i = 0; i < positions.length; i++)
310
+ if (!Number.isFinite(positions[i])) throw Error('Invalid positions: finite numbers');
311
+ for (let i = 0; i < indices.length; i++)
312
+ if (!Number.isInteger(indices[i]) || indices[i] < 0 || indices[i] >= count)
313
+ throw Error(`Invalid indices: ${indices[i]} is not a vertex`);
314
+ if (source.uvs && source.uvs.length !== count * 2)
315
+ throw Error('Invalid uvs: two numbers a vertex');
316
+ const weld = source.weld ?? 1e-5;
317
+ if (!(Number.isFinite(weld) && weld > 0 && weld <= 1))
318
+ throw Error('Invalid weld: metres, more than 0');
319
+ // Vertices that share a cell of the welding grid, or sit across its border within reach.
320
+ const particleOf = new Uint32Array(count);
321
+ const cells = new Map<string, number[]>();
322
+ const found: number[] = [];
323
+ const cell = (v: number) => Math.floor(v / weld);
324
+ for (let v = 0; v < count; v++) {
325
+ const x = positions[v * 3],
326
+ y = positions[v * 3 + 1],
327
+ z = positions[v * 3 + 2];
328
+ let particle = -1;
329
+ search: for (let dx = -1; dx <= 1; dx++)
330
+ for (let dy = -1; dy <= 1; dy++)
331
+ for (let dz = -1; dz <= 1; dz++)
332
+ for (const p of cells.get(`${cell(x) + dx},${cell(y) + dy},${cell(z) + dz}`) ?? [])
333
+ if (Math.hypot(found[p * 3] - x, found[p * 3 + 1] - y, found[p * 3 + 2] - z) <= weld) {
334
+ particle = p;
335
+ break search;
336
+ }
337
+ if (particle < 0) {
338
+ particle = found.length / 3;
339
+ found.push(x, y, z);
340
+ const key = `${cell(x)},${cell(y)},${cell(z)}`;
341
+ const list = cells.get(key);
342
+ if (list) list.push(particle);
343
+ else cells.set(key, [particle]);
344
+ }
345
+ particleOf[v] = particle;
346
+ }
347
+ const particles = Float32Array.from(found);
348
+ const list: ConstraintList = { a: [], b: [], rest: [], kind: [] };
349
+ // Each edge once, and the corner opposite it in each triangle that has it.
350
+ const edges = new Map<number, number[]>();
351
+ const total = particles.length / 3;
352
+ for (let t = 0; t < indices.length; t += 3) {
353
+ const corner = [particleOf[indices[t]], particleOf[indices[t + 1]], particleOf[indices[t + 2]]];
354
+ if (corner[0] === corner[1] || corner[1] === corner[2] || corner[2] === corner[0]) continue;
355
+ for (let e = 0; e < 3; e++) {
356
+ const a = corner[e],
357
+ b = corner[(e + 1) % 3],
358
+ far = corner[(e + 2) % 3];
359
+ const key = Math.min(a, b) * total + Math.max(a, b);
360
+ const seen = edges.get(key);
361
+ if (seen) seen.push(far);
362
+ else {
363
+ edges.set(key, [far]);
364
+ link(list, particles, Math.min(a, b), Math.max(a, b), ZHUDA_CLOTH_STRUCTURAL);
365
+ }
366
+ }
367
+ }
368
+ for (const far of edges.values())
369
+ if (far.length === 2 && far[0] !== far[1])
370
+ link(list, particles, Math.min(far[0], far[1]), Math.max(far[0], far[1]), ZHUDA_CLOTH_BEND);
371
+ return {
372
+ particles,
373
+ constraints: packed(list),
374
+ particleOf,
375
+ uvs: source.uvs ? Float32Array.from(source.uvs) : new Float32Array(count * 2),
376
+ indices: Uint16Array.from(indices),
377
+ };
378
+ }
379
+
380
+ /** What is wrong with a shape, in words; empty when nothing is. Pure. */
381
+ export function validateZhudaClothShape(shape: unknown): string[] {
382
+ const s = shape as ZhudaClothShape;
383
+ if (s === null || typeof s !== 'object') return ['shape: an object'];
384
+ const issues: string[] = [];
385
+ const count = s.particles instanceof Float32Array ? s.particles.length / 3 : NaN;
386
+ if (!Number.isInteger(count) || count < 2 || count > 65535)
387
+ return ['shape.particles: a Float32Array, three numbers a particle, 2 to 65535 particles'];
388
+ for (let i = 0; i < s.particles.length; i++)
389
+ if (!Number.isFinite(s.particles[i])) {
390
+ issues.push(`shape.particles[${i}]: a finite number`);
391
+ break;
392
+ }
393
+ const c = s.constraints;
394
+ if (
395
+ !c ||
396
+ !(c.a instanceof Uint32Array) ||
397
+ !(c.b instanceof Uint32Array) ||
398
+ !(c.rest instanceof Float32Array) ||
399
+ !(c.kind instanceof Uint8Array) ||
400
+ c.a.length !== c.b.length ||
401
+ c.a.length !== c.rest.length ||
402
+ c.a.length !== c.kind.length
403
+ )
404
+ issues.push('shape.constraints: a, b, rest and kind, one of each a constraint');
405
+ else
406
+ for (let i = 0; i < c.a.length; i++) {
407
+ if (c.a[i] >= count || c.b[i] >= count) {
408
+ issues.push(
409
+ `shape.constraints[${i}]: particle ${Math.max(c.a[i], c.b[i])} is not there (${count} particles)`,
410
+ );
411
+ break;
412
+ }
413
+ if (c.a[i] === c.b[i]) {
414
+ issues.push(`shape.constraints[${i}]: a particle cannot be held to itself`);
415
+ break;
416
+ }
417
+ if (!(Number.isFinite(c.rest[i]) && c.rest[i] > 0)) {
418
+ issues.push(`shape.constraints[${i}].rest: metres, more than 0`);
419
+ break;
420
+ }
421
+ if (c.kind[i] > ZHUDA_CLOTH_SPAN) {
422
+ issues.push(
423
+ `shape.constraints[${i}].kind: 0 (structural), 1 (shear), 2 (bend) or 3 (span)`,
424
+ );
425
+ break;
426
+ }
427
+ }
428
+ const vertices = s.particleOf instanceof Uint32Array ? s.particleOf.length : NaN;
429
+ if (!Number.isInteger(vertices) || vertices < 3 || vertices > 65535)
430
+ issues.push('shape.particleOf: a Uint32Array, one particle a vertex, 3 to 65535 vertices');
431
+ else {
432
+ for (let v = 0; v < vertices; v++)
433
+ if (s.particleOf[v] >= count) {
434
+ issues.push(`shape.particleOf[${v}]: particle ${s.particleOf[v]} is not there`);
435
+ break;
436
+ }
437
+ if (!(s.uvs instanceof Float32Array) || s.uvs.length !== vertices * 2)
438
+ issues.push('shape.uvs: a Float32Array, two numbers a vertex');
439
+ if (!(s.indices instanceof Uint16Array) || !s.indices.length || s.indices.length % 3)
440
+ issues.push('shape.indices: a Uint16Array, three vertices a triangle');
441
+ else
442
+ for (let i = 0; i < s.indices.length; i++)
443
+ if (s.indices[i] >= vertices) {
444
+ issues.push(`shape.indices[${i}]: vertex ${s.indices[i]} is not there`);
445
+ break;
446
+ }
447
+ }
448
+ return issues;
449
+ }
450
+
451
+ export interface ZhudaClothOptions {
452
+ /** Seconds a step (default 1/240: four steps a frame at sixty frames a second; 1/1000 to 1/10). */
453
+ step?: number;
454
+ /** How many times a step goes over the constraints (default 2; 1 to 32). Smaller steps do more for a stiff cloth than more rounds. */
455
+ rounds?: number;
456
+ /** The most steps one `advance` runs; time over that is dropped (default 8, a thirtieth of a second; 1 to 64). */
457
+ maxSteps?: number;
458
+ /** Metres a second a second (default 0, -9.81, 0). */
459
+ gravity?: ZhudaSimVec3;
460
+ /** How fast a particle loses its speed whichever way it moves: the share it keeps after a second is `exp(-drag)` (default 1; 0 to 20). */
461
+ drag?: number;
462
+ /**
463
+ * How much each kind of constraint gives: 0 is as stiff as the rounds
464
+ * make it, more is softer. Defaults: structural 0, shear 3e-4, bend 2e-4
465
+ * (0 to 1). A woven cloth gives little along its threads and much across
466
+ * them: with a stiff shear a sheet behaves like a sheet of paper.
467
+ */
468
+ compliance?: { structural?: number; shear?: number; bend?: number };
469
+ /** How far outside a body the cloth is kept, metres (default 0.01; 0 to 0.5). */
470
+ thickness?: number;
471
+ /**
472
+ * How well the cloth grips a body it lies on (default 0.5; 0 to 4): it
473
+ * stays on a slope whose rise over its run is less than this, and slides
474
+ * on a steeper one.
475
+ */
476
+ friction?: number;
477
+ /** The fastest a particle moves, metres a second (default 6; 0.1 to 100). See `ZhudaClothSolver` on thin bodies. */
478
+ maxSpeed?: number;
479
+ /** The air's velocity, metres a second (default none). */
480
+ wind?: ZhudaSimVec3;
481
+ /**
482
+ * How hard the air that meets the cloth face on pushes it, a second
483
+ * (default 2; 0 for no air; 0 to 50). It is what a wind blows a cloth
484
+ * with, and what stops a cloth swinging: still air resists a cloth that
485
+ * moves face on, and lets it slide edge on.
486
+ */
487
+ windDrag?: number;
488
+ /**
489
+ * How much farther from the pin it hangs from a particle may get than
490
+ * the cloth between them is long, as a share (default 0.02; 0 to 1; false
491
+ * for no such limit). Without it a cloth hung from few pins stretches
492
+ * like rubber where it hangs: the rounds of a step pass a pull along
493
+ * only a few particles at a time.
494
+ */
495
+ tether?: number | false;
496
+ /** The fastest a hand that holds a particle moves, metres a second (default 10; 0.1 to 100). */
497
+ grabSpeed?: number;
498
+ /**
499
+ * How far ahead of the cloth a hand may get, as a share of the weave's
500
+ * spacing at the particle it holds (default 0.1; 0.01 to 2). A hand
501
+ * pulls the particle to itself outright; held back to this, it cannot
502
+ * tear the particle away from its neighbours faster than the weave
503
+ * passes the pull on. Where the threads at the particle are stretched by
504
+ * more than a twentieth (the cloth is taut) the hand's lead is cut, step
505
+ * by step, to a sixty-fourth of this: a hand asked to go where the cloth
506
+ * cannot reach waits at the cloth.
507
+ */
508
+ grabStretch?: number;
509
+ }
510
+
511
+ export interface ZhudaClothResolved {
512
+ step: number;
513
+ rounds: number;
514
+ maxSteps: number;
515
+ gravity: [number, number, number];
516
+ drag: number;
517
+ compliance: { structural: number; shear: number; bend: number };
518
+ thickness: number;
519
+ friction: number;
520
+ maxSpeed: number;
521
+ wind: [number, number, number];
522
+ windDrag: number;
523
+ tether: number | false;
524
+ grabSpeed: number;
525
+ grabStretch: number;
526
+ }
527
+
528
+ const OPTION_KEYS = [
529
+ 'step',
530
+ 'rounds',
531
+ 'maxSteps',
532
+ 'gravity',
533
+ 'drag',
534
+ 'compliance',
535
+ 'thickness',
536
+ 'friction',
537
+ 'maxSpeed',
538
+ 'wind',
539
+ 'windDrag',
540
+ 'tether',
541
+ 'grabSpeed',
542
+ 'grabStretch',
543
+ ];
544
+
545
+ /** A cloth's options with their defaults. Throws, with the name, on one out of range or unknown. Pure. */
546
+ export function resolveZhudaClothOptions(options: ZhudaClothOptions = {}): ZhudaClothResolved {
547
+ if (options === null || typeof options !== 'object') throw Error('Invalid cloth options');
548
+ for (const key of Object.keys(options))
549
+ if (!OPTION_KEYS.includes(key)) throw Error(`Unknown cloth option: ${key}`);
550
+ const number = (name: string, value: unknown, fallback: number, min: number, max: number) => {
551
+ if (value === undefined) return fallback;
552
+ if (!(Number.isFinite(value) && (value as number) >= min && (value as number) <= max))
553
+ throw Error(`Invalid ${name}: ${min} to ${max}`);
554
+ return value as number;
555
+ };
556
+ const vector = (name: string, value: unknown, fallback: [number, number, number]) => {
557
+ if (value === undefined) return fallback;
558
+ if (!finite3(value)) throw Error(`Invalid ${name}: three finite numbers`);
559
+ return [value[0], value[1], value[2]] as [number, number, number];
560
+ };
561
+ const rounds = options.rounds ?? 2;
562
+ if (!whole(rounds, 1, 32)) throw Error('Invalid rounds: a whole number, 1 to 32');
563
+ const give = options.compliance ?? {};
564
+ if (give === null || typeof give !== 'object') throw Error('Invalid compliance');
565
+ for (const key of Object.keys(give))
566
+ if (!['structural', 'shear', 'bend'].includes(key))
567
+ throw Error(`Unknown compliance: ${key} (structural, shear, bend)`);
568
+ // The step and the cap are checked by the fixed step itself, with the same words.
569
+ const fixed = new ZhudaFixedStep({
570
+ step: options.step ?? 1 / 240,
571
+ maxSteps: options.maxSteps ?? 8,
572
+ });
573
+ return {
574
+ step: fixed.step,
575
+ rounds,
576
+ maxSteps: fixed.maxSteps,
577
+ gravity: vector('gravity', options.gravity, [0, -9.81, 0]),
578
+ drag: number('drag', options.drag, 1, 0, 20),
579
+ compliance: {
580
+ structural: number('compliance.structural', give.structural, 0, 0, 1),
581
+ shear: number('compliance.shear', give.shear, 3e-4, 0, 1),
582
+ bend: number('compliance.bend', give.bend, 2e-4, 0, 1),
583
+ },
584
+ thickness: number('thickness', options.thickness, 0.01, 0, 0.5),
585
+ friction: number('friction', options.friction, 0.5, 0, 4),
586
+ maxSpeed: number('maxSpeed', options.maxSpeed, 6, 0.1, 100),
587
+ wind: vector('wind', options.wind, [0, 0, 0]),
588
+ windDrag: number('windDrag', options.windDrag, 2, 0, 50),
589
+ tether: options.tether === false ? false : number('tether', options.tether, 0.02, 0, 1),
590
+ grabSpeed: number('grabSpeed', options.grabSpeed, 10, 0.1, 100),
591
+ grabStretch: number('grabStretch', options.grabStretch, 0.1, 0.01, 2),
592
+ };
593
+ }
594
+
595
+ export interface ZhudaClothReport {
596
+ particles: number;
597
+ constraints: number;
598
+ triangles: number;
599
+ colliders: number;
600
+ pinned: number;
601
+ grabbed: number;
602
+ /** Steps run and steps dropped since the cloth was made or reset. */
603
+ steps: number;
604
+ dropped: number;
605
+ /** Seconds of the clock gathered and not yet spent. */
606
+ pendingSeconds: number;
607
+ /** Particles deeper inside a body than a tenth of a millimetre. Pins are counted too. */
608
+ inside: number;
609
+ /** The most any weave constraint is longer than its rest length, as a share of it. */
610
+ maxStretch: number;
611
+ /** Half the sum of the squares of the particles' speeds (masses of one). */
612
+ kineticEnergy: number;
613
+ /** The smallest body radius a particle cannot cross in one step: `maxSpeed * step`. */
614
+ safeRadius: number;
615
+ }
616
+
617
+ /**
618
+ * A cloth in motion.
619
+ *
620
+ * Each step: every free particle keeps its motion (less the air's drag),
621
+ * takes gravity and the wind, and moves, no faster than `maxSpeed`; then
622
+ * `rounds` times the constraints pull particles back to their distances
623
+ * and the bodies push them out; the bodies have the last word, so a step
624
+ * ends with no particle inside one. A particle is tested where it is, not
625
+ * along its path: it cannot cross a body whose radius is more than
626
+ * `maxSpeed * step` (2.5 cm by default); a thinner body may be crossed by a
627
+ * fast cloth, and `inspect().safeRadius` says where the line is.
628
+ *
629
+ * Positions are in one space, the world's unless the cloth's owner says
630
+ * otherwise; bodies and pins are given in the same space and may be moved
631
+ * between steps.
632
+ */
633
+ export class ZhudaClothSolver {
634
+ readonly shape: ZhudaClothShape;
635
+ readonly options: ZhudaClothResolved;
636
+ /** Where each particle is: three numbers each. Read it; write through `pin`, `movePin`, `reset`. */
637
+ readonly positions: Float32Array;
638
+ /** Where each particle was a step ago. */
639
+ readonly previous: Float32Array;
640
+ /** How far each particle's weave is stretched, as a share of its rest length; filled by `measureStrain`. */
641
+ readonly strain: Float32Array;
642
+ readonly count: number;
643
+ private readonly clock: ZhudaFixedStep;
644
+ private readonly inverseMass: Float32Array;
645
+ private readonly lambda: Float32Array;
646
+ private readonly alpha: Float32Array;
647
+ private readonly triangles: Uint32Array;
648
+ private readonly air: Float32Array;
649
+ private readonly facing: Uint16Array;
650
+ private readonly touched: Uint8Array;
651
+ /** How far the bodies have pushed each particle back this step: how hard it presses on them. */
652
+ private readonly pressed: Float32Array;
653
+ private readonly normal: Float32Array;
654
+ private readonly scratch = new Float64Array(6);
655
+ private readonly bodies = new Map<number, ZhudaCollider>();
656
+ /** Each table worked out for the step: its slopes, its widest radius. */
657
+ private readonly tables = new Map<number, ZhudaTablePrepared>();
658
+
659
+ private nextBody = 1;
660
+ /** A hand's hold on a particle: where the hand is, where it wants to be, and what its hold has done this step. */
661
+ private readonly hands = new Map<
662
+ number,
663
+ { at: Float64Array; to: Float64Array; slack: number; lead: number; threads: number[] }
664
+ >();
665
+ private readonly structuralCount: Uint16Array;
666
+ /** For each particle: the pin it hangs from (-1: none) and how far from it it may get. */
667
+ private readonly anchor: Int32Array;
668
+ private readonly leash: Float32Array;
669
+ private tethersStale = true;
670
+ /** Rounds run in all: odd ones go over the constraints backwards. */
671
+ private sweeps = 0;
672
+ /** The weave as a graph, for finding how much cloth lies between a particle and a pin. */
673
+ private readonly neighbours: { start: Uint32Array; to: Uint32Array; far: Float32Array };
674
+ private disposed = false;
675
+
676
+ constructor(shape: ZhudaClothShape, options: ZhudaClothOptions = {}) {
677
+ const issues = validateZhudaClothShape(shape);
678
+ if (issues.length) throw Error(`Invalid cloth shape: ${issues.join('; ')}`);
679
+ this.shape = shape;
680
+ this.options = resolveZhudaClothOptions(options);
681
+ this.clock = new ZhudaFixedStep(this.options);
682
+ this.count = shape.particles.length / 3;
683
+ this.positions = Float32Array.from(shape.particles);
684
+ this.previous = Float32Array.from(shape.particles);
685
+ this.strain = new Float32Array(this.count);
686
+ this.inverseMass = new Float32Array(this.count).fill(1);
687
+ const constraints = shape.constraints.a.length;
688
+ this.lambda = new Float32Array(constraints);
689
+ this.alpha = new Float32Array(constraints);
690
+ this.setCompliance(this.options.compliance);
691
+ // The triangles by particle, for the air; a seam's two vertices are one particle.
692
+ const { indices, particleOf } = shape;
693
+ const triangles: number[] = [];
694
+ for (let t = 0; t < indices.length; t += 3) {
695
+ const a = particleOf[indices[t]],
696
+ b = particleOf[indices[t + 1]],
697
+ c = particleOf[indices[t + 2]];
698
+ if (a !== b && b !== c && c !== a) triangles.push(a, b, c);
699
+ }
700
+ this.triangles = Uint32Array.from(triangles);
701
+ this.air = new Float32Array(this.count * 3);
702
+ this.facing = new Uint16Array(this.count);
703
+ for (const p of this.triangles) this.facing[p]++;
704
+ this.touched = new Uint8Array(this.count);
705
+
706
+ this.pressed = new Float32Array(this.count);
707
+ this.normal = new Float32Array(this.count * 3);
708
+ this.structuralCount = new Uint16Array(this.count);
709
+ const { a, b, kind } = shape.constraints;
710
+ for (let i = 0; i < constraints; i++)
711
+ if (kind[i] === ZHUDA_CLOTH_STRUCTURAL) {
712
+ this.structuralCount[a[i]]++;
713
+ this.structuralCount[b[i]]++;
714
+ }
715
+ this.anchor = new Int32Array(this.count).fill(-1);
716
+ this.leash = new Float32Array(this.count);
717
+ // The weave and its diagonals (not the constraints that keep it flat: they skip a particle).
718
+ const { rest } = shape.constraints;
719
+ const degree = new Uint32Array(this.count + 1);
720
+ for (let i = 0; i < constraints; i++)
721
+ if (kind[i] !== ZHUDA_CLOTH_BEND) {
722
+ degree[a[i] + 1]++;
723
+ degree[b[i] + 1]++;
724
+ }
725
+ for (let i = 0; i < this.count; i++) degree[i + 1] += degree[i];
726
+ const to = new Uint32Array(degree[this.count]),
727
+ far = new Float32Array(degree[this.count]),
728
+ fill = Uint32Array.from(degree.subarray(0, this.count));
729
+ for (let i = 0; i < constraints; i++)
730
+ if (kind[i] !== ZHUDA_CLOTH_BEND) {
731
+ to[fill[a[i]]] = b[i];
732
+ far[fill[a[i]]++] = rest[i];
733
+ to[fill[b[i]]] = a[i];
734
+ far[fill[b[i]]++] = rest[i];
735
+ }
736
+ this.neighbours = { start: degree, to, far };
737
+ }
738
+
739
+ /**
740
+ * For every particle, the pin with the least cloth between them and how
741
+ * much that is: the shortest way along the weave and its diagonals, which
742
+ * is never shorter than the cloth itself, so the limit never forbids a
743
+ * shape the cloth could take. Worked out again when a pin is set or let
744
+ * go, not when one is moved.
745
+ */
746
+ private tether() {
747
+ this.tethersStale = false;
748
+ const { anchor, leash, count } = this,
749
+ { start, to, far } = this.neighbours;
750
+ anchor.fill(-1);
751
+ const reach = new Float64Array(count).fill(Infinity);
752
+ // The nearest first, from a heap ordered by how far along the weave each is.
753
+ const heap: number[] = [];
754
+ const push = (i: number) => {
755
+ heap.push(i);
756
+ let at = heap.length - 1;
757
+ while (at > 0) {
758
+ const up = (at - 1) >> 1;
759
+ // Ties go to the lower particle, so the order is the same every time.
760
+ if (reach[heap[up]] < reach[i] || (reach[heap[up]] === reach[i] && heap[up] < i)) break;
761
+ heap[at] = heap[up];
762
+ at = up;
763
+ }
764
+ heap[at] = i;
765
+ };
766
+ const pop = () => {
767
+ const top = heap[0],
768
+ last = heap.pop() as number;
769
+ if (heap.length) {
770
+ let at = 0;
771
+ for (;;) {
772
+ let child = at * 2 + 1;
773
+ if (child >= heap.length) break;
774
+ const other = child + 1;
775
+ if (
776
+ other < heap.length &&
777
+ (reach[heap[other]] < reach[heap[child]] ||
778
+ (reach[heap[other]] === reach[heap[child]] && heap[other] < heap[child]))
779
+ )
780
+ child = other;
781
+ if (
782
+ reach[last] < reach[heap[child]] ||
783
+ (reach[last] === reach[heap[child]] && last < heap[child])
784
+ )
785
+ break;
786
+ heap[at] = heap[child];
787
+ at = child;
788
+ }
789
+ heap[at] = last;
790
+ }
791
+ return top;
792
+ };
793
+ for (let i = 0; i < count; i++)
794
+ if (this.inverseMass[i] === 0) {
795
+ reach[i] = 0;
796
+ anchor[i] = i;
797
+ push(i);
798
+ }
799
+ const done = new Uint8Array(count);
800
+ while (heap.length) {
801
+ const i = pop();
802
+ if (done[i]) continue;
803
+ done[i] = 1;
804
+ for (let k = start[i]; k < start[i + 1]; k++) {
805
+ const j = to[k],
806
+ next = reach[i] + far[k];
807
+ if (next < reach[j]) {
808
+ reach[j] = next;
809
+ anchor[j] = anchor[i];
810
+ push(j);
811
+ }
812
+ }
813
+ }
814
+ const slack = 1 + (this.options.tether === false ? 0 : this.options.tether);
815
+ for (let i = 0; i < count; i++) {
816
+ leash[i] = Number.isFinite(reach[i]) ? reach[i] * slack : 0;
817
+ if (this.inverseMass[i] === 0) anchor[i] = -1;
818
+ }
819
+ }
820
+
821
+ private alive() {
822
+ if (this.disposed) throw Error('Disposed cloth');
823
+ }
824
+ private particle(index: number, what = 'particle') {
825
+ if (!Number.isInteger(index) || index < 0 || index >= this.count)
826
+ throw Error(`Invalid ${what}: 0 to ${this.count - 1}`);
827
+ }
828
+
829
+ /** How much each kind of constraint gives, from now on. */
830
+ setCompliance(compliance: { structural?: number; shear?: number; bend?: number }) {
831
+ this.alive();
832
+ const next = resolveZhudaClothOptions({
833
+ compliance: { ...this.options.compliance, ...compliance },
834
+ }).compliance;
835
+ this.options.compliance = next;
836
+ const by = [next.structural, next.shear, next.bend, 0],
837
+ { kind } = this.shape.constraints,
838
+ dt2 = this.options.step * this.options.step;
839
+ for (let i = 0; i < kind.length; i++) this.alpha[i] = by[kind[i]] / dt2;
840
+ }
841
+ setGravity(x: number, y: number, z: number) {
842
+ this.alive();
843
+ this.options.gravity = resolveZhudaClothOptions({ gravity: [x, y, z] }).gravity;
844
+ }
845
+ /** The air's velocity and how hard it pushes, from now on. */
846
+ setWind(x: number, y: number, z: number, windDrag = this.options.windDrag) {
847
+ this.alive();
848
+ const next = resolveZhudaClothOptions({ wind: [x, y, z], windDrag });
849
+ this.options.wind = next.wind;
850
+ this.options.windDrag = next.windDrag;
851
+ }
852
+
853
+ /**
854
+ * Holds a particle where it is, or at a place. It no longer falls, and
855
+ * no body moves it.
856
+ */
857
+ pin(index: number, at?: ZhudaSimVec3) {
858
+ this.alive();
859
+ this.particle(index);
860
+ if (this.hands.has(index)) throw Error(`Particle ${index} is held by a hand: release it first`);
861
+ if (at !== undefined && !finite3(at)) throw Error('Invalid place: three finite numbers');
862
+ this.inverseMass[index] = 0;
863
+ this.tethersStale = true;
864
+ if (at) this.positions.set(at, index * 3);
865
+ this.previous.set(this.positions.subarray(index * 3, index * 3 + 3), index * 3);
866
+ }
867
+ /**
868
+ * Moves a pinned particle: with what it hangs from, each frame, a little
869
+ * at a time. For a jump, `shift` the cloth.
870
+ */
871
+ movePin(index: number, x: number, y: number, z: number) {
872
+ this.alive();
873
+ this.particle(index);
874
+ if (this.inverseMass[index] !== 0) throw Error(`Particle ${index} is not pinned`);
875
+ if (![x, y, z].every(Number.isFinite)) throw Error('Invalid place: three finite numbers');
876
+ this.positions.set([x, y, z], index * 3);
877
+ this.previous.set([x, y, z], index * 3);
878
+ }
879
+ unpin(index: number) {
880
+ this.alive();
881
+ this.particle(index);
882
+ if (this.inverseMass[index] !== 0) throw Error(`Particle ${index} is not pinned`);
883
+ this.inverseMass[index] = 1;
884
+ this.tethersStale = true;
885
+ }
886
+ isPinned(index: number): boolean {
887
+ this.particle(index);
888
+ return this.inverseMass[index] === 0;
889
+ }
890
+
891
+ /**
892
+ * A hand takes hold of a particle where it is. `dragTo` then says where
893
+ * the hand wants to be; it goes there no faster than `grabSpeed`, and
894
+ * the particle follows as far as the cloth lets it.
895
+ */
896
+ grab(index: number) {
897
+ this.alive();
898
+ this.particle(index);
899
+ if (this.inverseMass[index] === 0)
900
+ throw Error(`Particle ${index} is pinned: unpin it before a hand takes it`);
901
+ if (this.hands.has(index)) throw Error(`Particle ${index} is held already`);
902
+ const at = Float64Array.from(this.positions.subarray(index * 3, index * 3 + 3));
903
+ // The weave's spacing here: the shortest thread the particle is held by.
904
+ const { a, b, rest, kind } = this.shape.constraints;
905
+ let spacing = Infinity;
906
+ const threads: number[] = [];
907
+ for (let i = 0; i < a.length; i++)
908
+ if (kind[i] === ZHUDA_CLOTH_STRUCTURAL && (a[i] === index || b[i] === index)) {
909
+ spacing = Math.min(spacing, rest[i]);
910
+ threads.push(i);
911
+ }
912
+ if (!Number.isFinite(spacing)) spacing = 0.05;
913
+ this.hands.set(index, {
914
+ at,
915
+ to: Float64Array.from(at),
916
+ slack: spacing * this.options.grabStretch,
917
+ lead: spacing * this.options.grabStretch,
918
+ threads,
919
+ });
920
+ }
921
+ dragTo(index: number, x: number, y: number, z: number) {
922
+ this.alive();
923
+ const hand = this.hands.get(index);
924
+ if (!hand) throw Error(`Particle ${index} is not held`);
925
+ if (![x, y, z].every(Number.isFinite)) throw Error('Invalid place: three finite numbers');
926
+ hand.to.set([x, y, z]);
927
+ }
928
+ /** The hand lets go; the particle keeps the motion it had. */
929
+ release(index: number) {
930
+ this.alive();
931
+ if (!this.hands.delete(index)) throw Error(`Particle ${index} is not held`);
932
+ }
933
+ isGrabbed(index: number): boolean {
934
+ return this.hands.has(index);
935
+ }
936
+ /** Where the hand that holds a particle is now. */
937
+ handOf(index: number): [number, number, number] {
938
+ const hand = this.hands.get(index);
939
+ if (!hand) throw Error(`Particle ${index} is not held`);
940
+ return [hand.at[0], hand.at[1], hand.at[2]];
941
+ }
942
+
943
+ /**
944
+ * The particle a ray (a pointer's, from the camera) would touch: of the
945
+ * layer of cloth it comes to first, the particle it passes closest to; -1
946
+ * when none is within `reach` metres of it. `direction` need not be a
947
+ * unit.
948
+ */
949
+ nearestToRay(origin: ZhudaSimVec3, direction: ZhudaSimVec3, reach = 0.1): number {
950
+ this.alive();
951
+ if (!finite3(origin) || !finite3(direction))
952
+ throw Error('Invalid ray: an origin and a direction of three finite numbers');
953
+ if (!(Number.isFinite(reach) && reach > 0)) throw Error('Invalid reach: metres, more than 0');
954
+ const d = unit(direction, 'direction');
955
+ const p = this.positions;
956
+ // The nearest layer of cloth the ray passes within reach of...
957
+ let front = Infinity;
958
+ for (let i = 0; i < this.count; i++) {
959
+ const x = p[i * 3] - origin[0],
960
+ y = p[i * 3 + 1] - origin[1],
961
+ z = p[i * 3 + 2] - origin[2];
962
+ const along = x * d[0] + y * d[1] + z * d[2];
963
+ if (along < 0 || along >= front) continue;
964
+ if (x * x + y * y + z * z - along * along <= reach * reach) front = along;
965
+ }
966
+ if (front === Infinity) return -1;
967
+ // ...and of that layer, the particle the ray passes closest to.
968
+ let best = -1,
969
+ bestOff = Infinity;
970
+ for (let i = 0; i < this.count; i++) {
971
+ const x = p[i * 3] - origin[0],
972
+ y = p[i * 3 + 1] - origin[1],
973
+ z = p[i * 3 + 2] - origin[2];
974
+ const along = x * d[0] + y * d[1] + z * d[2];
975
+ if (along < 0 || along > front + reach) continue;
976
+ const off = x * x + y * y + z * z - along * along;
977
+ if (off <= reach * reach && off < bestOff) {
978
+ best = i;
979
+ bestOff = off;
980
+ }
981
+ }
982
+ return best;
983
+ }
984
+
985
+ /** Adds a body the cloth is kept out of; returns a number to move or remove it by. */
986
+ addCollider(collider: ZhudaCollider): number {
987
+ this.alive();
988
+ const issues = validateZhudaCollider(collider);
989
+ if (issues.length) throw Error(`Invalid collider: ${issues.join('; ')}`);
990
+ const id = this.nextBody++;
991
+ this.bodies.set(id, collider);
992
+ return id;
993
+ }
994
+ /** Puts another body in a body's place (it moved, it changed shape). */
995
+ setCollider(id: number, collider: ZhudaCollider) {
996
+ this.alive();
997
+ if (!this.bodies.has(id)) throw Error(`No collider ${id}`);
998
+ const issues = validateZhudaCollider(collider);
999
+ if (issues.length) throw Error(`Invalid collider: ${issues.join('; ')}`);
1000
+ this.bodies.set(id, collider);
1001
+ }
1002
+ removeCollider(id: number) {
1003
+ this.alive();
1004
+ if (!this.bodies.delete(id)) throw Error(`No collider ${id}`);
1005
+ this.tables.delete(id);
1006
+ }
1007
+ get colliders(): ReadonlyMap<number, ZhudaCollider> {
1008
+ return this.bodies;
1009
+ }
1010
+
1011
+ /**
1012
+ * Takes a delta of the clock, in seconds, and runs the whole steps it
1013
+ * holds with what was left over before. Zero changes nothing. Returns
1014
+ * how many steps ran and how many were dropped for being over the cap.
1015
+ */
1016
+ advance(deltaSeconds: number): { steps: number; dropped: number } {
1017
+ this.alive();
1018
+ const before = this.clock.dropped;
1019
+ const steps = this.clock.take(deltaSeconds);
1020
+ for (let i = 0; i < steps; i++) this.stepOnce();
1021
+ return { steps, dropped: this.clock.dropped - before };
1022
+ }
1023
+
1024
+ /**
1025
+ * Carries the whole cloth somewhere else as it is: every particle, pin
1026
+ * and hand moved by the same amount, its motion kept. For when what it
1027
+ * hangs from is put elsewhere at once (a character set down at another
1028
+ * place): pins moved far in one frame would throw the cloth after them.
1029
+ * The bodies are their owner's to move.
1030
+ */
1031
+ shift(x: number, y: number, z: number) {
1032
+ this.alive();
1033
+ if (![x, y, z].every(Number.isFinite)) throw Error('Invalid shift: three finite numbers');
1034
+ const p = this.positions,
1035
+ q = this.previous;
1036
+ for (let k = 0; k < p.length; k += 3) {
1037
+ p[k] += x;
1038
+ p[k + 1] += y;
1039
+ p[k + 2] += z;
1040
+ q[k] += x;
1041
+ q[k + 1] += y;
1042
+ q[k + 2] += z;
1043
+ }
1044
+ for (const hand of this.hands.values()) {
1045
+ hand.at[0] += x;
1046
+ hand.at[1] += y;
1047
+ hand.at[2] += z;
1048
+ hand.to[0] += x;
1049
+ hand.to[1] += y;
1050
+ hand.to[2] += z;
1051
+ }
1052
+ }
1053
+
1054
+ /** Back to the shape it was made in, at rest; pins, hands and bodies stay. */
1055
+ reset() {
1056
+ this.alive();
1057
+ for (let i = 0; i < this.count; i++)
1058
+ if (this.inverseMass[i] !== 0) {
1059
+ this.positions.set(this.shape.particles.subarray(i * 3, i * 3 + 3), i * 3);
1060
+ this.previous.set(this.shape.particles.subarray(i * 3, i * 3 + 3), i * 3);
1061
+ }
1062
+ for (const [index, hand] of this.hands) {
1063
+ hand.at.set(this.positions.subarray(index * 3, index * 3 + 3));
1064
+ hand.to.set(hand.at);
1065
+ }
1066
+ this.clock.reset();
1067
+ this.sweeps = 0;
1068
+ }
1069
+
1070
+ private stepOnce() {
1071
+ const o = this.options,
1072
+ dt = o.step,
1073
+ p = this.positions,
1074
+ q = this.previous,
1075
+ w = this.inverseMass,
1076
+ n = this.count;
1077
+ const keep = Math.exp(-o.drag * dt),
1078
+ limit = o.maxSpeed * dt,
1079
+ gx = o.gravity[0] * dt * dt,
1080
+ gy = o.gravity[1] * dt * dt,
1081
+ gz = o.gravity[2] * dt * dt;
1082
+ // The air: each triangle is pushed along its own normal by the air that meets it.
1083
+ const air = this.air;
1084
+ if (o.windDrag > 0) {
1085
+ air.fill(0);
1086
+ const t = this.triangles;
1087
+ for (let i = 0; i < t.length; i += 3) {
1088
+ const a = t[i] * 3,
1089
+ b = t[i + 1] * 3,
1090
+ c = t[i + 2] * 3;
1091
+ const ux = p[b] - p[a],
1092
+ uy = p[b + 1] - p[a + 1],
1093
+ uz = p[b + 2] - p[a + 2],
1094
+ vx = p[c] - p[a],
1095
+ vy = p[c + 1] - p[a + 1],
1096
+ vz = p[c + 2] - p[a + 2];
1097
+ let nx = uy * vz - uz * vy,
1098
+ ny = uz * vx - ux * vz,
1099
+ nz = ux * vy - uy * vx;
1100
+ const size = Math.sqrt(nx * nx + ny * ny + nz * nz);
1101
+ if (!(size > 1e-12)) continue;
1102
+ nx /= size;
1103
+ ny /= size;
1104
+ nz /= size;
1105
+ // The air's velocity as the triangle sees it: the wind less its own motion.
1106
+ const rx = o.wind[0] - (p[a] - q[a] + p[b] - q[b] + p[c] - q[c]) / (3 * dt),
1107
+ ry =
1108
+ o.wind[1] -
1109
+ (p[a + 1] - q[a + 1] + p[b + 1] - q[b + 1] + p[c + 1] - q[c + 1]) / (3 * dt),
1110
+ rz =
1111
+ o.wind[2] -
1112
+ (p[a + 2] - q[a + 2] + p[b + 2] - q[b + 2] + p[c + 2] - q[c + 2]) / (3 * dt);
1113
+ const push = nx * rx + ny * ry + nz * rz,
1114
+ px = nx * push,
1115
+ py = ny * push,
1116
+ pz = nz * push;
1117
+ air[a] += px;
1118
+ air[a + 1] += py;
1119
+ air[a + 2] += pz;
1120
+ air[b] += px;
1121
+ air[b + 1] += py;
1122
+ air[b + 2] += pz;
1123
+ air[c] += px;
1124
+ air[c + 1] += py;
1125
+ air[c + 2] += pz;
1126
+ }
1127
+ }
1128
+ const blow = o.windDrag * dt * dt;
1129
+ for (let i = 0; i < n; i++) {
1130
+ if (w[i] === 0) continue;
1131
+ const k = i * 3;
1132
+ let vx = (p[k] - q[k]) * keep + gx,
1133
+ vy = (p[k + 1] - q[k + 1]) * keep + gy,
1134
+ vz = (p[k + 2] - q[k + 2]) * keep + gz;
1135
+ if (blow > 0 && this.facing[i]) {
1136
+ const share = blow / this.facing[i];
1137
+ vx += air[k] * share;
1138
+ vy += air[k + 1] * share;
1139
+ vz += air[k + 2] * share;
1140
+ }
1141
+ const speed = Math.sqrt(vx * vx + vy * vy + vz * vz);
1142
+ if (speed > limit) {
1143
+ const slow = limit / speed;
1144
+ vx *= slow;
1145
+ vy *= slow;
1146
+ vz *= slow;
1147
+ }
1148
+ q[k] = p[k];
1149
+ q[k + 1] = p[k + 1];
1150
+ q[k + 2] = p[k + 2];
1151
+ p[k] += vx;
1152
+ p[k + 1] += vy;
1153
+ p[k + 2] += vz;
1154
+ }
1155
+ // Each hand goes toward where it wants to be, no faster than it may, and
1156
+ // no farther ahead of the particle it holds than its slack.
1157
+ const reach = o.grabSpeed * dt;
1158
+ for (const hand of this.hands.values()) {
1159
+ const dx = hand.to[0] - hand.at[0],
1160
+ dy = hand.to[1] - hand.at[1],
1161
+ dz = hand.to[2] - hand.at[2];
1162
+ const far = Math.sqrt(dx * dx + dy * dy + dz * dz);
1163
+ const share = far > reach ? reach / far : 1;
1164
+ hand.at[0] += dx * share;
1165
+ hand.at[1] += dy * share;
1166
+ hand.at[2] += dz * share;
1167
+ }
1168
+ this.leashHands(false);
1169
+ const { a, b, rest, kind } = this.shape.constraints,
1170
+ lambda = this.lambda,
1171
+ alpha = this.alpha;
1172
+ lambda.fill(0);
1173
+ this.touched.fill(0);
1174
+ this.pressed.fill(0);
1175
+ this.prepareTables();
1176
+ const tethered = o.tether !== false;
1177
+ if (tethered && this.tethersStale) this.tether();
1178
+ const anchor = this.anchor,
1179
+ leash = this.leash;
1180
+ for (let round = 0; round < o.rounds; round++) {
1181
+ // The hands first, the weave after: where the two disagree the cloth wins and the hand lags.
1182
+ for (const [index, hand] of this.hands) {
1183
+ const k = index * 3;
1184
+ p[k] = hand.at[0];
1185
+ p[k + 1] = hand.at[1];
1186
+ p[k + 2] = hand.at[2];
1187
+ }
1188
+ // Over the constraints one way, then the other: a pull is passed on both ways alike.
1189
+ const backwards = (this.sweeps++ & 1) === 1,
1190
+ total = a.length;
1191
+ for (let at = 0; at < total; at++) {
1192
+ const i = backwards ? total - 1 - at : at;
1193
+ const ka = a[i] * 3,
1194
+ kb = b[i] * 3,
1195
+ wa = w[a[i]],
1196
+ wb = w[b[i]];
1197
+ if (wa + wb === 0) continue;
1198
+ const dx = p[ka] - p[kb],
1199
+ dy = p[ka + 1] - p[kb + 1],
1200
+ dz = p[ka + 2] - p[kb + 2];
1201
+ const far = Math.sqrt(dx * dx + dy * dy + dz * dz);
1202
+ if (!(far > 1e-12)) continue;
1203
+ let change: number;
1204
+ if (kind[i] === ZHUDA_CLOTH_SPAN) {
1205
+ // Nearer is free; no farther than the thread between is long.
1206
+ const most = rest[i] * SPAN_SLACK;
1207
+ if (far <= most) continue;
1208
+ change = (most - far) / (wa + wb);
1209
+ } else {
1210
+ change = (rest[i] - far - alpha[i] * lambda[i]) / (wa + wb + alpha[i]);
1211
+ lambda[i] += change;
1212
+ }
1213
+ const sx = (dx / far) * change,
1214
+ sy = (dy / far) * change,
1215
+ sz = (dz / far) * change;
1216
+ p[ka] += sx * wa;
1217
+ p[ka + 1] += sy * wa;
1218
+ p[ka + 2] += sz * wa;
1219
+ p[kb] -= sx * wb;
1220
+ p[kb + 1] -= sy * wb;
1221
+ p[kb + 2] -= sz * wb;
1222
+ }
1223
+ // No particle farther from the pin it hangs from than the cloth between them is long.
1224
+ if (tethered)
1225
+ for (let i = 0; i < n; i++) {
1226
+ const pin = anchor[i];
1227
+ if (pin < 0) continue;
1228
+ const k = i * 3,
1229
+ kp = pin * 3;
1230
+ const dx = p[k] - p[kp],
1231
+ dy = p[k + 1] - p[kp + 1],
1232
+ dz = p[k + 2] - p[kp + 2];
1233
+ const far = Math.sqrt(dx * dx + dy * dy + dz * dz);
1234
+ if (far <= leash[i]) continue;
1235
+ const back = leash[i] / far;
1236
+ p[k] = p[kp] + dx * back;
1237
+ p[k + 1] = p[kp + 1] + dy * back;
1238
+ p[k + 2] = p[kp + 2] + dz * back;
1239
+ }
1240
+ // (After the last round the bodies are asked below.)
1241
+ if (round < o.rounds - 1) this.pushOut();
1242
+ }
1243
+ if (this.bodies.size) {
1244
+ // Where each particle touches a body, and which way that body faces there.
1245
+ this.pushOut();
1246
+ // Where a particle touches a body it grips it: how hard it was pressed in
1247
+ // (how far the body pushed it back this step) times the friction is how
1248
+ // much of its sliding the body takes away. A particle pressed harder than
1249
+ // it slides stays put, so a cloth lies on a slope less steep than its
1250
+ // friction; one that slides more keeps the rest. Its motion into the
1251
+ // body is gone.
1252
+ const grip = o.friction,
1253
+ normal = this.normal,
1254
+ pressed = this.pressed;
1255
+ for (let i = 0, k = 0; i < n; i++, k += 3) {
1256
+ if (!this.touched[i]) continue;
1257
+ const vx = p[k] - q[k],
1258
+ vy = p[k + 1] - q[k + 1],
1259
+ vz = p[k + 2] - q[k + 2];
1260
+ const into = vx * normal[k] + vy * normal[k + 1] + vz * normal[k + 2];
1261
+ let tx = vx - normal[k] * into,
1262
+ ty = vy - normal[k + 1] * into,
1263
+ tz = vz - normal[k + 2] * into;
1264
+ const slide = Math.sqrt(tx * tx + ty * ty + tz * tz);
1265
+ if (slide > 1e-12 && grip > 0) {
1266
+ const held = grip * pressed[i],
1267
+ taken = held >= slide ? 1 : held / slide;
1268
+ p[k] -= tx * taken;
1269
+ p[k + 1] -= ty * taken;
1270
+ p[k + 2] -= tz * taken;
1271
+ tx *= 1 - taken;
1272
+ ty *= 1 - taken;
1273
+ tz *= 1 - taken;
1274
+ }
1275
+ const away = into > 0 ? into : 0;
1276
+ q[k] = p[k] - (tx + normal[k] * away);
1277
+ q[k + 1] = p[k + 1] - (ty + normal[k + 1] * away);
1278
+ q[k + 2] = p[k + 2] - (tz + normal[k + 2] * away);
1279
+ }
1280
+ // The bodies have the last word: held back along a curved body, or pushed
1281
+ // out of one body into another, a particle may be inside one again. Out of
1282
+ // one body is out of it for good; where there are several they are asked
1283
+ // again, a few times.
1284
+ if (this.pushOut() && this.bodies.size > 1)
1285
+ for (let pass = 0; pass < 3 && this.pushOut(); pass++);
1286
+ }
1287
+ // Where the cloth pulled a held particle back, its hand comes back with it.
1288
+ this.leashHands(true);
1289
+ }
1290
+
1291
+ /**
1292
+ * No hand farther from the particle it holds than its lead. After a
1293
+ * step the lead is set for the next one: halved where the threads at the
1294
+ * particle are stretched by more than a twentieth (the cloth is taut:
1295
+ * pulling harder would only stretch the weave at the hand), grown again
1296
+ * where they are not. So a hand moves a slack cloth briskly and waits at
1297
+ * a taut one.
1298
+ */
1299
+ private leashHands(after: boolean) {
1300
+ const p = this.positions,
1301
+ { a, b, rest } = this.shape.constraints;
1302
+ for (const [index, hand] of this.hands) {
1303
+ const k = index * 3;
1304
+ if (after) {
1305
+ let stretched = 0;
1306
+ for (const i of hand.threads) {
1307
+ const ka = a[i] * 3,
1308
+ kb = b[i] * 3;
1309
+ const far = Math.hypot(p[ka] - p[kb], p[ka + 1] - p[kb + 1], p[ka + 2] - p[kb + 2]);
1310
+ stretched = Math.max(stretched, far / rest[i] - 1);
1311
+ }
1312
+ hand.lead =
1313
+ stretched > 0.05
1314
+ ? Math.max(hand.lead * 0.5, hand.slack / 64)
1315
+ : Math.min(hand.lead * 1.5, hand.slack);
1316
+ }
1317
+ const dx = hand.at[0] - p[k],
1318
+ dy = hand.at[1] - p[k + 1],
1319
+ dz = hand.at[2] - p[k + 2];
1320
+ const far = Math.sqrt(dx * dx + dy * dy + dz * dz);
1321
+ if (far > hand.lead) {
1322
+ const back = hand.lead / far;
1323
+ hand.at[0] = p[k] + dx * back;
1324
+ hand.at[1] = p[k + 1] + dy * back;
1325
+ hand.at[2] = p[k + 2] + dz * back;
1326
+ }
1327
+ }
1328
+ }
1329
+
1330
+ /**
1331
+ * Pushes every free particle out of every body; says whether any was
1332
+ * moved. A floor, a ball and a rod are worked out here, over all the
1333
+ * particles at once, by the arithmetic `zhudaColliderDepth` does a point
1334
+ * at a time (the two are held to each other by `countInside`, which asks
1335
+ * it); a table is asked there.
1336
+ */
1337
+ private pushOut(): boolean {
1338
+ if (!this.bodies.size) return false;
1339
+ const p = this.positions,
1340
+ w = this.inverseMass,
1341
+ normal = this.normal,
1342
+ touched = this.touched,
1343
+ pressed = this.pressed,
1344
+ n = this.count,
1345
+ margin = this.options.thickness;
1346
+ let moved = false;
1347
+ for (const [id, body] of this.bodies) {
1348
+ if (body.type === 'plane') {
1349
+ const nx = body.normal[0],
1350
+ ny = body.normal[1],
1351
+ nz = body.normal[2],
1352
+ level = body.point[0] * nx + body.point[1] * ny + body.point[2] * nz + margin;
1353
+ for (let i = 0, k = 0; i < n; i++, k += 3) {
1354
+ if (w[i] === 0) continue;
1355
+ const depth = level - (p[k] * nx + p[k + 1] * ny + p[k + 2] * nz);
1356
+ if (depth <= ON_THE_SKIN) continue;
1357
+ p[k] += nx * depth;
1358
+ p[k + 1] += ny * depth;
1359
+ p[k + 2] += nz * depth;
1360
+ normal[k] = nx;
1361
+ normal[k + 1] = ny;
1362
+ normal[k + 2] = nz;
1363
+ touched[i] = 1;
1364
+ pressed[i] += depth;
1365
+ moved = true;
1366
+ }
1367
+ } else if (body.type === 'sphere' || body.type === 'capsule') {
1368
+ const rod = body.type === 'capsule',
1369
+ from = rod ? body.a : body.center,
1370
+ ax = from[0],
1371
+ ay = from[1],
1372
+ az = from[2],
1373
+ abx = rod ? body.b[0] - ax : 0,
1374
+ aby = rod ? body.b[1] - ay : 0,
1375
+ abz = rod ? body.b[2] - az : 0,
1376
+ span = abx * abx + aby * aby + abz * abz,
1377
+ reach = body.radius + margin,
1378
+ reach2 = reach * reach;
1379
+ for (let i = 0, k = 0; i < n; i++, k += 3) {
1380
+ if (w[i] === 0) continue;
1381
+ let dx = p[k] - ax,
1382
+ dy = p[k + 1] - ay,
1383
+ dz = p[k + 2] - az;
1384
+ let cx = ax,
1385
+ cy = ay,
1386
+ cz = az;
1387
+ if (span > 1e-18) {
1388
+ let t = (dx * abx + dy * aby + dz * abz) / span;
1389
+ t = t < 0 ? 0 : t > 1 ? 1 : t;
1390
+ cx += abx * t;
1391
+ cy += aby * t;
1392
+ cz += abz * t;
1393
+ dx = p[k] - cx;
1394
+ dy = p[k + 1] - cy;
1395
+ dz = p[k + 2] - cz;
1396
+ }
1397
+ const far2 = dx * dx + dy * dy + dz * dz;
1398
+ if (far2 >= reach2) continue;
1399
+ const far = Math.sqrt(far2);
1400
+ if (reach - far <= ON_THE_SKIN) continue;
1401
+ // A point at the very middle has no way out of its own: up.
1402
+ const ux = far > 1e-9 ? dx / far : 0,
1403
+ uy = far > 1e-9 ? dy / far : 1,
1404
+ uz = far > 1e-9 ? dz / far : 0;
1405
+ p[k] = cx + ux * reach;
1406
+ p[k + 1] = cy + uy * reach;
1407
+ p[k + 2] = cz + uz * reach;
1408
+ normal[k] = ux;
1409
+ normal[k + 1] = uy;
1410
+ normal[k + 2] = uz;
1411
+ touched[i] = 1;
1412
+ pressed[i] += reach - far;
1413
+ moved = true;
1414
+ }
1415
+ } else {
1416
+ const out = this.scratch,
1417
+ prepared = this.tables.get(id);
1418
+ for (let i = 0, k = 0; i < n; i++, k += 3) {
1419
+ if (w[i] === 0) continue;
1420
+ const depth = zhudaColliderDepth(body, margin, p[k], p[k + 1], p[k + 2], out, prepared);
1421
+ if (depth <= ON_THE_SKIN) continue;
1422
+ p[k] = out[0];
1423
+ p[k + 1] = out[1];
1424
+ p[k + 2] = out[2];
1425
+ normal[k] = out[3];
1426
+ normal[k + 1] = out[4];
1427
+ normal[k + 2] = out[5];
1428
+ touched[i] = 1;
1429
+ pressed[i] += depth;
1430
+ moved = true;
1431
+ }
1432
+ }
1433
+ }
1434
+ return moved;
1435
+ }
1436
+
1437
+ /** Each table worked out as it is now (a body may have been moved or changed since the last step). */
1438
+ private prepareTables() {
1439
+ for (const [id, body] of this.bodies)
1440
+ if (body.type === 'table') this.tables.set(id, prepareZhudaTable(body, this.tables.get(id)));
1441
+ }
1442
+
1443
+ /**
1444
+ * How many particles are inside a body by more than `tolerance` metres
1445
+ * (default a tenth of a millimetre; single precision moves a particle put
1446
+ * on a surface by less). `margin` is how far outside the body the surface
1447
+ * counted from is: the cloth's thickness by default, 0 for the body
1448
+ * itself. Pins are counted.
1449
+ */
1450
+ countInside(tolerance = 1e-4, margin = this.options.thickness): number {
1451
+ this.alive();
1452
+ this.prepareTables();
1453
+ const p = this.positions;
1454
+ let inside = 0;
1455
+ for (let i = 0; i < this.count; i++)
1456
+ for (const [id, body] of this.bodies)
1457
+ if (
1458
+ zhudaColliderDepth(
1459
+ body,
1460
+ margin,
1461
+ p[i * 3],
1462
+ p[i * 3 + 1],
1463
+ p[i * 3 + 2],
1464
+ undefined,
1465
+ this.tables.get(id),
1466
+ ) > tolerance
1467
+ ) {
1468
+ inside++;
1469
+ break;
1470
+ }
1471
+ return inside;
1472
+ }
1473
+
1474
+ /**
1475
+ * Fills `strain` with how far each particle's weave is stretched (the
1476
+ * mean over its structural constraints of how much longer than its rest
1477
+ * length each is, as a share; 0 where it is slack) and returns the most
1478
+ * any one constraint is stretched.
1479
+ */
1480
+ measureStrain(): number {
1481
+ this.alive();
1482
+ const { a, b, rest, kind } = this.shape.constraints,
1483
+ p = this.positions,
1484
+ strain = this.strain;
1485
+ strain.fill(0);
1486
+ let most = 0;
1487
+ for (let i = 0; i < a.length; i++) {
1488
+ if (kind[i] !== ZHUDA_CLOTH_STRUCTURAL) continue;
1489
+ const ka = a[i] * 3,
1490
+ kb = b[i] * 3;
1491
+ const far = Math.hypot(p[ka] - p[kb], p[ka + 1] - p[kb + 1], p[ka + 2] - p[kb + 2]);
1492
+ const longer = Math.max(0, far / rest[i] - 1);
1493
+ strain[a[i]] += longer;
1494
+ strain[b[i]] += longer;
1495
+ if (longer > most) most = longer;
1496
+ }
1497
+ for (let i = 0; i < this.count; i++)
1498
+ if (this.structuralCount[i]) strain[i] /= this.structuralCount[i];
1499
+ return most;
1500
+ }
1501
+
1502
+ inspect(): ZhudaClothReport {
1503
+ this.alive();
1504
+ const p = this.positions,
1505
+ q = this.previous,
1506
+ dt = this.options.step;
1507
+ let energy = 0,
1508
+ pinned = 0;
1509
+ for (let i = 0; i < this.count; i++) {
1510
+ if (this.inverseMass[i] === 0) pinned++;
1511
+ const k = i * 3;
1512
+ const vx = (p[k] - q[k]) / dt,
1513
+ vy = (p[k + 1] - q[k + 1]) / dt,
1514
+ vz = (p[k + 2] - q[k + 2]) / dt;
1515
+ energy += 0.5 * (vx * vx + vy * vy + vz * vz);
1516
+ }
1517
+ return {
1518
+ particles: this.count,
1519
+ constraints: this.shape.constraints.a.length,
1520
+ triangles: this.triangles.length / 3,
1521
+ colliders: this.bodies.size,
1522
+ pinned,
1523
+ grabbed: this.hands.size,
1524
+ steps: this.clock.steps,
1525
+ dropped: this.clock.dropped,
1526
+ pendingSeconds: this.clock.pendingSeconds,
1527
+ inside: this.countInside(),
1528
+ maxStretch: this.measureStrain(),
1529
+ kineticEnergy: energy,
1530
+ safeRadius: this.options.maxSpeed * dt,
1531
+ };
1532
+ }
1533
+
1534
+ /** Lets go of everything; the cloth takes no more calls. */
1535
+ dispose() {
1536
+ if (this.disposed) return;
1537
+ this.disposed = true;
1538
+ this.bodies.clear();
1539
+ this.hands.clear();
1540
+ }
1541
+ }