@egret/engine 0.0.0-stage → 7.0.2

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 (601) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +21 -0
  3. package/README.md +101 -2
  4. package/README.zh-CN.md +102 -0
  5. package/dist/Engine.d.ts +77 -0
  6. package/dist/Engine.js +469 -0
  7. package/dist/animation/index.d.ts +6 -0
  8. package/dist/animation/index.js +5 -0
  9. package/dist/animation/sequenceController.d.ts +22 -0
  10. package/dist/animation/sequenceController.js +618 -0
  11. package/dist/animation/sequenceCoordinator.d.ts +26 -0
  12. package/dist/animation/sequenceCoordinator.js +161 -0
  13. package/dist/animation/sequenceInput.d.ts +25 -0
  14. package/dist/animation/sequenceInput.js +108 -0
  15. package/dist/animation/sequenceMath.d.ts +35 -0
  16. package/dist/animation/sequenceMath.js +164 -0
  17. package/dist/animation/sequenceResetSlot.d.ts +7 -0
  18. package/dist/animation/sequenceResetSlot.js +367 -0
  19. package/dist/animation/sequenceResetTypes.d.ts +48 -0
  20. package/dist/animation/sequenceResetTypes.js +2 -0
  21. package/dist/animation/types.d.ts +75 -0
  22. package/dist/animation/types.js +2 -0
  23. package/dist/assetBundle.d.ts +54 -0
  24. package/dist/assetBundle.js +173 -0
  25. package/dist/assetGroup.d.ts +17 -0
  26. package/dist/assetGroup.js +332 -0
  27. package/dist/assetGroupTypes.d.ts +17 -0
  28. package/dist/assetGroupTypes.js +2 -0
  29. package/dist/audio.d.ts +4 -0
  30. package/dist/audio.js +4 -0
  31. package/dist/audioCues.d.ts +47 -0
  32. package/dist/audioCues.js +127 -0
  33. package/dist/audioMixer.d.ts +7 -0
  34. package/dist/audioMixer.js +785 -0
  35. package/dist/audioTypes.d.ts +246 -0
  36. package/dist/audioTypes.js +3 -0
  37. package/dist/bitmapBindingTicket.d.ts +24 -0
  38. package/dist/bitmapBindingTicket.js +215 -0
  39. package/dist/bitmapButton.d.ts +4 -0
  40. package/dist/bitmapButton.js +407 -0
  41. package/dist/bitmapButtonTypes.d.ts +31 -0
  42. package/dist/bitmapButtonTypes.js +2 -0
  43. package/dist/bitmapFontParse.d.ts +10 -0
  44. package/dist/bitmapFontParse.js +246 -0
  45. package/dist/bitmapFontTypes.d.ts +112 -0
  46. package/dist/bitmapFontTypes.js +2 -0
  47. package/dist/bitmapResourceSlot.d.ts +26 -0
  48. package/dist/bitmapResourceSlot.js +340 -0
  49. package/dist/bitmapText.d.ts +6 -0
  50. package/dist/bitmapText.js +320 -0
  51. package/dist/bitmapfont.d.ts +5 -0
  52. package/dist/bitmapfont.js +5 -0
  53. package/dist/camera.d.ts +4 -0
  54. package/dist/camera.js +4 -0
  55. package/dist/camera2d.d.ts +7 -0
  56. package/dist/camera2d.js +419 -0
  57. package/dist/cameraTypes.d.ts +126 -0
  58. package/dist/cameraTypes.js +2 -0
  59. package/dist/closeHost.d.ts +4 -0
  60. package/dist/closeHost.js +66 -0
  61. package/dist/contentArtifacts.d.ts +12 -0
  62. package/dist/contentArtifacts.js +153 -0
  63. package/dist/contentAtlasPages.d.ts +13 -0
  64. package/dist/contentAtlasPages.js +117 -0
  65. package/dist/contentLoader.d.ts +6 -0
  66. package/dist/contentLoader.js +327 -0
  67. package/dist/contentTypes.d.ts +35 -0
  68. package/dist/contentTypes.js +2 -0
  69. package/dist/createEngine.d.ts +7 -0
  70. package/dist/createEngine.js +25 -0
  71. package/dist/createEngineForProfile.d.ts +5 -0
  72. package/dist/createEngineForProfile.js +45 -0
  73. package/dist/displaySubtreeTicket.d.ts +25 -0
  74. package/dist/displaySubtreeTicket.js +76 -0
  75. package/dist/fontContentLoader.d.ts +6 -0
  76. package/dist/fontContentLoader.js +298 -0
  77. package/dist/fontContentTypes.d.ts +39 -0
  78. package/dist/fontContentTypes.js +2 -0
  79. package/dist/gameClock.d.ts +24 -0
  80. package/dist/gameClock.js +246 -0
  81. package/dist/gameClockTypes.d.ts +37 -0
  82. package/dist/gameClockTypes.js +2 -0
  83. package/dist/gameRun.d.ts +53 -0
  84. package/dist/gameRun.js +97 -0
  85. package/dist/gamepad.d.ts +5 -0
  86. package/dist/gamepad.js +5 -0
  87. package/dist/gamepadInput.d.ts +4 -0
  88. package/dist/gamepadInput.js +281 -0
  89. package/dist/gamepadKeys.d.ts +4 -0
  90. package/dist/gamepadKeys.js +205 -0
  91. package/dist/gamepadTypes.d.ts +142 -0
  92. package/dist/gamepadTypes.js +3 -0
  93. package/dist/hover.d.ts +4 -0
  94. package/dist/hover.js +4 -0
  95. package/dist/hoverInput.d.ts +8 -0
  96. package/dist/hoverInput.js +503 -0
  97. package/dist/hoverInputTypes.d.ts +129 -0
  98. package/dist/hoverInputTypes.js +2 -0
  99. package/dist/index.d.ts +52 -0
  100. package/dist/index.js +21 -0
  101. package/dist/input.d.ts +6 -0
  102. package/dist/input.js +5 -0
  103. package/dist/inputActions.d.ts +86 -0
  104. package/dist/inputActions.js +330 -0
  105. package/dist/internal/contentFactoryState.d.ts +19 -0
  106. package/dist/internal/contentFactoryState.js +42 -0
  107. package/dist/internal/mouseInputFactoryState.d.ts +57 -0
  108. package/dist/internal/mouseInputFactoryState.js +125 -0
  109. package/dist/internal/sequenceControllerFactoryState.d.ts +42 -0
  110. package/dist/internal/sequenceControllerFactoryState.js +98 -0
  111. package/dist/internal/textFieldFactoryState.d.ts +11 -0
  112. package/dist/internal/textFieldFactoryState.js +32 -0
  113. package/dist/internal/texturedFrameState.d.ts +4 -0
  114. package/dist/internal/texturedFrameState.js +12 -0
  115. package/dist/internal/uiFactoryState.d.ts +68 -0
  116. package/dist/internal/uiFactoryState.js +173 -0
  117. package/dist/internal/uiRootState.d.ts +67 -0
  118. package/dist/internal/uiRootState.js +103 -0
  119. package/dist/keyboardInput.d.ts +13 -0
  120. package/dist/keyboardInput.js +579 -0
  121. package/dist/keyboardInputTypes.d.ts +93 -0
  122. package/dist/keyboardInputTypes.js +2 -0
  123. package/dist/kit.d.ts +16 -0
  124. package/dist/kit.js +10 -0
  125. package/dist/layout.d.ts +5 -0
  126. package/dist/layout.js +132 -0
  127. package/dist/layoutTypes.d.ts +28 -0
  128. package/dist/layoutTypes.js +2 -0
  129. package/dist/manifestAssetGroup.d.ts +10 -0
  130. package/dist/manifestAssetGroup.js +477 -0
  131. package/dist/manifestResourceState.d.ts +17 -0
  132. package/dist/manifestResourceState.js +58 -0
  133. package/dist/manifestResourceTypes.d.ts +26 -0
  134. package/dist/manifestResourceTypes.js +2 -0
  135. package/dist/motion.d.ts +35 -0
  136. package/dist/motion.js +206 -0
  137. package/dist/mouseInput.d.ts +102 -0
  138. package/dist/mouseInput.js +562 -0
  139. package/dist/mouseInputTypes.d.ts +41 -0
  140. package/dist/mouseInputTypes.js +2 -0
  141. package/dist/paragraph.d.ts +8 -0
  142. package/dist/paragraph.js +7 -0
  143. package/dist/particleEmitter.d.ts +4 -0
  144. package/dist/particleEmitter.js +300 -0
  145. package/dist/particleTypes.d.ts +115 -0
  146. package/dist/particleTypes.js +2 -0
  147. package/dist/particles.d.ts +4 -0
  148. package/dist/particles.js +4 -0
  149. package/dist/physics2d.d.ts +4 -0
  150. package/dist/physics2d.js +4 -0
  151. package/dist/physics2dGeometry.d.ts +39 -0
  152. package/dist/physics2dGeometry.js +230 -0
  153. package/dist/physics2dTypes.d.ts +221 -0
  154. package/dist/physics2dTypes.js +2 -0
  155. package/dist/physics2dWorld.d.ts +4 -0
  156. package/dist/physics2dWorld.js +823 -0
  157. package/dist/pointerInput.d.ts +12 -0
  158. package/dist/pointerInput.js +300 -0
  159. package/dist/pointerInputTypes.d.ts +41 -0
  160. package/dist/pointerInputTypes.js +2 -0
  161. package/dist/random.d.ts +30 -0
  162. package/dist/random.js +131 -0
  163. package/dist/rendering/ownedPreparationFailure2D.d.ts +5 -0
  164. package/dist/rendering/ownedPreparationFailure2D.js +33 -0
  165. package/dist/rendering/packSharedDraws2D.d.ts +50 -0
  166. package/dist/rendering/packSharedDraws2D.js +379 -0
  167. package/dist/rendering/packedOrientation.d.ts +4 -0
  168. package/dist/rendering/packedOrientation.js +7 -0
  169. package/dist/rendering/prepareRectangles2D.d.ts +92 -0
  170. package/dist/rendering/prepareRectangles2D.js +481 -0
  171. package/dist/rendering/prepareSharedDraws2D.d.ts +90 -0
  172. package/dist/rendering/prepareSharedDraws2D.js +663 -0
  173. package/dist/rendering/preparedBatches2D.d.ts +17 -0
  174. package/dist/rendering/preparedBatches2D.js +30 -0
  175. package/dist/rendering/preparedSharedBatches2D.d.ts +44 -0
  176. package/dist/rendering/preparedSharedBatches2D.js +247 -0
  177. package/dist/rendering/sharedColor2D.d.ts +52 -0
  178. package/dist/rendering/sharedColor2D.js +191 -0
  179. package/dist/rendering/sharedEffects2D.d.ts +23 -0
  180. package/dist/rendering/sharedEffects2D.js +126 -0
  181. package/dist/rendering/sharedPreparationRetention2D.d.ts +120 -0
  182. package/dist/rendering/sharedPreparationRetention2D.js +357 -0
  183. package/dist/rendering/srgbBytes.d.ts +3 -0
  184. package/dist/rendering/srgbBytes.js +64 -0
  185. package/dist/rendering/texturedPreparationBudget2D.d.ts +31 -0
  186. package/dist/rendering/texturedPreparationBudget2D.js +81 -0
  187. package/dist/rendering/world3d/Animation3D.d.ts +104 -0
  188. package/dist/rendering/world3d/Animation3D.js +384 -0
  189. package/dist/rendering/world3d/AnimationTexture3D.d.ts +163 -0
  190. package/dist/rendering/world3d/AnimationTexture3D.js +603 -0
  191. package/dist/rendering/world3d/Batch3D.d.ts +75 -0
  192. package/dist/rendering/world3d/Batch3D.js +332 -0
  193. package/dist/rendering/world3d/Camera3D.d.ts +62 -0
  194. package/dist/rendering/world3d/Camera3D.js +132 -0
  195. package/dist/rendering/world3d/Character3D.d.ts +127 -0
  196. package/dist/rendering/world3d/Character3D.js +305 -0
  197. package/dist/rendering/world3d/Cloth3D.d.ts +130 -0
  198. package/dist/rendering/world3d/Cloth3D.js +503 -0
  199. package/dist/rendering/world3d/Compression3D.d.ts +22 -0
  200. package/dist/rendering/world3d/Compression3D.js +703 -0
  201. package/dist/rendering/world3d/Curves3D.d.ts +119 -0
  202. package/dist/rendering/world3d/Curves3D.js +460 -0
  203. package/dist/rendering/world3d/Decal3D.d.ts +43 -0
  204. package/dist/rendering/world3d/Decal3D.js +242 -0
  205. package/dist/rendering/world3d/Export3D.d.ts +13 -0
  206. package/dist/rendering/world3d/Export3D.js +263 -0
  207. package/dist/rendering/world3d/Geometry3D.d.ts +95 -0
  208. package/dist/rendering/world3d/Geometry3D.js +335 -0
  209. package/dist/rendering/world3d/GeometryBuilder3D.d.ts +53 -0
  210. package/dist/rendering/world3d/GeometryBuilder3D.js +196 -0
  211. package/dist/rendering/world3d/Gizmo3D.d.ts +80 -0
  212. package/dist/rendering/world3d/Gizmo3D.js +514 -0
  213. package/dist/rendering/world3d/Gltf3D.d.ts +73 -0
  214. package/dist/rendering/world3d/Gltf3D.js +996 -0
  215. package/dist/rendering/world3d/GpuParticles3D.d.ts +109 -0
  216. package/dist/rendering/world3d/GpuParticles3D.js +401 -0
  217. package/dist/rendering/world3d/Hdr3D.d.ts +35 -0
  218. package/dist/rendering/world3d/Hdr3D.js +191 -0
  219. package/dist/rendering/world3d/Jelly3D.d.ts +77 -0
  220. package/dist/rendering/world3d/Jelly3D.js +267 -0
  221. package/dist/rendering/world3d/Ktx23D.d.ts +30 -0
  222. package/dist/rendering/world3d/Ktx23D.js +112 -0
  223. package/dist/rendering/world3d/Light3D.d.ts +84 -0
  224. package/dist/rendering/world3d/Light3D.js +97 -0
  225. package/dist/rendering/world3d/Material3D.d.ts +253 -0
  226. package/dist/rendering/world3d/Material3D.js +269 -0
  227. package/dist/rendering/world3d/Mesh3D.d.ts +112 -0
  228. package/dist/rendering/world3d/Mesh3D.js +337 -0
  229. package/dist/rendering/world3d/Meshopt3D.d.ts +15 -0
  230. package/dist/rendering/world3d/Meshopt3D.js +276 -0
  231. package/dist/rendering/world3d/Navigation3D.d.ts +213 -0
  232. package/dist/rendering/world3d/Navigation3D.js +1043 -0
  233. package/dist/rendering/world3d/Object3D.d.ts +118 -0
  234. package/dist/rendering/world3d/Object3D.js +315 -0
  235. package/dist/rendering/world3d/Particles3D.d.ts +90 -0
  236. package/dist/rendering/world3d/Particles3D.js +277 -0
  237. package/dist/rendering/world3d/Physics3D.d.ts +430 -0
  238. package/dist/rendering/world3d/Physics3D.js +2840 -0
  239. package/dist/rendering/world3d/PhysicsHull3D.d.ts +37 -0
  240. package/dist/rendering/world3d/PhysicsHull3D.js +229 -0
  241. package/dist/rendering/world3d/PhysicsMesh3D.d.ts +41 -0
  242. package/dist/rendering/world3d/PhysicsMesh3D.js +340 -0
  243. package/dist/rendering/world3d/PhysicsQueries3D.d.ts +24 -0
  244. package/dist/rendering/world3d/PhysicsQueries3D.js +323 -0
  245. package/dist/rendering/world3d/PhysicsRemote3D.d.ts +179 -0
  246. package/dist/rendering/world3d/PhysicsRemote3D.js +536 -0
  247. package/dist/rendering/world3d/Ragdoll3D.d.ts +74 -0
  248. package/dist/rendering/world3d/Ragdoll3D.js +202 -0
  249. package/dist/rendering/world3d/Raycast3D.d.ts +40 -0
  250. package/dist/rendering/world3d/Raycast3D.js +247 -0
  251. package/dist/rendering/world3d/RenderList3D.d.ts +91 -0
  252. package/dist/rendering/world3d/RenderList3D.js +502 -0
  253. package/dist/rendering/world3d/Retarget3D.d.ts +21 -0
  254. package/dist/rendering/world3d/Retarget3D.js +185 -0
  255. package/dist/rendering/world3d/Rig3D.d.ts +57 -0
  256. package/dist/rendering/world3d/Rig3D.js +219 -0
  257. package/dist/rendering/world3d/Sdf3D.d.ts +181 -0
  258. package/dist/rendering/world3d/Sdf3D.js +1136 -0
  259. package/dist/rendering/world3d/SdfLod3D.d.ts +17 -0
  260. package/dist/rendering/world3d/SdfLod3D.js +56 -0
  261. package/dist/rendering/world3d/Serialize3D.d.ts +66 -0
  262. package/dist/rendering/world3d/Serialize3D.js +629 -0
  263. package/dist/rendering/world3d/Simplify3D.d.ts +29 -0
  264. package/dist/rendering/world3d/Simplify3D.js +426 -0
  265. package/dist/rendering/world3d/StateMachine3D.d.ts +87 -0
  266. package/dist/rendering/world3d/StateMachine3D.js +280 -0
  267. package/dist/rendering/world3d/SurfaceMaterial3D.d.ts +26 -0
  268. package/dist/rendering/world3d/SurfaceMaterial3D.js +138 -0
  269. package/dist/rendering/world3d/Terrain3D.d.ts +248 -0
  270. package/dist/rendering/world3d/Terrain3D.js +827 -0
  271. package/dist/rendering/world3d/Text3D.d.ts +95 -0
  272. package/dist/rendering/world3d/Text3D.js +560 -0
  273. package/dist/rendering/world3d/Texture3D.d.ts +148 -0
  274. package/dist/rendering/world3d/Texture3D.js +270 -0
  275. package/dist/rendering/world3d/Trail3D.d.ts +62 -0
  276. package/dist/rendering/world3d/Trail3D.js +195 -0
  277. package/dist/rendering/world3d/Vehicle3D.d.ts +102 -0
  278. package/dist/rendering/world3d/Vehicle3D.js +228 -0
  279. package/dist/rendering/world3d/VolumetricFog3D.d.ts +57 -0
  280. package/dist/rendering/world3d/VolumetricFog3D.js +230 -0
  281. package/dist/rendering/world3d/Water3D.d.ts +165 -0
  282. package/dist/rendering/world3d/Water3D.js +607 -0
  283. package/dist/rendering/world3d/core3D.d.ts +36 -0
  284. package/dist/rendering/world3d/core3D.js +99 -0
  285. package/dist/rendering/world3d/debug3D.d.ts +69 -0
  286. package/dist/rendering/world3d/debug3D.js +262 -0
  287. package/dist/rendering/world3d/decoders3D.d.ts +42 -0
  288. package/dist/rendering/world3d/decoders3D.js +133 -0
  289. package/dist/rendering/world3d/geometries3D.d.ts +111 -0
  290. package/dist/rendering/world3d/geometries3D.js +610 -0
  291. package/dist/rendering/world3d/helpers3D.d.ts +67 -0
  292. package/dist/rendering/world3d/helpers3D.js +178 -0
  293. package/dist/rendering/world3d/index.d.ts +103 -0
  294. package/dist/rendering/world3d/index.js +62 -0
  295. package/dist/rendering/world3d/math3D.d.ts +114 -0
  296. package/dist/rendering/world3d/math3D.js +675 -0
  297. package/dist/rendering/world3d/primitives3D.d.ts +67 -0
  298. package/dist/rendering/world3d/primitives3D.js +197 -0
  299. package/dist/resources.d.ts +8 -0
  300. package/dist/resources.js +6 -0
  301. package/dist/saveStore.d.ts +71 -0
  302. package/dist/saveStore.js +182 -0
  303. package/dist/semanticList.d.ts +126 -0
  304. package/dist/semanticList.js +175 -0
  305. package/dist/semanticTree.d.ts +10 -0
  306. package/dist/semanticTree.js +539 -0
  307. package/dist/semanticTypes.d.ts +175 -0
  308. package/dist/semanticTypes.js +2 -0
  309. package/dist/semantics.d.ts +6 -0
  310. package/dist/semantics.js +5 -0
  311. package/dist/sequencePlayback.d.ts +19 -0
  312. package/dist/sequencePlayback.js +391 -0
  313. package/dist/sequencePlaybackTypes.d.ts +24 -0
  314. package/dist/sequencePlaybackTypes.js +2 -0
  315. package/dist/sequenceTargetClaims.d.ts +10 -0
  316. package/dist/sequenceTargetClaims.js +31 -0
  317. package/dist/srgbBytes.d.ts +3 -0
  318. package/dist/srgbBytes.js +64 -0
  319. package/dist/startEngine.d.ts +5 -0
  320. package/dist/startEngine.js +31 -0
  321. package/dist/surfaceOwnership.d.ts +3 -0
  322. package/dist/surfaceOwnership.js +15 -0
  323. package/dist/text.d.ts +24 -0
  324. package/dist/text.js +4 -0
  325. package/dist/textField.d.ts +6 -0
  326. package/dist/textField.js +6 -0
  327. package/dist/textTable.d.ts +41 -0
  328. package/dist/textTable.js +114 -0
  329. package/dist/tileMapDraw.d.ts +7 -0
  330. package/dist/tileMapDraw.js +310 -0
  331. package/dist/tiledImport.d.ts +9 -0
  332. package/dist/tiledImport.js +357 -0
  333. package/dist/tilemap.d.ts +5 -0
  334. package/dist/tilemap.js +5 -0
  335. package/dist/tilemapTypes.d.ts +182 -0
  336. package/dist/tilemapTypes.js +2 -0
  337. package/dist/timer.d.ts +34 -0
  338. package/dist/timer.js +119 -0
  339. package/dist/tween.d.ts +15 -0
  340. package/dist/tween.js +329 -0
  341. package/dist/tweenTypes.d.ts +31 -0
  342. package/dist/tweenTypes.js +2 -0
  343. package/dist/ui/controlKeys.d.ts +113 -0
  344. package/dist/ui/controlKeys.js +250 -0
  345. package/dist/ui/controls.d.ts +58 -0
  346. package/dist/ui/controls.js +290 -0
  347. package/dist/ui/editing.d.ts +3 -0
  348. package/dist/ui/editing.js +3 -0
  349. package/dist/ui/index.d.ts +19 -0
  350. package/dist/ui/index.js +11 -0
  351. package/dist/ui/keyboardFocus.d.ts +32 -0
  352. package/dist/ui/keyboardFocus.js +146 -0
  353. package/dist/ui/layerTypes.d.ts +98 -0
  354. package/dist/ui/layerTypes.js +2 -0
  355. package/dist/ui/layers.d.ts +4 -0
  356. package/dist/ui/layers.js +586 -0
  357. package/dist/ui/leafContent.d.ts +21 -0
  358. package/dist/ui/leafContent.js +136 -0
  359. package/dist/ui/rangeControl.d.ts +53 -0
  360. package/dist/ui/rangeControl.js +168 -0
  361. package/dist/ui/scrollControl.d.ts +84 -0
  362. package/dist/ui/scrollControl.js +222 -0
  363. package/dist/ui/stateControls.d.ts +90 -0
  364. package/dist/ui/stateControls.js +331 -0
  365. package/dist/ui/textEditor.d.ts +54 -0
  366. package/dist/ui/textEditor.js +371 -0
  367. package/dist/ui/textEditorTypes.d.ts +85 -0
  368. package/dist/ui/textEditorTypes.js +2 -0
  369. package/dist/ui/textFieldControl.d.ts +100 -0
  370. package/dist/ui/textFieldControl.js +260 -0
  371. package/dist/ui/theme.d.ts +64 -0
  372. package/dist/ui/theme.js +166 -0
  373. package/dist/ui/types.d.ts +285 -0
  374. package/dist/ui/types.js +2 -0
  375. package/dist/ui/uiRoot.d.ts +4 -0
  376. package/dist/ui/uiRoot.js +1526 -0
  377. package/dist/ui/uiRootDisplay.d.ts +46 -0
  378. package/dist/ui/uiRootDisplay.js +148 -0
  379. package/dist/ui/uiRootModel.d.ts +114 -0
  380. package/dist/ui/uiRootModel.js +66 -0
  381. package/dist/ui/virtualList.d.ts +20 -0
  382. package/dist/ui/virtualList.js +105 -0
  383. package/dist/video.d.ts +4 -0
  384. package/dist/video.js +4 -0
  385. package/dist/videoPlayer.d.ts +4 -0
  386. package/dist/videoPlayer.js +215 -0
  387. package/dist/videoTypes.d.ts +77 -0
  388. package/dist/videoTypes.js +2 -0
  389. package/dist/web/BrowserAssetBundle.d.ts +41 -0
  390. package/dist/web/BrowserAssetBundle.js +136 -0
  391. package/dist/web/BrowserAudio.d.ts +74 -0
  392. package/dist/web/BrowserAudio.js +503 -0
  393. package/dist/web/BrowserFrameLoop.d.ts +69 -0
  394. package/dist/web/BrowserFrameLoop.js +125 -0
  395. package/dist/web/BrowserGame.d.ts +174 -0
  396. package/dist/web/BrowserGame.js +473 -0
  397. package/dist/web/BrowserGamepadInput.d.ts +38 -0
  398. package/dist/web/BrowserGamepadInput.js +134 -0
  399. package/dist/web/BrowserHoverInput.d.ts +25 -0
  400. package/dist/web/BrowserHoverInput.js +266 -0
  401. package/dist/web/BrowserImageTextures.d.ts +72 -0
  402. package/dist/web/BrowserImageTextures.js +169 -0
  403. package/dist/web/BrowserInputActions.d.ts +63 -0
  404. package/dist/web/BrowserInputActions.js +345 -0
  405. package/dist/web/BrowserKeyboardInput.d.ts +4 -0
  406. package/dist/web/BrowserKeyboardInput.js +223 -0
  407. package/dist/web/BrowserPlay.d.ts +417 -0
  408. package/dist/web/BrowserPlay.js +1648 -0
  409. package/dist/web/BrowserPointerInput.d.ts +4 -0
  410. package/dist/web/BrowserPointerInput.js +511 -0
  411. package/dist/web/BrowserPreferences.d.ts +10 -0
  412. package/dist/web/BrowserPreferences.js +39 -0
  413. package/dist/web/BrowserSemantics.d.ts +36 -0
  414. package/dist/web/BrowserSemantics.js +418 -0
  415. package/dist/web/BrowserStatsPanel.d.ts +56 -0
  416. package/dist/web/BrowserStatsPanel.js +143 -0
  417. package/dist/web/BrowserStorage.d.ts +35 -0
  418. package/dist/web/BrowserStorage.js +132 -0
  419. package/dist/web/BrowserTextEditor.d.ts +52 -0
  420. package/dist/web/BrowserTextEditor.js +839 -0
  421. package/dist/web/BrowserTextInput.d.ts +89 -0
  422. package/dist/web/BrowserTextInput.js +524 -0
  423. package/dist/web/BrowserTextureProvider.d.ts +58 -0
  424. package/dist/web/BrowserTextureProvider.js +935 -0
  425. package/dist/web/BrowserVideo.d.ts +40 -0
  426. package/dist/web/BrowserVideo.js +237 -0
  427. package/dist/web/CanvasKitFontSetProvider.d.ts +32 -0
  428. package/dist/web/CanvasKitFontSetProvider.js +215 -0
  429. package/dist/web/CanvasKitParagraphProvider.d.ts +173 -0
  430. package/dist/web/CanvasKitParagraphProvider.js +692 -0
  431. package/dist/web/CanvasTextRasterProvider.d.ts +4 -0
  432. package/dist/web/CanvasTextRasterProvider.js +312 -0
  433. package/dist/web/ManifestByteSource.d.ts +151 -0
  434. package/dist/web/ManifestByteSource.js +975 -0
  435. package/dist/web/ManifestFontContentLoader.d.ts +13 -0
  436. package/dist/web/ManifestFontContentLoader.js +30 -0
  437. package/dist/web/MouseInput.d.ts +4 -0
  438. package/dist/web/MouseInput.js +218 -0
  439. package/dist/web/WebGL2Host.d.ts +5 -0
  440. package/dist/web/WebGL2Host.js +2711 -0
  441. package/dist/web/WebGPUHost.d.ts +100 -0
  442. package/dist/web/WebGPUHost.js +3430 -0
  443. package/dist/web/audio.d.ts +3 -0
  444. package/dist/web/audio.js +4 -0
  445. package/dist/web/canvasKitParagraphGeometry.d.ts +27 -0
  446. package/dist/web/canvasKitParagraphGeometry.js +83 -0
  447. package/dist/web/editing.d.ts +5 -0
  448. package/dist/web/editing.js +4 -0
  449. package/dist/web/editorFonts.d.ts +54 -0
  450. package/dist/web/editorFonts.js +297 -0
  451. package/dist/web/game.d.ts +5 -0
  452. package/dist/web/game.js +6 -0
  453. package/dist/web/gamepad.d.ts +3 -0
  454. package/dist/web/gamepad.js +4 -0
  455. package/dist/web/hover.d.ts +3 -0
  456. package/dist/web/hover.js +4 -0
  457. package/dist/web/image.d.ts +4 -0
  458. package/dist/web/image.js +4 -0
  459. package/dist/web/imageProjections2D.d.ts +16 -0
  460. package/dist/web/imageProjections2D.js +2 -0
  461. package/dist/web/input.d.ts +6 -0
  462. package/dist/web/input.js +4 -0
  463. package/dist/web/keyboard.d.ts +3 -0
  464. package/dist/web/keyboard.js +3 -0
  465. package/dist/web/kit.d.ts +11 -0
  466. package/dist/web/kit.js +8 -0
  467. package/dist/web/loop.d.ts +3 -0
  468. package/dist/web/loop.js +4 -0
  469. package/dist/web/paragraph.d.ts +6 -0
  470. package/dist/web/paragraph.js +4 -0
  471. package/dist/web/play.d.ts +10 -0
  472. package/dist/web/play.js +9 -0
  473. package/dist/web/playMusic.d.ts +33 -0
  474. package/dist/web/playMusic.js +194 -0
  475. package/dist/web/playSfx.d.ts +36 -0
  476. package/dist/web/playSfx.js +112 -0
  477. package/dist/web/playTouch.d.ts +45 -0
  478. package/dist/web/playTouch.js +141 -0
  479. package/dist/web/playWidgets.d.ts +71 -0
  480. package/dist/web/playWidgets.js +431 -0
  481. package/dist/web/pointer.d.ts +3 -0
  482. package/dist/web/pointer.js +3 -0
  483. package/dist/web/prepareSharedSubmission2D.d.ts +27 -0
  484. package/dist/web/prepareSharedSubmission2D.js +63 -0
  485. package/dist/web/resources.d.ts +29 -0
  486. package/dist/web/resources.js +96 -0
  487. package/dist/web/semantics.d.ts +3 -0
  488. package/dist/web/semantics.js +4 -0
  489. package/dist/web/sharedDeviceSession.d.ts +64 -0
  490. package/dist/web/sharedDeviceSession.js +2 -0
  491. package/dist/web/sharedEffectShaders.d.ts +13 -0
  492. package/dist/web/sharedEffectShaders.js +181 -0
  493. package/dist/web/sharedFrameUpload.d.ts +25 -0
  494. package/dist/web/sharedFrameUpload.js +2 -0
  495. package/dist/web/sharedHostDeferred.d.ts +7 -0
  496. package/dist/web/sharedHostDeferred.js +12 -0
  497. package/dist/web/sharedRetentionLedger.d.ts +85 -0
  498. package/dist/web/sharedRetentionLedger.js +391 -0
  499. package/dist/web/sharedSubmissionStorage2D.d.ts +56 -0
  500. package/dist/web/sharedSubmissionStorage2D.js +73 -0
  501. package/dist/web/sharedWebGL2Arena.d.ts +40 -0
  502. package/dist/web/sharedWebGL2Arena.js +77 -0
  503. package/dist/web/sharedWebGL2Options.d.ts +33 -0
  504. package/dist/web/sharedWebGL2Options.js +102 -0
  505. package/dist/web/sharedWebGL2Shaders.d.ts +33 -0
  506. package/dist/web/sharedWebGL2Shaders.js +408 -0
  507. package/dist/web/sharedWebGPUOptions.d.ts +41 -0
  508. package/dist/web/sharedWebGPUOptions.js +130 -0
  509. package/dist/web/text.d.ts +3 -0
  510. package/dist/web/text.js +3 -0
  511. package/dist/web/textInput.d.ts +3 -0
  512. package/dist/web/textInput.js +3 -0
  513. package/dist/web/video.d.ts +3 -0
  514. package/dist/web/video.js +4 -0
  515. package/dist/web/webgl2.d.ts +7 -0
  516. package/dist/web/webgl2.js +4 -0
  517. package/dist/web/webgl2LayerState.d.ts +50 -0
  518. package/dist/web/webgl2LayerState.js +102 -0
  519. package/dist/web/webgl2Native.d.ts +72 -0
  520. package/dist/web/webgl2Native.js +572 -0
  521. package/dist/web/webgl2Types.d.ts +150 -0
  522. package/dist/web/webgl2Types.js +2 -0
  523. package/dist/web/webgpu.d.ts +6 -0
  524. package/dist/web/webgpu.js +2 -0
  525. package/dist/web/webgpuBindingCache.d.ts +26 -0
  526. package/dist/web/webgpuBindingCache.js +223 -0
  527. package/dist/web/webgpuBufferPool.d.ts +51 -0
  528. package/dist/web/webgpuBufferPool.js +58 -0
  529. package/dist/web/webgpuImageResidency.d.ts +56 -0
  530. package/dist/web/webgpuImageResidency.js +65 -0
  531. package/dist/web/webgpuLifetime.d.ts +45 -0
  532. package/dist/web/webgpuLifetime.js +64 -0
  533. package/dist/web/webgpuRetentionPool.d.ts +66 -0
  534. package/dist/web/webgpuRetentionPool.js +85 -0
  535. package/dist/web/webgpuSharedPass.d.ts +7 -0
  536. package/dist/web/webgpuSharedPass.js +293 -0
  537. package/dist/web/webgpuSharedResolvePass.d.ts +3 -0
  538. package/dist/web/webgpuSharedResolvePass.js +34 -0
  539. package/dist/web/world3d/Audio3D.d.ts +85 -0
  540. package/dist/web/world3d/Audio3D.js +271 -0
  541. package/dist/web/world3d/BrowserPlay3D.d.ts +295 -0
  542. package/dist/web/world3d/BrowserPlay3D.js +1256 -0
  543. package/dist/web/world3d/FlyControls3D.d.ts +59 -0
  544. package/dist/web/world3d/FlyControls3D.js +162 -0
  545. package/dist/web/world3d/FollowControls3D.d.ts +31 -0
  546. package/dist/web/world3d/FollowControls3D.js +119 -0
  547. package/dist/web/world3d/OrbitControls3D.d.ts +58 -0
  548. package/dist/web/world3d/OrbitControls3D.js +189 -0
  549. package/dist/web/world3d/TransformControls3D.d.ts +37 -0
  550. package/dist/web/world3d/TransformControls3D.js +128 -0
  551. package/dist/web/world3d/WebGL2Renderer3D.d.ts +136 -0
  552. package/dist/web/world3d/WebGL2Renderer3D.js +1175 -0
  553. package/dist/web/world3d/WebGPURenderer3D.d.ts +112 -0
  554. package/dist/web/world3d/WebGPURenderer3D.js +1916 -0
  555. package/dist/web/world3d/World3DView.d.ts +54 -0
  556. package/dist/web/world3d/World3DView.js +79 -0
  557. package/dist/web/world3d/app3D.d.ts +143 -0
  558. package/dist/web/world3d/app3D.js +305 -0
  559. package/dist/web/world3d/assets3D.d.ts +39 -0
  560. package/dist/web/world3d/assets3D.js +112 -0
  561. package/dist/web/world3d/atmosphere3D.d.ts +56 -0
  562. package/dist/web/world3d/atmosphere3D.js +193 -0
  563. package/dist/web/world3d/canvasFont3D.d.ts +21 -0
  564. package/dist/web/world3d/canvasFont3D.js +165 -0
  565. package/dist/web/world3d/frame3D.d.ts +144 -0
  566. package/dist/web/world3d/frame3D.js +388 -0
  567. package/dist/web/world3d/gpuResources3D.d.ts +98 -0
  568. package/dist/web/world3d/gpuResources3D.js +901 -0
  569. package/dist/web/world3d/host3D.d.ts +24 -0
  570. package/dist/web/world3d/host3D.js +61 -0
  571. package/dist/web/world3d/index.d.ts +40 -0
  572. package/dist/web/world3d/index.js +24 -0
  573. package/dist/web/world3d/loaders3D.d.ts +27 -0
  574. package/dist/web/world3d/loaders3D.js +138 -0
  575. package/dist/web/world3d/markers3D.d.ts +53 -0
  576. package/dist/web/world3d/markers3D.js +130 -0
  577. package/dist/web/world3d/materialWebGL23D.d.ts +34 -0
  578. package/dist/web/world3d/materialWebGL23D.js +218 -0
  579. package/dist/web/world3d/overlay3D.d.ts +47 -0
  580. package/dist/web/world3d/overlay3D.js +187 -0
  581. package/dist/web/world3d/post3D.d.ts +116 -0
  582. package/dist/web/world3d/post3D.js +462 -0
  583. package/dist/web/world3d/postWebGL23D.d.ts +24 -0
  584. package/dist/web/world3d/postWebGL23D.js +237 -0
  585. package/dist/web/world3d/postWebGPU3D.d.ts +23 -0
  586. package/dist/web/world3d/postWebGPU3D.js +255 -0
  587. package/dist/web/world3d/probe3D.d.ts +15 -0
  588. package/dist/web/world3d/probe3D.js +66 -0
  589. package/dist/web/world3d/shaders3D.d.ts +43 -0
  590. package/dist/web/world3d/shaders3D.js +851 -0
  591. package/dist/web/world3d/stats3D.d.ts +34 -0
  592. package/dist/web/world3d/stats3D.js +81 -0
  593. package/dist/web/world3d/webGL2FrameMetrics3D.d.ts +71 -0
  594. package/dist/web/world3d/webGL2FrameMetrics3D.js +185 -0
  595. package/dist/web/world3d/webgpu.d.ts +37 -0
  596. package/dist/web/world3d/webgpu.js +23 -0
  597. package/dist/web/world3d/webgpuPacking3D.d.ts +13 -0
  598. package/dist/web/world3d/webgpuPacking3D.js +85 -0
  599. package/dist/web/world3d/wgsl3D.d.ts +68 -0
  600. package/dist/web/world3d/wgsl3D.js +781 -0
  601. package/package.json +220 -4
@@ -0,0 +1,1043 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Navigation for characters that walk: a navigation mesh built from the
3
+ // walkable triangles of level geometry, shortest paths across it, straight
4
+ // line-of-sight checks, movement that slides along its edges, a small crowd
5
+ // that steers agents along paths while keeping them apart, and weighted search
6
+ // on a grid for tile maps. Everything is plain data and arithmetic: no physics world
7
+ // and no DOM is needed.
8
+ import { World3DError, requireFinite3D, requirePositive3D } from './core3D.js';
9
+ import { Geometry3D, createGeometry3D } from './Geometry3D.js';
10
+ import { createMeshShape3D } from './PhysicsMesh3D.js';
11
+ import { HeightField3D } from './Terrain3D.js';
12
+ /** The most triangles, agents and grid cells navigation holds. */
13
+ export const NAVIGATION3D_LIMITS = Object.freeze({
14
+ maxTriangles: 1048576,
15
+ maxAgents: 4096,
16
+ maxGridCells: 16777216,
17
+ maxCellReferences: 8388608
18
+ });
19
+ /** Twice the signed area of a triangle seen from above; the sign tells which side of a-b the point c lies on. */
20
+ const side = (ax, az, bx, bz, cx, cz) => (cx - ax) * (bz - az) - (bx - ax) * (cz - az);
21
+ /** Store a snapshot of each priority so reprioritizing a node cannot corrupt the heap. */
22
+ class Heap {
23
+ score;
24
+ #items = [];
25
+ constructor(score) {
26
+ this.score = score;
27
+ }
28
+ get size() {
29
+ return this.#items.length;
30
+ }
31
+ push(index) {
32
+ const items = this.#items;
33
+ const item = { index, priority: this.score[index] };
34
+ let i = items.length;
35
+ items.push(item);
36
+ while (i > 0) {
37
+ const parent = (i - 1) >> 1;
38
+ if (items[parent].priority <= item.priority)
39
+ break;
40
+ items[i] = items[parent];
41
+ i = parent;
42
+ }
43
+ items[i] = item;
44
+ }
45
+ pop() {
46
+ const items = this.#items;
47
+ while (items.length > 0) {
48
+ const top = items[0];
49
+ const last = items.pop();
50
+ if (items.length > 0) {
51
+ let i = 0;
52
+ for (;;) {
53
+ let child = i * 2 + 1;
54
+ if (child >= items.length)
55
+ break;
56
+ if (child + 1 < items.length && items[child + 1].priority < items[child].priority)
57
+ child++;
58
+ if (items[child].priority >= last.priority)
59
+ break;
60
+ items[i] = items[child];
61
+ i = child;
62
+ }
63
+ items[i] = last;
64
+ }
65
+ if (this.score[top.index] === top.priority)
66
+ return top.index;
67
+ }
68
+ return undefined;
69
+ }
70
+ }
71
+ /** The walkable surface of a level: find paths, nearest points and ray hits on it, and set the cost of its areas. */
72
+ export class NavMesh3D {
73
+ /** Welded corner positions, three floats each. */
74
+ vertices;
75
+ /** Corner indices, three per triangle, counter-clockwise seen from above. */
76
+ triangles;
77
+ /** For each triangle edge (corner i to corner i + 1), the triangle on the other side or -1. */
78
+ neighbors;
79
+ triangleCount;
80
+ /** Connected region of each triangle; two points can reach each other only within one region. */
81
+ regions;
82
+ #cost;
83
+ #minimumCost = 1;
84
+ #nonDefaultCostCount = 0;
85
+ #enabled;
86
+ #cellSize;
87
+ #minX;
88
+ #minZ;
89
+ #columns;
90
+ #rows;
91
+ #cellStart;
92
+ #cellItems;
93
+ /** @param positions Triangle corners in world space; @param indices three per triangle, or none for a plain list. */
94
+ constructor(positions, indices, options = {}) {
95
+ const walkable = Math.cos(options.slopeLimit ?? Math.PI / 4), tolerance = requirePositive3D(options.weldTolerance ?? 0.001, 'weldTolerance');
96
+ const count = (indices ? indices.length : positions.length / 3) / 3;
97
+ if (!Number.isInteger(count) || count > NAVIGATION3D_LIMITS.maxTriangles)
98
+ throw new World3DError(count > NAVIGATION3D_LIMITS.maxTriangles ? 'WORLD3D_BUDGET' : 'WORLD3D_INPUT_INVALID', 'navigation triangles');
99
+ const welded = new Map(), vertices = [], triangles = [];
100
+ const corner = (i) => {
101
+ const at = (indices ? indices[i] : i) * 3, x = requireFinite3D(positions[at], 'navigation position'), y = requireFinite3D(positions[at + 1], 'navigation position'), z = requireFinite3D(positions[at + 2], 'navigation position');
102
+ const key = Math.round(x / tolerance) + ',' + Math.round(y / tolerance) + ',' + Math.round(z / tolerance);
103
+ let index = welded.get(key);
104
+ if (index === undefined) {
105
+ index = vertices.length / 3;
106
+ welded.set(key, index);
107
+ vertices.push(x, y, z);
108
+ }
109
+ return index;
110
+ };
111
+ for (let t = 0; t < count; t++) {
112
+ const a = corner(t * 3), b = corner(t * 3 + 1), c = corner(t * 3 + 2);
113
+ if (a === b || b === c || a === c)
114
+ continue;
115
+ const ux = vertices[b * 3] - vertices[a * 3], uy = vertices[b * 3 + 1] - vertices[a * 3 + 1], uz = vertices[b * 3 + 2] - vertices[a * 3 + 2];
116
+ const vx = vertices[c * 3] - vertices[a * 3], vy = vertices[c * 3 + 1] - vertices[a * 3 + 1], vz = vertices[c * 3 + 2] - vertices[a * 3 + 2];
117
+ const nx = uy * vz - uz * vy, ny = uz * vx - ux * vz, nz = ux * vy - uy * vx, length = Math.hypot(nx, ny, nz);
118
+ // Keep faces that point up and are flat enough to walk on.
119
+ if (length < 1e-12 || ny / length < walkable)
120
+ continue;
121
+ triangles.push(a, b, c);
122
+ }
123
+ if (triangles.length === 0)
124
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'the geometry has no walkable triangles');
125
+ this.vertices = Float32Array.from(vertices);
126
+ this.triangles = Uint32Array.from(triangles);
127
+ const total = (this.triangleCount = triangles.length / 3);
128
+ // Two triangles are neighbors when they share an edge walked in opposite directions.
129
+ this.neighbors = new Int32Array(total * 3).fill(-1);
130
+ const edges = new Map(), vertexCount = vertices.length / 3;
131
+ for (let t = 0; t < total; t++)
132
+ for (let e = 0; e < 3; e++)
133
+ edges.set(triangles[t * 3 + e] * vertexCount + triangles[t * 3 + ((e + 1) % 3)], t * 3 + e);
134
+ for (const [key, slot] of edges) {
135
+ const from = Math.floor(key / vertexCount), to = key % vertexCount, other = edges.get(to * vertexCount + from);
136
+ if (other !== undefined)
137
+ this.neighbors[slot] = Math.floor(other / 3);
138
+ }
139
+ this.#cost = new Float64Array(total).fill(1);
140
+ this.#enabled = new Uint8Array(total).fill(1);
141
+ this.regions = new Int32Array(total).fill(-1);
142
+ for (let t = 0, region = 0; t < total; t++) {
143
+ if (this.regions[t] !== -1)
144
+ continue;
145
+ const stack = [t];
146
+ this.regions[t] = region;
147
+ while (stack.length > 0) {
148
+ const current = stack.pop();
149
+ for (let e = 0; e < 3; e++) {
150
+ const next = this.neighbors[current * 3 + e];
151
+ if (next >= 0 && this.regions[next] === -1) {
152
+ this.regions[next] = region;
153
+ stack.push(next);
154
+ }
155
+ }
156
+ }
157
+ region++;
158
+ }
159
+ // A grid over the ground plan finds the triangles near a point quickly.
160
+ let minX = Infinity, minZ = Infinity, maxX = -Infinity, maxZ = -Infinity;
161
+ for (let i = 0; i < vertices.length; i += 3) {
162
+ minX = Math.min(minX, vertices[i]);
163
+ maxX = Math.max(maxX, vertices[i]);
164
+ minZ = Math.min(minZ, vertices[i + 2]);
165
+ maxZ = Math.max(maxZ, vertices[i + 2]);
166
+ }
167
+ const cell = (this.#cellSize = Math.max(Math.sqrt(((maxX - minX + 1e-6) * (maxZ - minZ + 1e-6)) / total) * 1.5, 1e-3));
168
+ this.#minX = minX;
169
+ this.#minZ = minZ;
170
+ const columns = (this.#columns = Math.min(Math.max(Math.ceil((maxX - minX) / cell), 1), 2048)), rows = (this.#rows = Math.min(Math.max(Math.ceil((maxZ - minZ) / cell), 1), 2048));
171
+ const spans = new Uint16Array(total * 4);
172
+ let references = 0;
173
+ for (let t = 0; t < total; t++) {
174
+ let x0 = Infinity, z0 = Infinity, x1 = -Infinity, z1 = -Infinity;
175
+ for (let e = 0; e < 3; e++) {
176
+ const v = triangles[t * 3 + e] * 3;
177
+ x0 = Math.min(x0, vertices[v]);
178
+ x1 = Math.max(x1, vertices[v]);
179
+ z0 = Math.min(z0, vertices[v + 2]);
180
+ z1 = Math.max(z1, vertices[v + 2]);
181
+ }
182
+ const c0 = this.column(x0), c1 = this.column(x1), r0 = this.row(z0), r1 = this.row(z1);
183
+ references += (c1 - c0 + 1) * (r1 - r0 + 1);
184
+ if (references > NAVIGATION3D_LIMITS.maxCellReferences)
185
+ throw new World3DError('WORLD3D_BUDGET', 'navigation cell references');
186
+ const span = t * 4;
187
+ spans[span] = c0;
188
+ spans[span + 1] = c1;
189
+ spans[span + 2] = r0;
190
+ spans[span + 3] = r1;
191
+ }
192
+ // Both passes are bounded by the admitted reference count.
193
+ const counts = new Uint32Array(columns * rows + 1);
194
+ for (let span = 0; span < spans.length; span += 4) {
195
+ for (let r = spans[span + 2]; r <= spans[span + 3]; r++) {
196
+ for (let c = spans[span]; c <= spans[span + 1]; c++)
197
+ counts[r * columns + c + 1]++;
198
+ }
199
+ }
200
+ for (let i = 1; i < counts.length; i++)
201
+ counts[i] += counts[i - 1];
202
+ this.#cellStart = counts;
203
+ this.#cellItems = new Uint32Array(references);
204
+ const fill = counts.slice(0, counts.length - 1);
205
+ for (let span = 0; span < spans.length; span += 4) {
206
+ for (let r = spans[span + 2]; r <= spans[span + 3]; r++) {
207
+ for (let c = spans[span]; c <= spans[span + 1]; c++)
208
+ this.#cellItems[fill[r * columns + c]++] = span / 4;
209
+ }
210
+ }
211
+ }
212
+ column(x) {
213
+ return Math.min(Math.max(Math.floor((x - this.#minX) / this.#cellSize), 0), this.#columns - 1);
214
+ }
215
+ row(z) {
216
+ return Math.min(Math.max(Math.floor((z - this.#minZ) / this.#cellSize), 0), this.#rows - 1);
217
+ }
218
+ /** Travel cost multiplier of a triangle (mud, shallow water); 1 by default. */
219
+ setCost(triangle, cost) {
220
+ const index = this.checked(triangle);
221
+ const price = requirePositive3D(cost, 'cost');
222
+ this.#nonDefaultCostCount += Number(price !== 1) - Number(this.#cost[index] !== 1);
223
+ this.#cost[index] = price;
224
+ // A historical minimum remains a safe lower bound when a cost rises again.
225
+ this.#minimumCost = this.#nonDefaultCostCount === 0 ? 1 : Math.min(this.#minimumCost, price);
226
+ }
227
+ /** Close or reopen a triangle, as for a door or a collapsed bridge. Paths and movement avoid closed triangles. */
228
+ setEnabled(triangle, enabled) {
229
+ this.#enabled[this.checked(triangle)] = enabled ? 1 : 0;
230
+ }
231
+ isEnabled(triangle) {
232
+ return this.#enabled[this.checked(triangle)] === 1;
233
+ }
234
+ checked(triangle) {
235
+ if (!Number.isInteger(triangle) || triangle < 0 || triangle >= this.triangleCount)
236
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'navigation triangle');
237
+ return triangle;
238
+ }
239
+ /** Middle of a triangle. */
240
+ center(triangle, out = [0, 0, 0]) {
241
+ const v = this.vertices, t = this.triangles, a = t[triangle * 3] * 3, b = t[triangle * 3 + 1] * 3, c = t[triangle * 3 + 2] * 3;
242
+ out[0] = (v[a] + v[b] + v[c]) / 3;
243
+ out[1] = (v[a + 1] + v[b + 1] + v[c + 1]) / 3;
244
+ out[2] = (v[a + 2] + v[b + 2] + v[c + 2]) / 3;
245
+ return out;
246
+ }
247
+ /** Closest point of one triangle to a point, written to `out`; returns the squared distance. */
248
+ closestOn(triangle, px, py, pz, out) {
249
+ const v = this.vertices, t = this.triangles, a = t[triangle * 3] * 3, b = t[triangle * 3 + 1] * 3, c = t[triangle * 3 + 2] * 3;
250
+ const ax = v[a], ay = v[a + 1], az = v[a + 2], abx = v[b] - ax, aby = v[b + 1] - ay, abz = v[b + 2] - az, acx = v[c] - ax, acy = v[c + 1] - ay, acz = v[c + 2] - az;
251
+ const apx = px - ax, apy = py - ay, apz = pz - az, d1 = abx * apx + aby * apy + abz * apz, d2 = acx * apx + acy * apy + acz * apz;
252
+ let u = 0, w = 0; // point = a + ab * u + ac * w
253
+ if (!(d1 <= 0 && d2 <= 0)) {
254
+ const bpx = apx - abx, bpy = apy - aby, bpz = apz - abz, d3 = abx * bpx + aby * bpy + abz * bpz, d4 = acx * bpx + acy * bpy + acz * bpz;
255
+ if (d3 >= 0 && d4 <= d3)
256
+ u = 1;
257
+ else {
258
+ const vc = d1 * d4 - d3 * d2;
259
+ if (vc <= 0 && d1 >= 0 && d3 <= 0)
260
+ u = d1 / (d1 - d3);
261
+ else {
262
+ const cpx = apx - acx, cpy = apy - acy, cpz = apz - acz, d5 = abx * cpx + aby * cpy + abz * cpz, d6 = acx * cpx + acy * cpy + acz * cpz;
263
+ if (d6 >= 0 && d5 <= d6)
264
+ w = 1;
265
+ else {
266
+ const vb = d5 * d2 - d1 * d6;
267
+ if (vb <= 0 && d2 >= 0 && d6 <= 0)
268
+ w = d2 / (d2 - d6);
269
+ else {
270
+ const va = d3 * d6 - d5 * d4;
271
+ if (va <= 0 && d4 - d3 >= 0 && d5 - d6 >= 0) {
272
+ w = (d4 - d3) / (d4 - d3 + d5 - d6);
273
+ u = 1 - w;
274
+ }
275
+ else {
276
+ const scale = 1 / (va + vb + vc);
277
+ u = vb * scale;
278
+ w = vc * scale;
279
+ }
280
+ }
281
+ }
282
+ }
283
+ }
284
+ }
285
+ out[0] = ax + abx * u + acx * w;
286
+ out[1] = ay + aby * u + acy * w;
287
+ out[2] = az + abz * u + acz * w;
288
+ return (out[0] - px) ** 2 + (out[1] - py) ** 2 + (out[2] - pz) ** 2;
289
+ }
290
+ /** Nearest place on the open part of the mesh within `maxDistance`, or undefined. */
291
+ findNearest(point, maxDistance = 2) {
292
+ const px = point[0], py = point[1], pz = point[2], columns = this.#columns, candidate = [0, 0, 0], bestPoint = [0, 0, 0];
293
+ let best = -1, bestDistance = maxDistance * maxDistance;
294
+ const c0 = this.column(px - maxDistance), c1 = this.column(px + maxDistance), r0 = this.row(pz - maxDistance), r1 = this.row(pz + maxDistance);
295
+ for (let r = r0; r <= r1; r++)
296
+ for (let c = c0; c <= c1; c++) {
297
+ for (let i = this.#cellStart[r * columns + c]; i < this.#cellStart[r * columns + c + 1]; i++) {
298
+ const triangle = this.#cellItems[i];
299
+ if (!this.#enabled[triangle])
300
+ continue;
301
+ const distance = this.closestOn(triangle, px, py, pz, candidate);
302
+ if (distance <= bestDistance && (distance < bestDistance || best < 0)) {
303
+ bestDistance = distance;
304
+ best = triangle;
305
+ bestPoint[0] = candidate[0];
306
+ bestPoint[1] = candidate[1];
307
+ bestPoint[2] = candidate[2];
308
+ }
309
+ }
310
+ }
311
+ return best < 0 ? undefined : { triangle: best, point: bestPoint };
312
+ }
313
+ /** Height of a triangle's plane at a ground position. */
314
+ heightAt(triangle, x, z) {
315
+ const v = this.vertices, t = this.triangles, a = t[triangle * 3] * 3, b = t[triangle * 3 + 1] * 3, c = t[triangle * 3 + 2] * 3;
316
+ const ux = v[b] - v[a], uy = v[b + 1] - v[a + 1], uz = v[b + 2] - v[a + 2], vx = v[c] - v[a], vy = v[c + 1] - v[a + 1], vz = v[c + 2] - v[a + 2];
317
+ const ny = uz * vx - ux * vz;
318
+ return Math.abs(ny) < 1e-12
319
+ ? v[a + 1]
320
+ : v[a + 1] - ((uy * vz - uz * vy) * (x - v[a]) + (ux * vy - uy * vx) * (z - v[a + 2])) / ny;
321
+ }
322
+ /** True when the two places can reach each other across open triangles. */
323
+ isReachable(a, b) {
324
+ return this.search(a.triangle, b.triangle, b.point, 0) !== undefined && this.#reached;
325
+ }
326
+ #reached = false;
327
+ /** A* over triangles. Returns the chain of triangles from start to the end, or to the open triangle nearest the end. */
328
+ search(start, end, target, radius) {
329
+ const total = this.triangleCount, g = new Float64Array(total).fill(Infinity), f = new Float64Array(total).fill(Infinity), from = new Int32Array(total).fill(-1), closed = new Uint8Array(total);
330
+ // Each triangle is entered at a point: the start itself, or the middle of the edge crossed.
331
+ const entry = new Float32Array(total * 3), v = this.vertices, tri = this.triangles, heap = new Heap(f), center = [0, 0, 0];
332
+ this.center(start, center);
333
+ entry[start * 3] = center[0];
334
+ entry[start * 3 + 1] = center[1];
335
+ entry[start * 3 + 2] = center[2];
336
+ const distanceToGoal = (x, y, z) => Math.hypot(x - target[0], y - target[1], z - target[2]);
337
+ let nearest = start, nearestDistance = distanceToGoal(center[0], center[1], center[2]);
338
+ g[start] = 0;
339
+ f[start] = nearestDistance * this.#minimumCost;
340
+ heap.push(start);
341
+ this.#reached = false;
342
+ while (heap.size > 0) {
343
+ const current = heap.pop();
344
+ if (current === undefined)
345
+ break;
346
+ if (closed[current])
347
+ continue;
348
+ closed[current] = 1;
349
+ if (current === end) {
350
+ this.#reached = true;
351
+ nearest = end;
352
+ break;
353
+ }
354
+ const here = distanceToGoal(entry[current * 3], entry[current * 3 + 1], entry[current * 3 + 2]);
355
+ if (here < nearestDistance) {
356
+ nearestDistance = here;
357
+ nearest = current;
358
+ }
359
+ for (let e = 0; e < 3; e++) {
360
+ const next = this.neighbors[current * 3 + e];
361
+ if (next < 0 || closed[next] || !this.#enabled[next])
362
+ continue;
363
+ const a = tri[current * 3 + e] * 3, b = tri[current * 3 + ((e + 1) % 3)] * 3;
364
+ // A gap narrower than the agent cannot be passed.
365
+ if (radius > 0 && Math.hypot(v[b] - v[a], v[b + 1] - v[a + 1], v[b + 2] - v[a + 2]) < radius * 2)
366
+ continue;
367
+ const mx = (v[a] + v[b]) / 2, my = (v[a + 1] + v[b + 1]) / 2, mz = (v[a + 2] + v[b + 2]) / 2;
368
+ const step = Math.hypot(mx - entry[current * 3], my - entry[current * 3 + 1], mz - entry[current * 3 + 2]) *
369
+ this.#cost[current], cost = g[current] + step;
370
+ if (cost >= g[next])
371
+ continue;
372
+ g[next] = cost;
373
+ from[next] = current;
374
+ entry[next * 3] = mx;
375
+ entry[next * 3 + 1] = my;
376
+ entry[next * 3 + 2] = mz;
377
+ f[next] = cost + distanceToGoal(mx, my, mz) * this.#minimumCost;
378
+ heap.push(next);
379
+ }
380
+ }
381
+ const chain = [];
382
+ for (let t = nearest; t >= 0; t = from[t])
383
+ chain.push(t);
384
+ return chain.reverse();
385
+ }
386
+ /**
387
+ * Cost-guided walkable path between two points. The points are snapped onto
388
+ * the mesh first; undefined is returned when either lies too far from it.
389
+ */
390
+ findPath(start, end, options = {}) {
391
+ const snap = options.snapDistance ?? 2, radius = Math.max(options.agentRadius ?? 0, 0), from = this.findNearest(start, snap), to = this.findNearest(end, snap);
392
+ if (!from || !to)
393
+ return undefined;
394
+ const chain = this.search(from.triangle, to.triangle, to.point, radius), complete = this.#reached;
395
+ // When the end cannot be reached, head for the closest point of the last reachable triangle.
396
+ const goal = [to.point[0], to.point[1], to.point[2]];
397
+ if (!complete)
398
+ this.closestOn(chain[chain.length - 1], to.point[0], to.point[1], to.point[2], goal);
399
+ // Portals: the shared edges crossed in order, with their left and right ends as seen while walking.
400
+ const v = this.vertices, tri = this.triangles, portals = [from.point[0], from.point[1], from.point[2], from.point[0], from.point[1], from.point[2]], middle = [0, 0, 0];
401
+ for (let i = 0; i + 1 < chain.length; i++) {
402
+ const current = chain[i], next = chain[i + 1];
403
+ let e = 0;
404
+ while (e < 3 && this.neighbors[current * 3 + e] !== next)
405
+ e++;
406
+ let a = tri[current * 3 + e] * 3, b = tri[current * 3 + ((e + 1) % 3)] * 3;
407
+ this.center(current, middle);
408
+ // Order the two ends so that "left" really is on the walker's left.
409
+ if (side(middle[0], middle[2], v[b], v[b + 2], v[a], v[a + 2]) >= 0) {
410
+ const swap = a;
411
+ a = b;
412
+ b = swap;
413
+ }
414
+ let lx = v[a], ly = v[a + 1], lz = v[a + 2], rx = v[b], ry = v[b + 1], rz = v[b + 2];
415
+ if (radius > 0) {
416
+ // Pull both ends inward so corners are passed at arm's length.
417
+ const length = Math.hypot(rx - lx, ry - ly, rz - lz), inset = Math.min(radius / length, 0.5), dx = (rx - lx) * inset, dy = (ry - ly) * inset, dz = (rz - lz) * inset;
418
+ lx += dx;
419
+ ly += dy;
420
+ lz += dz;
421
+ rx -= dx;
422
+ ry -= dy;
423
+ rz -= dz;
424
+ }
425
+ portals.push(lx, ly, lz, rx, ry, rz);
426
+ }
427
+ portals.push(goal[0], goal[1], goal[2], goal[0], goal[1], goal[2]);
428
+ // String pulling: keep a funnel from the last corner and tighten it portal by portal.
429
+ const count = portals.length / 6, corners = [0], cornerPoints = [[from.point[0], from.point[1], from.point[2]]];
430
+ let apex = [portals[0], portals[1], portals[2]], left = [...apex], right = [...apex], apexIndex = 0, leftIndex = 0, rightIndex = 0;
431
+ const same = (p, q) => Math.abs(p[0] - q[0]) < 1e-9 && Math.abs(p[2] - q[2]) < 1e-9;
432
+ for (let i = 1; i < count; i++) {
433
+ const nl = [portals[i * 6], portals[i * 6 + 1], portals[i * 6 + 2]], nr = [portals[i * 6 + 3], portals[i * 6 + 4], portals[i * 6 + 5]];
434
+ if (side(apex[0], apex[2], right[0], right[2], nr[0], nr[2]) <= 0) {
435
+ if (same(apex, right) || side(apex[0], apex[2], left[0], left[2], nr[0], nr[2]) > 0) {
436
+ right = nr;
437
+ rightIndex = i;
438
+ }
439
+ else {
440
+ // The right side crossed over the left: the left end is a corner of the path.
441
+ cornerPoints.push(left);
442
+ corners.push(leftIndex);
443
+ apex = left;
444
+ apexIndex = leftIndex;
445
+ left = [...apex];
446
+ right = [...apex];
447
+ rightIndex = apexIndex;
448
+ i = apexIndex;
449
+ continue;
450
+ }
451
+ }
452
+ if (side(apex[0], apex[2], left[0], left[2], nl[0], nl[2]) >= 0) {
453
+ if (same(apex, left) || side(apex[0], apex[2], right[0], right[2], nl[0], nl[2]) < 0) {
454
+ left = nl;
455
+ leftIndex = i;
456
+ }
457
+ else {
458
+ cornerPoints.push(right);
459
+ corners.push(rightIndex);
460
+ apex = right;
461
+ apexIndex = rightIndex;
462
+ left = [...apex];
463
+ right = [...apex];
464
+ leftIndex = apexIndex;
465
+ i = apexIndex;
466
+ continue;
467
+ }
468
+ }
469
+ }
470
+ cornerPoints.push(goal);
471
+ corners.push(count - 1);
472
+ let points = cornerPoints;
473
+ // A walkable shortcut can cost more than the selected corridor, even if the shortcut's own cost is uniform.
474
+ if (this.#nonDefaultCostCount === 0) {
475
+ points = [cornerPoints[0]];
476
+ for (let i = 0; i + 1 < cornerPoints.length;) {
477
+ const here = this.findNearest(cornerPoints[i], 0.5);
478
+ let next = i + 1;
479
+ if (here)
480
+ for (let j = cornerPoints.length - 1; j > i + 1; j--)
481
+ if (this.clearWalk(here, cornerPoints[j], radius)) {
482
+ next = j;
483
+ break;
484
+ }
485
+ points.push(cornerPoints[next]);
486
+ i = next;
487
+ }
488
+ }
489
+ if (options.followSurface) {
490
+ // Add a point wherever a straight piece crosses from one triangle to the next, so the path hugs slopes.
491
+ const detailed = [points[0]];
492
+ for (let k = 0; k + 1 < points.length; k++) {
493
+ const here = this.findNearest(points[k], 0.5);
494
+ if (here)
495
+ this.raycast(here, points[k + 1][0], points[k + 1][2], detailed);
496
+ detailed.push(points[k + 1]);
497
+ }
498
+ points = detailed;
499
+ }
500
+ // Drop repeated points.
501
+ const result = [];
502
+ let length = 0;
503
+ for (const p of points) {
504
+ const last = result[result.length - 1];
505
+ if (last && Math.hypot(p[0] - last[0], p[1] - last[1], p[2] - last[2]) < 1e-6)
506
+ continue;
507
+ if (last)
508
+ length += Math.hypot(p[0] - last[0], p[1] - last[1], p[2] - last[2]);
509
+ result.push([p[0], p[1], p[2]]);
510
+ }
511
+ return { points: result, complete, length };
512
+ }
513
+ /**
514
+ * Walk from a place toward a ground position, staying on open triangles
515
+ * and sliding along the mesh edge when the way is blocked. `blocked`
516
+ * tells whether an edge stopped the move.
517
+ */
518
+ moveAlong(from, x, z) {
519
+ const v = this.vertices, tri = this.triangles;
520
+ let triangle = this.checked(from.triangle), px = from.point[0], pz = from.point[2], tx = x, tz = z, blocked = false, slides = 0;
521
+ for (let guard = 0; guard < 256; guard++) {
522
+ // Find the edge the remaining move leaves through, if any.
523
+ let exit = -1, exitAt = Infinity;
524
+ for (let e = 0; e < 3; e++) {
525
+ const a = tri[triangle * 3 + e] * 3, b = tri[triangle * 3 + ((e + 1) % 3)] * 3, ax = v[a], az = v[a + 2], bx = v[b], bz = v[b + 2];
526
+ const target = side(ax, az, bx, bz, tx, tz);
527
+ if (target >= -1e-12)
528
+ continue; // the target is on the inner side of this edge
529
+ const here = side(ax, az, bx, bz, px, pz), along = here <= 0 ? 0 : here / (here - target);
530
+ if (along < exitAt) {
531
+ exitAt = along;
532
+ exit = e;
533
+ }
534
+ }
535
+ if (exit < 0) {
536
+ px = tx;
537
+ pz = tz;
538
+ break;
539
+ }
540
+ const next = this.neighbors[triangle * 3 + exit];
541
+ if (next >= 0 && this.#enabled[next]) {
542
+ px += (tx - px) * exitAt;
543
+ pz += (tz - pz) * exitAt;
544
+ triangle = next;
545
+ continue;
546
+ }
547
+ // Blocked: keep only the part of the move that runs along the edge, and carry on from where it was met.
548
+ px += (tx - px) * exitAt;
549
+ pz += (tz - pz) * exitAt;
550
+ if (++slides > 4)
551
+ break;
552
+ blocked = true;
553
+ const a = tri[triangle * 3 + exit] * 3, b = tri[triangle * 3 + ((exit + 1) % 3)] * 3, ex = v[b] - v[a], ez = v[b + 2] - v[a + 2], size = Math.hypot(ex, ez) || 1;
554
+ const along = ((tx - v[a]) * ex + (tz - v[a + 2]) * ez) / (size * size);
555
+ // Aim a hair inside the edge (its inner side is to the left of a-to-b seen from above) so the walk does not leave through it again.
556
+ tx = v[a] + ex * along + (ez / size) * 1e-4;
557
+ tz = v[a + 2] + ez * along - (ex / size) * 1e-4;
558
+ }
559
+ return { triangle, point: [px, this.heightAt(triangle, px, pz), pz], blocked };
560
+ }
561
+ /** True when an agent of the given radius can walk straight from a place to a point. */
562
+ clearWalk(from, to, radius) {
563
+ // A shortcut must not cut across ground the search priced differently, or it would undo a detour.
564
+ if (this.trace(from, to[0], to[2], undefined, true) < 1)
565
+ return false;
566
+ if (!(radius > 0))
567
+ return true;
568
+ // The agent's two shoulders must get through as well.
569
+ const dx = to[0] - from.point[0], dz = to[2] - from.point[2], length = Math.hypot(dx, dz);
570
+ if (length < 1e-9)
571
+ return true;
572
+ const sx = (-dz / length) * radius, sz = (dx / length) * radius;
573
+ for (const sign of [1, -1]) {
574
+ const shoulder = this.moveAlong(from, from.point[0] + sx * sign, from.point[2] + sz * sign);
575
+ if (shoulder.blocked || this.trace(shoulder, to[0] + sx * sign, to[2] + sz * sign, undefined, true) < 1)
576
+ return false;
577
+ }
578
+ return true;
579
+ }
580
+ /**
581
+ * Line of sight along the ground: how far a straight walk from a place
582
+ * toward a ground position gets, as a share from 0 to 1. A value of 1
583
+ * means nothing is in the way. When `crossings` is given, the points
584
+ * where the walk passes from one triangle to the next are appended to it.
585
+ */
586
+ raycast(from, x, z, crossings) {
587
+ return this.trace(from, x, z, crossings, false);
588
+ }
589
+ trace(from, x, z, crossings, plainGroundOnly) {
590
+ const v = this.vertices, tri = this.triangles, px = from.point[0], pz = from.point[2];
591
+ let triangle = this.checked(from.triangle), previous = -1;
592
+ for (let guard = 0; guard < 4096; guard++) {
593
+ let exit = -1, exitAt = Infinity;
594
+ for (let e = 0; e < 3; e++) {
595
+ if (this.neighbors[triangle * 3 + e] === previous && previous >= 0)
596
+ continue;
597
+ const a = tri[triangle * 3 + e] * 3, b = tri[triangle * 3 + ((e + 1) % 3)] * 3, target = side(v[a], v[a + 2], v[b], v[b + 2], x, z);
598
+ if (target >= -1e-12)
599
+ continue;
600
+ const here = side(v[a], v[a + 2], v[b], v[b + 2], px, pz), along = here <= 0 ? 0 : here / (here - target);
601
+ if (along < exitAt) {
602
+ exitAt = along;
603
+ exit = e;
604
+ }
605
+ }
606
+ if (exit < 0)
607
+ return 1;
608
+ const next = this.neighbors[triangle * 3 + exit];
609
+ if (next < 0 || !this.#enabled[next] || (plainGroundOnly && this.#cost[next] !== this.#cost[triangle]))
610
+ return Math.min(Math.max(exitAt, 0), 1);
611
+ if (crossings) {
612
+ const cx = px + (x - px) * exitAt, cz = pz + (z - pz) * exitAt;
613
+ crossings.push([cx, this.heightAt(triangle, cx, cz), cz]);
614
+ }
615
+ previous = triangle;
616
+ triangle = next;
617
+ }
618
+ return 0;
619
+ }
620
+ /** A place chosen evenly over the open area; pass a random function for repeatable results. */
621
+ randomPoint(random = Math.random) {
622
+ const v = this.vertices, tri = this.triangles, areas = new Float64Array(this.triangleCount);
623
+ let total = 0;
624
+ for (let t = 0; t < this.triangleCount; t++) {
625
+ if (!this.#enabled[t])
626
+ continue;
627
+ const a = tri[t * 3] * 3, b = tri[t * 3 + 1] * 3, c = tri[t * 3 + 2] * 3;
628
+ total += areas[t] = Math.abs(side(v[a], v[a + 2], v[b], v[b + 2], v[c], v[c + 2]));
629
+ }
630
+ let pick = random() * total, triangle = 0;
631
+ for (let t = 0; t < this.triangleCount; t++) {
632
+ if (areas[t] <= 0)
633
+ continue;
634
+ triangle = t;
635
+ pick -= areas[t];
636
+ if (pick <= 0)
637
+ break;
638
+ }
639
+ let s = random(), w = random();
640
+ if (s + w > 1) {
641
+ s = 1 - s;
642
+ w = 1 - w;
643
+ }
644
+ const a = tri[triangle * 3] * 3, b = tri[triangle * 3 + 1] * 3, c = tri[triangle * 3 + 2] * 3;
645
+ return {
646
+ triangle,
647
+ point: [
648
+ v[a] + (v[b] - v[a]) * s + (v[c] - v[a]) * w,
649
+ v[a + 1] + (v[b + 1] - v[a + 1]) * s + (v[c + 1] - v[a + 1]) * w,
650
+ v[a + 2] + (v[b + 2] - v[a + 2]) * s + (v[c + 2] - v[a + 2]) * w
651
+ ]
652
+ };
653
+ }
654
+ /** Line geometry of every triangle edge, for drawing the mesh while tuning a level. */
655
+ createDebugGeometry(lift = 0.02) {
656
+ const v = this.vertices, tri = this.triangles, positions = [];
657
+ for (let t = 0; t < this.triangleCount; t++)
658
+ for (let e = 0; e < 3; e++) {
659
+ const other = this.neighbors[t * 3 + e];
660
+ if (other > t)
661
+ continue; // shared edges are drawn once
662
+ const a = tri[t * 3 + e] * 3, b = tri[t * 3 + ((e + 1) % 3)] * 3;
663
+ positions.push(v[a], v[a + 1] + lift, v[a + 2], v[b], v[b + 1] + lift, v[b + 2]);
664
+ }
665
+ return createGeometry3D({ positions, topology: 'lines' });
666
+ }
667
+ }
668
+ /**
669
+ * Build a navigation mesh from level geometry: a geometry, or an object whose
670
+ * meshes are merged with their world transforms. Faces steeper than
671
+ * `slopeLimit` and faces pointing down are left out.
672
+ */
673
+ export function createNavMesh3D(source, options = {}) {
674
+ if (source instanceof HeightField3D)
675
+ return heightFieldNavMesh(source, options);
676
+ if (source instanceof Geometry3D || (typeof source === 'object' && source !== null && 'traverse' in source)) {
677
+ const shape = createMeshShape3D(source);
678
+ return new NavMesh3D(shape.positions, shape.indices, options);
679
+ }
680
+ if (!source || !('positions' in source))
681
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'navigation source');
682
+ return new NavMesh3D(source.positions, source.indices, options);
683
+ }
684
+ /** A grid of two triangles a cell over the ground of a height field, less the holes and what lies too low. */
685
+ function heightFieldNavMesh(field, options) {
686
+ const [cx, cz] = options.center ?? [0, 0];
687
+ requireFinite3D(cx, 'center');
688
+ requireFinite3D(cz, 'center');
689
+ const region = options.region ?? {
690
+ x: cx - field.width / 2,
691
+ z: cz - field.depth / 2,
692
+ width: field.width,
693
+ depth: field.depth
694
+ };
695
+ const x0 = requireFinite3D(region.x, 'region'), z0 = requireFinite3D(region.z, 'region'), width = requirePositive3D(region.width, 'region'), depth = requirePositive3D(region.depth, 'region'), cell = requirePositive3D(options.cellSize ?? Math.min(field.cellWidth, field.cellDepth), 'cellSize'), floor = options.minHeight === undefined ? -Infinity : requireFinite3D(options.minHeight, 'minHeight');
696
+ const holes = (options.holes ?? []).map(hole => [
697
+ requireFinite3D(hole.x, 'hole'),
698
+ requireFinite3D(hole.z, 'hole'),
699
+ requirePositive3D(hole.radius, 'hole')
700
+ ]);
701
+ const columns = Math.ceil(width / cell - 1e-9), rows = Math.ceil(depth / cell - 1e-9);
702
+ if (columns * rows * 2 > NAVIGATION3D_LIMITS.maxTriangles)
703
+ throw new World3DError('WORLD3D_BUDGET', 'navigation triangles');
704
+ // Corners clamp to the field: a region past its edge stops at the edge.
705
+ const halfW = field.width / 2, halfD = field.depth / 2, positions = new Float32Array((columns + 1) * (rows + 1) * 3), open = new Uint8Array((columns + 1) * (rows + 1));
706
+ for (let row = 0; row <= rows; row++)
707
+ for (let column = 0; column <= columns; column++) {
708
+ const x = x0 + Math.min(column * cell, width), z = z0 + Math.min(row * cell, depth), lx = Math.max(-halfW, Math.min(halfW, x - cx)), lz = Math.max(-halfD, Math.min(halfD, z - cz)), y = field.heightAt(lx, lz), i = row * (columns + 1) + column;
709
+ positions[i * 3] = lx + cx;
710
+ positions[i * 3 + 1] = y;
711
+ positions[i * 3 + 2] = lz + cz;
712
+ open[i] =
713
+ y >= floor && holes.every(([hx, hz, r]) => (lx + cx - hx) ** 2 + (lz + cz - hz) ** 2 > r * r) ? 1 : 0;
714
+ }
715
+ const indices = [];
716
+ const add = (a, b, c) => {
717
+ if (open[a] && open[b] && open[c])
718
+ indices.push(a, b, c);
719
+ };
720
+ for (let row = 0; row < rows; row++)
721
+ for (let column = 0; column < columns; column++) {
722
+ const a = row * (columns + 1) + column, b = a + columns + 1;
723
+ // Counter-clockwise seen from above: (x, z), (x, z + 1), (x + 1, z), then (x + 1, z), (x, z + 1), (x + 1, z + 1).
724
+ add(a, b, a + 1);
725
+ add(a + 1, b, b + 1);
726
+ }
727
+ if (indices.length === 0)
728
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'no walkable ground in the region');
729
+ return new NavMesh3D(positions, indices, options);
730
+ }
731
+ /** An agent of a crowd: where it is and how it moves; give it a target, or stop it. */
732
+ export class NavAgent3D {
733
+ position = [0, 0, 0];
734
+ velocity = [0, 0, 0];
735
+ radius;
736
+ speed;
737
+ acceleration;
738
+ node;
739
+ /** Radians per second the node turns to face the way it walks; undefined leaves its turning to the game. */
740
+ turnSpeed;
741
+ /** The angle about the vertical axis the node faces, in radians, 0 facing -Z; kept while `turnSpeed` is set. */
742
+ heading = 0;
743
+ userData;
744
+ /** True once the agent has reached its target, or has none. */
745
+ arrived = true;
746
+ /** False when the last path request could not reach the target. */
747
+ reachable = true;
748
+ /** @internal */ triangle = 0;
749
+ /** @internal */ corners = [];
750
+ /** @internal */ target;
751
+ /** @internal */ stale = false;
752
+ /** @internal */ stalled = 0;
753
+ /** @internal */ closest = Infinity;
754
+ constructor(options) {
755
+ this.radius = requirePositive3D(options.radius ?? 0.35, 'agent radius');
756
+ this.speed = requirePositive3D(options.speed ?? 3.5, 'agent speed');
757
+ this.acceleration = requirePositive3D(options.acceleration ?? 20, 'agent acceleration');
758
+ this.node = options.node;
759
+ if (options.turnSpeed !== undefined)
760
+ this.turnSpeed =
761
+ options.turnSpeed === Infinity ? Infinity : requirePositive3D(options.turnSpeed, 'agent turn speed');
762
+ this.userData = options.userData;
763
+ }
764
+ /** Walk to a point; the path is worked out on the next crowd update. */
765
+ setTarget(point) {
766
+ this.target = [
767
+ requireFinite3D(point[0], 'target'),
768
+ requireFinite3D(point[1], 'target'),
769
+ requireFinite3D(point[2], 'target')
770
+ ];
771
+ this.stale = true;
772
+ this.arrived = false;
773
+ this.closest = Infinity;
774
+ this.stalled = 0;
775
+ }
776
+ /** Stop where the agent stands. */
777
+ stop() {
778
+ this.target = undefined;
779
+ this.corners = [];
780
+ this.arrived = true;
781
+ this.stale = false;
782
+ }
783
+ /** Remaining path corners, the next one first. */
784
+ get path() {
785
+ return this.corners;
786
+ }
787
+ }
788
+ /**
789
+ * Agents walking on one navigation mesh. Each follows its own path and
790
+ * steers away from neighbors it would otherwise walk into.
791
+ */
792
+ export class NavCrowd3D {
793
+ navMesh;
794
+ agents = [];
795
+ /** How strongly agents push apart when they overlap; 0 lets them pass through each other. */
796
+ separation = 1;
797
+ constructor(navMesh) {
798
+ if (!(navMesh instanceof NavMesh3D))
799
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'navigation mesh');
800
+ this.navMesh = navMesh;
801
+ }
802
+ addAgent(options) {
803
+ if (this.agents.length >= NAVIGATION3D_LIMITS.maxAgents)
804
+ throw new World3DError('WORLD3D_BUDGET', 'agents');
805
+ const agent = new NavAgent3D(options), place = this.navMesh.findNearest(options.position, 10);
806
+ if (!place)
807
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'the agent is not near the navigation mesh');
808
+ agent.triangle = place.triangle;
809
+ agent.position[0] = place.point[0];
810
+ agent.position[1] = place.point[1];
811
+ agent.position[2] = place.point[2];
812
+ this.agents.push(agent);
813
+ this.sync(agent);
814
+ return agent;
815
+ }
816
+ removeAgent(agent) {
817
+ const index = this.agents.indexOf(agent);
818
+ if (index >= 0)
819
+ this.agents.splice(index, 1);
820
+ }
821
+ sync(agent, dt = 0) {
822
+ const node = agent.node;
823
+ if (node) {
824
+ node.position.set(agent.position);
825
+ const turn = agent.turnSpeed, vx = agent.velocity[0], vz = agent.velocity[2];
826
+ // Standing still, or nearly, keeps the way it faced.
827
+ if (turn !== undefined && dt > 0 && vx * vx + vz * vz > 1e-4) {
828
+ const wanted = Math.atan2(-vx, -vz);
829
+ let delta = wanted - agent.heading;
830
+ delta -= Math.round(delta / (2 * Math.PI)) * 2 * Math.PI;
831
+ const most = turn * dt;
832
+ agent.heading += Math.abs(delta) <= most ? delta : Math.sign(delta) * most;
833
+ agent.heading -= Math.round(agent.heading / (2 * Math.PI)) * 2 * Math.PI;
834
+ node.setQuaternion(0, Math.sin(agent.heading / 2), 0, Math.cos(agent.heading / 2));
835
+ }
836
+ node.markTransformDirty();
837
+ }
838
+ }
839
+ /** Advance every agent by `deltaSeconds`. */
840
+ update(deltaSeconds) {
841
+ const dt = requireFinite3D(deltaSeconds, 'deltaSeconds'), agents = this.agents, mesh = this.navMesh;
842
+ if (!(dt > 0))
843
+ return;
844
+ // Neighbors are found through a coarse grid so large crowds stay cheap.
845
+ let reach = 0;
846
+ for (const agent of agents)
847
+ reach = Math.max(reach, agent.radius);
848
+ const cell = Math.max(reach * 4, 0.5), buckets = new Map(), key = (x, z) => Math.floor(x / cell) * 73856093 + Math.floor(z / cell) * 19349663;
849
+ for (const agent of agents) {
850
+ const k = key(agent.position[0], agent.position[2]);
851
+ let list = buckets.get(k);
852
+ if (!list)
853
+ buckets.set(k, (list = []));
854
+ list.push(agent);
855
+ }
856
+ for (const agent of agents) {
857
+ const p = agent.position, v = agent.velocity;
858
+ if (agent.stale && agent.target) {
859
+ const path = mesh.findPath(p, agent.target, { agentRadius: agent.radius, snapDistance: 10 });
860
+ agent.stale = false;
861
+ agent.reachable = path?.complete === true;
862
+ agent.corners = path ? path.points.slice(1) : [];
863
+ if (agent.corners.length === 0)
864
+ agent.arrived = true;
865
+ }
866
+ // Wanted velocity: toward the next corner, slowing down for the last one.
867
+ let wx = 0, wz = 0;
868
+ while (agent.corners.length > 0) {
869
+ const corner = agent.corners[0], dx = corner[0] - p[0], dz = corner[2] - p[2], distance = Math.hypot(dx, dz), last = agent.corners.length === 1;
870
+ if (distance < (last ? Math.max(agent.radius * 0.25, 0.05) : agent.radius)) {
871
+ agent.corners.shift();
872
+ continue;
873
+ }
874
+ const speed = last ? Math.min(agent.speed, Math.sqrt(2 * agent.acceleration * distance)) : agent.speed;
875
+ wx = (dx / distance) * speed;
876
+ wz = (dz / distance) * speed;
877
+ break;
878
+ }
879
+ // In a crowd the exact spot may be taken: standing still close to it counts as arriving.
880
+ const remaining = agent.corners.length === 1 ? Math.hypot(agent.corners[0][0] - p[0], agent.corners[0][2] - p[2]) : Infinity;
881
+ if (remaining < agent.closest - 0.05) {
882
+ agent.closest = remaining;
883
+ agent.stalled = 0;
884
+ }
885
+ else if (remaining < agent.radius * 20) {
886
+ agent.stalled += dt;
887
+ // Right beside the spot a short wait is enough; farther back in the queue it takes longer to give up.
888
+ if (agent.stalled > (remaining < agent.radius * 6 ? 0.75 : 2)) {
889
+ agent.corners = [];
890
+ wx = 0;
891
+ wz = 0;
892
+ }
893
+ }
894
+ if (agent.corners.length === 0 && agent.target) {
895
+ agent.arrived = true;
896
+ agent.target = undefined;
897
+ }
898
+ // Keep clear of neighbors: a push away from each one that is too close.
899
+ if (this.separation > 0) {
900
+ for (let cx = -1; cx <= 1; cx++)
901
+ for (let cz = -1; cz <= 1; cz++) {
902
+ const list = buckets.get(key(p[0] + cx * cell, p[2] + cz * cell));
903
+ if (!list)
904
+ continue;
905
+ for (const other of list) {
906
+ if (other === agent)
907
+ continue;
908
+ const dx = p[0] - other.position[0], dz = p[2] - other.position[2], distance = Math.hypot(dx, dz), room = agent.radius + other.radius;
909
+ if (distance >= room * 1.5 || Math.abs(p[1] - other.position[1]) > 2)
910
+ continue;
911
+ // Agents standing on the same spot part along a direction of their own.
912
+ const away = distance > 1e-6 ? 1 / distance : 0, ax = away ? dx * away : Math.cos(agents.indexOf(agent)), az = away ? dz * away : Math.sin(agents.indexOf(agent));
913
+ const push = ((room * 1.5 - distance) / (room * 1.5)) * agent.speed * this.separation * 1.5;
914
+ wx += ax * push;
915
+ wz += az * push;
916
+ }
917
+ }
918
+ }
919
+ const ax = wx - v[0], az = wz - v[2], change = Math.hypot(ax, az), most = agent.acceleration * dt;
920
+ if (change <= most) {
921
+ v[0] = wx;
922
+ v[2] = wz;
923
+ }
924
+ else {
925
+ v[0] += (ax / change) * most;
926
+ v[2] += (az / change) * most;
927
+ }
928
+ if (v[0] === 0 && v[2] === 0)
929
+ continue;
930
+ const moved = mesh.moveAlong({ triangle: agent.triangle, point: p }, p[0] + v[0] * dt, p[2] + v[2] * dt);
931
+ // Report the speed really made, so an agent pressed against an edge reads as slow.
932
+ v[0] = (moved.point[0] - p[0]) / dt;
933
+ v[1] = (moved.point[1] - p[1]) / dt;
934
+ v[2] = (moved.point[2] - p[2]) / dt;
935
+ agent.triangle = moved.triangle;
936
+ p[0] = moved.point[0];
937
+ p[1] = moved.point[1];
938
+ p[2] = moved.point[2];
939
+ }
940
+ // Agents that still overlap are moved apart, half the overlap each, staying on the mesh.
941
+ if (this.separation > 0) {
942
+ for (let i = 0; i < agents.length; i++) {
943
+ const agent = agents[i], p = agent.position;
944
+ for (let cx = -1; cx <= 1; cx++)
945
+ for (let cz = -1; cz <= 1; cz++) {
946
+ const list = buckets.get(key(p[0] + cx * cell, p[2] + cz * cell));
947
+ if (!list)
948
+ continue;
949
+ for (const other of list) {
950
+ if (other === agent || Math.abs(p[1] - other.position[1]) > 2)
951
+ continue;
952
+ const dx = p[0] - other.position[0], dz = p[2] - other.position[2], distance = Math.hypot(dx, dz), overlap = agent.radius + other.radius - distance;
953
+ if (overlap <= 0 || distance < 1e-6)
954
+ continue;
955
+ const moved = mesh.moveAlong({ triangle: agent.triangle, point: p }, p[0] + (dx / distance) * overlap * 0.5, p[2] + (dz / distance) * overlap * 0.5);
956
+ agent.triangle = moved.triangle;
957
+ p[0] = moved.point[0];
958
+ p[1] = moved.point[1];
959
+ p[2] = moved.point[2];
960
+ }
961
+ }
962
+ }
963
+ }
964
+ for (const agent of agents)
965
+ this.sync(agent, dt);
966
+ }
967
+ }
968
+ /** Makes a crowd of agents that move along a navigation mesh and keep apart. */
969
+ export function createNavCrowd3D(navMesh) {
970
+ return new NavCrowd3D(navMesh);
971
+ }
972
+ /**
973
+ * Shortest path on a grid of cells, for tile maps. `cost(x, y)` returns the
974
+ * finite positive price of entering a cell (including fractional costs).
975
+ * All other values mark a blocked cell. Returns the cells from start to end
976
+ * inclusive, or undefined when there is no way.
977
+ */
978
+ export function findGridPath3D(width, height, cost, start, end, options = {}) {
979
+ if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0)
980
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'grid size');
981
+ if (width * height > NAVIGATION3D_LIMITS.maxGridCells)
982
+ throw new World3DError('WORLD3D_BUDGET', 'grid size');
983
+ const inside = (x, y) => Number.isInteger(x) && Number.isInteger(y) && x >= 0 && y >= 0 && x < width && y < height;
984
+ if (!inside(start[0], start[1]) || !inside(end[0], end[1]))
985
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'grid cell');
986
+ const priceAt = (x, y) => {
987
+ const price = cost(x, y);
988
+ return Number.isFinite(price) && price > 0 ? price : 0;
989
+ };
990
+ if (!priceAt(end[0], end[1]))
991
+ return undefined;
992
+ const total = width * height;
993
+ const distances = new Float64Array(total).fill(Infinity);
994
+ const from = new Int32Array(total).fill(-1);
995
+ const closed = new Uint8Array(total);
996
+ const heap = new Heap(distances);
997
+ const diagonal = options.diagonal ?? true, limit = options.maxVisited ?? total, goal = end[1] * width + end[0], first = start[1] * width + start[0];
998
+ // Without a lower bound on callback costs, Dijkstra preserves the cheapest route.
999
+ distances[first] = 0;
1000
+ heap.push(first);
1001
+ for (let visited = 0; heap.size > 0 && visited < limit;) {
1002
+ const current = heap.pop();
1003
+ if (current === undefined)
1004
+ break;
1005
+ if (closed[current])
1006
+ continue;
1007
+ closed[current] = 1;
1008
+ visited++;
1009
+ if (current === goal) {
1010
+ const path = [];
1011
+ for (let cell = goal; cell >= 0; cell = from[cell])
1012
+ path.push([cell % width, Math.floor(cell / width)]);
1013
+ return path.reverse();
1014
+ }
1015
+ const x = current % width, y = Math.floor(current / width);
1016
+ for (let dy = -1; dy <= 1; dy++)
1017
+ for (let dx = -1; dx <= 1; dx++) {
1018
+ if ((dx === 0 && dy === 0) || (!diagonal && dx !== 0 && dy !== 0))
1019
+ continue;
1020
+ const nx = x + dx, ny = y + dy;
1021
+ if (nx < 0 || ny < 0 || nx >= width || ny >= height)
1022
+ continue;
1023
+ const next = ny * width + nx;
1024
+ if (closed[next])
1025
+ continue;
1026
+ const price = priceAt(nx, ny);
1027
+ if (price === 0)
1028
+ continue;
1029
+ // A diagonal step needs both cells beside it open.
1030
+ const diagonalStep = dx !== 0 && dy !== 0;
1031
+ if (diagonalStep && (!priceAt(x + dx, y) || !priceAt(x, y + dy)))
1032
+ continue;
1033
+ const reached = distances[current] + price * (diagonalStep ? Math.SQRT2 : 1);
1034
+ if (reached >= distances[next])
1035
+ continue;
1036
+ distances[next] = reached;
1037
+ from[next] = current;
1038
+ heap.push(next);
1039
+ }
1040
+ }
1041
+ return undefined;
1042
+ }
1043
+ //# sourceMappingURL=Navigation3D.js.map