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,1871 @@
1
+ /**
2
+ * Procedural geometry kit.
3
+ *
4
+ * Builders are pure data: they return the engine's existing `MeshData`
5
+ * (POSITION/NORMAL/UV + 16-bit indices) and never touch Laya. Pass the result
6
+ * to `ZhudaDynamicMesh` to draw it.
7
+ *
8
+ * Conventions
9
+ * - Units are metres. UVs are metres too, so a material that repeats every
10
+ * `tileMeters` keeps real-world texel density on every part.
11
+ * - Triangles use the same front-face winding as the terrain meshes in this
12
+ * engine: cross(v1 - v0, v2 - v0) points away from the surface normal.
13
+ */
14
+ import type { MeshData } from '../runtime3d/Surfaces.ts';
15
+
16
+ export type ZhudaVec3 = [number, number, number];
17
+
18
+ const positive = (value: unknown, label: string): number => {
19
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0)
20
+ throw Error(`${label} must be a positive number`);
21
+ return value;
22
+ };
23
+ const vec = (value: unknown, label: string): ZhudaVec3 => {
24
+ if (!Array.isArray(value) || value.length !== 3 || !value.every((n) => Number.isFinite(n)))
25
+ throw Error(`${label} must be three finite numbers`);
26
+ return value as ZhudaVec3;
27
+ };
28
+ const sub = (a: ZhudaVec3, b: ZhudaVec3): ZhudaVec3 => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
29
+ const cross = (a: ZhudaVec3, b: ZhudaVec3): ZhudaVec3 => [
30
+ a[1] * b[2] - a[2] * b[1],
31
+ a[2] * b[0] - a[0] * b[2],
32
+ a[0] * b[1] - a[1] * b[0],
33
+ ];
34
+ const dot = (a: ZhudaVec3, b: ZhudaVec3) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
35
+ const normalize = (a: ZhudaVec3): ZhudaVec3 => {
36
+ const length = Math.hypot(a[0], a[1], a[2]);
37
+ if (!(length > 1e-12)) throw Error('Degenerate direction in procedural geometry');
38
+ return [a[0] / length, a[1] / length, a[2] / length];
39
+ };
40
+
41
+ class MeshWriter {
42
+ private readonly positions: number[] = [];
43
+ private readonly normals: number[] = [];
44
+ private readonly uvs: number[] = [];
45
+ private readonly indices: number[] = [];
46
+
47
+ vertex(p: ZhudaVec3, n: ZhudaVec3, u: number, v: number): number {
48
+ this.positions.push(p[0], p[1], p[2]);
49
+ this.normals.push(n[0], n[1], n[2]);
50
+ this.uvs.push(u, v);
51
+ return this.positions.length / 3 - 1;
52
+ }
53
+ point(index: number): ZhudaVec3 {
54
+ return [
55
+ this.positions[index * 3],
56
+ this.positions[index * 3 + 1],
57
+ this.positions[index * 3 + 2],
58
+ ];
59
+ }
60
+ /** Adds one triangle, ordering it for the engine winding relative to `outward`. */
61
+ triangle(a: number, b: number, c: number, outward: ZhudaVec3): void {
62
+ const facing = dot(
63
+ cross(sub(this.point(b), this.point(a)), sub(this.point(c), this.point(a))),
64
+ outward,
65
+ );
66
+ if (facing > 0) this.indices.push(a, c, b);
67
+ else this.indices.push(a, b, c);
68
+ }
69
+ /** Flat quad with one shared normal; corners may be given in either rotation. */
70
+ quad(corners: [ZhudaVec3, ZhudaVec3, ZhudaVec3, ZhudaVec3], n: ZhudaVec3, uv: number[]): void {
71
+ const base = corners.map((p, i) => this.vertex(p, n, uv[i * 2], uv[i * 2 + 1]));
72
+ this.triangle(base[0], base[1], base[2], n);
73
+ this.triangle(base[0], base[2], base[3], n);
74
+ }
75
+ finish(): MeshData {
76
+ const count = this.positions.length / 3;
77
+ if (count > 65535) throw Error('Procedural mesh exceeds 65535 vertices; split it into parts');
78
+ return {
79
+ positions: new Float32Array(this.positions),
80
+ normals: new Float32Array(this.normals),
81
+ uvs: new Float32Array(this.uvs),
82
+ indices: new Uint16Array(this.indices),
83
+ };
84
+ }
85
+ }
86
+
87
+ const FACES: Array<{ n: ZhudaVec3; u: ZhudaVec3; v: ZhudaVec3 }> = [
88
+ { n: [1, 0, 0], u: [0, 0, -1], v: [0, 1, 0] },
89
+ { n: [-1, 0, 0], u: [0, 0, 1], v: [0, 1, 0] },
90
+ { n: [0, 1, 0], u: [1, 0, 0], v: [0, 0, -1] },
91
+ { n: [0, -1, 0], u: [1, 0, 0], v: [0, 0, 1] },
92
+ { n: [0, 0, 1], u: [1, 0, 0], v: [0, 1, 0] },
93
+ { n: [0, 0, -1], u: [-1, 0, 0], v: [0, 1, 0] },
94
+ ];
95
+ const along = (axis: ZhudaVec3, half: ZhudaVec3) =>
96
+ Math.abs(axis[0]) * half[0] + Math.abs(axis[1]) * half[1] + Math.abs(axis[2]) * half[2];
97
+
98
+ export interface ZhudaBoxOptions {
99
+ width: number;
100
+ height: number;
101
+ depth: number;
102
+ /** 'center' (default) or 'bottom': the origin sits on the middle of the underside. */
103
+ origin?: 'center' | 'bottom';
104
+ /** Chamfer size in metres; 0 or omitted gives sharp edges. */
105
+ bevel?: number;
106
+ }
107
+
108
+ /** Axis-aligned box, optionally chamfered. X = width, Y = height, Z = depth. */
109
+ export function createZhudaBoxMesh(options: ZhudaBoxOptions): MeshData {
110
+ const half: ZhudaVec3 = [
111
+ positive(options.width, 'width') / 2,
112
+ positive(options.height, 'height') / 2,
113
+ positive(options.depth, 'depth') / 2,
114
+ ],
115
+ bevel = options.bevel ?? 0,
116
+ lift = options.origin === 'bottom' ? half[1] : 0;
117
+ if (options.origin !== undefined && options.origin !== 'center' && options.origin !== 'bottom')
118
+ throw Error('origin must be center or bottom');
119
+ if (!Number.isFinite(bevel) || bevel < 0 || bevel >= Math.min(half[0], half[1], half[2]))
120
+ throw Error('bevel must be at least 0 and smaller than half the shortest side');
121
+ const writer = new MeshWriter();
122
+ const place = (x: number, y: number, z: number): ZhudaVec3 => [x, y + lift, z];
123
+ for (const face of FACES) {
124
+ const hn = along(face.n, half),
125
+ hu = along(face.u, half) - bevel,
126
+ hv = along(face.v, half) - bevel;
127
+ const corner = (su: number, sv: number) =>
128
+ place(
129
+ face.n[0] * hn + face.u[0] * hu * su + face.v[0] * hv * sv,
130
+ face.n[1] * hn + face.u[1] * hu * su + face.v[1] * hv * sv,
131
+ face.n[2] * hn + face.u[2] * hu * su + face.v[2] * hv * sv,
132
+ );
133
+ writer.quad([corner(-1, -1), corner(1, -1), corner(1, 1), corner(-1, 1)], face.n, [
134
+ 0,
135
+ 0,
136
+ hu * 2,
137
+ 0,
138
+ hu * 2,
139
+ hv * 2,
140
+ 0,
141
+ hv * 2,
142
+ ]);
143
+ }
144
+ if (bevel > 0) {
145
+ const width = bevel * Math.SQRT2;
146
+ // Twelve edge chamfers: the edge runs along axis c between faces a and b.
147
+ for (let c = 0; c < 3; c++) {
148
+ const a = (c + 1) % 3,
149
+ b = (c + 2) % 3;
150
+ for (const sa of [-1, 1])
151
+ for (const sb of [-1, 1]) {
152
+ const point = (onA: boolean, sc: number) => {
153
+ const p: ZhudaVec3 = [0, 0, 0];
154
+ p[a] = sa * (onA ? half[a] : half[a] - bevel);
155
+ p[b] = sb * (onA ? half[b] - bevel : half[b]);
156
+ p[c] = sc * (half[c] - bevel);
157
+ return place(p[0], p[1], p[2]);
158
+ };
159
+ const n: ZhudaVec3 = [0, 0, 0];
160
+ n[a] = sa * Math.SQRT1_2;
161
+ n[b] = sb * Math.SQRT1_2;
162
+ const length = (half[c] - bevel) * 2;
163
+ writer.quad([point(true, -1), point(true, 1), point(false, 1), point(false, -1)], n, [
164
+ 0,
165
+ 0,
166
+ length,
167
+ 0,
168
+ length,
169
+ width,
170
+ 0,
171
+ width,
172
+ ]);
173
+ }
174
+ }
175
+ // Eight corner triangles close the chamfered edges.
176
+ for (const sx of [-1, 1])
177
+ for (const sy of [-1, 1])
178
+ for (const sz of [-1, 1]) {
179
+ const n = normalize([sx, sy, sz]),
180
+ p0 = place(sx * half[0], sy * (half[1] - bevel), sz * (half[2] - bevel)),
181
+ p1 = place(sx * (half[0] - bevel), sy * half[1], sz * (half[2] - bevel)),
182
+ p2 = place(sx * (half[0] - bevel), sy * (half[1] - bevel), sz * half[2]),
183
+ i0 = writer.vertex(p0, n, 0, 0),
184
+ i1 = writer.vertex(p1, n, width, 0),
185
+ i2 = writer.vertex(p2, n, width / 2, width);
186
+ writer.triangle(i0, i1, i2, n);
187
+ }
188
+ }
189
+ return writer.finish();
190
+ }
191
+
192
+ export interface ZhudaTubeOptions {
193
+ /** Centre line; at least two distinct points. */
194
+ path: ZhudaVec3[];
195
+ radius: number;
196
+ /** Sides around the tube, 3..64 (default 8). */
197
+ radialSegments?: number;
198
+ /** Close both ends with flat caps (default true). */
199
+ caps?: boolean;
200
+ }
201
+
202
+ /** Round tube swept along a polyline with twist-free frames. */
203
+ export function createZhudaTubeMesh(options: ZhudaTubeOptions): MeshData {
204
+ const radius = positive(options.radius, 'radius'),
205
+ sides = options.radialSegments ?? 8,
206
+ caps = options.caps ?? true;
207
+ if (!Number.isInteger(sides) || sides < 3 || sides > 64)
208
+ throw Error('radialSegments must be an integer 3..64');
209
+ if (!Array.isArray(options.path) || options.path.length < 2 || options.path.length > 1024)
210
+ throw Error('path needs 2..1024 points');
211
+ const path = options.path.map((p, i) => vec(p, `path[${i}]`));
212
+ const directions = path.slice(1).map((p, i) => normalize(sub(p, path[i])));
213
+ const tangents = path.map((_, i) =>
214
+ i === 0
215
+ ? directions[0]
216
+ : i === path.length - 1
217
+ ? directions[directions.length - 1]
218
+ : normalize([
219
+ directions[i - 1][0] + directions[i][0],
220
+ directions[i - 1][1] + directions[i][1],
221
+ directions[i - 1][2] + directions[i][2],
222
+ ]),
223
+ );
224
+ const seedAxis: ZhudaVec3 = Math.abs(tangents[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];
225
+ let side = normalize(cross(seedAxis, tangents[0]));
226
+ const writer = new MeshWriter();
227
+ const rings: number[][] = [];
228
+ let travelled = 0;
229
+ for (let i = 0; i < path.length; i++) {
230
+ const t = tangents[i];
231
+ if (i > 0) {
232
+ travelled += Math.hypot(...sub(path[i], path[i - 1]));
233
+ const k = dot(side, t);
234
+ side = normalize([side[0] - t[0] * k, side[1] - t[1] * k, side[2] - t[2] * k]);
235
+ }
236
+ const other = cross(t, side);
237
+ const ring: number[] = [];
238
+ for (let s = 0; s <= sides; s++) {
239
+ const angle = (s / sides) * Math.PI * 2,
240
+ cs = Math.cos(angle),
241
+ sn = Math.sin(angle),
242
+ n: ZhudaVec3 = [
243
+ side[0] * cs + other[0] * sn,
244
+ side[1] * cs + other[1] * sn,
245
+ side[2] * cs + other[2] * sn,
246
+ ];
247
+ ring.push(
248
+ writer.vertex(
249
+ [path[i][0] + n[0] * radius, path[i][1] + n[1] * radius, path[i][2] + n[2] * radius],
250
+ n,
251
+ (s / sides) * Math.PI * 2 * radius,
252
+ travelled,
253
+ ),
254
+ );
255
+ }
256
+ rings.push(ring);
257
+ }
258
+ for (let i = 0; i < rings.length - 1; i++)
259
+ for (let s = 0; s < sides; s++) {
260
+ // Any vector on the outward side of this strip is enough to orient it.
261
+ const ref = normalize(
262
+ sub(midpoint(writer, rings[i][s], rings[i + 1][s + 1]), midLine(path, i)),
263
+ );
264
+ writer.triangle(rings[i][s], rings[i][s + 1], rings[i + 1][s + 1], ref);
265
+ writer.triangle(rings[i][s], rings[i + 1][s + 1], rings[i + 1][s], ref);
266
+ }
267
+ if (caps)
268
+ for (const end of [0, path.length - 1]) {
269
+ const outward: ZhudaVec3 =
270
+ end === 0 ? [-tangents[0][0], -tangents[0][1], -tangents[0][2]] : tangents[end],
271
+ centre = writer.vertex(path[end], outward, 0, 0),
272
+ rim: number[] = [];
273
+ for (let s = 0; s <= sides; s++) {
274
+ const angle = (s / sides) * Math.PI * 2;
275
+ rim.push(
276
+ writer.vertex(
277
+ writer.point(rings[end][s]),
278
+ outward,
279
+ Math.cos(angle) * radius,
280
+ Math.sin(angle) * radius,
281
+ ),
282
+ );
283
+ }
284
+ for (let s = 0; s < sides; s++) writer.triangle(centre, rim[s], rim[s + 1], outward);
285
+ }
286
+ return writer.finish();
287
+ }
288
+ function midpoint(writer: MeshWriter, a: number, b: number): ZhudaVec3 {
289
+ const p = writer.point(a),
290
+ q = writer.point(b);
291
+ return [(p[0] + q[0]) / 2, (p[1] + q[1]) / 2, (p[2] + q[2]) / 2];
292
+ }
293
+ function midLine(path: ZhudaVec3[], i: number): ZhudaVec3 {
294
+ return [
295
+ (path[i][0] + path[i + 1][0]) / 2,
296
+ (path[i][1] + path[i + 1][1]) / 2,
297
+ (path[i][2] + path[i + 1][2]) / 2,
298
+ ];
299
+ }
300
+
301
+ /** Straight round rod between two points. */
302
+ export function createZhudaRodMesh(
303
+ from: ZhudaVec3,
304
+ to: ZhudaVec3,
305
+ radius: number,
306
+ radialSegments = 8,
307
+ ): MeshData {
308
+ return createZhudaTubeMesh({ path: [vec(from, 'from'), vec(to, 'to')], radius, radialSegments });
309
+ }
310
+
311
+ export interface ZhudaIBeamOptions {
312
+ length: number;
313
+ /** Overall section height (flange to flange). */
314
+ depth: number;
315
+ /** Flange width. */
316
+ width: number;
317
+ flangeThickness: number;
318
+ webThickness: number;
319
+ /** Direction the beam runs along (default 'x'). The section height is Y, or Z for a 'y' beam. */
320
+ axis?: 'x' | 'y' | 'z';
321
+ }
322
+
323
+ /** Structural I-section: two flanges and a web, centred on the origin. */
324
+ export function createZhudaIBeamMesh(options: ZhudaIBeamOptions): MeshData {
325
+ const length = positive(options.length, 'length'),
326
+ depth = positive(options.depth, 'depth'),
327
+ width = positive(options.width, 'width'),
328
+ flange = positive(options.flangeThickness, 'flangeThickness'),
329
+ web = positive(options.webThickness, 'webThickness'),
330
+ axis = options.axis ?? 'x';
331
+ if (flange * 2 >= depth) throw Error('flangeThickness must be less than half the depth');
332
+ if (web > width) throw Error('webThickness cannot exceed the flange width');
333
+ if (axis !== 'x' && axis !== 'y' && axis !== 'z') throw Error('axis must be x, y or z');
334
+ const top = transformZhudaMesh(
335
+ createZhudaBoxMesh({ width: length, height: flange, depth: width }),
336
+ {
337
+ position: [0, depth / 2 - flange / 2, 0],
338
+ },
339
+ ),
340
+ bottom = transformZhudaMesh(
341
+ createZhudaBoxMesh({ width: length, height: flange, depth: width }),
342
+ { position: [0, -depth / 2 + flange / 2, 0] },
343
+ ),
344
+ middle = createZhudaBoxMesh({ width: length, height: depth - flange * 2, depth: web }),
345
+ beam = mergeZhudaMeshes([top, bottom, middle])[0];
346
+ return axis === 'x'
347
+ ? beam
348
+ : transformZhudaMesh(beam, axis === 'z' ? { yawDeg: 90 } : { rollDeg: 90 });
349
+ }
350
+
351
+ export interface ZhudaLatheOptions {
352
+ /**
353
+ * The outline as [radius, height] pairs in metres, 2..256 of them, turned
354
+ * about the vertical axis. List it from the bottom up along the outside; a
355
+ * hollow form (a bowl, a pipe) goes on down the inside. A radius of 0 is on
356
+ * the axis.
357
+ */
358
+ profile: readonly (readonly [number, number])[];
359
+ /** Sides around the axis, 3..96 (default 24). */
360
+ segments?: number;
361
+ /** Close the first and the last ring with a flat disc where it is off the axis (default true). */
362
+ caps?: boolean;
363
+ /** A corner of the outline sharper than this many degrees stays an edge (default 30). */
364
+ creaseDeg?: number;
365
+ }
366
+
367
+ export function validateZhudaLathe(options: ZhudaLatheOptions): string[] {
368
+ const issues: string[] = [];
369
+ const o = options ?? ({} as ZhudaLatheOptions);
370
+ const profile = o.profile as unknown;
371
+ if (!Array.isArray(profile) || profile.length < 2 || profile.length > 256)
372
+ issues.push('profile: use 2..256 [radius, height] pairs');
373
+ else
374
+ profile.forEach((point, i) => {
375
+ if (
376
+ !Array.isArray(point) ||
377
+ point.length !== 2 ||
378
+ !point.every((n) => typeof n === 'number' && Number.isFinite(n) && Math.abs(n) <= 1000)
379
+ )
380
+ issues.push(`profile[${i}]: use [radius, height] in metres`);
381
+ else if (point[0] < 0) issues.push(`profile[${i}]: the radius cannot be negative`);
382
+ else if (i > 0) {
383
+ const before = profile[i - 1];
384
+ if (Array.isArray(before) && before[0] === point[0] && before[1] === point[1])
385
+ issues.push(`profile[${i}]: the same point as the one before it`);
386
+ }
387
+ });
388
+ if (
389
+ o.segments !== undefined &&
390
+ !(Number.isInteger(o.segments) && o.segments >= 3 && o.segments <= 96)
391
+ )
392
+ issues.push('segments: use a whole number 3..96');
393
+ if (o.caps !== undefined && typeof o.caps !== 'boolean') issues.push('caps: true or false');
394
+ if (o.creaseDeg !== undefined && !(o.creaseDeg >= 0 && o.creaseDeg <= 180))
395
+ issues.push('creaseDeg: use 0..180');
396
+ return issues;
397
+ }
398
+
399
+ /** A body of revolution about +Y: bollards, barrels, domes, bowls, columns, cones. */
400
+ export function createZhudaLatheMesh(options: ZhudaLatheOptions): MeshData {
401
+ const issues = validateZhudaLathe(options);
402
+ if (issues.length) throw Error('Invalid lathe: ' + issues.join('; '));
403
+ const profile = options.profile,
404
+ sides = options.segments ?? 24,
405
+ caps = options.caps ?? true,
406
+ crease = Math.cos(((options.creaseDeg ?? 30) * Math.PI) / 180),
407
+ count = profile.length - 1;
408
+ // Outward in the outline's own plane: to the right of the way it is drawn.
409
+ const flat: [number, number][] = [];
410
+ const travelled = [0];
411
+ for (let i = 0; i < count; i++) {
412
+ const dr = profile[i + 1][0] - profile[i][0],
413
+ dh = profile[i + 1][1] - profile[i][1],
414
+ length = Math.hypot(dr, dh);
415
+ flat.push([dh / length, -dr / length]);
416
+ travelled.push(travelled[i] + length);
417
+ }
418
+ // The normal a segment uses at one of its ends: shared with its neighbour unless the corner is sharp.
419
+ const at = (segment: number, neighbour: number): [number, number] => {
420
+ const own = flat[segment],
421
+ other = flat[neighbour];
422
+ if (!other || own[0] * other[0] + own[1] * other[1] < crease) return own;
423
+ const x = own[0] + other[0],
424
+ y = own[1] + other[1],
425
+ length = Math.hypot(x, y);
426
+ return length > 1e-9 ? [x / length, y / length] : own;
427
+ };
428
+ const writer = new MeshWriter();
429
+ const EPS = 1e-9;
430
+ // One scale around the axis for the whole form: UVs then run on from ring to
431
+ // ring, and a normal map keeps one direction across them. Toward the axis
432
+ // the texture is squeezed, as on any cap.
433
+ const widest = Math.max(1e-3, ...profile.map((point) => point[0]));
434
+ for (let i = 0; i < count; i++) {
435
+ const ends = [at(i, i - 1), at(i, i + 1)];
436
+ const rings = [0, 1].map((end) => {
437
+ const [radius, height] = profile[i + end],
438
+ normal = ends[end],
439
+ ring: number[] = [];
440
+ for (let s = 0; s <= sides; s++) {
441
+ const angle = (s / sides) * Math.PI * 2,
442
+ cs = Math.cos(angle),
443
+ sn = Math.sin(angle);
444
+ ring.push(
445
+ writer.vertex(
446
+ [radius * cs, height, radius * sn],
447
+ [normal[0] * cs, normal[1], normal[0] * sn],
448
+ angle * widest,
449
+ travelled[i + end],
450
+ ),
451
+ );
452
+ }
453
+ return ring;
454
+ });
455
+ for (let s = 0; s < sides; s++) {
456
+ const middle = ((s + 0.5) / sides) * Math.PI * 2;
457
+ const outward: ZhudaVec3 = [
458
+ flat[i][0] * Math.cos(middle),
459
+ flat[i][1],
460
+ flat[i][0] * Math.sin(middle),
461
+ ];
462
+ // A ring on the axis is one point: that side of the quad has no triangle.
463
+ if (profile[i][0] > EPS)
464
+ writer.triangle(rings[0][s], rings[0][s + 1], rings[1][s + 1], outward);
465
+ if (profile[i + 1][0] > EPS)
466
+ writer.triangle(rings[0][s], rings[1][s + 1], rings[1][s], outward);
467
+ }
468
+ }
469
+ if (caps)
470
+ for (const [end, up] of [
471
+ [0, -1],
472
+ [count, 1],
473
+ ] as const) {
474
+ const [radius, height] = profile[end];
475
+ if (!(radius > EPS)) continue;
476
+ const outward: ZhudaVec3 = [0, up, 0];
477
+ const centre = writer.vertex([0, height, 0], outward, 0, 0),
478
+ rim: number[] = [];
479
+ for (let s = 0; s <= sides; s++) {
480
+ const angle = (s / sides) * Math.PI * 2,
481
+ x = radius * Math.cos(angle),
482
+ z = radius * Math.sin(angle);
483
+ rim.push(writer.vertex([x, height, z], outward, x, z));
484
+ }
485
+ for (let s = 0; s < sides; s++) writer.triangle(centre, rim[s], rim[s + 1], outward);
486
+ }
487
+ return writer.finish();
488
+ }
489
+
490
+ export interface ZhudaExtrudeOptions {
491
+ /**
492
+ * The outline on the ground as [x, z] pairs in metres, 3..256 of them, in
493
+ * either direction. It may be concave; it must not cross itself.
494
+ */
495
+ outline: readonly (readonly [number, number])[];
496
+ /** How far it is raised along +Y, in metres. */
497
+ height: number;
498
+ /** Close the top and the underside (default true). */
499
+ caps?: boolean;
500
+ /**
501
+ * Openings right through the shape (a window in a wall piece, the inside of
502
+ * a letter O), up to 32 of them. Each is an outline of 3..256 [x, z] pairs
503
+ * in either direction, inside the outline and clear of its edges and of
504
+ * the other holes.
505
+ */
506
+ holes?: readonly (readonly (readonly [number, number])[])[];
507
+ /**
508
+ * Cut off or round the top edges by this many metres (default 0, sharp).
509
+ * The wall stops this far below the top and the top is drawn in by it;
510
+ * holes are widened by it at the top. The footprint stays the outline.
511
+ * Needs caps.
512
+ */
513
+ bevel?: number;
514
+ /** 1 (default) cuts the edges off flat; 2..8 rounds them in that many steps. */
515
+ bevelSegments?: number;
516
+ /** Bevel the underside's edges too (default false: a shape standing on the ground). */
517
+ bevelBottom?: boolean;
518
+ }
519
+
520
+ type Point2 = readonly [number, number];
521
+
522
+ const area2 = (outline: readonly (readonly [number, number])[]) => {
523
+ let sum = 0;
524
+ for (let i = 0; i < outline.length; i++) {
525
+ const [x0, z0] = outline[i],
526
+ [x1, z1] = outline[(i + 1) % outline.length];
527
+ sum += x0 * z1 - x1 * z0;
528
+ }
529
+ return sum;
530
+ };
531
+ /** Whether segments ab and cd cross at a point inside both. */
532
+ const crosses = (
533
+ a: readonly [number, number],
534
+ b: readonly [number, number],
535
+ c: readonly [number, number],
536
+ d: readonly [number, number],
537
+ ) => {
538
+ const side = (
539
+ p: readonly [number, number],
540
+ q: readonly [number, number],
541
+ r: readonly [number, number],
542
+ ) => (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]);
543
+ const d1 = side(a, b, c),
544
+ d2 = side(a, b, d),
545
+ d3 = side(c, d, a),
546
+ d4 = side(c, d, b);
547
+ return d1 * d2 < 0 && d3 * d4 < 0;
548
+ };
549
+
550
+ const turn2 = (p: Point2, q: Point2, r: Point2) =>
551
+ (q[0] - p[0]) * (r[1] - p[1]) - (q[1] - p[1]) * (r[0] - p[0]);
552
+ /** Distance from p to the segment ab. */
553
+ const toSegment = (p: Point2, a: Point2, b: Point2) => {
554
+ const dx = b[0] - a[0],
555
+ dz = b[1] - a[1],
556
+ length2 = dx * dx + dz * dz,
557
+ t =
558
+ length2 > 0
559
+ ? Math.max(0, Math.min(1, ((p[0] - a[0]) * dx + (p[1] - a[1]) * dz) / length2))
560
+ : 0;
561
+ return Math.hypot(p[0] - a[0] - dx * t, p[1] - a[1] - dz * t);
562
+ };
563
+ /** Whether p lies inside the closed ring (crossing count; a point on an edge may go either way). */
564
+ const insideRing = (p: Point2, ring: readonly Point2[]) => {
565
+ let inside = false;
566
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
567
+ const a = ring[i],
568
+ b = ring[j];
569
+ if (
570
+ a[1] > p[1] !== b[1] > p[1] &&
571
+ p[0] < a[0] + ((p[1] - a[1]) * (b[0] - a[0])) / (b[1] - a[1])
572
+ )
573
+ inside = !inside;
574
+ }
575
+ return inside;
576
+ };
577
+ /** Closest approach of two closed rings, or 0 where their edges cross. */
578
+ const ringGap = (a: readonly Point2[], b: readonly Point2[]) => {
579
+ let gap = Infinity;
580
+ for (let i = 0; i < a.length; i++) {
581
+ const a0 = a[i],
582
+ a1 = a[(i + 1) % a.length];
583
+ for (let j = 0; j < b.length; j++) {
584
+ const b0 = b[j],
585
+ b1 = b[(j + 1) % b.length];
586
+ if (crosses(a0, a1, b0, b1)) return 0;
587
+ gap = Math.min(gap, toSegment(a0, b0, b1), toSegment(b0, a0, a1));
588
+ }
589
+ }
590
+ return gap;
591
+ };
592
+ /** Whether a closed ring crosses itself. */
593
+ const selfCrossing = (ring: readonly Point2[]) => {
594
+ for (let i = 0; i < ring.length; i++)
595
+ for (let j = i + 2; j < ring.length; j++) {
596
+ if (i === 0 && j === ring.length - 1) continue;
597
+ if (crosses(ring[i], ring[i + 1], ring[j], ring[(j + 1) % ring.length])) return true;
598
+ }
599
+ return false;
600
+ };
601
+ /** Smallest gap allowed between a hole and the outline or another hole, in metres. */
602
+ const CLEARANCE = 1e-4;
603
+
604
+ /** The outline counter-clockwise and the holes clockwise: the material is then to the left of every edge. */
605
+ function extrudeRings(options: ZhudaExtrudeOptions): [number, number][][] {
606
+ const outline = options.outline.map(([x, z]) => [x, z] as [number, number]);
607
+ if (area2(outline) < 0) outline.reverse();
608
+ const holes = (options.holes ?? []).map((hole) => {
609
+ const ring = hole.map(([x, z]) => [x, z] as [number, number]);
610
+ if (area2(ring) > 0) ring.reverse();
611
+ return ring;
612
+ });
613
+ return [outline, ...holes];
614
+ }
615
+
616
+ /**
617
+ * A ring moved `amount` metres into the material, every edge kept parallel:
618
+ * the outline is drawn in, a hole is widened. Null where a corner turns
619
+ * straight back on itself.
620
+ */
621
+ function offsetRing(ring: readonly Point2[], amount: number): [number, number][] | null {
622
+ if (amount === 0) return ring.map(([x, z]) => [x, z]);
623
+ const count = ring.length;
624
+ // Each edge's normal away from the material: to its right.
625
+ const away = ring.map((p, i) => {
626
+ const q = ring[(i + 1) % count],
627
+ length = Math.hypot(q[0] - p[0], q[1] - p[1]);
628
+ return [(q[1] - p[1]) / length, -(q[0] - p[0]) / length];
629
+ });
630
+ const out: [number, number][] = [];
631
+ for (let i = 0; i < count; i++) {
632
+ const a = away[(i + count - 1) % count],
633
+ b = away[i],
634
+ k = 1 + a[0] * b[0] + a[1] * b[1];
635
+ if (!(k > 1e-6)) return null;
636
+ out.push([
637
+ ring[i][0] - (amount * (a[0] + b[0])) / k,
638
+ ring[i][1] - (amount * (a[1] + b[1])) / k,
639
+ ]);
640
+ }
641
+ return out;
642
+ }
643
+
644
+ /** Issues where a hole is not inside the outline, clear of it and of the other holes. */
645
+ function ringLayoutIssues(rings: readonly (readonly Point2[])[]): string[] {
646
+ const issues: string[] = [];
647
+ const [outline, ...holes] = rings;
648
+ holes.forEach((hole, h) => {
649
+ if (!hole.every((p) => insideRing(p, outline)) || ringGap(hole, outline) < CLEARANCE)
650
+ issues.push(`holes[${h}]: it must lie inside the outline, clear of its edges`);
651
+ for (let g = h + 1; g < holes.length; g++)
652
+ if (
653
+ ringGap(hole, holes[g]) < CLEARANCE ||
654
+ hole.some((p) => insideRing(p, holes[g])) ||
655
+ holes[g].some((p) => insideRing(p, hole))
656
+ )
657
+ issues.push(`holes[${h}]: it overlaps or touches holes[${g}]`);
658
+ });
659
+ return issues;
660
+ }
661
+
662
+ export function validateZhudaExtrude(options: ZhudaExtrudeOptions): string[] {
663
+ const o = options ?? ({} as ZhudaExtrudeOptions);
664
+ const issues = outlineIssues(o.outline, 'outline', '[x, z]', 256);
665
+ let sound = issues.length === 0;
666
+ const holes = o.holes as unknown;
667
+ if (holes !== undefined) {
668
+ if (!Array.isArray(holes) || holes.length > 32) {
669
+ issues.push('holes: use a list of up to 32 outlines');
670
+ sound = false;
671
+ } else
672
+ holes.forEach((hole, h) => {
673
+ const own = outlineIssues(hole, `holes[${h}]`, '[x, z]', 256);
674
+ issues.push(...own);
675
+ if (own.length) sound = false;
676
+ });
677
+ }
678
+ if (!(typeof o.height === 'number' && o.height > 0 && o.height <= 1000))
679
+ issues.push('height: use a positive number of metres');
680
+ if (o.caps !== undefined && typeof o.caps !== 'boolean') issues.push('caps: true or false');
681
+ const bevel = o.bevel ?? 0,
682
+ steps = o.bevelSegments ?? 1;
683
+ let bevelSound = true;
684
+ if (!(typeof bevel === 'number' && bevel >= 0 && bevel <= 100)) {
685
+ issues.push('bevel: use 0..100 metres');
686
+ bevelSound = false;
687
+ }
688
+ if (!(Number.isInteger(steps) && steps >= 1 && steps <= 8)) {
689
+ issues.push('bevelSegments: use a whole number 1..8');
690
+ bevelSound = false;
691
+ }
692
+ if (o.bevelBottom !== undefined && typeof o.bevelBottom !== 'boolean') {
693
+ issues.push('bevelBottom: true or false');
694
+ bevelSound = false;
695
+ }
696
+ if (bevelSound && bevel > 0) {
697
+ if (o.caps === false) issues.push('bevel: a bevel needs caps');
698
+ if (typeof o.height === 'number' && bevel * (o.bevelBottom ? 2 : 1) >= o.height)
699
+ issues.push(
700
+ o.bevelBottom
701
+ ? 'bevel: twice the bevel must be less than the height'
702
+ : 'bevel: it must be less than the height',
703
+ );
704
+ }
705
+ if (sound) {
706
+ const rings = extrudeRings(o);
707
+ const layout = ringLayoutIssues(rings);
708
+ issues.push(...layout);
709
+ if (!layout.length && bevelSound && bevel > 0) {
710
+ // The top, drawn in by the bevel, must still be the same shape: no edge
711
+ // turned round, nothing crossing, every hole still inside.
712
+ const top = rings.map((ring) => offsetRing(ring, bevel));
713
+ const kept = top.every(
714
+ (ring, r) =>
715
+ ring !== null &&
716
+ ring.every((p) => Number.isFinite(p[0]) && Number.isFinite(p[1])) &&
717
+ ring.every((p, i) => {
718
+ const q = ring[(i + 1) % ring.length],
719
+ a = rings[r][i],
720
+ b = rings[r][(i + 1) % ring.length];
721
+ return (q[0] - p[0]) * (b[0] - a[0]) + (q[1] - p[1]) * (b[1] - a[1]) > 1e-12;
722
+ }) &&
723
+ !selfCrossing(ring),
724
+ );
725
+ if (!kept || ringLayoutIssues(top as [number, number][][]).length)
726
+ issues.push('bevel: too wide for this outline; the top would cross itself');
727
+ }
728
+ const edges = rings.reduce((sum, ring) => sum + ring.length, 0),
729
+ sides = bevel > 0 && bevelSound ? (o.bevelBottom ? 2 : 1) : 0,
730
+ vertices = 4 * edges + sides * 2 * edges * (steps + 1) + (o.caps === false ? 0 : 2 * edges);
731
+ if (vertices > 65535)
732
+ issues.push(
733
+ `outline: ${vertices} vertices is more than one mesh holds (65535); use fewer points`,
734
+ );
735
+ }
736
+ return issues;
737
+ }
738
+
739
+ /**
740
+ * A flat outline raised to a height: kerbs, steps, signs, wall pieces, L and
741
+ * U profiles; with `holes`, windows and doorways; with `bevel`, worn or
742
+ * rounded top edges. The outline lies on the ground as `[x, z]` points; the
743
+ * piece stands from y = 0, its base, to y = `height`, its top, which faces
744
+ * +Y. A bevel, at the top or at the bottom, cuts into that box and never
745
+ * reaches outside it. To stand the piece on its side use `transformZhudaMesh`
746
+ * (a positive `pitchDeg` turns the top towards +Z).
747
+ */
748
+ export function createZhudaExtrudeMesh(options: ZhudaExtrudeOptions): MeshData {
749
+ const issues = validateZhudaExtrude(options);
750
+ if (issues.length) throw Error('Invalid extrusion: ' + issues.join('; '));
751
+ const rings = extrudeRings(options);
752
+ const height = options.height,
753
+ caps = options.caps ?? true,
754
+ bevel = options.bevel ?? 0,
755
+ steps = options.bevelSegments ?? 1,
756
+ bevelBottom = bevel > 0 && options.bevelBottom === true,
757
+ low = bevelBottom ? bevel : 0,
758
+ high = bevel > 0 ? height - bevel : height,
759
+ writer = new MeshWriter();
760
+ // Across a bevel, step k is drawn in by `inset` and raised by `rise` from
761
+ // where the wall stops; its normal is tilted from level toward up by `tilt`.
762
+ // A flat cut has one tilt; a rounded one follows a quarter circle.
763
+ const profile = Array.from({ length: steps + 1 }, (_, k) => {
764
+ const angle = (k / steps) * (Math.PI / 2);
765
+ // The ends are exact, so that the band meets the wall and the cap.
766
+ return {
767
+ inset: k === 0 ? 0 : k === steps ? bevel : bevel * (1 - Math.cos(angle)),
768
+ rise: k === 0 ? 0 : k === steps ? bevel : bevel * Math.sin(angle),
769
+ arc: steps === 1 ? k * bevel * Math.SQRT2 : bevel * angle,
770
+ tilt: steps === 1 ? Math.PI / 4 : angle,
771
+ };
772
+ });
773
+ for (const ring of rings) {
774
+ const count = ring.length,
775
+ rows = bevel > 0 ? profile.map((step) => offsetRing(ring, step.inset)!) : [];
776
+ let along = 0;
777
+ for (let i = 0; i < count; i++) {
778
+ const [x0, z0] = ring[i],
779
+ [x1, z1] = ring[(i + 1) % count],
780
+ length = Math.hypot(x1 - x0, z1 - z0),
781
+ away: [number, number] = [(z1 - z0) / length, -(x1 - x0) / length];
782
+ writer.quad(
783
+ [
784
+ [x0, low, z0],
785
+ [x1, low, z1],
786
+ [x1, high, z1],
787
+ [x0, high, z0],
788
+ ],
789
+ [away[0], 0, away[1]],
790
+ [along, low, along + length, low, along + length, high, along, high],
791
+ );
792
+ const bands: [number, 1 | -1][] = bevel > 0 ? [[high, 1]] : [];
793
+ if (bevelBottom) bands.push([low, -1]);
794
+ for (const [base, up] of bands) {
795
+ const tilted = (tilt: number): ZhudaVec3 => [
796
+ away[0] * Math.cos(tilt),
797
+ up * Math.sin(tilt),
798
+ away[1] * Math.cos(tilt),
799
+ ];
800
+ const band = profile.map((step, k) => {
801
+ const a = rows[k][i],
802
+ b = rows[k][(i + 1) % count],
803
+ y = k === steps ? (up > 0 ? height : 0) : base + up * step.rise,
804
+ v = base + up * step.arc,
805
+ n = tilted(step.tilt);
806
+ return [
807
+ writer.vertex([a[0], y, a[1]], n, along, v),
808
+ writer.vertex([b[0], y, b[1]], n, along + length, v),
809
+ ];
810
+ });
811
+ for (let k = 0; k < steps; k++) {
812
+ const outward = tilted((profile[k].tilt + profile[k + 1].tilt) / 2);
813
+ writer.triangle(band[k][0], band[k][1], band[k + 1][1], outward);
814
+ writer.triangle(band[k][0], band[k + 1][1], band[k + 1][0], outward);
815
+ }
816
+ }
817
+ along += length;
818
+ }
819
+ }
820
+ if (caps)
821
+ for (const [y, up, inset] of [
822
+ [height, 1, bevel],
823
+ [0, -1, bevelBottom ? bevel : 0],
824
+ ] as const) {
825
+ const capRings = rings.map((ring) => offsetRing(ring, inset)!),
826
+ points = capRings.flat(),
827
+ spans: [number, number][] = [];
828
+ for (let r = 0, first = 0; r < capRings.length; first += capRings[r].length, r++)
829
+ spans.push([first, capRings[r].length]);
830
+ const outward: ZhudaVec3 = [0, up, 0];
831
+ const corner = points.map(([x, z]) => writer.vertex([x, y, z], outward, x, z));
832
+ for (const [a, b, c] of fillRegion(points, spans, 'Invalid extrusion'))
833
+ writer.triangle(corner[a], corner[b], corner[c], outward);
834
+ }
835
+ return writer.finish();
836
+ }
837
+
838
+ /**
839
+ * Triangles that fill an outline with holes, as indices into `points`.
840
+ * `rings` gives each ring as [first, count]: the first is the outline,
841
+ * counter-clockwise; the rest are holes, clockwise, inside it and clear of it
842
+ * and of each other. Each hole is joined to the outline by a cut, rightmost
843
+ * hole first, which leaves one outline to fill by ear clipping.
844
+ */
845
+ function fillRegion(
846
+ points: readonly Point2[],
847
+ rings: readonly (readonly [number, number])[],
848
+ what: string,
849
+ ): [number, number, number][] {
850
+ const ring = ([first, count]: readonly [number, number]) =>
851
+ Array.from({ length: count }, (_, i) => first + i);
852
+ const right = (indices: number[]) => Math.max(...indices.map((i) => points[i][0]));
853
+ let polygon = ring(rings[0]);
854
+ const holes = rings
855
+ .slice(1)
856
+ .map(ring)
857
+ .sort((a, b) => right(b) - right(a));
858
+ for (const hole of holes) polygon = joinHole(points, polygon, hole, what);
859
+ return clipEars(points, polygon, what);
860
+ }
861
+
862
+ /**
863
+ * Joins a hole to the polygon: from the hole's rightmost point M along +x to
864
+ * the first edge met, then to a corner of the polygon that M can see.
865
+ */
866
+ function joinHole(points: readonly Point2[], polygon: number[], hole: number[], what: string) {
867
+ let m = 0;
868
+ for (let i = 1; i < hole.length; i++) {
869
+ const p = points[hole[i]],
870
+ q = points[hole[m]];
871
+ if (p[0] > q[0] || (p[0] === q[0] && p[1] < q[1])) m = i;
872
+ }
873
+ const M = points[hole[m]];
874
+ let nearest = Infinity,
875
+ edge = -1;
876
+ for (let i = 0; i < polygon.length; i++) {
877
+ const a = points[polygon[i]],
878
+ b = points[polygon[(i + 1) % polygon.length]];
879
+ if (a[1] === b[1] || (a[1] - M[1]) * (b[1] - M[1]) > 0) continue;
880
+ const x = a[0] + ((M[1] - a[1]) * (b[0] - a[0])) / (b[1] - a[1]);
881
+ if (x >= M[0] && x < nearest) {
882
+ nearest = x;
883
+ edge = i;
884
+ }
885
+ }
886
+ if (edge < 0) throw Error(`${what}: a hole could not be joined to the outline`);
887
+ const I: Point2 = [nearest, M[1]],
888
+ a = polygon[edge],
889
+ b = polygon[(edge + 1) % polygon.length];
890
+ let target = points[a][0] > points[b][0] ? a : b;
891
+ if (points[a][0] === I[0] && points[a][1] === I[1]) target = a;
892
+ else if (points[b][0] === I[0] && points[b][1] === I[1]) target = b;
893
+ else {
894
+ // A corner inside the triangle M, I, P may hide P from M: then the one
895
+ // closest in angle to the line from M is seen.
896
+ const P = points[target];
897
+ let best = Infinity,
898
+ bestDistance = Infinity,
899
+ seen = -1;
900
+ for (const index of polygon) {
901
+ if (index === target) continue;
902
+ const R = points[index],
903
+ d1 = turn2(M, I, R),
904
+ d2 = turn2(I, P, R),
905
+ d3 = turn2(P, M, R);
906
+ if ((d1 < 0 || d2 < 0 || d3 < 0) && (d1 > 0 || d2 > 0 || d3 > 0)) continue;
907
+ const dx = R[0] - M[0],
908
+ dz = Math.abs(R[1] - M[1]);
909
+ if (!(dx > 0)) continue;
910
+ const tan = dz / dx,
911
+ distance = Math.hypot(dx, dz);
912
+ if (tan < best - 1e-12 || (Math.abs(tan - best) <= 1e-12 && distance < bestDistance)) {
913
+ best = tan;
914
+ bestDistance = distance;
915
+ seen = index;
916
+ }
917
+ }
918
+ if (seen >= 0) target = seen;
919
+ }
920
+ // A corner already used by a cut appears twice: join at the copy whose
921
+ // angle opens toward M.
922
+ let at = -1;
923
+ for (let k = 0; k < polygon.length && at < 0; k++) {
924
+ if (polygon[k] !== target) continue;
925
+ const before = points[polygon[(k + polygon.length - 1) % polygon.length]],
926
+ here = points[target],
927
+ after = points[polygon[(k + 1) % polygon.length]],
928
+ inLeft = turn2(before, here, M) >= 0,
929
+ outLeft = turn2(here, after, M) >= 0;
930
+ if (turn2(before, here, after) >= 0 ? inLeft && outLeft : inLeft || outLeft) at = k;
931
+ }
932
+ if (at < 0) at = polygon.indexOf(target);
933
+ return [
934
+ ...polygon.slice(0, at + 1),
935
+ ...hole.slice(m),
936
+ ...hole.slice(0, m),
937
+ hole[m],
938
+ target,
939
+ ...polygon.slice(at + 1),
940
+ ];
941
+ }
942
+
943
+ /**
944
+ * Ear clipping of a counter-clockwise polygon given as indices into `points`
945
+ * (a corner may appear twice where a cut joins a hole): cut off a corner that
946
+ * bulges outward and holds no other point. A corner on a straight line is cut
947
+ * only when nothing else is left.
948
+ */
949
+ function clipEars(points: readonly Point2[], polygon: number[], what: string) {
950
+ const left = polygon.slice(),
951
+ triangles: [number, number, number][] = [];
952
+ const corner = (i: number): [number, number, number] => [
953
+ left[(i + left.length - 1) % left.length],
954
+ left[i],
955
+ left[(i + 1) % left.length],
956
+ ];
957
+ const ear = (i: number, flat: boolean) => {
958
+ const [a, b, c] = corner(i),
959
+ A = points[a],
960
+ B = points[b],
961
+ C = points[c],
962
+ t = turn2(A, B, C);
963
+ if (flat ? t < -1e-12 : t <= 1e-12) return false;
964
+ for (const p of left) {
965
+ if (p === a || p === b || p === c) continue;
966
+ const P = points[p];
967
+ if (turn2(A, B, P) >= 0 && turn2(B, C, P) >= 0 && turn2(C, A, P) >= 0) return false;
968
+ }
969
+ return true;
970
+ };
971
+ let i = 0,
972
+ misses = 0,
973
+ flat = false;
974
+ while (left.length > 3) {
975
+ if (ear(i, flat)) {
976
+ triangles.push(corner(i));
977
+ left.splice(i, 1);
978
+ // The corner before may have become an ear.
979
+ i = (i + left.length - 1) % left.length;
980
+ misses = 0;
981
+ flat = false;
982
+ } else {
983
+ i = (i + 1) % left.length;
984
+ if (++misses < left.length) continue;
985
+ if (flat) throw Error(`${what}: the outline could not be filled`);
986
+ flat = true;
987
+ misses = 0;
988
+ }
989
+ }
990
+ triangles.push([left[0], left[1], left[2]]);
991
+ return triangles;
992
+ }
993
+
994
+ /** Checks a closed outline of [a, b] pairs: finite, no point twice in a row, no crossing, some area. */
995
+ function outlineIssues(outline: unknown, name: string, pair: string, most: number): string[] {
996
+ const issues: string[] = [];
997
+ let sound = false;
998
+ if (!Array.isArray(outline) || outline.length < 3 || outline.length > most)
999
+ issues.push(`${name}: use 3..${most} ${pair} pairs`);
1000
+ else {
1001
+ sound = true;
1002
+ outline.forEach((point, i) => {
1003
+ if (
1004
+ !Array.isArray(point) ||
1005
+ point.length !== 2 ||
1006
+ !point.every((n) => typeof n === 'number' && Number.isFinite(n) && Math.abs(n) <= 1000)
1007
+ ) {
1008
+ issues.push(`${name}[${i}]: use ${pair} in metres`);
1009
+ sound = false;
1010
+ } else {
1011
+ const before = outline[(i + outline.length - 1) % outline.length];
1012
+ if (Array.isArray(before) && before[0] === point[0] && before[1] === point[1]) {
1013
+ issues.push(`${name}[${i}]: the same point as the one before it`);
1014
+ sound = false;
1015
+ }
1016
+ }
1017
+ });
1018
+ }
1019
+ if (sound) {
1020
+ const points = outline as [number, number][];
1021
+ let crossing = false;
1022
+ search: for (let i = 0; i < points.length; i++)
1023
+ for (let j = i + 2; j < points.length; j++) {
1024
+ if (i === 0 && j === points.length - 1) continue;
1025
+ if (crosses(points[i], points[i + 1], points[j], points[(j + 1) % points.length])) {
1026
+ issues.push(`${name}: edge ${i} crosses edge ${j}`);
1027
+ crossing = true;
1028
+ break search;
1029
+ }
1030
+ }
1031
+ if (!crossing && Math.abs(area2(points)) < 1e-9) issues.push(`${name}: it encloses no area`);
1032
+ }
1033
+ return issues;
1034
+ }
1035
+
1036
+ export interface ZhudaSweepOptions {
1037
+ /**
1038
+ * The cross-section as [across, up] pairs in metres, 3..64 of them, in
1039
+ * either direction: across is to the left of the way the path runs, up is
1040
+ * up. It may be concave; it must not cross itself. [0, 0] lies on the path.
1041
+ */
1042
+ profile: readonly (readonly [number, number])[];
1043
+ /** The line the section is carried along, 2..1024 points in metres. */
1044
+ path: readonly (readonly [number, number, number])[];
1045
+ /** The path returns to its first point: a ring with no ends (default false). */
1046
+ closed?: boolean;
1047
+ /** Close the two ends (default true); ignored on a closed path. */
1048
+ caps?: boolean;
1049
+ }
1050
+
1051
+ export function validateZhudaSweep(options: ZhudaSweepOptions): string[] {
1052
+ const o = options ?? ({} as ZhudaSweepOptions);
1053
+ const issues = outlineIssues(o.profile, 'profile', '[across, up]', 64);
1054
+ const path = o.path as unknown;
1055
+ if (!Array.isArray(path) || path.length < 2 || path.length > 1024)
1056
+ issues.push('path: use 2..1024 [x, y, z] points');
1057
+ else {
1058
+ let sound = true;
1059
+ path.forEach((point, i) => {
1060
+ if (
1061
+ !Array.isArray(point) ||
1062
+ point.length !== 3 ||
1063
+ !point.every((n) => typeof n === 'number' && Number.isFinite(n) && Math.abs(n) <= 100000)
1064
+ ) {
1065
+ issues.push(`path[${i}]: use [x, y, z] in metres`);
1066
+ sound = false;
1067
+ } else if (
1068
+ i > 0 &&
1069
+ Array.isArray(path[i - 1]) &&
1070
+ path[i - 1].every((n: unknown, k: number) => n === point[k])
1071
+ ) {
1072
+ issues.push(`path[${i}]: the same point as the one before it`);
1073
+ sound = false;
1074
+ }
1075
+ });
1076
+ if (sound && o.closed === true) {
1077
+ if (path.length < 3) {
1078
+ issues.push('path: a closed path needs three points or more');
1079
+ sound = false;
1080
+ } else if (path[0].every((n: number, k: number) => n === path[path.length - 1][k])) {
1081
+ issues.push('path: a closed path does not repeat its first point at the end');
1082
+ sound = false;
1083
+ }
1084
+ }
1085
+ // A corner sharper than about 150 degrees would stretch the section without end.
1086
+ if (sound) {
1087
+ const points = path as ZhudaVec3[];
1088
+ const count = points.length;
1089
+ for (let i = 0; i < count; i++) {
1090
+ const closed = o.closed === true;
1091
+ if (!closed && (i === 0 || i === count - 1)) continue;
1092
+ const before = normalize(sub(points[i], points[(i + count - 1) % count])),
1093
+ after = normalize(sub(points[(i + 1) % count], points[i]));
1094
+ if (dot(before, after) < -0.87)
1095
+ issues.push(`path[${i}]: the path turns back on itself here`);
1096
+ }
1097
+ }
1098
+ }
1099
+ if (o.closed !== undefined && typeof o.closed !== 'boolean') issues.push('closed: true or false');
1100
+ if (o.caps !== undefined && typeof o.caps !== 'boolean') issues.push('caps: true or false');
1101
+ return issues;
1102
+ }
1103
+
1104
+ /**
1105
+ * A cross-section carried along a path: kerbs and gutters round a bend,
1106
+ * rails, cornices, cables, walls that follow a street. The section stays
1107
+ * upright (its up is the world's, as far as the path allows) and keeps its
1108
+ * width through a corner. Each edge of the section is a flat band with its
1109
+ * own normal; the corners of the path are not rounded.
1110
+ */
1111
+ export function createZhudaSweepMesh(options: ZhudaSweepOptions): MeshData {
1112
+ const issues = validateZhudaSweep(options);
1113
+ if (issues.length) throw Error('Invalid sweep: ' + issues.join('; '));
1114
+ const profile = options.profile.map(([a, b]) => [a, b] as [number, number]);
1115
+ // Counter-clockwise seen along the path: the outside is then to the right of each edge.
1116
+ if (area2(profile) < 0) profile.reverse();
1117
+ const path = options.path.map((p) => [p[0], p[1], p[2]] as ZhudaVec3),
1118
+ closed = options.closed ?? false,
1119
+ caps = (options.caps ?? true) && !closed,
1120
+ count = path.length;
1121
+ const direction = (i: number) => normalize(sub(path[(i + 1) % count], path[i]));
1122
+ // At each point: the way the path runs there, the section's across and up
1123
+ // axes, and how much wider the section is drawn to keep its width through
1124
+ // the corner.
1125
+ const frames: { tangent: ZhudaVec3; across: ZhudaVec3; up: ZhudaVec3; wide: number }[] = [];
1126
+ let carried: ZhudaVec3 | undefined;
1127
+ for (let i = 0; i < count; i++) {
1128
+ const before = closed || i > 0 ? direction((i + count - 1) % count) : undefined,
1129
+ after = closed || i < count - 1 ? direction(i) : undefined;
1130
+ const tangent = normalize(
1131
+ before && after
1132
+ ? [before[0] + after[0], before[1] + after[1], before[2] + after[2]]
1133
+ : (after ?? before)!,
1134
+ );
1135
+ // Across is level and to the left of the way the path runs; where the
1136
+ // path runs straight up or down, the across of the point before is kept.
1137
+ let across: ZhudaVec3;
1138
+ if (Math.abs(tangent[1]) < 0.999) across = normalize(cross([0, 1, 0], tangent));
1139
+ else {
1140
+ const from: ZhudaVec3 = carried ?? [1, 0, 0],
1141
+ k = dot(from, tangent);
1142
+ across = normalize([
1143
+ from[0] - tangent[0] * k,
1144
+ from[1] - tangent[1] * k,
1145
+ from[2] - tangent[2] * k,
1146
+ ]);
1147
+ }
1148
+ carried = across;
1149
+ const up = cross(tangent, across);
1150
+ const half = before && after ? Math.max(dot(before, tangent), 0.25) : 1;
1151
+ frames.push({ tangent, across, up, wide: 1 / half });
1152
+ }
1153
+ const at = (i: number, [a, b]: readonly [number, number]): ZhudaVec3 => {
1154
+ const f = frames[i],
1155
+ p = path[i];
1156
+ return [
1157
+ p[0] + f.across[0] * a * f.wide + f.up[0] * b,
1158
+ p[1] + f.across[1] * a * f.wide + f.up[1] * b,
1159
+ p[2] + f.across[2] * a * f.wide + f.up[2] * b,
1160
+ ];
1161
+ };
1162
+ const writer = new MeshWriter();
1163
+ const travelled: number[] = [0];
1164
+ for (let i = 1; i <= count; i++)
1165
+ travelled.push(travelled[i - 1] + Math.hypot(...sub(path[i % count], path[i - 1])));
1166
+ let round = 0;
1167
+ for (let e = 0; e < profile.length; e++) {
1168
+ const p0 = profile[e],
1169
+ p1 = profile[(e + 1) % profile.length],
1170
+ length = Math.hypot(p1[0] - p0[0], p1[1] - p0[1]),
1171
+ // Outward in the section's own plane.
1172
+ n2: [number, number] = [(p1[1] - p0[1]) / length, -(p1[0] - p0[0]) / length];
1173
+ const normalAt = (i: number): ZhudaVec3 => {
1174
+ const f = frames[i];
1175
+ return normalize([
1176
+ f.across[0] * n2[0] + f.up[0] * n2[1],
1177
+ f.across[1] * n2[0] + f.up[1] * n2[1],
1178
+ f.across[2] * n2[0] + f.up[2] * n2[1],
1179
+ ]);
1180
+ };
1181
+ const rows: [number, number][] = [];
1182
+ const steps = closed ? count + 1 : count;
1183
+ for (let k = 0; k < steps; k++) {
1184
+ const i = k % count,
1185
+ n = normalAt(i);
1186
+ rows.push([
1187
+ writer.vertex(at(i, p0), n, round, travelled[k]),
1188
+ writer.vertex(at(i, p1), n, round + length, travelled[k]),
1189
+ ]);
1190
+ }
1191
+ for (let k = 0; k < rows.length - 1; k++) {
1192
+ const outward = normalAt(k % count);
1193
+ writer.triangle(rows[k][0], rows[k][1], rows[k + 1][1], outward);
1194
+ writer.triangle(rows[k][0], rows[k + 1][1], rows[k + 1][0], outward);
1195
+ }
1196
+ round += length;
1197
+ }
1198
+ if (caps) {
1199
+ const triangles = fillRegion(profile, [[0, profile.length]], 'Invalid sweep');
1200
+ for (const end of [0, count - 1]) {
1201
+ const t = frames[end].tangent,
1202
+ outward: ZhudaVec3 = end === 0 ? [-t[0], -t[1], -t[2]] : t;
1203
+ const corner = profile.map((p) => writer.vertex(at(end, p), outward, p[0], p[1]));
1204
+ for (const [a, b, c] of triangles) writer.triangle(corner[a], corner[b], corner[c], outward);
1205
+ }
1206
+ }
1207
+ return writer.finish();
1208
+ }
1209
+
1210
+ export type ZhudaCurveKind = 'centripetal' | 'chordal' | 'uniform';
1211
+
1212
+ export interface ZhudaCurveOptions {
1213
+ /** Points the curve passes through: 2..256 [x, y, z] in metres (3 or more when closed). */
1214
+ points: readonly (readonly [number, number, number])[];
1215
+ /** The curve runs on from the last point back round to the first (default false). */
1216
+ closed?: boolean;
1217
+ /**
1218
+ * How a Catmull-Rom curve spaces its points (default 'centripetal': no
1219
+ * loops or cusps where points bunch up; 'chordal' bends wider; 'uniform' is
1220
+ * the classic curve).
1221
+ */
1222
+ kind?: ZhudaCurveKind;
1223
+ }
1224
+
1225
+ export function validateZhudaCurve(options: ZhudaCurveOptions): string[] {
1226
+ const o = options ?? ({} as ZhudaCurveOptions);
1227
+ const issues: string[] = [];
1228
+ const closed = o.closed === true,
1229
+ points = o.points as unknown,
1230
+ least = closed ? 3 : 2;
1231
+ if (o.closed !== undefined && typeof o.closed !== 'boolean') issues.push('closed: true or false');
1232
+ if (o.kind !== undefined && !['centripetal', 'chordal', 'uniform'].includes(o.kind))
1233
+ issues.push('kind: use centripetal, chordal or uniform');
1234
+ if (!Array.isArray(points) || points.length < least || points.length > 256)
1235
+ issues.push(`points: use ${least}..256 [x, y, z] points`);
1236
+ else
1237
+ points.forEach((point, i) => {
1238
+ if (
1239
+ !Array.isArray(point) ||
1240
+ point.length !== 3 ||
1241
+ !point.every((n) => typeof n === 'number' && Number.isFinite(n) && Math.abs(n) <= 100000)
1242
+ )
1243
+ issues.push(`points[${i}]: use [x, y, z] in metres`);
1244
+ else {
1245
+ const before = i > 0 ? points[i - 1] : closed ? points[points.length - 1] : undefined;
1246
+ if (
1247
+ Array.isArray(before) &&
1248
+ before.length === 3 &&
1249
+ before.every((n: unknown, k: number) => n === point[k])
1250
+ )
1251
+ issues.push(
1252
+ i > 0
1253
+ ? `points[${i}]: the same point as the one before it`
1254
+ : 'points: a closed curve does not repeat its first point at the end',
1255
+ );
1256
+ }
1257
+ });
1258
+ return issues;
1259
+ }
1260
+
1261
+ /** Steps per curve segment in the table that measures length along it. */
1262
+ const CURVE_TABLE_STEPS = 64;
1263
+
1264
+ /**
1265
+ * A Catmull-Rom curve measured by length: `at(s)` is the point `s` metres
1266
+ * along it and the way it runs there.
1267
+ */
1268
+ function curveByLength(options: ZhudaCurveOptions) {
1269
+ const points = options.points.map((p) => [p[0], p[1], p[2]] as ZhudaVec3),
1270
+ closed = options.closed ?? false,
1271
+ kind = options.kind ?? 'centripetal',
1272
+ count = points.length,
1273
+ segments = closed ? count : count - 1,
1274
+ power = kind === 'centripetal' ? 0.25 : kind === 'chordal' ? 0.5 : 0;
1275
+ // Before the first point and after the last of an open curve, the line
1276
+ // carries straight on.
1277
+ const point = (i: number): ZhudaVec3 => {
1278
+ if (closed) return points[((i % count) + count) % count];
1279
+ if (i < 0) return points[0].map((v, k) => 2 * v - points[1][k]) as ZhudaVec3;
1280
+ if (i >= count)
1281
+ return points[count - 1].map((v, k) => 2 * v - points[count - 2][k]) as ZhudaVec3;
1282
+ return points[i];
1283
+ };
1284
+ // Each segment as a cubic c0 + c1 t + c2 t² + c3 t³, from the
1285
+ // non-uniform Catmull-Rom tangents at its two ends.
1286
+ const cubic = Array.from({ length: segments }, (_, s) => {
1287
+ const p0 = point(s - 1),
1288
+ p1 = point(s),
1289
+ p2 = point(s + 1),
1290
+ p3 = point(s + 2);
1291
+ let d0 = Math.pow(Math.hypot(...sub(p1, p0)) ** 2, power),
1292
+ d1 = Math.pow(Math.hypot(...sub(p2, p1)) ** 2, power),
1293
+ d2 = Math.pow(Math.hypot(...sub(p3, p2)) ** 2, power);
1294
+ if (d1 < 1e-4) d1 = 1;
1295
+ if (d0 < 1e-4) d0 = d1;
1296
+ if (d2 < 1e-4) d2 = d1;
1297
+ return [0, 1, 2].map((k) => {
1298
+ const t1 =
1299
+ power === 0
1300
+ ? (p2[k] - p0[k]) / 2
1301
+ : ((p1[k] - p0[k]) / d0 - (p2[k] - p0[k]) / (d0 + d1) + (p2[k] - p1[k]) / d1) * d1,
1302
+ t2 =
1303
+ power === 0
1304
+ ? (p3[k] - p1[k]) / 2
1305
+ : ((p2[k] - p1[k]) / d1 - (p3[k] - p1[k]) / (d1 + d2) + (p3[k] - p2[k]) / d2) * d1;
1306
+ return [p1[k], t1, -3 * p1[k] + 3 * p2[k] - 2 * t1 - t2, 2 * p1[k] - 2 * p2[k] + t1 + t2];
1307
+ });
1308
+ });
1309
+ const evaluate = (u: number) => {
1310
+ const s = Math.min(Math.floor(u), segments - 1),
1311
+ t = u - s,
1312
+ c = cubic[s];
1313
+ return {
1314
+ point: c.map((q) => q[0] + t * (q[1] + t * (q[2] + t * q[3]))) as ZhudaVec3,
1315
+ slope: c.map((q) => q[1] + t * (2 * q[2] + 3 * t * q[3])) as ZhudaVec3,
1316
+ };
1317
+ };
1318
+ const steps = segments * CURVE_TABLE_STEPS,
1319
+ lengths = new Float64Array(steps + 1);
1320
+ let previous = evaluate(0).point;
1321
+ for (let i = 1; i <= steps; i++) {
1322
+ const next = evaluate(i / CURVE_TABLE_STEPS).point;
1323
+ lengths[i] = lengths[i - 1] + Math.hypot(...sub(next, previous));
1324
+ previous = next;
1325
+ }
1326
+ const length = lengths[steps];
1327
+ if (!(length > 1e-9)) throw Error('Degenerate curve: it has no length');
1328
+ const at = (s: number) => {
1329
+ const target = Math.max(0, Math.min(length, s));
1330
+ let lo = 0,
1331
+ hi = steps;
1332
+ while (hi - lo > 1) {
1333
+ const mid = (lo + hi) >> 1;
1334
+ if (lengths[mid] <= target) lo = mid;
1335
+ else hi = mid;
1336
+ }
1337
+ const span = lengths[hi] - lengths[lo],
1338
+ u = (lo + (span > 0 ? (target - lengths[lo]) / span : 0)) / CURVE_TABLE_STEPS,
1339
+ here = evaluate(u);
1340
+ let tangent = here.slope;
1341
+ // A cusp has no slope: take the way the curve runs either side of it.
1342
+ if (!(Math.hypot(...tangent) > 1e-9))
1343
+ tangent = sub(
1344
+ evaluate(Math.min(segments, u + 1e-4)).point,
1345
+ evaluate(Math.max(0, u - 1e-4)).point,
1346
+ );
1347
+ return { point: here.point, tangent: normalize(tangent) };
1348
+ };
1349
+ return { length, at };
1350
+ }
1351
+
1352
+ /**
1353
+ * `count` points (2..4096) along a Catmull-Rom curve, the same distance
1354
+ * apart. An open curve starts and ends on its first and last points; a
1355
+ * closed one does not repeat its start. Feed them to `createZhudaSweepMesh`
1356
+ * (with `closed` for a closed curve) or `createZhudaTubeMesh`.
1357
+ */
1358
+ export function sampleZhudaCurve(options: ZhudaCurveOptions, count: number): ZhudaVec3[] {
1359
+ const issues = validateZhudaCurve(options);
1360
+ if (!(Number.isInteger(count) && count >= 2 && count <= 4096))
1361
+ issues.push('count: use a whole number 2..4096');
1362
+ if (issues.length) throw Error('Invalid curve: ' + issues.join('; '));
1363
+ const curve = curveByLength(options),
1364
+ closed = options.closed ?? false;
1365
+ return Array.from(
1366
+ { length: count },
1367
+ (_, i) => curve.at((curve.length * i) / (closed ? count : count - 1)).point,
1368
+ );
1369
+ }
1370
+
1371
+ /** What `sampleZhudaCurvePath` reads of a LayaAir `CurvePath`: a point a share 0..1 of the way along. */
1372
+ export interface ZhudaCurvePathLike {
1373
+ getPointAt(t: number): { readonly x: number; readonly y: number; readonly z: number };
1374
+ }
1375
+
1376
+ /**
1377
+ * `count` points (2..4096) evenly spaced along a LayaAir `CurvePath`
1378
+ * (Catmull-Rom, Bezier or straight runs), for `createZhudaTubeMesh` or
1379
+ * `createZhudaSweepMesh`. With `closed`, the last point is not repeated, as
1380
+ * a closed sweep wants.
1381
+ *
1382
+ * LayaAir walks a spline by its control points, not by length: on a spline
1383
+ * through five uneven points its own steps were 0.04 to 0.25 apart. The
1384
+ * path is read finely here (sixteen times as often as asked, a thousand
1385
+ * times at least) and the points are spaced along what was read: a sharp
1386
+ * corner between two straight runs is rounded by up to a thousandth of the
1387
+ * path's length.
1388
+ */
1389
+ export function sampleZhudaCurvePath(
1390
+ path: ZhudaCurvePathLike,
1391
+ count: number,
1392
+ options: { closed?: boolean } = {},
1393
+ ): ZhudaVec3[] {
1394
+ if (!path || typeof path.getPointAt !== 'function')
1395
+ throw Error('Invalid curve path: it needs getPointAt(t)');
1396
+ if (!(Number.isInteger(count) && count >= 2 && count <= 4096))
1397
+ throw Error('Invalid curve path: count must be a whole number 2..4096');
1398
+ const closed = options.closed === true,
1399
+ fine = Math.max(count * 16, 1024);
1400
+ const read: ZhudaVec3[] = [],
1401
+ along = [0];
1402
+ for (let i = 0; i <= fine; i++) {
1403
+ // LayaAir hands back one shared vector: copy it at once.
1404
+ const p = path.getPointAt(i / fine),
1405
+ point: ZhudaVec3 = [p?.x, p?.y, p?.z];
1406
+ if (!point.every(Number.isFinite))
1407
+ throw Error(`Invalid curve path: the point at ${i / fine} is not finite`);
1408
+ const last = read[read.length - 1];
1409
+ if (last) along.push(along[along.length - 1] + Math.hypot(...sub(point, last)));
1410
+ read.push(point);
1411
+ }
1412
+ const length = along[fine];
1413
+ if (!(length > 0)) throw Error('Invalid curve path: it has no length');
1414
+ const out: ZhudaVec3[] = [];
1415
+ let k = 0;
1416
+ for (let i = 0; i < count; i++) {
1417
+ const s = (length * i) / (closed ? count : count - 1);
1418
+ while (k < fine - 1 && along[k + 1] < s) k++;
1419
+ const span = along[k + 1] - along[k],
1420
+ t = span > 0 ? Math.min(1, (s - along[k]) / span) : 0;
1421
+ const a = read[k],
1422
+ b = read[k + 1];
1423
+ out.push([a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t]);
1424
+ }
1425
+ return out;
1426
+ }
1427
+
1428
+ export interface ZhudaCurveTubeOptions extends ZhudaCurveOptions {
1429
+ radius: number;
1430
+ /** Rings along the curve, 2..2048 (default 64), the same distance apart. */
1431
+ tubularSegments?: number;
1432
+ /** Sides around the tube, 3..64 (default 12). */
1433
+ radialSegments?: number;
1434
+ /** Close the two ends of an open curve with flat caps (default true). */
1435
+ caps?: boolean;
1436
+ }
1437
+
1438
+ export function validateZhudaCurveTube(options: ZhudaCurveTubeOptions): string[] {
1439
+ const o = options ?? ({} as ZhudaCurveTubeOptions);
1440
+ const issues = validateZhudaCurve(o);
1441
+ if (!(typeof o.radius === 'number' && o.radius > 0 && o.radius <= 100))
1442
+ issues.push('radius: use a positive number of metres, up to 100');
1443
+ const along = o.tubularSegments ?? 64,
1444
+ around = o.radialSegments ?? 12;
1445
+ if (!(Number.isInteger(along) && along >= 2 && along <= 2048))
1446
+ issues.push('tubularSegments: use a whole number 2..2048');
1447
+ if (!(Number.isInteger(around) && around >= 3 && around <= 64))
1448
+ issues.push('radialSegments: use a whole number 3..64');
1449
+ if (o.caps !== undefined && typeof o.caps !== 'boolean') issues.push('caps: true or false');
1450
+ const vertices = (along + 1) * (around + 1) + (o.closed ? 0 : 2 * (around + 2));
1451
+ if (Number.isFinite(vertices) && vertices > 65535)
1452
+ issues.push(`tubularSegments: ${vertices} vertices is more than one mesh holds (65535)`);
1453
+ return issues;
1454
+ }
1455
+
1456
+ /**
1457
+ * A round tube along a Catmull-Rom curve through the given points: pipes,
1458
+ * cables, handles, a loop of hose. Its rings are spaced evenly along the
1459
+ * curve and turn with it as little as possible (rotation-minimising frames);
1460
+ * a closed tube's seam is twisted into place over its whole length. A radius
1461
+ * wider than the tightest bend folds the tube into itself; that is not
1462
+ * checked.
1463
+ */
1464
+ export function createZhudaCurveTubeMesh(options: ZhudaCurveTubeOptions): MeshData {
1465
+ const issues = validateZhudaCurveTube(options);
1466
+ if (issues.length) throw Error('Invalid curve tube: ' + issues.join('; '));
1467
+ const radius = options.radius,
1468
+ along = options.tubularSegments ?? 64,
1469
+ around = options.radialSegments ?? 12,
1470
+ closed = options.closed ?? false,
1471
+ caps = (options.caps ?? true) && !closed,
1472
+ curve = curveByLength(options);
1473
+ const stations = Array.from({ length: along + 1 }, (_, i) => {
1474
+ const s = (curve.length * i) / along;
1475
+ return { s, ...curve.at(s) };
1476
+ });
1477
+ // Rotation-minimising frames by double reflection (Wang et al. 2008).
1478
+ const first = stations[0].tangent;
1479
+ let side = normalize(cross(Math.abs(first[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0], first));
1480
+ const sides: ZhudaVec3[] = [side];
1481
+ for (let i = 1; i <= along; i++) {
1482
+ const before = stations[i - 1],
1483
+ here = stations[i],
1484
+ v1 = sub(here.point, before.point),
1485
+ c1 = dot(v1, v1);
1486
+ if (c1 > 1e-18) {
1487
+ const reflect = (a: ZhudaVec3, v: ZhudaVec3, c: number): ZhudaVec3 => {
1488
+ const k = (2 / c) * dot(v, a);
1489
+ return [a[0] - k * v[0], a[1] - k * v[1], a[2] - k * v[2]];
1490
+ };
1491
+ const sideL = reflect(side, v1, c1),
1492
+ tangentL = reflect(before.tangent, v1, c1),
1493
+ v2 = sub(here.tangent, tangentL),
1494
+ c2 = dot(v2, v2);
1495
+ side = c2 > 1e-18 ? reflect(sideL, v2, c2) : sideL;
1496
+ }
1497
+ const k = dot(side, here.tangent);
1498
+ side = normalize([
1499
+ side[0] - here.tangent[0] * k,
1500
+ side[1] - here.tangent[1] * k,
1501
+ side[2] - here.tangent[2] * k,
1502
+ ]);
1503
+ sides.push(side);
1504
+ }
1505
+ if (closed) {
1506
+ // Turn every ring a share of the twist left at the seam.
1507
+ const t = stations[0].tangent,
1508
+ last = sides[along],
1509
+ twist = Math.atan2(dot(cross(last, sides[0]), t), dot(last, sides[0]));
1510
+ for (let i = 1; i <= along; i++) {
1511
+ const angle = (twist * stations[i].s) / curve.length,
1512
+ ti = stations[i].tangent,
1513
+ other = cross(ti, sides[i]);
1514
+ sides[i] = normalize([
1515
+ sides[i][0] * Math.cos(angle) + other[0] * Math.sin(angle),
1516
+ sides[i][1] * Math.cos(angle) + other[1] * Math.sin(angle),
1517
+ sides[i][2] * Math.cos(angle) + other[2] * Math.sin(angle),
1518
+ ]);
1519
+ }
1520
+ }
1521
+ const writer = new MeshWriter();
1522
+ const rings: number[][] = [];
1523
+ for (let i = 0; i <= along; i++) {
1524
+ const ring: number[] = [];
1525
+ // A closed tube's last ring lies exactly on its first (a seam for the UVs only).
1526
+ const station = closed && i === along ? stations[0] : stations[i],
1527
+ r = closed && i === along ? sides[0] : sides[i],
1528
+ b = cross(station.tangent, r);
1529
+ for (let j = 0; j <= around; j++) {
1530
+ const angle = (j / around) * Math.PI * 2,
1531
+ n: ZhudaVec3 = [
1532
+ r[0] * Math.cos(angle) + b[0] * Math.sin(angle),
1533
+ r[1] * Math.cos(angle) + b[1] * Math.sin(angle),
1534
+ r[2] * Math.cos(angle) + b[2] * Math.sin(angle),
1535
+ ];
1536
+ ring.push(
1537
+ writer.vertex(
1538
+ [
1539
+ station.point[0] + n[0] * radius,
1540
+ station.point[1] + n[1] * radius,
1541
+ station.point[2] + n[2] * radius,
1542
+ ],
1543
+ n,
1544
+ angle * radius,
1545
+ stations[i].s,
1546
+ ),
1547
+ );
1548
+ }
1549
+ rings.push(ring);
1550
+ }
1551
+ for (let i = 0; i < along; i++)
1552
+ for (let j = 0; j < around; j++) {
1553
+ const angle = ((j + 0.5) / around) * Math.PI * 2,
1554
+ r = sides[i],
1555
+ b = cross(stations[i].tangent, r),
1556
+ outward: ZhudaVec3 = [
1557
+ r[0] * Math.cos(angle) + b[0] * Math.sin(angle),
1558
+ r[1] * Math.cos(angle) + b[1] * Math.sin(angle),
1559
+ r[2] * Math.cos(angle) + b[2] * Math.sin(angle),
1560
+ ];
1561
+ writer.triangle(rings[i][j], rings[i][j + 1], rings[i + 1][j + 1], outward);
1562
+ writer.triangle(rings[i][j], rings[i + 1][j + 1], rings[i + 1][j], outward);
1563
+ }
1564
+ if (caps)
1565
+ for (const end of [0, along]) {
1566
+ const t = stations[end].tangent,
1567
+ outward: ZhudaVec3 = end === 0 ? [-t[0], -t[1], -t[2]] : t,
1568
+ centre = writer.vertex(stations[end].point, outward, 0, 0),
1569
+ rim: number[] = [];
1570
+ for (let j = 0; j <= around; j++) {
1571
+ const angle = (j / around) * Math.PI * 2;
1572
+ rim.push(
1573
+ writer.vertex(
1574
+ writer.point(rings[end][j]),
1575
+ outward,
1576
+ Math.cos(angle) * radius,
1577
+ Math.sin(angle) * radius,
1578
+ ),
1579
+ );
1580
+ }
1581
+ for (let j = 0; j < around; j++) writer.triangle(centre, rim[j], rim[j + 1], outward);
1582
+ }
1583
+ return writer.finish();
1584
+ }
1585
+
1586
+ export interface ZhudaTorusOptions {
1587
+ /** From the middle of the hole to the middle of the tube, in metres. */
1588
+ radius: number;
1589
+ /** The tube's own radius: less than `radius`. */
1590
+ tube: number;
1591
+ /** Sides around the tube, 3..64 (default 16). */
1592
+ radialSegments?: number;
1593
+ /** Steps around the hole, 3..256 (default 48). */
1594
+ tubularSegments?: number;
1595
+ }
1596
+
1597
+ export function validateZhudaTorus(options: ZhudaTorusOptions): string[] {
1598
+ const o = options ?? ({} as ZhudaTorusOptions);
1599
+ const issues: string[] = [];
1600
+ const radiusOk = typeof o.radius === 'number' && o.radius > 0 && o.radius <= 1000;
1601
+ if (!radiusOk) issues.push('radius: use a positive number of metres');
1602
+ if (!(typeof o.tube === 'number' && o.tube > 0))
1603
+ issues.push('tube: use a positive number of metres');
1604
+ else if (radiusOk && o.tube >= o.radius)
1605
+ issues.push('tube: it must be less than radius, or the ring closes up its hole');
1606
+ const around = o.radialSegments ?? 16,
1607
+ along = o.tubularSegments ?? 48;
1608
+ if (!(Number.isInteger(around) && around >= 3 && around <= 64))
1609
+ issues.push('radialSegments: use a whole number 3..64');
1610
+ if (!(Number.isInteger(along) && along >= 3 && along <= 256))
1611
+ issues.push('tubularSegments: use a whole number 3..256');
1612
+ return issues;
1613
+ }
1614
+
1615
+ /** A ring-shaped tube lying flat about +Y, centred on the origin: rings, tyres, handles, haloes. */
1616
+ export function createZhudaTorusMesh(options: ZhudaTorusOptions): MeshData {
1617
+ const issues = validateZhudaTorus(options);
1618
+ if (issues.length) throw Error('Invalid torus: ' + issues.join('; '));
1619
+ const { radius, tube } = options,
1620
+ around = options.radialSegments ?? 16,
1621
+ along = options.tubularSegments ?? 48,
1622
+ writer = new MeshWriter(),
1623
+ grid: number[][] = [];
1624
+ const normal = (theta: number, phi: number): ZhudaVec3 => [
1625
+ Math.cos(phi) * Math.cos(theta),
1626
+ Math.sin(phi),
1627
+ Math.cos(phi) * Math.sin(theta),
1628
+ ];
1629
+ for (let i = 0; i <= along; i++) {
1630
+ const theta = (i / along) * Math.PI * 2,
1631
+ row: number[] = [];
1632
+ for (let j = 0; j <= around; j++) {
1633
+ const phi = (j / around) * Math.PI * 2,
1634
+ n = normal(theta, phi);
1635
+ row.push(
1636
+ writer.vertex(
1637
+ [
1638
+ radius * Math.cos(theta) + tube * n[0],
1639
+ tube * n[1],
1640
+ radius * Math.sin(theta) + tube * n[2],
1641
+ ],
1642
+ n,
1643
+ theta * radius,
1644
+ phi * tube,
1645
+ ),
1646
+ );
1647
+ }
1648
+ grid.push(row);
1649
+ }
1650
+ for (let i = 0; i < along; i++)
1651
+ for (let j = 0; j < around; j++) {
1652
+ const outward = normal(((i + 0.5) / along) * Math.PI * 2, ((j + 0.5) / around) * Math.PI * 2);
1653
+ writer.triangle(grid[i][j], grid[i][j + 1], grid[i + 1][j + 1], outward);
1654
+ writer.triangle(grid[i][j], grid[i + 1][j + 1], grid[i + 1][j], outward);
1655
+ }
1656
+ return writer.finish();
1657
+ }
1658
+
1659
+ export interface ZhudaRingOptions {
1660
+ outerRadius: number;
1661
+ /** The hole's radius; 0 (default) gives a disc. */
1662
+ innerRadius?: number;
1663
+ /** Steps around, 3..256 (default 48). */
1664
+ segments?: number;
1665
+ /**
1666
+ * Omitted: a flat ring at y = 0 facing +Y, seen from above only (a marker
1667
+ * on the ground). Given: a solid washer this many metres thick, from y = 0 up.
1668
+ */
1669
+ thickness?: number;
1670
+ }
1671
+
1672
+ export function validateZhudaRing(options: ZhudaRingOptions): string[] {
1673
+ const o = options ?? ({} as ZhudaRingOptions);
1674
+ const issues: string[] = [];
1675
+ const outerOk = typeof o.outerRadius === 'number' && o.outerRadius > 0 && o.outerRadius <= 1000;
1676
+ if (!outerOk) issues.push('outerRadius: use a positive number of metres');
1677
+ const inner = o.innerRadius ?? 0;
1678
+ if (!(typeof inner === 'number' && inner >= 0)) issues.push('innerRadius: use 0 or more metres');
1679
+ else if (outerOk && inner >= o.outerRadius)
1680
+ issues.push('innerRadius: it must be less than outerRadius');
1681
+ const segments = o.segments ?? 48;
1682
+ if (!(Number.isInteger(segments) && segments >= 3 && segments <= 256))
1683
+ issues.push('segments: use a whole number 3..256');
1684
+ if (
1685
+ o.thickness !== undefined &&
1686
+ !(typeof o.thickness === 'number' && o.thickness > 0 && o.thickness <= 1000)
1687
+ )
1688
+ issues.push('thickness: use a positive number of metres');
1689
+ return issues;
1690
+ }
1691
+
1692
+ /** A flat ring or disc, or with `thickness` a washer or coin, about +Y on the origin. */
1693
+ export function createZhudaRingMesh(options: ZhudaRingOptions): MeshData {
1694
+ const issues = validateZhudaRing(options);
1695
+ if (issues.length) throw Error('Invalid ring: ' + issues.join('; '));
1696
+ const outer = options.outerRadius,
1697
+ inner = options.innerRadius ?? 0,
1698
+ segments = options.segments ?? 48,
1699
+ writer = new MeshWriter();
1700
+ const angleOf = (j: number) => (j / segments) * Math.PI * 2;
1701
+ const face = (y: number, up: 1 | -1) => {
1702
+ const outward: ZhudaVec3 = [0, up, 0];
1703
+ const circle = (r: number) =>
1704
+ Array.from({ length: segments + 1 }, (_, j) => {
1705
+ const x = r * Math.cos(angleOf(j)),
1706
+ z = r * Math.sin(angleOf(j));
1707
+ return writer.vertex([x, y, z], outward, x, z);
1708
+ });
1709
+ const rim = circle(outer);
1710
+ if (inner > 0) {
1711
+ const hole = circle(inner);
1712
+ for (let j = 0; j < segments; j++) {
1713
+ writer.triangle(hole[j], rim[j], rim[j + 1], outward);
1714
+ writer.triangle(hole[j], rim[j + 1], hole[j + 1], outward);
1715
+ }
1716
+ } else {
1717
+ const centre = writer.vertex([0, y, 0], outward, 0, 0);
1718
+ for (let j = 0; j < segments; j++) writer.triangle(centre, rim[j], rim[j + 1], outward);
1719
+ }
1720
+ };
1721
+ const wall = (r: number, height: number, facing: 1 | -1) => {
1722
+ const column = (j: number, y: number) => {
1723
+ const a = angleOf(j),
1724
+ n: ZhudaVec3 = [facing * Math.cos(a), 0, facing * Math.sin(a)];
1725
+ return writer.vertex([r * Math.cos(a), y, r * Math.sin(a)], n, a * r, y);
1726
+ };
1727
+ const bottom = Array.from({ length: segments + 1 }, (_, j) => column(j, 0)),
1728
+ top = Array.from({ length: segments + 1 }, (_, j) => column(j, height));
1729
+ for (let j = 0; j < segments; j++) {
1730
+ const a = angleOf(j + 0.5),
1731
+ outward: ZhudaVec3 = [facing * Math.cos(a), 0, facing * Math.sin(a)];
1732
+ writer.triangle(bottom[j], bottom[j + 1], top[j + 1], outward);
1733
+ writer.triangle(bottom[j], top[j + 1], top[j], outward);
1734
+ }
1735
+ };
1736
+ if (options.thickness === undefined) face(0, 1);
1737
+ else {
1738
+ face(options.thickness, 1);
1739
+ face(0, -1);
1740
+ wall(outer, options.thickness, 1);
1741
+ if (inner > 0) wall(inner, options.thickness, -1);
1742
+ }
1743
+ return writer.finish();
1744
+ }
1745
+
1746
+ export interface ZhudaMeshTransform {
1747
+ position?: ZhudaVec3;
1748
+ /** Rotation about +Y, applied after pitch and roll. A positive angle turns +Z towards +X. */
1749
+ yawDeg?: number;
1750
+ /**
1751
+ * Rotation about +X. A positive angle turns +Y towards +Z: a piece standing
1752
+ * upright leans its top towards +Z and at 90 lies along +Z; a negative
1753
+ * angle leans it towards -Z.
1754
+ */
1755
+ pitchDeg?: number;
1756
+ /** Rotation about +Z, applied first. A positive angle turns +X towards +Y. */
1757
+ rollDeg?: number;
1758
+ /** Uniform positive scale, applied before rotation. */
1759
+ scale?: number;
1760
+ }
1761
+
1762
+ /** Returns a transformed copy (scale, then roll, pitch, yaw, then translation). UVs are unchanged. */
1763
+ export function transformZhudaMesh(data: MeshData, transform: ZhudaMeshTransform): MeshData {
1764
+ const position = transform.position ? vec(transform.position, 'position') : [0, 0, 0],
1765
+ scale = transform.scale ?? 1;
1766
+ if (!Number.isFinite(scale) || scale <= 0) throw Error('scale must be a positive number');
1767
+ const angles = [transform.pitchDeg ?? 0, transform.yawDeg ?? 0, transform.rollDeg ?? 0];
1768
+ if (!angles.every(Number.isFinite)) throw Error('rotation angles must be finite');
1769
+ const [px, py, pz] = angles.map((deg) => (deg * Math.PI) / 180),
1770
+ cx = Math.cos(px),
1771
+ sx = Math.sin(px),
1772
+ cy = Math.cos(py),
1773
+ sy = Math.sin(py),
1774
+ cz = Math.cos(pz),
1775
+ sz = Math.sin(pz);
1776
+ // R = Ry * Rx * Rz
1777
+ const m = [
1778
+ cy * cz + sy * sx * sz,
1779
+ -cy * sz + sy * sx * cz,
1780
+ sy * cx,
1781
+ cx * sz,
1782
+ cx * cz,
1783
+ -sx,
1784
+ -sy * cz + cy * sx * sz,
1785
+ sy * sz + cy * sx * cz,
1786
+ cy * cx,
1787
+ ];
1788
+ const rotate = (
1789
+ source: Float32Array,
1790
+ i: number,
1791
+ k: number,
1792
+ target: Float32Array,
1793
+ add: number[],
1794
+ ) => {
1795
+ const x = source[i] * k,
1796
+ y = source[i + 1] * k,
1797
+ z = source[i + 2] * k;
1798
+ target[i] = m[0] * x + m[1] * y + m[2] * z + add[0];
1799
+ target[i + 1] = m[3] * x + m[4] * y + m[5] * z + add[1];
1800
+ target[i + 2] = m[6] * x + m[7] * y + m[8] * z + add[2];
1801
+ };
1802
+ const positions = new Float32Array(data.positions.length),
1803
+ normals = data.normals ? new Float32Array(data.normals.length) : undefined;
1804
+ for (let i = 0; i < positions.length; i += 3) {
1805
+ rotate(data.positions, i, scale, positions, position);
1806
+ if (normals && data.normals) rotate(data.normals, i, 1, normals, [0, 0, 0]);
1807
+ }
1808
+ return {
1809
+ positions,
1810
+ normals,
1811
+ uvs: data.uvs ? new Float32Array(data.uvs) : undefined,
1812
+ indices: data.indices.slice(),
1813
+ };
1814
+ }
1815
+
1816
+ /**
1817
+ * Concatenates meshes that share one material. Output is split into chunks of
1818
+ * at most `maxVertices` so each chunk fits one `ZhudaDynamicMesh`.
1819
+ */
1820
+ export function mergeZhudaMeshes(parts: readonly MeshData[], maxVertices = 65535): MeshData[] {
1821
+ if (!Number.isInteger(maxVertices) || maxVertices < 3 || maxVertices > 65535)
1822
+ throw Error('maxVertices must be an integer 3..65535');
1823
+ const chunks: MeshData[] = [];
1824
+ let group: MeshData[] = [],
1825
+ vertices = 0;
1826
+ const flush = () => {
1827
+ if (!group.length) return;
1828
+ const indexCount = group.reduce((sum, part) => sum + part.indices.length, 0),
1829
+ positions = new Float32Array(vertices * 3),
1830
+ normals = new Float32Array(vertices * 3),
1831
+ uvs = new Float32Array(vertices * 2),
1832
+ indices = new Uint16Array(indexCount);
1833
+ let v = 0,
1834
+ k = 0;
1835
+ for (const part of group) {
1836
+ const count = part.positions.length / 3;
1837
+ positions.set(part.positions, v * 3);
1838
+ if (part.normals) normals.set(part.normals, v * 3);
1839
+ else for (let i = 0; i < count; i++) normals[(v + i) * 3 + 1] = 1;
1840
+ if (part.uvs) uvs.set(part.uvs, v * 2);
1841
+ for (let i = 0; i < part.indices.length; i++) indices[k++] = part.indices[i] + v;
1842
+ v += count;
1843
+ }
1844
+ chunks.push({ positions, normals, uvs, indices });
1845
+ group = [];
1846
+ vertices = 0;
1847
+ };
1848
+ for (const part of parts) {
1849
+ const count = part.positions.length / 3;
1850
+ if (!Number.isInteger(count) || count < 3 || part.indices.length % 3)
1851
+ throw Error('Cannot merge an invalid mesh');
1852
+ if (count > maxVertices) throw Error('A single part exceeds maxVertices; split it first');
1853
+ if (vertices + count > maxVertices) flush();
1854
+ group.push(part);
1855
+ vertices += count;
1856
+ }
1857
+ flush();
1858
+ return chunks;
1859
+ }
1860
+
1861
+ export function computeZhudaMeshBounds(data: MeshData): { min: ZhudaVec3; max: ZhudaVec3 } {
1862
+ const min: ZhudaVec3 = [Infinity, Infinity, Infinity],
1863
+ max: ZhudaVec3 = [-Infinity, -Infinity, -Infinity];
1864
+ for (let i = 0; i < data.positions.length; i += 3)
1865
+ for (let a = 0; a < 3; a++) {
1866
+ const value = data.positions[i + a];
1867
+ if (value < min[a]) min[a] = value;
1868
+ if (value > max[a]) max[a] = value;
1869
+ }
1870
+ return { min, max };
1871
+ }