@loaders.gl/gltf 3.1.3 → 4.0.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 (211) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/bundle.js.map +1 -0
  3. package/dist/dist.min.js +13 -13
  4. package/dist/glb-loader.js +28 -33
  5. package/dist/glb-loader.js.map +1 -0
  6. package/dist/glb-writer.js +26 -34
  7. package/dist/glb-writer.js.map +1 -0
  8. package/dist/gltf-loader.js +43 -46
  9. package/dist/gltf-loader.js.map +1 -0
  10. package/dist/gltf-writer.js +26 -29
  11. package/dist/gltf-writer.js.map +1 -0
  12. package/dist/index.js +7 -21
  13. package/dist/index.js.map +1 -0
  14. package/dist/lib/api/gltf-extensions.js +31 -71
  15. package/dist/lib/api/gltf-extensions.js.map +1 -0
  16. package/dist/lib/api/gltf-scenegraph.js +512 -550
  17. package/dist/lib/api/gltf-scenegraph.js.map +1 -0
  18. package/dist/lib/api/normalize-gltf-v1.js +229 -274
  19. package/dist/lib/api/normalize-gltf-v1.js.map +1 -0
  20. package/dist/lib/api/post-process-gltf.js +376 -324
  21. package/dist/lib/api/post-process-gltf.js.map +1 -0
  22. package/dist/lib/encoders/encode-glb.js +58 -56
  23. package/dist/lib/encoders/encode-glb.js.map +1 -0
  24. package/dist/lib/encoders/encode-gltf.js +12 -25
  25. package/dist/lib/encoders/encode-gltf.js.map +1 -0
  26. package/dist/lib/extensions/EXT_meshopt_compression.js +49 -47
  27. package/dist/lib/extensions/EXT_meshopt_compression.js.map +1 -0
  28. package/dist/lib/extensions/EXT_texture_webp.js +28 -34
  29. package/dist/lib/extensions/EXT_texture_webp.js.map +1 -0
  30. package/dist/lib/extensions/KHR_binary_gltf.js +23 -39
  31. package/dist/lib/extensions/KHR_binary_gltf.js.map +1 -0
  32. package/dist/lib/extensions/KHR_draco_mesh_compression.js +111 -126
  33. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +1 -0
  34. package/dist/lib/extensions/KHR_texture_basisu.js +19 -28
  35. package/dist/lib/extensions/KHR_texture_basisu.js.map +1 -0
  36. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +44 -57
  37. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
  38. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +32 -42
  39. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
  40. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +62 -75
  41. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
  42. package/dist/lib/gltf-utils/get-typed-array.js +14 -39
  43. package/dist/lib/gltf-utils/get-typed-array.js.map +1 -0
  44. package/dist/lib/gltf-utils/gltf-attribute-utils.js +65 -65
  45. package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +1 -0
  46. package/dist/lib/gltf-utils/gltf-constants.js +30 -36
  47. package/dist/lib/gltf-utils/gltf-constants.js.map +1 -0
  48. package/dist/lib/gltf-utils/gltf-utils.js +45 -52
  49. package/dist/lib/gltf-utils/gltf-utils.js.map +1 -0
  50. package/dist/lib/gltf-utils/resolve-url.js +15 -17
  51. package/dist/lib/gltf-utils/resolve-url.js.map +1 -0
  52. package/dist/lib/parsers/parse-glb.js +107 -120
  53. package/dist/lib/parsers/parse-glb.js.map +1 -0
  54. package/dist/lib/parsers/parse-gltf.js +154 -173
  55. package/dist/lib/parsers/parse-gltf.js.map +1 -0
  56. package/dist/lib/types/glb-types.js +2 -2
  57. package/dist/{es5/lib → lib}/types/glb-types.js.map +0 -0
  58. package/dist/lib/types/gltf-json-schema.js +2 -4
  59. package/dist/{es5/lib → lib}/types/gltf-json-schema.js.map +0 -0
  60. package/dist/lib/types/gltf-postprocessed-schema.js +2 -4
  61. package/dist/{es5/lib → lib}/types/gltf-postprocessed-schema.js.map +0 -0
  62. package/dist/lib/types/gltf-types.js +2 -3
  63. package/dist/{es5/lib → lib}/types/gltf-types.js.map +0 -0
  64. package/dist/lib/utils/assert.js +5 -11
  65. package/dist/lib/utils/assert.js.map +1 -0
  66. package/dist/lib/utils/version.js +2 -7
  67. package/dist/lib/utils/version.js.map +1 -0
  68. package/dist/meshopt/meshopt-decoder.js +82 -97
  69. package/dist/meshopt/meshopt-decoder.js.map +1 -0
  70. package/dist/{es5/meshopt → meshopt}/meshopt-encoder.ts.disabled +0 -0
  71. package/dist/webp/webp.js +23 -32
  72. package/dist/webp/webp.js.map +1 -0
  73. package/package.json +8 -11
  74. package/src/lib/parsers/parse-gltf.ts +1 -1
  75. package/dist/es5/bundle.js +0 -7
  76. package/dist/es5/bundle.js.map +0 -1
  77. package/dist/es5/glb-loader.js +0 -70
  78. package/dist/es5/glb-loader.js.map +0 -1
  79. package/dist/es5/glb-writer.js +0 -41
  80. package/dist/es5/glb-writer.js.map +0 -1
  81. package/dist/es5/gltf-loader.js +0 -97
  82. package/dist/es5/gltf-loader.js.map +0 -1
  83. package/dist/es5/gltf-writer.js +0 -40
  84. package/dist/es5/gltf-writer.js.map +0 -1
  85. package/dist/es5/index.js +0 -56
  86. package/dist/es5/index.js.map +0 -1
  87. package/dist/es5/lib/api/gltf-extensions.js +0 -146
  88. package/dist/es5/lib/api/gltf-extensions.js.map +0 -1
  89. package/dist/es5/lib/api/gltf-scenegraph.js +0 -623
  90. package/dist/es5/lib/api/gltf-scenegraph.js.map +0 -1
  91. package/dist/es5/lib/api/normalize-gltf-v1.js +0 -392
  92. package/dist/es5/lib/api/normalize-gltf-v1.js.map +0 -1
  93. package/dist/es5/lib/api/post-process-gltf.js +0 -502
  94. package/dist/es5/lib/api/post-process-gltf.js.map +0 -1
  95. package/dist/es5/lib/encoders/encode-glb.js +0 -75
  96. package/dist/es5/lib/encoders/encode-glb.js.map +0 -1
  97. package/dist/es5/lib/encoders/encode-gltf.js +0 -26
  98. package/dist/es5/lib/encoders/encode-gltf.js.map +0 -1
  99. package/dist/es5/lib/extensions/EXT_meshopt_compression.js +0 -137
  100. package/dist/es5/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  101. package/dist/es5/lib/extensions/EXT_texture_webp.js +0 -60
  102. package/dist/es5/lib/extensions/EXT_texture_webp.js.map +0 -1
  103. package/dist/es5/lib/extensions/KHR_binary_gltf.js +0 -53
  104. package/dist/es5/lib/extensions/KHR_binary_gltf.js.map +0 -1
  105. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js +0 -331
  106. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  107. package/dist/es5/lib/extensions/KHR_texture_basisu.js +0 -49
  108. package/dist/es5/lib/extensions/KHR_texture_basisu.js.map +0 -1
  109. package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js +0 -129
  110. package/dist/es5/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  111. package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js +0 -97
  112. package/dist/es5/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  113. package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -139
  114. package/dist/es5/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  115. package/dist/es5/lib/gltf-utils/get-typed-array.js +0 -26
  116. package/dist/es5/lib/gltf-utils/get-typed-array.js.map +0 -1
  117. package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js +0 -85
  118. package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  119. package/dist/es5/lib/gltf-utils/gltf-constants.js +0 -48
  120. package/dist/es5/lib/gltf-utils/gltf-constants.js.map +0 -1
  121. package/dist/es5/lib/gltf-utils/gltf-utils.js +0 -69
  122. package/dist/es5/lib/gltf-utils/gltf-utils.js.map +0 -1
  123. package/dist/es5/lib/gltf-utils/resolve-url.js +0 -23
  124. package/dist/es5/lib/gltf-utils/resolve-url.js.map +0 -1
  125. package/dist/es5/lib/parsers/parse-glb.js +0 -142
  126. package/dist/es5/lib/parsers/parse-glb.js.map +0 -1
  127. package/dist/es5/lib/parsers/parse-gltf.js +0 -353
  128. package/dist/es5/lib/parsers/parse-gltf.js.map +0 -1
  129. package/dist/es5/lib/types/glb-types.js +0 -2
  130. package/dist/es5/lib/types/gltf-json-schema.js +0 -2
  131. package/dist/es5/lib/types/gltf-postprocessed-schema.js +0 -2
  132. package/dist/es5/lib/types/gltf-types.js +0 -2
  133. package/dist/es5/lib/utils/assert.js +0 -13
  134. package/dist/es5/lib/utils/assert.js.map +0 -1
  135. package/dist/es5/lib/utils/version.js +0 -9
  136. package/dist/es5/lib/utils/version.js.map +0 -1
  137. package/dist/es5/meshopt/meshopt-decoder.js +0 -270
  138. package/dist/es5/meshopt/meshopt-decoder.js.map +0 -1
  139. package/dist/es5/webp/webp.js +0 -104
  140. package/dist/es5/webp/webp.js.map +0 -1
  141. package/dist/esm/bundle.js +0 -5
  142. package/dist/esm/bundle.js.map +0 -1
  143. package/dist/esm/glb-loader.js +0 -34
  144. package/dist/esm/glb-loader.js.map +0 -1
  145. package/dist/esm/glb-writer.js +0 -29
  146. package/dist/esm/glb-writer.js.map +0 -1
  147. package/dist/esm/gltf-loader.js +0 -47
  148. package/dist/esm/gltf-loader.js.map +0 -1
  149. package/dist/esm/gltf-writer.js +0 -29
  150. package/dist/esm/gltf-writer.js.map +0 -1
  151. package/dist/esm/index.js +0 -7
  152. package/dist/esm/index.js.map +0 -1
  153. package/dist/esm/lib/api/gltf-extensions.js +0 -35
  154. package/dist/esm/lib/api/gltf-extensions.js.map +0 -1
  155. package/dist/esm/lib/api/gltf-scenegraph.js +0 -515
  156. package/dist/esm/lib/api/gltf-scenegraph.js.map +0 -1
  157. package/dist/esm/lib/api/normalize-gltf-v1.js +0 -250
  158. package/dist/esm/lib/api/normalize-gltf-v1.js.map +0 -1
  159. package/dist/esm/lib/api/post-process-gltf.js +0 -422
  160. package/dist/esm/lib/api/post-process-gltf.js.map +0 -1
  161. package/dist/esm/lib/encoders/encode-glb.js +0 -63
  162. package/dist/esm/lib/encoders/encode-glb.js.map +0 -1
  163. package/dist/esm/lib/encoders/encode-gltf.js +0 -14
  164. package/dist/esm/lib/encoders/encode-gltf.js.map +0 -1
  165. package/dist/esm/lib/extensions/EXT_meshopt_compression.js +0 -56
  166. package/dist/esm/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  167. package/dist/esm/lib/extensions/EXT_texture_webp.js +0 -32
  168. package/dist/esm/lib/extensions/EXT_texture_webp.js.map +0 -1
  169. package/dist/esm/lib/extensions/KHR_binary_gltf.js +0 -26
  170. package/dist/esm/lib/extensions/KHR_binary_gltf.js.map +0 -1
  171. package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js +0 -126
  172. package/dist/esm/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  173. package/dist/esm/lib/extensions/KHR_texture_basisu.js +0 -22
  174. package/dist/esm/lib/extensions/KHR_texture_basisu.js.map +0 -1
  175. package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js +0 -49
  176. package/dist/esm/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  177. package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js +0 -37
  178. package/dist/esm/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  179. package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -68
  180. package/dist/esm/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  181. package/dist/esm/lib/gltf-utils/get-typed-array.js +0 -16
  182. package/dist/esm/lib/gltf-utils/get-typed-array.js.map +0 -1
  183. package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js +0 -73
  184. package/dist/esm/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  185. package/dist/esm/lib/gltf-utils/gltf-constants.js +0 -37
  186. package/dist/esm/lib/gltf-utils/gltf-constants.js.map +0 -1
  187. package/dist/esm/lib/gltf-utils/gltf-utils.js +0 -56
  188. package/dist/esm/lib/gltf-utils/gltf-utils.js.map +0 -1
  189. package/dist/esm/lib/gltf-utils/resolve-url.js +0 -16
  190. package/dist/esm/lib/gltf-utils/resolve-url.js.map +0 -1
  191. package/dist/esm/lib/parsers/parse-glb.js +0 -128
  192. package/dist/esm/lib/parsers/parse-glb.js.map +0 -1
  193. package/dist/esm/lib/parsers/parse-gltf.js +0 -170
  194. package/dist/esm/lib/parsers/parse-gltf.js.map +0 -1
  195. package/dist/esm/lib/types/glb-types.js +0 -2
  196. package/dist/esm/lib/types/glb-types.js.map +0 -1
  197. package/dist/esm/lib/types/gltf-json-schema.js +0 -2
  198. package/dist/esm/lib/types/gltf-json-schema.js.map +0 -1
  199. package/dist/esm/lib/types/gltf-postprocessed-schema.js +0 -2
  200. package/dist/esm/lib/types/gltf-postprocessed-schema.js.map +0 -1
  201. package/dist/esm/lib/types/gltf-types.js +0 -2
  202. package/dist/esm/lib/types/gltf-types.js.map +0 -1
  203. package/dist/esm/lib/utils/assert.js +0 -6
  204. package/dist/esm/lib/utils/assert.js.map +0 -1
  205. package/dist/esm/lib/utils/version.js +0 -2
  206. package/dist/esm/lib/utils/version.js.map +0 -1
  207. package/dist/esm/meshopt/meshopt-decoder.js +0 -103
  208. package/dist/esm/meshopt/meshopt-decoder.js.map +0 -1
  209. package/dist/esm/meshopt/meshopt-encoder.ts.disabled +0 -409
  210. package/dist/esm/webp/webp.js +0 -29
  211. package/dist/esm/webp/webp.js.map +0 -1
@@ -1,553 +1,515 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const images_1 = require("@loaders.gl/images");
4
- const loader_utils_1 = require("@loaders.gl/loader-utils");
5
- const assert_1 = require("../utils/assert");
6
- const gltf_utils_1 = require("../gltf-utils/gltf-utils");
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import { getBinaryImageMetadata } from '@loaders.gl/images';
3
+ import { padToNBytes, copyToArray } from '@loaders.gl/loader-utils';
4
+ import { assert } from '../utils/assert';
5
+ import { getAccessorArrayTypeAndLength, getAccessorTypeFromSize, getComponentTypeFromArray } from '../gltf-utils/gltf-utils';
7
6
  const DEFAULT_GLTF_JSON = {
8
- asset: {
9
- version: '2.0',
10
- generator: 'loaders.gl'
11
- },
12
- buffers: []
7
+ asset: {
8
+ version: '2.0',
9
+ generator: 'loaders.gl'
10
+ },
11
+ buffers: []
13
12
  };
14
- /**
15
- * Class for structured access to GLTF data
16
- */
17
- class GLTFScenegraph {
18
- constructor(gltf) {
19
- // @ts-ignore
20
- this.gltf = gltf || {
21
- json: { ...DEFAULT_GLTF_JSON },
22
- buffers: []
23
- };
24
- this.sourceBuffers = [];
25
- this.byteLength = 0;
26
- // Initialize buffers
27
- if (this.gltf.buffers && this.gltf.buffers[0]) {
28
- this.byteLength = this.gltf.buffers[0].byteLength;
29
- this.sourceBuffers = [this.gltf.buffers[0]];
30
- }
31
- }
32
- // Accessors
33
- get json() {
34
- return this.gltf.json;
35
- }
36
- getApplicationData(key) {
37
- // TODO - Data is already unpacked by GLBParser
38
- const data = this.json[key];
39
- return data;
40
- }
41
- getExtraData(key) {
42
- // TODO - Data is already unpacked by GLBParser
43
- const extras = this.json.extras || {};
44
- return extras[key];
45
- }
46
- getExtension(extensionName) {
47
- const isExtension = this.getUsedExtensions().find((name) => name === extensionName);
48
- const extensions = this.json.extensions || {};
49
- return isExtension ? extensions[extensionName] || true : null;
50
- }
51
- getRequiredExtension(extensionName) {
52
- const isRequired = this.getRequiredExtensions().find((name) => name === extensionName);
53
- return isRequired ? this.getExtension(extensionName) : null;
54
- }
55
- getRequiredExtensions() {
56
- return this.json.extensionsRequired || [];
57
- }
58
- getUsedExtensions() {
59
- return this.json.extensionsUsed || [];
60
- }
61
- getObjectExtension(object, extensionName) {
62
- const extensions = object.extensions || {};
63
- return extensions[extensionName];
64
- }
65
- getScene(index) {
66
- return this.getObject('scenes', index);
67
- }
68
- getNode(index) {
69
- return this.getObject('nodes', index);
70
- }
71
- getSkin(index) {
72
- return this.getObject('skins', index);
73
- }
74
- getMesh(index) {
75
- return this.getObject('meshes', index);
76
- }
77
- getMaterial(index) {
78
- return this.getObject('materials', index);
79
- }
80
- getAccessor(index) {
81
- return this.getObject('accessors', index);
82
- }
83
- // getCamera(index: number): object | null {
84
- // return null; // TODO: fix thi: object as null;
85
- // }
86
- getTexture(index) {
87
- return this.getObject('textures', index);
88
- }
89
- getSampler(index) {
90
- return this.getObject('samplers', index);
91
- }
92
- getImage(index) {
93
- return this.getObject('images', index);
94
- }
95
- getBufferView(index) {
96
- return this.getObject('bufferViews', index);
97
- }
98
- getBuffer(index) {
99
- return this.getObject('buffers', index);
100
- }
101
- getObject(array, index) {
102
- // check if already resolved
103
- if (typeof index === 'object') {
104
- return index;
105
- }
106
- const object = this.json[array] && this.json[array][index];
107
- if (!object) {
108
- throw new Error(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
109
- }
110
- return object;
111
- }
112
- /**
113
- * Accepts buffer view index or buffer view object
114
- * @returns a `Uint8Array`
115
- */
116
- getTypedArrayForBufferView(bufferView) {
117
- bufferView = this.getBufferView(bufferView);
118
- // @ts-ignore
119
- const bufferIndex = bufferView.buffer;
120
- // Get hold of the arrayBuffer
121
- // const buffer = this.getBuffer(bufferIndex);
122
- const binChunk = this.gltf.buffers[bufferIndex];
123
- (0, assert_1.assert)(binChunk);
124
- // @ts-ignore
125
- const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
126
- // @ts-ignore
127
- return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
128
- }
129
- /** Accepts accessor index or accessor object
130
- * @returns a typed array with type that matches the types
131
- */
132
- getTypedArrayForAccessor(accessor) {
133
- // @ts-ignore
134
- accessor = this.getAccessor(accessor);
135
- // @ts-ignore
136
- const bufferView = this.getBufferView(accessor.bufferView);
137
- const buffer = this.getBuffer(bufferView.buffer);
138
- // @ts-ignore
139
- const arrayBuffer = buffer.data;
140
- // Create a new typed array as a view into the combined buffer
141
- const { ArrayType, length } = (0, gltf_utils_1.getAccessorArrayTypeAndLength)(accessor, bufferView);
142
- // @ts-ignore
143
- const byteOffset = bufferView.byteOffset + accessor.byteOffset;
144
- return new ArrayType(arrayBuffer, byteOffset, length);
145
- }
146
- /** accepts accessor index or accessor object
147
- * returns a `Uint8Array`
148
- */
149
- getTypedArrayForImageData(image) {
150
- // @ts-ignore
151
- image = this.getAccessor(image);
152
- // @ts-ignore
153
- const bufferView = this.getBufferView(image.bufferView);
154
- const buffer = this.getBuffer(bufferView.buffer);
155
- // @ts-ignore
156
- const arrayBuffer = buffer.data;
157
- const byteOffset = bufferView.byteOffset || 0;
158
- return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
159
- }
160
- // MODIFERS
161
- /**
162
- * Add an extra application-defined key to the top-level data structure
163
- */
164
- addApplicationData(key, data) {
165
- this.json[key] = data;
166
- return this;
167
- }
168
- /**
169
- * `extras` - Standard GLTF field for storing application specific data
170
- */
171
- addExtraData(key, data) {
172
- this.json.extras = this.json.extras || {};
173
- this.json.extras[key] = data;
174
- return this;
175
- }
176
- addObjectExtension(object, extensionName, data) {
177
- // @ts-ignore
178
- object.extensions = object.extensions || {};
179
- // TODO - clobber or merge?
180
- // @ts-ignore
181
- object.extensions[extensionName] = data;
182
- this.registerUsedExtension(extensionName);
183
- return this;
184
- }
185
- setObjectExtension(object, extensionName, data) {
186
- // @ts-ignore
187
- const extensions = object.extensions || {};
188
- extensions[extensionName] = data;
189
- // TODO - add to usedExtensions...
190
- }
191
- removeObjectExtension(object, extensionName) {
192
- // @ts-ignore
193
- const extensions = object.extensions || {};
194
- const extension = extensions[extensionName];
195
- delete extensions[extensionName];
196
- return extension;
197
- }
198
- /**
199
- * Add to standard GLTF top level extension object, mark as used
200
- */
201
- addExtension(extensionName, extensionData = {}) {
202
- (0, assert_1.assert)(extensionData);
203
- this.json.extensions = this.json.extensions || {};
204
- this.json.extensions[extensionName] = extensionData;
205
- this.registerUsedExtension(extensionName);
206
- return extensionData;
207
- }
208
- /**
209
- * Standard GLTF top level extension object, mark as used and required
210
- */
211
- addRequiredExtension(extensionName, extensionData = {}) {
212
- (0, assert_1.assert)(extensionData);
213
- this.addExtension(extensionName, extensionData);
214
- this.registerRequiredExtension(extensionName);
215
- return extensionData;
216
- }
217
- /**
218
- * Add extensionName to list of used extensions
219
- */
220
- registerUsedExtension(extensionName) {
221
- this.json.extensionsUsed = this.json.extensionsUsed || [];
222
- if (!this.json.extensionsUsed.find((ext) => ext === extensionName)) {
223
- this.json.extensionsUsed.push(extensionName);
224
- }
225
- }
226
- /**
227
- * Add extensionName to list of required extensions
228
- */
229
- registerRequiredExtension(extensionName) {
230
- this.registerUsedExtension(extensionName);
231
- this.json.extensionsRequired = this.json.extensionsRequired || [];
232
- if (!this.json.extensionsRequired.find((ext) => ext === extensionName)) {
233
- this.json.extensionsRequired.push(extensionName);
234
- }
235
- }
236
- /**
237
- * Removes an extension from the top-level list
238
- */
239
- removeExtension(extensionName) {
240
- if (this.json.extensionsRequired) {
241
- this._removeStringFromArray(this.json.extensionsRequired, extensionName);
242
- }
243
- if (this.json.extensionsUsed) {
244
- this._removeStringFromArray(this.json.extensionsUsed, extensionName);
245
- }
246
- if (this.json.extensions) {
247
- delete this.json.extensions[extensionName];
248
- }
249
- }
250
- /**
251
- * Set default scene which is to be displayed at load time
252
- */
253
- setDefaultScene(sceneIndex) {
254
- this.json.scene = sceneIndex;
255
- }
256
- /**
257
- * @todo: add more properties for scene initialization:
258
- * name`, `extensions`, `extras`
259
- * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#reference-scene
260
- */
261
- addScene(scene) {
262
- const { nodeIndices } = scene;
263
- this.json.scenes = this.json.scenes || [];
264
- this.json.scenes.push({ nodes: nodeIndices });
265
- return this.json.scenes.length - 1;
266
- }
267
- /**
268
- * @todo: add more properties for node initialization:
269
- * `name`, `extensions`, `extras`, `camera`, `children`, `skin`, `rotation`, `scale`, `translation`, `weights`
270
- * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#node
271
- */
272
- addNode(node) {
273
- const { meshIndex, matrix } = node;
274
- this.json.nodes = this.json.nodes || [];
275
- const nodeData = { mesh: meshIndex };
276
- if (matrix) {
277
- // @ts-ignore
278
- nodeData.matrix = matrix;
279
- }
280
- this.json.nodes.push(nodeData);
281
- return this.json.nodes.length - 1;
282
- }
283
- /** Adds a mesh to the json part */
284
- addMesh(mesh) {
285
- const { attributes, indices, material, mode = 4 } = mesh;
286
- const accessors = this._addAttributes(attributes);
287
- const glTFMesh = {
288
- primitives: [
289
- {
290
- attributes: accessors,
291
- mode
292
- }
293
- ]
294
- };
295
- if (indices) {
296
- const indicesAccessor = this._addIndices(indices);
297
- // @ts-ignore
298
- glTFMesh.primitives[0].indices = indicesAccessor;
299
- }
300
- if (Number.isFinite(material)) {
301
- // @ts-ignore
302
- glTFMesh.primitives[0].material = material;
303
- }
304
- this.json.meshes = this.json.meshes || [];
305
- this.json.meshes.push(glTFMesh);
306
- return this.json.meshes.length - 1;
307
- }
308
- addPointCloud(attributes) {
309
- // @ts-ignore
310
- const accessorIndices = this._addAttributes(attributes);
311
- const glTFMesh = {
312
- primitives: [
313
- {
314
- attributes: accessorIndices,
315
- mode: 0 // GL.POINTS
316
- }
317
- ]
318
- };
319
- this.json.meshes = this.json.meshes || [];
320
- this.json.meshes.push(glTFMesh);
321
- return this.json.meshes.length - 1;
322
- }
323
- /**
324
- * Adds a binary image. Builds glTF "JSON metadata" and saves buffer reference
325
- * Buffer will be copied into BIN chunk during "pack"
326
- * Currently encodes as glTF image
327
- * @param imageData
328
- * @param mimeType
329
- */
330
- addImage(imageData, mimeTypeOpt) {
331
- // If image is referencing a bufferView instead of URI, mimeType must be defined:
332
- // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#images
333
- // "a reference to a bufferView; in that case mimeType must be defined."
334
- const metadata = (0, images_1.getBinaryImageMetadata)(imageData);
335
- const mimeType = mimeTypeOpt || metadata?.mimeType;
336
- const bufferViewIndex = this.addBufferView(imageData);
337
- const glTFImage = {
338
- bufferView: bufferViewIndex,
339
- mimeType
340
- };
341
- this.json.images = this.json.images || [];
342
- this.json.images.push(glTFImage);
343
- return this.json.images.length - 1;
344
- }
345
- /**
346
- * Add one untyped source buffer, create a matching glTF `bufferView`, and return its index
347
- * @param buffer
348
- */
349
- addBufferView(buffer) {
350
- const byteLength = buffer.byteLength;
351
- (0, assert_1.assert)(Number.isFinite(byteLength));
352
- // Add this buffer to the list of buffers to be written to the body.
353
- this.sourceBuffers = this.sourceBuffers || [];
354
- this.sourceBuffers.push(buffer);
355
- const glTFBufferView = {
356
- buffer: 0,
357
- // Write offset from the start of the binary body
358
- byteOffset: this.byteLength,
359
- byteLength
360
- };
361
- // We've now added the contents to the body, so update the total length
362
- // Every sub-chunk needs to be 4-byte align ed
363
- this.byteLength += (0, loader_utils_1.padToNBytes)(byteLength, 4);
364
- // Add a bufferView indicating start and length of this binary sub-chunk
365
- this.json.bufferViews = this.json.bufferViews || [];
366
- this.json.bufferViews.push(glTFBufferView);
367
- return this.json.bufferViews.length - 1;
368
- }
369
- /**
370
- * Adds an accessor to a bufferView
371
- * @param bufferViewIndex
372
- * @param accessor
373
- */
374
- addAccessor(bufferViewIndex, accessor) {
375
- const glTFAccessor = {
376
- bufferView: bufferViewIndex,
377
- // @ts-ignore
378
- type: (0, gltf_utils_1.getAccessorTypeFromSize)(accessor.size),
379
- // @ts-ignore
380
- componentType: accessor.componentType,
381
- // @ts-ignore
382
- count: accessor.count,
383
- // @ts-ignore
384
- max: accessor.max,
385
- // @ts-ignore
386
- min: accessor.min
387
- };
388
- this.json.accessors = this.json.accessors || [];
389
- this.json.accessors.push(glTFAccessor);
390
- return this.json.accessors.length - 1;
391
- }
392
- /**
393
- * Add a binary buffer. Builds glTF "JSON metadata" and saves buffer reference
394
- * Buffer will be copied into BIN chunk during "pack"
395
- * Currently encodes buffers as glTF accessors, but this could be optimized
396
- * @param sourceBuffer
397
- * @param accessor
398
- */
399
- addBinaryBuffer(sourceBuffer, accessor = { size: 3 }) {
400
- const bufferViewIndex = this.addBufferView(sourceBuffer);
401
- // @ts-ignore
402
- let minMax = { min: accessor.min, max: accessor.max };
403
- if (!minMax.min || !minMax.max) {
404
- // @ts-ignore
405
- minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
406
- }
407
- const accessorDefaults = {
408
- // @ts-ignore
409
- size: accessor.size,
410
- componentType: (0, gltf_utils_1.getComponentTypeFromArray)(sourceBuffer),
411
- // @ts-ignore
412
- count: Math.round(sourceBuffer.length / accessor.size),
413
- min: minMax.min,
414
- max: minMax.max
415
- };
416
- return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
417
- }
418
- /**
419
- * Adds a texture to the json part
420
- * @todo: add more properties for texture initialization
421
- * `sampler`, `name`, `extensions`, `extras`
422
- * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
423
- */
424
- addTexture(texture) {
425
- const { imageIndex } = texture;
426
- const glTFTexture = {
427
- source: imageIndex
428
- };
429
- this.json.textures = this.json.textures || [];
430
- this.json.textures.push(glTFTexture);
431
- return this.json.textures.length - 1;
432
- }
433
- /** Adds a material to the json part */
434
- addMaterial(pbrMaterialInfo) {
435
- this.json.materials = this.json.materials || [];
436
- this.json.materials.push(pbrMaterialInfo);
437
- return this.json.materials.length - 1;
438
- }
439
- /** Pack the binary chunk */
440
- createBinaryChunk() {
441
- // Encoder expects this array undefined or empty
442
- this.gltf.buffers = [];
443
- // Allocate total array
444
- const totalByteLength = this.byteLength;
445
- const arrayBuffer = new ArrayBuffer(totalByteLength);
446
- const targetArray = new Uint8Array(arrayBuffer);
447
- // Copy each array into
448
- let dstByteOffset = 0;
449
- for (const sourceBuffer of this.sourceBuffers || []) {
450
- dstByteOffset = (0, loader_utils_1.copyToArray)(sourceBuffer, targetArray, dstByteOffset);
451
- }
452
- // Update the glTF BIN CHUNK byte length
453
- if (this.json?.buffers?.[0]) {
454
- this.json.buffers[0].byteLength = totalByteLength;
455
- }
456
- else {
457
- this.json.buffers = [{ byteLength: totalByteLength }];
458
- }
459
- // Save generated arrayBuffer
460
- this.gltf.binary = arrayBuffer;
461
- // Put arrayBuffer to sourceBuffers for possible additional writing data in the chunk
462
- this.sourceBuffers = [arrayBuffer];
463
- }
464
- // PRIVATE
465
- _removeStringFromArray(array, string) {
466
- let found = true;
467
- while (found) {
468
- const index = array.indexOf(string);
469
- if (index > -1) {
470
- array.splice(index, 1);
471
- }
472
- else {
473
- found = false;
474
- }
475
- }
476
- }
477
- /**
478
- * Add attributes to buffers and create `attributes` object which is part of `mesh`
479
- */
480
- _addAttributes(attributes = {}) {
481
- const result = {};
482
- for (const attributeKey in attributes) {
483
- const attributeData = attributes[attributeKey];
484
- const attrName = this._getGltfAttributeName(attributeKey);
485
- const accessor = this.addBinaryBuffer(attributeData.value, attributeData);
486
- result[attrName] = accessor;
487
- }
488
- return result;
489
- }
490
- /**
491
- * Add indices to buffers
492
- */
493
- _addIndices(indices) {
494
- return this.addBinaryBuffer(indices, { size: 1 });
495
- }
496
- /**
497
- * Deduce gltf specific attribue name from input attribute name
498
- */
499
- _getGltfAttributeName(attributeName) {
500
- switch (attributeName.toLowerCase()) {
501
- case 'position':
502
- case 'positions':
503
- case 'vertices':
504
- return 'POSITION';
505
- case 'normal':
506
- case 'normals':
507
- return 'NORMAL';
508
- case 'color':
509
- case 'colors':
510
- return 'COLOR_0';
511
- case 'texcoord':
512
- case 'texcoords':
513
- return 'TEXCOORD_0';
514
- default:
515
- return attributeName;
516
- }
517
- }
518
- /**
519
- * Calculate `min` and `max` arrays of accessor according to spec:
520
- * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#reference-accessor
521
- */
522
- _getAccessorMinMax(buffer, size) {
523
- const result = { min: null, max: null };
524
- if (buffer.length < size) {
525
- return result;
526
- }
527
- // @ts-ignore
528
- result.min = [];
529
- // @ts-ignore
530
- result.max = [];
531
- const initValues = buffer.subarray(0, size);
532
- for (const value of initValues) {
533
- // @ts-ignore
534
- result.min.push(value);
535
- // @ts-ignore
536
- result.max.push(value);
537
- }
538
- for (let index = size; index < buffer.length; index += size) {
539
- for (let componentIndex = 0; componentIndex < size; componentIndex++) {
540
- // @ts-ignore
541
- result.min[0 + componentIndex] = Math.min(
542
- // @ts-ignore
543
- result.min[0 + componentIndex], buffer[index + componentIndex]);
544
- // @ts-ignore
545
- result.max[0 + componentIndex] = Math.max(
546
- // @ts-ignore
547
- result.max[0 + componentIndex], buffer[index + componentIndex]);
548
- }
549
- }
550
- return result;
551
- }
13
+ export default class GLTFScenegraph {
14
+ constructor(gltf) {
15
+ _defineProperty(this, "gltf", void 0);
16
+
17
+ _defineProperty(this, "sourceBuffers", void 0);
18
+
19
+ _defineProperty(this, "byteLength", void 0);
20
+
21
+ this.gltf = gltf || {
22
+ json: { ...DEFAULT_GLTF_JSON
23
+ },
24
+ buffers: []
25
+ };
26
+ this.sourceBuffers = [];
27
+ this.byteLength = 0;
28
+
29
+ if (this.gltf.buffers && this.gltf.buffers[0]) {
30
+ this.byteLength = this.gltf.buffers[0].byteLength;
31
+ this.sourceBuffers = [this.gltf.buffers[0]];
32
+ }
33
+ }
34
+
35
+ get json() {
36
+ return this.gltf.json;
37
+ }
38
+
39
+ getApplicationData(key) {
40
+ const data = this.json[key];
41
+ return data;
42
+ }
43
+
44
+ getExtraData(key) {
45
+ const extras = this.json.extras || {};
46
+ return extras[key];
47
+ }
48
+
49
+ getExtension(extensionName) {
50
+ const isExtension = this.getUsedExtensions().find(name => name === extensionName);
51
+ const extensions = this.json.extensions || {};
52
+ return isExtension ? extensions[extensionName] || true : null;
53
+ }
54
+
55
+ getRequiredExtension(extensionName) {
56
+ const isRequired = this.getRequiredExtensions().find(name => name === extensionName);
57
+ return isRequired ? this.getExtension(extensionName) : null;
58
+ }
59
+
60
+ getRequiredExtensions() {
61
+ return this.json.extensionsRequired || [];
62
+ }
63
+
64
+ getUsedExtensions() {
65
+ return this.json.extensionsUsed || [];
66
+ }
67
+
68
+ getObjectExtension(object, extensionName) {
69
+ const extensions = object.extensions || {};
70
+ return extensions[extensionName];
71
+ }
72
+
73
+ getScene(index) {
74
+ return this.getObject('scenes', index);
75
+ }
76
+
77
+ getNode(index) {
78
+ return this.getObject('nodes', index);
79
+ }
80
+
81
+ getSkin(index) {
82
+ return this.getObject('skins', index);
83
+ }
84
+
85
+ getMesh(index) {
86
+ return this.getObject('meshes', index);
87
+ }
88
+
89
+ getMaterial(index) {
90
+ return this.getObject('materials', index);
91
+ }
92
+
93
+ getAccessor(index) {
94
+ return this.getObject('accessors', index);
95
+ }
96
+
97
+ getTexture(index) {
98
+ return this.getObject('textures', index);
99
+ }
100
+
101
+ getSampler(index) {
102
+ return this.getObject('samplers', index);
103
+ }
104
+
105
+ getImage(index) {
106
+ return this.getObject('images', index);
107
+ }
108
+
109
+ getBufferView(index) {
110
+ return this.getObject('bufferViews', index);
111
+ }
112
+
113
+ getBuffer(index) {
114
+ return this.getObject('buffers', index);
115
+ }
116
+
117
+ getObject(array, index) {
118
+ if (typeof index === 'object') {
119
+ return index;
120
+ }
121
+
122
+ const object = this.json[array] && this.json[array][index];
123
+
124
+ if (!object) {
125
+ throw new Error("glTF file error: Could not find ".concat(array, "[").concat(index, "]"));
126
+ }
127
+
128
+ return object;
129
+ }
130
+
131
+ getTypedArrayForBufferView(bufferView) {
132
+ bufferView = this.getBufferView(bufferView);
133
+ const bufferIndex = bufferView.buffer;
134
+ const binChunk = this.gltf.buffers[bufferIndex];
135
+ assert(binChunk);
136
+ const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
137
+ return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
138
+ }
139
+
140
+ getTypedArrayForAccessor(accessor) {
141
+ accessor = this.getAccessor(accessor);
142
+ const bufferView = this.getBufferView(accessor.bufferView);
143
+ const buffer = this.getBuffer(bufferView.buffer);
144
+ const arrayBuffer = buffer.data;
145
+ const {
146
+ ArrayType,
147
+ length
148
+ } = getAccessorArrayTypeAndLength(accessor, bufferView);
149
+ const byteOffset = bufferView.byteOffset + accessor.byteOffset;
150
+ return new ArrayType(arrayBuffer, byteOffset, length);
151
+ }
152
+
153
+ getTypedArrayForImageData(image) {
154
+ image = this.getAccessor(image);
155
+ const bufferView = this.getBufferView(image.bufferView);
156
+ const buffer = this.getBuffer(bufferView.buffer);
157
+ const arrayBuffer = buffer.data;
158
+ const byteOffset = bufferView.byteOffset || 0;
159
+ return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
160
+ }
161
+
162
+ addApplicationData(key, data) {
163
+ this.json[key] = data;
164
+ return this;
165
+ }
166
+
167
+ addExtraData(key, data) {
168
+ this.json.extras = this.json.extras || {};
169
+ this.json.extras[key] = data;
170
+ return this;
171
+ }
172
+
173
+ addObjectExtension(object, extensionName, data) {
174
+ object.extensions = object.extensions || {};
175
+ object.extensions[extensionName] = data;
176
+ this.registerUsedExtension(extensionName);
177
+ return this;
178
+ }
179
+
180
+ setObjectExtension(object, extensionName, data) {
181
+ const extensions = object.extensions || {};
182
+ extensions[extensionName] = data;
183
+ }
184
+
185
+ removeObjectExtension(object, extensionName) {
186
+ const extensions = object.extensions || {};
187
+ const extension = extensions[extensionName];
188
+ delete extensions[extensionName];
189
+ return extension;
190
+ }
191
+
192
+ addExtension(extensionName, extensionData = {}) {
193
+ assert(extensionData);
194
+ this.json.extensions = this.json.extensions || {};
195
+ this.json.extensions[extensionName] = extensionData;
196
+ this.registerUsedExtension(extensionName);
197
+ return extensionData;
198
+ }
199
+
200
+ addRequiredExtension(extensionName, extensionData = {}) {
201
+ assert(extensionData);
202
+ this.addExtension(extensionName, extensionData);
203
+ this.registerRequiredExtension(extensionName);
204
+ return extensionData;
205
+ }
206
+
207
+ registerUsedExtension(extensionName) {
208
+ this.json.extensionsUsed = this.json.extensionsUsed || [];
209
+
210
+ if (!this.json.extensionsUsed.find(ext => ext === extensionName)) {
211
+ this.json.extensionsUsed.push(extensionName);
212
+ }
213
+ }
214
+
215
+ registerRequiredExtension(extensionName) {
216
+ this.registerUsedExtension(extensionName);
217
+ this.json.extensionsRequired = this.json.extensionsRequired || [];
218
+
219
+ if (!this.json.extensionsRequired.find(ext => ext === extensionName)) {
220
+ this.json.extensionsRequired.push(extensionName);
221
+ }
222
+ }
223
+
224
+ removeExtension(extensionName) {
225
+ if (this.json.extensionsRequired) {
226
+ this._removeStringFromArray(this.json.extensionsRequired, extensionName);
227
+ }
228
+
229
+ if (this.json.extensionsUsed) {
230
+ this._removeStringFromArray(this.json.extensionsUsed, extensionName);
231
+ }
232
+
233
+ if (this.json.extensions) {
234
+ delete this.json.extensions[extensionName];
235
+ }
236
+ }
237
+
238
+ setDefaultScene(sceneIndex) {
239
+ this.json.scene = sceneIndex;
240
+ }
241
+
242
+ addScene(scene) {
243
+ const {
244
+ nodeIndices
245
+ } = scene;
246
+ this.json.scenes = this.json.scenes || [];
247
+ this.json.scenes.push({
248
+ nodes: nodeIndices
249
+ });
250
+ return this.json.scenes.length - 1;
251
+ }
252
+
253
+ addNode(node) {
254
+ const {
255
+ meshIndex,
256
+ matrix
257
+ } = node;
258
+ this.json.nodes = this.json.nodes || [];
259
+ const nodeData = {
260
+ mesh: meshIndex
261
+ };
262
+
263
+ if (matrix) {
264
+ nodeData.matrix = matrix;
265
+ }
266
+
267
+ this.json.nodes.push(nodeData);
268
+ return this.json.nodes.length - 1;
269
+ }
270
+
271
+ addMesh(mesh) {
272
+ const {
273
+ attributes,
274
+ indices,
275
+ material,
276
+ mode = 4
277
+ } = mesh;
278
+
279
+ const accessors = this._addAttributes(attributes);
280
+
281
+ const glTFMesh = {
282
+ primitives: [{
283
+ attributes: accessors,
284
+ mode
285
+ }]
286
+ };
287
+
288
+ if (indices) {
289
+ const indicesAccessor = this._addIndices(indices);
290
+
291
+ glTFMesh.primitives[0].indices = indicesAccessor;
292
+ }
293
+
294
+ if (Number.isFinite(material)) {
295
+ glTFMesh.primitives[0].material = material;
296
+ }
297
+
298
+ this.json.meshes = this.json.meshes || [];
299
+ this.json.meshes.push(glTFMesh);
300
+ return this.json.meshes.length - 1;
301
+ }
302
+
303
+ addPointCloud(attributes) {
304
+ const accessorIndices = this._addAttributes(attributes);
305
+
306
+ const glTFMesh = {
307
+ primitives: [{
308
+ attributes: accessorIndices,
309
+ mode: 0
310
+ }]
311
+ };
312
+ this.json.meshes = this.json.meshes || [];
313
+ this.json.meshes.push(glTFMesh);
314
+ return this.json.meshes.length - 1;
315
+ }
316
+
317
+ addImage(imageData, mimeTypeOpt) {
318
+ const metadata = getBinaryImageMetadata(imageData);
319
+ const mimeType = mimeTypeOpt || (metadata === null || metadata === void 0 ? void 0 : metadata.mimeType);
320
+ const bufferViewIndex = this.addBufferView(imageData);
321
+ const glTFImage = {
322
+ bufferView: bufferViewIndex,
323
+ mimeType
324
+ };
325
+ this.json.images = this.json.images || [];
326
+ this.json.images.push(glTFImage);
327
+ return this.json.images.length - 1;
328
+ }
329
+
330
+ addBufferView(buffer) {
331
+ const byteLength = buffer.byteLength;
332
+ assert(Number.isFinite(byteLength));
333
+ this.sourceBuffers = this.sourceBuffers || [];
334
+ this.sourceBuffers.push(buffer);
335
+ const glTFBufferView = {
336
+ buffer: 0,
337
+ byteOffset: this.byteLength,
338
+ byteLength
339
+ };
340
+ this.byteLength += padToNBytes(byteLength, 4);
341
+ this.json.bufferViews = this.json.bufferViews || [];
342
+ this.json.bufferViews.push(glTFBufferView);
343
+ return this.json.bufferViews.length - 1;
344
+ }
345
+
346
+ addAccessor(bufferViewIndex, accessor) {
347
+ const glTFAccessor = {
348
+ bufferView: bufferViewIndex,
349
+ type: getAccessorTypeFromSize(accessor.size),
350
+ componentType: accessor.componentType,
351
+ count: accessor.count,
352
+ max: accessor.max,
353
+ min: accessor.min
354
+ };
355
+ this.json.accessors = this.json.accessors || [];
356
+ this.json.accessors.push(glTFAccessor);
357
+ return this.json.accessors.length - 1;
358
+ }
359
+
360
+ addBinaryBuffer(sourceBuffer, accessor = {
361
+ size: 3
362
+ }) {
363
+ const bufferViewIndex = this.addBufferView(sourceBuffer);
364
+ let minMax = {
365
+ min: accessor.min,
366
+ max: accessor.max
367
+ };
368
+
369
+ if (!minMax.min || !minMax.max) {
370
+ minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
371
+ }
372
+
373
+ const accessorDefaults = {
374
+ size: accessor.size,
375
+ componentType: getComponentTypeFromArray(sourceBuffer),
376
+ count: Math.round(sourceBuffer.length / accessor.size),
377
+ min: minMax.min,
378
+ max: minMax.max
379
+ };
380
+ return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
381
+ }
382
+
383
+ addTexture(texture) {
384
+ const {
385
+ imageIndex
386
+ } = texture;
387
+ const glTFTexture = {
388
+ source: imageIndex
389
+ };
390
+ this.json.textures = this.json.textures || [];
391
+ this.json.textures.push(glTFTexture);
392
+ return this.json.textures.length - 1;
393
+ }
394
+
395
+ addMaterial(pbrMaterialInfo) {
396
+ this.json.materials = this.json.materials || [];
397
+ this.json.materials.push(pbrMaterialInfo);
398
+ return this.json.materials.length - 1;
399
+ }
400
+
401
+ createBinaryChunk() {
402
+ var _this$json, _this$json$buffers;
403
+
404
+ this.gltf.buffers = [];
405
+ const totalByteLength = this.byteLength;
406
+ const arrayBuffer = new ArrayBuffer(totalByteLength);
407
+ const targetArray = new Uint8Array(arrayBuffer);
408
+ let dstByteOffset = 0;
409
+
410
+ for (const sourceBuffer of this.sourceBuffers || []) {
411
+ dstByteOffset = copyToArray(sourceBuffer, targetArray, dstByteOffset);
412
+ }
413
+
414
+ if ((_this$json = this.json) !== null && _this$json !== void 0 && (_this$json$buffers = _this$json.buffers) !== null && _this$json$buffers !== void 0 && _this$json$buffers[0]) {
415
+ this.json.buffers[0].byteLength = totalByteLength;
416
+ } else {
417
+ this.json.buffers = [{
418
+ byteLength: totalByteLength
419
+ }];
420
+ }
421
+
422
+ this.gltf.binary = arrayBuffer;
423
+ this.sourceBuffers = [arrayBuffer];
424
+ }
425
+
426
+ _removeStringFromArray(array, string) {
427
+ let found = true;
428
+
429
+ while (found) {
430
+ const index = array.indexOf(string);
431
+
432
+ if (index > -1) {
433
+ array.splice(index, 1);
434
+ } else {
435
+ found = false;
436
+ }
437
+ }
438
+ }
439
+
440
+ _addAttributes(attributes = {}) {
441
+ const result = {};
442
+
443
+ for (const attributeKey in attributes) {
444
+ const attributeData = attributes[attributeKey];
445
+
446
+ const attrName = this._getGltfAttributeName(attributeKey);
447
+
448
+ const accessor = this.addBinaryBuffer(attributeData.value, attributeData);
449
+ result[attrName] = accessor;
450
+ }
451
+
452
+ return result;
453
+ }
454
+
455
+ _addIndices(indices) {
456
+ return this.addBinaryBuffer(indices, {
457
+ size: 1
458
+ });
459
+ }
460
+
461
+ _getGltfAttributeName(attributeName) {
462
+ switch (attributeName.toLowerCase()) {
463
+ case 'position':
464
+ case 'positions':
465
+ case 'vertices':
466
+ return 'POSITION';
467
+
468
+ case 'normal':
469
+ case 'normals':
470
+ return 'NORMAL';
471
+
472
+ case 'color':
473
+ case 'colors':
474
+ return 'COLOR_0';
475
+
476
+ case 'texcoord':
477
+ case 'texcoords':
478
+ return 'TEXCOORD_0';
479
+
480
+ default:
481
+ return attributeName;
482
+ }
483
+ }
484
+
485
+ _getAccessorMinMax(buffer, size) {
486
+ const result = {
487
+ min: null,
488
+ max: null
489
+ };
490
+
491
+ if (buffer.length < size) {
492
+ return result;
493
+ }
494
+
495
+ result.min = [];
496
+ result.max = [];
497
+ const initValues = buffer.subarray(0, size);
498
+
499
+ for (const value of initValues) {
500
+ result.min.push(value);
501
+ result.max.push(value);
502
+ }
503
+
504
+ for (let index = size; index < buffer.length; index += size) {
505
+ for (let componentIndex = 0; componentIndex < size; componentIndex++) {
506
+ result.min[0 + componentIndex] = Math.min(result.min[0 + componentIndex], buffer[index + componentIndex]);
507
+ result.max[0 + componentIndex] = Math.max(result.max[0 + componentIndex], buffer[index + componentIndex]);
508
+ }
509
+ }
510
+
511
+ return result;
512
+ }
513
+
552
514
  }
553
- exports.default = GLTFScenegraph;
515
+ //# sourceMappingURL=gltf-scenegraph.js.map