@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 +1 @@
1
- {"version":3,"file":"gltf-attribute-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/gltf-utils/gltf-attribute-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,2BAA2B,CAAC;AAO5D,wBAAgB,gBAAgB,CAAC,UAAU,KAAA,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAA;CAAC,CAU1E;AAKD,wBAAgB,eAAe,CAAC,SAAS,KAAA,gBAmBxC"}
1
+ {"version":3,"file":"gltf-attribute-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/gltf-utils/gltf-attribute-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,qCAAkC;AAO5D,wBAAgB,gBAAgB,CAAC,UAAU,KAAA,GAAG;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAA;CAAC,CAU1E;AAKD,wBAAgB,eAAe,CAAC,SAAS,KAAA,gBAmBxC"}
@@ -1,62 +1,68 @@
1
+ // TODO - remove
1
2
  import { getAccessorTypeFromSize, getComponentTypeFromArray } from "./gltf-utils.js";
3
+ // Returns a fresh attributes object with glTF-standardized attributes names
4
+ // Attributes that cannot be identified will not be included
5
+ // Removes `indices` if present, as it should be stored separately from the attributes
2
6
  export function getGLTFAccessors(attributes) {
3
- const accessors = {};
4
- for (const name in attributes) {
5
- const attribute = attributes[name];
6
- if (name !== 'indices') {
7
- const glTFAccessor = getGLTFAccessor(attribute);
8
- accessors[name] = glTFAccessor;
7
+ const accessors = {};
8
+ for (const name in attributes) {
9
+ const attribute = attributes[name];
10
+ if (name !== 'indices') {
11
+ const glTFAccessor = getGLTFAccessor(attribute);
12
+ accessors[name] = glTFAccessor;
13
+ }
9
14
  }
10
- }
11
- return accessors;
15
+ return accessors;
12
16
  }
17
+ // Fix up a single accessor.
18
+ // Input: typed array or a partial accessor object
19
+ // Return: accessor object
13
20
  export function getGLTFAccessor(attribute) {
14
- const {
15
- buffer,
16
- size,
17
- count
18
- } = getAccessorData(attribute);
19
- const glTFAccessor = {
20
- value: buffer,
21
- size,
22
- byteOffset: 0,
23
- count,
24
- type: getAccessorTypeFromSize(size),
25
- componentType: getComponentTypeFromArray(buffer)
26
- };
27
- return glTFAccessor;
21
+ const { buffer, size, count } = getAccessorData(attribute);
22
+ const glTFAccessor = {
23
+ // glTF Accessor values
24
+ // TODO: Instead of a bufferView index we could have an actual buffer (typed array)
25
+ // bufferView: null,
26
+ // TODO: Deprecate `value` in favor of bufferView?
27
+ // @ts-ignore
28
+ value: buffer,
29
+ size, // Decoded `type` (e.g. SCALAR)
30
+ byteOffset: 0,
31
+ count,
32
+ type: getAccessorTypeFromSize(size),
33
+ componentType: getComponentTypeFromArray(buffer)
34
+ };
35
+ return glTFAccessor;
28
36
  }
37
+ // export function getGLTFAttribute(data, gltfAttributeName): GLTFAccessor {
38
+ // return data.attributes[data.glTFAttributeMap[gltfAttributeName]];
39
+ // }
29
40
  function getAccessorData(attribute) {
30
- let buffer = attribute;
31
- let size = 1;
32
- let count = 0;
33
- if (attribute && attribute.value) {
34
- buffer = attribute.value;
35
- size = attribute.size || 1;
36
- }
37
- if (buffer) {
38
- if (!ArrayBuffer.isView(buffer)) {
39
- buffer = toTypedArray(buffer, Float32Array);
41
+ let buffer = attribute;
42
+ let size = 1;
43
+ let count = 0;
44
+ if (attribute && attribute.value) {
45
+ buffer = attribute.value;
46
+ size = attribute.size || 1;
40
47
  }
41
- count = buffer.length / size;
42
- }
43
- return {
44
- buffer,
45
- size,
46
- count
47
- };
48
+ if (buffer) {
49
+ if (!ArrayBuffer.isView(buffer)) {
50
+ buffer = toTypedArray(buffer, Float32Array);
51
+ }
52
+ count = buffer.length / size;
53
+ }
54
+ return { buffer, size, count };
48
55
  }
49
- function toTypedArray(array, ArrayType) {
50
- let convertTypedArrays = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
51
- if (!array) {
52
- return null;
53
- }
54
- if (Array.isArray(array)) {
55
- return new ArrayType(array);
56
- }
57
- if (convertTypedArrays && !(array instanceof ArrayType)) {
58
- return new ArrayType(array);
59
- }
60
- return array;
56
+ // Convert non-typed arrays to arrays of specified format
57
+ function toTypedArray(array, ArrayType, convertTypedArrays = false) {
58
+ if (!array) {
59
+ return null;
60
+ }
61
+ if (Array.isArray(array)) {
62
+ return new ArrayType(array);
63
+ }
64
+ if (convertTypedArrays && !(array instanceof ArrayType)) {
65
+ return new ArrayType(array);
66
+ }
67
+ return array;
61
68
  }
62
- //# sourceMappingURL=gltf-attribute-utils.js.map
@@ -1,37 +1,37 @@
1
1
  export const COMPONENTS = {
2
- SCALAR: 1,
3
- VEC2: 2,
4
- VEC3: 3,
5
- VEC4: 4,
6
- MAT2: 4,
7
- MAT3: 9,
8
- MAT4: 16
2
+ SCALAR: 1,
3
+ VEC2: 2,
4
+ VEC3: 3,
5
+ VEC4: 4,
6
+ MAT2: 4,
7
+ MAT3: 9,
8
+ MAT4: 16
9
9
  };
10
10
  export const BYTES = {
11
- 5120: 1,
12
- 5121: 1,
13
- 5122: 2,
14
- 5123: 2,
15
- 5125: 4,
16
- 5126: 4
11
+ 5120: 1, // BYTE
12
+ 5121: 1, // UNSIGNED_BYTE
13
+ 5122: 2, // SHORT
14
+ 5123: 2, // UNSIGNED_SHORT
15
+ 5125: 4, // UNSIGNED_INT
16
+ 5126: 4 // FLOAT
17
17
  };
18
+ // ENUM LOOKUP
18
19
  export function getBytesFromComponentType(componentType) {
19
- return BYTES[componentType];
20
+ return BYTES[componentType];
20
21
  }
21
22
  export function getSizeFromAccessorType(type) {
22
- return COMPONENTS[type];
23
+ return COMPONENTS[type];
23
24
  }
24
25
  export function getGLEnumFromSamplerParameter(parameter) {
25
- const GL_TEXTURE_MAG_FILTER = 0x2800;
26
- const GL_TEXTURE_MIN_FILTER = 0x2801;
27
- const GL_TEXTURE_WRAP_S = 0x2802;
28
- const GL_TEXTURE_WRAP_T = 0x2803;
29
- const PARAMETER_MAP = {
30
- magFilter: GL_TEXTURE_MAG_FILTER,
31
- minFilter: GL_TEXTURE_MIN_FILTER,
32
- wrapS: GL_TEXTURE_WRAP_S,
33
- wrapT: GL_TEXTURE_WRAP_T
34
- };
35
- return PARAMETER_MAP[parameter];
26
+ const GL_TEXTURE_MAG_FILTER = 0x2800;
27
+ const GL_TEXTURE_MIN_FILTER = 0x2801;
28
+ const GL_TEXTURE_WRAP_S = 0x2802;
29
+ const GL_TEXTURE_WRAP_T = 0x2803;
30
+ const PARAMETER_MAP = {
31
+ magFilter: GL_TEXTURE_MAG_FILTER,
32
+ minFilter: GL_TEXTURE_MIN_FILTER,
33
+ wrapS: GL_TEXTURE_WRAP_S,
34
+ wrapT: GL_TEXTURE_WRAP_T
35
+ };
36
+ return PARAMETER_MAP[parameter];
36
37
  }
37
- //# sourceMappingURL=gltf-constants.js.map
@@ -1,4 +1,4 @@
1
- import type { GLTFPostprocessed } from '../types/gltf-postprocessed-schema';
1
+ import type { GLTFPostprocessed } from "../types/gltf-postprocessed-schema.js";
2
2
  export declare function getAccessorTypeFromSize(size: any): string;
3
3
  export declare function getComponentTypeFromArray(typedArray: any): number;
4
4
  export declare function getAccessorArrayTypeAndLength(accessor: any, bufferView: any): {
@@ -1 +1 @@
1
- {"version":3,"file":"gltf-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/gltf-utils/gltf-utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,oCAAoC,CAAC;AA8D1E,wBAAgB,uBAAuB,CAAC,IAAI,KAAA,UAG3C;AAED,wBAAgB,yBAAyB,CAAC,UAAU,KAAA,UAMnD;AAED,wBAAgB,6BAA6B,CAAC,QAAQ,KAAA,EAAE,UAAU,KAAA;;;;;;EAUjE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAiBlE"}
1
+ {"version":3,"file":"gltf-utils.d.ts","sourceRoot":"","sources":["../../../src/lib/gltf-utils/gltf-utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,iBAAiB,EAAC,8CAA2C;AA8D1E,wBAAgB,uBAAuB,CAAC,IAAI,KAAA,UAG3C;AAED,wBAAgB,yBAAyB,CAAC,UAAU,KAAA,UAMnD;AAED,wBAAgB,6BAA6B,CAAC,QAAQ,KAAA,EAAE,UAAU,KAAA;;;;;;EAUjE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,iBAAiB,GAAG,MAAM,CAiBlE"}
@@ -1,79 +1,86 @@
1
1
  import { assert } from "../utils/assert.js";
2
2
  import { BYTES, COMPONENTS } from "../gltf-utils/gltf-constants.js";
3
+ /**
4
+ * Memory needed to store texture and all mipmap levels 1 + 1/4 + 1/16 + 1/64 + ...
5
+ * Minimum 1.33, but due to GPU layout may be 1.5
6
+ */
3
7
  const MIPMAP_FACTOR = 1.33;
4
8
  const TYPES = ['SCALAR', 'VEC2', 'VEC3', 'VEC4'];
5
- const ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT = [[Int8Array, 5120], [Uint8Array, 5121], [Int16Array, 5122], [Uint16Array, 5123], [Uint32Array, 5125], [Float32Array, 5126], [Float64Array, 5130]];
9
+ const ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT = [
10
+ [Int8Array, 5120],
11
+ [Uint8Array, 5121],
12
+ [Int16Array, 5122],
13
+ [Uint16Array, 5123],
14
+ [Uint32Array, 5125],
15
+ [Float32Array, 5126],
16
+ [Float64Array, 5130]
17
+ ];
6
18
  const ARRAY_TO_COMPONENT_TYPE = new Map(ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT);
7
19
  const ATTRIBUTE_TYPE_TO_COMPONENTS = {
8
- SCALAR: 1,
9
- VEC2: 2,
10
- VEC3: 3,
11
- VEC4: 4,
12
- MAT2: 4,
13
- MAT3: 9,
14
- MAT4: 16
20
+ SCALAR: 1,
21
+ VEC2: 2,
22
+ VEC3: 3,
23
+ VEC4: 4,
24
+ MAT2: 4,
25
+ MAT3: 9,
26
+ MAT4: 16
15
27
  };
16
28
  const ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE = {
17
- 5120: 1,
18
- 5121: 1,
19
- 5122: 2,
20
- 5123: 2,
21
- 5125: 4,
22
- 5126: 4
29
+ 5120: 1,
30
+ 5121: 1,
31
+ 5122: 2,
32
+ 5123: 2,
33
+ 5125: 4,
34
+ 5126: 4
23
35
  };
24
36
  const ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY = {
25
- 5120: Int8Array,
26
- 5121: Uint8Array,
27
- 5122: Int16Array,
28
- 5123: Uint16Array,
29
- 5125: Uint32Array,
30
- 5126: Float32Array
37
+ 5120: Int8Array,
38
+ 5121: Uint8Array,
39
+ 5122: Int16Array,
40
+ 5123: Uint16Array,
41
+ 5125: Uint32Array,
42
+ 5126: Float32Array
31
43
  };
32
44
  export function getAccessorTypeFromSize(size) {
33
- const type = TYPES[size - 1];
34
- return type || TYPES[0];
45
+ const type = TYPES[size - 1];
46
+ return type || TYPES[0];
35
47
  }
36
48
  export function getComponentTypeFromArray(typedArray) {
37
- const componentType = ARRAY_TO_COMPONENT_TYPE.get(typedArray.constructor);
38
- if (!componentType) {
39
- throw new Error('Illegal typed array');
40
- }
41
- return componentType;
49
+ const componentType = ARRAY_TO_COMPONENT_TYPE.get(typedArray.constructor);
50
+ if (!componentType) {
51
+ throw new Error('Illegal typed array');
52
+ }
53
+ return componentType;
42
54
  }
43
55
  export function getAccessorArrayTypeAndLength(accessor, bufferView) {
44
- const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[accessor.componentType];
45
- const components = ATTRIBUTE_TYPE_TO_COMPONENTS[accessor.type];
46
- const bytesPerComponent = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[accessor.componentType];
47
- const length = accessor.count * components;
48
- const byteLength = accessor.count * components * bytesPerComponent;
49
- assert(byteLength >= 0 && byteLength <= bufferView.byteLength);
50
- const componentByteSize = BYTES[accessor.componentType];
51
- const numberOfComponentsInElement = COMPONENTS[accessor.type];
52
- return {
53
- ArrayType,
54
- length,
55
- byteLength,
56
- componentByteSize,
57
- numberOfComponentsInElement
58
- };
56
+ const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[accessor.componentType];
57
+ const components = ATTRIBUTE_TYPE_TO_COMPONENTS[accessor.type];
58
+ const bytesPerComponent = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[accessor.componentType];
59
+ const length = accessor.count * components;
60
+ const byteLength = accessor.count * components * bytesPerComponent;
61
+ assert(byteLength >= 0 && byteLength <= bufferView.byteLength);
62
+ const componentByteSize = BYTES[accessor.componentType];
63
+ const numberOfComponentsInElement = COMPONENTS[accessor.type];
64
+ return { ArrayType, length, byteLength, componentByteSize, numberOfComponentsInElement };
59
65
  }
66
+ /**
67
+ * Calculate the GPU memory used by a GLTF tile, for both buffer and texture memory
68
+ * @param gltf - the gltf content of a GLTF tile
69
+ * @returns - total memory usage in bytes
70
+ */
60
71
  export function getMemoryUsageGLTF(gltf) {
61
- let {
62
- images,
63
- bufferViews
64
- } = gltf;
65
- images = images || [];
66
- bufferViews = bufferViews || [];
67
- const imageBufferViews = images.map(i => i.bufferView);
68
- bufferViews = bufferViews.filter(view => !imageBufferViews.includes(view));
69
- const bufferMemory = bufferViews.reduce((acc, view) => acc + view.byteLength, 0);
70
- const pixelCount = images.reduce((acc, image) => {
71
- const {
72
- width,
73
- height
74
- } = image.image;
75
- return acc + width * height;
76
- }, 0);
77
- return bufferMemory + Math.ceil(4 * pixelCount * MIPMAP_FACTOR);
72
+ let { images, bufferViews } = gltf;
73
+ images = images || [];
74
+ bufferViews = bufferViews || [];
75
+ const imageBufferViews = images.map((i) => i.bufferView);
76
+ bufferViews = bufferViews.filter((view) => !imageBufferViews.includes(view));
77
+ const bufferMemory = bufferViews.reduce((acc, view) => acc + view.byteLength, 0);
78
+ // Assume each pixel of the texture is 4 channel with mimmaps (which add 33%)
79
+ // TODO correctly handle compressed textures
80
+ const pixelCount = images.reduce((acc, image) => {
81
+ // @ts-ignore
82
+ const { width, height } = image.image;
83
+ return acc + width * height;
84
+ }, 0);
85
+ return bufferMemory + Math.ceil(4 * pixelCount * MIPMAP_FACTOR);
78
86
  }
79
- //# sourceMappingURL=gltf-utils.js.map
@@ -1,12 +1,14 @@
1
+ // Resolves a relative url against a baseUrl
2
+ // If url is absolute, return it unchanged
1
3
  export function resolveUrl(url, options) {
2
- const absolute = url.startsWith('data:') || url.startsWith('http:') || url.startsWith('https:');
3
- if (absolute) {
4
- return url;
5
- }
6
- const baseUrl = options.baseUri || options.uri;
7
- if (!baseUrl) {
8
- throw new Error(`'baseUri' must be provided to resolve relative url ${url}`);
9
- }
10
- return baseUrl.substr(0, baseUrl.lastIndexOf('/') + 1) + url;
4
+ // TODO: Use better logic to handle all protocols plus not delay on data
5
+ const absolute = url.startsWith('data:') || url.startsWith('http:') || url.startsWith('https:');
6
+ if (absolute) {
7
+ return url;
8
+ }
9
+ const baseUrl = options.baseUri || options.uri;
10
+ if (!baseUrl) {
11
+ throw new Error(`'baseUri' must be provided to resolve relative url ${url}`);
12
+ }
13
+ return baseUrl.substr(0, baseUrl.lastIndexOf('/') + 1) + url;
11
14
  }
12
- //# sourceMappingURL=resolve-url.js.map
@@ -1,4 +1,4 @@
1
- import type { GLB } from '../types/glb-types';
1
+ import type { GLB } from "../types/glb-types.js";
2
2
  /** Options for parsing a GLB */
3
3
  export type ParseGLBOptions = {
4
4
  /** @deprecated This option was used by XVIZ protocol to define a non-standard magic number */
@@ -1 +1 @@
1
- {"version":3,"file":"parse-glb.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-glb.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oBAAoB,CAAC;AAG5C,gCAAgC;AAChC,MAAM,MAAM,eAAe,GAAG;IAC5B,8FAA8F;IAC9F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qFAAqF;IACrF,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AA0BF,yEAAyE;AACzE,wBAAgB,KAAK,CACnB,WAAW,EAAE,WAAW,EACxB,UAAU,GAAE,MAAU,EACtB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAMT;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,WAAW,EAAE,WAAW,EACxB,UAAU,GAAE,MAAU,EACtB,OAAO,GAAE,eAAoB,UAmC9B"}
1
+ {"version":3,"file":"parse-glb.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-glb.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,GAAG,EAAC,8BAA2B;AAG5C,gCAAgC;AAChC,MAAM,MAAM,eAAe,GAAG;IAC5B,8FAA8F;IAC9F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,qFAAqF;IACrF,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC;AA0BF,yEAAyE;AACzE,wBAAgB,KAAK,CACnB,WAAW,EAAE,WAAW,EACxB,UAAU,GAAE,MAAU,EACtB,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAMT;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,WAAW,EAAE,WAAW,EACxB,UAAU,GAAE,MAAU,EACtB,OAAO,GAAE,eAAoB,UAmC9B"}
@@ -1,118 +1,161 @@
1
1
  import { padToNBytes, assert } from '@loaders.gl/loader-utils';
2
+ /** Binary GLTF is little endian. */
2
3
  const LITTLE_ENDIAN = true;
4
+ /** 'glTF' in Big-Endian ASCII */
3
5
  const MAGIC_glTF = 0x676c5446;
4
6
  const GLB_FILE_HEADER_SIZE = 12;
5
7
  const GLB_CHUNK_HEADER_SIZE = 8;
6
8
  const GLB_CHUNK_TYPE_JSON = 0x4e4f534a;
7
9
  const GLB_CHUNK_TYPE_BIN = 0x004e4942;
8
10
  const GLB_V1_CONTENT_FORMAT_JSON = 0x0;
11
+ /** @deprecated - Backward compatibility for old xviz files */
9
12
  const GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED = 0;
13
+ /** @deprecated - Backward compatibility for old xviz files */
10
14
  const GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED = 1;
11
- function getMagicString(dataView) {
12
- let byteOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
13
- return `\
15
+ function getMagicString(dataView, byteOffset = 0) {
16
+ return `\
14
17
  ${String.fromCharCode(dataView.getUint8(byteOffset + 0))}\
15
18
  ${String.fromCharCode(dataView.getUint8(byteOffset + 1))}\
16
19
  ${String.fromCharCode(dataView.getUint8(byteOffset + 2))}\
17
20
  ${String.fromCharCode(dataView.getUint8(byteOffset + 3))}`;
18
21
  }
19
- export function isGLB(arrayBuffer) {
20
- let byteOffset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
21
- let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
22
- const dataView = new DataView(arrayBuffer);
23
- const {
24
- magic = MAGIC_glTF
25
- } = options;
26
- const magic1 = dataView.getUint32(byteOffset, false);
27
- return magic1 === magic || magic1 === MAGIC_glTF;
22
+ /** Check if the contents of an array buffer contains GLB byte markers */
23
+ export function isGLB(arrayBuffer, byteOffset = 0, options = {}) {
24
+ const dataView = new DataView(arrayBuffer);
25
+ // Check that GLB Header starts with the magic number
26
+ const { magic = MAGIC_glTF } = options;
27
+ const magic1 = dataView.getUint32(byteOffset, false);
28
+ return magic1 === magic || magic1 === MAGIC_glTF;
28
29
  }
29
- export function parseGLBSync(glb, arrayBuffer) {
30
- let byteOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
31
- let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
32
- const dataView = new DataView(arrayBuffer);
33
- const type = getMagicString(dataView, byteOffset + 0);
34
- const version = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN);
35
- const byteLength = dataView.getUint32(byteOffset + 8, LITTLE_ENDIAN);
36
- Object.assign(glb, {
37
- header: {
38
- byteOffset,
39
- byteLength,
40
- hasBinChunk: false
41
- },
42
- type,
43
- version,
44
- json: {},
45
- binChunks: []
46
- });
47
- byteOffset += GLB_FILE_HEADER_SIZE;
48
- switch (glb.version) {
49
- case 1:
50
- return parseGLBV1(glb, dataView, byteOffset);
51
- case 2:
52
- return parseGLBV2(glb, dataView, byteOffset, options = {});
53
- default:
54
- throw new Error(`Invalid GLB version ${glb.version}. Only supports version 1 and 2.`);
55
- }
30
+ /**
31
+ * Synchronously parse a GLB
32
+ * @param glb - Target, Output is stored there
33
+ * @param arrayBuffer - Input data
34
+ * @param byteOffset - Offset into arrayBuffer to start parsing from (for "embedded" GLBs, e.g. in 3D tiles)
35
+ * @param options
36
+ * @returns
37
+ */
38
+ export function parseGLBSync(glb, arrayBuffer, byteOffset = 0, options = {}) {
39
+ // Check that GLB Header starts with the magic number
40
+ const dataView = new DataView(arrayBuffer);
41
+ // Compare format with GLBLoader documentation
42
+ const type = getMagicString(dataView, byteOffset + 0);
43
+ const version = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Version 2 of binary glTF container format
44
+ const byteLength = dataView.getUint32(byteOffset + 8, LITTLE_ENDIAN); // Total byte length of binary file
45
+ Object.assign(glb, {
46
+ // Put less important stuff in a header, to avoid clutter
47
+ header: {
48
+ byteOffset, // Byte offset into the initial arrayBuffer
49
+ byteLength,
50
+ hasBinChunk: false
51
+ },
52
+ type,
53
+ version,
54
+ json: {},
55
+ binChunks: []
56
+ });
57
+ byteOffset += GLB_FILE_HEADER_SIZE;
58
+ switch (glb.version) {
59
+ case 1:
60
+ return parseGLBV1(glb, dataView, byteOffset);
61
+ case 2:
62
+ return parseGLBV2(glb, dataView, byteOffset, (options = {}));
63
+ default:
64
+ throw new Error(`Invalid GLB version ${glb.version}. Only supports version 1 and 2.`);
65
+ }
56
66
  }
67
+ /**
68
+ * Parse a V1 GLB
69
+ * @param glb - target, output is stored in this object
70
+ * @param dataView - Input, memory to be parsed
71
+ * @param byteOffset - Offset of first byte of GLB data in the data view
72
+ * @returns Number of bytes parsed (there could be additional non-GLB data after the GLB)
73
+ */
57
74
  function parseGLBV1(glb, dataView, byteOffset) {
58
- assert(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
59
- const contentLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN);
60
- const contentFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN);
61
- byteOffset += GLB_CHUNK_HEADER_SIZE;
62
- assert(contentFormat === GLB_V1_CONTENT_FORMAT_JSON);
63
- parseJSONChunk(glb, dataView, byteOffset, contentLength);
64
- byteOffset += contentLength;
65
- byteOffset += parseBINChunk(glb, dataView, byteOffset, glb.header.byteLength);
66
- return byteOffset;
75
+ // Sanity: ensure file is big enough to hold at least the headers
76
+ assert(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
77
+ // Explanation of GLB structure:
78
+ // https://cloud.githubusercontent.com/assets/3479527/22600725/36b87122-ea55-11e6-9d40-6fd42819fcab.png
79
+ const contentLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN); // Byte length of chunk
80
+ const contentFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Chunk format as uint32
81
+ byteOffset += GLB_CHUNK_HEADER_SIZE;
82
+ // GLB v1 only supports a single chunk type
83
+ assert(contentFormat === GLB_V1_CONTENT_FORMAT_JSON);
84
+ parseJSONChunk(glb, dataView, byteOffset, contentLength);
85
+ // No need to call the function padToBytes() from parseJSONChunk()
86
+ byteOffset += contentLength;
87
+ byteOffset += parseBINChunk(glb, dataView, byteOffset, glb.header.byteLength);
88
+ return byteOffset;
67
89
  }
90
+ /**
91
+ * Parse a V2 GLB
92
+ * @param glb - target, output is stored in this object
93
+ * @param dataView - Input, memory to be parsed
94
+ * @param byteOffset - Offset of first byte of GLB data in the data view
95
+ * @returns Number of bytes parsed (there could be additional non-GLB data after the GLB)
96
+ */
68
97
  function parseGLBV2(glb, dataView, byteOffset, options) {
69
- assert(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
70
- parseGLBChunksSync(glb, dataView, byteOffset, options);
71
- return byteOffset + glb.header.byteLength;
98
+ // Sanity: ensure file is big enough to hold at least the first chunk header
99
+ assert(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
100
+ parseGLBChunksSync(glb, dataView, byteOffset, options);
101
+ return byteOffset + glb.header.byteLength;
72
102
  }
103
+ /** Iterate over GLB chunks and parse them */
73
104
  function parseGLBChunksSync(glb, dataView, byteOffset, options) {
74
- while (byteOffset + 8 <= glb.header.byteLength) {
75
- const chunkLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN);
76
- const chunkFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN);
77
- byteOffset += GLB_CHUNK_HEADER_SIZE;
78
- switch (chunkFormat) {
79
- case GLB_CHUNK_TYPE_JSON:
80
- parseJSONChunk(glb, dataView, byteOffset, chunkLength);
81
- break;
82
- case GLB_CHUNK_TYPE_BIN:
83
- parseBINChunk(glb, dataView, byteOffset, chunkLength);
84
- break;
85
- case GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED:
86
- if (!options.strict) {
87
- parseJSONChunk(glb, dataView, byteOffset, chunkLength);
88
- }
89
- break;
90
- case GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED:
91
- if (!options.strict) {
92
- parseBINChunk(glb, dataView, byteOffset, chunkLength);
105
+ // Per spec we must iterate over chunks, ignoring all except JSON and BIN
106
+ // Iterate as long as there is space left for another chunk header
107
+ while (byteOffset + 8 <= glb.header.byteLength) {
108
+ const chunkLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN); // Byte length of chunk
109
+ const chunkFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Chunk format as uint32
110
+ byteOffset += GLB_CHUNK_HEADER_SIZE;
111
+ // Per spec we must iterate over chunks, ignoring all except JSON and BIN
112
+ switch (chunkFormat) {
113
+ case GLB_CHUNK_TYPE_JSON:
114
+ parseJSONChunk(glb, dataView, byteOffset, chunkLength);
115
+ break;
116
+ case GLB_CHUNK_TYPE_BIN:
117
+ parseBINChunk(glb, dataView, byteOffset, chunkLength);
118
+ break;
119
+ // Backward compatibility for very old xviz files
120
+ case GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED:
121
+ if (!options.strict) {
122
+ parseJSONChunk(glb, dataView, byteOffset, chunkLength);
123
+ }
124
+ break;
125
+ case GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED:
126
+ if (!options.strict) {
127
+ parseBINChunk(glb, dataView, byteOffset, chunkLength);
128
+ }
129
+ break;
130
+ default:
131
+ // Ignore, per spec
132
+ // console.warn(`Unknown GLB chunk type`); // eslint-disable-line
133
+ break;
93
134
  }
94
- break;
95
- default:
96
- break;
135
+ byteOffset += padToNBytes(chunkLength, 4);
97
136
  }
98
- byteOffset += padToNBytes(chunkLength, 4);
99
- }
100
- return byteOffset;
137
+ return byteOffset;
101
138
  }
139
+ /* Parse a GLB JSON chunk */
102
140
  function parseJSONChunk(glb, dataView, byteOffset, chunkLength) {
103
- const jsonChunk = new Uint8Array(dataView.buffer, byteOffset, chunkLength);
104
- const textDecoder = new TextDecoder('utf8');
105
- const jsonText = textDecoder.decode(jsonChunk);
106
- glb.json = JSON.parse(jsonText);
107
- return padToNBytes(chunkLength, 4);
141
+ // 1. Create a "view" of the binary encoded JSON data inside the GLB
142
+ const jsonChunk = new Uint8Array(dataView.buffer, byteOffset, chunkLength);
143
+ // 2. Decode the JSON binary array into clear text
144
+ const textDecoder = new TextDecoder('utf8');
145
+ const jsonText = textDecoder.decode(jsonChunk);
146
+ // 3. Parse the JSON text into a JavaScript data structure
147
+ glb.json = JSON.parse(jsonText);
148
+ return padToNBytes(chunkLength, 4);
108
149
  }
150
+ /** Parse a GLB BIN chunk */
109
151
  function parseBINChunk(glb, dataView, byteOffset, chunkLength) {
110
- glb.header.hasBinChunk = true;
111
- glb.binChunks.push({
112
- byteOffset,
113
- byteLength: chunkLength,
114
- arrayBuffer: dataView.buffer
115
- });
116
- return padToNBytes(chunkLength, 4);
152
+ // Note: BIN chunk can be optional
153
+ glb.header.hasBinChunk = true;
154
+ glb.binChunks.push({
155
+ byteOffset,
156
+ byteLength: chunkLength,
157
+ arrayBuffer: dataView.buffer
158
+ // TODO - copy, or create typed array view?
159
+ });
160
+ return padToNBytes(chunkLength, 4);
117
161
  }
118
- //# sourceMappingURL=parse-glb.js.map