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

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 (148) hide show
  1. package/dist/dist.dev.js +1049 -517
  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 +95 -284
  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 +561 -438
  24. package/dist/lib/api/normalize-gltf-v1.js +250 -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 +375 -339
  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 +194 -154
  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 +3 -1
  103. package/dist/meshopt/meshopt-decoder.js +86 -67
  104. package/dist/webp/webp.js +28 -19
  105. package/package.json +12 -8
  106. package/dist/glb-loader.js.map +0 -1
  107. package/dist/glb-writer.js.map +0 -1
  108. package/dist/gltf-loader.js.map +0 -1
  109. package/dist/gltf-writer.js.map +0 -1
  110. package/dist/index.js.map +0 -1
  111. package/dist/lib/api/gltf-extensions.js.map +0 -1
  112. package/dist/lib/api/gltf-scenegraph.js.map +0 -1
  113. package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
  114. package/dist/lib/api/post-process-gltf.js.map +0 -1
  115. package/dist/lib/encoders/encode-glb.js.map +0 -1
  116. package/dist/lib/encoders/encode-gltf.js.map +0 -1
  117. package/dist/lib/extensions/EXT_mesh_features.js.map +0 -1
  118. package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  119. package/dist/lib/extensions/EXT_structural_metadata.js.map +0 -1
  120. package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
  121. package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
  122. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  123. package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
  124. package/dist/lib/extensions/KHR_texture_transform.js.map +0 -1
  125. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js.map +0 -1
  126. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  127. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  128. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  129. package/dist/lib/extensions/utils/3d-tiles-utils.js.map +0 -1
  130. package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
  131. package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  132. package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
  133. package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
  134. package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
  135. package/dist/lib/parsers/parse-glb.js.map +0 -1
  136. package/dist/lib/parsers/parse-gltf.js.map +0 -1
  137. package/dist/lib/types/glb-types.js.map +0 -1
  138. package/dist/lib/types/gltf-ext-feature-metadata-schema.js.map +0 -1
  139. package/dist/lib/types/gltf-ext-mesh-features-schema.js.map +0 -1
  140. package/dist/lib/types/gltf-ext-structural-metadata-schema.js.map +0 -1
  141. package/dist/lib/types/gltf-json-schema.js.map +0 -1
  142. package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
  143. package/dist/lib/types/gltf-types.js.map +0 -1
  144. package/dist/lib/utils/assert.js.map +0 -1
  145. package/dist/lib/utils/version.js.map +0 -1
  146. package/dist/meshopt/meshopt-decoder.js.map +0 -1
  147. package/dist/meshopt/meshopt-encoder.ts.disabled +0 -409
  148. package/dist/webp/webp.js.map +0 -1
@@ -1,395 +1,431 @@
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));
69
+ constructor() {
70
+ this.baseUri = '';
71
+ this.buffers = [];
72
+ this.images = [];
87
73
  }
88
- if (gltf.images) {
89
- json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
74
+ postProcess(gltf, options = {}) {
75
+ const { json, buffers = [], images = [] } = gltf;
76
+ // @ts-expect-error
77
+ const { baseUri = '' } = gltf;
78
+ assert(json);
79
+ this.baseUri = baseUri;
80
+ this.buffers = buffers;
81
+ this.images = images;
82
+ this.jsonUnprocessed = json;
83
+ this.json = this._resolveTree(gltf.json, options);
84
+ return this.json;
90
85
  }
91
- if (gltf.samplers) {
92
- json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
86
+ // Convert indexed glTF structure into tree structure
87
+ // cross-link index resolution, enum lookup, convenience calculations
88
+ // eslint-disable-next-line complexity, max-statements
89
+ _resolveTree(gltf, options = {}) {
90
+ // @ts-expect-error
91
+ const json = { ...gltf };
92
+ this.json = json;
93
+ if (gltf.bufferViews) {
94
+ json.bufferViews = gltf.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
95
+ }
96
+ if (gltf.images) {
97
+ json.images = gltf.images.map((image, i) => this._resolveImage(image, i));
98
+ }
99
+ if (gltf.samplers) {
100
+ json.samplers = gltf.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
101
+ }
102
+ if (gltf.textures) {
103
+ json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
104
+ }
105
+ if (gltf.accessors) {
106
+ json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
107
+ }
108
+ if (gltf.materials) {
109
+ json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
110
+ }
111
+ if (gltf.meshes) {
112
+ json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
113
+ }
114
+ if (gltf.nodes) {
115
+ json.nodes = gltf.nodes.map((node, i) => this._resolveNode(node, i));
116
+ json.nodes = json.nodes.map((node, i) => this._resolveNodeChildren(node));
117
+ }
118
+ if (gltf.skins) {
119
+ json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
120
+ }
121
+ if (gltf.scenes) {
122
+ json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
123
+ }
124
+ if (typeof this.json.scene === 'number' && json.scenes) {
125
+ json.scene = json.scenes[this.json.scene];
126
+ }
127
+ return json;
93
128
  }
94
- if (gltf.textures) {
95
- json.textures = gltf.textures.map((texture, i) => this._resolveTexture(texture, i));
129
+ getScene(index) {
130
+ return this._get(this.json.scenes, index);
96
131
  }
97
- if (gltf.accessors) {
98
- json.accessors = gltf.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
132
+ getNode(index) {
133
+ return this._get(this.json.nodes, index);
99
134
  }
100
- if (gltf.materials) {
101
- json.materials = gltf.materials.map((material, i) => this._resolveMaterial(material, i));
135
+ getSkin(index) {
136
+ return this._get(this.json.skins, index);
102
137
  }
103
- if (gltf.meshes) {
104
- json.meshes = gltf.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
138
+ getMesh(index) {
139
+ return this._get(this.json.meshes, index);
105
140
  }
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));
141
+ getMaterial(index) {
142
+ return this._get(this.json.materials, index);
109
143
  }
110
- if (gltf.skins) {
111
- json.skins = gltf.skins.map((skin, i) => this._resolveSkin(skin, i));
144
+ getAccessor(index) {
145
+ return this._get(this.json.accessors, index);
112
146
  }
113
- if (gltf.scenes) {
114
- json.scenes = gltf.scenes.map((scene, i) => this._resolveScene(scene, i));
147
+ getCamera(index) {
148
+ return this._get(this.json.cameras, index);
115
149
  }
116
- if (typeof this.json.scene === 'number' && json.scenes) {
117
- json.scene = json.scenes[this.json.scene];
150
+ getTexture(index) {
151
+ return this._get(this.json.textures, index);
118
152
  }
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;
153
+ getSampler(index) {
154
+ return this._get(this.json.samplers, index);
160
155
  }
161
- const object = array && array[index];
162
- if (!object) {
163
- console.warn(`glTF file error: Could not find ${array}[${index}]`);
156
+ getImage(index) {
157
+ return this._get(this.json.images, index);
164
158
  }
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);
159
+ getBufferView(index) {
160
+ return this._get(this.json.bufferViews, index);
181
161
  }
182
- if (gltfNode.camera !== undefined) {
183
- node.camera = this.getCamera(gltfNode.camera);
162
+ getBuffer(index) {
163
+ return this._get(this.json.buffers, index);
184
164
  }
185
- if (gltfNode.skin !== undefined) {
186
- node.skin = this.getSkin(gltfNode.skin);
187
- }
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
- });
165
+ _get(array, index) {
166
+ // check if already resolved
167
+ if (typeof index === 'object') {
168
+ return index;
169
+ }
170
+ const object = array && array[index];
171
+ if (!object) {
172
+ console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
173
+ }
174
+ return object;
197
175
  }
198
- return node;
199
- }
200
- _resolveNodeChildren(node) {
201
- if (node.children) {
202
- node.children = node.children.map(child => this.getNode(child));
176
+ // PARSING HELPERS
177
+ _resolveScene(scene, index) {
178
+ return {
179
+ ...scene,
180
+ // @ts-ignore
181
+ id: scene.id || `scene-${index}`,
182
+ nodes: (scene.nodes || []).map((node) => this.getNode(node))
183
+ };
203
184
  }
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
185
+ _resolveNode(gltfNode, index) {
186
+ // @ts-expect-error
187
+ const node = {
188
+ ...gltfNode,
189
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
190
+ id: gltfNode?.id || `node-${index}`
227
191
  };
228
- const attributes = gltfPrimitive.attributes;
229
- for (const attribute in attributes) {
230
- primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
192
+ if (gltfNode.mesh !== undefined) {
193
+ node.mesh = this.getMesh(gltfNode.mesh);
231
194
  }
232
- if (gltfPrimitive.indices !== undefined) {
233
- primitive.indices = this.getAccessor(gltfPrimitive.indices);
195
+ if (gltfNode.camera !== undefined) {
196
+ node.camera = this.getCamera(gltfNode.camera);
234
197
  }
235
- if (gltfPrimitive.material !== undefined) {
236
- primitive.material = this.getMaterial(gltfPrimitive.material);
198
+ if (gltfNode.skin !== undefined) {
199
+ node.skin = this.getSkin(gltfNode.skin);
237
200
  }
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);
201
+ // TODO deprecated - Delete in v4.0?
202
+ // @ts-expect-error node.meshes does not seem to be part of the GLTF standard
203
+ if (gltfNode.meshes !== undefined && gltfNode.meshes.length) {
204
+ // @ts-expect-error
205
+ node.mesh = gltfNode.meshes.reduce((accum, meshIndex) => {
206
+ const mesh = this.getMesh(meshIndex);
207
+ accum.id = mesh.id;
208
+ accum.primitives = accum.primitives.concat(mesh.primitives);
209
+ return accum;
210
+ }, { primitives: [] });
211
+ }
212
+ return node;
253
213
  }
254
- if (material.occlusionTexture) {
255
- material.occlusionTexture = {
256
- ...material.occlusionTexture
257
- };
258
- material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
214
+ _resolveNodeChildren(node) {
215
+ if (node.children) {
216
+ // @ts-expect-error node.children are numbers at this stage
217
+ node.children = node.children.map((child) => this.getNode(child));
218
+ }
219
+ return node;
259
220
  }
260
- if (material.emissiveTexture) {
261
- material.emissiveTexture = {
262
- ...material.emissiveTexture
263
- };
264
- material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
221
+ _resolveSkin(gltfSkin, index) {
222
+ const inverseBindMatrices = typeof gltfSkin.inverseBindMatrices === 'number'
223
+ ? this.getAccessor(gltfSkin.inverseBindMatrices)
224
+ : undefined;
225
+ return {
226
+ ...gltfSkin,
227
+ id: gltfSkin.id || `skin-${index}`,
228
+ inverseBindMatrices
229
+ };
265
230
  }
266
- if (!material.emissiveFactor) {
267
- material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
231
+ _resolveMesh(gltfMesh, index) {
232
+ const mesh = {
233
+ ...gltfMesh,
234
+ id: gltfMesh.id || `mesh-${index}`,
235
+ primitives: []
236
+ };
237
+ if (gltfMesh.primitives) {
238
+ mesh.primitives = gltfMesh.primitives.map((gltfPrimitive) => {
239
+ const primitive = {
240
+ ...gltfPrimitive,
241
+ attributes: {},
242
+ indices: undefined,
243
+ material: undefined
244
+ };
245
+ const attributes = gltfPrimitive.attributes;
246
+ for (const attribute in attributes) {
247
+ primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
248
+ }
249
+ if (gltfPrimitive.indices !== undefined) {
250
+ primitive.indices = this.getAccessor(gltfPrimitive.indices);
251
+ }
252
+ if (gltfPrimitive.material !== undefined) {
253
+ primitive.material = this.getMaterial(gltfPrimitive.material);
254
+ }
255
+ return primitive;
256
+ });
257
+ }
258
+ return mesh;
268
259
  }
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
260
+ _resolveMaterial(gltfMaterial, index) {
261
+ // @ts-expect-error
262
+ const material = {
263
+ ...gltfMaterial,
264
+ // @ts-expect-error
265
+ id: gltfMaterial.id || `material-${index}`
277
266
  };
278
- mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
279
- }
280
- if (mr.metallicRoughnessTexture) {
281
- mr.metallicRoughnessTexture = {
282
- ...mr.metallicRoughnessTexture
267
+ if (material.normalTexture) {
268
+ material.normalTexture = { ...material.normalTexture };
269
+ material.normalTexture.texture = this.getTexture(material.normalTexture.index);
270
+ }
271
+ if (material.occlusionTexture) {
272
+ material.occlusionTexture = { ...material.occlusionTexture };
273
+ material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
274
+ }
275
+ if (material.emissiveTexture) {
276
+ material.emissiveTexture = { ...material.emissiveTexture };
277
+ material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
278
+ }
279
+ if (!material.emissiveFactor) {
280
+ material.emissiveFactor = material.emissiveTexture ? [1, 1, 1] : [0, 0, 0];
281
+ }
282
+ if (material.pbrMetallicRoughness) {
283
+ material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness };
284
+ const mr = material.pbrMetallicRoughness;
285
+ if (mr.baseColorTexture) {
286
+ mr.baseColorTexture = { ...mr.baseColorTexture };
287
+ mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
288
+ }
289
+ if (mr.metallicRoughnessTexture) {
290
+ mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture };
291
+ mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
292
+ }
293
+ }
294
+ return material;
295
+ }
296
+ _resolveAccessor(gltfAccessor, index) {
297
+ // Look up enums
298
+ const bytesPerComponent = getBytesFromComponentType(gltfAccessor.componentType);
299
+ const components = getSizeFromAccessorType(gltfAccessor.type);
300
+ const bytesPerElement = bytesPerComponent * components;
301
+ const accessor = {
302
+ ...gltfAccessor,
303
+ // @ts-expect-error
304
+ id: gltfAccessor.id || `accessor-${index}`,
305
+ bytesPerComponent,
306
+ components,
307
+ bytesPerElement,
308
+ value: undefined,
309
+ bufferView: undefined,
310
+ sparse: undefined
283
311
  };
284
- mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
285
- }
312
+ if (gltfAccessor.bufferView !== undefined) {
313
+ // Draco encoded meshes don't have bufferView
314
+ accessor.bufferView = this.getBufferView(gltfAccessor.bufferView);
315
+ }
316
+ // Create TypedArray for the accessor
317
+ // Note: The canonical way to instantiate is to ignore this array and create
318
+ // WebGLBuffer's using the bufferViews.
319
+ if (accessor.bufferView) {
320
+ const buffer = accessor.bufferView.buffer;
321
+ const { ArrayType, byteLength } = getAccessorArrayTypeAndLength(accessor, accessor.bufferView);
322
+ const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
323
+ let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
324
+ if (accessor.bufferView.byteStride) {
325
+ cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count);
326
+ }
327
+ accessor.value = new ArrayType(cutBuffer);
328
+ }
329
+ return accessor;
286
330
  }
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);
331
+ /**
332
+ * Take values of particular accessor from interleaved buffer
333
+ * various parts of the buffer
334
+ * @param buffer
335
+ * @param byteOffset
336
+ * @param byteStride
337
+ * @param bytesPerElement
338
+ * @param count
339
+ * @returns
340
+ */
341
+ _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) {
342
+ const result = new Uint8Array(count * bytesPerElement);
343
+ for (let i = 0; i < count; i++) {
344
+ const elementOffset = byteOffset + i * byteStride;
345
+ result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement);
346
+ }
347
+ return result.buffer;
305
348
  }
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);
349
+ _resolveTexture(gltfTexture, index) {
350
+ return {
351
+ ...gltfTexture,
352
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
353
+ id: gltfTexture.id || `texture-${index}`,
354
+ sampler: typeof gltfTexture.sampler === 'number'
355
+ ? this.getSampler(gltfTexture.sampler)
356
+ : makeDefaultSampler(),
357
+ source: typeof gltfTexture.source === 'number' ? this.getImage(gltfTexture.source) : undefined
358
+ };
318
359
  }
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);
360
+ _resolveSampler(gltfSampler, index) {
361
+ const sampler = {
362
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
363
+ id: gltfSampler.id || `sampler-${index}`,
364
+ ...gltfSampler,
365
+ parameters: {}
366
+ };
367
+ // Map textual parameters to GL parameter values
368
+ for (const key in sampler) {
369
+ const glEnum = this._enumSamplerParameter(key);
370
+ if (glEnum !== undefined) {
371
+ sampler.parameters[glEnum] = sampler[key];
372
+ }
373
+ }
374
+ return sampler;
326
375
  }
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
- }
376
+ _enumSamplerParameter(key) {
377
+ return SAMPLER_PARAMETER_GLTF_TO_GL[key];
348
378
  }
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;
379
+ _resolveImage(gltfImage, index) {
380
+ const image = {
381
+ ...gltfImage,
382
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
383
+ id: gltfImage.id || `image-${index}`,
384
+ image: null,
385
+ bufferView: gltfImage.bufferView !== undefined ? this.getBufferView(gltfImage.bufferView) : undefined
386
+ };
387
+ // Check if image has been preloaded by the GLTFLoader
388
+ // If so, link it into the JSON and drop the URI
389
+ const preloadedImage = this.images[index];
390
+ if (preloadedImage) {
391
+ image.image = preloadedImage;
392
+ }
393
+ return image;
394
+ }
395
+ _resolveBufferView(gltfBufferView, index) {
396
+ const bufferIndex = gltfBufferView.buffer;
397
+ const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
398
+ // Add offset of buffer, then offset of buffer view
399
+ let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
400
+ if (gltfBufferView.byteOffset) {
401
+ byteOffset += gltfBufferView.byteOffset;
402
+ }
403
+ const bufferView = {
404
+ // // @ts-expect-error id could already be present, glTF standard does not prevent it
405
+ id: `bufferView-${index}`,
406
+ ...gltfBufferView,
407
+ // ...this.buffers[bufferIndex],
408
+ buffer: this.buffers[bufferIndex],
409
+ data: new Uint8Array(arrayBuffer, byteOffset, gltfBufferView.byteLength)
410
+ };
411
+ return bufferView;
364
412
  }
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;
413
+ _resolveCamera(gltfCamera, index) {
414
+ const camera = {
415
+ ...gltfCamera,
416
+ // @ts-expect-error id could already be present, glTF standard does not prevent it
417
+ id: gltfCamera.id || `camera-${index}`
418
+ };
419
+ // TODO - create 4x4 matrices
420
+ if (camera.perspective) {
421
+ // camera.matrix = createPerspectiveMatrix(camera.perspective);
422
+ }
423
+ if (camera.orthographic) {
424
+ // camera.matrix = createOrthographicMatrix(camera.orthographic);
425
+ }
426
+ return camera;
373
427
  }
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
428
  }
392
429
  export function postProcessGLTF(gltf, options) {
393
- return new GLTFPostProcessor().postProcess(gltf, options);
430
+ return new GLTFPostProcessor().postProcess(gltf, options);
394
431
  }
395
- //# sourceMappingURL=post-process-gltf.js.map