@loaders.gl/gltf 4.2.0-alpha.4 → 4.2.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/dist/dist.dev.js +1056 -524
  2. package/dist/dist.min.js +9 -0
  3. package/dist/glb-loader.d.ts +2 -2
  4. package/dist/glb-loader.d.ts.map +1 -1
  5. package/dist/glb-loader.js +22 -21
  6. package/dist/glb-writer.d.ts +2 -2
  7. package/dist/glb-writer.d.ts.map +1 -1
  8. package/dist/glb-writer.js +27 -24
  9. package/dist/gltf-loader.d.ts +3 -3
  10. package/dist/gltf-loader.d.ts.map +1 -1
  11. package/dist/gltf-loader.js +31 -36
  12. package/dist/gltf-writer.js +24 -26
  13. package/dist/index.cjs +138 -330
  14. package/dist/index.cjs.map +7 -0
  15. package/dist/index.d.ts +17 -17
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +3 -1
  18. package/dist/lib/api/gltf-extensions.d.ts +2 -2
  19. package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
  20. package/dist/lib/api/gltf-extensions.js +45 -22
  21. package/dist/lib/api/gltf-scenegraph.d.ts +2 -2
  22. package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
  23. package/dist/lib/api/gltf-scenegraph.js +565 -438
  24. package/dist/lib/api/normalize-gltf-v1.js +249 -181
  25. package/dist/lib/api/post-process-gltf.d.ts +3 -3
  26. package/dist/lib/api/post-process-gltf.d.ts.map +1 -1
  27. package/dist/lib/api/post-process-gltf.js +378 -340
  28. package/dist/lib/encoders/encode-glb.js +62 -48
  29. package/dist/lib/encoders/encode-gltf.js +24 -10
  30. package/dist/lib/extensions/EXT_mesh_features.d.ts +2 -2
  31. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -1
  32. package/dist/lib/extensions/EXT_mesh_features.js +55 -33
  33. package/dist/lib/extensions/EXT_meshopt_compression.d.ts +2 -2
  34. package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -1
  35. package/dist/lib/extensions/EXT_meshopt_compression.js +27 -31
  36. package/dist/lib/extensions/EXT_structural_metadata.d.ts +2 -2
  37. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
  38. package/dist/lib/extensions/EXT_structural_metadata.js +434 -230
  39. package/dist/lib/extensions/EXT_texture_webp.d.ts +2 -2
  40. package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -1
  41. package/dist/lib/extensions/EXT_texture_webp.js +24 -17
  42. package/dist/lib/extensions/KHR_binary_gltf.d.ts +1 -1
  43. package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -1
  44. package/dist/lib/extensions/KHR_binary_gltf.js +29 -15
  45. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +2 -2
  46. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -1
  47. package/dist/lib/extensions/KHR_draco_mesh_compression.js +110 -87
  48. package/dist/lib/extensions/KHR_texture_basisu.d.ts +2 -2
  49. package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -1
  50. package/dist/lib/extensions/KHR_texture_basisu.js +19 -12
  51. package/dist/lib/extensions/KHR_texture_transform.d.ts +2 -2
  52. package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -1
  53. package/dist/lib/extensions/KHR_texture_transform.js +188 -156
  54. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +2 -2
  55. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  56. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +263 -143
  57. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +1 -1
  58. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -1
  59. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +44 -32
  60. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +1 -1
  61. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -1
  62. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +30 -24
  63. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +1 -1
  64. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -1
  65. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +65 -52
  66. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +2 -2
  67. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -1
  68. package/dist/lib/extensions/utils/3d-tiles-utils.js +298 -181
  69. package/dist/lib/gltf-utils/get-typed-array.d.ts +1 -1
  70. package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -1
  71. package/dist/lib/gltf-utils/get-typed-array.js +54 -42
  72. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +1 -1
  73. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -1
  74. package/dist/lib/gltf-utils/gltf-attribute-utils.js +58 -52
  75. package/dist/lib/gltf-utils/gltf-constants.js +27 -27
  76. package/dist/lib/gltf-utils/gltf-utils.d.ts +1 -1
  77. package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -1
  78. package/dist/lib/gltf-utils/gltf-utils.js +67 -60
  79. package/dist/lib/gltf-utils/resolve-url.js +12 -10
  80. package/dist/lib/parsers/parse-glb.d.ts +1 -1
  81. package/dist/lib/parsers/parse-glb.d.ts.map +1 -1
  82. package/dist/lib/parsers/parse-glb.js +132 -89
  83. package/dist/lib/parsers/parse-gltf.d.ts +3 -3
  84. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
  85. package/dist/lib/parsers/parse-gltf.js +155 -126
  86. package/dist/lib/types/glb-types.js +0 -1
  87. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts +1 -1
  88. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts.map +1 -1
  89. package/dist/lib/types/gltf-ext-feature-metadata-schema.js +0 -1
  90. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +1 -1
  91. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -1
  92. package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -1
  93. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +1 -1
  94. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
  95. package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -1
  96. package/dist/lib/types/gltf-json-schema.js +2 -1
  97. package/dist/lib/types/gltf-postprocessed-schema.js +2 -1
  98. package/dist/lib/types/gltf-types.d.ts +3 -3
  99. package/dist/lib/types/gltf-types.d.ts.map +1 -1
  100. package/dist/lib/types/gltf-types.js +1 -1
  101. package/dist/lib/utils/assert.js +6 -4
  102. package/dist/lib/utils/version.js +4 -2
  103. package/dist/meshopt/meshopt-decoder.js +86 -67
  104. package/dist/webp/webp.js +28 -19
  105. package/package.json +13 -8
  106. package/src/lib/extensions/KHR_texture_transform.ts +18 -24
  107. package/dist/glb-loader.js.map +0 -1
  108. package/dist/glb-writer.js.map +0 -1
  109. package/dist/gltf-loader.js.map +0 -1
  110. package/dist/gltf-writer.js.map +0 -1
  111. package/dist/index.js.map +0 -1
  112. package/dist/lib/api/gltf-extensions.js.map +0 -1
  113. package/dist/lib/api/gltf-scenegraph.js.map +0 -1
  114. package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
  115. package/dist/lib/api/post-process-gltf.js.map +0 -1
  116. package/dist/lib/encoders/encode-glb.js.map +0 -1
  117. package/dist/lib/encoders/encode-gltf.js.map +0 -1
  118. package/dist/lib/extensions/EXT_mesh_features.js.map +0 -1
  119. package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  120. package/dist/lib/extensions/EXT_structural_metadata.js.map +0 -1
  121. package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
  122. package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
  123. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  124. package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
  125. package/dist/lib/extensions/KHR_texture_transform.js.map +0 -1
  126. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js.map +0 -1
  127. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  128. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  129. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  130. package/dist/lib/extensions/utils/3d-tiles-utils.js.map +0 -1
  131. package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
  132. package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  133. package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
  134. package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
  135. package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
  136. package/dist/lib/parsers/parse-glb.js.map +0 -1
  137. package/dist/lib/parsers/parse-gltf.js.map +0 -1
  138. package/dist/lib/types/glb-types.js.map +0 -1
  139. package/dist/lib/types/gltf-ext-feature-metadata-schema.js.map +0 -1
  140. package/dist/lib/types/gltf-ext-mesh-features-schema.js.map +0 -1
  141. package/dist/lib/types/gltf-ext-structural-metadata-schema.js.map +0 -1
  142. package/dist/lib/types/gltf-json-schema.js.map +0 -1
  143. package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
  144. package/dist/lib/types/gltf-types.js.map +0 -1
  145. package/dist/lib/utils/assert.js.map +0 -1
  146. package/dist/lib/utils/version.js.map +0 -1
  147. package/dist/meshopt/meshopt-decoder.js.map +0 -1
  148. package/dist/meshopt/meshopt-encoder.ts.disabled +0 -409
  149. package/dist/webp/webp.js.map +0 -1
@@ -1,395 +1,433 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { assert } from "../utils/assert.js";
2
5
  import { getAccessorArrayTypeAndLength } from "../gltf-utils/gltf-utils.js";
6
+ // This is a post processor for loaded glTF files
7
+ // The goal is to make the loaded data easier to use in WebGL applications
8
+ //
9
+ // Functions:
10
+ // * Resolve indexed arrays structure of glTF into a linked tree.
11
+ // * Translate stringified enum keys and values into WebGL constants.
12
+ // * Load images (optional)
13
+ // ENUM LOOKUP
3
14
  const COMPONENTS = {
4
- SCALAR: 1,
5
- VEC2: 2,
6
- VEC3: 3,
7
- VEC4: 4,
8
- MAT2: 4,
9
- MAT3: 9,
10
- MAT4: 16
15
+ SCALAR: 1,
16
+ VEC2: 2,
17
+ VEC3: 3,
18
+ VEC4: 4,
19
+ MAT2: 4,
20
+ MAT3: 9,
21
+ MAT4: 16
11
22
  };
12
23
  const BYTES = {
13
- 5120: 1,
14
- 5121: 1,
15
- 5122: 2,
16
- 5123: 2,
17
- 5125: 4,
18
- 5126: 4
24
+ 5120: 1, // BYTE
25
+ 5121: 1, // UNSIGNED_BYTE
26
+ 5122: 2, // SHORT
27
+ 5123: 2, // UNSIGNED_SHORT
28
+ 5125: 4, // UNSIGNED_INT
29
+ 5126: 4 // FLOAT
19
30
  };
20
31
  const GL_SAMPLER = {
21
- TEXTURE_MAG_FILTER: 0x2800,
22
- TEXTURE_MIN_FILTER: 0x2801,
23
- TEXTURE_WRAP_S: 0x2802,
24
- TEXTURE_WRAP_T: 0x2803,
25
- REPEAT: 0x2901,
26
- LINEAR: 0x2601,
27
- NEAREST_MIPMAP_LINEAR: 0x2702
32
+ // Sampler parameters
33
+ TEXTURE_MAG_FILTER: 0x2800,
34
+ TEXTURE_MIN_FILTER: 0x2801,
35
+ TEXTURE_WRAP_S: 0x2802,
36
+ TEXTURE_WRAP_T: 0x2803,
37
+ // Sampler default values
38
+ REPEAT: 0x2901,
39
+ LINEAR: 0x2601,
40
+ NEAREST_MIPMAP_LINEAR: 0x2702
28
41
  };
29
42
  const SAMPLER_PARAMETER_GLTF_TO_GL = {
30
- magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER,
31
- minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER,
32
- wrapS: GL_SAMPLER.TEXTURE_WRAP_S,
33
- wrapT: GL_SAMPLER.TEXTURE_WRAP_T
43
+ magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER,
44
+ minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER,
45
+ wrapS: GL_SAMPLER.TEXTURE_WRAP_S,
46
+ wrapT: GL_SAMPLER.TEXTURE_WRAP_T
34
47
  };
48
+ // When undefined, a sampler with repeat wrapping and auto filtering should be used.
49
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
35
50
  const DEFAULT_SAMPLER_PARAMETERS = {
36
- [GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR,
37
- [GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR,
38
- [GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT,
39
- [GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT
51
+ [GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR,
52
+ [GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR,
53
+ [GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT,
54
+ [GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT
40
55
  };
41
56
  function makeDefaultSampler() {
42
- return {
43
- id: 'default-sampler',
44
- parameters: DEFAULT_SAMPLER_PARAMETERS
45
- };
57
+ return {
58
+ id: 'default-sampler',
59
+ parameters: DEFAULT_SAMPLER_PARAMETERS
60
+ };
46
61
  }
47
62
  function getBytesFromComponentType(componentType) {
48
- return BYTES[componentType];
63
+ return BYTES[componentType];
49
64
  }
50
65
  function getSizeFromAccessorType(type) {
51
- return COMPONENTS[type];
66
+ return COMPONENTS[type];
52
67
  }
53
68
  class GLTFPostProcessor {
54
- constructor() {
55
- this.baseUri = '';
56
- this.jsonUnprocessed = void 0;
57
- this.json = void 0;
58
- this.buffers = [];
59
- this.images = [];
60
- }
61
- postProcess(gltf) {
62
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
63
- const {
64
- json,
65
- buffers = [],
66
- images = []
67
- } = gltf;
68
- const {
69
- baseUri = ''
70
- } = gltf;
71
- assert(json);
72
- this.baseUri = baseUri;
73
- this.buffers = buffers;
74
- this.images = images;
75
- this.jsonUnprocessed = json;
76
- this.json = this._resolveTree(gltf.json, options);
77
- return this.json;
78
- }
79
- _resolveTree(gltf) {
80
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
81
- const json = {
82
- ...gltf
83
- };
84
- this.json = json;
85
- if (gltf.bufferViews) {
86
- json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
87
- }
88
- if (gltf.images) {
89
- json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
69
+ baseUri = '';
70
+ // @ts-expect-error
71
+ jsonUnprocessed;
72
+ // @ts-expect-error
73
+ json;
74
+ buffers = [];
75
+ images = [];
76
+ postProcess(gltf, options = {}) {
77
+ const { json, buffers = [], images = [] } = gltf;
78
+ // @ts-expect-error
79
+ const { baseUri = '' } = gltf;
80
+ assert(json);
81
+ this.baseUri = baseUri;
82
+ this.buffers = buffers;
83
+ this.images = images;
84
+ this.jsonUnprocessed = json;
85
+ this.json = this._resolveTree(gltf.json, options);
86
+ return this.json;
90
87
  }
91
- if (gltf.samplers) {
92
- json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
93
- }
94
- if (gltf.textures) {
95
- json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
88
+ // Convert indexed glTF structure into tree structure
89
+ // cross-link index resolution, enum lookup, convenience calculations
90
+ // eslint-disable-next-line complexity, max-statements
91
+ _resolveTree(gltf, options = {}) {
92
+ // @ts-expect-error
93
+ const json = { ...gltf };
94
+ this.json = json;
95
+ if (gltf.bufferViews) {
96
+ json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
97
+ }
98
+ if (gltf.images) {
99
+ json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
100
+ }
101
+ if (gltf.samplers) {
102
+ json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
103
+ }
104
+ if (gltf.textures) {
105
+ json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
106
+ }
107
+ if (gltf.accessors) {
108
+ json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
109
+ }
110
+ if (gltf.materials) {
111
+ json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
112
+ }
113
+ if (gltf.meshes) {
114
+ json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
115
+ }
116
+ if (gltf.nodes) {
117
+ json.nodes = gltf.nodes.map((node, i) => this._resolveNode(node, i));
118
+ json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
119
+ }
120
+ if (gltf.skins) {
121
+ json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
122
+ }
123
+ if (gltf.scenes) {
124
+ json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
125
+ }
126
+ if (typeof this.json.scene === 'number' && json.scenes) {
127
+ json.scene = json.scenes[this.json.scene];
128
+ }
129
+ return json;
96
130
  }
97
- if (gltf.accessors) {
98
- json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
131
+ getScene(index) {
132
+ return this._get(this.json.scenes, index);
99
133
  }
100
- if (gltf.materials) {
101
- json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
134
+ getNode(index) {
135
+ return this._get(this.json.nodes, index);
102
136
  }
103
- if (gltf.meshes) {
104
- json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
137
+ getSkin(index) {
138
+ return this._get(this.json.skins, index);
105
139
  }
106
- if (gltf.nodes) {
107
- json.nodes = gltf.nodes.map((node, i) => this._resolveNode(node, i));
108
- json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
140
+ getMesh(index) {
141
+ return this._get(this.json.meshes, index);
109
142
  }
110
- if (gltf.skins) {
111
- json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
143
+ getMaterial(index) {
144
+ return this._get(this.json.materials, index);
112
145
  }
113
- if (gltf.scenes) {
114
- json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
146
+ getAccessor(index) {
147
+ return this._get(this.json.accessors, index);
115
148
  }
116
- if (typeof this.json.scene === 'number' && json.scenes) {
117
- json.scene = json.scenes[this.json.scene];
149
+ getCamera(index) {
150
+ return this._get(this.json.cameras, index);
118
151
  }
119
- return json;
120
- }
121
- getScene(index) {
122
- return this._get(this.json.scenes, index);
123
- }
124
- getNode(index) {
125
- return this._get(this.json.nodes, index);
126
- }
127
- getSkin(index) {
128
- return this._get(this.json.skins, index);
129
- }
130
- getMesh(index) {
131
- return this._get(this.json.meshes, index);
132
- }
133
- getMaterial(index) {
134
- return this._get(this.json.materials, index);
135
- }
136
- getAccessor(index) {
137
- return this._get(this.json.accessors, index);
138
- }
139
- getCamera(index) {
140
- return this._get(this.json.cameras, index);
141
- }
142
- getTexture(index) {
143
- return this._get(this.json.textures, index);
144
- }
145
- getSampler(index) {
146
- return this._get(this.json.samplers, index);
147
- }
148
- getImage(index) {
149
- return this._get(this.json.images, index);
150
- }
151
- getBufferView(index) {
152
- return this._get(this.json.bufferViews, index);
153
- }
154
- getBuffer(index) {
155
- return this._get(this.json.buffers, index);
156
- }
157
- _get(array, index) {
158
- if (typeof index === 'object') {
159
- return index;
152
+ getTexture(index) {
153
+ return this._get(this.json.textures, index);
160
154
  }
161
- const object = array && array[index];
162
- if (!object) {
163
- console.warn(`glTF file error: Could not find ${array}[${index}]`);
155
+ getSampler(index) {
156
+ return this._get(this.json.samplers, index);
164
157
  }
165
- return object;
166
- }
167
- _resolveScene(scene, index) {
168
- return {
169
- ...scene,
170
- id: scene.id || `scene-${index}`,
171
- nodes: (scene.nodes || []).map(node => this.getNode(node))
172
- };
173
- }
174
- _resolveNode(gltfNode, index) {
175
- const node = {
176
- ...gltfNode,
177
- id: (gltfNode === null || gltfNode === void 0 ? void 0 : gltfNode.id) || `node-${index}`
178
- };
179
- if (gltfNode.mesh !== undefined) {
180
- node.mesh = this.getMesh(gltfNode.mesh);
158
+ getImage(index) {
159
+ return this._get(this.json.images, index);
181
160
  }
182
- if (gltfNode.camera !== undefined) {
183
- node.camera = this.getCamera(gltfNode.camera);
161
+ getBufferView(index) {
162
+ return this._get(this.json.bufferViews, index);
184
163
  }
185
- if (gltfNode.skin !== undefined) {
186
- node.skin = this.getSkin(gltfNode.skin);
164
+ getBuffer(index) {
165
+ return this._get(this.json.buffers, index);
187
166
  }
188
- if (gltfNode.meshes !== undefined && gltfNode.meshes.length) {
189
- node.mesh = gltfNode.meshes.reduce((accum, meshIndex) => {
190
- const mesh = this.getMesh(meshIndex);
191
- accum.id = mesh.id;
192
- accum.primitives = accum.primitives.concat(mesh.primitives);
193
- return accum;
194
- }, {
195
- primitives: []
196
- });
167
+ _get(array, index) {
168
+ // check if already resolved
169
+ if (typeof index === 'object') {
170
+ return index;
171
+ }
172
+ const object = array && array[index];
173
+ if (!object) {
174
+ console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
175
+ }
176
+ return object;
197
177
  }
198
- return node;
199
- }
200
- _resolveNodeChildren(node) {
201
- if (node.children) {
202
- node.children = node.children.map(child => this.getNode(child));
178
+ // PARSING HELPERS
179
+ _resolveScene(scene, index) {
180
+ return {
181
+ ...scene,
182
+ // @ts-ignore
183
+ id: scene.id || `scene-${index}`,
184
+ nodes: (scene.nodes || []).map((node) => this.getNode(node))
185
+ };
203
186
  }
204
- return node;
205
- }
206
- _resolveSkin(gltfSkin, index) {
207
- const inverseBindMatrices = typeof gltfSkin.inverseBindMatrices === 'number' ? this.getAccessor(gltfSkin.inverseBindMatrices) : undefined;
208
- return {
209
- ...gltfSkin,
210
- id: gltfSkin.id || `skin-${index}`,
211
- inverseBindMatrices
212
- };
213
- }
214
- _resolveMesh(gltfMesh, index) {
215
- const mesh = {
216
- ...gltfMesh,
217
- id: gltfMesh.id || `mesh-${index}`,
218
- primitives: []
219
- };
220
- if (gltfMesh.primitives) {
221
- mesh.primitives = gltfMesh.primitives.map(gltfPrimitive => {
222
- const primitive = {
223
- ...gltfPrimitive,
224
- attributes: {},
225
- indices: undefined,
226
- material: undefined
187
+ _resolveNode(gltfNode, index) {
188
+ // @ts-expect-error
189
+ const node = {
190
+ ...gltfNode,
191
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
192
+ id: gltfNode?.id || `node-${index}`
227
193
  };
228
- const attributes = gltfPrimitive.attributes;
229
- for (const attribute in attributes) {
230
- primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
194
+ if (gltfNode.mesh !== undefined) {
195
+ node.mesh = this.getMesh(gltfNode.mesh);
231
196
  }
232
- if (gltfPrimitive.indices !== undefined) {
233
- primitive.indices = this.getAccessor(gltfPrimitive.indices);
197
+ if (gltfNode.camera !== undefined) {
198
+ node.camera = this.getCamera(gltfNode.camera);
234
199
  }
235
- if (gltfPrimitive.material !== undefined) {
236
- primitive.material = this.getMaterial(gltfPrimitive.material);
200
+ if (gltfNode.skin !== undefined) {
201
+ node.skin = this.getSkin(gltfNode.skin);
237
202
  }
238
- return primitive;
239
- });
240
- }
241
- return mesh;
242
- }
243
- _resolveMaterial(gltfMaterial, index) {
244
- const material = {
245
- ...gltfMaterial,
246
- id: gltfMaterial.id || `material-${index}`
247
- };
248
- if (material.normalTexture) {
249
- material.normalTexture = {
250
- ...material.normalTexture
251
- };
252
- material.normalTexture.texture = this.getTexture(material.normalTexture.index);
203
+ // TODO deprecated - Delete in v4.0?
204
+ // @ts-expect-error node.meshes does not seem to be part of the GLTF standard
205
+ if (gltfNode.meshes !== undefined && gltfNode.meshes.length) {
206
+ // @ts-expect-error
207
+ node.mesh = gltfNode.meshes.reduce((accum, meshIndex) => {
208
+ const mesh = this.getMesh(meshIndex);
209
+ accum.id = mesh.id;
210
+ accum.primitives = accum.primitives.concat(mesh.primitives);
211
+ return accum;
212
+ }, { primitives: [] });
213
+ }
214
+ return node;
253
215
  }
254
- if (material.occlusionTexture) {
255
- material.occlusionTexture = {
256
- ...material.occlusionTexture
257
- };
258
- material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
216
+ _resolveNodeChildren(node) {
217
+ if (node.children) {
218
+ // @ts-expect-error node.children are numbers at this stage
219
+ node.children = node.children.map((child) => this.getNode(child));
220
+ }
221
+ return node;
259
222
  }
260
- if (material.emissiveTexture) {
261
- material.emissiveTexture = {
262
- ...material.emissiveTexture
263
- };
264
- material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
223
+ _resolveSkin(gltfSkin, index) {
224
+ const inverseBindMatrices = typeof gltfSkin.inverseBindMatrices === 'number'
225
+ ? this.getAccessor(gltfSkin.inverseBindMatrices)
226
+ : undefined;
227
+ return {
228
+ ...gltfSkin,
229
+ id: gltfSkin.id || `skin-${index}`,
230
+ inverseBindMatrices
231
+ };
265
232
  }
266
- if (!material.emissiveFactor) {
267
- material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
233
+ _resolveMesh(gltfMesh, index) {
234
+ const mesh = {
235
+ ...gltfMesh,
236
+ id: gltfMesh.id || `mesh-${index}`,
237
+ primitives: []
238
+ };
239
+ if (gltfMesh.primitives) {
240
+ mesh.primitives = gltfMesh.primitives.map((gltfPrimitive) => {
241
+ const primitive = {
242
+ ...gltfPrimitive,
243
+ attributes: {},
244
+ indices: undefined,
245
+ material: undefined
246
+ };
247
+ const attributes = gltfPrimitive.attributes;
248
+ for (const attribute in attributes) {
249
+ primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
250
+ }
251
+ if (gltfPrimitive.indices !== undefined) {
252
+ primitive.indices = this.getAccessor(gltfPrimitive.indices);
253
+ }
254
+ if (gltfPrimitive.material !== undefined) {
255
+ primitive.material = this.getMaterial(gltfPrimitive.material);
256
+ }
257
+ return primitive;
258
+ });
259
+ }
260
+ return mesh;
268
261
  }
269
- if (material.pbrMetallicRoughness) {
270
- material.pbrMetallicRoughness = {
271
- ...material.pbrMetallicRoughness
272
- };
273
- const mr = material.pbrMetallicRoughness;
274
- if (mr.baseColorTexture) {
275
- mr.baseColorTexture = {
276
- ...mr.baseColorTexture
262
+ _resolveMaterial(gltfMaterial, index) {
263
+ // @ts-expect-error
264
+ const material = {
265
+ ...gltfMaterial,
266
+ // @ts-expect-error
267
+ id: gltfMaterial.id || `material-${index}`
277
268
  };
278
- mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
279
- }
280
- if (mr.metallicRoughnessTexture) {
281
- mr.metallicRoughnessTexture = {
282
- ...mr.metallicRoughnessTexture
269
+ if (material.normalTexture) {
270
+ material.normalTexture = { ...material.normalTexture };
271
+ material.normalTexture.texture = this.getTexture(material.normalTexture.index);
272
+ }
273
+ if (material.occlusionTexture) {
274
+ material.occlusionTexture = { ...material.occlusionTexture };
275
+ material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
276
+ }
277
+ if (material.emissiveTexture) {
278
+ material.emissiveTexture = { ...material.emissiveTexture };
279
+ material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
280
+ }
281
+ if (!material.emissiveFactor) {
282
+ material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
283
+ }
284
+ if (material.pbrMetallicRoughness) {
285
+ material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness };
286
+ const mr = material.pbrMetallicRoughness;
287
+ if (mr.baseColorTexture) {
288
+ mr.baseColorTexture = { ...mr.baseColorTexture };
289
+ mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
290
+ }
291
+ if (mr.metallicRoughnessTexture) {
292
+ mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture };
293
+ mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
294
+ }
295
+ }
296
+ return material;
297
+ }
298
+ _resolveAccessor(gltfAccessor, index) {
299
+ // Look up enums
300
+ const bytesPerComponent = getBytesFromComponentType(gltfAccessor.componentType);
301
+ const components = getSizeFromAccessorType(gltfAccessor.type);
302
+ const bytesPerElement = bytesPerComponent * components;
303
+ const accessor = {
304
+ ...gltfAccessor,
305
+ // @ts-expect-error
306
+ id: gltfAccessor.id || `accessor-${index}`,
307
+ bytesPerComponent,
308
+ components,
309
+ bytesPerElement,
310
+ value: undefined,
311
+ bufferView: undefined,
312
+ sparse: undefined
283
313
  };
284
- mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
285
- }
314
+ if (gltfAccessor.bufferView !== undefined) {
315
+ // Draco encoded meshes don't have bufferView
316
+ accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
317
+ }
318
+ // Create TypedArray for the accessor
319
+ // Note: The canonical way to instantiate is to ignore this array and create
320
+ // WebGLBuffer's using the bufferViews.
321
+ if (accessor.bufferView) {
322
+ const buffer = accessor.bufferView.buffer;
323
+ const { ArrayType, byteLength } = getAccessorArrayTypeAndLength(accessor, accessor.bufferView);
324
+ const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
325
+ let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
326
+ if (accessor.bufferView.byteStride) {
327
+ cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count);
328
+ }
329
+ accessor.value = new ArrayType(cutBuffer);
330
+ }
331
+ return accessor;
286
332
  }
287
- return material;
288
- }
289
- _resolveAccessor(gltfAccessor, index) {
290
- const bytesPerComponent = getBytesFromComponentType(gltfAccessor.componentType);
291
- const components = getSizeFromAccessorType(gltfAccessor.type);
292
- const bytesPerElement = bytesPerComponent * components;
293
- const accessor = {
294
- ...gltfAccessor,
295
- id: gltfAccessor.id || `accessor-${index}`,
296
- bytesPerComponent,
297
- components,
298
- bytesPerElement,
299
- value: undefined,
300
- bufferView: undefined,
301
- sparse: undefined
302
- };
303
- if (gltfAccessor.bufferView !== undefined) {
304
- accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
333
+ /**
334
+ * Take values of particular accessor from interleaved buffer
335
+ * various parts of the buffer
336
+ * @param buffer
337
+ * @param byteOffset
338
+ * @param byteStride
339
+ * @param bytesPerElement
340
+ * @param count
341
+ * @returns
342
+ */
343
+ _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) {
344
+ const result = new Uint8Array(count * bytesPerElement);
345
+ for (let i = 0; i < count; i++) {
346
+ const elementOffset = byteOffset + i * byteStride;
347
+ result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement);
348
+ }
349
+ return result.buffer;
305
350
  }
306
- if (accessor.bufferView) {
307
- const buffer = accessor.bufferView.buffer;
308
- const {
309
- ArrayType,
310
- byteLength
311
- } = getAccessorArrayTypeAndLength(accessor, accessor.bufferView);
312
- const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
313
- let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
314
- if (accessor.bufferView.byteStride) {
315
- cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count);
316
- }
317
- accessor.value = new ArrayType(cutBuffer);
351
+ _resolveTexture(gltfTexture, index) {
352
+ return {
353
+ ...gltfTexture,
354
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
355
+ id: gltfTexture.id || `texture-${index}`,
356
+ sampler: typeof gltfTexture.sampler === 'number'
357
+ ? this.getSampler(gltfTexture.sampler)
358
+ : makeDefaultSampler(),
359
+ source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
360
+ };
318
361
  }
319
- return accessor;
320
- }
321
- _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) {
322
- const result = new Uint8Array(count * bytesPerElement);
323
- for (let i = 0; i < count; i++) {
324
- const elementOffset = byteOffset + i * byteStride;
325
- result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement);
362
+ _resolveSampler(gltfSampler, index) {
363
+ const sampler = {
364
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
365
+ id: gltfSampler.id || `sampler-${index}`,
366
+ ...gltfSampler,
367
+ parameters: {}
368
+ };
369
+ // Map textual parameters to GL parameter values
370
+ for (const key in sampler) {
371
+ const glEnum = this._enumSamplerParameter(key);
372
+ if (glEnum !== undefined) {
373
+ sampler.parameters[glEnum] = sampler[key];
374
+ }
375
+ }
376
+ return sampler;
326
377
  }
327
- return result.buffer;
328
- }
329
- _resolveTexture(gltfTexture, index) {
330
- return {
331
- ...gltfTexture,
332
- id: gltfTexture.id || `texture-${index}`,
333
- sampler: typeof gltfTexture.sampler === 'number' ? this.getSampler(gltfTexture.sampler) : makeDefaultSampler(),
334
- source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
335
- };
336
- }
337
- _resolveSampler(gltfSampler, index) {
338
- const sampler = {
339
- id: gltfSampler.id || `sampler-${index}`,
340
- ...gltfSampler,
341
- parameters: {}
342
- };
343
- for (const key in sampler) {
344
- const glEnum = this._enumSamplerParameter(key);
345
- if (glEnum !== undefined) {
346
- sampler.parameters[glEnum] = sampler[key];
347
- }
378
+ _enumSamplerParameter(key) {
379
+ return SAMPLER_PARAMETER_GLTF_TO_GL[key];
348
380
  }
349
- return sampler;
350
- }
351
- _enumSamplerParameter(key) {
352
- return SAMPLER_PARAMETER_GLTF_TO_GL[key];
353
- }
354
- _resolveImage(gltfImage, index) {
355
- const image = {
356
- ...gltfImage,
357
- id: gltfImage.id || `image-${index}`,
358
- image: null,
359
- bufferView: gltfImage.bufferView !== undefined ? this.getBufferView(gltfImage.bufferView) : undefined
360
- };
361
- const preloadedImage = this.images[index];
362
- if (preloadedImage) {
363
- image.image = preloadedImage;
381
+ _resolveImage(gltfImage, index) {
382
+ const image = {
383
+ ...gltfImage,
384
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
385
+ id: gltfImage.id || `image-${index}`,
386
+ image: null,
387
+ bufferView: gltfImage.bufferView !== undefined ? this.getBufferView(gltfImage.bufferView) : undefined
388
+ };
389
+ // Check if image has been preloaded by the GLTFLoader
390
+ // If so, link it into the JSON and drop the URI
391
+ const preloadedImage = this.images[index];
392
+ if (preloadedImage) {
393
+ image.image = preloadedImage;
394
+ }
395
+ return image;
396
+ }
397
+ _resolveBufferView(gltfBufferView, index) {
398
+ const bufferIndex = gltfBufferView.buffer;
399
+ const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
400
+ // Add offset of buffer, then offset of buffer view
401
+ let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
402
+ if (gltfBufferView.byteOffset) {
403
+ byteOffset += gltfBufferView.byteOffset;
404
+ }
405
+ const bufferView = {
406
+ // // @ts-expect-error id could already be present, glTF standard does not prevent it
407
+ id: `bufferView-${index}`,
408
+ ...gltfBufferView,
409
+ // ...this.buffers[bufferIndex],
410
+ buffer: this.buffers[bufferIndex],
411
+ data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
412
+ };
413
+ return bufferView;
364
414
  }
365
- return image;
366
- }
367
- _resolveBufferView(gltfBufferView, index) {
368
- const bufferIndex = gltfBufferView.buffer;
369
- const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
370
- let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
371
- if (gltfBufferView.byteOffset) {
372
- byteOffset += gltfBufferView.byteOffset;
415
+ _resolveCamera(gltfCamera, index) {
416
+ const camera = {
417
+ ...gltfCamera,
418
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
419
+ id: gltfCamera.id || `camera-${index}`
420
+ };
421
+ // TODO - create 4x4 matrices
422
+ if (camera.perspective) {
423
+ // camera.matrix = createPerspectiveMatrix(camera.perspective);
424
+ }
425
+ if (camera.orthographic) {
426
+ // camera.matrix = createOrthographicMatrix(camera.orthographic);
427
+ }
428
+ return camera;
373
429
  }
374
- const bufferView = {
375
- id: `bufferView-${index}`,
376
- ...gltfBufferView,
377
- buffer: this.buffers[bufferIndex],
378
- data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
379
- };
380
- return bufferView;
381
- }
382
- _resolveCamera(gltfCamera, index) {
383
- const camera = {
384
- ...gltfCamera,
385
- id: gltfCamera.id || `camera-${index}`
386
- };
387
- if (camera.perspective) {}
388
- if (camera.orthographic) {}
389
- return camera;
390
- }
391
430
  }
392
431
  export function postProcessGLTF(gltf, options) {
393
- return new GLTFPostProcessor().postProcess(gltf, options);
432
+ return new GLTFPostProcessor().postProcess(gltf, options);
394
433
  }
395
- //# sourceMappingURL=post-process-gltf.js.map