@loaders.gl/gltf 4.0.0-alpha.1 → 4.0.0-alpha.11
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/dist/bundle.d.ts +2 -0
- package/dist/bundle.d.ts.map +1 -0
- package/dist/bundle.js +2 -2
- package/dist/dist.min.js +5767 -0
- package/dist/es5/bundle.js +6 -0
- package/dist/es5/bundle.js.map +1 -0
- package/dist/es5/glb-loader.js +56 -0
- package/dist/es5/glb-loader.js.map +1 -0
- package/dist/es5/glb-writer.js +34 -0
- package/dist/es5/glb-writer.js.map +1 -0
- package/dist/es5/gltf-loader.js +81 -0
- package/dist/es5/gltf-loader.js.map +1 -0
- package/dist/es5/gltf-writer.js +35 -0
- package/dist/es5/gltf-writer.js.map +1 -0
- package/dist/es5/index.js +55 -0
- package/dist/es5/index.js.map +1 -0
- package/dist/es5/lib/api/gltf-extensions.js +109 -0
- package/dist/es5/lib/api/gltf-extensions.js.map +1 -0
- package/dist/es5/lib/api/gltf-scenegraph.js +598 -0
- package/dist/es5/lib/api/gltf-scenegraph.js.map +1 -0
- package/dist/es5/lib/api/normalize-gltf-v1.js +331 -0
- package/dist/es5/lib/api/normalize-gltf-v1.js.map +1 -0
- package/dist/es5/lib/api/post-process-gltf.js +470 -0
- package/dist/es5/lib/api/post-process-gltf.js.map +1 -0
- package/dist/es5/lib/encoders/encode-glb.js +61 -0
- package/dist/es5/lib/encoders/encode-glb.js.map +1 -0
- package/dist/es5/lib/encoders/encode-gltf.js +20 -0
- package/dist/es5/lib/encoders/encode-gltf.js.map +1 -0
- package/dist/es5/lib/extensions/EXT_meshopt_compression.js +94 -0
- package/dist/es5/lib/extensions/EXT_meshopt_compression.js.map +1 -0
- package/dist/es5/lib/extensions/EXT_texture_webp.js +43 -0
- package/dist/es5/lib/extensions/EXT_texture_webp.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_binary_gltf.js +39 -0
- package/dist/es5/lib/extensions/KHR_binary_gltf.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js +247 -0
- package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_texture_basisu.js +36 -0
- package/dist/es5/lib/extensions/KHR_texture_basisu.js.map +1 -0
- package/dist/es5/lib/extensions/KHR_texture_transform.js +230 -0
- package/dist/es5/lib/extensions/KHR_texture_transform.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +281 -0
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js +99 -0
- package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js +76 -0
- package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
- package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js +113 -0
- package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
- package/dist/es5/lib/gltf-utils/get-typed-array.js +23 -0
- package/dist/es5/lib/gltf-utils/get-typed-array.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js +68 -0
- package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-constants.js +48 -0
- package/dist/es5/lib/gltf-utils/gltf-constants.js.map +1 -0
- package/dist/es5/lib/gltf-utils/gltf-utils.js +86 -0
- package/dist/es5/lib/gltf-utils/gltf-utils.js.map +1 -0
- package/dist/es5/lib/gltf-utils/resolve-url.js +18 -0
- package/dist/es5/lib/gltf-utils/resolve-url.js.map +1 -0
- package/dist/es5/lib/parsers/parse-glb.js +120 -0
- package/dist/es5/lib/parsers/parse-glb.js.map +1 -0
- package/dist/es5/lib/parsers/parse-gltf.js +280 -0
- package/dist/es5/lib/parsers/parse-gltf.js.map +1 -0
- package/dist/es5/lib/types/glb-types.js +2 -0
- package/dist/es5/lib/types/glb-types.js.map +1 -0
- package/dist/es5/lib/types/gltf-json-schema.js +2 -0
- package/dist/es5/lib/types/gltf-json-schema.js.map +1 -0
- package/dist/es5/lib/types/gltf-postprocessed-schema.js +2 -0
- package/dist/es5/lib/types/gltf-postprocessed-schema.js.map +1 -0
- package/dist/es5/lib/types/gltf-types.js +2 -0
- package/dist/es5/lib/types/gltf-types.js.map +1 -0
- package/dist/es5/lib/utils/assert.js +12 -0
- package/dist/es5/lib/utils/assert.js.map +1 -0
- package/dist/es5/lib/utils/version.js +9 -0
- package/dist/es5/lib/utils/version.js.map +1 -0
- package/dist/es5/meshopt/meshopt-decoder.js +215 -0
- package/dist/es5/meshopt/meshopt-decoder.js.map +1 -0
- package/dist/es5/webp/webp.js +83 -0
- package/dist/es5/webp/webp.js.map +1 -0
- package/dist/esm/bundle.js +4 -0
- package/dist/esm/bundle.js.map +1 -0
- package/dist/esm/glb-loader.js +31 -0
- package/dist/esm/glb-loader.js.map +1 -0
- package/dist/esm/glb-writer.js +27 -0
- package/dist/esm/glb-writer.js.map +1 -0
- package/dist/esm/gltf-loader.js +51 -0
- package/dist/esm/gltf-loader.js.map +1 -0
- package/dist/esm/gltf-writer.js +28 -0
- package/dist/esm/gltf-writer.js.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lib/api/gltf-extensions.js +35 -0
- package/dist/esm/lib/api/gltf-extensions.js.map +1 -0
- package/dist/esm/lib/api/gltf-scenegraph.js +455 -0
- package/dist/esm/lib/api/gltf-scenegraph.js.map +1 -0
- package/dist/esm/lib/api/normalize-gltf-v1.js +204 -0
- package/dist/esm/lib/api/normalize-gltf-v1.js.map +1 -0
- package/dist/esm/lib/api/post-process-gltf.js +396 -0
- package/dist/esm/lib/api/post-process-gltf.js.map +1 -0
- package/dist/esm/lib/encoders/encode-glb.js +54 -0
- package/dist/esm/lib/encoders/encode-glb.js.map +1 -0
- package/dist/esm/lib/encoders/encode-gltf.js +14 -0
- package/dist/esm/lib/encoders/encode-gltf.js.map +1 -0
- package/dist/esm/lib/extensions/EXT_meshopt_compression.js +41 -0
- package/dist/esm/lib/extensions/EXT_meshopt_compression.js.map +1 -0
- package/dist/esm/lib/extensions/EXT_texture_webp.js +25 -0
- package/dist/esm/lib/extensions/EXT_texture_webp.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_binary_gltf.js +21 -0
- package/dist/esm/lib/extensions/KHR_binary_gltf.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js +111 -0
- package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_texture_basisu.js +18 -0
- package/dist/esm/lib/extensions/KHR_texture_basisu.js.map +1 -0
- package/dist/esm/lib/extensions/KHR_texture_transform.js +182 -0
- package/dist/esm/lib/extensions/KHR_texture_transform.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +223 -0
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js +42 -0
- package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js +33 -0
- package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
- package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js +61 -0
- package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
- package/dist/esm/lib/gltf-utils/get-typed-array.js +16 -0
- package/dist/esm/lib/gltf-utils/get-typed-array.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js +62 -0
- package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-constants.js +37 -0
- package/dist/esm/lib/gltf-utils/gltf-constants.js.map +1 -0
- package/dist/esm/lib/gltf-utils/gltf-utils.js +74 -0
- package/dist/esm/lib/gltf-utils/gltf-utils.js.map +1 -0
- package/dist/esm/lib/gltf-utils/resolve-url.js +12 -0
- package/dist/esm/lib/gltf-utils/resolve-url.js.map +1 -0
- package/dist/esm/lib/parsers/parse-glb.js +114 -0
- package/dist/esm/lib/parsers/parse-glb.js.map +1 -0
- package/dist/esm/lib/parsers/parse-gltf.js +151 -0
- package/dist/esm/lib/parsers/parse-gltf.js.map +1 -0
- package/dist/esm/lib/types/glb-types.js +2 -0
- package/dist/esm/lib/types/glb-types.js.map +1 -0
- package/dist/esm/lib/types/gltf-json-schema.js +2 -0
- package/dist/esm/lib/types/gltf-json-schema.js.map +1 -0
- package/dist/esm/lib/types/gltf-postprocessed-schema.js +2 -0
- package/dist/esm/lib/types/gltf-postprocessed-schema.js.map +1 -0
- package/dist/esm/lib/types/gltf-types.js +2 -0
- package/dist/esm/lib/types/gltf-types.js.map +1 -0
- package/dist/esm/lib/utils/assert.js +6 -0
- package/dist/esm/lib/utils/assert.js.map +1 -0
- package/dist/esm/lib/utils/version.js +2 -0
- package/dist/esm/lib/utils/version.js.map +1 -0
- package/dist/esm/meshopt/meshopt-decoder.js +91 -0
- package/dist/esm/meshopt/meshopt-decoder.js.map +1 -0
- package/dist/esm/meshopt/meshopt-encoder.ts.disabled +409 -0
- package/dist/esm/webp/webp.js +25 -0
- package/dist/esm/webp/webp.js.map +1 -0
- package/dist/glb-loader.d.ts +14 -0
- package/dist/glb-loader.d.ts.map +1 -0
- package/dist/glb-loader.js +30 -28
- package/dist/glb-writer.d.ts +13 -0
- package/dist/glb-writer.d.ts.map +1 -0
- package/dist/glb-writer.js +32 -26
- package/dist/gltf-loader.d.ts +19 -0
- package/dist/gltf-loader.d.ts.map +1 -0
- package/dist/gltf-loader.js +46 -43
- package/dist/gltf-writer.d.ts +25 -0
- package/dist/gltf-writer.d.ts.map +1 -0
- package/dist/gltf-writer.js +29 -26
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -7
- package/dist/lib/api/gltf-extensions.d.ts +31 -0
- package/dist/lib/api/gltf-extensions.d.ts.map +1 -0
- package/dist/lib/api/gltf-extensions.js +79 -31
- package/dist/lib/api/gltf-scenegraph.d.ts +180 -0
- package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -0
- package/dist/lib/api/gltf-scenegraph.js +578 -513
- package/dist/lib/api/normalize-gltf-v1.d.ts +2 -0
- package/dist/lib/api/normalize-gltf-v1.d.ts.map +1 -0
- package/dist/lib/api/normalize-gltf-v1.js +278 -226
- package/dist/lib/api/post-process-gltf.d.ts +5 -0
- package/dist/lib/api/post-process-gltf.d.ts.map +1 -0
- package/dist/lib/api/post-process-gltf.js +386 -345
- package/dist/lib/encoders/encode-glb.d.ts +15 -0
- package/dist/lib/encoders/encode-glb.d.ts.map +1 -0
- package/dist/lib/encoders/encode-glb.js +67 -58
- package/dist/lib/encoders/encode-gltf.d.ts +20 -0
- package/dist/lib/encoders/encode-gltf.d.ts.map +1 -0
- package/dist/lib/encoders/encode-gltf.js +30 -12
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts +7 -0
- package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -0
- package/dist/lib/extensions/EXT_meshopt_compression.js +35 -50
- package/dist/lib/extensions/EXT_texture_webp.d.ts +12 -0
- package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -0
- package/dist/lib/extensions/EXT_texture_webp.js +32 -28
- package/dist/lib/extensions/KHR_binary_gltf.d.ts +7 -0
- package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -0
- package/dist/lib/extensions/KHR_binary_gltf.js +36 -23
- package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +13 -0
- package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -0
- package/dist/lib/extensions/KHR_draco_mesh_compression.js +123 -111
- package/dist/lib/extensions/KHR_texture_basisu.d.ts +12 -0
- package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -0
- package/dist/lib/extensions/KHR_texture_basisu.js +26 -19
- package/dist/lib/extensions/KHR_texture_transform.d.ts +13 -0
- package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -0
- package/dist/lib/extensions/KHR_texture_transform.js +227 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +7 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -0
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +290 -0
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +7 -0
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -0
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +54 -44
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +7 -0
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -0
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +39 -32
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +7 -0
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -0
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +73 -62
- package/dist/lib/gltf-utils/get-typed-array.d.ts +3 -0
- package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -0
- package/dist/lib/gltf-utils/get-typed-array.js +39 -14
- package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +6 -0
- package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -0
- package/dist/lib/gltf-utils/gltf-attribute-utils.js +65 -65
- package/dist/lib/gltf-utils/gltf-constants.d.ts +21 -0
- package/dist/lib/gltf-utils/gltf-constants.d.ts.map +1 -0
- package/dist/lib/gltf-utils/gltf-constants.js +38 -32
- package/dist/lib/gltf-utils/gltf-utils.d.ts +15 -0
- package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -0
- package/dist/lib/gltf-utils/gltf-utils.js +79 -45
- package/dist/lib/gltf-utils/resolve-url.d.ts +2 -0
- package/dist/lib/gltf-utils/resolve-url.d.ts.map +1 -0
- package/dist/lib/gltf-utils/resolve-url.js +17 -15
- package/dist/lib/parsers/parse-glb.d.ts +20 -0
- package/dist/lib/parsers/parse-glb.d.ts.map +1 -0
- package/dist/lib/parsers/parse-glb.js +143 -105
- package/dist/lib/parsers/parse-gltf.d.ts +18 -0
- package/dist/lib/parsers/parse-gltf.d.ts.map +1 -0
- package/dist/lib/parsers/parse-gltf.js +164 -155
- package/dist/lib/types/glb-types.d.ts +17 -0
- package/dist/lib/types/glb-types.d.ts.map +1 -0
- package/dist/lib/types/glb-types.js +2 -2
- package/dist/lib/types/gltf-json-schema.d.ts +1119 -0
- package/dist/lib/types/gltf-json-schema.d.ts.map +1 -0
- package/dist/lib/types/gltf-json-schema.js +4 -2
- package/dist/lib/types/gltf-postprocessed-schema.d.ts +651 -0
- package/dist/lib/types/gltf-postprocessed-schema.d.ts.map +1 -0
- package/dist/lib/types/gltf-postprocessed-schema.js +4 -2
- package/dist/lib/types/gltf-types.d.ts +26 -0
- package/dist/lib/types/gltf-types.d.ts.map +1 -0
- package/dist/lib/types/gltf-types.js +3 -2
- package/dist/lib/utils/assert.d.ts +2 -0
- package/dist/lib/utils/assert.d.ts.map +1 -0
- package/dist/lib/utils/assert.js +11 -5
- package/dist/lib/utils/version.d.ts +2 -0
- package/dist/lib/utils/version.d.ts.map +1 -0
- package/dist/lib/utils/version.js +7 -2
- package/dist/meshopt/meshopt-decoder.d.ts +6 -0
- package/dist/meshopt/meshopt-decoder.d.ts.map +1 -0
- package/dist/meshopt/meshopt-decoder.js +97 -82
- package/dist/webp/webp.d.ts +7 -0
- package/dist/webp/webp.d.ts.map +1 -0
- package/dist/webp/webp.js +32 -23
- package/package.json +11 -13
- package/src/glb-loader.ts +4 -4
- package/src/glb-writer.ts +11 -3
- package/src/gltf-loader.ts +12 -8
- package/src/gltf-writer.ts +2 -2
- package/src/index.ts +34 -5
- package/src/lib/api/gltf-extensions.ts +14 -3
- package/src/lib/api/gltf-scenegraph.ts +68 -40
- package/src/lib/api/{normalize-gltf-v1.js → normalize-gltf-v1.ts} +20 -17
- package/src/lib/api/post-process-gltf.ts +556 -0
- package/src/lib/encoders/{encode-glb.js → encode-glb.ts} +21 -4
- package/src/lib/encoders/encode-gltf.ts +34 -0
- package/src/lib/extensions/EXT_meshopt_compression.ts +16 -25
- package/src/lib/extensions/EXT_texture_webp.ts +5 -4
- package/src/lib/extensions/KHR_binary_gltf.ts +3 -3
- package/src/lib/extensions/KHR_draco_mesh_compression.ts +8 -8
- package/src/lib/extensions/KHR_texture_basisu.ts +6 -5
- package/src/lib/extensions/KHR_texture_transform.ts +308 -0
- package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +404 -0
- package/src/lib/extensions/deprecated/KHR_lights_punctual.ts +2 -2
- package/src/lib/extensions/deprecated/KHR_materials_unlit.ts +5 -5
- package/src/lib/extensions/deprecated/KHR_techniques_webgl.ts +3 -2
- package/src/lib/gltf-utils/gltf-attribute-utils.ts +1 -1
- package/src/lib/gltf-utils/gltf-constants.ts +2 -2
- package/src/lib/gltf-utils/gltf-utils.ts +31 -0
- package/src/lib/parsers/parse-glb.ts +50 -24
- package/src/lib/parsers/parse-gltf.ts +38 -28
- package/src/lib/types/glb-types.ts +1 -1
- package/src/lib/types/gltf-json-schema.ts +585 -126
- package/src/lib/types/gltf-postprocessed-schema.ts +290 -212
- package/src/lib/types/gltf-types.ts +43 -43
- package/src/lib/utils/{assert.js → assert.ts} +1 -1
- package/dist/bundle.js.map +0 -1
- package/dist/glb-loader.js.map +0 -1
- package/dist/glb-writer.js.map +0 -1
- package/dist/gltf-loader.js.map +0 -1
- package/dist/gltf-writer.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/api/gltf-extensions.js.map +0 -1
- package/dist/lib/api/gltf-scenegraph.js.map +0 -1
- package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
- package/dist/lib/api/post-process-gltf.js.map +0 -1
- package/dist/lib/encoders/encode-glb.js.map +0 -1
- package/dist/lib/encoders/encode-gltf.js.map +0 -1
- package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
- package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
- package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
- package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
- package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
- package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
- package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
- package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
- package/dist/lib/parsers/parse-glb.js.map +0 -1
- package/dist/lib/parsers/parse-gltf.js.map +0 -1
- package/dist/lib/types/glb-types.js.map +0 -1
- package/dist/lib/types/gltf-json-schema.js.map +0 -1
- package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
- package/dist/lib/types/gltf-types.js.map +0 -1
- package/dist/lib/utils/assert.js.map +0 -1
- package/dist/lib/utils/version.js.map +0 -1
- package/dist/meshopt/meshopt-decoder.js.map +0 -1
- package/dist/webp/webp.js.map +0 -1
- package/src/lib/api/post-process-gltf.js +0 -388
- package/src/lib/encoders/encode-gltf.js +0 -25
- /package/dist/{meshopt → es5/meshopt}/meshopt-encoder.ts.disabled +0 -0
- /package/src/lib/utils/{version.js → version.ts} +0 -0
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
|
|
3
|
+
import type {GLTFWithBuffers} from '../types/gltf-types';
|
|
4
|
+
import type {ParseGLTFOptions} from '../parsers/parse-gltf';
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
GLTF,
|
|
8
|
+
GLTFAccessor,
|
|
9
|
+
GLTFBufferView,
|
|
10
|
+
GLTFCamera,
|
|
11
|
+
GLTFImage,
|
|
12
|
+
GLTFMaterial,
|
|
13
|
+
GLTFMesh,
|
|
14
|
+
GLTFNode,
|
|
15
|
+
GLTFSampler,
|
|
16
|
+
GLTFScene,
|
|
17
|
+
GLTFSkin,
|
|
18
|
+
GLTFTexture
|
|
19
|
+
} from '../types/gltf-json-schema';
|
|
20
|
+
|
|
21
|
+
import type {
|
|
22
|
+
GLTFPostprocessed,
|
|
23
|
+
GLTFAccessorPostprocessed,
|
|
24
|
+
GLTFBufferPostprocessed,
|
|
25
|
+
GLTFBufferViewPostprocessed,
|
|
26
|
+
GLTFCameraPostprocessed,
|
|
27
|
+
GLTFImagePostprocessed,
|
|
28
|
+
GLTFMaterialPostprocessed,
|
|
29
|
+
GLTFMeshPostprocessed,
|
|
30
|
+
GLTFNodePostprocessed,
|
|
31
|
+
GLTFSamplerPostprocessed,
|
|
32
|
+
GLTFScenePostprocessed,
|
|
33
|
+
GLTFSkinPostprocessed,
|
|
34
|
+
GLTFTexturePostprocessed,
|
|
35
|
+
GLTFMeshPrimitivePostprocessed
|
|
36
|
+
} from '../types/gltf-postprocessed-schema';
|
|
37
|
+
|
|
38
|
+
import {assert} from '../utils/assert';
|
|
39
|
+
import {getAccessorArrayTypeAndLength} from '../gltf-utils/gltf-utils';
|
|
40
|
+
|
|
41
|
+
// This is a post processor for loaded glTF files
|
|
42
|
+
// The goal is to make the loaded data easier to use in WebGL applications
|
|
43
|
+
//
|
|
44
|
+
// Functions:
|
|
45
|
+
// * Resolve indexed arrays structure of glTF into a linked tree.
|
|
46
|
+
// * Translate stringified enum keys and values into WebGL constants.
|
|
47
|
+
// * Load images (optional)
|
|
48
|
+
|
|
49
|
+
// ENUM LOOKUP
|
|
50
|
+
|
|
51
|
+
const COMPONENTS = {
|
|
52
|
+
SCALAR: 1,
|
|
53
|
+
VEC2: 2,
|
|
54
|
+
VEC3: 3,
|
|
55
|
+
VEC4: 4,
|
|
56
|
+
MAT2: 4,
|
|
57
|
+
MAT3: 9,
|
|
58
|
+
MAT4: 16
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const BYTES = {
|
|
62
|
+
5120: 1, // BYTE
|
|
63
|
+
5121: 1, // UNSIGNED_BYTE
|
|
64
|
+
5122: 2, // SHORT
|
|
65
|
+
5123: 2, // UNSIGNED_SHORT
|
|
66
|
+
5125: 4, // UNSIGNED_INT
|
|
67
|
+
5126: 4 // FLOAT
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const GL_SAMPLER = {
|
|
71
|
+
// Sampler parameters
|
|
72
|
+
TEXTURE_MAG_FILTER: 0x2800,
|
|
73
|
+
TEXTURE_MIN_FILTER: 0x2801,
|
|
74
|
+
TEXTURE_WRAP_S: 0x2802,
|
|
75
|
+
TEXTURE_WRAP_T: 0x2803,
|
|
76
|
+
|
|
77
|
+
// Sampler default values
|
|
78
|
+
REPEAT: 0x2901,
|
|
79
|
+
LINEAR: 0x2601,
|
|
80
|
+
NEAREST_MIPMAP_LINEAR: 0x2702
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const SAMPLER_PARAMETER_GLTF_TO_GL = {
|
|
84
|
+
magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER,
|
|
85
|
+
minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER,
|
|
86
|
+
wrapS: GL_SAMPLER.TEXTURE_WRAP_S,
|
|
87
|
+
wrapT: GL_SAMPLER.TEXTURE_WRAP_T
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// When undefined, a sampler with repeat wrapping and auto filtering should be used.
|
|
91
|
+
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
|
|
92
|
+
const DEFAULT_SAMPLER_PARAMETERS = {
|
|
93
|
+
[GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR,
|
|
94
|
+
[GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR,
|
|
95
|
+
[GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT,
|
|
96
|
+
[GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
function makeDefaultSampler(): GLTFSamplerPostprocessed {
|
|
100
|
+
return {
|
|
101
|
+
id: 'default-sampler',
|
|
102
|
+
parameters: DEFAULT_SAMPLER_PARAMETERS
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function getBytesFromComponentType(componentType) {
|
|
107
|
+
return BYTES[componentType];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getSizeFromAccessorType(type) {
|
|
111
|
+
return COMPONENTS[type];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
class GLTFPostProcessor {
|
|
115
|
+
baseUri: string = '';
|
|
116
|
+
// @ts-expect-error
|
|
117
|
+
jsonUnprocessed: GLTF;
|
|
118
|
+
// @ts-expect-error
|
|
119
|
+
json: GLTFPostprocessed;
|
|
120
|
+
buffers: {
|
|
121
|
+
arrayBuffer: ArrayBuffer;
|
|
122
|
+
byteOffset: number;
|
|
123
|
+
byteLength: number;
|
|
124
|
+
}[] = [];
|
|
125
|
+
images: any[] = [];
|
|
126
|
+
|
|
127
|
+
postProcess(gltf: GLTFWithBuffers, options = {}) {
|
|
128
|
+
const {json, buffers = [], images = []} = gltf;
|
|
129
|
+
// @ts-expect-error
|
|
130
|
+
const {baseUri = ''} = gltf;
|
|
131
|
+
assert(json);
|
|
132
|
+
|
|
133
|
+
this.baseUri = baseUri;
|
|
134
|
+
this.buffers = buffers;
|
|
135
|
+
this.images = images;
|
|
136
|
+
this.jsonUnprocessed = json;
|
|
137
|
+
|
|
138
|
+
this.json = this._resolveTree(gltf.json, options);
|
|
139
|
+
|
|
140
|
+
return this.json;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Convert indexed glTF structure into tree structure
|
|
144
|
+
// cross-link index resolution, enum lookup, convenience calculations
|
|
145
|
+
// eslint-disable-next-line complexity, max-statements
|
|
146
|
+
_resolveTree(gltf: GLTF, options = {}): GLTFPostprocessed {
|
|
147
|
+
// @ts-expect-error
|
|
148
|
+
const json: GLTFPostprocessed = {...gltf};
|
|
149
|
+
this.json = json;
|
|
150
|
+
|
|
151
|
+
if (gltf.bufferViews) {
|
|
152
|
+
json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
|
|
153
|
+
}
|
|
154
|
+
if (gltf.images) {
|
|
155
|
+
json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
|
|
156
|
+
}
|
|
157
|
+
if (gltf.samplers) {
|
|
158
|
+
json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
|
|
159
|
+
}
|
|
160
|
+
if (gltf.textures) {
|
|
161
|
+
json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
|
|
162
|
+
}
|
|
163
|
+
if (gltf.accessors) {
|
|
164
|
+
json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
|
|
165
|
+
}
|
|
166
|
+
if (gltf.materials) {
|
|
167
|
+
json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
|
|
168
|
+
}
|
|
169
|
+
if (gltf.meshes) {
|
|
170
|
+
json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
|
|
171
|
+
}
|
|
172
|
+
if (gltf.nodes) {
|
|
173
|
+
json.nodes = gltf.nodes.map((node, i) => this._resolveNode(node, i));
|
|
174
|
+
json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
|
|
175
|
+
}
|
|
176
|
+
if (gltf.skins) {
|
|
177
|
+
json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
|
|
178
|
+
}
|
|
179
|
+
if (gltf.scenes) {
|
|
180
|
+
json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
|
|
181
|
+
}
|
|
182
|
+
if (typeof this.json.scene === 'number' && json.scenes) {
|
|
183
|
+
json.scene = json.scenes[this.json.scene];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return json;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
getScene(index: number): GLTFScenePostprocessed {
|
|
190
|
+
return this._get(this.json.scenes, index);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
getNode(index: number): GLTFNodePostprocessed {
|
|
194
|
+
return this._get(this.json.nodes, index);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
getSkin(index: number): GLTFSkinPostprocessed {
|
|
198
|
+
return this._get(this.json.skins, index);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
getMesh(index: number): GLTFMeshPostprocessed {
|
|
202
|
+
return this._get(this.json.meshes, index);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getMaterial(index: number): GLTFMaterialPostprocessed {
|
|
206
|
+
return this._get(this.json.materials, index);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
getAccessor(index: number): GLTFAccessorPostprocessed {
|
|
210
|
+
return this._get(this.json.accessors, index);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
getCamera(index: number): GLTFCameraPostprocessed {
|
|
214
|
+
return this._get(this.json.cameras, index);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
getTexture(index: number): GLTFTexturePostprocessed {
|
|
218
|
+
return this._get(this.json.textures, index);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
getSampler(index: number): GLTFSamplerPostprocessed {
|
|
222
|
+
return this._get(this.json.samplers, index);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
getImage(index: number): GLTFImagePostprocessed {
|
|
226
|
+
return this._get(this.json.images, index);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
getBufferView(index: number): GLTFBufferViewPostprocessed {
|
|
230
|
+
return this._get(this.json.bufferViews, index);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
getBuffer(index: number): GLTFBufferPostprocessed {
|
|
234
|
+
return this._get(this.json.buffers, index);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
_get<T>(array: T[] | undefined, index: number): T {
|
|
238
|
+
// check if already resolved
|
|
239
|
+
if (typeof index === 'object') {
|
|
240
|
+
return index;
|
|
241
|
+
}
|
|
242
|
+
const object = array && array[index];
|
|
243
|
+
if (!object) {
|
|
244
|
+
console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
|
|
245
|
+
}
|
|
246
|
+
return object as T;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// PARSING HELPERS
|
|
250
|
+
|
|
251
|
+
_resolveScene(scene: GLTFScene, index: number): GLTFScenePostprocessed {
|
|
252
|
+
return {
|
|
253
|
+
...scene,
|
|
254
|
+
// @ts-ignore
|
|
255
|
+
id: scene.id || `scene-${index}`,
|
|
256
|
+
nodes: (scene.nodes || []).map((node) => this.getNode(node))
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
_resolveNode(gltfNode: GLTFNode, index: number): GLTFNodePostprocessed {
|
|
261
|
+
// @ts-expect-error
|
|
262
|
+
const node: GLTFNodePostprocessed = {
|
|
263
|
+
...gltfNode,
|
|
264
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
265
|
+
id: gltfNode?.id || `node-${index}`
|
|
266
|
+
};
|
|
267
|
+
if (gltfNode.mesh !== undefined) {
|
|
268
|
+
node.mesh = this.getMesh(gltfNode.mesh);
|
|
269
|
+
}
|
|
270
|
+
if (gltfNode.camera !== undefined) {
|
|
271
|
+
node.camera = this.getCamera(gltfNode.camera);
|
|
272
|
+
}
|
|
273
|
+
if (gltfNode.skin !== undefined) {
|
|
274
|
+
node.skin = this.getSkin(gltfNode.skin);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// TODO deprecated - Delete in v4.0?
|
|
278
|
+
// @ts-expect-error node.meshes does not seem to be part of the GLTF standard
|
|
279
|
+
if (gltfNode.meshes !== undefined && gltfNode.meshes.length) {
|
|
280
|
+
// @ts-expect-error
|
|
281
|
+
node.mesh = gltfNode.meshes.reduce(
|
|
282
|
+
(accum, meshIndex) => {
|
|
283
|
+
const mesh = this.getMesh(meshIndex);
|
|
284
|
+
accum.id = mesh.id;
|
|
285
|
+
accum.primitives = accum.primitives.concat(mesh.primitives);
|
|
286
|
+
return accum;
|
|
287
|
+
},
|
|
288
|
+
{primitives: []}
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return node;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
_resolveNodeChildren(node: GLTFNodePostprocessed): GLTFNodePostprocessed {
|
|
296
|
+
if (node.children) {
|
|
297
|
+
// @ts-expect-error node.children are numbers at this stage
|
|
298
|
+
node.children = node.children.map((child) => this.getNode(child));
|
|
299
|
+
}
|
|
300
|
+
return node;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
_resolveSkin(gltfSkin: GLTFSkin, index: number): GLTFSkinPostprocessed {
|
|
304
|
+
const inverseBindMatrices =
|
|
305
|
+
typeof gltfSkin.inverseBindMatrices === 'number'
|
|
306
|
+
? this.getAccessor(gltfSkin.inverseBindMatrices)
|
|
307
|
+
: undefined;
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
...gltfSkin,
|
|
311
|
+
id: gltfSkin.id || `skin-${index}`,
|
|
312
|
+
inverseBindMatrices
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
_resolveMesh(gltfMesh: GLTFMesh, index: number): GLTFMeshPostprocessed {
|
|
317
|
+
const mesh: GLTFMeshPostprocessed = {
|
|
318
|
+
...gltfMesh,
|
|
319
|
+
id: gltfMesh.id || `mesh-${index}`,
|
|
320
|
+
primitives: []
|
|
321
|
+
};
|
|
322
|
+
if (gltfMesh.primitives) {
|
|
323
|
+
mesh.primitives = gltfMesh.primitives.map((gltfPrimitive) => {
|
|
324
|
+
const primitive: GLTFMeshPrimitivePostprocessed = {
|
|
325
|
+
...gltfPrimitive,
|
|
326
|
+
attributes: {},
|
|
327
|
+
indices: undefined,
|
|
328
|
+
material: undefined
|
|
329
|
+
};
|
|
330
|
+
const attributes = gltfPrimitive.attributes;
|
|
331
|
+
for (const attribute in attributes) {
|
|
332
|
+
primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
|
|
333
|
+
}
|
|
334
|
+
if (gltfPrimitive.indices !== undefined) {
|
|
335
|
+
primitive.indices = this.getAccessor(gltfPrimitive.indices);
|
|
336
|
+
}
|
|
337
|
+
if (gltfPrimitive.material !== undefined) {
|
|
338
|
+
primitive.material = this.getMaterial(gltfPrimitive.material);
|
|
339
|
+
}
|
|
340
|
+
return primitive;
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return mesh;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
_resolveMaterial(gltfMaterial: GLTFMaterial, index: number): GLTFMaterialPostprocessed {
|
|
347
|
+
// @ts-expect-error
|
|
348
|
+
const material: GLTFMaterialPostprocessed = {
|
|
349
|
+
...gltfMaterial,
|
|
350
|
+
// @ts-expect-error
|
|
351
|
+
id: gltfMaterial.id || `material-${index}`
|
|
352
|
+
};
|
|
353
|
+
if (material.normalTexture) {
|
|
354
|
+
material.normalTexture = {...material.normalTexture};
|
|
355
|
+
material.normalTexture.texture = this.getTexture(material.normalTexture.index);
|
|
356
|
+
}
|
|
357
|
+
if (material.occlusionTexture) {
|
|
358
|
+
material.occlusionTexture = {...material.occlusionTexture};
|
|
359
|
+
material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
|
|
360
|
+
}
|
|
361
|
+
if (material.emissiveTexture) {
|
|
362
|
+
material.emissiveTexture = {...material.emissiveTexture};
|
|
363
|
+
material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
|
|
364
|
+
}
|
|
365
|
+
if (!material.emissiveFactor) {
|
|
366
|
+
material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (material.pbrMetallicRoughness) {
|
|
370
|
+
material.pbrMetallicRoughness = {...material.pbrMetallicRoughness};
|
|
371
|
+
const mr = material.pbrMetallicRoughness;
|
|
372
|
+
if (mr.baseColorTexture) {
|
|
373
|
+
mr.baseColorTexture = {...mr.baseColorTexture};
|
|
374
|
+
mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
|
|
375
|
+
}
|
|
376
|
+
if (mr.metallicRoughnessTexture) {
|
|
377
|
+
mr.metallicRoughnessTexture = {...mr.metallicRoughnessTexture};
|
|
378
|
+
mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return material;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
_resolveAccessor(gltfAccessor: GLTFAccessor, index: number): GLTFAccessorPostprocessed {
|
|
385
|
+
// Look up enums
|
|
386
|
+
const bytesPerComponent = getBytesFromComponentType(gltfAccessor.componentType);
|
|
387
|
+
const components = getSizeFromAccessorType(gltfAccessor.type);
|
|
388
|
+
const bytesPerElement = bytesPerComponent * components;
|
|
389
|
+
|
|
390
|
+
const accessor: GLTFAccessorPostprocessed = {
|
|
391
|
+
...gltfAccessor,
|
|
392
|
+
// @ts-expect-error
|
|
393
|
+
id: gltfAccessor.id || `accessor-${index}`,
|
|
394
|
+
bytesPerComponent,
|
|
395
|
+
components,
|
|
396
|
+
bytesPerElement,
|
|
397
|
+
value: undefined!,
|
|
398
|
+
bufferView: undefined!,
|
|
399
|
+
sparse: undefined!
|
|
400
|
+
};
|
|
401
|
+
if (gltfAccessor.bufferView !== undefined) {
|
|
402
|
+
// Draco encoded meshes don't have bufferView
|
|
403
|
+
accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// Create TypedArray for the accessor
|
|
407
|
+
// Note: The canonical way to instantiate is to ignore this array and create
|
|
408
|
+
// WebGLBuffer's using the bufferViews.
|
|
409
|
+
if (accessor.bufferView) {
|
|
410
|
+
const buffer = accessor.bufferView.buffer;
|
|
411
|
+
const {ArrayType, byteLength} = getAccessorArrayTypeAndLength(accessor, accessor.bufferView);
|
|
412
|
+
const byteOffset =
|
|
413
|
+
(accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
|
|
414
|
+
let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
|
|
415
|
+
if (accessor.bufferView.byteStride) {
|
|
416
|
+
cutBuffer = this._getValueFromInterleavedBuffer(
|
|
417
|
+
buffer,
|
|
418
|
+
byteOffset,
|
|
419
|
+
accessor.bufferView.byteStride,
|
|
420
|
+
accessor.bytesPerElement,
|
|
421
|
+
accessor.count
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
accessor.value = new ArrayType(cutBuffer);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return accessor;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Take values of particular accessor from interleaved buffer
|
|
432
|
+
* various parts of the buffer
|
|
433
|
+
* @param buffer
|
|
434
|
+
* @param byteOffset
|
|
435
|
+
* @param byteStride
|
|
436
|
+
* @param bytesPerElement
|
|
437
|
+
* @param count
|
|
438
|
+
* @returns
|
|
439
|
+
*/
|
|
440
|
+
_getValueFromInterleavedBuffer(
|
|
441
|
+
buffer,
|
|
442
|
+
byteOffset: number,
|
|
443
|
+
byteStride: number,
|
|
444
|
+
bytesPerElement: number,
|
|
445
|
+
count: number
|
|
446
|
+
): ArrayBufferLike {
|
|
447
|
+
const result = new Uint8Array(count * bytesPerElement);
|
|
448
|
+
for (let i = 0; i < count; i++) {
|
|
449
|
+
const elementOffset = byteOffset + i * byteStride;
|
|
450
|
+
result.set(
|
|
451
|
+
new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)),
|
|
452
|
+
i * bytesPerElement
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
return result.buffer;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
_resolveTexture(gltfTexture: GLTFTexture, index: number): GLTFTexturePostprocessed {
|
|
459
|
+
return {
|
|
460
|
+
...gltfTexture,
|
|
461
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
462
|
+
id: gltfTexture.id || `texture-${index}`,
|
|
463
|
+
sampler:
|
|
464
|
+
typeof gltfTexture.sampler === 'number'
|
|
465
|
+
? this.getSampler(gltfTexture.sampler)
|
|
466
|
+
: makeDefaultSampler(),
|
|
467
|
+
source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
_resolveSampler(gltfSampler: GLTFSampler, index: number): GLTFSamplerPostprocessed {
|
|
472
|
+
const sampler: GLTFSamplerPostprocessed = {
|
|
473
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
474
|
+
id: gltfSampler.id || `sampler-${index}`,
|
|
475
|
+
...gltfSampler,
|
|
476
|
+
parameters: {}
|
|
477
|
+
};
|
|
478
|
+
// Map textual parameters to GL parameter values
|
|
479
|
+
for (const key in sampler) {
|
|
480
|
+
const glEnum = this._enumSamplerParameter(key);
|
|
481
|
+
if (glEnum !== undefined) {
|
|
482
|
+
sampler.parameters[glEnum] = sampler[key];
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return sampler;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
_enumSamplerParameter(key: string): number {
|
|
489
|
+
return SAMPLER_PARAMETER_GLTF_TO_GL[key];
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
_resolveImage(gltfImage: GLTFImage, index: number): GLTFImagePostprocessed {
|
|
493
|
+
const image: GLTFImagePostprocessed = {
|
|
494
|
+
...gltfImage,
|
|
495
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
496
|
+
id: gltfImage.id || `image-${index}`,
|
|
497
|
+
image: null!,
|
|
498
|
+
bufferView:
|
|
499
|
+
gltfImage.bufferView !== undefined ? this.getBufferView(gltfImage.bufferView) : undefined
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
// Check if image has been preloaded by the GLTFLoader
|
|
503
|
+
// If so, link it into the JSON and drop the URI
|
|
504
|
+
const preloadedImage = this.images[index];
|
|
505
|
+
if (preloadedImage) {
|
|
506
|
+
image.image = preloadedImage;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return image;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
_resolveBufferView(gltfBufferView: GLTFBufferView, index: number): GLTFBufferViewPostprocessed {
|
|
513
|
+
const bufferIndex = gltfBufferView.buffer;
|
|
514
|
+
const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
|
|
515
|
+
// Add offset of buffer, then offset of buffer view
|
|
516
|
+
let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
|
|
517
|
+
if (gltfBufferView.byteOffset) {
|
|
518
|
+
byteOffset += gltfBufferView.byteOffset;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
const bufferView: GLTFBufferViewPostprocessed = {
|
|
522
|
+
// // @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
523
|
+
id: `bufferView-${index}`,
|
|
524
|
+
...gltfBufferView,
|
|
525
|
+
// ...this.buffers[bufferIndex],
|
|
526
|
+
buffer: this.buffers[bufferIndex],
|
|
527
|
+
data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
return bufferView;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
_resolveCamera(gltfCamera: GLTFCamera, index): GLTFCameraPostprocessed {
|
|
534
|
+
const camera: GLTFCameraPostprocessed = {
|
|
535
|
+
...gltfCamera,
|
|
536
|
+
// @ts-expect-error id could already be present, glTF standard does not prevent it
|
|
537
|
+
id: gltfCamera.id || `camera-${index}`
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// TODO - create 4x4 matrices
|
|
541
|
+
if (camera.perspective) {
|
|
542
|
+
// camera.matrix = createPerspectiveMatrix(camera.perspective);
|
|
543
|
+
}
|
|
544
|
+
if (camera.orthographic) {
|
|
545
|
+
// camera.matrix = createOrthographicMatrix(camera.orthographic);
|
|
546
|
+
}
|
|
547
|
+
return camera;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
export function postProcessGLTF(
|
|
552
|
+
gltf: GLTFWithBuffers,
|
|
553
|
+
options?: ParseGLTFOptions
|
|
554
|
+
): GLTFPostprocessed {
|
|
555
|
+
return new GLTFPostProcessor().postProcess(gltf, options);
|
|
556
|
+
}
|
|
@@ -3,6 +3,7 @@ import {
|
|
|
3
3
|
copyPaddedStringToDataView,
|
|
4
4
|
copyPaddedArrayBufferToDataView
|
|
5
5
|
} from '@loaders.gl/loader-utils';
|
|
6
|
+
// import type {GLB} from '../types/glb-types';
|
|
6
7
|
|
|
7
8
|
const MAGIC_glTF = 0x46546c67; // glTF in ASCII
|
|
8
9
|
const MAGIC_JSON = 0x4e4f534a; // JSON in ASCII
|
|
@@ -10,10 +11,26 @@ const MAGIC_BIN = 0x004e4942; // BIN\0 in ASCII
|
|
|
10
11
|
|
|
11
12
|
const LE = true; // Binary GLTF is little endian.
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export type GLBEncodeOptions = {};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Encode the full GLB buffer with header etc
|
|
18
|
+
*
|
|
19
|
+
* @param glb
|
|
20
|
+
* @param dataView - if `null`, does not encode but just calculates length
|
|
21
|
+
* @param byteOffset
|
|
22
|
+
* @param options
|
|
23
|
+
* @returns
|
|
24
|
+
*
|
|
25
|
+
* @see https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#glb-file-format-specification
|
|
26
|
+
* @todo type GLB argument
|
|
27
|
+
*/
|
|
28
|
+
export function encodeGLBSync(
|
|
29
|
+
glb,
|
|
30
|
+
dataView: DataView | null,
|
|
31
|
+
byteOffset = 0,
|
|
32
|
+
options: GLBEncodeOptions = {}
|
|
33
|
+
) {
|
|
17
34
|
const {magic = MAGIC_glTF, version = 2, json = {}, binary} = glb;
|
|
18
35
|
|
|
19
36
|
const byteOffsetStart = byteOffset;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {encodeGLBSync} from './encode-glb';
|
|
2
|
+
|
|
3
|
+
export type GLTFEncodeOptions = Record<string, any>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Encode the full glTF file as a binary GLB file
|
|
7
|
+
* Returns an ArrayBuffer that represents the complete GLB image that can be saved to file
|
|
8
|
+
*
|
|
9
|
+
* @todo - Does not support encoding to non-GLB versions of glTF format. Other formats
|
|
10
|
+
* - Encode as a textual JSON file with binary data in base64 data URLs.
|
|
11
|
+
* - Encode as a JSON with all images (and buffers?) in separate binary files
|
|
12
|
+
*
|
|
13
|
+
* glb-file-format-specification
|
|
14
|
+
* @see https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#
|
|
15
|
+
*
|
|
16
|
+
* @param gltf
|
|
17
|
+
* @param arrayBuffer
|
|
18
|
+
* @param byteOffset
|
|
19
|
+
* @param options
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export function encodeGLTFSync(gltf, arrayBuffer, byteOffset, options) {
|
|
23
|
+
convertBuffersToBase64(gltf);
|
|
24
|
+
|
|
25
|
+
// TODO: Copy buffers to binary
|
|
26
|
+
|
|
27
|
+
return encodeGLBSync(gltf, arrayBuffer, byteOffset, options);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function convertBuffersToBase64(gltf, {firstBuffer = 0} = {}) {
|
|
31
|
+
if (gltf.buffers && gltf.buffers.length > firstBuffer) {
|
|
32
|
+
throw new Error('encodeGLTF: multiple buffers not yet implemented');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/* eslint-disable camelcase */
|
|
2
|
-
import type {GLTF, GLTFBufferView, GLTF_EXT_meshopt_compression} from '../types/gltf-
|
|
2
|
+
import type {GLTF, GLTFBufferView, GLTF_EXT_meshopt_compression} from '../types/gltf-json-schema';
|
|
3
3
|
import type {GLTFLoaderOptions} from '../../gltf-loader';
|
|
4
|
-
import GLTFScenegraph from '../api/gltf-scenegraph';
|
|
5
|
-
import {
|
|
4
|
+
import {GLTFScenegraph} from '../api/gltf-scenegraph';
|
|
5
|
+
import {meshoptDecodeGltfBuffer} from '../../meshopt/meshopt-decoder';
|
|
6
6
|
|
|
7
7
|
// @ts-ignore
|
|
8
8
|
// eslint-disable-next-line
|
|
@@ -16,16 +16,6 @@ const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
|
|
|
16
16
|
|
|
17
17
|
export const name = EXT_MESHOPT_COMPRESSION;
|
|
18
18
|
|
|
19
|
-
export function preprocess(gltfData: {json: GLTF}) {
|
|
20
|
-
const scenegraph = new GLTFScenegraph(gltfData);
|
|
21
|
-
if (
|
|
22
|
-
scenegraph.getRequiredExtensions().includes(EXT_MESHOPT_COMPRESSION) &&
|
|
23
|
-
!isMeshoptSupported()
|
|
24
|
-
) {
|
|
25
|
-
throw new Error(`gltf: Required extension ${EXT_MESHOPT_COMPRESSION} not supported by browser`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
19
|
export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions) {
|
|
30
20
|
const scenegraph = new GLTFScenegraph(gltfData);
|
|
31
21
|
|
|
@@ -41,7 +31,7 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
|
|
|
41
31
|
// Decompress meshes in parallel
|
|
42
32
|
await Promise.all(promises);
|
|
43
33
|
|
|
44
|
-
// We have now decompressed all primitives, so remove the top-level
|
|
34
|
+
// We have now decompressed all primitives, so remove the top-level extension
|
|
45
35
|
scenegraph.removeExtension(EXT_MESHOPT_COMPRESSION);
|
|
46
36
|
}
|
|
47
37
|
|
|
@@ -49,29 +39,30 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
|
|
|
49
39
|
async function decodeMeshoptBufferView(
|
|
50
40
|
scenegraph: GLTFScenegraph,
|
|
51
41
|
bufferView: GLTFBufferView
|
|
52
|
-
): Promise<
|
|
42
|
+
): Promise<void> {
|
|
53
43
|
const meshoptExtension = scenegraph.getObjectExtension<GLTF_EXT_meshopt_compression>(
|
|
54
44
|
bufferView,
|
|
55
45
|
EXT_MESHOPT_COMPRESSION
|
|
56
46
|
);
|
|
57
47
|
if (meshoptExtension) {
|
|
58
|
-
const buffer = bufferView.buffer;
|
|
59
|
-
|
|
60
48
|
const {
|
|
61
49
|
byteOffset = 0,
|
|
62
50
|
byteLength = 0,
|
|
63
51
|
byteStride,
|
|
64
52
|
count,
|
|
65
53
|
mode,
|
|
66
|
-
filter = 'NONE'
|
|
54
|
+
filter = 'NONE',
|
|
55
|
+
buffer: bufferIndex
|
|
67
56
|
} = meshoptExtension;
|
|
57
|
+
const buffer = scenegraph.gltf.buffers[bufferIndex];
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
59
|
+
const source = new Uint8Array(buffer.arrayBuffer, buffer.byteOffset + byteOffset, byteLength);
|
|
60
|
+
const result = new Uint8Array(
|
|
61
|
+
scenegraph.gltf.buffers[bufferView.buffer].arrayBuffer,
|
|
62
|
+
bufferView.byteOffset,
|
|
63
|
+
bufferView.byteLength
|
|
64
|
+
);
|
|
65
|
+
await meshoptDecodeGltfBuffer(result, count, byteStride, source, mode, filter);
|
|
66
|
+
scenegraph.removeObjectExtension(bufferView, EXT_MESHOPT_COMPRESSION);
|
|
74
67
|
}
|
|
75
|
-
|
|
76
|
-
return null;
|
|
77
68
|
}
|