chorama 0.1.2 → 0.2.1

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 (483) hide show
  1. package/README.md +2 -1
  2. package/dist/index.module.js +32480 -15098
  3. package/dist/index.module.js.map +1 -1
  4. package/dist/index.umd.js +32628 -15121
  5. package/dist/index.umd.js.map +1 -1
  6. package/package.json +7 -2
  7. package/.configs/tsconfig.lib.json +0 -43
  8. package/.configs/tsconfig.website.json +0 -34
  9. package/.github/workflows/publish.yml +0 -24
  10. package/.github/workflows/static.yml +0 -57
  11. package/.vscode/launch.json +0 -29
  12. package/.vscode/tasks.json +0 -19
  13. package/assets/images/disappointed.jpg +0 -0
  14. package/assets/images/skybox/grimmnight_back.png +0 -0
  15. package/assets/images/skybox/grimmnight_bottom.png +0 -0
  16. package/assets/images/skybox/grimmnight_front.png +0 -0
  17. package/assets/images/skybox/grimmnight_left.png +0 -0
  18. package/assets/images/skybox/grimmnight_right.png +0 -0
  19. package/assets/images/skybox/grimmnight_top.png +0 -0
  20. package/assets/images/skybox/miramar_back.png +0 -0
  21. package/assets/images/skybox/miramar_bottom.png +0 -0
  22. package/assets/images/skybox/miramar_front.png +0 -0
  23. package/assets/images/skybox/miramar_left.png +0 -0
  24. package/assets/images/skybox/miramar_right.png +0 -0
  25. package/assets/images/skybox/miramar_top.png +0 -0
  26. package/assets/images/uv.jpg +0 -0
  27. package/assets/models/glb/AlphaBlendModeTest.glb +0 -0
  28. package/assets/models/glb/IridescenceLamp.glb +0 -0
  29. package/assets/models/glb/teapot.glb +0 -0
  30. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
  31. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
  32. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
  33. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
  34. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
  35. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
  36. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
  37. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
  38. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
  39. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
  40. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
  41. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
  42. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
  43. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
  44. package/assets/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
  45. package/assets/models/gltf/flight_helmet/index.bin +0 -0
  46. package/assets/models/gltf/flight_helmet/index.gltf +0 -705
  47. package/assets/models/gltf/object.gltf +0 -23
  48. package/assets/models/gltf/pirate_girl/index.bin +0 -0
  49. package/assets/models/gltf/pirate_girl/index.gltf +0 -2082
  50. package/assets/models/obj/pirate_girl/pirate_girl.obj +0 -18459
  51. package/assets/models/obj/pirate_girl/pirate_girl.png +0 -0
  52. package/astro.config.mjs +0 -33
  53. package/content/guide/api-map.md +0 -89
  54. package/content/guide/camera-and-controls.md +0 -98
  55. package/content/guide/first-scene.md +0 -176
  56. package/content/guide/index.md +0 -72
  57. package/content/guide/installation.md +0 -179
  58. package/content/guide/materials-and-lighting.md +0 -138
  59. package/content/guide/plugins-and-render-pipeline.md +0 -124
  60. package/content/guide/render-targets-and-views.md +0 -146
  61. package/content/guide/scene-graph-and-transforms.md +0 -113
  62. package/content/guide/textures-and-assets.md +0 -123
  63. package/content/guide/troubleshooting.md +0 -49
  64. package/dist/website/_astro/DocLayout.BsV7p0dY.css +0 -1
  65. package/dist/website/examples/camera/orthographic/example.html +0 -172
  66. package/dist/website/examples/camera/orthographic/index.html +0 -124
  67. package/dist/website/examples/camera/perspective/example.html +0 -190
  68. package/dist/website/examples/camera/perspective/index.html +0 -142
  69. package/dist/website/examples/camera/tone_mapping/example.html +0 -238
  70. package/dist/website/examples/camera/tone_mapping/index.html +0 -190
  71. package/dist/website/examples/index.html +0 -2
  72. package/dist/website/examples/lights/directional/example.html +0 -447
  73. package/dist/website/examples/lights/directional/index.html +0 -399
  74. package/dist/website/examples/lights/multiple_spot_lights/example.html +0 -356
  75. package/dist/website/examples/lights/multiple_spot_lights/index.html +0 -308
  76. package/dist/website/examples/lights/point/example.html +0 -387
  77. package/dist/website/examples/lights/point/index.html +0 -339
  78. package/dist/website/examples/lights/spot/example.html +0 -416
  79. package/dist/website/examples/lights/spot/index.html +0 -368
  80. package/dist/website/examples/loader/gltf/basic/example.html +0 -129
  81. package/dist/website/examples/loader/gltf/basic/index.html +0 -81
  82. package/dist/website/examples/loader/gltf/material/alpha-blend-mode/example.html +0 -131
  83. package/dist/website/examples/loader/gltf/material/alpha-blend-mode/index.html +0 -83
  84. package/dist/website/examples/loader/gltf/material/flight_helmet/example.html +0 -162
  85. package/dist/website/examples/loader/gltf/material/flight_helmet/index.html +0 -114
  86. package/dist/website/examples/loader/gltf/material/iridescence_lamp/example.html +0 -169
  87. package/dist/website/examples/loader/gltf/material/iridescence_lamp/index.html +0 -121
  88. package/dist/website/examples/loader/objloader/example.html +0 -144
  89. package/dist/website/examples/loader/objloader/index.html +0 -96
  90. package/dist/website/examples/material/cullface/example.html +0 -163
  91. package/dist/website/examples/material/cullface/index.html +0 -115
  92. package/dist/website/examples/material/materials/example.html +0 -175
  93. package/dist/website/examples/material/materials/index.html +0 -127
  94. package/dist/website/examples/material/standard/basic/example.html +0 -212
  95. package/dist/website/examples/material/standard/basic/index.html +0 -164
  96. package/dist/website/examples/material/standard/reflection_refraction/example.html +0 -241
  97. package/dist/website/examples/material/standard/reflection_refraction/index.html +0 -193
  98. package/dist/website/examples/mesh/capsule/example.html +0 -173
  99. package/dist/website/examples/mesh/capsule/index.html +0 -125
  100. package/dist/website/examples/mesh/circle/example.html +0 -173
  101. package/dist/website/examples/mesh/circle/index.html +0 -125
  102. package/dist/website/examples/mesh/cone/example.html +0 -180
  103. package/dist/website/examples/mesh/cone/index.html +0 -132
  104. package/dist/website/examples/mesh/cuboid/example.html +0 -207
  105. package/dist/website/examples/mesh/cuboid/index.html +0 -159
  106. package/dist/website/examples/mesh/cylinder/example.html +0 -195
  107. package/dist/website/examples/mesh/cylinder/index.html +0 -147
  108. package/dist/website/examples/mesh/dodecahedron/example.html +0 -158
  109. package/dist/website/examples/mesh/dodecahedron/index.html +0 -110
  110. package/dist/website/examples/mesh/ellipsoid/example.html +0 -179
  111. package/dist/website/examples/mesh/ellipsoid/index.html +0 -131
  112. package/dist/website/examples/mesh/extrude/example.html +0 -169
  113. package/dist/website/examples/mesh/extrude/index.html +0 -121
  114. package/dist/website/examples/mesh/frustum/example.html +0 -185
  115. package/dist/website/examples/mesh/frustum/index.html +0 -137
  116. package/dist/website/examples/mesh/helix/example.html +0 -184
  117. package/dist/website/examples/mesh/helix/index.html +0 -136
  118. package/dist/website/examples/mesh/icosahedron/example.html +0 -158
  119. package/dist/website/examples/mesh/icosahedron/index.html +0 -110
  120. package/dist/website/examples/mesh/icosphere/example.html +0 -163
  121. package/dist/website/examples/mesh/icosphere/index.html +0 -115
  122. package/dist/website/examples/mesh/lathe/example.html +0 -175
  123. package/dist/website/examples/mesh/lathe/index.html +0 -127
  124. package/dist/website/examples/mesh/meshTopology/example.html +0 -210
  125. package/dist/website/examples/mesh/meshTopology/index.html +0 -162
  126. package/dist/website/examples/mesh/octahedron/example.html +0 -158
  127. package/dist/website/examples/mesh/octahedron/index.html +0 -110
  128. package/dist/website/examples/mesh/plane/example.html +0 -173
  129. package/dist/website/examples/mesh/plane/index.html +0 -125
  130. package/dist/website/examples/mesh/prism/example.html +0 -173
  131. package/dist/website/examples/mesh/prism/index.html +0 -125
  132. package/dist/website/examples/mesh/pyramid/example.html +0 -172
  133. package/dist/website/examples/mesh/pyramid/index.html +0 -124
  134. package/dist/website/examples/mesh/ring/example.html +0 -173
  135. package/dist/website/examples/mesh/ring/index.html +0 -125
  136. package/dist/website/examples/mesh/skinning/example.html +0 -187
  137. package/dist/website/examples/mesh/skinning/index.html +0 -139
  138. package/dist/website/examples/mesh/terrain/example.html +0 -214
  139. package/dist/website/examples/mesh/terrain/index.html +0 -166
  140. package/dist/website/examples/mesh/tetrahedron/example.html +0 -158
  141. package/dist/website/examples/mesh/tetrahedron/index.html +0 -110
  142. package/dist/website/examples/mesh/torus/example.html +0 -173
  143. package/dist/website/examples/mesh/torus/index.html +0 -125
  144. package/dist/website/examples/mesh/torus-knot/example.html +0 -188
  145. package/dist/website/examples/mesh/torus-knot/index.html +0 -140
  146. package/dist/website/examples/mesh/tube/example.html +0 -180
  147. package/dist/website/examples/mesh/tube/index.html +0 -132
  148. package/dist/website/examples/mesh/uvsphere/example.html +0 -169
  149. package/dist/website/examples/mesh/uvsphere/index.html +0 -121
  150. package/dist/website/examples/mesh/wedge/example.html +0 -168
  151. package/dist/website/examples/mesh/wedge/index.html +0 -120
  152. package/dist/website/examples/other/rotatingCube/example.html +0 -142
  153. package/dist/website/examples/other/rotatingCube/index.html +0 -94
  154. package/dist/website/examples/other/rotatingSphere/example.html +0 -144
  155. package/dist/website/examples/other/rotatingSphere/index.html +0 -96
  156. package/dist/website/examples/rendertarget/basic_canvas/example.html +0 -178
  157. package/dist/website/examples/rendertarget/basic_canvas/index.html +0 -130
  158. package/dist/website/examples/rendertarget/depth_texture/example.html +0 -188
  159. package/dist/website/examples/rendertarget/depth_texture/index.html +0 -140
  160. package/dist/website/examples/rendertarget/image_target/example.html +0 -196
  161. package/dist/website/examples/rendertarget/image_target/index.html +0 -148
  162. package/dist/website/examples/rendertarget/multiple_views/example.html +0 -204
  163. package/dist/website/examples/rendertarget/multiple_views/index.html +0 -156
  164. package/dist/website/examples/rendertarget/render_masks/example.html +0 -222
  165. package/dist/website/examples/rendertarget/render_masks/index.html +0 -174
  166. package/dist/website/examples/rendertarget/split_screen/example.html +0 -178
  167. package/dist/website/examples/rendertarget/split_screen/index.html +0 -130
  168. package/dist/website/examples/rendertarget/split_view/example.html +0 -191
  169. package/dist/website/examples/rendertarget/split_view/index.html +0 -143
  170. package/dist/website/examples/skybox/skybox/example.html +0 -162
  171. package/dist/website/examples/skybox/skybox/index.html +0 -114
  172. package/dist/website/examples/texture/arrays/example.html +0 -204
  173. package/dist/website/examples/texture/arrays/index.html +0 -156
  174. package/dist/website/examples/texture/textureWrap/example.html +0 -169
  175. package/dist/website/examples/texture/textureWrap/index.html +0 -121
  176. package/dist/website/examples/transform/propagation/example.html +0 -141
  177. package/dist/website/examples/transform/propagation/index.html +0 -93
  178. package/dist/website/guide/api-map/index.html +0 -82
  179. package/dist/website/guide/camera-and-controls/index.html +0 -62
  180. package/dist/website/guide/first-scene/index.html +0 -119
  181. package/dist/website/guide/index.html +0 -58
  182. package/dist/website/guide/installation/index.html +0 -133
  183. package/dist/website/guide/materials-and-lighting/index.html +0 -90
  184. package/dist/website/guide/plugins-and-render-pipeline/index.html +0 -89
  185. package/dist/website/guide/render-targets-and-views/index.html +0 -95
  186. package/dist/website/guide/scene-graph-and-transforms/index.html +0 -69
  187. package/dist/website/guide/textures-and-assets/index.html +0 -85
  188. package/dist/website/guide/troubleshooting/index.html +0 -39
  189. package/dist/website/images/disappointed.jpg +0 -0
  190. package/dist/website/images/skybox/grimmnight_back.png +0 -0
  191. package/dist/website/images/skybox/grimmnight_bottom.png +0 -0
  192. package/dist/website/images/skybox/grimmnight_front.png +0 -0
  193. package/dist/website/images/skybox/grimmnight_left.png +0 -0
  194. package/dist/website/images/skybox/grimmnight_right.png +0 -0
  195. package/dist/website/images/skybox/grimmnight_top.png +0 -0
  196. package/dist/website/images/skybox/miramar_back.png +0 -0
  197. package/dist/website/images/skybox/miramar_bottom.png +0 -0
  198. package/dist/website/images/skybox/miramar_front.png +0 -0
  199. package/dist/website/images/skybox/miramar_left.png +0 -0
  200. package/dist/website/images/skybox/miramar_right.png +0 -0
  201. package/dist/website/images/skybox/miramar_top.png +0 -0
  202. package/dist/website/images/uv.jpg +0 -0
  203. package/dist/website/index.html +0 -157
  204. package/dist/website/models/glb/AlphaBlendModeTest.glb +0 -0
  205. package/dist/website/models/glb/IridescenceLamp.glb +0 -0
  206. package/dist/website/models/glb/teapot.glb +0 -0
  207. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
  208. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
  209. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
  210. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
  211. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
  212. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
  213. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
  214. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
  215. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
  216. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
  217. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
  218. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
  219. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
  220. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
  221. package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
  222. package/dist/website/models/gltf/flight_helmet/index.bin +0 -0
  223. package/dist/website/models/gltf/flight_helmet/index.gltf +0 -705
  224. package/dist/website/models/gltf/object.gltf +0 -23
  225. package/dist/website/models/gltf/pirate_girl/index.bin +0 -0
  226. package/dist/website/models/gltf/pirate_girl/index.gltf +0 -2082
  227. package/dist/website/models/obj/pirate_girl/pirate_girl.obj +0 -18459
  228. package/dist/website/models/obj/pirate_girl/pirate_girl.png +0 -0
  229. package/env.d.ts +0 -5
  230. package/examples/camera/orthographic.js +0 -121
  231. package/examples/camera/perspective.js +0 -139
  232. package/examples/camera/tone_mapping.js +0 -187
  233. package/examples/lights/directional.js +0 -396
  234. package/examples/lights/multiple_spot_lights.js +0 -305
  235. package/examples/lights/point.js +0 -336
  236. package/examples/lights/spot.js +0 -365
  237. package/examples/loader/gltf/basic.js +0 -78
  238. package/examples/loader/gltf/material/alpha-blend-mode.js +0 -80
  239. package/examples/loader/gltf/material/flight_helmet.js +0 -111
  240. package/examples/loader/gltf/material/iridescence_lamp.js +0 -118
  241. package/examples/loader/objloader.js +0 -93
  242. package/examples/material/cullface.js +0 -112
  243. package/examples/material/materials.js +0 -124
  244. package/examples/material/standard/basic.js +0 -161
  245. package/examples/material/standard/reflection_refraction.js +0 -190
  246. package/examples/mesh/capsule.js +0 -122
  247. package/examples/mesh/circle.js +0 -122
  248. package/examples/mesh/cone.js +0 -129
  249. package/examples/mesh/cuboid.js +0 -156
  250. package/examples/mesh/cylinder.js +0 -144
  251. package/examples/mesh/dodecahedron.js +0 -107
  252. package/examples/mesh/ellipsoid.js +0 -128
  253. package/examples/mesh/extrude.js +0 -118
  254. package/examples/mesh/frustum.js +0 -134
  255. package/examples/mesh/helix.js +0 -133
  256. package/examples/mesh/icosahedron.js +0 -107
  257. package/examples/mesh/icosphere.js +0 -112
  258. package/examples/mesh/lathe.js +0 -124
  259. package/examples/mesh/meshTopology.js +0 -159
  260. package/examples/mesh/octahedron.js +0 -107
  261. package/examples/mesh/plane.js +0 -122
  262. package/examples/mesh/prism.js +0 -122
  263. package/examples/mesh/pyramid.js +0 -121
  264. package/examples/mesh/ring.js +0 -122
  265. package/examples/mesh/skinning.js +0 -136
  266. package/examples/mesh/terrain.js +0 -163
  267. package/examples/mesh/tetrahedron.js +0 -107
  268. package/examples/mesh/torus-knot.js +0 -137
  269. package/examples/mesh/torus.js +0 -122
  270. package/examples/mesh/tube.js +0 -129
  271. package/examples/mesh/uvsphere.js +0 -118
  272. package/examples/mesh/wedge.js +0 -117
  273. package/examples/other/rotatingCube.js +0 -91
  274. package/examples/other/rotatingSphere.js +0 -93
  275. package/examples/rendertarget/basic_canvas.js +0 -127
  276. package/examples/rendertarget/depth_texture.js +0 -137
  277. package/examples/rendertarget/image_target.js +0 -145
  278. package/examples/rendertarget/multiple_views.js +0 -153
  279. package/examples/rendertarget/render_masks.js +0 -171
  280. package/examples/rendertarget/split_screen.js +0 -127
  281. package/examples/rendertarget/split_view.js +0 -140
  282. package/examples/skybox/skybox.js +0 -111
  283. package/examples/texture/arrays.js +0 -153
  284. package/examples/texture/textureWrap.js +0 -118
  285. package/examples/transform/propagation.js +0 -90
  286. package/rollup.config.js +0 -66
  287. package/src/caches/cache.js +0 -359
  288. package/src/caches/index.js +0 -2
  289. package/src/caches/uniformbuffers.js +0 -93
  290. package/src/cameracontrols/index.js +0 -258
  291. package/src/constants/index.js +0 -3
  292. package/src/constants/mesh.js +0 -197
  293. package/src/constants/others.js +0 -218
  294. package/src/constants/texture.js +0 -183
  295. package/src/content.config.js +0 -1
  296. package/src/core/constants.js +0 -14
  297. package/src/core/extensions.js +0 -42
  298. package/src/core/index.js +0 -7
  299. package/src/core/layouts/bindgroup.js +0 -167
  300. package/src/core/layouts/index.js +0 -6
  301. package/src/core/layouts/meshvertex.js +0 -60
  302. package/src/core/layouts/pipeline.js +0 -165
  303. package/src/core/layouts/uniform.js +0 -21
  304. package/src/core/layouts/uniformbuffer.js +0 -23
  305. package/src/core/layouts/vertexbuffer.js +0 -43
  306. package/src/core/limits.js +0 -247
  307. package/src/core/resources/blendparams.js +0 -89
  308. package/src/core/resources/gpubuffer.js +0 -53
  309. package/src/core/resources/gpumesh.js +0 -76
  310. package/src/core/resources/gpusampler.js +0 -43
  311. package/src/core/resources/gputexture.js +0 -102
  312. package/src/core/resources/index.js +0 -7
  313. package/src/core/resources/newuniformbuffer.js +0 -58
  314. package/src/core/resources/webglshadermodule.js +0 -58
  315. package/src/core/shader.js +0 -75
  316. package/src/core/webgl/bindgroup.js +0 -247
  317. package/src/core/webgl/descriptors.js +0 -166
  318. package/src/core/webgl/gpuqueue.js +0 -63
  319. package/src/core/webgl/index.js +0 -8
  320. package/src/core/webgl/renderpassencoder.js +0 -783
  321. package/src/core/webgl/renderpipeline.js +0 -91
  322. package/src/core/webgl/utils.js +0 -521
  323. package/src/core/webgl/webglrenderdevice.js +0 -259
  324. package/src/function.js +0 -358
  325. package/src/index.js +0 -15
  326. package/src/loader/gltf.js +0 -2722
  327. package/src/loader/index.js +0 -3
  328. package/src/loader/loader.js +0 -174
  329. package/src/loader/obj.js +0 -188
  330. package/src/loader/texture.js +0 -138
  331. package/src/loader/utils.js +0 -60
  332. package/src/material/alphablend.js +0 -21
  333. package/src/material/basic.js +0 -89
  334. package/src/material/depth.js +0 -87
  335. package/src/material/index.js +0 -9
  336. package/src/material/lambert.js +0 -87
  337. package/src/material/material.js +0 -148
  338. package/src/material/normal.js +0 -44
  339. package/src/material/phong.js +0 -100
  340. package/src/material/raw.js +0 -71
  341. package/src/material/standard.js +0 -309
  342. package/src/math/index.js +0 -3
  343. package/src/math/transform.js +0 -38
  344. package/src/mesh/attribute/attribute.js +0 -79
  345. package/src/mesh/attribute/index.js +0 -1
  346. package/src/mesh/attributedata/index.js +0 -1
  347. package/src/mesh/attributedata/separate.js +0 -180
  348. package/src/mesh/builders/base.js +0 -41
  349. package/src/mesh/builders/capsule.js +0 -47
  350. package/src/mesh/builders/circle.js +0 -63
  351. package/src/mesh/builders/cone.js +0 -29
  352. package/src/mesh/builders/cuboid.js +0 -135
  353. package/src/mesh/builders/cylinder.js +0 -131
  354. package/src/mesh/builders/dodecahedron.js +0 -130
  355. package/src/mesh/builders/ellipsoid.js +0 -65
  356. package/src/mesh/builders/extrude.js +0 -136
  357. package/src/mesh/builders/frustum.js +0 -26
  358. package/src/mesh/builders/geometry.js +0 -121
  359. package/src/mesh/builders/helix.js +0 -35
  360. package/src/mesh/builders/icosahedron.js +0 -61
  361. package/src/mesh/builders/icosphere.js +0 -141
  362. package/src/mesh/builders/index.js +0 -28
  363. package/src/mesh/builders/lathe.js +0 -85
  364. package/src/mesh/builders/octahedron.js +0 -43
  365. package/src/mesh/builders/plane.js +0 -73
  366. package/src/mesh/builders/polyhedron.js +0 -121
  367. package/src/mesh/builders/prism.js +0 -19
  368. package/src/mesh/builders/pyramid.js +0 -90
  369. package/src/mesh/builders/ring.js +0 -109
  370. package/src/mesh/builders/surfaces.js +0 -69
  371. package/src/mesh/builders/terrain.js +0 -80
  372. package/src/mesh/builders/tetrahedron.js +0 -37
  373. package/src/mesh/builders/torusknot.js +0 -36
  374. package/src/mesh/builders/tube.js +0 -209
  375. package/src/mesh/builders/utils.js +0 -148
  376. package/src/mesh/builders/uvsphere.js +0 -80
  377. package/src/mesh/builders/wedge.js +0 -22
  378. package/src/mesh/builders/wireframe.js +0 -62
  379. package/src/mesh/index.js +0 -4
  380. package/src/mesh/mesh.js +0 -157
  381. package/src/objects/bone.js +0 -17
  382. package/src/objects/camera/camera.js +0 -159
  383. package/src/objects/camera/index.js +0 -2
  384. package/src/objects/camera/projection.js +0 -203
  385. package/src/objects/debug/index.js +0 -1
  386. package/src/objects/debug/skeleton.js +0 -28
  387. package/src/objects/index.js +0 -7
  388. package/src/objects/light/ambient.js +0 -41
  389. package/src/objects/light/directional.js +0 -51
  390. package/src/objects/light/index.js +0 -5
  391. package/src/objects/light/point.js +0 -56
  392. package/src/objects/light/shadow/index.js +0 -1
  393. package/src/objects/light/shadow/shadow.js +0 -67
  394. package/src/objects/light/spot.js +0 -82
  395. package/src/objects/mesh.js +0 -89
  396. package/src/objects/object3d.js +0 -167
  397. package/src/objects/skybox.js +0 -38
  398. package/src/plugins/camera/RenderTarget2DPool.js +0 -94
  399. package/src/plugins/camera/camera.js +0 -27
  400. package/src/plugins/camera/index.js +0 -4
  401. package/src/plugins/camera/nodes/canvasblit.js +0 -153
  402. package/src/plugins/camera/nodes/index.js +0 -5
  403. package/src/plugins/camera/nodes/opaquepass.js +0 -94
  404. package/src/plugins/camera/nodes/tonemapping.js +0 -217
  405. package/src/plugins/camera/nodes/transparentpass.js +0 -87
  406. package/src/plugins/camera/nodes/view.js +0 -72
  407. package/src/plugins/camera/resources/cameracolortargets.js +0 -66
  408. package/src/plugins/camera/resources/canvasblitpipeline.js +0 -6
  409. package/src/plugins/camera/resources/index.js +0 -3
  410. package/src/plugins/camera/resources/tonemappingpipeline.js +0 -6
  411. package/src/plugins/index.js +0 -6
  412. package/src/plugins/light/index.js +0 -2
  413. package/src/plugins/light/light.js +0 -19
  414. package/src/plugins/light/nodes/index.js +0 -1
  415. package/src/plugins/light/nodes/light.js +0 -127
  416. package/src/plugins/meshmaterial/index.js +0 -3
  417. package/src/plugins/meshmaterial/meshmaterial.js +0 -644
  418. package/src/plugins/meshmaterial/nodes/index.js +0 -1
  419. package/src/plugins/meshmaterial/nodes/meshmaterial.js +0 -83
  420. package/src/plugins/meshmaterial/resources/bonetexture.js +0 -176
  421. package/src/plugins/meshmaterial/resources/environmentmap.js +0 -44
  422. package/src/plugins/meshmaterial/resources/index.js +0 -5
  423. package/src/plugins/meshmaterial/resources/materialuniformbuffers.js +0 -94
  424. package/src/plugins/meshmaterial/resources/meshmaterialbindgroups.js +0 -40
  425. package/src/plugins/meshmaterial/resources/meshmaterialpipelines.js +0 -50
  426. package/src/plugins/shadow/index.js +0 -3
  427. package/src/plugins/shadow/nodes/index.js +0 -3
  428. package/src/plugins/shadow/nodes/occluder.js +0 -106
  429. package/src/plugins/shadow/nodes/opaquePass.js +0 -110
  430. package/src/plugins/shadow/nodes/view.js +0 -272
  431. package/src/plugins/shadow/resources/ShadowMap.js +0 -99
  432. package/src/plugins/shadow/resources/index.js +0 -2
  433. package/src/plugins/shadow/resources/shadowpipelines.js +0 -25
  434. package/src/plugins/shadow/shadow.js +0 -26
  435. package/src/plugins/skeletonhelper/index.js +0 -1
  436. package/src/plugins/skeletonhelper/skeletonhelper.js +0 -202
  437. package/src/plugins/skybox/index.js +0 -3
  438. package/src/plugins/skybox/nodes/index.js +0 -1
  439. package/src/plugins/skybox/nodes/skybox.js +0 -335
  440. package/src/plugins/skybox/resources/index.js +0 -2
  441. package/src/plugins/skybox/resources/skyboxmesh.js +0 -14
  442. package/src/plugins/skybox/resources/skyboxpipeline.js +0 -16
  443. package/src/plugins/skybox/skybox.js +0 -18
  444. package/src/renderer/core/index.js +0 -293
  445. package/src/renderer/graph/index.js +0 -3
  446. package/src/renderer/graph/nodes.js +0 -34
  447. package/src/renderer/graph/rendergraph.js +0 -182
  448. package/src/renderer/index.js +0 -5
  449. package/src/renderer/plugin.js +0 -16
  450. package/src/renderer/renderer.js +0 -195
  451. package/src/renderer/views.js +0 -28
  452. package/src/rendertarget/canvastarget.js +0 -33
  453. package/src/rendertarget/image.js +0 -132
  454. package/src/rendertarget/index.js +0 -3
  455. package/src/rendertarget/rendertarget.js +0 -89
  456. package/src/shader/basicFragment.glsl +0 -38
  457. package/src/shader/basicVertex.glsl +0 -98
  458. package/src/shader/blitFragment.glsl +0 -9
  459. package/src/shader/common/color.glsl +0 -7
  460. package/src/shader/common/common.glsl +0 -54
  461. package/src/shader/common/index.js +0 -5
  462. package/src/shader/common/light.glsl +0 -437
  463. package/src/shader/common/math.glsl +0 -12
  464. package/src/shader/common/tonemap.glsl +0 -118
  465. package/src/shader/debug/index.js +0 -2
  466. package/src/shader/debug/skeletonFragment.glsl +0 -6
  467. package/src/shader/debug/skeletonVertex.glsl +0 -25
  468. package/src/shader/depthFragment.glsl +0 -34
  469. package/src/shader/fullscreenVertex.glsl +0 -26
  470. package/src/shader/index.js +0 -14
  471. package/src/shader/lambertFragment.glsl +0 -134
  472. package/src/shader/normalFragment.glsl +0 -22
  473. package/src/shader/phongFragment.glsl +0 -148
  474. package/src/shader/skyboxFragment.glsl +0 -16
  475. package/src/shader/skyboxVertex.glsl +0 -21
  476. package/src/shader/standardFragment.glsl +0 -331
  477. package/src/shader/tonemappingFragment.glsl +0 -37
  478. package/src/texture/index.js +0 -2
  479. package/src/texture/sampler.js +0 -191
  480. package/src/texture/texture.js +0 -438
  481. package/src/utils/index.js +0 -129
  482. package/src/utils/rendergraph_gui.js +0 -583
  483. package/tsconfig.json +0 -11
@@ -1,2722 +0,0 @@
1
- /**@import { LoadSettings } from './loader.js' */
2
- import { Attribute, Mesh } from '../mesh/index.js';
3
- import { StandardMaterial, AlphaMaskMode, OpaqueMode, TransparentMode } from '../material/index.js';
4
- import { DirectionalLight, MeshMaterial3D, Object3D, PointLight, Skin, SpotLight } from '../objects/index.js';
5
- import { Loader } from './loader.js';
6
- import { arrayBufferToJSON } from './utils.js';
7
- import { Bone3D } from '../objects/bone.js';
8
- import { Affine3 } from '../math/index.js';
9
- import { SeparateAttributeData } from '../mesh/attributedata/separate.js';
10
- import { TextureLoader } from './texture.js';
11
- import { Texture, Sampler } from '../texture/index.js';
12
- import { assert } from '../utils/index.js';
13
- import { CullFace, TextureFilter, TextureType, TextureWrap } from '../constants/index.js';
14
-
15
- const defaultMaterial = new StandardMaterial()
16
- const GLB_MAGIC = 0x46546C67
17
- const GLB_CHUNK_TYPE_JSON = 0x4E4F534A
18
- const GLB_CHUNK_TYPE_BIN = 0x004E4942
19
- const GLTF_LIGHTS_PUNCTUAL_EXTENSION = "KHR_lights_punctual"
20
- // Keep the fallback finite so attenuation and shadow math remain stable.
21
- const DEFAULT_GLTF_LIGHT_RANGE = 1000
22
- /**
23
- * @extends {Loader<Object3D, GLTFLoadSettings>}
24
- */
25
- export class GLTFLoader extends Loader {
26
- textureLoader
27
- constructor({ textureLoader = new TextureLoader() } = {}) {
28
- super(Object3D)
29
- this.textureLoader = textureLoader
30
- }
31
- /**
32
- * @override
33
- * @param {ArrayBuffer[]} buffers
34
- * @param {Object3D} destination
35
- * @param {GLTFLoadSettings} settings
36
- */
37
- async parse(buffers, destination, settings) {
38
- const buffer = buffers[0]
39
- const path = settings.paths[0]
40
- if (!buffer || !path) {
41
- return
42
- }
43
-
44
- /**@type {Map<number, Object3D>} */
45
- const entityMap = new Map()
46
- /** @type {Map<number, DirectionalLight | PointLight | SpotLight>} */
47
- const lightCache = new Map()
48
- const baseUrl = new URL(path, location.href).href
49
- const gltf = await loadGLTF(buffer, baseUrl)
50
- const scene = gltf.scenes[gltf.scene]
51
-
52
- if (!scene) {
53
- throw "No root scene defined"
54
- }
55
-
56
- const images = await Promise.all(gltf.images.map(async (gltfTexture) => {
57
- if (gltfTexture.uri) {
58
- return this.textureLoader.asyncLoad({
59
- paths: [new URL(gltfTexture.uri, baseUrl).href]
60
- })
61
- }
62
-
63
- if (typeof gltfTexture.bufferView === "number") {
64
- const bufferView = gltf.bufferViews[gltfTexture.bufferView]
65
- assert(bufferView, "GLTF image does not have a valid buffer view")
66
-
67
- const imageBuffer = gltf.buffers[bufferView.buffer]
68
- assert(imageBuffer, "GLTF image buffer view does not have a valid buffer")
69
-
70
- const texture = new Texture({
71
- type: TextureType.Texture2D
72
- })
73
- const slice = imageBuffer.slice(
74
- bufferView.offset,
75
- bufferView.offset + bufferView.length
76
- )
77
- await this.textureLoader.parse([slice], texture, {
78
- mimeType: gltfTexture.mimeType
79
- })
80
- return texture
81
- }
82
-
83
- throw "Unsupported gltf image setting"
84
- }))
85
- const samplers = gltf.samplers.map((gltfSampler) => createGLTFSampler(gltfSampler))
86
-
87
- /**
88
- * @type {[Texture, Sampler][]}
89
- */
90
- const textures = gltf.textures.map((gltfTextures) => {
91
- const image = images[gltfTextures.source]
92
- assert(image, "GLTF texture does not have an image source")
93
-
94
- if (gltfTextures.sampler !== undefined) {
95
- const sampler = samplers[gltfTextures.sampler]
96
- assert(sampler, "GLTF texture does not have a valid sampler")
97
- return [image, sampler]
98
- }
99
- return [image, createDefaultGLTFSampler()]
100
- })
101
- const materials = gltf.materials.map((gltfMaterial) => {
102
- const {
103
- emissiveFactor,
104
- emissiveTexture,
105
- extensions = {},
106
- normalTexture,
107
- occlusionTexture,
108
- pbrMetallicRoughness
109
- } = gltfMaterial
110
- const {
111
- baseColorFactor,
112
- baseColorTexture,
113
- metallicFactor,
114
- roughnessFactor,
115
- metallicRoughnessTexture
116
- } = pbrMetallicRoughness
117
- const material = new StandardMaterial()
118
-
119
- material.color.set(
120
- baseColorFactor[0],
121
- baseColorFactor[1],
122
- baseColorFactor[2],
123
- baseColorFactor[3]
124
- )
125
- material.emissiveColor.set(
126
- emissiveFactor[0],
127
- emissiveFactor[1],
128
- emissiveFactor[2]
129
- )
130
- material.metallic = metallicFactor
131
- material.roughness = roughnessFactor
132
-
133
- if (baseColorTexture) {
134
- const texture = textures[baseColorTexture.index]
135
-
136
- if (texture) {
137
- material.mainTexture = texture[0]
138
- material.mainSampler = texture[1]
139
- } else {
140
- console.warn("gltf: invalid color texture on material");
141
- }
142
- }
143
-
144
- if (normalTexture) {
145
- const texture = textures[normalTexture.index]
146
-
147
- if (texture) {
148
- material.normalTexture = texture[0]
149
- material.normalSampler = texture[1]
150
- } else {
151
- console.warn("gltf: invalid normal texture on material");
152
- }
153
- }
154
-
155
- if (occlusionTexture) {
156
- const texture = textures[occlusionTexture.index]
157
-
158
- if (texture) {
159
- material.occlusionStrength = occlusionTexture.strength
160
- material.occlusionTexture = texture[0]
161
- material.occlusionSampler = texture[1]
162
- } else {
163
- console.warn("gltf: invalid occlusion texture on material");
164
- }
165
- }
166
-
167
- if (emissiveTexture) {
168
- const texture = textures[emissiveTexture.index]
169
-
170
- if (texture) {
171
- material.emissiveTexture = texture[0]
172
- material.emissiveSampler = texture[1]
173
- } else {
174
- console.warn("gltf: invalid emissive texture on material");
175
- }
176
- }
177
-
178
- if (metallicRoughnessTexture) {
179
- const texture = textures[metallicRoughnessTexture.index]
180
-
181
- if (texture) {
182
- material.metallicTexture = texture[0]
183
- material.metallicSampler = texture[1]
184
- material.roughnessTexture = texture[0]
185
- material.roughnessSampler = texture[1]
186
- } else {
187
- console.warn("gltf: invalid metallic-rougness texture on material");
188
- }
189
- }
190
-
191
- const transmissionExtension = extensions["KHR_materials_transmission"]
192
- if (transmissionExtension) {
193
- material.transmission = transmissionExtension.transmissionFactor ?? material.transmission
194
-
195
- const transmissionTexture = transmissionExtension.transmissionTexture
196
- if (transmissionTexture) {
197
- const texture = textures[transmissionTexture.index]
198
-
199
- if (texture) {
200
- material.transmissionTexture = texture[0]
201
- material.transmissionSampler = texture[1]
202
- } else {
203
- console.warn("gltf: invalid transmission texture on material");
204
- }
205
- }
206
- }
207
-
208
- const volumeExtension = extensions["KHR_materials_volume"]
209
- if (volumeExtension) {
210
- material.thickness = volumeExtension.thicknessFactor ?? material.thickness
211
-
212
- const thicknessTexture = volumeExtension.thicknessTexture
213
- if (thicknessTexture) {
214
- const texture = textures[thicknessTexture.index]
215
-
216
- if (texture) {
217
- material.thicknessTexture = texture[0]
218
- material.thicknessSampler = texture[1]
219
- } else {
220
- console.warn("gltf: invalid thickness texture on material");
221
- }
222
- }
223
- }
224
-
225
- const iorExtension = extensions["KHR_materials_ior"]
226
- if (iorExtension) {
227
- material.ior = iorExtension.ior ?? material.ior
228
- }
229
-
230
- if (gltfMaterial.alphaMode === GLFTAlphaMode.Mask) {
231
- material.alphaBlend = new AlphaMaskMode(gltfMaterial.alphaCutoff)
232
- } else if (gltfMaterial.alphaMode === GLFTAlphaMode.Blend) {
233
- material.alphaBlend = new TransparentMode()
234
- } else {
235
- material.alphaBlend = new OpaqueMode()
236
- }
237
-
238
- if (gltfMaterial.doubleSided) {
239
- material.cullFace = CullFace.None
240
- }
241
-
242
- return material
243
- })
244
- const geometries = gltf.meshes.map((data) => {
245
- return parseGeometry(data, gltf)
246
- })
247
-
248
- gltf.nodes.forEach((node, index) => {
249
- const object = parseObject(index, node, gltf, geometries, materials, lightCache)
250
-
251
- if (object) {
252
- entityMap.set(index, object)
253
- }
254
- })
255
-
256
- gltf.nodes.forEach((node, index) => {
257
- const parent = entityMap.get(index)
258
-
259
- if (!parent) {
260
- return
261
- }
262
-
263
- for (const child of node.children) {
264
- const childEntity = entityMap.get(child)
265
-
266
- if (childEntity) {
267
- parent.add(childEntity)
268
- }
269
- }
270
- })
271
-
272
- scene.nodes.forEach((node) => {
273
- entityMap.get(node)?.update()
274
- });
275
-
276
- const skins = gltf.skins.map((skin) => {
277
- return parseSkin(skin, gltf, entityMap)
278
- })
279
-
280
- entityMap.forEach((entity, index) => {
281
- const node = /**@type {GLTFNode} */ (gltf.nodes[index])
282
- if (node.skin !== undefined) {
283
- entity.traverseBFS((mesh) => {
284
- if (mesh instanceof MeshMaterial3D) {
285
- mesh.skin = skins[/**@type {number} */ (node.skin)]
286
- }
287
- return true
288
- })
289
- }
290
- })
291
-
292
- const sceneEntities = scene.nodes.map((node) => {
293
- return /**@type {Object3D} */ (entityMap.get(node))
294
- })
295
-
296
- sceneEntities.forEach((object) => {
297
- object.traverseDFS((innerObject) => {
298
- innerObject.update()
299
- return true
300
- })
301
- })
302
-
303
-
304
- destination.add(...sceneEntities)
305
- }
306
-
307
- /**
308
- * @override
309
- */
310
- default() {
311
- return new Object3D()
312
- }
313
- }
314
-
315
- /**
316
- * @param {ArrayBuffer} data
317
- * @param {string} baseUrl
318
- */
319
- async function loadGLTF(data, baseUrl) {
320
- const { json, buffers: embeddedBuffers } = parseGLTFAsset(data)
321
- const buffers = json.buffers instanceof Array
322
- ? await loadBuffers(baseUrl, json.buffers, embeddedBuffers)
323
- : embeddedBuffers
324
- const gltf = GLTF.deserialize(json)
325
- gltf.buffers = buffers
326
-
327
- return gltf
328
- }
329
-
330
- /**
331
- * @param {string} base
332
- * @param {{uri?: string; byteLength?: number;}[]} uris
333
- * @param {ArrayBuffer[]} [embeddedBuffers]
334
- */
335
- async function loadBuffers(base, uris, embeddedBuffers = []) {
336
- if (uris.length === 0) {
337
- return embeddedBuffers.slice()
338
- }
339
-
340
- return Promise.all(
341
- uris.map(async (buffer, index) => {
342
- if (typeof buffer.uri !== "string") {
343
- const embeddedBuffer = embeddedBuffers[index]
344
- if (embeddedBuffer !== undefined) {
345
- if (
346
- typeof buffer.byteLength === "number" &&
347
- embeddedBuffer.byteLength < buffer.byteLength
348
- ) {
349
- throw new Error(`Embedded buffer ${index} is shorter than declared byteLength`)
350
- }
351
- return embeddedBuffer
352
- }
353
- throw new Error(`Missing URI for buffer ${index}`)
354
- }
355
-
356
- const url = buffer.uri.startsWith('data') ?
357
- buffer.uri :
358
- new URL(buffer.uri, base).href
359
- const response = await fetch(url);
360
- if (!response.ok) throw new Error(`Failed to fetch buffer`);
361
- return await response.arrayBuffer();
362
- })
363
- )
364
- }
365
-
366
- /**
367
- * @param {number} filter
368
- * @returns {TextureFilter}
369
- */
370
- function mapGLTFMagnificationFilter(filter) {
371
- switch (filter) {
372
- case TextureFilter.Nearest:
373
- return TextureFilter.Nearest
374
- case TextureFilter.Linear:
375
- return TextureFilter.Linear
376
- default:
377
- throw "GLTF: Invalid magnification sampler"
378
- }
379
- }
380
-
381
- /**
382
- * @param {number} filter
383
- * @returns {[TextureFilter, TextureFilter | undefined]}
384
- */
385
- function mapGLTFMinificationFilter(filter) {
386
- switch (filter) {
387
- case TextureFilter.Nearest:
388
- return [TextureFilter.Nearest, undefined]
389
- case TextureFilter.Linear:
390
- return [TextureFilter.Linear, undefined]
391
- case 0x2700:
392
- return [TextureFilter.Nearest, TextureFilter.Nearest]
393
- case 0x2701:
394
- return [TextureFilter.Nearest, TextureFilter.Linear]
395
- case 0x2702:
396
- return [TextureFilter.Linear, TextureFilter.Nearest]
397
- case 0x2703:
398
- return [TextureFilter.Linear, TextureFilter.Linear]
399
- default:
400
- throw "GLTF: Invalid minification sampler"
401
- }
402
- }
403
-
404
- /**
405
- * @param {number} wrap
406
- * @returns {TextureWrap}
407
- */
408
- function mapGLTFTextureWrap(wrap) {
409
- switch (wrap) {
410
- case TextureWrap.Clamp:
411
- return TextureWrap.Clamp
412
- case TextureWrap.MirrorRepeat:
413
- return TextureWrap.MirrorRepeat
414
- case TextureWrap.Repeat:
415
- return TextureWrap.Repeat
416
- default:
417
- throw "GLTF: Invalid texture wrap"
418
- }
419
- }
420
-
421
- /**
422
- * @param {GLFTSampler} [gltfSampler]
423
- * @returns {Sampler}
424
- */
425
- function createGLTFSampler(gltfSampler) {
426
- const sampler = createDefaultGLTFSampler()
427
-
428
- if (gltfSampler?.magFilter !== undefined) {
429
- sampler.magnificationFilter = mapGLTFMagnificationFilter(gltfSampler.magFilter)
430
- }
431
-
432
- if (gltfSampler?.minFilter !== undefined) {
433
- const [minificationFilter, mipmapFilter] = mapGLTFMinificationFilter(gltfSampler.minFilter)
434
- sampler.minificationFilter = minificationFilter
435
- sampler.mipmapFilter = mipmapFilter
436
- }
437
-
438
- if (gltfSampler?.wrapS !== undefined) {
439
- sampler.wrapS = mapGLTFTextureWrap(gltfSampler.wrapS)
440
- }
441
-
442
- if (gltfSampler?.wrapT !== undefined) {
443
- sampler.wrapT = mapGLTFTextureWrap(gltfSampler.wrapT)
444
- }
445
-
446
- return sampler
447
- }
448
-
449
- /**
450
- * @returns {Sampler}
451
- */
452
- function createDefaultGLTFSampler() {
453
- return new Sampler({
454
- magnificationFilter: TextureFilter.Linear,
455
- minificationFilter: TextureFilter.Linear,
456
- mipmapFilter: undefined,
457
- wrapS: TextureWrap.Repeat,
458
- wrapT: TextureWrap.Repeat,
459
- wrapR: TextureWrap.Repeat
460
- })
461
- }
462
-
463
- /**
464
- * @param {ArrayBuffer} data
465
- * @returns {{ json: any, buffers: ArrayBuffer[] }}
466
- */
467
- function parseGLTFAsset(data) {
468
- if (isGLB(data)) {
469
- return parseGLB(data)
470
- }
471
-
472
- return {
473
- json: arrayBufferToJSON(data),
474
- buffers: []
475
- }
476
- }
477
-
478
- /**
479
- * @param {ArrayBuffer} data
480
- * @returns {boolean}
481
- */
482
- function isGLB(data) {
483
- if (data.byteLength < 12) {
484
- return false
485
- }
486
-
487
- const view = new DataView(data)
488
- return view.getUint32(0, true) === GLB_MAGIC
489
- }
490
-
491
- /**
492
- * @param {ArrayBuffer} data
493
- * @returns {{ json: any, buffers: ArrayBuffer[] }}
494
- */
495
- function parseGLB(data) {
496
- const view = new DataView(data)
497
- if (view.byteLength < 12) {
498
- throw new Error("Invalid GLB file")
499
- }
500
-
501
- const magic = view.getUint32(0, true)
502
- const version = view.getUint32(4, true)
503
- const length = view.getUint32(8, true)
504
-
505
- if (magic !== GLB_MAGIC) {
506
- throw new Error("Invalid GLB magic")
507
- }
508
-
509
- if (version !== 2) {
510
- throw new Error(`Unsupported GLB version: ${version}`)
511
- }
512
-
513
- if (length !== data.byteLength) {
514
- throw new Error("GLB length does not match file size")
515
- }
516
-
517
- const decoder = new TextDecoder("utf-8")
518
- let offset = 12
519
- let json = undefined
520
- /** @type {ArrayBuffer | undefined} */
521
- let binBuffer
522
- let sawJsonChunk = false
523
-
524
- while (offset + 8 <= data.byteLength) {
525
- const chunkStart = offset
526
- const chunkLength = view.getUint32(chunkStart, true)
527
- const chunkType = view.getUint32(chunkStart + 4, true)
528
- offset = chunkStart + 8
529
-
530
- if (offset + chunkLength > data.byteLength) {
531
- throw new Error("Invalid GLB chunk length")
532
- }
533
-
534
- const chunk = data.slice(offset, offset + chunkLength)
535
- offset += chunkLength
536
-
537
- if (chunkType === GLB_CHUNK_TYPE_JSON) {
538
- if (sawJsonChunk) {
539
- throw new Error("GLB file contains multiple JSON chunks")
540
- }
541
- if (chunkStart !== 12) {
542
- throw new Error("GLB JSON chunk must be the first chunk")
543
- }
544
- sawJsonChunk = true
545
- json = JSON.parse(decoder.decode(chunk))
546
- } else if (chunkType === GLB_CHUNK_TYPE_BIN) {
547
- binBuffer = chunk
548
- }
549
- }
550
-
551
- if (offset !== data.byteLength) {
552
- throw new Error("GLB file has trailing or truncated chunk data")
553
- }
554
-
555
- if (json === undefined) {
556
- throw new Error("GLB file is missing a JSON chunk")
557
- }
558
-
559
- return {
560
- json,
561
- buffers: binBuffer !== undefined ? [binBuffer] : []
562
- }
563
- }
564
- /**
565
- * @typedef {LoadSettings} GLTFLoadSettings
566
- */
567
-
568
- class GLTF {
569
- /**
570
- * @type {number}
571
- */
572
- scene = 0
573
- /**
574
- * @type {GLTFScene[]}
575
- */
576
- scenes = []
577
- /**
578
- * @type {GLTFNode[]}
579
- */
580
- nodes = []
581
- /**
582
- * @type {GLTFImage[]}
583
- */
584
- images = []
585
- /**
586
- * @type {GLFTSampler[]}
587
- */
588
- samplers = []
589
- /**
590
- * @type {GLFTTexture[]}
591
- */
592
- textures = []
593
- /**
594
- * @type {GLTFMesh[]}
595
- */
596
- meshes = []
597
- /**
598
- * @type {GLTFMaterial[]}
599
- */
600
- materials = []
601
- /**
602
- * @type {GLTFSkin[]}
603
- */
604
- skins = []
605
- /**
606
- * @type {ArrayBuffer[]}
607
- */
608
- buffers = []
609
- /**
610
- * @type {GLTFBufferView[]}
611
- */
612
- bufferViews = []
613
- /**
614
- * @type {GLTFAccessor[]}
615
- */
616
- accessors = []
617
- /**
618
- * @type {GLTFMetaData}
619
- */
620
- metaData
621
- /**
622
- * @type {Record<string, any>}
623
- */
624
- extensions = {}
625
- /**
626
- * @type {string[]}
627
- */
628
- extensionsUsed = []
629
- /**
630
- * @type {string[]}
631
- */
632
- extensionsRequired = []
633
-
634
- /**
635
- * @param {GLTFMetaData} meta
636
- */
637
- constructor(meta) {
638
- this.metaData = meta
639
- }
640
- /**
641
- * @param {any} data
642
- */
643
- static deserialize(data) {
644
- const {
645
- scene,
646
- scenes,
647
- nodes,
648
- meshes,
649
- images,
650
- textures,
651
- samplers,
652
- materials,
653
- bufferViews,
654
- accessors,
655
- asset,
656
- skins,
657
- extensions,
658
- extensionsUsed,
659
- extensionsRequired
660
- } = data
661
-
662
- if (
663
- !(asset instanceof Object) ||
664
- !(scenes instanceof Array) ||
665
- !(nodes instanceof Array) ||
666
- !(meshes instanceof Array) ||
667
- !(bufferViews instanceof Array) ||
668
- !(accessors instanceof Array)
669
- ) {
670
- throw new Error("Invalid gltf json")
671
- }
672
- const gltf = new GLTF(GLTFMetaData.deserialize(asset))
673
-
674
- if (typeof scene === "number") {
675
- gltf.scene = scene
676
- } else {
677
- gltf.scene = 0
678
- }
679
-
680
- gltf.scenes = scenes.map((/**@type {any}*/d) => GLTFScene.deserialize(d))
681
- gltf.nodes = nodes.map((/**@type {any}*/d) => GLTFNode.deserialize(d))
682
- gltf.meshes = meshes.map((/**@type {any}*/d) => GLTFMesh.deserialize(d))
683
- gltf.bufferViews = bufferViews.map((/**@type {any}*/d) => GLTFBufferView.deserialize(d))
684
- gltf.accessors = accessors.map((/**@type {any}*/d) => GLTFAccessor.deserialize(d))
685
-
686
- if (materials instanceof Array) {
687
- gltf.materials = materials.map((/**@type {any}*/d) => GLTFMaterial.deserialize(d))
688
- }
689
-
690
- if (images instanceof Array) {
691
- gltf.images = images.map((/**@type {any}*/d) => GLTFImage.deserialize(d))
692
- }
693
-
694
- if (samplers instanceof Array) {
695
- gltf.samplers = samplers.map((/**@type {any}*/d) => GLFTSampler.deserialize(d))
696
- }
697
-
698
- if (textures instanceof Array) {
699
- gltf.textures = textures.map((/**@type {any}*/d) => GLFTTexture.deserialize(d))
700
- }
701
-
702
- if (skins instanceof Array) {
703
- gltf.skins = skins.map(a => GLTFSkin.deserialize(a))
704
- } else {
705
- gltf.skins = []
706
- }
707
-
708
- if (extensions instanceof Object) {
709
- gltf.extensions = extensions
710
- } else {
711
- gltf.extensions = {}
712
- }
713
-
714
- if (extensionsUsed instanceof Array) {
715
- gltf.extensionsUsed = extensionsUsed.filter((value) => typeof value === "string")
716
- } else {
717
- gltf.extensionsUsed = []
718
- }
719
-
720
- if (extensionsRequired instanceof Array) {
721
- gltf.extensionsRequired = extensionsRequired.filter((value) => typeof value === "string")
722
- } else {
723
- gltf.extensionsRequired = []
724
- }
725
- return gltf
726
- }
727
- }
728
-
729
- class GLTFScene {
730
- /**
731
- * @type {string}
732
- */
733
- name = ''
734
- /**
735
- * @type {Record<string,any>}
736
- */
737
- extensions = {}
738
- /**
739
- * @type {Record<string,any>}
740
- */
741
- extras = []
742
- /**
743
- * @type {number[]}
744
- */
745
- nodes = []
746
-
747
- /**
748
- * @param {any} data
749
- */
750
- static deserialize(data) {
751
- const { nodes, name, extensions, extras } = data
752
- const scene = new GLTFScene()
753
-
754
- if (nodes instanceof Array) {
755
- scene.nodes = nodes
756
- .filter((node) => typeof node == "number")
757
- }
758
-
759
- if (typeof name === "string") {
760
- scene.name = name
761
- } else {
762
- scene.name = ''
763
- }
764
- if (extensions instanceof Object) {
765
- scene.extensions = extensions
766
- } else {
767
- scene.extensions = {}
768
- }
769
- if (extras instanceof Object) {
770
- scene.extras = extras
771
- } else {
772
- scene.extras = {}
773
- }
774
- return scene
775
- }
776
- }
777
-
778
- class GLTFNode {
779
- /**
780
- * @type {string}
781
- */
782
- name = ''
783
- /**
784
- * @type {Record<string,any>}
785
- */
786
- extensions = {}
787
- /**
788
- * @type {Record<string,any>}
789
- */
790
- extras = []
791
- /**
792
- * @type {number | undefined}
793
- */
794
- mesh
795
- /**
796
- * @type {number | undefined}
797
- */
798
- skin
799
-
800
- /**
801
- * @type {number | undefined}
802
- */
803
- camera
804
- /**
805
- * @type {number[] | undefined}
806
- */
807
- weights
808
- /**
809
- * @type {number[]}
810
- */
811
- children = []
812
- /**
813
- * @type {TRSTransform | MatrixTransform | undefined}
814
- */
815
- transform
816
- /**
817
- * @param {any} data
818
- */
819
- static deserialize(data) {
820
- const {
821
- mesh, matrix, translation,
822
- rotation, scale, weights,
823
- children, skin, camera,
824
- name, extensions, extras
825
- } = data
826
- const node = new GLTFNode()
827
-
828
- if (typeof mesh === "number") {
829
- node.mesh = mesh
830
- }
831
- if (typeof skin === 'number') {
832
- node.skin = skin
833
- }
834
- if (typeof camera === 'number') {
835
- node.camera = camera
836
- }
837
- if (weights instanceof Array) {
838
- node.weights = weights.filter(w => typeof w === "number")
839
- }
840
- if (matrix) {
841
- node.transform = MatrixTransform.deserialize(matrix)
842
- }
843
- if (translation || rotation || scale) {
844
- const ntranslation = translation || [0, 0, 0]
845
- const nrotation = rotation || [0, 0, 0, 1]
846
- const nscale = scale || [1, 1, 1]
847
- node.transform = TRSTransform.deserialize(
848
- ntranslation,
849
- nrotation,
850
- nscale
851
- )
852
- } else if (matrix) {
853
- node.transform = MatrixTransform.deserialize(matrix)
854
- } else {
855
- node.transform = new TRSTransform()
856
- }
857
-
858
- if (children instanceof Array) {
859
- node.children = children.filter(c => typeof c === "number")
860
- } else {
861
- node.children = []
862
- }
863
-
864
- if (typeof name === 'string') {
865
- node.name = name
866
- } else {
867
- node.name = ""
868
- }
869
- if (extensions instanceof Object) {
870
- node.extensions = extensions
871
- } else {
872
- node.extensions = {}
873
- }
874
- if (extras instanceof Object) {
875
- node.extras = extras
876
- } else {
877
- node.extras = {}
878
- }
879
- return node
880
- }
881
- }
882
-
883
- class GLTFMesh {
884
- /**
885
- * @type {string}
886
- */
887
- name = ''
888
- /**
889
- * @type {Record<string,any>}
890
- */
891
- extensions = {}
892
- /**
893
- * @type {Record<string,any>}
894
- */
895
- extras = {}
896
-
897
- /**
898
- * @type {GLTFPrimitive[]}
899
- */
900
- primitives = []
901
- /**
902
- * @type {number[]}
903
- */
904
- weights = []
905
- /**
906
- * @param {any} data
907
- */
908
- static deserialize(data) {
909
- const { primitives, weights, name, extensions, extras } = data
910
- const mesh = new GLTFMesh()
911
-
912
- if (primitives instanceof Array) {
913
- mesh.primitives = primitives.map((p) => GLTFPrimitive.deserialize(p))
914
- } else {
915
- mesh.primitives = []
916
- }
917
-
918
- if (weights instanceof Array) {
919
- mesh.weights = weights.filter(weight => typeof weight === "number")
920
- } else {
921
- mesh.weights = []
922
- }
923
-
924
- if (typeof name === "string") {
925
- mesh.name = name
926
- } else {
927
- mesh.name = ''
928
- }
929
- if (extensions instanceof Object) {
930
- mesh.extensions = extensions
931
- } else {
932
- mesh.extensions = {}
933
- }
934
- if (extras instanceof Object) {
935
- mesh.extras = extras
936
- } else {
937
- mesh.extras = {}
938
- }
939
- return mesh
940
- }
941
- }
942
-
943
- class GLTFMaterial {
944
- /**
945
- * @type {string}
946
- */
947
- name = ''
948
- /**
949
- * @type {Record<string,any>}
950
- */
951
- extensions = {}
952
- /**
953
- * @type {Record<string,any>}
954
- */
955
- extras = {}
956
-
957
- /**
958
- * @type {GLTFPBRetallicRoughness}
959
- */
960
- pbrMetallicRoughness
961
-
962
- /**
963
- * @type {GLFTTextureInfo | undefined}
964
- */
965
- normalTexture
966
-
967
- /**
968
- * @type {GLFTTextureInfo | undefined}
969
- */
970
- occlusionTexture
971
-
972
- /**
973
- * @type {GLFTTextureInfo | undefined}
974
- */
975
- emissiveTexture
976
-
977
- /**
978
- * @type {[number, number, number]}
979
- */
980
- emissiveFactor = [0, 0, 0]
981
-
982
- /**
983
- * @type {GLFTAlphaMode}
984
- */
985
- alphaMode = GLFTAlphaMode.Opaque
986
-
987
- /**
988
- * @type {number}
989
- */
990
- alphaCutoff = 0.5
991
-
992
- /**
993
- * @type {boolean}
994
- */
995
- doubleSided = false
996
-
997
- /**
998
- * @param {GLTFPBRetallicRoughness} metallicRoughness
999
- */
1000
- constructor(metallicRoughness) {
1001
- this.pbrMetallicRoughness = metallicRoughness
1002
- }
1003
- /**
1004
- * @param {any} data
1005
- */
1006
- static deserialize(data) {
1007
- const {
1008
- pbrMetallicRoughness,
1009
- normalTexture,
1010
- occlusionTexture,
1011
- emissiveTexture,
1012
- emissiveFactor,
1013
- alphaMode,
1014
- alphaCutoff,
1015
- doubleSided,
1016
- name,
1017
- extensions,
1018
- extras
1019
- } = data
1020
- const pbr = pbrMetallicRoughness ? GLTFPBRetallicRoughness.deserialize(pbrMetallicRoughness) : new GLTFPBRetallicRoughness()
1021
- const result = new GLTFMaterial(pbr)
1022
-
1023
- if (normalTexture instanceof Object) {
1024
- result.normalTexture = GLFTTextureInfo.deserialize(normalTexture)
1025
- }
1026
-
1027
- if (occlusionTexture instanceof Object) {
1028
- result.occlusionTexture = GLFTTextureInfo.deserialize(occlusionTexture)
1029
- }
1030
-
1031
- if (emissiveTexture instanceof Object) {
1032
- result.emissiveTexture = GLFTTextureInfo.deserialize(emissiveTexture)
1033
- }
1034
-
1035
- if (emissiveFactor instanceof Array && emissiveFactor.length === 3) {
1036
- result.emissiveFactor = /**@type {[number, number, number]}*/(emissiveFactor)
1037
- }
1038
-
1039
- if (typeof alphaCutoff === 'number') {
1040
- result.alphaCutoff = alphaCutoff
1041
- }
1042
-
1043
- if (typeof alphaMode === 'string') {
1044
- result.alphaMode = alphaMode
1045
- }
1046
-
1047
- if (typeof doubleSided === 'boolean') {
1048
- result.doubleSided = doubleSided
1049
- }
1050
-
1051
- if (typeof name === "string") {
1052
- result.name = name
1053
- } else {
1054
- result.name = ''
1055
- }
1056
-
1057
- if (extensions instanceof Object) {
1058
- result.extensions = extensions
1059
- } else {
1060
- result.extensions = {}
1061
- }
1062
-
1063
- if (extras instanceof Object) {
1064
- result.extras = extras
1065
- } else {
1066
- result.extras = {}
1067
- }
1068
- return result
1069
- }
1070
- }
1071
-
1072
- class GLTFImage {
1073
- /**
1074
- * @type {string | undefined}
1075
- */
1076
- uri
1077
-
1078
- /**
1079
- * @type {number | undefined}
1080
- */
1081
- bufferView
1082
-
1083
- /**
1084
- * @type {string | undefined}
1085
- */
1086
- mimeType
1087
-
1088
-
1089
- /**
1090
- * @type {string}
1091
- */
1092
- name = ''
1093
- /**
1094
- * @type {Record<string,any>}
1095
- */
1096
- extensions = {}
1097
- /**
1098
- * @type {Record<string,any>}
1099
- */
1100
- extras = []
1101
-
1102
- /**
1103
- * @param {any} data
1104
- */
1105
- static deserialize(data) {
1106
- const { uri, bufferView, mimeType, name, extensions, extras } = data
1107
- const result = new GLTFImage()
1108
-
1109
- if (typeof uri === 'string') {
1110
- result.uri = uri
1111
- }
1112
-
1113
- if (typeof bufferView === 'number') {
1114
- result.bufferView = bufferView
1115
- }
1116
- if (typeof mimeType === 'string') {
1117
- result.mimeType = mimeType
1118
- }
1119
- if (typeof name === 'string') {
1120
- result.name = name
1121
- }
1122
- if (extensions instanceof Object) {
1123
- result.extensions = extensions
1124
- } else {
1125
- result.extensions = {}
1126
- }
1127
- if (extras instanceof Object) {
1128
- result.extras = extras
1129
- } else {
1130
- result.extras = {}
1131
- }
1132
-
1133
- return result
1134
- }
1135
- }
1136
-
1137
- class GLFTSampler {
1138
- /**
1139
- * @type {number | undefined}
1140
- */
1141
- magFilter
1142
- /**
1143
- * @type {number | undefined}
1144
- */
1145
- minFilter
1146
- /**
1147
- * @type {number | undefined}
1148
- */
1149
- wrapS
1150
- /**
1151
- * @type {number | undefined}
1152
- */
1153
- wrapT
1154
- /**
1155
- * @type {string}
1156
- */
1157
- name = ''
1158
- /**
1159
- * @type {Record<string,any>}
1160
- */
1161
- extensions = {}
1162
- /**
1163
- * @type {Record<string,any>}
1164
- */
1165
- extras = []
1166
- /**
1167
- * @param {any} data
1168
- */
1169
- static deserialize(data) {
1170
- const { magFilter, minFilter, wrapS, wrapT, name, extensions, extras } = data
1171
- const result = new GLFTSampler()
1172
-
1173
- if (typeof magFilter === 'number') {
1174
- result.magFilter = magFilter
1175
- }
1176
-
1177
- if (typeof minFilter === 'number') {
1178
- result.minFilter = minFilter
1179
- }
1180
-
1181
- if (typeof wrapS === 'number') {
1182
- result.wrapS = wrapS
1183
- }
1184
-
1185
- if (typeof wrapT === 'number') {
1186
- result.wrapT = wrapT
1187
- }
1188
-
1189
- if (typeof name === 'string') {
1190
- result.name = name
1191
- }
1192
-
1193
- if (extensions instanceof Object) {
1194
- result.extensions = extensions
1195
- } else {
1196
- result.extensions = {}
1197
- }
1198
- if (extras instanceof Object) {
1199
- result.extras = extras
1200
- } else {
1201
- result.extras = {}
1202
- }
1203
-
1204
- return result
1205
- }
1206
- }
1207
-
1208
- class GLFTTexture {
1209
- /**
1210
- * @type {number | undefined}
1211
- */
1212
- sampler
1213
-
1214
- /**
1215
- * @type {number}
1216
- */
1217
- source
1218
-
1219
- /**
1220
- * @type {string}
1221
- */
1222
- name = ''
1223
- /**
1224
- * @type {Record<string,any>}
1225
- */
1226
- extensions = {}
1227
- /**
1228
- * @type {Record<string,any>}
1229
- */
1230
- extras = []
1231
-
1232
- /**
1233
- * @param {number} source
1234
- */
1235
- constructor(source) {
1236
- this.source = source
1237
- }
1238
- /**
1239
- * @param {any} data
1240
- */
1241
- static deserialize(data) {
1242
- const { source, sampler, name, extensions, extras } = data
1243
-
1244
- if (typeof source !== 'number') {
1245
- throw 'No source for the texture.'
1246
- }
1247
-
1248
- const result = new GLFTTexture(source)
1249
-
1250
- if (typeof sampler === 'number') {
1251
- result.sampler = sampler
1252
- }
1253
-
1254
- if (typeof name === 'string') {
1255
- result.name = name
1256
- }
1257
-
1258
- if (extensions instanceof Object) {
1259
- result.extensions = extensions
1260
- } else {
1261
- result.extensions = {}
1262
- }
1263
-
1264
- if (extras instanceof Object) {
1265
- result.extras = extras
1266
- } else {
1267
- result.extras = {}
1268
- }
1269
-
1270
- return result
1271
- }
1272
- }
1273
-
1274
- class GLTFSkin {
1275
- /**
1276
- * @type {number | undefined}
1277
- */
1278
- inverseBindMatrices
1279
-
1280
- /**
1281
- * @type {number | undefined}
1282
- */
1283
- skeleton
1284
- /**
1285
- * @type {number[]}
1286
- */
1287
- joints
1288
-
1289
- /**
1290
- * @param {number[]} joints
1291
- * @param {number} inverseBindMatrices
1292
- */
1293
- constructor(joints, inverseBindMatrices) {
1294
- this.inverseBindMatrices = inverseBindMatrices
1295
- this.joints = joints
1296
- }
1297
-
1298
- /**
1299
-
1300
- * @param {any} data
1301
- */
1302
- static deserialize(data) {
1303
- const { joints, inverseBindMatrices, skeleton } = data
1304
-
1305
- if (!(joints instanceof Array)) {
1306
- throw 'Invalid skin'
1307
- }
1308
- if (
1309
- inverseBindMatrices !== undefined &&
1310
- typeof inverseBindMatrices !== 'number'
1311
- ) {
1312
- throw 'Invalid skin'
1313
- }
1314
- const object = new GLTFSkin(
1315
- joints.filter(e => typeof e === 'number'),
1316
- inverseBindMatrices
1317
- )
1318
-
1319
- if (typeof skeleton === 'number') {
1320
- object.skeleton = skeleton
1321
- }
1322
- return object
1323
- }
1324
- }
1325
-
1326
- class GLTFBufferView {
1327
- /**
1328
- * @type {string}
1329
- */
1330
- name = ''
1331
- /**
1332
- * @type {Record<string,any>}
1333
- */
1334
- extensions = {}
1335
- /**
1336
- * @type {Record<string,any>}
1337
- */
1338
- extras = []
1339
- /**
1340
- * @type {number}
1341
- */
1342
- buffer
1343
- /**
1344
- * @type {number}
1345
- */
1346
- offset = 0
1347
- /**
1348
- * @type {number}
1349
- */
1350
- length
1351
- /**
1352
- * @type {number}
1353
- */
1354
- stride = 0
1355
- /**
1356
- * @type {number | undefined}
1357
- */
1358
- target
1359
-
1360
- /**
1361
- * @param {number} buffer
1362
- * @param {number} length
1363
- */
1364
- constructor(buffer, length) {
1365
- this.buffer = buffer
1366
- this.length = length
1367
- }
1368
-
1369
- /**
1370
- * @param {any} data
1371
- */
1372
- static deserialize(data) {
1373
- const {
1374
- buffer,
1375
- byteOffset,
1376
- byteLength,
1377
- target,
1378
- byteStride,
1379
- name,
1380
- extensions,
1381
- extras
1382
- } = data
1383
-
1384
- if (
1385
- typeof buffer !== "number" ||
1386
- typeof byteLength !== "number"
1387
- ) {
1388
- throw "Invalid buffer view provided"
1389
- }
1390
-
1391
- const struct = new GLTFBufferView(buffer, byteLength)
1392
-
1393
- if (typeof byteOffset === "number") {
1394
- struct.offset = byteOffset
1395
- } else {
1396
- struct.offset = 0
1397
- }
1398
-
1399
- if (typeof byteStride === "number") {
1400
- struct.stride = byteStride
1401
- } else {
1402
- struct.stride = 0
1403
- }
1404
-
1405
- if (typeof target === "number") {
1406
- struct.target = target
1407
- }
1408
-
1409
- if (typeof name === 'string') {
1410
- struct.name = name
1411
- } else {
1412
- struct.name = ""
1413
- }
1414
- if (extensions instanceof Object) {
1415
- struct.extensions = extensions
1416
- } else {
1417
- struct.extensions = {}
1418
- }
1419
- if (extras instanceof Object) {
1420
- struct.extras = extras
1421
- } else {
1422
- struct.extras = {}
1423
- }
1424
- return struct
1425
- }
1426
- }
1427
-
1428
- class GLTFAccessor {
1429
- /**
1430
- * @type {string}
1431
- */
1432
- name = ''
1433
- /**
1434
- * @type {Record<string,any>}
1435
- */
1436
- extensions = {}
1437
- /**
1438
- * @type {Record<string,any>}
1439
- */
1440
- extras = {}
1441
- /**
1442
- * @type {boolean}
1443
- */
1444
- normalized = false
1445
- /**
1446
- * @type {number}
1447
- */
1448
- view = 0
1449
- /**
1450
- * @type {number}
1451
- */
1452
- offset = 0
1453
- /**
1454
- * @type {GLTFComponentType}
1455
- */
1456
- componentType
1457
- /**
1458
- * @type {number}
1459
- */
1460
- count
1461
- /**
1462
- * @type {GLTFAccessorType}
1463
- */
1464
- type
1465
- /**
1466
- * @type {number[] | undefined}
1467
- */
1468
- max
1469
- /**
1470
- * @type {number[] | undefined}
1471
- */
1472
- min
1473
-
1474
- /**
1475
- * @param {number} type
1476
- * @param {number} componentType
1477
- * @param {number} count
1478
- */
1479
- constructor(type, componentType, count) {
1480
- this.type = type
1481
- this.componentType = componentType
1482
- this.count = count
1483
- }
1484
- /**
1485
- * @param {any} data
1486
- */
1487
- static deserialize(data) {
1488
- const {
1489
- bufferView,
1490
- byteOffset,
1491
- componentType,
1492
- count,
1493
- type,
1494
- max,
1495
- min,
1496
- normalized,
1497
- sparse,
1498
- name,
1499
- extensions,
1500
- extras
1501
- } = data
1502
-
1503
- if (
1504
- typeof componentType !== "number" ||
1505
- typeof type !== "string" ||
1506
- typeof count !== "number"
1507
- ) {
1508
- throw "Invalid accessor"
1509
- }
1510
- const struct = new GLTFAccessor(
1511
- mapAccessorType(type),
1512
- mapComponentType(componentType),
1513
- count
1514
- )
1515
-
1516
- if (typeof bufferView === "number") {
1517
- struct.view = bufferView
1518
- } else {
1519
- if (sparse instanceof Object) {
1520
- throw "Sparse accessors are not yet supported"
1521
- } else {
1522
- throw "No buffer view to index into for accessor"
1523
- }
1524
- }
1525
-
1526
- if (typeof byteOffset === "number") {
1527
- struct.offset = byteOffset
1528
- } else {
1529
- struct.offset = 0
1530
- }
1531
-
1532
- if (typeof normalized === "boolean") {
1533
- struct.normalized = normalized
1534
- } else {
1535
- struct.normalized = false
1536
- }
1537
-
1538
- if (max instanceof Array) {
1539
- struct.max = max
1540
- }
1541
-
1542
- if (min instanceof Array) {
1543
- struct.min = min
1544
- }
1545
-
1546
- if (typeof name === 'string') {
1547
- struct.name = name
1548
- } else {
1549
- struct.name = ""
1550
- }
1551
- if (extensions instanceof Object) {
1552
- struct.extensions = extensions
1553
- } else {
1554
- struct.extensions = {}
1555
- }
1556
- if (extras instanceof Object) {
1557
- struct.extras = extras
1558
- } else {
1559
- struct.extras = {}
1560
- }
1561
- return struct
1562
- }
1563
- }
1564
-
1565
- class GLTFMetaData {
1566
- /**
1567
- * @type {string}
1568
- */
1569
- version
1570
- /**
1571
- * @type {string | undefined}
1572
- */
1573
- generator
1574
-
1575
- /**
1576
- * @param {string} version
1577
- */
1578
- constructor(version) {
1579
- this.version = version
1580
- }
1581
- /**
1582
- * @param {any} data
1583
- */
1584
- static deserialize(data) {
1585
- const { version, generator } = data
1586
-
1587
- if (typeof version !== "string") {
1588
- throw "GLTF asset version is required"
1589
- }
1590
-
1591
- const meta = new GLTFMetaData(version)
1592
- if (typeof generator === "string") {
1593
- meta.generator = generator
1594
- }
1595
- return meta
1596
- }
1597
- }
1598
-
1599
- class GLTFPrimitive {
1600
- /**
1601
- * @type {Map<GLTFAttributeName,number>}
1602
- */
1603
- attributes = new Map()
1604
- /**
1605
- * @type {number | undefined}
1606
- */
1607
- indices
1608
- /**
1609
- * @type {number | undefined}
1610
- */
1611
- material
1612
- /**
1613
- * @type {GLTFPrimitiveMode}
1614
- */
1615
- mode = GLTFPrimitiveMode.Triangles
1616
- /**
1617
- * @param {any} data
1618
- */
1619
- static deserialize(data) {
1620
- const { attributes, material, indices, mode } = data
1621
- const primitive = new GLTFPrimitive()
1622
-
1623
- if (typeof indices === "number") {
1624
- primitive.indices = indices
1625
- }
1626
-
1627
- if (typeof material === "number") {
1628
- primitive.material = material
1629
- }
1630
-
1631
- if (attributes instanceof Object) {
1632
- for (const key in attributes) {
1633
- const value = attributes[key]
1634
- if (typeof value === "number") {
1635
- primitive.attributes.set(key, value)
1636
- }
1637
- }
1638
- }
1639
- if (typeof mode === "number") {
1640
- primitive.mode = mapPrimitiveMode(mode)
1641
- } else {
1642
- primitive.mode = GLTFPrimitiveMode.Triangles
1643
- }
1644
- return primitive
1645
- }
1646
- }
1647
-
1648
- class GLTFPBRetallicRoughness {
1649
- /**
1650
- * @type {Record<string,any>}
1651
- */
1652
- extensions = {}
1653
-
1654
- /**
1655
- * @type {Record<string,any>}
1656
- */
1657
- extras = {}
1658
-
1659
- /**
1660
- * @type {[number, number, number, number]}
1661
- */
1662
- baseColorFactor = [1, 1, 1, 1]
1663
-
1664
- /**
1665
- * @type { GLFTTextureInfo | undefined }
1666
- */
1667
- baseColorTexture
1668
-
1669
- /**
1670
- * @type {number}
1671
- */
1672
- metallicFactor = 1
1673
-
1674
- /**
1675
- * @type {number}
1676
- */
1677
- roughnessFactor = 1
1678
-
1679
- /**
1680
- * @type {GLFTTextureInfo | undefined}
1681
- */
1682
- metallicRoughnessTexture
1683
-
1684
- /**
1685
- * @param {any} data
1686
- */
1687
- static deserialize(data) {
1688
- const {
1689
- baseColorFactor,
1690
- baseColorTexture,
1691
- metallicFactor,
1692
- roughnessFactor,
1693
- metallicRoughnessTexture,
1694
- extensions,
1695
- extras
1696
- } = data
1697
-
1698
- const result = new GLTFPBRetallicRoughness()
1699
-
1700
- if (typeof metallicFactor === 'number') {
1701
- result.metallicFactor = metallicFactor
1702
- }
1703
-
1704
- if (typeof roughnessFactor === 'number') {
1705
- result.roughnessFactor = roughnessFactor
1706
- }
1707
-
1708
- if (baseColorFactor instanceof Array && baseColorFactor.length === 4) [
1709
- result.baseColorFactor = /**@type {[number,number,number,number]}*/(baseColorFactor)
1710
- ]
1711
-
1712
- if (baseColorTexture) {
1713
- result.baseColorTexture = GLFTTextureInfo.deserialize(baseColorTexture)
1714
- }
1715
-
1716
- if (metallicRoughnessTexture) {
1717
- result.metallicRoughnessTexture = GLFTTextureInfo.deserialize(metallicRoughnessTexture)
1718
- }
1719
-
1720
- if (extensions instanceof Object) {
1721
- result.extensions = extensions
1722
- } else {
1723
- result.extensions = {}
1724
- }
1725
- if (extras instanceof Object) {
1726
- result.extras = extras
1727
- } else {
1728
- result.extras = {}
1729
- }
1730
-
1731
- return result
1732
- }
1733
- }
1734
-
1735
- class GLFTTextureInfo {
1736
- /**
1737
- * @type {number}
1738
- */
1739
- index
1740
-
1741
- /**
1742
- * @type {number}
1743
- */
1744
- texCoord = 0
1745
-
1746
- /**
1747
- * @type {number}
1748
- */
1749
- scale = 1
1750
-
1751
- /**
1752
- * @type {number}
1753
- */
1754
- strength = 1
1755
-
1756
- /**
1757
- * @type {Record<string, any>}
1758
- */
1759
- extensions = {}
1760
-
1761
- /**
1762
- * @type {Record<string, any>}
1763
- */
1764
- extras = {}
1765
-
1766
- /**
1767
- * @param {number} index
1768
- */
1769
- constructor(index) {
1770
- this.index = index
1771
- }
1772
- /**
1773
- * @param {any} data
1774
- */
1775
- static deserialize(data) {
1776
- const { index, texCoord, scale, strength, extensions, extras } = data
1777
-
1778
- if (typeof index !== 'number') {
1779
- throw 'Texture info index is not type of number'
1780
- }
1781
- const result = new GLFTTextureInfo(index)
1782
-
1783
- if (typeof texCoord === 'number') [
1784
- result.texCoord = texCoord
1785
- ]
1786
-
1787
- if (typeof scale === 'number') [
1788
- result.scale = scale
1789
- ]
1790
-
1791
- if (typeof strength === 'number') [
1792
- result.strength = strength
1793
- ]
1794
-
1795
- if (extensions instanceof Object) {
1796
- result.extensions = extensions
1797
- } else {
1798
- result.extensions = {}
1799
- }
1800
- if (extras instanceof Object) {
1801
- result.extras = extras
1802
- } else {
1803
- result.extras = {}
1804
- }
1805
-
1806
- return result
1807
- }
1808
- }
1809
-
1810
- /**
1811
- * Enum of GLTF's semantic mesh attribute names.
1812
- * @enum {string}
1813
- */
1814
- export const GLTFAttributeName = /**@type {const}*/({
1815
- /** Vertex positions */
1816
- Position: "POSITION",
1817
- /** Vertex normals */
1818
- Normal: "NORMAL",
1819
- /** Vertex tangents */
1820
- Tangent: "TANGENT",
1821
- /** Vertex texture coordinates set 0 */
1822
- TexCoord0: "TEXCOORD_0",
1823
- /** Vertex texture coordinates set 1 */
1824
- TexCoord1: "TEXCOORD_1",
1825
- /** Vertex colors set 0 */
1826
- Color0: "COLOR_0",
1827
- /** Joint indices for skinning set 0 */
1828
- Joints0: "JOINTS_0",
1829
- /** Joint weights for skinning set 0 */
1830
- Weights0: "WEIGHTS_0"
1831
- })
1832
-
1833
- /**
1834
- * @enum {number}
1835
- */
1836
- export const GLTFComponentType = /**@type {const}*/({
1837
- Byte: 0x1400,
1838
- UnsignedByte: 0x1401,
1839
- Short: 0x1402,
1840
- UnsignedShort: 0x1403,
1841
- Int: 0x1404,
1842
- UnsignedInt: 0x1405,
1843
- Float: 0x1406
1844
- })
1845
-
1846
- /**
1847
- * @enum {number}
1848
- */
1849
- export const GLTFAccessorType = /**@type {const}*/({
1850
- Scalar: 0,
1851
- Vec2: 1,
1852
- Vec3: 2,
1853
- Vec4: 3,
1854
- Mat2: 4,
1855
- Mat3: 5,
1856
- Mat4: 6
1857
- })
1858
-
1859
- /**
1860
- * @enum {number}
1861
- */
1862
- export const GLTFPrimitiveMode = /**@type {const}*/({
1863
- Points: 0,
1864
- Lines: 1,
1865
- LineLoop: 2,
1866
- LineStrip: 3,
1867
- Triangles: 4,
1868
- TriangleStrip: 5,
1869
- TriangleFan: 6
1870
- })
1871
-
1872
- /**
1873
- * @enum {string}
1874
- */
1875
- export const GLFTAlphaMode = /**@type {const}*/({
1876
- Opaque: "OPAQUE",
1877
- Mask: "MASK",
1878
- Blend: "BLEND"
1879
- })
1880
-
1881
- /**
1882
- * Maps a raw glTF accessor type string to a valid AccessorType enum value.
1883
- * @param {string} typeString - Raw type from the glTF JSON (e.g. "VEC3", "MAT4").
1884
- * @returns {GLTFAccessorType} One of the AccessorType values.
1885
- * @throws {Error} If the type is not recognized.
1886
- */
1887
- function mapAccessorType(typeString) {
1888
- switch (typeString.toUpperCase()) {
1889
- case "SCALAR": return GLTFAccessorType.Scalar;
1890
- case "VEC2": return GLTFAccessorType.Vec2;
1891
- case "VEC3": return GLTFAccessorType.Vec3;
1892
- case "VEC4": return GLTFAccessorType.Vec4;
1893
- case "MAT2": return GLTFAccessorType.Mat2;
1894
- case "MAT3": return GLTFAccessorType.Mat3;
1895
- case "MAT4": return GLTFAccessorType.Mat4;
1896
- default:
1897
- throw new Error(`Unknown accessor type: ${typeString}`);
1898
- }
1899
- }
1900
-
1901
- /**
1902
- * Maps a raw glTF accessor component type string to a valid AccessorComponentType enum value.
1903
- * @param {number} value - Raw type from the glTF JSON (e.g. "VEC3", "MAT4").
1904
- * @returns {GLTFAccessorType} One of the AccessorType values.
1905
- * @throws {Error} If the component type is not recognized.
1906
- */
1907
- function mapComponentType(value) {
1908
- switch (value) {
1909
- case 0x1400: return GLTFComponentType.Byte;
1910
- case 0x1401: return GLTFComponentType.UnsignedByte;
1911
- case 0x1402: return GLTFComponentType.Short;
1912
- case 0x1403: return GLTFComponentType.UnsignedShort;
1913
- case 0x1404: return GLTFComponentType.Int;
1914
- case 0x1405: return GLTFComponentType.UnsignedInt;
1915
- case 0x1406: return GLTFComponentType.Float;
1916
- default:
1917
- throw new Error(`Unknown accessor component type: ${value}`);
1918
- }
1919
- }
1920
-
1921
- /**
1922
- * @param {number} mode
1923
- */
1924
- export function mapPrimitiveMode(mode) {
1925
- switch (mode) {
1926
- case 0: return GLTFPrimitiveMode.Points;
1927
- case 1: return GLTFPrimitiveMode.Lines;
1928
- case 2: return GLTFPrimitiveMode.LineLoop;
1929
- case 3: return GLTFPrimitiveMode.LineStrip;
1930
- case 4: return GLTFPrimitiveMode.Triangles;
1931
- case 5: return GLTFPrimitiveMode.TriangleStrip;
1932
- case 6: return GLTFPrimitiveMode.TriangleFan;
1933
- default: throw "Unrecognized primitive mode: " + mode
1934
- }
1935
- }
1936
-
1937
- class TRSTransform {
1938
- /**
1939
- * @type {[number,number,number]}
1940
- */
1941
- translation
1942
- /**
1943
- * @type {[number,number,number,number]}
1944
- */
1945
- rotation
1946
- /**
1947
- * @type {[number,number,number]}
1948
- */
1949
- scale
1950
- /**
1951
- * @param {[number, number, number]} [translation]
1952
- * @param {[number, number, number, number]} [rotation]
1953
- * @param {[number, number, number]} [scale]
1954
- */
1955
- constructor(
1956
- translation = [0, 0, 0],
1957
- rotation = [0, 0, 0, 1],
1958
- scale = [1, 1, 1]
1959
- ) {
1960
- this.translation = translation
1961
- this.rotation = rotation
1962
- this.scale = scale
1963
- }
1964
- /**
1965
- * @param {any} translation
1966
- * @param {any} rotation
1967
- * @param {any} scale
1968
- */
1969
- static deserialize(translation, rotation, scale) {
1970
- const transform = new TRSTransform()
1971
-
1972
- if (
1973
- !(translation instanceof Array) ||
1974
- !(rotation instanceof Array) ||
1975
- !(scale instanceof Array)
1976
- ) {
1977
- throw "Invalid transform"
1978
- }
1979
-
1980
- //@ts-ignore
1981
- transform.translation = translation.filter((data) => typeof data === "number")
1982
- //@ts-ignore
1983
- transform.rotation = rotation.filter((data) => typeof data === "number")
1984
- //@ts-ignore
1985
- transform.scale = scale.filter((data) => typeof data === "number")
1986
- return transform
1987
- }
1988
- }
1989
-
1990
- class MatrixTransform {
1991
- /**
1992
- * @type {Affine3}
1993
- */
1994
- value
1995
-
1996
- /**
1997
- * @param {Affine3} value
1998
- */
1999
- constructor(value) {
2000
- this.value = value
2001
- }
2002
- /**
2003
- * @param {any} data
2004
- */
2005
- static deserialize(data) {
2006
-
2007
- if (
2008
- !(data instanceof Array)
2009
- ) {
2010
- throw "Invalid transform"
2011
- }
2012
- const t = data.filter((data) => typeof data === "number")
2013
-
2014
- if (t.length !== 16) {
2015
- throw "invalid matrix transform"
2016
- }
2017
- const transform = new MatrixTransform(new Affine3(
2018
- t[0], t[4], t[8], t[12],
2019
- t[1], t[5], t[9], t[13],
2020
- t[2], t[6], t[10], t[14]
2021
- ))
2022
-
2023
- return transform
2024
- }
2025
- }
2026
-
2027
- /**
2028
- *
2029
- * @param {number} index
2030
- * @param {GLTF} gltf
2031
- * @returns {[DataView,GLTFAccessor]}
2032
- */
2033
- function getAccessorData(index, gltf) {
2034
- const { accessors, bufferViews, buffers } = gltf
2035
- const accessor = accessors[index]
2036
- if (!accessor) throw "Invalid access to accessors"
2037
-
2038
- const view = bufferViews[accessor.view]
2039
-
2040
- if (!view) throw "Invalid access to buffer views"
2041
-
2042
- const buffer = buffers[view.buffer]
2043
-
2044
- if (!buffer) throw "Invalid access to buffer"
2045
-
2046
- const componentSize = getComponentSize(accessor.componentType)
2047
- const elementSize = getElementSize(accessor.type)
2048
- const elementByteLength = componentSize * elementSize
2049
- const stride = view.stride || elementByteLength
2050
-
2051
- if (stride < elementByteLength) {
2052
- throw "Invalid accessor stride"
2053
- }
2054
-
2055
- if (stride === elementByteLength) {
2056
- const byteLength = accessor.count * elementByteLength
2057
- return [
2058
- new DataView(buffer, view.offset + accessor.offset, byteLength),
2059
- accessor
2060
- ]
2061
- }
2062
-
2063
- const packedBuffer = new ArrayBuffer(accessor.count * elementByteLength)
2064
- const destination = new Uint8Array(packedBuffer)
2065
- const sourceOffset = view.offset + accessor.offset
2066
-
2067
- for (let i = 0; i < accessor.count; i++) {
2068
- const sourceStart = sourceOffset + i * stride
2069
- destination.set(
2070
- new Uint8Array(buffer, sourceStart, elementByteLength),
2071
- i * elementByteLength
2072
- )
2073
- }
2074
-
2075
- return [
2076
- new DataView(packedBuffer),
2077
- accessor
2078
- ]
2079
- }
2080
-
2081
- /**
2082
- * @param {GLTFAccessor} accessor
2083
- * @param {DataView} view
2084
- */
2085
- function mapToIndices(accessor, view) {
2086
- if (accessor.type !== GLTFAccessorType.Scalar) {
2087
- throw "Indices provided on a mesh is not valid"
2088
- }
2089
-
2090
- switch (accessor.componentType) {
2091
- case GLTFComponentType.UnsignedByte:
2092
- return new Uint8Array(
2093
- view.buffer,
2094
- view.byteOffset,
2095
- view.byteLength / Uint8Array.BYTES_PER_ELEMENT
2096
- )
2097
- case GLTFComponentType.UnsignedShort:
2098
- return new Uint16Array(
2099
- view.buffer,
2100
- view.byteOffset,
2101
- view.byteLength / Uint16Array.BYTES_PER_ELEMENT
2102
- )
2103
- case GLTFComponentType.UnsignedInt:
2104
- return new Uint32Array(
2105
- view.buffer,
2106
- view.byteOffset,
2107
- view.byteLength / Uint32Array.BYTES_PER_ELEMENT
2108
- )
2109
- default:
2110
- throw "Indices provided on a mesh is not valid"
2111
- }
2112
- }
2113
-
2114
- /**
2115
- * @param {GLTFComponentType} componentType
2116
- */
2117
- function isFloatComponentType(componentType) {
2118
- return componentType === GLTFComponentType.Float
2119
- }
2120
-
2121
- /**
2122
- * @param {GLTFComponentType} componentType
2123
- */
2124
- function isNormalizedIntegerComponentType(componentType) {
2125
- switch (componentType) {
2126
- case GLTFComponentType.Byte:
2127
- case GLTFComponentType.UnsignedByte:
2128
- case GLTFComponentType.Short:
2129
- case GLTFComponentType.UnsignedShort:
2130
- return true
2131
- default:
2132
- return false
2133
- }
2134
- }
2135
-
2136
- /**
2137
- * @param {GLTFComponentType} componentType
2138
- */
2139
- function isJointComponentType(componentType) {
2140
- switch (componentType) {
2141
- case GLTFComponentType.UnsignedByte:
2142
- case GLTFComponentType.UnsignedShort:
2143
- return true
2144
- default:
2145
- return false
2146
- }
2147
- }
2148
-
2149
- /**
2150
- * @param {DataView} view
2151
- * @param {number} offset
2152
- * @param {GLTFComponentType} componentType
2153
- * @param {boolean} normalized
2154
- * @returns {number}
2155
- */
2156
- function readAccessorComponent(view, offset, componentType, normalized) {
2157
- switch (componentType) {
2158
- case GLTFComponentType.Byte: {
2159
- const value = view.getInt8(offset)
2160
- if (!normalized) return value
2161
- return Math.max(value / 127, -1)
2162
- }
2163
- case GLTFComponentType.UnsignedByte: {
2164
- const value = view.getUint8(offset)
2165
- if (!normalized) return value
2166
- return value / 255
2167
- }
2168
- case GLTFComponentType.Short: {
2169
- const value = view.getInt16(offset, true)
2170
- if (!normalized) return value
2171
- return Math.max(value / 32767, -1)
2172
- }
2173
- case GLTFComponentType.UnsignedShort: {
2174
- const value = view.getUint16(offset, true)
2175
- if (!normalized) return value
2176
- return value / 65535
2177
- }
2178
- case GLTFComponentType.Int: {
2179
- const value = view.getInt32(offset, true)
2180
- if (!normalized) return value
2181
- return Math.max(value / 2147483647, -1)
2182
- }
2183
- case GLTFComponentType.UnsignedInt: {
2184
- const value = view.getUint32(offset, true)
2185
- if (!normalized) return value
2186
- return value / 4294967295
2187
- }
2188
- case GLTFComponentType.Float:
2189
- return view.getFloat32(offset, true)
2190
- default:
2191
- throw "Unsupported accessor component type"
2192
- }
2193
- }
2194
-
2195
- /**
2196
- * @param {GLTFAccessor} accessor
2197
- * @param {DataView} buffer
2198
- * @param {number} componentCount
2199
- * @param {boolean} [allowVec3Padding=false]
2200
- * @returns {DataView}
2201
- */
2202
- function convertAccessorToFloat32(accessor, buffer, componentCount, allowVec3Padding = false) {
2203
- const sourceComponents = getElementSize(accessor.type)
2204
- if (sourceComponents !== componentCount) {
2205
- if (!(allowVec3Padding && componentCount === 4 && sourceComponents === 3)) {
2206
- throw "Attribute types do not match"
2207
- }
2208
- }
2209
-
2210
- const componentSize = getComponentSize(accessor.componentType)
2211
- const normalized = accessor.normalized
2212
- const result = new Float32Array(accessor.count * componentCount)
2213
-
2214
- for (let i = 0; i < accessor.count; i++) {
2215
- const sourceOffset = i * sourceComponents * componentSize
2216
- const resultOffset = i * componentCount
2217
-
2218
- for (let component = 0; component < sourceComponents; component++) {
2219
- result[resultOffset + component] = readAccessorComponent(
2220
- buffer,
2221
- sourceOffset + component * componentSize,
2222
- accessor.componentType,
2223
- normalized
2224
- )
2225
- }
2226
-
2227
- if (allowVec3Padding && componentCount === 4 && sourceComponents === 3) {
2228
- result[resultOffset + 3] = 1.0
2229
- }
2230
- }
2231
-
2232
- return new DataView(result.buffer)
2233
- }
2234
-
2235
- /**
2236
- * @param {GLTFAccessor} accessor
2237
- * @param {DataView} buffer
2238
- * @returns {DataView}
2239
- */
2240
- function convertAccessorToUint16(accessor, buffer) {
2241
- const sourceComponents = getElementSize(accessor.type)
2242
- if (sourceComponents !== 4) {
2243
- throw "Attribute types do not match"
2244
- }
2245
-
2246
- const componentType = accessor.componentType
2247
- if (!isJointComponentType(componentType) || accessor.normalized) {
2248
- throw "Attribute types do not match"
2249
- }
2250
-
2251
- const componentSize = getComponentSize(componentType)
2252
- const result = new Uint16Array(accessor.count * sourceComponents)
2253
-
2254
- for (let i = 0; i < accessor.count; i++) {
2255
- const sourceOffset = i * sourceComponents * componentSize
2256
- const resultOffset = i * sourceComponents
2257
-
2258
- for (let component = 0; component < sourceComponents; component++) {
2259
- result[resultOffset + component] = readAccessorComponent(
2260
- buffer,
2261
- sourceOffset + component * componentSize,
2262
- componentType,
2263
- false
2264
- )
2265
- }
2266
- }
2267
-
2268
- return new DataView(result.buffer)
2269
- }
2270
-
2271
- /**
2272
- * @param {string} name
2273
- * @param {GLTFAccessor} accessor
2274
- * @param {DataView} buffer
2275
- * @returns {[string, DataView] | undefined}
2276
- */
2277
- function mapAccessorTypeToAttribute(name, accessor, buffer) {
2278
- switch (name) {
2279
- case GLTFAttributeName.Position:
2280
- if (accessor.type !== GLTFAccessorType.Vec3 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
2281
- throw "Attribute types do not match"
2282
- return [Attribute.Position.name, convertAccessorToFloat32(accessor, buffer, 3)]
2283
- case GLTFAttributeName.TexCoord0:
2284
- if (
2285
- accessor.type !== GLTFAccessorType.Vec2 ||
2286
- (!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
2287
- (accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
2288
- )
2289
- throw "Attribute types do not match"
2290
- return [Attribute.UV.name, convertAccessorToFloat32(accessor, buffer, 2)]
2291
- case GLTFAttributeName.TexCoord1:
2292
- if (
2293
- accessor.type !== GLTFAccessorType.Vec2 ||
2294
- (!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
2295
- (accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
2296
- )
2297
- throw "Attribute types do not match"
2298
- return [Attribute.UVB.name, convertAccessorToFloat32(accessor, buffer, 2)]
2299
- case GLTFAttributeName.Normal:
2300
- if (accessor.type !== GLTFAccessorType.Vec3 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
2301
- throw "Attribute types do not match"
2302
- return [Attribute.Normal.name, convertAccessorToFloat32(accessor, buffer, 3)]
2303
- case GLTFAttributeName.Tangent:
2304
- if (accessor.type !== GLTFAccessorType.Vec4 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
2305
- throw "Attribute types do not match"
2306
- return [Attribute.Tangent.name, convertAccessorToFloat32(accessor, buffer, 4)]
2307
- case GLTFAttributeName.Color0:
2308
- if (
2309
- (accessor.type !== GLTFAccessorType.Vec3 && accessor.type !== GLTFAccessorType.Vec4) ||
2310
- (!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
2311
- (accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
2312
- )
2313
- throw "Attribute types do not match"
2314
- return [Attribute.Color.name, convertAccessorToFloat32(accessor, buffer, 4, accessor.type === GLTFAccessorType.Vec3)]
2315
- case GLTFAttributeName.Weights0:
2316
- if (
2317
- accessor.type !== GLTFAccessorType.Vec4 ||
2318
- (!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
2319
- (accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
2320
- )
2321
- throw "Attribute types do not match"
2322
- return [Attribute.JointWeight.name, convertAccessorToFloat32(accessor, buffer, 4)]
2323
- case GLTFAttributeName.Joints0:
2324
- if (accessor.type !== GLTFAccessorType.Vec4 || accessor.normalized || !isJointComponentType(accessor.componentType)) {
2325
- throw "Attribute types do not match"
2326
- }
2327
- return [Attribute.JointIndex.name, convertAccessorToUint16(accessor, buffer)]
2328
- default:
2329
- return undefined;
2330
- }
2331
- }
2332
-
2333
-
2334
- /**
2335
- * @param {GLTFComponentType} componentType
2336
- */
2337
- function getComponentSize(componentType) {
2338
- switch (componentType) {
2339
- case GLTFComponentType.Byte: // 0x1400
2340
- return 1;
2341
- case GLTFComponentType.UnsignedByte: // 0x1401
2342
- return 1;
2343
- case GLTFComponentType.Short: // 0x1402
2344
- return 2;
2345
- case GLTFComponentType.UnsignedShort: // 0x1403
2346
- return 2;
2347
- case GLTFComponentType.Int: // 0x1404
2348
- return 4;
2349
- case GLTFComponentType.UnsignedInt: // 0x1405
2350
- return 4;
2351
- case GLTFComponentType.Float: // 0x1406
2352
- return 4;
2353
- default:
2354
- return 0; // Return 0 if componentType is unknown
2355
- }
2356
- }
2357
-
2358
-
2359
- /**
2360
- * @param {GLTFAccessorType} type
2361
- */
2362
- function getElementSize(type) {
2363
- switch (type) {
2364
- case GLTFAccessorType.Scalar:
2365
- return 1;
2366
- case GLTFAccessorType.Vec2:
2367
- return 2;
2368
- case GLTFAccessorType.Vec3:
2369
- return 3;
2370
- case GLTFAccessorType.Vec4:
2371
- return 4;
2372
- case GLTFAccessorType.Mat2:
2373
- return 4;
2374
- case GLTFAccessorType.Mat3:
2375
- return 9;
2376
- case GLTFAccessorType.Mat4:
2377
- return 16;
2378
- default:
2379
- return 0;;
2380
- }
2381
- }
2382
-
2383
- /**
2384
- * @param {number} mesh
2385
- * @param {GLTFMesh[]} meshes
2386
- * @param {[Mesh,number | undefined][][]} geometries
2387
- * @param {StandardMaterial[]} materials
2388
- */
2389
- function parseMeshObject(mesh, meshes, geometries, materials) {
2390
- const meshData = meshes[mesh]
2391
- const geometry = geometries[mesh]
2392
-
2393
- if (!meshData || !geometry) {
2394
- throw "Invalid mesh index on node"
2395
- }
2396
- if (geometry.length === 1) {
2397
- const item = /**@type {[Mesh,number | undefined]}*/(geometry[0])
2398
- const material = item[1] !== undefined ? materials[item[1]] : defaultMaterial
2399
-
2400
- return new MeshMaterial3D(item[0], material || defaultMaterial)
2401
- }
2402
- const root = new Object3D()
2403
- for (let i = 0; i < geometry.length; i++) {
2404
- const item = /**@type {[Mesh,number | undefined]}*/(geometry[i])
2405
- const material = item[1] !== undefined ? materials[item[1]] : defaultMaterial
2406
- const object = new MeshMaterial3D(item[0], material || defaultMaterial)
2407
-
2408
- root.add(object)
2409
- }
2410
-
2411
- return root
2412
- }
2413
-
2414
- /**
2415
- * @param {GLTFMesh} gltfMesh
2416
- * @param {GLTF} gltf
2417
- * @returns {[Mesh, number | undefined][]}
2418
- */
2419
- function parseGeometry(gltfMesh, gltf) {
2420
- /**@type {[Mesh, number | undefined][]}*/
2421
- const results = []
2422
- for (let i = 0; i < gltfMesh.primitives.length; i++) {
2423
- const primitive = /**@type {GLTFPrimitive} */ (gltfMesh.primitives[i])
2424
- const attributes = new SeparateAttributeData()
2425
- const mesh = new Mesh(attributes)
2426
- mesh.topology = primitive.mode
2427
- if (primitive.indices !== undefined) {
2428
- const [dataView, accessor] = getAccessorData(
2429
- primitive.indices,
2430
- gltf
2431
- )
2432
- mesh.indices = mapToIndices(accessor, dataView)
2433
- }
2434
- for (const [name, location] of primitive.attributes) {
2435
- const [buffer, accessor] = getAccessorData(
2436
- location,
2437
- gltf
2438
- )
2439
- const attribute = mapAccessorTypeToAttribute(name, accessor, buffer)
2440
- if (!attribute) continue
2441
- const [attributeName, attributeBuffer] = attribute
2442
- attributes.set(
2443
- attributeName,
2444
- attributeBuffer
2445
- )
2446
- }
2447
-
2448
- mesh.normalizeJointWeights()
2449
- results.push([mesh, primitive.material])
2450
- }
2451
- return results
2452
- }
2453
-
2454
- /**
2455
- * @param {GLTFSkin} gltfSkin
2456
- * @param {GLTF} gltf
2457
- * @param {Map<number, Object3D>} entityMap
2458
- * @returns {Skin}
2459
- */
2460
- function parseSkin(gltfSkin, gltf, entityMap) {
2461
- /** @type {Affine3[]} */
2462
- let bindPose = []
2463
-
2464
- if (gltfSkin.inverseBindMatrices !== undefined) {
2465
- const bindPoseAccessor = gltf.accessors[gltfSkin.inverseBindMatrices]
2466
- if (
2467
- !bindPoseAccessor ||
2468
- bindPoseAccessor.type !== GLTFAccessorType.Mat4 ||
2469
- bindPoseAccessor.componentType !== GLTFComponentType.Float ||
2470
- bindPoseAccessor.normalized
2471
- ) {
2472
- throw "Attribute types do not match"
2473
- }
2474
-
2475
- const [bindPoseData] = getAccessorData(gltfSkin.inverseBindMatrices, gltf)
2476
- bindPose = convertToInverseBindPose(bindPoseData)
2477
- }
2478
-
2479
- const skin = new Skin()
2480
-
2481
- if (gltfSkin.inverseBindMatrices !== undefined && bindPose.length !== gltfSkin.joints.length) {
2482
- console.warn(
2483
- "GLTF skin inverse bind matrix count does not match joint count",
2484
- bindPose.length,
2485
- gltfSkin.joints.length
2486
- )
2487
- }
2488
-
2489
- skin.inverseBindPose = gltfSkin.joints.map((_, index) => bindPose[index] || new Affine3())
2490
- skin.bones = gltfSkin.joints.map(joint => {
2491
- const entity = entityMap.get(joint)
2492
- if (!(entity instanceof Bone3D)) {
2493
- throw "One of the bones is not a `Bone3D`"
2494
- }
2495
-
2496
- return entity
2497
- })
2498
-
2499
- return skin
2500
- }
2501
-
2502
- /**
2503
- *
2504
- * @param {DataView} poseData
2505
- * @returns {Affine3[]}
2506
- */
2507
- function convertToInverseBindPose(poseData) {
2508
- const results = []
2509
- const data = new Float32Array(
2510
- poseData.buffer,
2511
- poseData.byteOffset,
2512
- poseData.byteLength / Float32Array.BYTES_PER_ELEMENT
2513
- )
2514
-
2515
- for (let offset = 0; offset < data.length; offset += 16) {
2516
- // glTF stores matrices in column-major order. Affine3 expects the same
2517
- // 3x4 affine components, with translation coming from the last column.
2518
- const affine = new Affine3(
2519
- data[offset + 0], data[offset + 4], data[offset + 8], data[offset + 12],
2520
- data[offset + 1], data[offset + 5], data[offset + 9], data[offset + 13],
2521
- data[offset + 2], data[offset + 6], data[offset + 10], data[offset + 14],
2522
- )
2523
-
2524
- results.push(affine)
2525
- }
2526
-
2527
- return results
2528
- }
2529
-
2530
- /**
2531
- * @param {number} index
2532
- * @param {GLTFNode} node
2533
- * @param {GLTF} gltf
2534
- * @param {[Mesh,number | undefined][][]} geometries
2535
- * @param {StandardMaterial[]} materials
2536
- * @param {Map<number, DirectionalLight | PointLight | SpotLight>} lightCache
2537
- */
2538
- function parseObject(index, node, gltf, geometries, materials, lightCache) {
2539
- const { mesh, transform, name } = node
2540
- const light = parseGLTFLight(node, gltf, lightCache)
2541
-
2542
- let object
2543
- if (mesh !== undefined) {
2544
- object = parseMeshObject(mesh, gltf.meshes, geometries, materials)
2545
- if (light) {
2546
- object.add(light)
2547
- }
2548
- } else {
2549
- const bone = parseBone(index, gltf)
2550
-
2551
- if (bone) {
2552
- object = bone
2553
- if (light) {
2554
- object.add(light)
2555
- }
2556
- } else if (light) {
2557
- object = light
2558
- } else {
2559
- object = new Object3D()
2560
- }
2561
- }
2562
-
2563
- if (transform) {
2564
- transferTransform(object, transform)
2565
- }
2566
-
2567
- object.name = name
2568
-
2569
- return object
2570
- }
2571
-
2572
- /**
2573
- * @param {GLTFNode} node
2574
- * @param {GLTF} gltf
2575
- * @param {Map<number, DirectionalLight | PointLight | SpotLight>} lightCache
2576
- * @returns {DirectionalLight | PointLight | SpotLight | undefined}
2577
- */
2578
- function parseGLTFLight(node, gltf, lightCache) {
2579
- const nodeLightExtension = node.extensions[GLTF_LIGHTS_PUNCTUAL_EXTENSION]
2580
-
2581
- if (!(nodeLightExtension instanceof Object)) {
2582
- return undefined
2583
- }
2584
-
2585
- const lightIndex = nodeLightExtension.light
2586
- if (typeof lightIndex !== "number") {
2587
- throw "GLTF light node is missing a light index"
2588
- }
2589
-
2590
- const extension = gltf.extensions[GLTF_LIGHTS_PUNCTUAL_EXTENSION]
2591
- if (!(extension instanceof Object) || !(extension.lights instanceof Array)) {
2592
- throw "GLTF punctual lights extension is missing its light table"
2593
- }
2594
-
2595
- const gltfLight = extension.lights[lightIndex]
2596
- if (!(gltfLight instanceof Object)) {
2597
- throw "GLTF light index is invalid"
2598
- }
2599
-
2600
- const cachedLight = lightCache.get(lightIndex)
2601
- if (cachedLight) {
2602
- return cachedLight.clone()
2603
- }
2604
-
2605
- const prototype = createGLTFLight(gltfLight)
2606
- lightCache.set(lightIndex, prototype)
2607
- return prototype.clone()
2608
- }
2609
-
2610
- /**
2611
- * @param {any} gltfLight
2612
- * @returns {DirectionalLight | PointLight | SpotLight}
2613
- */
2614
- function createGLTFLight(gltfLight) {
2615
- const color = Array.isArray(gltfLight.color) ? gltfLight.color : []
2616
- const red = Number.isFinite(color[0]) ? color[0] : 1
2617
- const green = Number.isFinite(color[1]) ? color[1] : 1
2618
- const blue = Number.isFinite(color[2]) ? color[2] : 1
2619
- const intensity = Number.isFinite(gltfLight.intensity) ? gltfLight.intensity : 1
2620
- const name = typeof gltfLight.name === "string" ? gltfLight.name : ""
2621
-
2622
- if (gltfLight.type === "directional") {
2623
- const light = new DirectionalLight()
2624
- light.color.set(red, green, blue)
2625
- light.intensity = intensity
2626
- light.name = name
2627
- return light
2628
- }
2629
-
2630
- if (gltfLight.type === "point") {
2631
- const light = new PointLight()
2632
- light.color.set(red, green, blue)
2633
- light.intensity = intensity
2634
- light.radius = Number.isFinite(gltfLight.range) && gltfLight.range > 0
2635
- ? gltfLight.range
2636
- : DEFAULT_GLTF_LIGHT_RANGE
2637
- light.name = name
2638
- return light
2639
- }
2640
-
2641
- if (gltfLight.type === "spot") {
2642
- const light = new SpotLight()
2643
- const spot = gltfLight.spot instanceof Object ? gltfLight.spot : {}
2644
- const innerConeAngle = Number.isFinite(spot.innerConeAngle) && spot.innerConeAngle >= 0
2645
- ? spot.innerConeAngle
2646
- : 0
2647
- const outerConeAngle = Number.isFinite(spot.outerConeAngle) && spot.outerConeAngle > 0
2648
- ? spot.outerConeAngle
2649
- : Math.PI / 4
2650
- const normalizedInnerConeAngle = Math.min(innerConeAngle, outerConeAngle)
2651
- const normalizedOuterConeAngle = Math.max(innerConeAngle, outerConeAngle)
2652
-
2653
- light.color.set(red, green, blue)
2654
- light.intensity = intensity
2655
- light.range = Number.isFinite(gltfLight.range) && gltfLight.range > 0
2656
- ? gltfLight.range
2657
- : DEFAULT_GLTF_LIGHT_RANGE
2658
- light.innerAngle = normalizedInnerConeAngle * 2
2659
- light.outerAngle = normalizedOuterConeAngle * 2
2660
- light.name = name
2661
- return light
2662
- }
2663
-
2664
- throw `Unsupported glTF light type: ${gltfLight.type}`
2665
- }
2666
-
2667
- /**
2668
- * @param {number} index
2669
- * @param {GLTF} gltf
2670
- * @returns {Bone3D | undefined}
2671
- */
2672
- function parseBone(index, gltf) {
2673
- for (const skin of gltf.skins) {
2674
- const boneIndex = skin.joints.findIndex((v) => v === index)
2675
- if (boneIndex !== -1) {
2676
- const bone = new Bone3D()
2677
- bone.index = boneIndex
2678
- return bone
2679
- }
2680
- }
2681
- return undefined
2682
- }
2683
-
2684
- /**
2685
- * @param {Object3D} object
2686
- * @param {TRSTransform | MatrixTransform} transform
2687
- */
2688
- function transferTransform(object, transform) {
2689
- if (transform instanceof TRSTransform) {
2690
- object.transform.position.x = transform.translation[0]
2691
- object.transform.position.y = transform.translation[1]
2692
- object.transform.position.z = transform.translation[2]
2693
- object.transform.orientation.x = transform.rotation[0]
2694
- object.transform.orientation.y = transform.rotation[1]
2695
- object.transform.orientation.z = transform.rotation[2]
2696
- object.transform.orientation.w = transform.rotation[3]
2697
- object.transform.scale.x = transform.scale[0]
2698
- object.transform.scale.y = transform.scale[1]
2699
- object.transform.scale.z = transform.scale[2]
2700
- }
2701
- if (transform instanceof MatrixTransform) {
2702
- const [position, rotation, scale] = transform.value.decompose()
2703
-
2704
- object.transform.position.copy(position)
2705
- object.transform.orientation.copy(rotation)
2706
- object.transform.scale.copy(scale)
2707
- }
2708
- }
2709
-
2710
- /**
2711
- *
2712
- * @param {TypedArray} from
2713
- * @param {new (...args:any[])=>TypedArray} to
2714
- * @returns
2715
- */
2716
- /**
2717
- *@typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray
2718
- */
2719
-
2720
- /**
2721
- * @typedef {[number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number]} MatrixArray
2722
- */