chorama 0.0.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +24 -0
- package/.github/workflows/static.yml +22 -53
- package/README.md +3 -1
- package/assets/models/glb/AlphaBlendModeTest.glb +0 -0
- package/assets/models/glb/IridescenceLamp.glb +0 -0
- package/assets/models/glb/teapot.glb +0 -0
- package/astro.config.mjs +9 -21
- package/content/guide/render-targets-and-views.md +3 -4
- package/content/guide/textures-and-assets.md +5 -2
- package/dist/index.module.js +23334 -0
- package/dist/index.module.js.map +1 -0
- package/dist/index.umd.js +23610 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/website/_astro/DocLayout.BsV7p0dY.css +1 -0
- package/dist/website/examples/camera/orthographic/example.html +172 -0
- package/dist/website/examples/camera/orthographic/index.html +124 -0
- package/dist/website/examples/camera/perspective/example.html +190 -0
- package/dist/website/examples/camera/perspective/index.html +142 -0
- package/dist/website/examples/camera/tone_mapping/example.html +238 -0
- package/dist/website/examples/camera/tone_mapping/index.html +190 -0
- package/dist/website/examples/index.html +2 -0
- package/dist/website/examples/lights/directional/example.html +447 -0
- package/dist/website/examples/lights/directional/index.html +399 -0
- package/dist/website/examples/lights/multiple_spot_lights/example.html +356 -0
- package/dist/website/examples/lights/multiple_spot_lights/index.html +308 -0
- package/dist/website/examples/lights/point/example.html +387 -0
- package/dist/website/examples/lights/point/index.html +339 -0
- package/dist/website/examples/lights/spot/example.html +416 -0
- package/dist/website/examples/lights/spot/index.html +368 -0
- package/dist/website/examples/loader/gltf/basic/example.html +129 -0
- package/dist/website/examples/loader/gltf/basic/index.html +81 -0
- package/dist/website/examples/loader/gltf/material/alpha-blend-mode/example.html +131 -0
- package/dist/website/examples/loader/gltf/material/alpha-blend-mode/index.html +83 -0
- package/dist/website/examples/loader/gltf/material/flight_helmet/example.html +162 -0
- package/dist/website/examples/loader/gltf/material/flight_helmet/index.html +114 -0
- package/dist/website/examples/loader/gltf/material/iridescence_lamp/example.html +169 -0
- package/dist/website/examples/loader/gltf/material/iridescence_lamp/index.html +121 -0
- package/dist/website/examples/loader/objloader/example.html +144 -0
- package/dist/website/examples/loader/objloader/index.html +96 -0
- package/dist/website/examples/material/cullface/example.html +163 -0
- package/dist/website/examples/material/cullface/index.html +115 -0
- package/dist/website/examples/material/materials/example.html +175 -0
- package/dist/website/examples/material/materials/index.html +127 -0
- package/dist/website/examples/material/standard/basic/example.html +212 -0
- package/dist/website/examples/material/standard/basic/index.html +164 -0
- package/dist/website/examples/material/standard/reflection_refraction/example.html +241 -0
- package/dist/website/examples/material/standard/reflection_refraction/index.html +193 -0
- package/dist/website/examples/mesh/capsule/example.html +173 -0
- package/dist/website/examples/mesh/capsule/index.html +125 -0
- package/dist/website/examples/mesh/circle/example.html +173 -0
- package/dist/website/examples/mesh/circle/index.html +125 -0
- package/dist/website/examples/mesh/cone/example.html +180 -0
- package/dist/website/examples/mesh/cone/index.html +132 -0
- package/dist/website/examples/mesh/cuboid/example.html +207 -0
- package/dist/website/examples/mesh/cuboid/index.html +159 -0
- package/dist/website/examples/mesh/cylinder/example.html +195 -0
- package/dist/website/examples/mesh/cylinder/index.html +147 -0
- package/dist/website/examples/mesh/dodecahedron/example.html +158 -0
- package/dist/website/examples/mesh/dodecahedron/index.html +110 -0
- package/dist/website/examples/mesh/ellipsoid/example.html +179 -0
- package/dist/website/examples/mesh/ellipsoid/index.html +131 -0
- package/dist/website/examples/mesh/extrude/example.html +169 -0
- package/dist/website/examples/mesh/extrude/index.html +121 -0
- package/dist/website/examples/mesh/frustum/example.html +185 -0
- package/dist/website/examples/mesh/frustum/index.html +137 -0
- package/dist/website/examples/mesh/helix/example.html +184 -0
- package/dist/website/examples/mesh/helix/index.html +136 -0
- package/dist/website/examples/mesh/icosahedron/example.html +158 -0
- package/dist/website/examples/mesh/icosahedron/index.html +110 -0
- package/dist/website/examples/mesh/icosphere/example.html +163 -0
- package/dist/website/examples/mesh/icosphere/index.html +115 -0
- package/dist/website/examples/mesh/lathe/example.html +175 -0
- package/dist/website/examples/mesh/lathe/index.html +127 -0
- package/dist/website/examples/mesh/meshTopology/example.html +210 -0
- package/dist/website/examples/mesh/meshTopology/index.html +162 -0
- package/dist/website/examples/mesh/octahedron/example.html +158 -0
- package/dist/website/examples/mesh/octahedron/index.html +110 -0
- package/dist/website/examples/mesh/plane/example.html +173 -0
- package/dist/website/examples/mesh/plane/index.html +125 -0
- package/dist/website/examples/mesh/prism/example.html +173 -0
- package/dist/website/examples/mesh/prism/index.html +125 -0
- package/dist/website/examples/mesh/pyramid/example.html +172 -0
- package/dist/website/examples/mesh/pyramid/index.html +124 -0
- package/dist/website/examples/mesh/ring/example.html +173 -0
- package/dist/website/examples/mesh/ring/index.html +125 -0
- package/dist/website/examples/mesh/skinning/example.html +187 -0
- package/dist/website/examples/mesh/skinning/index.html +139 -0
- package/dist/website/examples/mesh/terrain/example.html +214 -0
- package/dist/website/examples/mesh/terrain/index.html +166 -0
- package/dist/website/examples/mesh/tetrahedron/example.html +158 -0
- package/dist/website/examples/mesh/tetrahedron/index.html +110 -0
- package/dist/website/examples/mesh/torus/example.html +173 -0
- package/dist/website/examples/mesh/torus/index.html +125 -0
- package/dist/website/examples/mesh/torus-knot/example.html +188 -0
- package/dist/website/examples/mesh/torus-knot/index.html +140 -0
- package/dist/website/examples/mesh/tube/example.html +180 -0
- package/dist/website/examples/mesh/tube/index.html +132 -0
- package/dist/website/examples/mesh/uvsphere/example.html +169 -0
- package/dist/website/examples/mesh/uvsphere/index.html +121 -0
- package/dist/website/examples/mesh/wedge/example.html +168 -0
- package/dist/website/examples/mesh/wedge/index.html +120 -0
- package/dist/website/examples/other/rotatingCube/example.html +142 -0
- package/dist/website/examples/other/rotatingCube/index.html +94 -0
- package/dist/website/examples/other/rotatingSphere/example.html +144 -0
- package/dist/website/examples/other/rotatingSphere/index.html +96 -0
- package/dist/website/examples/rendertarget/basic_canvas/example.html +178 -0
- package/dist/website/examples/rendertarget/basic_canvas/index.html +130 -0
- package/dist/website/examples/rendertarget/depth_texture/example.html +188 -0
- package/dist/website/examples/rendertarget/depth_texture/index.html +140 -0
- package/dist/website/examples/rendertarget/image_target/example.html +196 -0
- package/dist/website/examples/rendertarget/image_target/index.html +148 -0
- package/dist/website/examples/rendertarget/multiple_views/example.html +204 -0
- package/dist/website/examples/rendertarget/multiple_views/index.html +156 -0
- package/dist/website/examples/rendertarget/render_masks/example.html +222 -0
- package/dist/website/examples/rendertarget/render_masks/index.html +174 -0
- package/dist/website/examples/rendertarget/split_screen/example.html +178 -0
- package/dist/website/examples/rendertarget/split_screen/index.html +130 -0
- package/dist/website/examples/rendertarget/split_view/example.html +191 -0
- package/dist/website/examples/rendertarget/split_view/index.html +143 -0
- package/dist/website/examples/skybox/skybox/example.html +162 -0
- package/dist/website/examples/skybox/skybox/index.html +114 -0
- package/dist/website/examples/texture/arrays/example.html +204 -0
- package/dist/website/examples/texture/arrays/index.html +156 -0
- package/dist/website/examples/texture/textureWrap/example.html +169 -0
- package/dist/website/examples/texture/textureWrap/index.html +121 -0
- package/dist/website/examples/transform/propagation/example.html +141 -0
- package/dist/website/examples/transform/propagation/index.html +93 -0
- package/dist/website/guide/api-map/index.html +82 -0
- package/dist/website/guide/camera-and-controls/index.html +62 -0
- package/dist/website/guide/first-scene/index.html +119 -0
- package/dist/website/guide/index.html +58 -0
- package/dist/website/guide/installation/index.html +133 -0
- package/dist/website/guide/materials-and-lighting/index.html +90 -0
- package/dist/website/guide/plugins-and-render-pipeline/index.html +89 -0
- package/dist/website/guide/render-targets-and-views/index.html +95 -0
- package/dist/website/guide/scene-graph-and-transforms/index.html +69 -0
- package/dist/website/guide/textures-and-assets/index.html +85 -0
- package/dist/website/guide/troubleshooting/index.html +39 -0
- package/dist/website/images/disappointed.jpg +0 -0
- package/dist/website/images/skybox/grimmnight_back.png +0 -0
- package/dist/website/images/skybox/grimmnight_bottom.png +0 -0
- package/dist/website/images/skybox/grimmnight_front.png +0 -0
- package/dist/website/images/skybox/grimmnight_left.png +0 -0
- package/dist/website/images/skybox/grimmnight_right.png +0 -0
- package/dist/website/images/skybox/grimmnight_top.png +0 -0
- package/dist/website/images/skybox/miramar_back.png +0 -0
- package/dist/website/images/skybox/miramar_bottom.png +0 -0
- package/dist/website/images/skybox/miramar_front.png +0 -0
- package/dist/website/images/skybox/miramar_left.png +0 -0
- package/dist/website/images/skybox/miramar_right.png +0 -0
- package/dist/website/images/skybox/miramar_top.png +0 -0
- package/dist/website/images/uv.jpg +0 -0
- package/dist/website/index.html +157 -0
- package/dist/website/models/glb/AlphaBlendModeTest.glb +0 -0
- package/dist/website/models/glb/IridescenceLamp.glb +0 -0
- package/dist/website/models/glb/teapot.glb +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_BaseColor.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_Normal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_GlassPlasticMat_OcclusionRoughMetal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_BaseColor.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_Normal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LeatherPartsMat_OcclusionRoughMetal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_BaseColor.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_Normal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_LensesMat_OcclusionRoughMetal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_BaseColor.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_Normal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_MetalPartsMat_OcclusionRoughMetal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_BaseColor.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_Normal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/FlightHelmet_Materials_RubberWoodMat_OcclusionRoughMetal.png +0 -0
- package/dist/website/models/gltf/flight_helmet/index.bin +0 -0
- package/dist/website/models/gltf/flight_helmet/index.gltf +705 -0
- package/dist/website/models/gltf/object.gltf +23 -0
- package/dist/website/models/gltf/pirate_girl/index.bin +0 -0
- package/dist/website/models/gltf/pirate_girl/index.gltf +2082 -0
- package/dist/website/models/obj/pirate_girl/pirate_girl.obj +18459 -0
- package/dist/website/models/obj/pirate_girl/pirate_girl.png +0 -0
- package/env.d.ts +0 -14
- package/examples/camera/orthographic.js +2 -1
- package/examples/camera/perspective.js +2 -1
- package/examples/camera/tone_mapping.js +187 -0
- package/examples/lights/directional.js +2 -3
- package/examples/lights/multiple_spot_lights.js +2 -1
- package/examples/lights/point.js +2 -3
- package/examples/lights/spot.js +2 -3
- package/examples/loader/{gltfloader.js → gltf/basic.js} +8 -8
- package/examples/loader/gltf/material/alpha-blend-mode.js +80 -0
- package/examples/loader/{gltf_material.js → gltf/material/flight_helmet.js} +8 -8
- package/examples/loader/gltf/material/iridescence_lamp.js +118 -0
- package/examples/loader/objloader.js +9 -11
- package/examples/material/cullface.js +8 -7
- package/examples/material/materials.js +9 -11
- package/examples/material/standard/basic.js +3 -6
- package/examples/material/standard/reflection_refraction.js +190 -0
- package/examples/mesh/capsule.js +122 -0
- package/examples/mesh/circle.js +11 -6
- package/examples/mesh/cone.js +129 -0
- package/examples/mesh/cuboid.js +11 -6
- package/examples/mesh/cylinder.js +11 -6
- package/examples/mesh/dodecahedron.js +107 -0
- package/examples/mesh/ellipsoid.js +128 -0
- package/examples/mesh/extrude.js +118 -0
- package/examples/mesh/frustum.js +134 -0
- package/examples/mesh/helix.js +133 -0
- package/examples/mesh/icosahedron.js +107 -0
- package/examples/mesh/icosphere.js +112 -0
- package/examples/mesh/lathe.js +124 -0
- package/examples/mesh/meshTopology.js +64 -8
- package/examples/mesh/octahedron.js +107 -0
- package/examples/mesh/plane.js +11 -6
- package/examples/mesh/prism.js +122 -0
- package/examples/mesh/pyramid.js +121 -0
- package/examples/mesh/ring.js +122 -0
- package/examples/mesh/skinning.js +8 -8
- package/examples/mesh/terrain.js +163 -0
- package/examples/mesh/tetrahedron.js +107 -0
- package/examples/mesh/torus-knot.js +137 -0
- package/examples/mesh/torus.js +122 -0
- package/examples/mesh/tube.js +129 -0
- package/examples/mesh/uvsphere.js +11 -6
- package/examples/mesh/wedge.js +117 -0
- package/examples/other/rotatingCube.js +9 -11
- package/examples/other/rotatingSphere.js +9 -12
- package/examples/rendertarget/basic_canvas.js +5 -8
- package/examples/rendertarget/depth_texture.js +18 -11
- package/examples/rendertarget/image_target.js +19 -14
- package/examples/rendertarget/multiple_views.js +11 -16
- package/examples/rendertarget/render_masks.js +3 -5
- package/examples/rendertarget/split_screen.js +18 -14
- package/examples/rendertarget/split_view.js +12 -9
- package/examples/skybox/skybox.js +8 -8
- package/examples/texture/arrays.js +9 -12
- package/examples/texture/textureWrap.js +8 -8
- package/examples/transform/propagation.js +9 -11
- package/package.json +18 -12
- package/src/caches/cache.js +121 -182
- package/src/caches/uniformbuffers.js +42 -53
- package/src/constants/texture.js +1 -1
- package/src/content.config.js +1 -0
- package/src/core/layouts/bindgroup.js +167 -0
- package/src/core/layouts/index.js +3 -1
- package/src/core/layouts/pipeline.js +165 -0
- package/src/core/layouts/uniformbuffer.js +11 -3
- package/src/core/resources/gpubuffer.js +23 -2
- package/src/core/resources/gpumesh.js +37 -4
- package/src/core/resources/gpusampler.js +43 -0
- package/src/core/resources/gputexture.js +31 -2
- package/src/core/resources/index.js +4 -2
- package/src/core/resources/newuniformbuffer.js +58 -0
- package/src/core/resources/webglshadermodule.js +58 -0
- package/src/core/shader.js +24 -11
- package/src/core/webgl/bindgroup.js +200 -42
- package/src/core/webgl/descriptors.js +87 -25
- package/src/core/webgl/gpuqueue.js +63 -0
- package/src/core/webgl/index.js +3 -0
- package/src/core/webgl/renderpassencoder.js +703 -16
- package/src/core/webgl/renderpipeline.js +38 -1
- package/src/core/webgl/utils.js +207 -57
- package/src/core/webgl/webglrenderdevice.js +77 -53
- package/src/loader/gltf.js +716 -166
- package/src/loader/texture.js +69 -16
- package/src/loader/utils.js +44 -0
- package/src/material/alphablend.js +21 -0
- package/src/material/basic.js +15 -1
- package/src/material/depth.js +15 -1
- package/src/material/index.js +1 -0
- package/src/material/lambert.js +15 -1
- package/src/material/material.js +49 -7
- package/src/material/normal.js +14 -0
- package/src/material/phong.js +16 -2
- package/src/material/raw.js +21 -2
- package/src/material/standard.js +90 -2
- package/src/mesh/attribute/attribute.js +2 -2
- package/src/mesh/attributedata/separate.js +2 -2
- package/src/mesh/builders/capsule.js +47 -0
- package/src/mesh/builders/cone.js +29 -0
- package/src/mesh/builders/dodecahedron.js +130 -0
- package/src/mesh/builders/ellipsoid.js +65 -0
- package/src/mesh/builders/extrude.js +136 -0
- package/src/mesh/builders/frustum.js +26 -0
- package/src/mesh/builders/geometry.js +121 -0
- package/src/mesh/builders/helix.js +35 -0
- package/src/mesh/builders/icosahedron.js +61 -0
- package/src/mesh/builders/icosphere.js +141 -0
- package/src/mesh/builders/index.js +22 -1
- package/src/mesh/builders/lathe.js +85 -0
- package/src/mesh/builders/octahedron.js +43 -0
- package/src/mesh/builders/polyhedron.js +121 -0
- package/src/mesh/builders/prism.js +19 -0
- package/src/mesh/builders/pyramid.js +90 -0
- package/src/mesh/builders/ring.js +109 -0
- package/src/mesh/builders/surfaces.js +69 -0
- package/src/mesh/builders/terrain.js +80 -0
- package/src/mesh/builders/tetrahedron.js +37 -0
- package/src/mesh/builders/torusknot.js +36 -0
- package/src/mesh/builders/tube.js +209 -0
- package/src/mesh/builders/utils.js +130 -2
- package/src/mesh/builders/wedge.js +22 -0
- package/src/mesh/mesh.js +12 -4
- package/src/objects/camera/camera.js +104 -1
- package/src/objects/light/ambient.js +22 -1
- package/src/objects/light/directional.js +23 -1
- package/src/objects/light/point.js +25 -1
- package/src/objects/light/spot.js +27 -1
- package/src/objects/mesh.js +1 -53
- package/src/plugins/camera/RenderTarget2DPool.js +94 -0
- package/src/plugins/camera/camera.js +14 -6
- package/src/plugins/camera/index.js +2 -0
- package/src/plugins/camera/nodes/canvasblit.js +153 -0
- package/src/plugins/camera/nodes/index.js +4 -1
- package/src/plugins/camera/nodes/opaquepass.js +48 -33
- package/src/plugins/camera/nodes/tonemapping.js +217 -0
- package/src/plugins/camera/nodes/transparentpass.js +87 -0
- package/src/plugins/camera/nodes/{cameraview.js → view.js} +27 -1
- package/src/plugins/camera/resources/cameracolortargets.js +66 -0
- package/src/plugins/camera/resources/canvasblitpipeline.js +6 -0
- package/src/plugins/camera/resources/index.js +3 -0
- package/src/plugins/camera/resources/tonemappingpipeline.js +6 -0
- package/src/plugins/light/light.js +0 -4
- package/src/plugins/meshmaterial/meshmaterial.js +433 -170
- package/src/plugins/meshmaterial/nodes/meshmaterial.js +39 -6
- package/src/plugins/meshmaterial/resources/bonetexture.js +176 -0
- package/src/plugins/meshmaterial/resources/environmentmap.js +44 -0
- package/src/plugins/meshmaterial/resources/index.js +4 -0
- package/src/plugins/meshmaterial/resources/materialuniformbuffers.js +94 -0
- package/src/plugins/meshmaterial/resources/meshmaterialbindgroups.js +40 -0
- package/src/plugins/shadow/nodes/index.js +3 -3
- package/src/plugins/shadow/nodes/{shadowOccluder.js → occluder.js} +13 -19
- package/src/plugins/shadow/nodes/opaquePass.js +110 -0
- package/src/plugins/shadow/shadow.js +0 -5
- package/src/plugins/skeletonhelper/skeletonhelper.js +111 -69
- package/src/plugins/skybox/nodes/skybox.js +230 -38
- package/src/plugins/skybox/resources/skyboxpipeline.js +10 -0
- package/src/plugins/skybox/skybox.js +0 -119
- package/src/renderer/core/index.js +135 -21
- package/src/renderer/plugin.js +0 -20
- package/src/renderer/renderer.js +28 -12
- package/src/rendertarget/canvastarget.js +8 -5
- package/src/rendertarget/image.js +3 -3
- package/src/shader/basicFragment.glsl +12 -4
- package/src/shader/basicVertex.glsl +26 -15
- package/src/shader/blitFragment.glsl +9 -0
- package/src/shader/common/common.glsl +35 -6
- package/src/shader/common/index.js +1 -0
- package/src/shader/common/tonemap.glsl +118 -0
- package/src/shader/debug/skeletonFragment.glsl +0 -2
- package/src/shader/debug/skeletonVertex.glsl +1 -3
- package/src/shader/depthFragment.glsl +0 -3
- package/src/shader/fullscreenVertex.glsl +26 -0
- package/src/shader/index.js +3 -0
- package/src/shader/lambertFragment.glsl +12 -4
- package/src/shader/normalFragment.glsl +0 -3
- package/src/shader/phongFragment.glsl +12 -4
- package/src/shader/skyboxFragment.glsl +5 -5
- package/src/shader/skyboxVertex.glsl +5 -4
- package/src/shader/standardFragment.glsl +74 -17
- package/src/shader/tonemappingFragment.glsl +37 -0
- package/src/texture/sampler.js +101 -21
- package/src/texture/texture.js +252 -48
- package/src/utils/index.js +15 -1
- package/{examples/addons → src/utils}/rendergraph_gui.js +5 -2
- package/examples/mesh/geometries.js +0 -108
- package/scripts/stage-chorama.mjs +0 -29
- package/src/core/resources/framebuffer.js +0 -127
- package/src/plugins/shadow/nodes/shadowOpaquePass.js +0 -73
- package/website/config/index.ts +0 -1
- package/website/config/navigation.ts +0 -53
- package/website/content.config.ts +0 -92
- package/website/layouts/DocLayout.astro +0 -501
- package/website/layouts/Example.astro +0 -91
- package/website/pages/examples/[...slug].astro +0 -77
- package/website/pages/examples/index.astro +0 -98
- package/website/pages/examples/samples/[...slug].astro +0 -17
- package/website/pages/guide/[slug].astro +0 -30
- package/website/pages/guide/index.astro +0 -21
- package/website/pages/index.astro +0 -9
- package/website/plugins/remark-link-base.js +0 -23
- package/website/utils/url.ts +0 -30
- /package/src/plugins/shadow/nodes/{shadow.js → view.js} +0 -0
package/src/loader/gltf.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**@import { LoadSettings } from './loader.js' */
|
|
2
2
|
import { Attribute, Mesh } from '../mesh/index.js';
|
|
3
|
-
import { StandardMaterial } from '../material/index.js';
|
|
4
|
-
import { MeshMaterial3D, Object3D, Skin } from '../objects/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
5
|
import { Loader } from './loader.js';
|
|
6
6
|
import { arrayBufferToJSON } from './utils.js';
|
|
7
7
|
import { Bone3D } from '../objects/bone.js';
|
|
@@ -10,9 +10,15 @@ import { SeparateAttributeData } from '../mesh/attributedata/separate.js';
|
|
|
10
10
|
import { TextureLoader } from './texture.js';
|
|
11
11
|
import { Texture, Sampler } from '../texture/index.js';
|
|
12
12
|
import { assert } from '../utils/index.js';
|
|
13
|
-
import {
|
|
13
|
+
import { CullFace, TextureFilter, TextureType, TextureWrap } from '../constants/index.js';
|
|
14
14
|
|
|
15
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
|
|
16
22
|
/**
|
|
17
23
|
* @extends {Loader<Object3D, GLTFLoadSettings>}
|
|
18
24
|
*/
|
|
@@ -37,6 +43,8 @@ export class GLTFLoader extends Loader {
|
|
|
37
43
|
|
|
38
44
|
/**@type {Map<number, Object3D>} */
|
|
39
45
|
const entityMap = new Map()
|
|
46
|
+
/** @type {Map<number, DirectionalLight | PointLight | SpotLight>} */
|
|
47
|
+
const lightCache = new Map()
|
|
40
48
|
const baseUrl = new URL(path, location.href).href
|
|
41
49
|
const gltf = await loadGLTF(buffer, baseUrl)
|
|
42
50
|
const scene = gltf.scenes[gltf.scene]
|
|
@@ -52,47 +60,32 @@ export class GLTFLoader extends Loader {
|
|
|
52
60
|
})
|
|
53
61
|
}
|
|
54
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
|
+
|
|
55
83
|
throw "Unsupported gltf image setting"
|
|
56
84
|
}))
|
|
57
|
-
const samplers = gltf.samplers.map((gltfSampler) =>
|
|
58
|
-
const sampler = new Sampler()
|
|
59
|
-
sampler.magnificationFilter = gltfSampler.magFilter || TextureFilter.Nearest
|
|
60
|
-
sampler.wrapS = gltfSampler.wrapS || TextureWrap.Clamp
|
|
61
|
-
sampler.wrapT = gltfSampler.wrapT || TextureWrap.Clamp
|
|
62
|
-
|
|
63
|
-
switch (gltfSampler.minFilter) {
|
|
64
|
-
case WebGL2RenderingContext.NEAREST:
|
|
65
|
-
sampler.minificationFilter = TextureFilter.Nearest
|
|
66
|
-
sampler.mipmapFilter = undefined
|
|
67
|
-
break;
|
|
68
|
-
case WebGL2RenderingContext.LINEAR:
|
|
69
|
-
sampler.minificationFilter = TextureFilter.Linear
|
|
70
|
-
sampler.mipmapFilter = undefined
|
|
71
|
-
break;
|
|
72
|
-
case WebGL2RenderingContext.NEAREST_MIPMAP_NEAREST:
|
|
73
|
-
sampler.minificationFilter = TextureFilter.Nearest
|
|
74
|
-
sampler.mipmapFilter = TextureFilter.Nearest
|
|
75
|
-
break;
|
|
76
|
-
case WebGL2RenderingContext.NEAREST_MIPMAP_LINEAR:
|
|
77
|
-
sampler.minificationFilter = TextureFilter.Nearest
|
|
78
|
-
sampler.mipmapFilter = TextureFilter.Linear
|
|
79
|
-
break;
|
|
80
|
-
case WebGL2RenderingContext.LINEAR_MIPMAP_NEAREST:
|
|
81
|
-
sampler.minificationFilter = TextureFilter.Linear
|
|
82
|
-
sampler.mipmapFilter = TextureFilter.Nearest
|
|
83
|
-
break;
|
|
84
|
-
case WebGL2RenderingContext.LINEAR_MIPMAP_LINEAR:
|
|
85
|
-
sampler.minificationFilter = TextureFilter.Linear
|
|
86
|
-
sampler.mipmapFilter = TextureFilter.Linear
|
|
87
|
-
break;
|
|
88
|
-
default:
|
|
89
|
-
throw 'GLTF: Invalid minification sampler';
|
|
90
|
-
}
|
|
91
|
-
return sampler
|
|
92
|
-
})
|
|
85
|
+
const samplers = gltf.samplers.map((gltfSampler) => createGLTFSampler(gltfSampler))
|
|
93
86
|
|
|
94
87
|
/**
|
|
95
|
-
* @type {[Texture, Sampler
|
|
88
|
+
* @type {[Texture, Sampler][]}
|
|
96
89
|
*/
|
|
97
90
|
const textures = gltf.textures.map((gltfTextures) => {
|
|
98
91
|
const image = images[gltfTextures.source]
|
|
@@ -100,17 +93,16 @@ export class GLTFLoader extends Loader {
|
|
|
100
93
|
|
|
101
94
|
if (gltfTextures.sampler !== undefined) {
|
|
102
95
|
const sampler = samplers[gltfTextures.sampler]
|
|
103
|
-
|
|
104
|
-
image.generateMipmaps = true
|
|
105
|
-
}
|
|
96
|
+
assert(sampler, "GLTF texture does not have a valid sampler")
|
|
106
97
|
return [image, sampler]
|
|
107
98
|
}
|
|
108
|
-
return [image,
|
|
99
|
+
return [image, createDefaultGLTFSampler()]
|
|
109
100
|
})
|
|
110
101
|
const materials = gltf.materials.map((gltfMaterial) => {
|
|
111
102
|
const {
|
|
112
103
|
emissiveFactor,
|
|
113
104
|
emissiveTexture,
|
|
105
|
+
extensions = {},
|
|
114
106
|
normalTexture,
|
|
115
107
|
occlusionTexture,
|
|
116
108
|
pbrMetallicRoughness
|
|
@@ -196,6 +188,57 @@ export class GLTFLoader extends Loader {
|
|
|
196
188
|
}
|
|
197
189
|
}
|
|
198
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
|
+
|
|
199
242
|
return material
|
|
200
243
|
})
|
|
201
244
|
const geometries = gltf.meshes.map((data) => {
|
|
@@ -203,7 +246,7 @@ export class GLTFLoader extends Loader {
|
|
|
203
246
|
})
|
|
204
247
|
|
|
205
248
|
gltf.nodes.forEach((node, index) => {
|
|
206
|
-
const object = parseObject(index, node, gltf, geometries, materials)
|
|
249
|
+
const object = parseObject(index, node, gltf, geometries, materials, lightCache)
|
|
207
250
|
|
|
208
251
|
if (object) {
|
|
209
252
|
entityMap.set(index, object)
|
|
@@ -274,9 +317,10 @@ export class GLTFLoader extends Loader {
|
|
|
274
317
|
* @param {string} baseUrl
|
|
275
318
|
*/
|
|
276
319
|
async function loadGLTF(data, baseUrl) {
|
|
277
|
-
const json =
|
|
278
|
-
const
|
|
279
|
-
|
|
320
|
+
const { json, buffers: embeddedBuffers } = parseGLTFAsset(data)
|
|
321
|
+
const buffers = json.buffers instanceof Array
|
|
322
|
+
? await loadBuffers(baseUrl, json.buffers, embeddedBuffers)
|
|
323
|
+
: embeddedBuffers
|
|
280
324
|
const gltf = GLTF.deserialize(json)
|
|
281
325
|
gltf.buffers = buffers
|
|
282
326
|
|
|
@@ -285,12 +329,30 @@ async function loadGLTF(data, baseUrl) {
|
|
|
285
329
|
|
|
286
330
|
/**
|
|
287
331
|
* @param {string} base
|
|
288
|
-
* @param {{uri
|
|
332
|
+
* @param {{uri?: string; byteLength?: number;}[]} uris
|
|
333
|
+
* @param {ArrayBuffer[]} [embeddedBuffers]
|
|
289
334
|
*/
|
|
290
|
-
async function loadBuffers(base, uris) {
|
|
335
|
+
async function loadBuffers(base, uris, embeddedBuffers = []) {
|
|
336
|
+
if (uris.length === 0) {
|
|
337
|
+
return embeddedBuffers.slice()
|
|
338
|
+
}
|
|
291
339
|
|
|
292
340
|
return Promise.all(
|
|
293
|
-
uris.map(async (buffer) => {
|
|
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
|
+
|
|
294
356
|
const url = buffer.uri.startsWith('data') ?
|
|
295
357
|
buffer.uri :
|
|
296
358
|
new URL(buffer.uri, base).href
|
|
@@ -300,6 +362,205 @@ async function loadBuffers(base, uris) {
|
|
|
300
362
|
})
|
|
301
363
|
)
|
|
302
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
|
+
}
|
|
303
564
|
/**
|
|
304
565
|
* @typedef {LoadSettings} GLTFLoadSettings
|
|
305
566
|
*/
|
|
@@ -357,6 +618,18 @@ class GLTF {
|
|
|
357
618
|
* @type {GLTFMetaData}
|
|
358
619
|
*/
|
|
359
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 = []
|
|
360
633
|
|
|
361
634
|
/**
|
|
362
635
|
* @param {GLTFMetaData} meta
|
|
@@ -380,7 +653,10 @@ class GLTF {
|
|
|
380
653
|
bufferViews,
|
|
381
654
|
accessors,
|
|
382
655
|
asset,
|
|
383
|
-
skins
|
|
656
|
+
skins,
|
|
657
|
+
extensions,
|
|
658
|
+
extensionsUsed,
|
|
659
|
+
extensionsRequired
|
|
384
660
|
} = data
|
|
385
661
|
|
|
386
662
|
if (
|
|
@@ -428,6 +704,24 @@ class GLTF {
|
|
|
428
704
|
} else {
|
|
429
705
|
gltf.skins = []
|
|
430
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
|
+
}
|
|
431
725
|
return gltf
|
|
432
726
|
}
|
|
433
727
|
}
|
|
@@ -698,7 +992,7 @@ class GLTFMaterial {
|
|
|
698
992
|
/**
|
|
699
993
|
* @type {boolean}
|
|
700
994
|
*/
|
|
701
|
-
|
|
995
|
+
doubleSided = false
|
|
702
996
|
|
|
703
997
|
/**
|
|
704
998
|
* @param {GLTFPBRetallicRoughness} metallicRoughness
|
|
@@ -718,7 +1012,7 @@ class GLTFMaterial {
|
|
|
718
1012
|
emissiveFactor,
|
|
719
1013
|
alphaMode,
|
|
720
1014
|
alphaCutoff,
|
|
721
|
-
|
|
1015
|
+
doubleSided,
|
|
722
1016
|
name,
|
|
723
1017
|
extensions,
|
|
724
1018
|
extras
|
|
@@ -750,8 +1044,8 @@ class GLTFMaterial {
|
|
|
750
1044
|
result.alphaMode = alphaMode
|
|
751
1045
|
}
|
|
752
1046
|
|
|
753
|
-
if (typeof
|
|
754
|
-
result.
|
|
1047
|
+
if (typeof doubleSided === 'boolean') {
|
|
1048
|
+
result.doubleSided = doubleSided
|
|
755
1049
|
}
|
|
756
1050
|
|
|
757
1051
|
if (typeof name === "string") {
|
|
@@ -979,7 +1273,7 @@ class GLFTTexture {
|
|
|
979
1273
|
|
|
980
1274
|
class GLTFSkin {
|
|
981
1275
|
/**
|
|
982
|
-
* @type {number}
|
|
1276
|
+
* @type {number | undefined}
|
|
983
1277
|
*/
|
|
984
1278
|
inverseBindMatrices
|
|
985
1279
|
|
|
@@ -1008,8 +1302,11 @@ class GLTFSkin {
|
|
|
1008
1302
|
static deserialize(data) {
|
|
1009
1303
|
const { joints, inverseBindMatrices, skeleton } = data
|
|
1010
1304
|
|
|
1305
|
+
if (!(joints instanceof Array)) {
|
|
1306
|
+
throw 'Invalid skin'
|
|
1307
|
+
}
|
|
1011
1308
|
if (
|
|
1012
|
-
|
|
1309
|
+
inverseBindMatrices !== undefined &&
|
|
1013
1310
|
typeof inverseBindMatrices !== 'number'
|
|
1014
1311
|
) {
|
|
1015
1312
|
throw 'Invalid skin'
|
|
@@ -1692,12 +1989,12 @@ class TRSTransform {
|
|
|
1692
1989
|
|
|
1693
1990
|
class MatrixTransform {
|
|
1694
1991
|
/**
|
|
1695
|
-
* @type {
|
|
1992
|
+
* @type {Affine3}
|
|
1696
1993
|
*/
|
|
1697
1994
|
value
|
|
1698
1995
|
|
|
1699
1996
|
/**
|
|
1700
|
-
* @param {
|
|
1997
|
+
* @param {Affine3} value
|
|
1701
1998
|
*/
|
|
1702
1999
|
constructor(value) {
|
|
1703
2000
|
this.value = value
|
|
@@ -1717,7 +2014,11 @@ class MatrixTransform {
|
|
|
1717
2014
|
if (t.length !== 16) {
|
|
1718
2015
|
throw "invalid matrix transform"
|
|
1719
2016
|
}
|
|
1720
|
-
const transform = new MatrixTransform(
|
|
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
|
+
))
|
|
1721
2022
|
|
|
1722
2023
|
return transform
|
|
1723
2024
|
}
|
|
@@ -1742,10 +2043,37 @@ function getAccessorData(index, gltf) {
|
|
|
1742
2043
|
|
|
1743
2044
|
if (!buffer) throw "Invalid access to buffer"
|
|
1744
2045
|
|
|
1745
|
-
const
|
|
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
|
+
}
|
|
1746
2074
|
|
|
1747
2075
|
return [
|
|
1748
|
-
new DataView(
|
|
2076
|
+
new DataView(packedBuffer),
|
|
1749
2077
|
accessor
|
|
1750
2078
|
]
|
|
1751
2079
|
}
|
|
@@ -1755,6 +2083,10 @@ function getAccessorData(index, gltf) {
|
|
|
1755
2083
|
* @param {DataView} view
|
|
1756
2084
|
*/
|
|
1757
2085
|
function mapToIndices(accessor, view) {
|
|
2086
|
+
if (accessor.type !== GLTFAccessorType.Scalar) {
|
|
2087
|
+
throw "Indices provided on a mesh is not valid"
|
|
2088
|
+
}
|
|
2089
|
+
|
|
1758
2090
|
switch (accessor.componentType) {
|
|
1759
2091
|
case GLTFComponentType.UnsignedByte:
|
|
1760
2092
|
return new Uint8Array(
|
|
@@ -1779,6 +2111,163 @@ function mapToIndices(accessor, view) {
|
|
|
1779
2111
|
}
|
|
1780
2112
|
}
|
|
1781
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
|
+
|
|
1782
2271
|
/**
|
|
1783
2272
|
* @param {string} name
|
|
1784
2273
|
* @param {GLTFAccessor} accessor
|
|
@@ -1786,48 +2275,56 @@ function mapToIndices(accessor, view) {
|
|
|
1786
2275
|
* @returns {[string, DataView] | undefined}
|
|
1787
2276
|
*/
|
|
1788
2277
|
function mapAccessorTypeToAttribute(name, accessor, buffer) {
|
|
1789
|
-
const { componentType: type } = accessor
|
|
1790
2278
|
switch (name) {
|
|
1791
2279
|
case GLTFAttributeName.Position:
|
|
1792
|
-
if (type !==
|
|
2280
|
+
if (accessor.type !== GLTFAccessorType.Vec3 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
|
|
1793
2281
|
throw "Attribute types do not match"
|
|
1794
|
-
return [Attribute.Position.name, buffer]
|
|
2282
|
+
return [Attribute.Position.name, convertAccessorToFloat32(accessor, buffer, 3)]
|
|
1795
2283
|
case GLTFAttributeName.TexCoord0:
|
|
1796
|
-
if (
|
|
2284
|
+
if (
|
|
2285
|
+
accessor.type !== GLTFAccessorType.Vec2 ||
|
|
2286
|
+
(!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
|
|
2287
|
+
(accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
|
|
2288
|
+
)
|
|
1797
2289
|
throw "Attribute types do not match"
|
|
1798
|
-
return [Attribute.UV.name, buffer]
|
|
2290
|
+
return [Attribute.UV.name, convertAccessorToFloat32(accessor, buffer, 2)]
|
|
1799
2291
|
case GLTFAttributeName.TexCoord1:
|
|
1800
|
-
if (
|
|
2292
|
+
if (
|
|
2293
|
+
accessor.type !== GLTFAccessorType.Vec2 ||
|
|
2294
|
+
(!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
|
|
2295
|
+
(accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
|
|
2296
|
+
)
|
|
1801
2297
|
throw "Attribute types do not match"
|
|
1802
|
-
return [Attribute.UVB.name, buffer]
|
|
2298
|
+
return [Attribute.UVB.name, convertAccessorToFloat32(accessor, buffer, 2)]
|
|
1803
2299
|
case GLTFAttributeName.Normal:
|
|
1804
|
-
if (type !==
|
|
2300
|
+
if (accessor.type !== GLTFAccessorType.Vec3 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
|
|
1805
2301
|
throw "Attribute types do not match"
|
|
1806
|
-
return [Attribute.Normal.name, buffer]
|
|
2302
|
+
return [Attribute.Normal.name, convertAccessorToFloat32(accessor, buffer, 3)]
|
|
1807
2303
|
case GLTFAttributeName.Tangent:
|
|
1808
|
-
if (type !==
|
|
2304
|
+
if (accessor.type !== GLTFAccessorType.Vec4 || accessor.componentType !== GLTFComponentType.Float || accessor.normalized)
|
|
1809
2305
|
throw "Attribute types do not match"
|
|
1810
|
-
return [Attribute.Tangent.name, buffer]
|
|
2306
|
+
return [Attribute.Tangent.name, convertAccessorToFloat32(accessor, buffer, 4)]
|
|
1811
2307
|
case GLTFAttributeName.Color0:
|
|
1812
|
-
if (
|
|
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
|
+
)
|
|
1813
2313
|
throw "Attribute types do not match"
|
|
1814
|
-
return [Attribute.Color.name, buffer]
|
|
2314
|
+
return [Attribute.Color.name, convertAccessorToFloat32(accessor, buffer, 4, accessor.type === GLTFAccessorType.Vec3)]
|
|
1815
2315
|
case GLTFAttributeName.Weights0:
|
|
1816
|
-
if (
|
|
2316
|
+
if (
|
|
2317
|
+
accessor.type !== GLTFAccessorType.Vec4 ||
|
|
2318
|
+
(!isFloatComponentType(accessor.componentType) && !isNormalizedIntegerComponentType(accessor.componentType)) ||
|
|
2319
|
+
(accessor.componentType === GLTFComponentType.Float ? accessor.normalized : !accessor.normalized)
|
|
2320
|
+
)
|
|
1817
2321
|
throw "Attribute types do not match"
|
|
1818
|
-
return [Attribute.JointWeight.name, buffer]
|
|
2322
|
+
return [Attribute.JointWeight.name, convertAccessorToFloat32(accessor, buffer, 4)]
|
|
1819
2323
|
case GLTFAttributeName.Joints0:
|
|
1820
|
-
if (type
|
|
1821
|
-
return [Attribute.JointIndex.name, buffer]
|
|
1822
|
-
} else if (type === GlDataType.UnsignedByte) {
|
|
1823
|
-
const newBuffer = widenTypedArray(
|
|
1824
|
-
new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength),
|
|
1825
|
-
Uint16Array
|
|
1826
|
-
)
|
|
1827
|
-
return [Attribute.JointIndex.name, new DataView(newBuffer.buffer)]
|
|
1828
|
-
} else {
|
|
2324
|
+
if (accessor.type !== GLTFAccessorType.Vec4 || accessor.normalized || !isJointComponentType(accessor.componentType)) {
|
|
1829
2325
|
throw "Attribute types do not match"
|
|
1830
2326
|
}
|
|
2327
|
+
return [Attribute.JointIndex.name, convertAccessorToUint16(accessor, buffer)]
|
|
1831
2328
|
default:
|
|
1832
2329
|
return undefined;
|
|
1833
2330
|
}
|
|
@@ -1926,6 +2423,7 @@ function parseGeometry(gltfMesh, gltf) {
|
|
|
1926
2423
|
const primitive = /**@type {GLTFPrimitive} */ (gltfMesh.primitives[i])
|
|
1927
2424
|
const attributes = new SeparateAttributeData()
|
|
1928
2425
|
const mesh = new Mesh(attributes)
|
|
2426
|
+
mesh.topology = primitive.mode
|
|
1929
2427
|
if (primitive.indices !== undefined) {
|
|
1930
2428
|
const [dataView, accessor] = getAccessorData(
|
|
1931
2429
|
primitive.indices,
|
|
@@ -1960,11 +2458,27 @@ function parseGeometry(gltfMesh, gltf) {
|
|
|
1960
2458
|
* @returns {Skin}
|
|
1961
2459
|
*/
|
|
1962
2460
|
function parseSkin(gltfSkin, gltf, entityMap) {
|
|
1963
|
-
|
|
1964
|
-
|
|
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
|
+
|
|
1965
2479
|
const skin = new Skin()
|
|
1966
2480
|
|
|
1967
|
-
if (bindPose.length !== gltfSkin.joints.length) {
|
|
2481
|
+
if (gltfSkin.inverseBindMatrices !== undefined && bindPose.length !== gltfSkin.joints.length) {
|
|
1968
2482
|
console.warn(
|
|
1969
2483
|
"GLTF skin inverse bind matrix count does not match joint count",
|
|
1970
2484
|
bindPose.length,
|
|
@@ -1972,7 +2486,7 @@ function parseSkin(gltfSkin, gltf, entityMap) {
|
|
|
1972
2486
|
)
|
|
1973
2487
|
}
|
|
1974
2488
|
|
|
1975
|
-
skin.inverseBindPose = bindPose
|
|
2489
|
+
skin.inverseBindPose = gltfSkin.joints.map((_, index) => bindPose[index] || new Affine3())
|
|
1976
2490
|
skin.bones = gltfSkin.joints.map(joint => {
|
|
1977
2491
|
const entity = entityMap.get(joint)
|
|
1978
2492
|
if (!(entity instanceof Bone3D)) {
|
|
@@ -2019,18 +2533,28 @@ function convertToInverseBindPose(poseData) {
|
|
|
2019
2533
|
* @param {GLTF} gltf
|
|
2020
2534
|
* @param {[Mesh,number | undefined][][]} geometries
|
|
2021
2535
|
* @param {StandardMaterial[]} materials
|
|
2536
|
+
* @param {Map<number, DirectionalLight | PointLight | SpotLight>} lightCache
|
|
2022
2537
|
*/
|
|
2023
|
-
function parseObject(index, node, gltf, geometries, materials) {
|
|
2538
|
+
function parseObject(index, node, gltf, geometries, materials, lightCache) {
|
|
2024
2539
|
const { mesh, transform, name } = node
|
|
2540
|
+
const light = parseGLTFLight(node, gltf, lightCache)
|
|
2025
2541
|
|
|
2026
2542
|
let object
|
|
2027
2543
|
if (mesh !== undefined) {
|
|
2028
2544
|
object = parseMeshObject(mesh, gltf.meshes, geometries, materials)
|
|
2545
|
+
if (light) {
|
|
2546
|
+
object.add(light)
|
|
2547
|
+
}
|
|
2029
2548
|
} else {
|
|
2030
2549
|
const bone = parseBone(index, gltf)
|
|
2031
2550
|
|
|
2032
2551
|
if (bone) {
|
|
2033
2552
|
object = bone
|
|
2553
|
+
if (light) {
|
|
2554
|
+
object.add(light)
|
|
2555
|
+
}
|
|
2556
|
+
} else if (light) {
|
|
2557
|
+
object = light
|
|
2034
2558
|
} else {
|
|
2035
2559
|
object = new Object3D()
|
|
2036
2560
|
}
|
|
@@ -2045,6 +2569,101 @@ function parseObject(index, node, gltf, geometries, materials) {
|
|
|
2045
2569
|
return object
|
|
2046
2570
|
}
|
|
2047
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
|
+
|
|
2048
2667
|
/**
|
|
2049
2668
|
* @param {number} index
|
|
2050
2669
|
* @param {GLTF} gltf
|
|
@@ -2080,7 +2699,11 @@ function transferTransform(object, transform) {
|
|
|
2080
2699
|
object.transform.scale.z = transform.scale[2]
|
|
2081
2700
|
}
|
|
2082
2701
|
if (transform instanceof MatrixTransform) {
|
|
2083
|
-
|
|
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)
|
|
2084
2707
|
}
|
|
2085
2708
|
}
|
|
2086
2709
|
|
|
@@ -2090,79 +2713,6 @@ function transferTransform(object, transform) {
|
|
|
2090
2713
|
* @param {new (...args:any[])=>TypedArray} to
|
|
2091
2714
|
* @returns
|
|
2092
2715
|
*/
|
|
2093
|
-
function widenTypedArray(from, to) {
|
|
2094
|
-
const result = new to(from.length);
|
|
2095
|
-
for (let i = 0; i < from.length; i++) {
|
|
2096
|
-
result[i] = /**@type {bigint | number}*/(from[i])
|
|
2097
|
-
}
|
|
2098
|
-
return result;
|
|
2099
|
-
}
|
|
2100
|
-
|
|
2101
|
-
/**
|
|
2102
|
-
* @param {VertexFormat} vertexFormat
|
|
2103
|
-
*/
|
|
2104
|
-
function mapToGLTFComponentType(vertexFormat) {
|
|
2105
|
-
switch (vertexFormat) {
|
|
2106
|
-
case VertexFormat.Uint8:
|
|
2107
|
-
case VertexFormat.Uint8x2:
|
|
2108
|
-
case VertexFormat.Uint8x3:
|
|
2109
|
-
case VertexFormat.Uint8x4:
|
|
2110
|
-
case VertexFormat.Unorm8:
|
|
2111
|
-
case VertexFormat.Unorm8x2:
|
|
2112
|
-
case VertexFormat.Unorm8x3:
|
|
2113
|
-
case VertexFormat.Unorm8x4:
|
|
2114
|
-
return GLTFComponentType.UnsignedByte;
|
|
2115
|
-
case VertexFormat.Snorm8:
|
|
2116
|
-
case VertexFormat.Snorm8x2:
|
|
2117
|
-
case VertexFormat.Snorm8x3:
|
|
2118
|
-
case VertexFormat.Snorm8x4:
|
|
2119
|
-
case VertexFormat.Sint8:
|
|
2120
|
-
case VertexFormat.Sint8x2:
|
|
2121
|
-
case VertexFormat.Sint8x3:
|
|
2122
|
-
case VertexFormat.Sint8x4:
|
|
2123
|
-
return GLTFComponentType.Byte;
|
|
2124
|
-
case VertexFormat.Uint16:
|
|
2125
|
-
case VertexFormat.Uint16x2:
|
|
2126
|
-
case VertexFormat.Uint16x3:
|
|
2127
|
-
case VertexFormat.Uint16x4:
|
|
2128
|
-
case VertexFormat.Unorm16:
|
|
2129
|
-
case VertexFormat.Unorm16x2:
|
|
2130
|
-
case VertexFormat.Unorm16x3:
|
|
2131
|
-
case VertexFormat.Unorm16x4:
|
|
2132
|
-
return GLTFComponentType.UnsignedShort;
|
|
2133
|
-
case VertexFormat.Snorm16:
|
|
2134
|
-
case VertexFormat.Snorm16x2:
|
|
2135
|
-
case VertexFormat.Snorm16x3:
|
|
2136
|
-
case VertexFormat.Snorm16x4:
|
|
2137
|
-
case VertexFormat.Sint16:
|
|
2138
|
-
case VertexFormat.Sint16x2:
|
|
2139
|
-
case VertexFormat.Sint16x3:
|
|
2140
|
-
case VertexFormat.Sint16x4:
|
|
2141
|
-
return GLTFComponentType.Short;
|
|
2142
|
-
// 32-bit unsigned ints
|
|
2143
|
-
case VertexFormat.Uint32:
|
|
2144
|
-
case VertexFormat.Uint32x2:
|
|
2145
|
-
case VertexFormat.Uint32x3:
|
|
2146
|
-
case VertexFormat.Uint32x4:
|
|
2147
|
-
return GLTFComponentType.UnsignedInt;
|
|
2148
|
-
|
|
2149
|
-
// 32-bit signed ints
|
|
2150
|
-
case VertexFormat.Sint32:
|
|
2151
|
-
case VertexFormat.Sint32x2:
|
|
2152
|
-
case VertexFormat.Sint32x3:
|
|
2153
|
-
case VertexFormat.Sint32x4:
|
|
2154
|
-
return GLTFComponentType.Int;
|
|
2155
|
-
case VertexFormat.Float32:
|
|
2156
|
-
case VertexFormat.Float32x2:
|
|
2157
|
-
case VertexFormat.Float32x3:
|
|
2158
|
-
case VertexFormat.Float32x4:
|
|
2159
|
-
return GLTFComponentType.Float
|
|
2160
|
-
default:
|
|
2161
|
-
throw new Error('Unsupported vertex format: ' + vertexFormat);
|
|
2162
|
-
}
|
|
2163
|
-
}
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
2716
|
/**
|
|
2167
2717
|
*@typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray
|
|
2168
2718
|
*/
|