@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,827 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Height-map terrain: a grid of heights that answers gameplay queries, a
3
+ // chunked mesh whose pieces drop detail with distance, and a collision shape
4
+ // built from the same triangles. DOM-free; decode images in the host and pass
5
+ // the pixels in.
6
+ import { World3DError, requireCount3D, requireFinite3D, requirePositive3D } from './core3D.js';
7
+ import { Attribute3D, Geometry3D } from './Geometry3D.js';
8
+ import { createStandardMaterial3D } from './Material3D.js';
9
+ import { DECAL3D_LIMITS, createDecalGeometry3D } from './Decal3D.js';
10
+ import { mat4Invert, vec3TransformMat4 } from './math3D.js';
11
+ import { Mesh3D } from './Mesh3D.js';
12
+ import { Object3D } from './Object3D.js';
13
+ import { TriangleMeshShape3D } from './PhysicsMesh3D.js';
14
+ /** The largest height field, chunk and number of levels and chunks terrain has, and the most streamed tiles. */
15
+ export const TERRAIN3D_LIMITS = Object.freeze({
16
+ maxSamplesPerSide: 4097,
17
+ maxChunkCells: 254,
18
+ maxLevels: 8,
19
+ maxChunks: 4096,
20
+ maxCachedGeometryBytes: 128 * 1024 * 1024,
21
+ maxStreamTiles: 256,
22
+ maxStreamPending: 32
23
+ });
24
+ /**
25
+ * Heights on a regular grid in the XZ plane. Every cell is split into two
26
+ * triangles along the same diagonal, and `heightAt` follows those triangles
27
+ * exactly, so an object placed with it sits on the full-detail mesh and on the
28
+ * collision shape alike.
29
+ */
30
+ export class HeightField3D {
31
+ columns;
32
+ rows;
33
+ width;
34
+ depth;
35
+ heights;
36
+ /** Increases on refresh(); terrains rebuild their meshes when it changes. */
37
+ version = 1;
38
+ #min = 0;
39
+ #max = 0;
40
+ constructor(options) {
41
+ if (!options)
42
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'height field options');
43
+ this.columns = requireCount3D(options.columns, 'columns', TERRAIN3D_LIMITS.maxSamplesPerSide);
44
+ this.rows = requireCount3D(options.rows, 'rows', TERRAIN3D_LIMITS.maxSamplesPerSide);
45
+ if (this.columns < 2 || this.rows < 2)
46
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'a height field needs at least 2 x 2 samples');
47
+ this.width = requirePositive3D(options.width, 'width');
48
+ this.depth = requirePositive3D(options.depth, 'depth');
49
+ const count = this.columns * this.rows;
50
+ this.heights = new Float32Array(count);
51
+ if (options.heights !== undefined) {
52
+ if (options.heights.length !== count)
53
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'heights length');
54
+ for (let i = 0; i < count; i++)
55
+ this.heights[i] = requireFinite3D(options.heights[i], 'heights');
56
+ }
57
+ else if (options.sample !== undefined) {
58
+ for (let row = 0; row < this.rows; row++) {
59
+ for (let column = 0; column < this.columns; column++)
60
+ this.heights[row * this.columns + column] = requireFinite3D(options.sample(this.columnX(column), this.rowZ(row)), 'sample');
61
+ }
62
+ }
63
+ this.refresh();
64
+ }
65
+ get minHeight() {
66
+ return this.#min;
67
+ }
68
+ get maxHeight() {
69
+ return this.#max;
70
+ }
71
+ get cellWidth() {
72
+ return this.width / (this.columns - 1);
73
+ }
74
+ get cellDepth() {
75
+ return this.depth / (this.rows - 1);
76
+ }
77
+ /** Local x of a sample column and local z of a sample row. */
78
+ columnX(column) {
79
+ return (column / (this.columns - 1)) * this.width - this.width / 2;
80
+ }
81
+ rowZ(row) {
82
+ return (row / (this.rows - 1)) * this.depth - this.depth / 2;
83
+ }
84
+ /** Height stored at a sample; indices are clamped to the grid. */
85
+ sampleAt(column, row) {
86
+ const c = column < 0 ? 0 : column >= this.columns ? this.columns - 1 : column | 0, r = row < 0 ? 0 : row >= this.rows ? this.rows - 1 : row | 0;
87
+ return this.heights[r * this.columns + c];
88
+ }
89
+ /** Call after writing to `heights`: recomputes the range and bumps the version. */
90
+ refresh() {
91
+ let min = Infinity, max = -Infinity;
92
+ for (let i = 0; i < this.heights.length; i++) {
93
+ const h = this.heights[i];
94
+ if (!Number.isFinite(h))
95
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'heights');
96
+ if (h < min)
97
+ min = h;
98
+ if (h > max)
99
+ max = h;
100
+ }
101
+ this.#min = min;
102
+ this.#max = max;
103
+ this.version++;
104
+ }
105
+ /** True when the local point lies over the grid. */
106
+ contains(x, z) {
107
+ return Math.abs(x) <= this.width / 2 && Math.abs(z) <= this.depth / 2;
108
+ }
109
+ /** Surface height under a local point; points outside the grid use the nearest edge. */
110
+ heightAt(x, z) {
111
+ const columns = this.columns, h = this.heights;
112
+ let gx = (x / this.width + 0.5) * (columns - 1), gz = (z / this.depth + 0.5) * (this.rows - 1);
113
+ gx = gx < 0 ? 0 : gx > columns - 1 ? columns - 1 : gx;
114
+ gz = gz < 0 ? 0 : gz > this.rows - 1 ? this.rows - 1 : gz;
115
+ const c = Math.min(gx | 0, columns - 2), r = Math.min(gz | 0, this.rows - 2), fx = gx - c, fz = gz - r, at = r * columns + c;
116
+ const a = h[at], b = h[at + 1], d = h[at + columns], e = h[at + columns + 1];
117
+ // The cell's diagonal runs from the +X corner to the +Z corner.
118
+ return fx + fz <= 1 ? a + (b - a) * fx + (d - a) * fz : e + (d - e) * (1 - fx) + (b - e) * (1 - fz);
119
+ }
120
+ /** Smoothed unit surface normal at a local point, from the neighbouring samples. */
121
+ normalAt(x, z, out = [0, 0, 0]) {
122
+ const dx = this.cellWidth, dz = this.cellDepth;
123
+ const nx = (this.heightAt(x - dx, z) - this.heightAt(x + dx, z)) / (2 * dx), nz = (this.heightAt(x, z - dz) - this.heightAt(x, z + dz)) / (2 * dz), length = Math.hypot(nx, 1, nz);
124
+ out[0] = nx / length;
125
+ out[1] = 1 / length;
126
+ out[2] = nz / length;
127
+ return out;
128
+ }
129
+ /**
130
+ * Distance along a unit local ray to where it first drops below the
131
+ * surface, or -1 within `limit`. The ray is walked in half-cell steps and
132
+ * the crossing refined, so features thinner than a cell can be skipped.
133
+ */
134
+ raycast(origin, direction, limit = Infinity) {
135
+ const ox = origin[0], oy = origin[1], oz = origin[2], dx = direction[0], dy = direction[1], dz = direction[2];
136
+ const hx = this.width / 2, hz = this.depth / 2;
137
+ // Clip the ray to the slab that holds the terrain.
138
+ let near = 0, far = limit;
139
+ const clip = (o, d, low, high) => {
140
+ if (Math.abs(d) < 1e-12)
141
+ return o >= low && o <= high;
142
+ let t0 = (low - o) / d, t1 = (high - o) / d;
143
+ if (t0 > t1) {
144
+ const swap = t0;
145
+ t0 = t1;
146
+ t1 = swap;
147
+ }
148
+ if (t0 > near)
149
+ near = t0;
150
+ if (t1 < far)
151
+ far = t1;
152
+ return near <= far;
153
+ };
154
+ if (!clip(ox, dx, -hx, hx) || !clip(oz, dz, -hz, hz) || !clip(oy, dy, this.#min - 1e-3, Infinity))
155
+ return -1;
156
+ if (!Number.isFinite(far))
157
+ return -1;
158
+ const step = Math.min(this.cellWidth, this.cellDepth) * 0.5;
159
+ let previous = near;
160
+ if (oy + dy * near < this.heightAt(ox + dx * near, oz + dz * near))
161
+ return near > 0 ? near : -1;
162
+ for (let t = near + step;; t += step) {
163
+ const at = Math.min(t, far);
164
+ if (oy + dy * at <= this.heightAt(ox + dx * at, oz + dz * at)) {
165
+ let low = previous, high = at;
166
+ for (let i = 0; i < 24; i++) {
167
+ const middle = (low + high) / 2;
168
+ if (oy + dy * middle <= this.heightAt(ox + dx * middle, oz + dz * middle))
169
+ high = middle;
170
+ else
171
+ low = middle;
172
+ }
173
+ return high;
174
+ }
175
+ if (at >= far)
176
+ return -1;
177
+ previous = at;
178
+ }
179
+ }
180
+ /**
181
+ * Collision shape for a static physics body placed at the terrain's
182
+ * origin: the full-detail triangles, indexed by a bounding-volume
183
+ * hierarchy so bodies only test the ground near them.
184
+ */
185
+ createShape() {
186
+ const columns = this.columns, rows = this.rows, positions = new Float32Array(columns * rows * 3), indices = new Uint32Array((columns - 1) * (rows - 1) * 6);
187
+ for (let row = 0, at = 0; row < rows; row++) {
188
+ for (let column = 0; column < columns; column++) {
189
+ positions[at++] = this.columnX(column);
190
+ positions[at++] = this.heights[row * columns + column];
191
+ positions[at++] = this.rowZ(row);
192
+ }
193
+ }
194
+ for (let row = 0, at = 0; row < rows - 1; row++) {
195
+ for (let column = 0; column < columns - 1; column++) {
196
+ const a = row * columns + column, b = a + 1, d = a + columns, e = d + 1;
197
+ indices[at++] = a;
198
+ indices[at++] = d;
199
+ indices[at++] = b;
200
+ indices[at++] = b;
201
+ indices[at++] = d;
202
+ indices[at++] = e;
203
+ }
204
+ }
205
+ return { type: 'mesh', mesh: new TriangleMeshShape3D(positions, indices) };
206
+ }
207
+ }
208
+ /** Makes a grid of heights for terrain and physics. */
209
+ export function createHeightField3D(options) {
210
+ return new HeightField3D(options);
211
+ }
212
+ /** Height field with one sample per pixel of a decoded RGBA image; the image's top row is the -Z edge. */
213
+ export function createHeightFieldFromPixels3D(pixels, options) {
214
+ if (!pixels || !pixels.data || pixels.data.length !== pixels.width * pixels.height * 4)
215
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'height image pixels');
216
+ const low = requireFinite3D(options.minHeight ?? 0, 'minHeight'), high = requireFinite3D(options.maxHeight ?? 1, 'maxHeight'), wide = options.encoding === 'rg16';
217
+ if (options.encoding !== undefined && options.encoding !== 'red' && !wide)
218
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'height image encoding');
219
+ const count = pixels.width * pixels.height, heights = new Float32Array(count), data = pixels.data;
220
+ for (let i = 0; i < count; i++)
221
+ heights[i] = low + (high - low) * (wide ? (data[i * 4] * 256 + data[i * 4 + 1]) / 65535 : data[i * 4] / 255);
222
+ return new HeightField3D({
223
+ columns: pixels.width,
224
+ rows: pixels.height,
225
+ width: options.width,
226
+ depth: options.depth,
227
+ heights
228
+ });
229
+ }
230
+ function chunkBufferLayout(nx, nz, skirts) {
231
+ const vertexCount = nx * nz + (skirts ? 2 * (nx + nz) : 0);
232
+ const indexCount = 6 * ((nx - 1) * (nz - 1) + (skirts ? 2 * (nx + nz - 2) : 0));
233
+ return { vertexCount, indexCount, indexBytes: vertexCount > 65535 ? 4 : 2 };
234
+ }
235
+ /**
236
+ * A height field drawn as a grid of chunk meshes. Before each frame every
237
+ * chunk picks a detail level from its distance to the camera; neighbouring
238
+ * chunks at different levels are sealed by skirts hanging from their borders.
239
+ * Normals always come from the full-detail field, so lighting does not shift
240
+ * when a chunk changes level.
241
+ */
242
+ export class Terrain3D extends Object3D {
243
+ field;
244
+ chunkCells;
245
+ levels;
246
+ lodDistance;
247
+ /** Added to every chunk's level: raise it to lower the detail of the whole terrain. */
248
+ lodBias = 0;
249
+ #chunks = [];
250
+ #decalGeometries = new WeakMap();
251
+ #skirt;
252
+ #uvScale;
253
+ #colorAt;
254
+ #fieldVersion;
255
+ #material;
256
+ constructor(field, material, options = {}) {
257
+ super();
258
+ if (!(field instanceof HeightField3D))
259
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain field');
260
+ this.field = field;
261
+ this.#material = material;
262
+ this.chunkCells = requireCount3D(options.chunkCells ?? 32, 'chunkCells', TERRAIN3D_LIMITS.maxChunkCells);
263
+ this.levels = requireCount3D(options.levels ?? 4, 'levels', TERRAIN3D_LIMITS.maxLevels);
264
+ if (this.chunkCells < 1 || this.levels < 1)
265
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain chunks and levels');
266
+ this.lodDistance = requirePositive3D(options.lodDistance ?? Math.max(field.cellWidth, field.cellDepth) * this.chunkCells * 2, 'lodDistance');
267
+ this.#skirt =
268
+ options.skirtDepth === undefined
269
+ ? Math.max(field.width, field.depth) * 0.02
270
+ : requireFinite3D(options.skirtDepth, 'skirtDepth');
271
+ if (this.#skirt < 0)
272
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'skirtDepth');
273
+ this.#uvScale = requirePositive3D(options.uvScale ?? 1, 'uvScale');
274
+ this.#colorAt = options.colorAt;
275
+ this.checkGeometryBudget();
276
+ this.#fieldVersion = field.version;
277
+ for (let row = 0; row < field.rows - 1; row += this.chunkCells) {
278
+ for (let column = 0; column < field.columns - 1; column += this.chunkCells) {
279
+ const mesh = new Mesh3D(new Geometry3D(), material);
280
+ const chunk = {
281
+ mesh,
282
+ column,
283
+ row,
284
+ columns: Math.min(this.chunkCells, field.columns - 1 - column),
285
+ rows: Math.min(this.chunkCells, field.rows - 1 - row),
286
+ geometries: [],
287
+ level: -1
288
+ };
289
+ mesh.name = 'terrain-chunk';
290
+ mesh.beforeRender = (camera) => this.select(chunk, camera);
291
+ this.#chunks.push(chunk);
292
+ this.addChild(mesh);
293
+ this.use(chunk, this.levels - 1);
294
+ }
295
+ }
296
+ }
297
+ get material() {
298
+ return this.#material;
299
+ }
300
+ set material(value) {
301
+ this.#material = value;
302
+ for (const chunk of this.#chunks)
303
+ chunk.mesh.material = value;
304
+ }
305
+ get chunkCount() {
306
+ return this.#chunks.length;
307
+ }
308
+ /** Detail level each chunk is currently drawn at, in row-major chunk order. */
309
+ get chunkLevels() {
310
+ return this.#chunks.map(chunk => chunk.level);
311
+ }
312
+ /** Triangles in the levels the chunks currently use. */
313
+ get triangleCount() {
314
+ let total = 0;
315
+ for (const chunk of this.#chunks)
316
+ total += chunk.mesh.geometry.drawCount / 3;
317
+ return total;
318
+ }
319
+ /** Set shadow casting and receiving on every chunk. */
320
+ setShadows(cast, receive) {
321
+ for (const chunk of this.#chunks) {
322
+ chunk.mesh.castShadow = cast;
323
+ chunk.mesh.receiveShadow = receive;
324
+ }
325
+ return this;
326
+ }
327
+ /**
328
+ * Choose every chunk's level for a camera now. Rendering does this by
329
+ * itself; call it when you read `chunkLevels` or `triangleCount` first.
330
+ */
331
+ update(camera) {
332
+ this.updateWorldMatrices();
333
+ for (const chunk of this.#chunks)
334
+ this.select(chunk, camera);
335
+ }
336
+ /** Rebuild the meshes after the field's heights changed and `field.refresh()` ran. Rendering checks this by itself. */
337
+ rebuild() {
338
+ this.#fieldVersion = this.field.version;
339
+ for (const chunk of this.#chunks) {
340
+ for (const geometry of chunk.geometries)
341
+ geometry?.dispose();
342
+ chunk.geometries.length = 0;
343
+ const level = chunk.level < 0 ? this.levels - 1 : chunk.level;
344
+ chunk.level = -1;
345
+ this.use(chunk, level);
346
+ }
347
+ }
348
+ /**
349
+ * Lay a decal on the terrain. It is cut from the full-detail heights, not
350
+ * from whichever level a chunk happens to show, so it stays put when the
351
+ * levels change; far away, where chunks are coarse, it can sink slightly
352
+ * into them. Attached decal geometry is released with the terrain; a
353
+ * reparented decal belongs to the caller. Returns undefined when the box misses the ground.
354
+ */
355
+ createDecal(material, options) {
356
+ const field = this.field, inverse = new Float64Array(16), local = [0, 0, 0];
357
+ this.updateWorldMatrix();
358
+ if (!mat4Invert(inverse, this.matrixWorld))
359
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'singular world matrix');
360
+ const position = options.position;
361
+ vec3TransformMat4(local, [
362
+ requireFinite3D(position[0], 'position'),
363
+ requireFinite3D(position[1], 'position'),
364
+ requireFinite3D(position[2], 'position')
365
+ ], inverse);
366
+ const width = requirePositive3D(options.width ?? 1, 'width'), height = requirePositive3D(options.height ?? 1, 'height');
367
+ const depth = requirePositive3D(options.depth ?? Math.max(width, height) / 2, 'depth');
368
+ // The world-space sphere encloses the rotated decal box. Project it onto local X and Z.
369
+ const reach = Math.hypot(width, height, depth) / 2;
370
+ const reachX = reach * Math.hypot(inverse[0], inverse[4], inverse[8]);
371
+ const reachZ = reach * Math.hypot(inverse[2], inverse[6], inverse[10]);
372
+ const toColumn = (x) => (x / field.width + 0.5) * (field.columns - 1), toRow = (z) => (z / field.depth + 0.5) * (field.rows - 1);
373
+ const c0 = Math.max(0, Math.floor(toColumn(local[0] - reachX))), c1 = Math.min(field.columns - 1, Math.ceil(toColumn(local[0] + reachX)));
374
+ const r0 = Math.max(0, Math.floor(toRow(local[2] - reachZ))), r1 = Math.min(field.rows - 1, Math.ceil(toRow(local[2] + reachZ)));
375
+ if (c1 <= c0 || r1 <= r0)
376
+ return undefined;
377
+ const triangleCount = 2 * (c1 - c0) * (r1 - r0);
378
+ if (triangleCount > DECAL3D_LIMITS.maxTriangles)
379
+ throw new World3DError('WORLD3D_BUDGET', 'terrain decal patch triangles');
380
+ // The bounded full-detail ground patch supplies the triangles for the exact decal clip.
381
+ const nx = c1 - c0 + 1, nz = r1 - r0 + 1, positions = new Float32Array(nx * nz * 3), normals = new Float32Array(nx * nz * 3);
382
+ const indices = new Uint32Array(triangleCount * 3), normal = [0, 0, 0];
383
+ for (let iz = 0; iz < nz; iz++)
384
+ for (let ix = 0; ix < nx; ix++) {
385
+ const o = (iz * nx + ix) * 3, x = field.columnX(c0 + ix), z = field.rowZ(r0 + iz);
386
+ positions[o] = x;
387
+ positions[o + 1] = field.heights[(r0 + iz) * field.columns + c0 + ix];
388
+ positions[o + 2] = z;
389
+ field.normalAt(x, z, normal);
390
+ normals[o] = normal[0];
391
+ normals[o + 1] = normal[1];
392
+ normals[o + 2] = normal[2];
393
+ }
394
+ let at = 0;
395
+ for (let iz = 0; iz < nz - 1; iz++)
396
+ for (let ix = 0; ix < nx - 1; ix++) {
397
+ const a = iz * nx + ix, b = a + 1, d = a + nx, e = d + 1;
398
+ indices[at++] = a;
399
+ indices[at++] = d;
400
+ indices[at++] = b;
401
+ indices[at++] = b;
402
+ indices[at++] = d;
403
+ indices[at++] = e;
404
+ }
405
+ const patch = new Geometry3D();
406
+ patch.setAttribute('position', new Attribute3D(positions, 3));
407
+ patch.setAttribute('normal', new Attribute3D(normals, 3));
408
+ patch.setIndex(indices);
409
+ const source = new Mesh3D(patch, material);
410
+ this.addChild(source);
411
+ let geometry;
412
+ try {
413
+ geometry = createDecalGeometry3D(source, { offset: 0.03, ...options });
414
+ }
415
+ finally {
416
+ source.removeFromParent();
417
+ patch.dispose();
418
+ }
419
+ if (!geometry)
420
+ return undefined;
421
+ material.depthWrite = false;
422
+ material.polygonOffset ??= [-4, -8];
423
+ const decal = new Mesh3D(geometry, material);
424
+ decal.name = 'decal';
425
+ decal.castShadow = false;
426
+ decal.receiveShadow = true;
427
+ this.addChild(decal);
428
+ this.#decalGeometries.set(decal, geometry);
429
+ return decal;
430
+ }
431
+ /** Release chunk geometry and attached factory decals. Materials and the field stay with the caller. */
432
+ dispose() {
433
+ const failures = [];
434
+ for (const chunk of this.#chunks.splice(0)) {
435
+ chunk.mesh.removeFromParent();
436
+ for (const geometry of chunk.geometries.splice(0)) {
437
+ try {
438
+ geometry?.dispose();
439
+ }
440
+ catch (cause) {
441
+ failures.push(cause);
442
+ }
443
+ }
444
+ }
445
+ for (const child of [...this.children]) {
446
+ if (child.parent !== this)
447
+ continue;
448
+ const geometry = this.#decalGeometries.get(child);
449
+ if (!geometry)
450
+ continue;
451
+ this.#decalGeometries.delete(child);
452
+ child.removeFromParent();
453
+ try {
454
+ geometry.dispose();
455
+ }
456
+ catch (cause) {
457
+ failures.push(cause);
458
+ }
459
+ }
460
+ if (failures.length === 1)
461
+ throw failures[0];
462
+ if (failures.length > 1)
463
+ throw new AggregateError(failures, 'WORLD3D_TERRAIN_DISPOSE_FAILED');
464
+ }
465
+ checkGeometryBudget() {
466
+ const columns = this.field.columns - 1, rows = this.field.rows - 1, chunkCells = this.chunkCells;
467
+ if (Math.ceil(columns / chunkCells) * Math.ceil(rows / chunkCells) > TERRAIN3D_LIMITS.maxChunks) {
468
+ throw new World3DError('WORLD3D_BUDGET', 'terrain chunk count');
469
+ }
470
+ // Every LOD may remain cached. Count all retained attribute and index bytes before building any mesh.
471
+ const bytesPerVertex = (this.#colorAt ? 11 : 8) * Float32Array.BYTES_PER_ELEMENT;
472
+ const skirts = this.#skirt > 0;
473
+ let bytes = 0;
474
+ for (let row = 0; row < rows; row += chunkCells) {
475
+ for (let column = 0; column < columns; column += chunkCells) {
476
+ const width = Math.min(chunkCells, columns - column), depth = Math.min(chunkCells, rows - row);
477
+ for (let level = 0; level < this.levels; level++) {
478
+ const stride = 1 << level;
479
+ const { vertexCount, indexCount, indexBytes } = chunkBufferLayout(Math.ceil(width / stride) + 1, Math.ceil(depth / stride) + 1, skirts);
480
+ bytes += vertexCount * bytesPerVertex + indexCount * indexBytes;
481
+ if (bytes > TERRAIN3D_LIMITS.maxCachedGeometryBytes) {
482
+ throw new World3DError('WORLD3D_BUDGET', 'terrain cached geometry bytes');
483
+ }
484
+ }
485
+ }
486
+ }
487
+ }
488
+ select(chunk, camera) {
489
+ if (this.#fieldVersion !== this.field.version)
490
+ this.rebuild();
491
+ const sphere = chunk.mesh.worldBoundingSphere, eye = camera.matrixWorld;
492
+ const distance = Math.hypot(eye[12] - sphere[0], eye[13] - sphere[1], eye[14] - sphere[2]) - sphere[3];
493
+ let level = distance <= this.lodDistance ? 0 : Math.floor(Math.log2(distance / this.lodDistance)) + 1;
494
+ level = Math.max(0, Math.min(this.levels - 1, level + this.lodBias));
495
+ if (level !== chunk.level)
496
+ this.use(chunk, level);
497
+ }
498
+ use(chunk, level) {
499
+ chunk.mesh.geometry = chunk.geometries[level] ??= this.build(chunk, level);
500
+ chunk.level = level;
501
+ }
502
+ build(chunk, level) {
503
+ const field = this.field, stride = 1 << level;
504
+ // Sample lines of this level; the last one always lands on the chunk's edge.
505
+ const line = (start, cells) => {
506
+ const out = [];
507
+ for (let i = 0; i < cells; i += stride)
508
+ out.push(start + i);
509
+ out.push(start + cells);
510
+ return out;
511
+ };
512
+ const xs = line(chunk.column, chunk.columns), zs = line(chunk.row, chunk.rows), nx = xs.length, nz = zs.length;
513
+ const skirts = this.#skirt > 0;
514
+ const { vertexCount, indexCount, indexBytes } = chunkBufferLayout(nx, nz, skirts);
515
+ const positions = new Float32Array(vertexCount * 3), normals = new Float32Array(vertexCount * 3), uvs = new Float32Array(vertexCount * 2);
516
+ const colors = this.#colorAt ? new Float32Array(vertexCount * 3) : undefined, normal = [0, 0, 0], color = [1, 1, 1];
517
+ const write = (to, column, row, drop) => {
518
+ const x = field.columnX(column), z = field.rowZ(row), y = field.heights[row * field.columns + column];
519
+ field.normalAt(x, z, normal);
520
+ positions[to * 3] = x;
521
+ positions[to * 3 + 1] = y - drop;
522
+ positions[to * 3 + 2] = z;
523
+ normals[to * 3] = normal[0];
524
+ normals[to * 3 + 1] = normal[1];
525
+ normals[to * 3 + 2] = normal[2];
526
+ uvs[to * 2] = (column / (field.columns - 1)) * this.#uvScale;
527
+ uvs[to * 2 + 1] = (row / (field.rows - 1)) * this.#uvScale;
528
+ if (colors) {
529
+ color[0] = color[1] = color[2] = 1;
530
+ this.#colorAt(y, 1 - normal[1], x, z, color);
531
+ colors[to * 3] = color[0];
532
+ colors[to * 3 + 1] = color[1];
533
+ colors[to * 3 + 2] = color[2];
534
+ }
535
+ };
536
+ for (let iz = 0; iz < nz; iz++)
537
+ for (let ix = 0; ix < nx; ix++)
538
+ write(iz * nx + ix, xs[ix], zs[iz], 0);
539
+ const indices = indexBytes === 4 ? new Uint32Array(indexCount) : new Uint16Array(indexCount);
540
+ let at = 0;
541
+ const quad = (a, b, c, d, e, f) => {
542
+ indices[at++] = a;
543
+ indices[at++] = b;
544
+ indices[at++] = c;
545
+ indices[at++] = d;
546
+ indices[at++] = e;
547
+ indices[at++] = f;
548
+ };
549
+ for (let iz = 0; iz < nz - 1; iz++) {
550
+ for (let ix = 0; ix < nx - 1; ix++) {
551
+ const a = iz * nx + ix, b = a + 1, d = a + nx, e = d + 1;
552
+ quad(a, d, b, b, d, e);
553
+ }
554
+ }
555
+ if (skirts) {
556
+ // Each border gets a strip hanging down; `flip` keeps its face pointing outward.
557
+ let next = nx * nz;
558
+ const side = (length, top, column, row, flip) => {
559
+ const first = next;
560
+ for (let i = 0; i < length; i++)
561
+ write(next++, column(i), row(i), this.#skirt);
562
+ for (let i = 0; i < length - 1; i++) {
563
+ const t0 = top(i), t1 = top(i + 1), b0 = first + i, b1 = b0 + 1;
564
+ if (flip)
565
+ quad(t0, t1, b0, t1, b1, b0);
566
+ else
567
+ quad(t0, b0, t1, t1, b0, b1);
568
+ }
569
+ };
570
+ side(nx, i => i, i => xs[i], () => zs[0], true);
571
+ side(nx, i => (nz - 1) * nx + i, i => xs[i], () => zs[nz - 1], false);
572
+ side(nz, i => i * nx, () => xs[0], i => zs[i], false);
573
+ side(nz, i => i * nx + nx - 1, () => xs[nx - 1], i => zs[i], true);
574
+ }
575
+ const geometry = new Geometry3D();
576
+ geometry.setAttribute('position', new Attribute3D(positions, 3));
577
+ geometry.setAttribute('normal', new Attribute3D(normals, 3));
578
+ geometry.setAttribute('uv', new Attribute3D(uvs, 2));
579
+ if (colors)
580
+ geometry.setAttribute('color', new Attribute3D(colors, 3));
581
+ geometry.setIndex(indices);
582
+ return geometry;
583
+ }
584
+ }
585
+ /** Makes terrain from a height field, drawn in chunks at levels of detail. */
586
+ export function createTerrain3D(field, material, options = {}) {
587
+ return new Terrain3D(field, material, options);
588
+ }
589
+ /**
590
+ * A standard material whose color is a blend of ground textures, such as
591
+ * sand, grass, rock and snow, painted by a weight map. Use it on a terrain
592
+ * with `uvScale` 1 so the weight map covers the terrain once.
593
+ */
594
+ export function createTerrainMaterial3D(options) {
595
+ if (!options ||
596
+ !Array.isArray(options.layers) ||
597
+ options.layers.length < 1 ||
598
+ options.layers.length > 4 ||
599
+ !options.weights)
600
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain layers');
601
+ const repeat = [0, 1, 2, 3].map(i => requirePositive3D(typeof options.repeat === 'number' ? options.repeat : (options.repeat?.[i] ?? 32), 'repeat'));
602
+ const material = createStandardMaterial3D({ roughness: 0.95, ...options });
603
+ material.splat = Object.freeze({
604
+ weights: options.weights,
605
+ layers: Object.freeze([...options.layers]),
606
+ repeat: Object.freeze(repeat)
607
+ });
608
+ return material;
609
+ }
610
+ /** Terrain loaded in tiles around the camera: read heights, and update it as the camera moves. */
611
+ export class TerrainStream3D extends Object3D {
612
+ tileSize;
613
+ viewDistance;
614
+ #options;
615
+ #loadsPerUpdate;
616
+ #tiles = new Map();
617
+ // Each request owns its key until it leaves retention or is published.
618
+ #requests = new Map();
619
+ #outstanding = 0;
620
+ #disposed = false;
621
+ #updating = false;
622
+ #lastFailure;
623
+ constructor(options) {
624
+ super();
625
+ if (!options || typeof options.load !== 'function' || !options.material)
626
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain stream options');
627
+ this.tileSize = requirePositive3D(options.tileSize, 'tileSize');
628
+ if (!Number.isFinite(Math.fround(this.tileSize)))
629
+ throw new World3DError('WORLD3D_BUDGET', 'terrain tile size');
630
+ this.viewDistance = requirePositive3D(options.viewDistance, 'viewDistance');
631
+ this.#loadsPerUpdate = requireCount3D(options.loadsPerUpdate ?? 2, 'loadsPerUpdate', TERRAIN3D_LIMITS.maxStreamPending);
632
+ if (this.#loadsPerUpdate === 0)
633
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'loadsPerUpdate');
634
+ this.#options = options;
635
+ }
636
+ #viewer;
637
+ /**
638
+ * How far the viewer of the last `update` is from the nearest point of a tile, in world units: for showing the
639
+ * things a game scattered on it in more or less detail. 0 inside it; Infinity before the first update.
640
+ */
641
+ distanceOf(tile) {
642
+ const viewer = this.#viewer;
643
+ if (viewer === undefined)
644
+ return Infinity;
645
+ const size = this.tileSize, [x, z] = viewer;
646
+ return Math.hypot(Math.max(tile.x * size - x, 0, x - (tile.x + 1) * size), Math.max(tile.z * size - z, 0, z - (tile.z + 1) * size));
647
+ }
648
+ get tiles() {
649
+ return [...this.#tiles.values()];
650
+ }
651
+ /** Outstanding loader calls, including retired requests that have yet to settle. */
652
+ get pendingCount() {
653
+ return this.#outstanding;
654
+ }
655
+ /** Most recent failure in a loader, tile preparation, or lifecycle callback. */
656
+ get lastFailure() {
657
+ return this.#lastFailure;
658
+ }
659
+ tileAt(x, z) {
660
+ return this.#tiles.get(Math.floor(x / this.tileSize) + ',' + Math.floor(z / this.tileSize));
661
+ }
662
+ /** Ground height at a world point of this node's space, or undefined where no tile is loaded. */
663
+ heightAt(x, z) {
664
+ const tile = this.tileAt(x, z);
665
+ return tile
666
+ ? tile.field.heightAt(x - (tile.x + 0.5) * this.tileSize, z - (tile.z + 0.5) * this.tileSize)
667
+ : undefined;
668
+ }
669
+ /** Request the tiles near a position and release those that fell out of range. */
670
+ update(x, z) {
671
+ if (this.#disposed)
672
+ throw new World3DError('WORLD3D_DISPOSED', 'terrain stream');
673
+ if (this.#updating)
674
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain stream update is reentrant');
675
+ requireFinite3D(x, 'viewer x');
676
+ requireFinite3D(z, 'viewer z');
677
+ const size = this.tileSize, reach = requirePositive3D(this.viewDistance, 'viewDistance');
678
+ const retention = reach * 1.25 + size * 0.25;
679
+ const minX = Math.floor((x - retention) / size), maxX = Math.floor((x + retention) / size);
680
+ const minZ = Math.floor((z - retention) / size), maxZ = Math.floor((z + retention) / size);
681
+ const bounds = [minX, maxX, minZ, maxZ];
682
+ if (!bounds.every(value => Number.isSafeInteger(value) && Number.isFinite(Math.fround((value + 0.5) * size))) ||
683
+ (maxX - minX + 1) * (maxZ - minZ + 1) > TERRAIN3D_LIMITS.maxStreamTiles) {
684
+ throw new World3DError('WORLD3D_BUDGET', 'terrain stream tile range');
685
+ }
686
+ const near = (tx, tz) => Math.hypot(Math.max(tx * size - x, 0, x - (tx + 1) * size), Math.max(tz * size - z, 0, z - (tz + 1) * size));
687
+ this.#viewer = [x, z];
688
+ this.#updating = true;
689
+ try {
690
+ for (const [key, tile] of this.#tiles)
691
+ if (near(tile.x, tile.z) > retention)
692
+ this.release(key, tile);
693
+ for (const [key, request] of this.#requests)
694
+ if (near(request.x, request.z) > retention)
695
+ this.#requests.delete(key);
696
+ const budget = Math.min(this.#loadsPerUpdate, TERRAIN3D_LIMITS.maxStreamPending - this.#outstanding);
697
+ if (budget <= 0 || this.#disposed)
698
+ return;
699
+ const wanted = [];
700
+ for (let tz = Math.floor((z - reach) / size); tz <= Math.floor((z + reach) / size); tz++) {
701
+ for (let tx = Math.floor((x - reach) / size); tx <= Math.floor((x + reach) / size); tx++) {
702
+ const key = tx + ',' + tz, distance = near(tx, tz);
703
+ if (distance <= reach && !this.#tiles.has(key) && !this.#requests.has(key))
704
+ wanted.push({ x: tx, z: tz, distance });
705
+ }
706
+ }
707
+ wanted.sort((a, b) => a.distance - b.distance);
708
+ for (const want of wanted.slice(0, budget)) {
709
+ if (this.#disposed)
710
+ break;
711
+ this.request(want.x, want.z);
712
+ }
713
+ }
714
+ finally {
715
+ this.#updating = false;
716
+ }
717
+ }
718
+ /** Retire the stream and release all tile geometry. Outstanding loads cannot publish. */
719
+ dispose() {
720
+ if (this.#disposed)
721
+ return;
722
+ this.#disposed = true;
723
+ this.#requests.clear();
724
+ for (const [key, tile] of this.#tiles)
725
+ this.release(key, tile);
726
+ }
727
+ request(x, z) {
728
+ const key = x + ',' + z, request = { x, z };
729
+ this.#requests.set(key, request);
730
+ this.#outstanding++;
731
+ const current = () => this.#requests.get(key) === request;
732
+ const reject = (cause) => {
733
+ this.#outstanding--;
734
+ if (!current())
735
+ return;
736
+ this.recordFailure(request, 'load', cause);
737
+ };
738
+ const accept = (field) => {
739
+ let staged;
740
+ try {
741
+ if (!current())
742
+ return;
743
+ if (field === undefined)
744
+ return;
745
+ if (!(field instanceof HeightField3D) || field.width !== this.tileSize || field.depth !== this.tileSize) {
746
+ throw new World3DError('WORLD3D_INPUT_INVALID', 'terrain tile field size');
747
+ }
748
+ const material = typeof this.#options.material === 'function' ? this.#options.material(x, z, field) : this.#options.material;
749
+ if (!current())
750
+ return;
751
+ const terrain = new Terrain3D(field, material, this.#options.terrain);
752
+ staged = terrain;
753
+ if (!current())
754
+ return;
755
+ terrain.setPosition((x + 0.5) * this.tileSize, 0, (z + 0.5) * this.tileSize);
756
+ this.addChild(terrain);
757
+ const tile = Object.freeze({
758
+ x,
759
+ z,
760
+ field,
761
+ terrain,
762
+ center: Object.freeze([(x + 0.5) * this.tileSize, (z + 0.5) * this.tileSize])
763
+ });
764
+ this.#tiles.set(key, tile);
765
+ this.#requests.delete(key);
766
+ staged = undefined;
767
+ try {
768
+ this.#options.onLoad?.(tile);
769
+ }
770
+ catch (cause) {
771
+ this.recordFailure(tile, 'onLoad', cause);
772
+ }
773
+ }
774
+ catch (cause) {
775
+ if (current())
776
+ this.recordFailure(request, 'prepare', cause);
777
+ }
778
+ finally {
779
+ this.#outstanding--;
780
+ if (staged) {
781
+ try {
782
+ staged.dispose();
783
+ }
784
+ catch (cause) {
785
+ this.recordFailure(request, 'dispose', cause);
786
+ }
787
+ }
788
+ }
789
+ };
790
+ let result;
791
+ try {
792
+ result = this.#options.load(x, z);
793
+ }
794
+ catch (cause) {
795
+ reject(cause);
796
+ return;
797
+ }
798
+ if (result === undefined || result instanceof HeightField3D)
799
+ accept(result);
800
+ else
801
+ void Promise.resolve(result).then(accept, reject);
802
+ }
803
+ release(key, tile) {
804
+ this.#tiles.delete(key);
805
+ tile.terrain.removeFromParent();
806
+ try {
807
+ tile.terrain.dispose();
808
+ }
809
+ catch (cause) {
810
+ this.recordFailure(tile, 'dispose', cause);
811
+ }
812
+ try {
813
+ this.#options.onUnload?.(tile);
814
+ }
815
+ catch (cause) {
816
+ this.recordFailure(tile, 'onUnload', cause);
817
+ }
818
+ }
819
+ recordFailure(location, phase, cause) {
820
+ this.#lastFailure = Object.freeze({ x: location.x, z: location.z, phase, cause });
821
+ }
822
+ }
823
+ /** Makes terrain that loads and unloads tiles around a point. */
824
+ export function createTerrainStream3D(options) {
825
+ return new TerrainStream3D(options);
826
+ }
827
+ //# sourceMappingURL=Terrain3D.js.map