@egret/engine 0.0.0-stage → 7.0.1-rc.3

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 (575) 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 +464 -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 +42 -0
  20. package/dist/animation/sequenceResetTypes.js +2 -0
  21. package/dist/animation/types.d.ts +69 -0
  22. package/dist/animation/types.js +2 -0
  23. package/dist/assetBundle.d.ts +52 -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 +15 -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 +43 -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 +233 -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 +29 -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 +109 -0
  46. package/dist/bitmapFontTypes.js +2 -0
  47. package/dist/bitmapResourceSlot.d.ts +25 -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 +411 -0
  57. package/dist/cameraTypes.d.ts +114 -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 +33 -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 +42 -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 +34 -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 +31 -0
  82. package/dist/gameClockTypes.js +2 -0
  83. package/dist/gameRun.d.ts +50 -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 +134 -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 +119 -0
  98. package/dist/hoverInputTypes.js +2 -0
  99. package/dist/index.d.ts +49 -0
  100. package/dist/index.js +20 -0
  101. package/dist/input.d.ts +6 -0
  102. package/dist/input.js +5 -0
  103. package/dist/inputActions.d.ts +81 -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 +64 -0
  116. package/dist/internal/uiFactoryState.js +157 -0
  117. package/dist/internal/uiRootState.d.ts +67 -0
  118. package/dist/internal/uiRootState.js +103 -0
  119. package/dist/keyboardInput.d.ts +12 -0
  120. package/dist/keyboardInput.js +578 -0
  121. package/dist/keyboardInputTypes.d.ts +81 -0
  122. package/dist/keyboardInputTypes.js +2 -0
  123. package/dist/kit.d.ts +14 -0
  124. package/dist/kit.js +9 -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 +16 -0
  132. package/dist/manifestResourceState.js +57 -0
  133. package/dist/manifestResourceTypes.d.ts +25 -0
  134. package/dist/manifestResourceTypes.js +2 -0
  135. package/dist/motion.d.ts +33 -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 +37 -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 +292 -0
  145. package/dist/particleTypes.d.ts +107 -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 +167 -0
  154. package/dist/physics2dTypes.js +2 -0
  155. package/dist/physics2dWorld.d.ts +4 -0
  156. package/dist/physics2dWorld.js +623 -0
  157. package/dist/pointerInput.d.ts +11 -0
  158. package/dist/pointerInput.js +299 -0
  159. package/dist/pointerInputTypes.d.ts +38 -0
  160. package/dist/pointerInputTypes.js +2 -0
  161. package/dist/random.d.ts +29 -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 +331 -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 +88 -0
  172. package/dist/rendering/prepareSharedDraws2D.js +652 -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 +18 -0
  178. package/dist/rendering/sharedColor2D.js +68 -0
  179. package/dist/rendering/sharedEffects2D.d.ts +23 -0
  180. package/dist/rendering/sharedEffects2D.js +118 -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 +95 -0
  188. package/dist/rendering/world3d/Animation3D.js +380 -0
  189. package/dist/rendering/world3d/AnimationTexture3D.d.ts +157 -0
  190. package/dist/rendering/world3d/AnimationTexture3D.js +601 -0
  191. package/dist/rendering/world3d/Batch3D.d.ts +71 -0
  192. package/dist/rendering/world3d/Batch3D.js +329 -0
  193. package/dist/rendering/world3d/Camera3D.d.ts +57 -0
  194. package/dist/rendering/world3d/Camera3D.js +129 -0
  195. package/dist/rendering/world3d/Character3D.d.ts +123 -0
  196. package/dist/rendering/world3d/Character3D.js +303 -0
  197. package/dist/rendering/world3d/Cloth3D.d.ts +115 -0
  198. package/dist/rendering/world3d/Cloth3D.js +498 -0
  199. package/dist/rendering/world3d/Compression3D.d.ts +21 -0
  200. package/dist/rendering/world3d/Compression3D.js +702 -0
  201. package/dist/rendering/world3d/Curves3D.d.ts +111 -0
  202. package/dist/rendering/world3d/Curves3D.js +455 -0
  203. package/dist/rendering/world3d/Decal3D.d.ts +41 -0
  204. package/dist/rendering/world3d/Decal3D.js +241 -0
  205. package/dist/rendering/world3d/Geometry3D.d.ts +85 -0
  206. package/dist/rendering/world3d/Geometry3D.js +326 -0
  207. package/dist/rendering/world3d/GeometryBuilder3D.d.ts +51 -0
  208. package/dist/rendering/world3d/GeometryBuilder3D.js +196 -0
  209. package/dist/rendering/world3d/Gizmo3D.d.ts +75 -0
  210. package/dist/rendering/world3d/Gizmo3D.js +512 -0
  211. package/dist/rendering/world3d/Gltf3D.d.ts +67 -0
  212. package/dist/rendering/world3d/Gltf3D.js +976 -0
  213. package/dist/rendering/world3d/GpuParticles3D.d.ts +107 -0
  214. package/dist/rendering/world3d/GpuParticles3D.js +400 -0
  215. package/dist/rendering/world3d/Hdr3D.d.ts +33 -0
  216. package/dist/rendering/world3d/Hdr3D.js +190 -0
  217. package/dist/rendering/world3d/Jelly3D.d.ts +75 -0
  218. package/dist/rendering/world3d/Jelly3D.js +266 -0
  219. package/dist/rendering/world3d/Ktx23D.d.ts +27 -0
  220. package/dist/rendering/world3d/Ktx23D.js +112 -0
  221. package/dist/rendering/world3d/Light3D.d.ts +75 -0
  222. package/dist/rendering/world3d/Light3D.js +93 -0
  223. package/dist/rendering/world3d/Material3D.d.ts +243 -0
  224. package/dist/rendering/world3d/Material3D.js +267 -0
  225. package/dist/rendering/world3d/Mesh3D.d.ts +108 -0
  226. package/dist/rendering/world3d/Mesh3D.js +323 -0
  227. package/dist/rendering/world3d/Meshopt3D.d.ts +12 -0
  228. package/dist/rendering/world3d/Meshopt3D.js +275 -0
  229. package/dist/rendering/world3d/Navigation3D.d.ts +173 -0
  230. package/dist/rendering/world3d/Navigation3D.js +971 -0
  231. package/dist/rendering/world3d/Object3D.d.ts +107 -0
  232. package/dist/rendering/world3d/Object3D.js +305 -0
  233. package/dist/rendering/world3d/Particles3D.d.ts +85 -0
  234. package/dist/rendering/world3d/Particles3D.js +274 -0
  235. package/dist/rendering/world3d/Physics3D.d.ts +416 -0
  236. package/dist/rendering/world3d/Physics3D.js +2835 -0
  237. package/dist/rendering/world3d/PhysicsHull3D.d.ts +35 -0
  238. package/dist/rendering/world3d/PhysicsHull3D.js +227 -0
  239. package/dist/rendering/world3d/PhysicsMesh3D.d.ts +39 -0
  240. package/dist/rendering/world3d/PhysicsMesh3D.js +338 -0
  241. package/dist/rendering/world3d/PhysicsQueries3D.d.ts +24 -0
  242. package/dist/rendering/world3d/PhysicsQueries3D.js +323 -0
  243. package/dist/rendering/world3d/PhysicsRemote3D.d.ts +174 -0
  244. package/dist/rendering/world3d/PhysicsRemote3D.js +534 -0
  245. package/dist/rendering/world3d/Ragdoll3D.d.ts +69 -0
  246. package/dist/rendering/world3d/Ragdoll3D.js +200 -0
  247. package/dist/rendering/world3d/Raycast3D.d.ts +37 -0
  248. package/dist/rendering/world3d/Raycast3D.js +246 -0
  249. package/dist/rendering/world3d/RenderList3D.d.ts +88 -0
  250. package/dist/rendering/world3d/RenderList3D.js +490 -0
  251. package/dist/rendering/world3d/Retarget3D.d.ts +20 -0
  252. package/dist/rendering/world3d/Retarget3D.js +185 -0
  253. package/dist/rendering/world3d/Serialize3D.d.ts +49 -0
  254. package/dist/rendering/world3d/Serialize3D.js +607 -0
  255. package/dist/rendering/world3d/StateMachine3D.d.ts +78 -0
  256. package/dist/rendering/world3d/StateMachine3D.js +277 -0
  257. package/dist/rendering/world3d/Terrain3D.d.ts +227 -0
  258. package/dist/rendering/world3d/Terrain3D.js +803 -0
  259. package/dist/rendering/world3d/Text3D.d.ts +90 -0
  260. package/dist/rendering/world3d/Text3D.js +558 -0
  261. package/dist/rendering/world3d/Texture3D.d.ts +133 -0
  262. package/dist/rendering/world3d/Texture3D.js +252 -0
  263. package/dist/rendering/world3d/Trail3D.d.ts +59 -0
  264. package/dist/rendering/world3d/Trail3D.js +193 -0
  265. package/dist/rendering/world3d/Vehicle3D.d.ts +97 -0
  266. package/dist/rendering/world3d/Vehicle3D.js +226 -0
  267. package/dist/rendering/world3d/VolumetricFog3D.d.ts +55 -0
  268. package/dist/rendering/world3d/VolumetricFog3D.js +229 -0
  269. package/dist/rendering/world3d/Water3D.d.ts +163 -0
  270. package/dist/rendering/world3d/Water3D.js +606 -0
  271. package/dist/rendering/world3d/core3D.d.ts +34 -0
  272. package/dist/rendering/world3d/core3D.js +99 -0
  273. package/dist/rendering/world3d/debug3D.d.ts +64 -0
  274. package/dist/rendering/world3d/debug3D.js +257 -0
  275. package/dist/rendering/world3d/decoders3D.d.ts +42 -0
  276. package/dist/rendering/world3d/decoders3D.js +133 -0
  277. package/dist/rendering/world3d/geometries3D.d.ts +99 -0
  278. package/dist/rendering/world3d/geometries3D.js +605 -0
  279. package/dist/rendering/world3d/helpers3D.d.ts +65 -0
  280. package/dist/rendering/world3d/helpers3D.js +177 -0
  281. package/dist/rendering/world3d/index.d.ts +91 -0
  282. package/dist/rendering/world3d/index.js +56 -0
  283. package/dist/rendering/world3d/math3D.d.ts +78 -0
  284. package/dist/rendering/world3d/math3D.js +640 -0
  285. package/dist/rendering/world3d/primitives3D.d.ts +60 -0
  286. package/dist/rendering/world3d/primitives3D.js +196 -0
  287. package/dist/resources.d.ts +8 -0
  288. package/dist/resources.js +6 -0
  289. package/dist/saveStore.d.ts +56 -0
  290. package/dist/saveStore.js +163 -0
  291. package/dist/semanticList.d.ts +122 -0
  292. package/dist/semanticList.js +174 -0
  293. package/dist/semanticTree.d.ts +10 -0
  294. package/dist/semanticTree.js +532 -0
  295. package/dist/semanticTypes.d.ts +167 -0
  296. package/dist/semanticTypes.js +2 -0
  297. package/dist/semantics.d.ts +6 -0
  298. package/dist/semantics.js +5 -0
  299. package/dist/sequencePlayback.d.ts +19 -0
  300. package/dist/sequencePlayback.js +391 -0
  301. package/dist/sequencePlaybackTypes.d.ts +21 -0
  302. package/dist/sequencePlaybackTypes.js +2 -0
  303. package/dist/sequenceTargetClaims.d.ts +10 -0
  304. package/dist/sequenceTargetClaims.js +31 -0
  305. package/dist/srgbBytes.d.ts +3 -0
  306. package/dist/srgbBytes.js +64 -0
  307. package/dist/startEngine.d.ts +5 -0
  308. package/dist/startEngine.js +31 -0
  309. package/dist/surfaceOwnership.d.ts +3 -0
  310. package/dist/surfaceOwnership.js +15 -0
  311. package/dist/text.d.ts +23 -0
  312. package/dist/text.js +4 -0
  313. package/dist/textField.d.ts +6 -0
  314. package/dist/textField.js +6 -0
  315. package/dist/tileMapDraw.d.ts +7 -0
  316. package/dist/tileMapDraw.js +310 -0
  317. package/dist/tiledImport.d.ts +9 -0
  318. package/dist/tiledImport.js +357 -0
  319. package/dist/tilemap.d.ts +5 -0
  320. package/dist/tilemap.js +5 -0
  321. package/dist/tilemapTypes.d.ts +169 -0
  322. package/dist/tilemapTypes.js +2 -0
  323. package/dist/tween.d.ts +15 -0
  324. package/dist/tween.js +329 -0
  325. package/dist/tweenTypes.d.ts +27 -0
  326. package/dist/tweenTypes.js +2 -0
  327. package/dist/ui/controlKeys.d.ts +109 -0
  328. package/dist/ui/controlKeys.js +249 -0
  329. package/dist/ui/controls.d.ts +52 -0
  330. package/dist/ui/controls.js +288 -0
  331. package/dist/ui/editing.d.ts +3 -0
  332. package/dist/ui/editing.js +3 -0
  333. package/dist/ui/index.d.ts +19 -0
  334. package/dist/ui/index.js +11 -0
  335. package/dist/ui/keyboardFocus.d.ts +32 -0
  336. package/dist/ui/keyboardFocus.js +146 -0
  337. package/dist/ui/layerTypes.d.ts +91 -0
  338. package/dist/ui/layerTypes.js +2 -0
  339. package/dist/ui/layers.d.ts +4 -0
  340. package/dist/ui/layers.js +586 -0
  341. package/dist/ui/leafContent.d.ts +21 -0
  342. package/dist/ui/leafContent.js +136 -0
  343. package/dist/ui/rangeControl.d.ts +50 -0
  344. package/dist/ui/rangeControl.js +167 -0
  345. package/dist/ui/scrollControl.d.ts +72 -0
  346. package/dist/ui/scrollControl.js +218 -0
  347. package/dist/ui/stateControls.d.ts +82 -0
  348. package/dist/ui/stateControls.js +328 -0
  349. package/dist/ui/textEditor.d.ts +53 -0
  350. package/dist/ui/textEditor.js +370 -0
  351. package/dist/ui/textEditorTypes.d.ts +78 -0
  352. package/dist/ui/textEditorTypes.js +2 -0
  353. package/dist/ui/textFieldControl.d.ts +96 -0
  354. package/dist/ui/textFieldControl.js +259 -0
  355. package/dist/ui/theme.d.ts +59 -0
  356. package/dist/ui/theme.js +165 -0
  357. package/dist/ui/types.d.ts +265 -0
  358. package/dist/ui/types.js +2 -0
  359. package/dist/ui/uiRoot.d.ts +4 -0
  360. package/dist/ui/uiRoot.js +1497 -0
  361. package/dist/ui/uiRootDisplay.d.ts +46 -0
  362. package/dist/ui/uiRootDisplay.js +148 -0
  363. package/dist/ui/uiRootModel.d.ts +112 -0
  364. package/dist/ui/uiRootModel.js +60 -0
  365. package/dist/ui/virtualList.d.ts +20 -0
  366. package/dist/ui/virtualList.js +105 -0
  367. package/dist/video.d.ts +4 -0
  368. package/dist/video.js +4 -0
  369. package/dist/videoPlayer.d.ts +4 -0
  370. package/dist/videoPlayer.js +215 -0
  371. package/dist/videoTypes.d.ts +74 -0
  372. package/dist/videoTypes.js +2 -0
  373. package/dist/web/BrowserAssetBundle.d.ts +39 -0
  374. package/dist/web/BrowserAssetBundle.js +136 -0
  375. package/dist/web/BrowserAudio.d.ts +41 -0
  376. package/dist/web/BrowserAudio.js +473 -0
  377. package/dist/web/BrowserFrameLoop.d.ts +65 -0
  378. package/dist/web/BrowserFrameLoop.js +125 -0
  379. package/dist/web/BrowserGame.d.ts +112 -0
  380. package/dist/web/BrowserGame.js +352 -0
  381. package/dist/web/BrowserGamepadInput.d.ts +35 -0
  382. package/dist/web/BrowserGamepadInput.js +134 -0
  383. package/dist/web/BrowserHoverInput.d.ts +24 -0
  384. package/dist/web/BrowserHoverInput.js +266 -0
  385. package/dist/web/BrowserImageTextures.d.ts +66 -0
  386. package/dist/web/BrowserImageTextures.js +169 -0
  387. package/dist/web/BrowserInputActions.d.ts +53 -0
  388. package/dist/web/BrowserInputActions.js +342 -0
  389. package/dist/web/BrowserKeyboardInput.d.ts +4 -0
  390. package/dist/web/BrowserKeyboardInput.js +223 -0
  391. package/dist/web/BrowserPlay.d.ts +289 -0
  392. package/dist/web/BrowserPlay.js +1301 -0
  393. package/dist/web/BrowserPointerInput.d.ts +4 -0
  394. package/dist/web/BrowserPointerInput.js +511 -0
  395. package/dist/web/BrowserSemantics.d.ts +34 -0
  396. package/dist/web/BrowserSemantics.js +418 -0
  397. package/dist/web/BrowserStatsPanel.d.ts +46 -0
  398. package/dist/web/BrowserStatsPanel.js +137 -0
  399. package/dist/web/BrowserStorage.d.ts +33 -0
  400. package/dist/web/BrowserStorage.js +132 -0
  401. package/dist/web/BrowserTextEditor.d.ts +49 -0
  402. package/dist/web/BrowserTextEditor.js +839 -0
  403. package/dist/web/BrowserTextInput.d.ts +83 -0
  404. package/dist/web/BrowserTextInput.js +524 -0
  405. package/dist/web/BrowserTextureProvider.d.ts +52 -0
  406. package/dist/web/BrowserTextureProvider.js +935 -0
  407. package/dist/web/BrowserVideo.d.ts +28 -0
  408. package/dist/web/BrowserVideo.js +211 -0
  409. package/dist/web/CanvasKitFontSetProvider.d.ts +29 -0
  410. package/dist/web/CanvasKitFontSetProvider.js +215 -0
  411. package/dist/web/CanvasKitParagraphProvider.d.ts +169 -0
  412. package/dist/web/CanvasKitParagraphProvider.js +691 -0
  413. package/dist/web/CanvasTextRasterProvider.d.ts +4 -0
  414. package/dist/web/CanvasTextRasterProvider.js +312 -0
  415. package/dist/web/ManifestByteSource.d.ts +147 -0
  416. package/dist/web/ManifestByteSource.js +974 -0
  417. package/dist/web/ManifestFontContentLoader.d.ts +12 -0
  418. package/dist/web/ManifestFontContentLoader.js +30 -0
  419. package/dist/web/MouseInput.d.ts +4 -0
  420. package/dist/web/MouseInput.js +218 -0
  421. package/dist/web/WebGL2Host.d.ts +5 -0
  422. package/dist/web/WebGL2Host.js +2672 -0
  423. package/dist/web/WebGPUHost.d.ts +87 -0
  424. package/dist/web/WebGPUHost.js +3349 -0
  425. package/dist/web/audio.d.ts +3 -0
  426. package/dist/web/audio.js +4 -0
  427. package/dist/web/canvasKitParagraphGeometry.d.ts +27 -0
  428. package/dist/web/canvasKitParagraphGeometry.js +83 -0
  429. package/dist/web/editing.d.ts +5 -0
  430. package/dist/web/editing.js +4 -0
  431. package/dist/web/editorFonts.d.ts +50 -0
  432. package/dist/web/editorFonts.js +297 -0
  433. package/dist/web/game.d.ts +5 -0
  434. package/dist/web/game.js +6 -0
  435. package/dist/web/gamepad.d.ts +3 -0
  436. package/dist/web/gamepad.js +4 -0
  437. package/dist/web/hover.d.ts +3 -0
  438. package/dist/web/hover.js +4 -0
  439. package/dist/web/image.d.ts +4 -0
  440. package/dist/web/image.js +4 -0
  441. package/dist/web/imageProjections2D.d.ts +14 -0
  442. package/dist/web/imageProjections2D.js +2 -0
  443. package/dist/web/input.d.ts +6 -0
  444. package/dist/web/input.js +4 -0
  445. package/dist/web/keyboard.d.ts +3 -0
  446. package/dist/web/keyboard.js +3 -0
  447. package/dist/web/kit.d.ts +10 -0
  448. package/dist/web/kit.js +7 -0
  449. package/dist/web/loop.d.ts +3 -0
  450. package/dist/web/loop.js +4 -0
  451. package/dist/web/paragraph.d.ts +6 -0
  452. package/dist/web/paragraph.js +4 -0
  453. package/dist/web/play.d.ts +5 -0
  454. package/dist/web/play.js +6 -0
  455. package/dist/web/playSfx.d.ts +36 -0
  456. package/dist/web/playSfx.js +112 -0
  457. package/dist/web/pointer.d.ts +3 -0
  458. package/dist/web/pointer.js +3 -0
  459. package/dist/web/prepareSharedSubmission2D.d.ts +27 -0
  460. package/dist/web/prepareSharedSubmission2D.js +57 -0
  461. package/dist/web/resources.d.ts +27 -0
  462. package/dist/web/resources.js +96 -0
  463. package/dist/web/semantics.d.ts +3 -0
  464. package/dist/web/semantics.js +4 -0
  465. package/dist/web/sharedDeviceSession.d.ts +64 -0
  466. package/dist/web/sharedDeviceSession.js +2 -0
  467. package/dist/web/sharedEffectShaders.d.ts +9 -0
  468. package/dist/web/sharedEffectShaders.js +165 -0
  469. package/dist/web/sharedFrameUpload.d.ts +25 -0
  470. package/dist/web/sharedFrameUpload.js +2 -0
  471. package/dist/web/sharedHostDeferred.d.ts +7 -0
  472. package/dist/web/sharedHostDeferred.js +12 -0
  473. package/dist/web/sharedRetentionLedger.d.ts +85 -0
  474. package/dist/web/sharedRetentionLedger.js +391 -0
  475. package/dist/web/sharedSubmissionStorage2D.d.ts +56 -0
  476. package/dist/web/sharedSubmissionStorage2D.js +73 -0
  477. package/dist/web/sharedWebGL2Arena.d.ts +40 -0
  478. package/dist/web/sharedWebGL2Arena.js +77 -0
  479. package/dist/web/sharedWebGL2Options.d.ts +30 -0
  480. package/dist/web/sharedWebGL2Options.js +96 -0
  481. package/dist/web/sharedWebGL2Shaders.d.ts +20 -0
  482. package/dist/web/sharedWebGL2Shaders.js +314 -0
  483. package/dist/web/sharedWebGPUOptions.d.ts +37 -0
  484. package/dist/web/sharedWebGPUOptions.js +126 -0
  485. package/dist/web/text.d.ts +3 -0
  486. package/dist/web/text.js +3 -0
  487. package/dist/web/textInput.d.ts +3 -0
  488. package/dist/web/textInput.js +3 -0
  489. package/dist/web/video.d.ts +3 -0
  490. package/dist/web/video.js +4 -0
  491. package/dist/web/webgl2.d.ts +7 -0
  492. package/dist/web/webgl2.js +4 -0
  493. package/dist/web/webgl2LayerState.d.ts +50 -0
  494. package/dist/web/webgl2LayerState.js +102 -0
  495. package/dist/web/webgl2Native.d.ts +70 -0
  496. package/dist/web/webgl2Native.js +568 -0
  497. package/dist/web/webgl2Types.d.ts +132 -0
  498. package/dist/web/webgl2Types.js +2 -0
  499. package/dist/web/webgpu.d.ts +6 -0
  500. package/dist/web/webgpu.js +2 -0
  501. package/dist/web/webgpuBindingCache.d.ts +26 -0
  502. package/dist/web/webgpuBindingCache.js +223 -0
  503. package/dist/web/webgpuBufferPool.d.ts +51 -0
  504. package/dist/web/webgpuBufferPool.js +58 -0
  505. package/dist/web/webgpuImageResidency.d.ts +53 -0
  506. package/dist/web/webgpuImageResidency.js +61 -0
  507. package/dist/web/webgpuLifetime.d.ts +45 -0
  508. package/dist/web/webgpuLifetime.js +64 -0
  509. package/dist/web/webgpuRetentionPool.d.ts +66 -0
  510. package/dist/web/webgpuRetentionPool.js +84 -0
  511. package/dist/web/webgpuSharedPass.d.ts +7 -0
  512. package/dist/web/webgpuSharedPass.js +211 -0
  513. package/dist/web/webgpuSharedResolvePass.d.ts +3 -0
  514. package/dist/web/webgpuSharedResolvePass.js +29 -0
  515. package/dist/web/world3d/Audio3D.d.ts +68 -0
  516. package/dist/web/world3d/Audio3D.js +212 -0
  517. package/dist/web/world3d/BrowserPlay3D.d.ts +182 -0
  518. package/dist/web/world3d/BrowserPlay3D.js +851 -0
  519. package/dist/web/world3d/FlyControls3D.d.ts +55 -0
  520. package/dist/web/world3d/FlyControls3D.js +160 -0
  521. package/dist/web/world3d/FollowControls3D.d.ts +28 -0
  522. package/dist/web/world3d/FollowControls3D.js +117 -0
  523. package/dist/web/world3d/OrbitControls3D.d.ts +55 -0
  524. package/dist/web/world3d/OrbitControls3D.js +187 -0
  525. package/dist/web/world3d/TransformControls3D.d.ts +34 -0
  526. package/dist/web/world3d/TransformControls3D.js +125 -0
  527. package/dist/web/world3d/WebGL2Renderer3D.d.ts +133 -0
  528. package/dist/web/world3d/WebGL2Renderer3D.js +1173 -0
  529. package/dist/web/world3d/WebGPURenderer3D.d.ts +110 -0
  530. package/dist/web/world3d/WebGPURenderer3D.js +1915 -0
  531. package/dist/web/world3d/World3DView.d.ts +51 -0
  532. package/dist/web/world3d/World3DView.js +77 -0
  533. package/dist/web/world3d/app3D.d.ts +136 -0
  534. package/dist/web/world3d/app3D.js +304 -0
  535. package/dist/web/world3d/assets3D.d.ts +38 -0
  536. package/dist/web/world3d/assets3D.js +112 -0
  537. package/dist/web/world3d/atmosphere3D.d.ts +47 -0
  538. package/dist/web/world3d/atmosphere3D.js +178 -0
  539. package/dist/web/world3d/frame3D.d.ts +142 -0
  540. package/dist/web/world3d/frame3D.js +388 -0
  541. package/dist/web/world3d/gpuResources3D.d.ts +98 -0
  542. package/dist/web/world3d/gpuResources3D.js +901 -0
  543. package/dist/web/world3d/host3D.d.ts +23 -0
  544. package/dist/web/world3d/host3D.js +61 -0
  545. package/dist/web/world3d/index.d.ts +38 -0
  546. package/dist/web/world3d/index.js +23 -0
  547. package/dist/web/world3d/loaders3D.d.ts +26 -0
  548. package/dist/web/world3d/loaders3D.js +138 -0
  549. package/dist/web/world3d/markers3D.d.ts +49 -0
  550. package/dist/web/world3d/markers3D.js +130 -0
  551. package/dist/web/world3d/materialWebGL23D.d.ts +34 -0
  552. package/dist/web/world3d/materialWebGL23D.js +218 -0
  553. package/dist/web/world3d/overlay3D.d.ts +28 -0
  554. package/dist/web/world3d/overlay3D.js +170 -0
  555. package/dist/web/world3d/post3D.d.ts +112 -0
  556. package/dist/web/world3d/post3D.js +459 -0
  557. package/dist/web/world3d/postWebGL23D.d.ts +24 -0
  558. package/dist/web/world3d/postWebGL23D.js +237 -0
  559. package/dist/web/world3d/postWebGPU3D.d.ts +23 -0
  560. package/dist/web/world3d/postWebGPU3D.js +255 -0
  561. package/dist/web/world3d/probe3D.d.ts +14 -0
  562. package/dist/web/world3d/probe3D.js +66 -0
  563. package/dist/web/world3d/shaders3D.d.ts +43 -0
  564. package/dist/web/world3d/shaders3D.js +851 -0
  565. package/dist/web/world3d/stats3D.d.ts +31 -0
  566. package/dist/web/world3d/stats3D.js +80 -0
  567. package/dist/web/world3d/webGL2FrameMetrics3D.d.ts +66 -0
  568. package/dist/web/world3d/webGL2FrameMetrics3D.js +185 -0
  569. package/dist/web/world3d/webgpu.d.ts +35 -0
  570. package/dist/web/world3d/webgpu.js +22 -0
  571. package/dist/web/world3d/webgpuPacking3D.d.ts +13 -0
  572. package/dist/web/world3d/webgpuPacking3D.js +85 -0
  573. package/dist/web/world3d/wgsl3D.d.ts +68 -0
  574. package/dist/web/world3d/wgsl3D.js +781 -0
  575. package/package.json +220 -4
@@ -0,0 +1,976 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // glTF 2.0 reader (.glb and .gltf JSON) producing scene-graph objects.
3
+ // Image decoding and external buffer loading are injected so this module
4
+ // stays DOM-free. Supported: meshes, PBR/unlit materials, texture transform,
5
+ // node hierarchies, skins, morph targets, animations, cameras and punctual
6
+ // lights. Meshopt decoding is built in; Draco and Basis use caller-supplied adapters.
7
+ import { decodeMeshopt3D } from './Meshopt3D.js';
8
+ import { AnimationClip3D } from './Animation3D.js';
9
+ import { Camera3D } from './Camera3D.js';
10
+ import { World3DError, requireCount3D } from './core3D.js';
11
+ import { Attribute3D, Geometry3D } from './Geometry3D.js';
12
+ import { Light3D } from './Light3D.js';
13
+ import { Material3D } from './Material3D.js';
14
+ import { Bone3D, InstancedMesh3D, MESH3D_LIMITS, Mesh3D, Skeleton3D } from './Mesh3D.js';
15
+ import { Object3D } from './Object3D.js';
16
+ import { isKtx23D, parseKtx23D } from './Ktx23D.js';
17
+ import { Texture3D, isCompressedPixels3D } from './Texture3D.js';
18
+ import { mat4Compose, mat4Identity } from './math3D.js';
19
+ const SUPPORTED_EXTENSIONS = new Set([
20
+ 'KHR_materials_unlit',
21
+ 'KHR_materials_emissive_strength',
22
+ 'KHR_texture_transform',
23
+ 'KHR_lights_punctual',
24
+ 'KHR_mesh_quantization',
25
+ 'KHR_texture_basisu',
26
+ 'EXT_mesh_gpu_instancing',
27
+ 'EXT_meshopt_compression',
28
+ 'KHR_materials_transmission',
29
+ 'KHR_materials_ior',
30
+ 'KHR_materials_volume',
31
+ 'KHR_materials_clearcoat',
32
+ 'KHR_materials_sheen'
33
+ ]);
34
+ /** Extensions accepted only when the caller injects the matching decoder. */
35
+ const DECODER_EXTENSIONS = Object.freeze({
36
+ KHR_draco_mesh_compression: 'decodeDraco'
37
+ });
38
+ const GLTF_LIMITS = Object.freeze({
39
+ maxNodes: 65536,
40
+ maxAccessors: 262144,
41
+ maxJsonBytes: 64 * 1024 * 1024,
42
+ maxAccessorBytes: 128 * 1024 * 1024,
43
+ maxDecodedAccessorBytes: 256 * 1024 * 1024,
44
+ maxInstanceMatrixBytes: 64 * 1024 * 1024
45
+ });
46
+ const COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 };
47
+ const ARRAY_TYPES = {
48
+ 5120: Int8Array,
49
+ 5121: Uint8Array,
50
+ 5122: Int16Array,
51
+ 5123: Uint16Array,
52
+ 5125: Uint32Array,
53
+ 5126: Float32Array
54
+ };
55
+ function fail(detail, cause) {
56
+ throw new World3DError('WORLD3D_GLTF_INVALID', detail, cause);
57
+ }
58
+ function decodeBase64(text) {
59
+ const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
60
+ const clean = text.replace(/[^A-Za-z0-9+/]/g, ''), out = new Uint8Array(Math.floor((clean.length * 3) / 4));
61
+ let bits = 0, count = 0, o = 0;
62
+ for (let i = 0; i < clean.length; i++) {
63
+ bits = (bits << 6) | alphabet.indexOf(clean[i]);
64
+ count += 6;
65
+ if (count >= 8) {
66
+ count -= 8;
67
+ out[o++] = (bits >> count) & 255;
68
+ }
69
+ }
70
+ return out.subarray(0, o);
71
+ }
72
+ /** Minimal UTF-8 codec: the DOM-free core cannot rely on TextDecoder being present. */
73
+ function decodeUtf8(bytes) {
74
+ let out = '', chunk = [];
75
+ for (let i = 0; i < bytes.length;) {
76
+ const b = bytes[i++];
77
+ let code;
78
+ if (b < 0x80)
79
+ code = b;
80
+ else if (b < 0xe0)
81
+ code = ((b & 0x1f) << 6) | (bytes[i++] & 0x3f);
82
+ else if (b < 0xf0) {
83
+ code = ((b & 0x0f) << 12) | ((bytes[i] & 0x3f) << 6) | (bytes[i + 1] & 0x3f);
84
+ i += 2;
85
+ }
86
+ else {
87
+ code = ((b & 0x07) << 18) | ((bytes[i] & 0x3f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f);
88
+ i += 3;
89
+ }
90
+ if (code > 0xffff) {
91
+ code -= 0x10000;
92
+ chunk.push(0xd800 + (code >> 10), 0xdc00 + (code & 0x3ff));
93
+ }
94
+ else
95
+ chunk.push(code);
96
+ if (chunk.length >= 8192) {
97
+ out += String.fromCharCode(...chunk);
98
+ chunk = [];
99
+ }
100
+ }
101
+ return out + String.fromCharCode(...chunk);
102
+ }
103
+ function encodeUtf8(text) {
104
+ const out = [];
105
+ for (const character of text) {
106
+ const code = character.codePointAt(0);
107
+ if (code < 0x80)
108
+ out.push(code);
109
+ else if (code < 0x800)
110
+ out.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
111
+ else if (code < 0x10000)
112
+ out.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
113
+ else
114
+ out.push(0xf0 | (code >> 18), 0x80 | ((code >> 12) & 0x3f), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
115
+ }
116
+ return Uint8Array.from(out);
117
+ }
118
+ function dataUri(uri) {
119
+ const match = /^data:([^;,]*)(;base64)?,(.*)$/s.exec(uri);
120
+ if (!match)
121
+ return undefined;
122
+ return {
123
+ mimeType: match[1] ?? '',
124
+ bytes: match[2] ? decodeBase64(match[3]) : encodeUtf8(decodeURIComponent(match[3]))
125
+ };
126
+ }
127
+ class GltfReader {
128
+ json;
129
+ buffers;
130
+ options;
131
+ geometries = [];
132
+ materials = [];
133
+ textures = [];
134
+ accessorCache = new Map();
135
+ floatAccessorCache = new Map();
136
+ decodedAccessorBytes = 0;
137
+ instanceMatrixBytes = 0;
138
+ geometryCache = new Map();
139
+ materialCache = new Map();
140
+ textureCache = new Map();
141
+ imageCache = new Map();
142
+ /** Transfer function recorded in a KTX2 container, kept for diagnostics. */
143
+ imageColorSpace = new Map();
144
+ nodes = [];
145
+ /** Mesh objects created for each node, used by skins and weight animations. */
146
+ nodeMeshes = [];
147
+ constructor(json, buffers, options) {
148
+ this.json = json;
149
+ this.buffers = buffers;
150
+ this.options = options;
151
+ }
152
+ // ---- binary access -------------------------------------------------------
153
+ meshoptCache = new Map();
154
+ bufferView(index) {
155
+ const view = this.json.bufferViews?.[index] ?? fail('bufferView ' + index);
156
+ const packed = view.extensions?.EXT_meshopt_compression;
157
+ if (packed) {
158
+ let bytes = this.meshoptCache.get(index);
159
+ if (!bytes) {
160
+ // The built-in decoder is used unless the caller supplies a faster one.
161
+ const decode = this.options.decodeMeshopt ??
162
+ ((request) => decodeMeshopt3D(request.source, request.count, request.stride, request.mode, request.filter));
163
+ const source = this.buffers[packed.buffer] ?? fail('meshopt buffer ' + packed.buffer);
164
+ const from = packed.byteOffset ?? 0, size = packed.byteLength, count = packed.count, stride = packed.byteStride;
165
+ if (!Number.isSafeInteger(from) ||
166
+ !Number.isSafeInteger(size) ||
167
+ from < 0 ||
168
+ size < 0 ||
169
+ size > source.byteLength - from)
170
+ fail('meshopt range');
171
+ if (!Number.isSafeInteger(count) ||
172
+ !Number.isSafeInteger(stride) ||
173
+ count < 0 ||
174
+ stride <= 0 ||
175
+ stride > 256 ||
176
+ count * stride > GLTF_LIMITS.maxJsonBytes * 16)
177
+ fail('meshopt layout');
178
+ if (!['ATTRIBUTES', 'TRIANGLES', 'INDICES'].includes(packed.mode))
179
+ fail('meshopt mode');
180
+ const filter = packed.filter ?? 'NONE';
181
+ if (!['NONE', 'OCTAHEDRAL', 'QUATERNION', 'EXPONENTIAL'].includes(filter))
182
+ fail('meshopt filter');
183
+ bytes = decode({ source: source.subarray(from, from + size), count, stride, mode: packed.mode, filter });
184
+ if (!(bytes instanceof Uint8Array) || bytes.byteLength !== count * stride)
185
+ fail('meshopt decoder output size');
186
+ this.meshoptCache.set(index, bytes);
187
+ }
188
+ // The uncompressed view describes the layout; only its data comes from the decoder.
189
+ return { bytes, stride: view.byteStride ?? 0 };
190
+ }
191
+ const buffer = this.buffers[view.buffer] ?? fail('buffer ' + view.buffer);
192
+ const offset = view.byteOffset ?? 0, length = view.byteLength;
193
+ if (!Number.isSafeInteger(offset) ||
194
+ !Number.isSafeInteger(length) ||
195
+ offset < 0 ||
196
+ length < 0 ||
197
+ length > buffer.byteLength - offset)
198
+ fail('bufferView range');
199
+ return { bytes: buffer.subarray(offset, offset + length), stride: view.byteStride ?? 0 };
200
+ }
201
+ accessor(index) {
202
+ const cached = this.accessorCache.get(index);
203
+ if (cached)
204
+ return cached;
205
+ const def = this.json.accessors?.[index] ?? fail('accessor ' + index);
206
+ const itemSize = COMPONENTS[def.type] ?? fail('accessor type'), ArrayType = ARRAY_TYPES[def.componentType] ?? fail('accessor componentType');
207
+ const count = def.count;
208
+ if (!Number.isSafeInteger(count) || count <= 0)
209
+ fail('accessor count');
210
+ const bytesPerElement = ArrayType.BYTES_PER_ELEMENT;
211
+ const elementBytes = itemSize * bytesPerElement;
212
+ const range = def.bufferView === undefined
213
+ ? undefined
214
+ : this.accessorRange(def.bufferView, def.byteOffset ?? 0, count, elementBytes);
215
+ let sparseData;
216
+ let sparseBytes = 0;
217
+ if (def.sparse) {
218
+ const sparse = def.sparse;
219
+ if (!Number.isSafeInteger(sparse.count) || sparse.count <= 0 || sparse.count > count)
220
+ fail('sparse count');
221
+ const IndexType = ARRAY_TYPES[sparse.indices?.componentType];
222
+ if (IndexType !== Uint8Array && IndexType !== Uint16Array && IndexType !== Uint32Array)
223
+ fail('sparse indices');
224
+ const indices = this.accessorRange(sparse.indices.bufferView, sparse.indices.byteOffset ?? 0, sparse.count, IndexType.BYTES_PER_ELEMENT, false);
225
+ const values = this.accessorRange(sparse.values.bufferView, sparse.values.byteOffset ?? 0, sparse.count, elementBytes, false);
226
+ sparseData = { count: sparse.count, IndexType, indices, values };
227
+ sparseBytes = sparse.count * (IndexType.BYTES_PER_ELEMENT + elementBytes);
228
+ }
229
+ // Validate every backing range and the combined allocation before constructing typed arrays.
230
+ this.reserveAccessorBytes(count * elementBytes + sparseBytes);
231
+ const array = new ArrayType(count * itemSize);
232
+ if (range)
233
+ this.copyElements(array, range, count, itemSize, ArrayType);
234
+ if (sparseData) {
235
+ const { count: sparseCount, IndexType, indices: indexRange, values: valueRange } = sparseData;
236
+ const indices = new IndexType(sparseCount), values = new ArrayType(sparseCount * itemSize);
237
+ this.copyElements(indices, indexRange, sparseCount, 1, IndexType);
238
+ this.copyElements(values, valueRange, sparseCount, itemSize, ArrayType);
239
+ let previous = -1;
240
+ for (let i = 0; i < sparseCount; i++) {
241
+ const target = indices[i];
242
+ if (target <= previous || target >= count)
243
+ fail('sparse index');
244
+ previous = target;
245
+ for (let c = 0; c < itemSize; c++)
246
+ array[target * itemSize + c] = values[i * itemSize + c];
247
+ }
248
+ }
249
+ const result = { array, itemSize, count, normalized: def.normalized === true };
250
+ this.accessorCache.set(index, result);
251
+ return result;
252
+ }
253
+ reserveAccessorBytes(bytes) {
254
+ if (bytes > GLTF_LIMITS.maxAccessorBytes ||
255
+ this.decodedAccessorBytes + bytes > GLTF_LIMITS.maxDecodedAccessorBytes) {
256
+ throw new World3DError('WORLD3D_BUDGET', 'gltf decoded accessor bytes');
257
+ }
258
+ this.decodedAccessorBytes += bytes;
259
+ }
260
+ accessorRange(viewIndex, byteOffset, count, elementBytes, allowStride = true) {
261
+ const { bytes, stride } = this.bufferView(viewIndex);
262
+ const step = stride === 0 ? elementBytes : stride;
263
+ if (!Number.isSafeInteger(byteOffset) ||
264
+ byteOffset < 0 ||
265
+ !Number.isSafeInteger(step) ||
266
+ step < elementBytes ||
267
+ (stride !== 0 && !allowStride) ||
268
+ count - 1 > Math.floor((bytes.byteLength - byteOffset - elementBytes) / step))
269
+ fail('accessor range');
270
+ return { bytes, byteOffset, step };
271
+ }
272
+ /** Copy preflighted interleaved elements into a packed array. */
273
+ copyElements(out, { bytes, byteOffset, step }, count, itemSize, ArrayType) {
274
+ const bytesPerElement = ArrayType.BYTES_PER_ELEMENT;
275
+ const data = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
276
+ const read = ArrayType === Float32Array
277
+ ? (o) => data.getFloat32(o, true)
278
+ : ArrayType === Uint32Array
279
+ ? (o) => data.getUint32(o, true)
280
+ : ArrayType === Uint16Array
281
+ ? (o) => data.getUint16(o, true)
282
+ : ArrayType === Int16Array
283
+ ? (o) => data.getInt16(o, true)
284
+ : ArrayType === Uint8Array
285
+ ? (o) => data.getUint8(o)
286
+ : (o) => data.getInt8(o);
287
+ for (let i = 0; i < count; i++) {
288
+ const base = byteOffset + i * step;
289
+ for (let c = 0; c < itemSize; c++)
290
+ out[i * itemSize + c] = read(base + c * bytesPerElement);
291
+ }
292
+ }
293
+ /** Float view of an accessor, expanding normalized integers. */
294
+ floats(index) {
295
+ const cached = this.floatAccessorCache.get(index);
296
+ if (cached)
297
+ return cached;
298
+ const { array, normalized } = this.accessor(index);
299
+ if (array instanceof Float32Array)
300
+ return array;
301
+ this.reserveAccessorBytes(array.length * Float32Array.BYTES_PER_ELEMENT);
302
+ const out = new Float32Array(array.length);
303
+ const scale = !normalized
304
+ ? 1
305
+ : array instanceof Uint8Array
306
+ ? 1 / 255
307
+ : array instanceof Uint16Array
308
+ ? 1 / 65535
309
+ : array instanceof Int8Array
310
+ ? 1 / 127
311
+ : array instanceof Int16Array
312
+ ? 1 / 32767
313
+ : 1;
314
+ for (let i = 0; i < array.length; i++)
315
+ out[i] = normalized ? Math.max(array[i] * scale, -1) : array[i];
316
+ this.floatAccessorCache.set(index, out);
317
+ return out;
318
+ }
319
+ // ---- textures and materials ------------------------------------------------
320
+ image(index) {
321
+ let pending = this.imageCache.get(index);
322
+ if (pending)
323
+ return pending;
324
+ const def = this.json.images?.[index] ?? fail('image ' + index);
325
+ const decode = this.options.decodeImage;
326
+ // KTX2 containers are read here; every other encoding goes to the injected decoder.
327
+ const embeddedBytes = def.bufferView !== undefined
328
+ ? this.bufferView(def.bufferView).bytes
329
+ : typeof def.uri === 'string'
330
+ ? dataUri(def.uri)?.bytes
331
+ : undefined;
332
+ if ((embeddedBytes && isKtx23D(embeddedBytes)) ||
333
+ (!embeddedBytes && def.mimeType === 'image/ktx2' && typeof def.uri === 'string' && this.options.loadBuffer)) {
334
+ const transcode = this.options.transcodeKtx2;
335
+ pending = (async () => {
336
+ const bytes = embeddedBytes ?? (await this.options.loadBuffer(def.uri));
337
+ const result = await parseKtx23D(bytes, transcode ? { transcode } : {});
338
+ this.imageColorSpace.set(index, result.colorSpace);
339
+ return result.source;
340
+ })();
341
+ this.imageCache.set(index, pending);
342
+ return pending;
343
+ }
344
+ if (!decode)
345
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'decodeImage is required for textured assets');
346
+ let request;
347
+ if (def.bufferView !== undefined)
348
+ request = { index, mimeType: def.mimeType, bytes: this.bufferView(def.bufferView).bytes, uri: undefined };
349
+ else if (typeof def.uri === 'string') {
350
+ const embedded = dataUri(def.uri);
351
+ request = embedded
352
+ ? { index, mimeType: def.mimeType ?? embedded.mimeType, bytes: embedded.bytes, uri: undefined }
353
+ : { index, mimeType: def.mimeType, bytes: undefined, uri: def.uri };
354
+ }
355
+ else
356
+ fail('image source');
357
+ pending = decode(request);
358
+ this.imageCache.set(index, pending);
359
+ return pending;
360
+ }
361
+ async texture(info, colorSpace, secondSet = false) {
362
+ if (!info || typeof info.index !== 'number')
363
+ return undefined;
364
+ const transform = info.extensions?.KHR_texture_transform;
365
+ const texCoord = transform?.texCoord ?? info.texCoord ?? 0;
366
+ // Occlusion and emissive maps may use the second set, where baked lighting lives.
367
+ if (texCoord !== 0 && !(texCoord === 1 && secondSet))
368
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'texture coordinate set ' + texCoord);
369
+ const key = info.index + ':' + colorSpace + ':' + texCoord + ':' + (transform ? JSON.stringify(transform) : '');
370
+ const cached = this.textureCache.get(key);
371
+ if (cached)
372
+ return cached;
373
+ const def = this.json.textures?.[info.index] ?? fail('texture ' + info.index);
374
+ const imageIndex = def.extensions?.KHR_texture_basisu?.source ?? def.source;
375
+ if (imageIndex === undefined)
376
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'texture without a supported image source');
377
+ const sampler = def.sampler !== undefined ? (this.json.samplers?.[def.sampler] ?? {}) : {};
378
+ const wrap = (mode) => mode === 33071 ? 'clamp' : mode === 33648 ? 'mirror' : 'repeat';
379
+ const min = sampler.minFilter;
380
+ const image = await this.image(imageIndex);
381
+ // A compressed chain carries its own mip levels; none can be generated for it.
382
+ const storedLevels = isCompressedPixels3D(image) ? image.levels.length : 0;
383
+ const texture = new Texture3D(image, {
384
+ name: def.name ?? '',
385
+ colorSpace,
386
+ flipY: false,
387
+ wrapS: wrap(sampler.wrapS),
388
+ wrapT: wrap(sampler.wrapT),
389
+ magFilter: sampler.magFilter === 9728 ? 'nearest' : 'linear',
390
+ minFilter: min === 9728 || min === 9984 || min === 9986 ? 'nearest' : 'linear',
391
+ mipmaps: storedLevels > 0 ? storedLevels > 1 : min === undefined || min >= 9984
392
+ });
393
+ if (transform) {
394
+ if (Array.isArray(transform.offset))
395
+ texture.offset.set(transform.offset);
396
+ if (Array.isArray(transform.scale))
397
+ texture.repeat.set(transform.scale);
398
+ if (typeof transform.rotation === 'number')
399
+ texture.rotation = transform.rotation;
400
+ }
401
+ texture.channel = texCoord === 1 ? 1 : 0;
402
+ this.textures.push(texture);
403
+ this.textureCache.set(key, texture);
404
+ return texture;
405
+ }
406
+ async material(index, vertexColors, flat) {
407
+ const key = index + ':' + vertexColors + ':' + flat;
408
+ const cached = this.materialCache.get(key);
409
+ if (cached)
410
+ return cached;
411
+ const def = index === undefined ? {} : (this.json.materials?.[index] ?? fail('material ' + index));
412
+ const pbr = def.pbrMetallicRoughness ?? {}, extensions = def.extensions ?? {};
413
+ const material = new Material3D(extensions.KHR_materials_unlit ? 'unlit' : 'standard', { name: def.name ?? '' });
414
+ const base = pbr.baseColorFactor ?? [1, 1, 1, 1];
415
+ material.color.set([base[0], base[1], base[2]]);
416
+ material.opacity = base[3] ?? 1;
417
+ material.metallic = pbr.metallicFactor ?? 1;
418
+ material.roughness = pbr.roughnessFactor ?? 1;
419
+ material.alphaMode = def.alphaMode === 'BLEND' ? 'blend' : def.alphaMode === 'MASK' ? 'mask' : 'opaque';
420
+ material.alphaCutoff = def.alphaCutoff ?? 0.5;
421
+ material.depthWrite = material.alphaMode !== 'blend';
422
+ material.side = def.doubleSided ? 'double' : 'front';
423
+ material.vertexColors = vertexColors;
424
+ material.flatShading = flat;
425
+ if (Array.isArray(def.emissiveFactor))
426
+ material.emissive.set(def.emissiveFactor);
427
+ material.emissiveIntensity = extensions.KHR_materials_emissive_strength?.emissiveStrength ?? 1;
428
+ // Surface extensions are read as their plain factors; their own textures are not used.
429
+ const transmission = extensions.KHR_materials_transmission, volume = extensions.KHR_materials_volume, coat = extensions.KHR_materials_clearcoat, sheen = extensions.KHR_materials_sheen;
430
+ if (transmission)
431
+ material.transmission = transmission.transmissionFactor ?? 0;
432
+ if (extensions.KHR_materials_ior)
433
+ material.ior = extensions.KHR_materials_ior.ior ?? 1.5;
434
+ if (volume) {
435
+ material.thickness = volume.thicknessFactor ?? 0;
436
+ if (Array.isArray(volume.attenuationColor))
437
+ material.attenuationColor.set(volume.attenuationColor);
438
+ if (volume.attenuationDistance > 0)
439
+ material.attenuationDistance = volume.attenuationDistance;
440
+ }
441
+ if (coat) {
442
+ material.clearcoat = coat.clearcoatFactor ?? 0;
443
+ material.clearcoatRoughness = coat.clearcoatRoughnessFactor ?? 0;
444
+ }
445
+ if (sheen) {
446
+ material.sheen = 1;
447
+ material.sheenColor.set(sheen.sheenColorFactor ?? [0, 0, 0]);
448
+ material.sheenRoughness = sheen.sheenRoughnessFactor ?? 0;
449
+ }
450
+ material.normalScale = def.normalTexture?.scale ?? 1;
451
+ material.occlusionStrength = def.occlusionTexture?.strength ?? 1;
452
+ const [map, metallicRoughness, normal, occlusion, emissive] = await Promise.all([
453
+ this.texture(pbr.baseColorTexture, 'srgb'),
454
+ this.texture(pbr.metallicRoughnessTexture, 'linear'),
455
+ this.texture(def.normalTexture, 'linear'),
456
+ this.texture(def.occlusionTexture, 'linear', true),
457
+ this.texture(def.emissiveTexture, 'srgb', true)
458
+ ]);
459
+ material.map = map;
460
+ material.metallicRoughnessMap = metallicRoughness;
461
+ material.normalMap = normal;
462
+ material.occlusionMap = occlusion;
463
+ material.emissiveMap = emissive;
464
+ this.materials.push(material);
465
+ this.materialCache.set(key, material);
466
+ return material;
467
+ }
468
+ // ---- geometry ----------------------------------------------------------------
469
+ /** Decode a Draco primitive once and seed the accessor cache with its arrays. */
470
+ dracoDecoded = new Set();
471
+ async prepareDraco(primitive) {
472
+ const packed = primitive.extensions?.KHR_draco_mesh_compression;
473
+ if (!packed || this.dracoDecoded.has(packed.bufferView))
474
+ return;
475
+ const decode = this.options.decodeDraco;
476
+ if (!decode)
477
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'KHR_draco_mesh_compression needs the decodeDraco option');
478
+ const semantics = packed.attributes ?? fail('draco attributes');
479
+ const componentTypes = {};
480
+ for (const semantic of Object.keys(semantics)) {
481
+ const accessor = this.json.accessors?.[primitive.attributes?.[semantic]];
482
+ if (accessor)
483
+ componentTypes[semantic] = accessor.componentType;
484
+ }
485
+ const indexComponentType = primitive.indices === undefined ? undefined : this.json.accessors?.[primitive.indices]?.componentType;
486
+ const result = await decode({
487
+ bytes: this.bufferView(packed.bufferView).bytes,
488
+ attributes: semantics,
489
+ componentTypes,
490
+ indexComponentType
491
+ });
492
+ const seed = (accessorIndex, array, label) => {
493
+ const def = this.json.accessors?.[accessorIndex] ?? fail('accessor ' + accessorIndex);
494
+ const itemSize = COMPONENTS[def.type] ?? fail('accessor type');
495
+ if (!array || !ArrayBuffer.isView(array) || array.length !== def.count * itemSize)
496
+ fail('draco decoder output for ' + label);
497
+ this.accessorCache.set(accessorIndex, { array, itemSize, count: def.count, normalized: def.normalized === true });
498
+ };
499
+ for (const semantic of Object.keys(semantics)) {
500
+ const accessorIndex = primitive.attributes?.[semantic];
501
+ if (accessorIndex !== undefined)
502
+ seed(accessorIndex, result.attributes?.[semantic], semantic);
503
+ }
504
+ if (primitive.indices !== undefined)
505
+ seed(primitive.indices, result.indices, 'indices');
506
+ this.dracoDecoded.add(packed.bufferView);
507
+ }
508
+ geometry(primitive, targetNames) {
509
+ const key = JSON.stringify([primitive.attributes, primitive.indices, primitive.mode, primitive.targets]);
510
+ const cached = this.geometryCache.get(key);
511
+ if (cached)
512
+ return cached;
513
+ const attributes = primitive.attributes ?? fail('primitive attributes');
514
+ if (attributes.POSITION === undefined)
515
+ fail('primitive without POSITION');
516
+ const geometry = new Geometry3D();
517
+ geometry.setAttribute('position', new Attribute3D(this.floats(attributes.POSITION), 3));
518
+ const vertexCount = geometry.vertexCount;
519
+ const add = (semantic, name, size) => {
520
+ if (attributes[semantic] === undefined)
521
+ return;
522
+ const data = this.floats(attributes[semantic]);
523
+ if (data.length !== vertexCount * size)
524
+ fail(semantic + ' length');
525
+ geometry.setAttribute(name, new Attribute3D(data, size));
526
+ };
527
+ add('NORMAL', 'normal', 3);
528
+ add('TANGENT', 'tangent', 4);
529
+ add('TEXCOORD_0', 'uv', 2);
530
+ add('TEXCOORD_1', 'uv1', 2);
531
+ add('WEIGHTS_0', 'weights', 4);
532
+ if (attributes.COLOR_0 !== undefined) {
533
+ const accessor = this.accessor(attributes.COLOR_0);
534
+ geometry.setAttribute('color', new Attribute3D(this.floats(attributes.COLOR_0), accessor.itemSize));
535
+ }
536
+ if (attributes.JOINTS_0 !== undefined) {
537
+ const joints = this.accessor(attributes.JOINTS_0).array;
538
+ geometry.setAttribute('joints', new Attribute3D(joints instanceof Uint16Array ? joints : Uint16Array.from(joints), 4));
539
+ }
540
+ let indices;
541
+ if (primitive.indices !== undefined) {
542
+ const array = this.accessor(primitive.indices).array;
543
+ indices = array instanceof Uint32Array || array instanceof Uint16Array ? array : Uint16Array.from(array);
544
+ for (let i = 0; i < indices.length; i++)
545
+ if (indices[i] >= vertexCount)
546
+ fail('index out of range');
547
+ }
548
+ const mode = primitive.mode ?? 4;
549
+ const topologies = {
550
+ 0: 'points',
551
+ 1: 'lines',
552
+ 2: 'line-strip',
553
+ 3: 'line-strip',
554
+ 4: 'triangles',
555
+ 5: 'triangles',
556
+ 6: 'triangles'
557
+ };
558
+ geometry.topology = topologies[mode] ?? fail('primitive mode');
559
+ if (mode === 2 || mode === 5 || mode === 6) {
560
+ // Rewrite loops, strips and fans as plain lists the renderer understands.
561
+ const source = indices ?? Uint32Array.from({ length: vertexCount }, (_, i) => i), out = [];
562
+ if (mode === 2) {
563
+ out.push(...source);
564
+ if (source.length)
565
+ out.push(source[0]);
566
+ }
567
+ else
568
+ for (let i = 2; i < source.length; i++) {
569
+ if (mode === 6)
570
+ out.push(source[0], source[i - 1], source[i]);
571
+ else if (i % 2 === 0)
572
+ out.push(source[i - 2], source[i - 1], source[i]);
573
+ else
574
+ out.push(source[i - 1], source[i - 2], source[i]);
575
+ }
576
+ indices = vertexCount > 65535 ? Uint32Array.from(out) : Uint16Array.from(out);
577
+ }
578
+ if (indices)
579
+ geometry.setIndex(indices);
580
+ if (Array.isArray(primitive.targets) && primitive.targets.length) {
581
+ geometry.setMorphTargets(primitive.targets.map((target, i) => ({
582
+ name: targetNames?.[i] ?? 'target_' + i,
583
+ ...(target.POSITION !== undefined ? { position: this.floats(target.POSITION) } : {}),
584
+ ...(target.NORMAL !== undefined ? { normal: this.floats(target.NORMAL) } : {})
585
+ })));
586
+ }
587
+ this.geometries.push(geometry);
588
+ this.geometryCache.set(key, geometry);
589
+ return geometry;
590
+ }
591
+ async meshes(index) {
592
+ const def = this.json.meshes?.[index] ?? fail('mesh ' + index);
593
+ const primitives = Array.isArray(def.primitives) ? def.primitives : fail('mesh primitives');
594
+ const names = def.extras?.targetNames;
595
+ return Promise.all(primitives.map(async (primitive, i) => {
596
+ await this.prepareDraco(primitive);
597
+ const geometry = this.geometry(primitive, names);
598
+ const lit = geometry.topology === 'triangles';
599
+ const material = await this.material(primitive.material, geometry.getAttribute('color') !== undefined, lit && !geometry.getAttribute('normal'));
600
+ const mesh = new Mesh3D(geometry, material);
601
+ mesh.name = (def.name ?? 'mesh_' + index) + (primitives.length > 1 ? '_' + i : '');
602
+ if (Array.isArray(def.weights))
603
+ mesh.morphWeights.set(def.weights.slice(0, mesh.morphWeights.length));
604
+ return mesh;
605
+ }));
606
+ }
607
+ /**
608
+ * EXT_mesh_gpu_instancing: replace a node's meshes with instanced meshes
609
+ * placed by the extension's TRANSLATION, ROTATION and SCALE accessors.
610
+ * An optional _COLOR_0 accessor tints each instance.
611
+ */
612
+ instanced(meshes, extension) {
613
+ const attributes = extension.attributes ?? fail('EXT_mesh_gpu_instancing.attributes');
614
+ const read = (name, itemSize) => {
615
+ const index = attributes[name];
616
+ if (index === undefined)
617
+ return undefined;
618
+ const accessor = this.accessor(index);
619
+ if (accessor.itemSize !== itemSize)
620
+ fail('instancing ' + name);
621
+ return this.floats(index);
622
+ };
623
+ const translation = read('TRANSLATION', 3), rotation = read('ROTATION', 4), scale = read('SCALE', 3);
624
+ const colorIndex = attributes['_COLOR_0'], colorSize = colorIndex === undefined ? 0 : this.accessor(colorIndex).itemSize;
625
+ if (colorSize !== 0 && colorSize !== 3 && colorSize !== 4)
626
+ fail('instancing _COLOR_0');
627
+ const colors = colorIndex === undefined ? undefined : this.floats(colorIndex);
628
+ const counts = [
629
+ translation && translation.length / 3,
630
+ rotation && rotation.length / 4,
631
+ scale && scale.length / 3,
632
+ colors && colors.length / colorSize
633
+ ].filter((n) => n !== undefined);
634
+ const count = counts[0] ?? fail('instancing needs an attribute');
635
+ if (count < 1 || counts.some(n => n !== count))
636
+ fail('instancing accessor counts');
637
+ if (count > MESH3D_LIMITS.maxInstances)
638
+ throw new World3DError('WORLD3D_BUDGET', 'gltf instances');
639
+ // Nodes and primitives may share accessors, but their generated matrices are distinct.
640
+ const matrixBytes = count * meshes.length * 16 * Float32Array.BYTES_PER_ELEMENT;
641
+ if (this.instanceMatrixBytes + matrixBytes > GLTF_LIMITS.maxInstanceMatrixBytes)
642
+ throw new World3DError('WORLD3D_BUDGET', 'gltf instance matrices');
643
+ this.instanceMatrixBytes += matrixBytes;
644
+ const t = [0, 0, 0], q = [0, 0, 0, 1], k = [1, 1, 1];
645
+ return meshes.map(mesh => {
646
+ const instances = new InstancedMesh3D(mesh.geometry, mesh.material, count, colorSize === 4 ? 4 : 3);
647
+ instances.name = mesh.name;
648
+ instances.morphWeights.set(mesh.morphWeights);
649
+ for (let i = 0; i < count; i++) {
650
+ if (translation) {
651
+ t[0] = translation[i * 3];
652
+ t[1] = translation[i * 3 + 1];
653
+ t[2] = translation[i * 3 + 2];
654
+ }
655
+ if (rotation) {
656
+ // Quantized rotations are only approximately unit length.
657
+ const x = rotation[i * 4], y = rotation[i * 4 + 1], z = rotation[i * 4 + 2], w = rotation[i * 4 + 3], length = Math.hypot(x, y, z, w) || 1;
658
+ q[0] = x / length;
659
+ q[1] = y / length;
660
+ q[2] = z / length;
661
+ q[3] = w / length;
662
+ }
663
+ if (scale) {
664
+ k[0] = scale[i * 3];
665
+ k[1] = scale[i * 3 + 1];
666
+ k[2] = scale[i * 3 + 2];
667
+ }
668
+ mat4Compose(instances.instanceMatrices.subarray(i * 16, i * 16 + 16), t, q, k);
669
+ }
670
+ if (colors)
671
+ instances.instanceColors = colors.slice();
672
+ instances.instanceVersion++;
673
+ return instances;
674
+ });
675
+ }
676
+ // ---- nodes, skins, scenes -----------------------------------------------------
677
+ camera(index) {
678
+ const def = this.json.cameras?.[index] ?? fail('camera ' + index);
679
+ const camera = new Camera3D(def.type === 'orthographic' ? 'orthographic' : 'perspective');
680
+ if (def.type === 'orthographic') {
681
+ const o = def.orthographic ?? fail('camera.orthographic');
682
+ camera.orthographicHeight = o.ymag * 2;
683
+ camera.aspect = o.xmag / o.ymag;
684
+ camera.near = o.znear;
685
+ camera.far = o.zfar;
686
+ }
687
+ else {
688
+ const p = def.perspective ?? fail('camera.perspective');
689
+ camera.fov = (p.yfov * 180) / Math.PI;
690
+ camera.aspect = p.aspectRatio ?? 1;
691
+ camera.near = p.znear;
692
+ camera.far = p.zfar ?? p.znear * 10000;
693
+ }
694
+ camera.name = def.name ?? 'camera_' + index;
695
+ return camera;
696
+ }
697
+ light(index) {
698
+ const def = this.json.extensions?.KHR_lights_punctual?.lights?.[index] ?? fail('light ' + index);
699
+ const light = new Light3D(def.type === 'point' ? 'point' : def.type === 'spot' ? 'spot' : 'directional');
700
+ if (Array.isArray(def.color))
701
+ light.color.set(def.color);
702
+ light.intensity = def.intensity ?? 1;
703
+ light.range = def.range ?? 0;
704
+ if (def.type === 'spot') {
705
+ const outer = def.spot?.outerConeAngle ?? Math.PI / 4, inner = def.spot?.innerConeAngle ?? 0;
706
+ light.angle = outer;
707
+ light.penumbra = outer > 0 ? 1 - inner / outer : 0;
708
+ }
709
+ light.name = def.name ?? 'light_' + index;
710
+ return light;
711
+ }
712
+ async build() {
713
+ const json = this.json, nodeDefs = json.nodes ?? [];
714
+ if (nodeDefs.length > GLTF_LIMITS.maxNodes)
715
+ throw new World3DError('WORLD3D_BUDGET', 'gltf nodes');
716
+ const joints = new Set();
717
+ for (const skin of (json.skins ?? []))
718
+ for (const joint of skin.joints ?? [])
719
+ joints.add(joint);
720
+ const usedNames = new Set(), cameras = [];
721
+ // Animation tracks bind by name, so every node receives a unique one.
722
+ const unique = (name) => {
723
+ let candidate = name, suffix = 1;
724
+ while (usedNames.has(candidate))
725
+ candidate = name + '_' + suffix++;
726
+ usedNames.add(candidate);
727
+ return candidate;
728
+ };
729
+ this.nodes = [];
730
+ this.nodeMeshes = [];
731
+ for (let i = 0; i < nodeDefs.length; i++) {
732
+ const def = nodeDefs[i];
733
+ let meshes = def.mesh !== undefined ? await this.meshes(def.mesh) : [];
734
+ const instancing = def.extensions?.EXT_mesh_gpu_instancing;
735
+ if (instancing && meshes.length > 0)
736
+ meshes = this.instanced(meshes, instancing);
737
+ // A single-primitive mesh becomes the node itself; otherwise the
738
+ // node is a plain parent holding one mesh per primitive.
739
+ let node;
740
+ if (meshes.length === 1 && !joints.has(i))
741
+ node = meshes[0];
742
+ else {
743
+ node = joints.has(i) ? new Bone3D() : new Object3D();
744
+ for (const mesh of meshes)
745
+ node.addChild(mesh);
746
+ }
747
+ node.name = unique(def.name ?? 'node_' + i);
748
+ for (const mesh of meshes)
749
+ if (mesh !== node)
750
+ mesh.name = unique(mesh.name);
751
+ if (Array.isArray(def.matrix))
752
+ node.setMatrix(def.matrix);
753
+ else {
754
+ if (Array.isArray(def.translation))
755
+ node.position.set(def.translation);
756
+ if (Array.isArray(def.rotation))
757
+ node.setQuaternion(def.rotation[0], def.rotation[1], def.rotation[2], def.rotation[3]);
758
+ if (Array.isArray(def.scale))
759
+ node.scale.set(def.scale);
760
+ node.markTransformDirty();
761
+ }
762
+ if (Array.isArray(def.weights))
763
+ for (const mesh of meshes)
764
+ mesh.morphWeights.set(def.weights.slice(0, mesh.morphWeights.length));
765
+ if (def.camera !== undefined) {
766
+ const camera = this.camera(def.camera);
767
+ camera.name = unique(camera.name);
768
+ node.addChild(camera);
769
+ cameras.push(camera);
770
+ }
771
+ const lightIndex = def.extensions?.KHR_lights_punctual?.light;
772
+ if (lightIndex !== undefined) {
773
+ const light = this.light(lightIndex);
774
+ light.name = unique(light.name);
775
+ node.addChild(light);
776
+ }
777
+ this.nodes.push(node);
778
+ this.nodeMeshes.push(meshes);
779
+ }
780
+ const parented = new Set();
781
+ nodeDefs.forEach((def, i) => {
782
+ for (const child of (def.children ?? [])) {
783
+ const node = this.nodes[child] ?? fail('node child');
784
+ if (parented.has(child) || child === i)
785
+ fail('node hierarchy');
786
+ parented.add(child);
787
+ this.nodes[i].addChild(node);
788
+ }
789
+ });
790
+ const assignedRoots = new Set();
791
+ const scenes = (json.scenes ?? [{ nodes: nodeDefs.map((_, i) => i).filter(i => !parented.has(i)) }]).map((def, i) => {
792
+ const root = new Object3D();
793
+ root.name = def.name ?? 'scene_' + i;
794
+ const sceneRoots = new Set();
795
+ for (const index of (def.nodes ?? [])) {
796
+ const node = this.nodes[index] ?? fail('scene node');
797
+ if (parented.has(index) || sceneRoots.has(index))
798
+ fail('scene root ' + index);
799
+ // A scene node has one owner. Reparenting would silently erase it from an earlier scene.
800
+ if (assignedRoots.has(index))
801
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'shared scene root ' + index);
802
+ sceneRoots.add(index);
803
+ assignedRoots.add(index);
804
+ root.addChild(node);
805
+ }
806
+ root.updateWorldMatrices();
807
+ return root;
808
+ });
809
+ const skeletons = (json.skins ?? []).map(skin => {
810
+ const jointIndices = skin.joints;
811
+ requireCount3D(jointIndices.length, 'bones', MESH3D_LIMITS.maxBones);
812
+ const bones = jointIndices.map(index => this.nodes[index] ?? fail('skin joint'));
813
+ const matrixLength = bones.length * 16;
814
+ if (skin.inverseBindMatrices !== undefined) {
815
+ const { array, count, itemSize } = this.accessor(skin.inverseBindMatrices);
816
+ if (!(array instanceof Float32Array) || itemSize !== 16 || count < bones.length)
817
+ fail('inverse bind matrices');
818
+ return new Skeleton3D(bones, array.subarray(0, matrixLength));
819
+ }
820
+ // glTF defines omitted inverse bind matrices as identity for every joint.
821
+ const inverse = new Float32Array(matrixLength);
822
+ for (let offset = 0; offset < matrixLength; offset += 16)
823
+ mat4Identity(inverse.subarray(offset, offset + 16));
824
+ return new Skeleton3D(bones, inverse);
825
+ });
826
+ nodeDefs.forEach((def, i) => {
827
+ if (def.skin === undefined)
828
+ return;
829
+ const skeleton = skeletons[def.skin] ?? fail('node skin');
830
+ for (const mesh of this.nodeMeshes[i]) {
831
+ // glTF ignores the skinned mesh's own transform: bind in world space.
832
+ if (mesh.geometry.getAttribute('joints') && mesh.geometry.getAttribute('weights'))
833
+ mesh.bind(skeleton, [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
834
+ }
835
+ });
836
+ return { scenes, cameras, animations: this.animations(), defaultScene: json.scene ?? 0 };
837
+ }
838
+ animations() {
839
+ return (this.json.animations ?? []).map((def, index) => {
840
+ const tracks = [];
841
+ for (const channel of (def.channels ?? [])) {
842
+ const sampler = def.samplers?.[channel.sampler] ?? fail('animation sampler');
843
+ const nodeIndex = channel.target?.node, path = channel.target?.path;
844
+ if (nodeIndex === undefined)
845
+ continue;
846
+ const node = this.nodes[nodeIndex] ?? fail('animation target');
847
+ const mapped = path === 'translation'
848
+ ? 'position'
849
+ : path === 'rotation'
850
+ ? 'quaternion'
851
+ : path === 'scale'
852
+ ? 'scale'
853
+ : path === 'weights'
854
+ ? 'weights'
855
+ : undefined;
856
+ if (!mapped)
857
+ continue;
858
+ const interpolation = sampler.interpolation === 'STEP' ? 'step' : sampler.interpolation === 'CUBICSPLINE' ? 'cubic' : 'linear';
859
+ const times = this.floats(sampler.input), values = this.floats(sampler.output);
860
+ // Morph weights live on mesh objects, which may be children of the node.
861
+ const targets = mapped === 'weights' ? this.nodeMeshes[nodeIndex].map(mesh => mesh.name) : [node.name];
862
+ for (const targetName of targets)
863
+ tracks.push({ targetName, path: mapped, times, values, interpolation });
864
+ }
865
+ return new AnimationClip3D(def.name ?? 'animation_' + index, tracks);
866
+ });
867
+ }
868
+ }
869
+ /** Split a .glb container, or parse .gltf JSON text, into JSON plus buffers. */
870
+ async function unpack(data, options) {
871
+ let json, binary;
872
+ if (data instanceof Uint8Array) {
873
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
874
+ if (data.byteLength >= 12 && view.getUint32(0, true) === 0x46546c67) {
875
+ if (view.getUint32(4, true) !== 2)
876
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'only glTF 2.0 containers are supported');
877
+ const total = Math.min(view.getUint32(8, true), data.byteLength);
878
+ let offset = 12, text;
879
+ while (offset + 8 <= total) {
880
+ const length = view.getUint32(offset, true), type = view.getUint32(offset + 4, true);
881
+ if (offset + 8 + length > total)
882
+ fail('glb chunk range');
883
+ const chunk = data.subarray(offset + 8, offset + 8 + length);
884
+ if (type === 0x4e4f534a)
885
+ text = chunk;
886
+ else if (type === 0x004e4942 && !binary)
887
+ binary = chunk;
888
+ offset += 8 + length + ((4 - (length % 4)) % 4);
889
+ }
890
+ if (!text)
891
+ fail('glb without JSON chunk');
892
+ if (text.byteLength > GLTF_LIMITS.maxJsonBytes)
893
+ throw new World3DError('WORLD3D_BUDGET', 'gltf json');
894
+ try {
895
+ json = JSON.parse(decodeUtf8(text));
896
+ }
897
+ catch (cause) {
898
+ fail('json', cause);
899
+ }
900
+ }
901
+ else {
902
+ if (data.byteLength > GLTF_LIMITS.maxJsonBytes)
903
+ throw new World3DError('WORLD3D_BUDGET', 'gltf json');
904
+ try {
905
+ json = JSON.parse(decodeUtf8(data));
906
+ }
907
+ catch (cause) {
908
+ fail('json', cause);
909
+ }
910
+ }
911
+ }
912
+ else if (data && typeof data === 'object')
913
+ json = data;
914
+ else
915
+ fail('input');
916
+ if (!json || typeof json !== 'object' || json.asset?.version?.[0] !== '2')
917
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'asset.version');
918
+ if (Array.isArray(json.accessors) && json.accessors.length > GLTF_LIMITS.maxAccessors)
919
+ throw new World3DError('WORLD3D_BUDGET', 'gltf accessors');
920
+ for (const extension of (json.extensionsRequired ?? [])) {
921
+ const decoder = DECODER_EXTENSIONS[extension];
922
+ if (decoder && typeof options[decoder] === 'function')
923
+ continue;
924
+ if (decoder)
925
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'required extension ' + extension + ' needs the ' + decoder + ' option');
926
+ if (!SUPPORTED_EXTENSIONS.has(extension))
927
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'required extension ' + extension);
928
+ }
929
+ const buffers = await Promise.all((json.buffers ?? []).map(async (def, index) => {
930
+ if (def.uri === undefined)
931
+ return index === 0 ? binary : undefined;
932
+ const embedded = dataUri(def.uri);
933
+ if (embedded)
934
+ return embedded.bytes;
935
+ if (!options.loadBuffer)
936
+ throw new World3DError('WORLD3D_GLTF_UNSUPPORTED', 'loadBuffer is required for external buffers');
937
+ return options.loadBuffer(def.uri);
938
+ }));
939
+ return { json, buffers };
940
+ }
941
+ /** Parse a glTF 2.0 asset from .glb bytes, .gltf JSON bytes or an already parsed JSON object. */
942
+ export async function loadGltf3D(data, options = {}) {
943
+ const { json, buffers } = await unpack(data, options);
944
+ const reader = new GltfReader(json, buffers, options);
945
+ let built;
946
+ try {
947
+ built = await reader.build();
948
+ }
949
+ catch (cause) {
950
+ // Never leak partially created resources when loading fails midway.
951
+ for (const resource of [...reader.geometries, ...reader.materials, ...reader.textures])
952
+ resource.dispose();
953
+ if (cause instanceof World3DError)
954
+ throw cause;
955
+ throw new World3DError('WORLD3D_GLTF_INVALID', 'malformed asset', cause);
956
+ }
957
+ const scene = built.scenes[built.defaultScene] ?? built.scenes[0] ?? new Object3D();
958
+ let disposed = false;
959
+ return Object.freeze({
960
+ scene,
961
+ scenes: Object.freeze(built.scenes),
962
+ animations: Object.freeze(built.animations),
963
+ cameras: Object.freeze(built.cameras),
964
+ materials: Object.freeze(reader.materials),
965
+ textures: Object.freeze(reader.textures),
966
+ geometries: Object.freeze(reader.geometries),
967
+ dispose() {
968
+ if (disposed)
969
+ return;
970
+ disposed = true;
971
+ for (const resource of [...reader.geometries, ...reader.materials, ...reader.textures])
972
+ resource.dispose();
973
+ }
974
+ });
975
+ }
976
+ //# sourceMappingURL=Gltf3D.js.map