@loaders.gl/gltf 3.1.0-alpha.2 → 4.0.0-alpha.3

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 (70) hide show
  1. package/dist/bundle.js +2 -4
  2. package/dist/bundle.js.map +1 -1
  3. package/dist/gltf-loader.js.map +1 -1
  4. package/dist/index.js +0 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/lib/api/gltf-extensions.js +35 -0
  7. package/dist/lib/api/gltf-extensions.js.map +1 -0
  8. package/dist/lib/api/gltf-scenegraph.js +1 -1
  9. package/dist/lib/api/gltf-scenegraph.js.map +1 -1
  10. package/dist/lib/api/normalize-gltf-v1.js +4 -4
  11. package/dist/lib/api/normalize-gltf-v1.js.map +1 -1
  12. package/dist/lib/api/post-process-gltf.js +12 -12
  13. package/dist/lib/api/post-process-gltf.js.map +1 -1
  14. package/dist/lib/extensions/EXT_meshopt_compression.js +16 -8
  15. package/dist/lib/extensions/EXT_meshopt_compression.js.map +1 -1
  16. package/dist/lib/extensions/EXT_texture_webp.js +32 -0
  17. package/dist/lib/extensions/EXT_texture_webp.js.map +1 -0
  18. package/dist/lib/extensions/KHR_binary_gltf.js +8 -8
  19. package/dist/lib/extensions/KHR_binary_gltf.js.map +1 -1
  20. package/dist/lib/extensions/KHR_draco_mesh_compression.js +9 -1
  21. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +1 -1
  22. package/dist/lib/extensions/KHR_texture_basisu.js +22 -0
  23. package/dist/lib/extensions/KHR_texture_basisu.js.map +1 -0
  24. package/dist/lib/extensions/{KHR_lights_punctual.js → deprecated/KHR_lights_punctual.js} +4 -3
  25. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +1 -0
  26. package/dist/lib/extensions/{KHR_materials_unlit.js → deprecated/KHR_materials_unlit.js} +3 -2
  27. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +1 -0
  28. package/dist/lib/extensions/{KHR_techniques_webgl.js → deprecated/KHR_techniques_webgl.js} +3 -2
  29. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +1 -0
  30. package/dist/lib/gltf-utils/gltf-constants.js +0 -6
  31. package/dist/lib/gltf-utils/gltf-constants.js.map +1 -1
  32. package/dist/lib/gltf-utils/resolve-url.js +1 -1
  33. package/dist/lib/gltf-utils/resolve-url.js.map +1 -1
  34. package/dist/lib/parsers/parse-glb.js +2 -6
  35. package/dist/lib/parsers/parse-glb.js.map +1 -1
  36. package/dist/lib/parsers/parse-gltf.js +42 -7
  37. package/dist/lib/parsers/parse-gltf.js.map +1 -1
  38. package/dist/lib/utils/version.js +1 -1
  39. package/dist/meshopt/meshopt-decoder.js +1 -1
  40. package/dist/meshopt/meshopt-decoder.js.map +1 -1
  41. package/dist/webp/webp.js +29 -0
  42. package/dist/webp/webp.js.map +1 -0
  43. package/package.json +10 -7
  44. package/src/bundle.ts +2 -3
  45. package/src/gltf-loader.ts +7 -4
  46. package/src/index.ts +8 -4
  47. package/src/lib/api/gltf-extensions.ts +71 -0
  48. package/src/lib/api/gltf-scenegraph.ts +4 -6
  49. package/src/lib/api/normalize-gltf-v1.js +2 -1
  50. package/src/lib/extensions/EXT_meshopt_compression.ts +32 -23
  51. package/src/lib/extensions/EXT_texture_webp.ts +45 -0
  52. package/src/lib/extensions/KHR_binary_gltf.ts +19 -10
  53. package/src/lib/extensions/KHR_draco_mesh_compression.ts +29 -4
  54. package/src/lib/extensions/KHR_texture_basisu.ts +36 -0
  55. package/src/lib/extensions/{KHR_lights_punctual.ts → deprecated/KHR_lights_punctual.ts} +7 -4
  56. package/src/lib/extensions/{KHR_materials_unlit.ts → deprecated/KHR_materials_unlit.ts} +6 -3
  57. package/src/lib/extensions/{KHR_techniques_webgl.ts → deprecated/KHR_techniques_webgl.ts} +6 -3
  58. package/src/lib/gltf-utils/gltf-constants.ts +0 -12
  59. package/src/lib/parsers/parse-gltf.ts +72 -20
  60. package/src/lib/types/gltf-json-schema.ts +96 -3
  61. package/src/lib/types/gltf-types.ts +19 -21
  62. package/src/webp/webp.ts +41 -0
  63. package/dist/dist.min.js +0 -2
  64. package/dist/dist.min.js.map +0 -1
  65. package/dist/lib/extensions/KHR_lights_punctual.js.map +0 -1
  66. package/dist/lib/extensions/KHR_materials_unlit.js.map +0 -1
  67. package/dist/lib/extensions/KHR_techniques_webgl.js.map +0 -1
  68. package/dist/lib/extensions/gltf-extensions.js +0 -24
  69. package/dist/lib/extensions/gltf-extensions.js.map +0 -1
  70. package/src/lib/extensions/gltf-extensions.ts +0 -48
@@ -36,6 +36,7 @@ const DEFAULT_GLTF_JSON: GLTF = {
36
36
  buffers: []
37
37
  };
38
38
 
39
+ type Extension = {[key: string]: any};
39
40
  /**
40
41
  * Class for structured access to GLTF data
41
42
  */
@@ -79,13 +80,13 @@ export default class GLTFScenegraph {
79
80
  return extras[key];
80
81
  }
81
82
 
82
- getExtension(extensionName: string): {[key: string]: any} | null {
83
+ getExtension<T = Extension>(extensionName: string): T | null {
83
84
  const isExtension = this.getUsedExtensions().find((name) => name === extensionName);
84
85
  const extensions = this.json.extensions || {};
85
86
  return isExtension ? extensions[extensionName] || true : null;
86
87
  }
87
88
 
88
- getRequiredExtension(extensionName: string): {[key: string]: any} | null {
89
+ getRequiredExtension<T = Extension>(extensionName: string): T | null {
89
90
  const isRequired = this.getRequiredExtensions().find((name) => name === extensionName);
90
91
  return isRequired ? this.getExtension(extensionName) : null;
91
92
  }
@@ -98,10 +99,7 @@ export default class GLTFScenegraph {
98
99
  return this.json.extensionsUsed || [];
99
100
  }
100
101
 
101
- getObjectExtension(
102
- object: {[key: string]: any},
103
- extensionName: string
104
- ): {[key: string]: any} | null {
102
+ getObjectExtension<T = Extension>(object: {[key: string]: any}, extensionName: string): T | null {
105
103
  const extensions = object.extensions || {};
106
104
  return extensions[extensionName];
107
105
  }
@@ -139,7 +139,8 @@ class GLTFV1Normalizer {
139
139
 
140
140
  // Extract bufferView indices for images
141
141
  // (this extension needs to be invoked early in the normalization process)
142
- KHR_binary_glTF.decode(gltf);
142
+ // TODO can this be handled by standard extension processing instead of called explicitly?
143
+ KHR_binary_glTF.preprocess(gltf);
143
144
 
144
145
  // Convert object references from ids to indices
145
146
  this._convertObjectIdsToArrayIndices(json);
@@ -1,19 +1,9 @@
1
1
  /* eslint-disable camelcase */
2
+ import type {GLTF, GLTFBufferView, GLTF_EXT_meshopt_compression} from '../types/gltf-types';
2
3
  import type {GLTFLoaderOptions} from '../../gltf-loader';
3
4
  import GLTFScenegraph from '../api/gltf-scenegraph';
4
- import {EXT_MESHOPT_COMPRESSION} from '../gltf-utils/gltf-constants';
5
5
  import {isMeshoptSupported, meshoptDecodeGltfBuffer} from '../../meshopt/meshopt-decoder';
6
6
 
7
- type GLTF_EXT_meshopt_compression = {
8
- buffer: number;
9
- byteOffset?: number;
10
- byteLength: number;
11
- byteStride: number;
12
- count: number;
13
- mode: 'ATTRIBUTES' | 'TRIANGLES' | 'INDICES';
14
- filter?: 'NONE' | 'OCTAHEDRAL' | 'QUATERNION' | 'EXPONENTIAL';
15
- };
16
-
17
7
  // @ts-ignore
18
8
  // eslint-disable-next-line
19
9
  const DEFAULT_MESHOPT_OPTIONS = {
@@ -21,34 +11,51 @@ const DEFAULT_MESHOPT_OPTIONS = {
21
11
  filter: 'NONE'
22
12
  };
23
13
 
24
- // Note: We have a "soft dependency" on DracoWriter to avoid bundling it when not needed
25
- export async function decode(gltfData, options: GLTFLoaderOptions) {
26
- if (!options?.gltf?.decompressMeshes || !isMeshoptSupported()) {
14
+ /** Extension name */
15
+ const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
16
+
17
+ export const name = EXT_MESHOPT_COMPRESSION;
18
+
19
+ export function preprocess(gltfData: {json: GLTF}) {
20
+ const scenegraph = new GLTFScenegraph(gltfData);
21
+ if (
22
+ scenegraph.getRequiredExtensions().includes(EXT_MESHOPT_COMPRESSION) &&
23
+ !isMeshoptSupported()
24
+ ) {
25
+ throw new Error(`gltf: Required extension ${EXT_MESHOPT_COMPRESSION} not supported by browser`);
26
+ }
27
+ }
28
+
29
+ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions) {
30
+ const scenegraph = new GLTFScenegraph(gltfData);
31
+
32
+ if (!options?.gltf?.decompressMeshes) {
27
33
  return;
28
34
  }
29
35
 
30
36
  const promises: Promise<any>[] = [];
31
37
  for (const bufferViewIndex of gltfData.json.bufferViews || []) {
32
- promises.push(decodeMeshoptBufferView(gltfData.json, bufferViewIndex));
38
+ promises.push(decodeMeshoptBufferView(scenegraph, bufferViewIndex));
33
39
  }
34
40
 
35
41
  // Decompress meshes in parallel
36
42
  await Promise.all(promises);
37
43
 
38
44
  // We have now decompressed all primitives, so remove the top-level extensions
39
- const scenegraph = new GLTFScenegraph(gltfData);
40
45
  scenegraph.removeExtension(EXT_MESHOPT_COMPRESSION);
41
46
  }
42
47
 
43
48
  /** Decode one meshopt buffer view */
44
- async function decodeMeshoptBufferView(json, index: number): Promise<ArrayBuffer | null> {
45
- const bufferView = json.bufferViews[index];
46
-
47
- const meshoptExtension =
48
- bufferView.extensions &&
49
- (bufferView.extensions[EXT_MESHOPT_COMPRESSION] as GLTF_EXT_meshopt_compression);
49
+ async function decodeMeshoptBufferView(
50
+ scenegraph: GLTFScenegraph,
51
+ bufferView: GLTFBufferView
52
+ ): Promise<ArrayBuffer | null> {
53
+ const meshoptExtension = scenegraph.getObjectExtension<GLTF_EXT_meshopt_compression>(
54
+ bufferView,
55
+ EXT_MESHOPT_COMPRESSION
56
+ );
50
57
  if (meshoptExtension) {
51
- const buffer = json.buffers[meshoptExtension.buffer];
58
+ const buffer = bufferView.buffer;
52
59
 
53
60
  const {
54
61
  byteOffset = 0,
@@ -58,6 +65,8 @@ async function decodeMeshoptBufferView(json, index: number): Promise<ArrayBuffer
58
65
  mode,
59
66
  filter = 'NONE'
60
67
  } = meshoptExtension;
68
+
69
+ // @ts-expect-error TODO - fix buffer handling
61
70
  const source = new Uint8Array(buffer, byteOffset, byteLength);
62
71
  const result = new ArrayBuffer(count * byteStride);
63
72
  await meshoptDecodeGltfBuffer(new Uint8Array(result), count, byteStride, source, mode, filter);
@@ -0,0 +1,45 @@
1
+ // GLTF EXTENSION: EXT_TEXTURE_WEBP
2
+ // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_TEXTURE_WEBP
3
+ /* eslint-disable camelcase */
4
+
5
+ import type {GLTF, GLTF_EXT_texture_webp} from '../types/gltf-types';
6
+ import type {GLTFLoaderOptions} from '../../gltf-loader';
7
+
8
+ import {_isImageFormatSupported} from '@loaders.gl/images';
9
+ import GLTFScenegraph from '../api/gltf-scenegraph';
10
+
11
+ const EXT_TEXTURE_WEBP = 'EXT_texture_webp';
12
+
13
+ /** Extension name */
14
+ export const name = EXT_TEXTURE_WEBP;
15
+
16
+ /**
17
+ * Replaces a texture source reference with the extension texture
18
+ * Done in preprocess() to prevent load of default image
19
+ */
20
+ export function preprocess(gltfData: {json: GLTF}, options: GLTFLoaderOptions): void {
21
+ const scenegraph = new GLTFScenegraph(gltfData);
22
+
23
+ if (!_isImageFormatSupported('image/webp')) {
24
+ if (scenegraph.getRequiredExtensions().includes(EXT_TEXTURE_WEBP)) {
25
+ throw new Error(`gltf: Required extension ${EXT_TEXTURE_WEBP} not supported by browser`);
26
+ }
27
+ return;
28
+ }
29
+
30
+ const {json} = scenegraph;
31
+
32
+ for (const texture of json.textures || []) {
33
+ const extension = scenegraph.getObjectExtension<GLTF_EXT_texture_webp>(
34
+ texture,
35
+ EXT_TEXTURE_WEBP
36
+ );
37
+ if (extension) {
38
+ texture.source = extension.source;
39
+ }
40
+ scenegraph.removeObjectExtension(texture, EXT_TEXTURE_WEBP);
41
+ }
42
+
43
+ // Remove the top-level extension
44
+ scenegraph.removeExtension(EXT_TEXTURE_WEBP);
45
+ }
@@ -1,12 +1,17 @@
1
1
  // GLTF 1.0 EXTENSION: KHR_binary_glTF
2
2
  // https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF
3
+ /* eslint-disable camelcase */
3
4
 
4
- import type {GLTF} from '../types/gltf-types';
5
+ import type {GLTF, GLTF_KHR_binary_glTF} from '../types/gltf-types';
5
6
 
6
7
  import GLTFScenegraph from '../api/gltf-scenegraph';
7
- import {KHR_BINARY_GLTF} from '../gltf-utils/gltf-constants';
8
8
 
9
- export function decode(gltfData: {json: GLTF}): void {
9
+ const KHR_BINARY_GLTF = 'KHR_binary_glTF';
10
+
11
+ /** Extension name */
12
+ export const name = KHR_BINARY_GLTF;
13
+
14
+ export function preprocess(gltfData: {json: GLTF}): void {
10
15
  const gltfScenegraph = new GLTFScenegraph(gltfData);
11
16
  const {json} = gltfScenegraph;
12
17
 
@@ -15,15 +20,19 @@ export function decode(gltfData: {json: GLTF}): void {
15
20
 
16
21
  // Image and shader nodes can have the extension
17
22
  // https://github.com/KhronosGroup/glTF/blob/master/extensions/1.0/Khronos/KHR_binary_glTF/schema/image.KHR_binary_glTF.schema.json
18
- for (const node of json.images || []) {
19
- const extension = gltfScenegraph.removeObjectExtension(node, KHR_BINARY_GLTF);
23
+ for (const image of json.images || []) {
24
+ const extension = gltfScenegraph.getObjectExtension<GLTF_KHR_binary_glTF>(
25
+ image,
26
+ KHR_BINARY_GLTF
27
+ );
20
28
  // The data in the extension is valid as glTF 2.0 data inside the object, so just copy it in
21
29
  if (extension) {
22
- Object.assign(node, extension);
30
+ Object.assign(image, extension);
23
31
  }
32
+ gltfScenegraph.removeObjectExtension(image, KHR_BINARY_GLTF);
24
33
  }
25
34
 
26
- // TODO shaders
35
+ // TODO shaders - At least traverse and throw error if used?
27
36
  // https://github.com/KhronosGroup/glTF/blob/master/extensions/1.0/Khronos/KHR_binary_glTF/schema/shader.KHR_binary_glTF.schema.json
28
37
 
29
38
  // glTF v1 one files have a partially formed URI field that is not expected in (and causes problems in) 2.0
@@ -36,6 +45,6 @@ export function decode(gltfData: {json: GLTF}): void {
36
45
  }
37
46
 
38
47
  // KHR_binary_gltf is a 1.0 extension that is supported natively by 2.0
39
- export function encode() {
40
- throw new Error(KHR_BINARY_GLTF);
41
- }
48
+ // export function encode() {
49
+ // throw new Error(KHR_BINARY_GLTF);
50
+ // }
@@ -1,8 +1,14 @@
1
1
  // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
2
2
  // Only TRIANGLES: 0x0004 and TRIANGLE_STRIP: 0x0005 are supported
3
+ /* eslint-disable camelcase */
3
4
 
4
5
  /* eslint-disable camelcase */
5
- import type {GLTF, GLTFAccessor, GLTFMeshPrimitive} from '../types/gltf-types';
6
+ import type {
7
+ GLTF,
8
+ GLTFAccessor,
9
+ GLTFMeshPrimitive,
10
+ GLTF_KHR_draco_mesh_compression
11
+ } from '../types/gltf-types';
6
12
  import type {GLTFLoaderOptions} from '../../gltf-loader';
7
13
 
8
14
  import type {LoaderContext} from '@loaders.gl/loader-utils';
@@ -10,10 +16,26 @@ import {DracoLoader} from '@loaders.gl/draco';
10
16
  import {DracoLoaderOptions, DracoMesh} from '@loaders.gl/draco';
11
17
  import {sliceArrayBuffer} from '@loaders.gl/loader-utils';
12
18
  import {default as Scenegraph} from '../api/gltf-scenegraph';
13
- import {KHR_DRACO_MESH_COMPRESSION} from '../gltf-utils/gltf-constants';
14
19
  import {getGLTFAccessors, getGLTFAccessor} from '../gltf-utils/gltf-attribute-utils';
15
20
 
16
- // Note: We have a "soft dependency" on DracoWriter to avoid bundling it when not needed
21
+ const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression';
22
+
23
+ /** Extension name */
24
+ export const name = KHR_DRACO_MESH_COMPRESSION;
25
+
26
+ export function preprocess(
27
+ gltfData: {json: GLTF},
28
+ options: GLTFLoaderOptions,
29
+ context: LoaderContext
30
+ ): void {
31
+ const scenegraph = new Scenegraph(gltfData);
32
+ for (const primitive of makeMeshPrimitiveIterator(scenegraph)) {
33
+ if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) {
34
+ // TODO - Remove fallback accessors to make sure we don't load unnecessary buffers
35
+ }
36
+ }
37
+ }
38
+
17
39
  export async function decode(
18
40
  gltfData: {json: GLTF},
19
41
  options: GLTFLoaderOptions,
@@ -64,7 +86,10 @@ async function decompressPrimitive(
64
86
  options: GLTFLoaderOptions,
65
87
  context: LoaderContext
66
88
  ): Promise<void> {
67
- const dracoExtension = scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION);
89
+ const dracoExtension = scenegraph.getObjectExtension<GLTF_KHR_draco_mesh_compression>(
90
+ primitive,
91
+ KHR_DRACO_MESH_COMPRESSION
92
+ );
68
93
  if (!dracoExtension) {
69
94
  return;
70
95
  }
@@ -0,0 +1,36 @@
1
+ // GLTF EXTENSION: EXT_TEXTURE_WEBP
2
+ // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_TEXTURE_WEBP
3
+ /* eslint-disable camelcase */
4
+
5
+ import type {GLTF, GLTF_KHR_texture_basisu} from '../types/gltf-types';
6
+ import type {GLTFLoaderOptions} from '../../gltf-loader';
7
+
8
+ import GLTFScenegraph from '../api/gltf-scenegraph';
9
+
10
+ const KHR_TEXTURE_BASISU = 'KHR_texture_basisu';
11
+
12
+ /** Extension name */
13
+ export const name = KHR_TEXTURE_BASISU;
14
+
15
+ /**
16
+ * Replaces a texture source reference with the extension texture
17
+ * Done in preprocess() to prevent load of default image
18
+ */
19
+ export function preprocess(gltfData: {json: GLTF}, options: GLTFLoaderOptions): void {
20
+ const scene = new GLTFScenegraph(gltfData);
21
+ const {json} = scene;
22
+
23
+ for (const texture of json.textures || []) {
24
+ const extension = scene.getObjectExtension<GLTF_KHR_texture_basisu>(
25
+ texture,
26
+ KHR_TEXTURE_BASISU
27
+ );
28
+ if (extension) {
29
+ texture.source = extension.source;
30
+ }
31
+ scene.removeObjectExtension(texture, KHR_TEXTURE_BASISU);
32
+ }
33
+
34
+ // Remove the top-level extension
35
+ scene.removeExtension(KHR_TEXTURE_BASISU);
36
+ }
@@ -1,11 +1,14 @@
1
1
  // GLTF EXTENSION: KHR_lights_punctual
2
2
  // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
3
3
 
4
- import type {GLTF} from '../types/gltf-types';
4
+ import type {GLTF} from '../../types/gltf-types';
5
5
 
6
- import {assert} from '../utils/assert';
7
- import GLTFScenegraph from '../api/gltf-scenegraph';
8
- import {KHR_LIGHTS_PUNCTUAL} from '../gltf-utils/gltf-constants';
6
+ import {assert} from '../../utils/assert';
7
+ import GLTFScenegraph from '../../api/gltf-scenegraph';
8
+
9
+ const KHR_LIGHTS_PUNCTUAL = 'KHR_lights_punctual';
10
+
11
+ export const name = KHR_LIGHTS_PUNCTUAL;
9
12
 
10
13
  export async function decode(gltfData: {json: GLTF}): Promise<void> {
11
14
  const gltfScenegraph = new GLTFScenegraph(gltfData);
@@ -1,10 +1,13 @@
1
1
  // GLTF EXTENSION: KHR_materials_unlit
2
2
  // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
3
3
 
4
- import type {GLTF} from '../types/gltf-types';
4
+ import type {GLTF} from '../../types/gltf-types';
5
5
 
6
- import GLTFScenegraph from '../api/gltf-scenegraph';
7
- import {KHR_MATERIALS_UNLIT} from '../gltf-utils/gltf-constants';
6
+ import GLTFScenegraph from '../../api/gltf-scenegraph';
7
+
8
+ const KHR_MATERIALS_UNLIT = 'KHR_materials_unlit';
9
+
10
+ export const name = KHR_MATERIALS_UNLIT;
8
11
 
9
12
  export async function decode(gltfData: {json: GLTF}): Promise<void> {
10
13
  const gltfScenegraph = new GLTFScenegraph(gltfData);
@@ -1,10 +1,13 @@
1
1
  // GLTF EXTENSION: KHR_techniques_webgl
2
2
  // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_techniques_webgl
3
3
 
4
- import type {GLTF} from '../types/gltf-types';
4
+ import type {GLTF} from '../../types/gltf-types';
5
5
 
6
- import GLTFScenegraph from '../api/gltf-scenegraph';
7
- import {KHR_TECHNIQUES_WEBGL} from '../gltf-utils/gltf-constants';
6
+ import GLTFScenegraph from '../../api/gltf-scenegraph';
7
+
8
+ const KHR_TECHNIQUES_WEBGL = 'KHR_techniques_webgl';
9
+
10
+ export const name = KHR_TECHNIQUES_WEBGL;
8
11
 
9
12
  export async function decode(gltfData: {json: GLTF}): Promise<void> {
10
13
  const gltfScenegraph = new GLTFScenegraph(gltfData);
@@ -1,15 +1,3 @@
1
- // GLTF 1.0 extensions
2
- export const KHR_BINARY_GLTF = 'KHR_binary_glTF';
3
-
4
- // GLTF 2.0 extensions
5
- export const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression';
6
- export const KHR_LIGHTS_PUNCTUAL = 'KHR_lights_punctual';
7
- export const KHR_MATERIALS_UNLIT = 'KHR_materials_unlit';
8
- export const KHR_TECHNIQUES_WEBGL = 'KHR_techniques_webgl';
9
-
10
- // External extensions
11
- export const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression';
12
-
13
1
  const COMPONENTS = {
14
2
  SCALAR: 1,
15
3
  VEC2: 2,
@@ -1,25 +1,27 @@
1
1
  /* eslint-disable camelcase, max-statements, no-restricted-globals */
2
2
  import type {LoaderContext} from '@loaders.gl/loader-utils';
3
+ import {BasisLoader, selectSupportedBasisFormat} from '@loaders.gl/textures';
4
+ import type {GLTFLoaderOptions} from '../../gltf-loader';
3
5
  import type {GLB} from '../types/glb-types';
4
- import type {GLBParseOptions} from './parse-glb';
6
+ import type {GLTFWithBuffers} from '../types/gltf-types';
5
7
 
6
8
  import {ImageLoader} from '@loaders.gl/images';
7
9
  import {parseJSON, sliceArrayBuffer} from '@loaders.gl/loader-utils';
8
10
  import {assert} from '../utils/assert';
9
11
  import {resolveUrl} from '../gltf-utils/resolve-url';
10
12
  import {getTypedArrayForBufferView} from '../gltf-utils/get-typed-array';
11
- import {decodeExtensions} from '../extensions/gltf-extensions';
13
+ import {preprocessExtensions, decodeExtensions} from '../api/gltf-extensions';
12
14
  import {normalizeGLTFV1} from '../api/normalize-gltf-v1';
13
15
  import {postProcessGLTF} from '../api/post-process-gltf';
14
16
  import parseGLBSync, {isGLB} from './parse-glb';
15
17
 
16
18
  export type GLTFParseOptions = {
17
- excludeExtensions?: string[];
18
- decompressMeshes?: boolean;
19
19
  normalize?: boolean;
20
- loadBuffers?: boolean;
21
20
  loadImages?: boolean;
21
+ loadBuffers?: boolean;
22
+ decompressMeshes?: boolean;
22
23
  postProcess?: boolean;
24
+ excludeExtensions?: string[];
23
25
  };
24
26
 
25
27
  // export type GLTFOptions = {
@@ -32,19 +34,18 @@ export function isGLTF(arrayBuffer, options?): boolean {
32
34
  }
33
35
 
34
36
  export async function parseGLTF(
35
- gltf,
37
+ gltf: GLTFWithBuffers,
36
38
  arrayBufferOrString,
37
39
  byteOffset = 0,
38
- options: {
39
- gltf?: GLTFParseOptions;
40
- glb?: GLBParseOptions;
41
- },
40
+ options: GLTFLoaderOptions,
42
41
  context: LoaderContext
43
42
  ) {
44
43
  parseGLTFContainerSync(gltf, arrayBufferOrString, byteOffset, options);
45
44
 
46
45
  normalizeGLTFV1(gltf, {normalize: options?.gltf?.normalize});
47
46
 
47
+ preprocessExtensions(gltf, options, context);
48
+
48
49
  const promises: Promise<any>[] = [];
49
50
 
50
51
  // Load linked buffers asynchronously and decodes base64 buffers in parallel
@@ -120,10 +121,14 @@ function parseGLTFContainerSync(gltf, data, byteOffset, options) {
120
121
  gltf.images = new Array(images.length).fill({});
121
122
  }
122
123
 
123
- // Asynchronously fetch and parse buffers, store in buffers array outside of json
124
- async function loadBuffers(gltf, options, context: LoaderContext) {
125
- for (let i = 0; i < gltf.json.buffers.length; ++i) {
126
- const buffer = gltf.json.buffers[i];
124
+ /** Asynchronously fetch and parse buffers, store in buffers array outside of json
125
+ * TODO - traverse gltf and determine which buffers are actually needed
126
+ */
127
+ async function loadBuffers(gltf: GLTFWithBuffers, options, context: LoaderContext) {
128
+ // TODO
129
+ const buffers = gltf.json.buffers || [];
130
+ for (let i = 0; i < buffers.length; ++i) {
131
+ const buffer = buffers[i];
127
132
  if (buffer.uri) {
128
133
  const {fetch} = context;
129
134
  assert(fetch);
@@ -143,19 +148,49 @@ async function loadBuffers(gltf, options, context: LoaderContext) {
143
148
  }
144
149
  }
145
150
 
146
- async function loadImages(gltf, options, context: LoaderContext) {
151
+ /**
152
+ * Loads all images
153
+ * TODO - traverse gltf and determine which images are actually needed
154
+ * @param gltf
155
+ * @param options
156
+ * @param context
157
+ * @returns
158
+ */
159
+ async function loadImages(gltf: GLTFWithBuffers, options, context: LoaderContext) {
160
+ const imageIndices = getReferencesImageIndices(gltf);
161
+
147
162
  const images = gltf.json.images || [];
148
163
 
149
164
  const promises: Promise<any>[] = [];
150
- for (let i = 0; i < images.length; ++i) {
151
- promises.push(loadImage(gltf, images[i], i, options, context));
165
+ for (const imageIndex of imageIndices) {
166
+ promises.push(loadImage(gltf, images[imageIndex], imageIndex, options, context));
152
167
  }
153
168
 
154
169
  return await Promise.all(promises);
155
170
  }
156
171
 
157
- // Asynchronously fetches and parses one image, store in images array outside of json
158
- async function loadImage(gltf, image, index: number, options, context: LoaderContext) {
172
+ /** Make sure we only load images that are actually referenced by textures */
173
+ function getReferencesImageIndices(gltf: GLTFWithBuffers): number[] {
174
+ const imageIndices = new Set<number>();
175
+
176
+ const textures = gltf.json.textures || [];
177
+ for (const texture of textures) {
178
+ if (texture.source !== undefined) {
179
+ imageIndices.add(texture.source);
180
+ }
181
+ }
182
+
183
+ return Array.from(imageIndices).sort();
184
+ }
185
+
186
+ /** Asynchronously fetches and parses one image, store in images array outside of json */
187
+ async function loadImage(
188
+ gltf: GLTFWithBuffers,
189
+ image,
190
+ index: number,
191
+ options,
192
+ context: LoaderContext
193
+ ) {
159
194
  const {fetch, parse} = context;
160
195
 
161
196
  let arrayBuffer;
@@ -174,9 +209,26 @@ async function loadImage(gltf, image, index: number, options, context: LoaderCon
174
209
  assert(arrayBuffer, 'glTF image has no data');
175
210
 
176
211
  // Call `parse`
177
- const parsedImage = await parse(arrayBuffer, ImageLoader, {}, context);
212
+ let parsedImage = await parse(
213
+ arrayBuffer,
214
+ [ImageLoader, BasisLoader],
215
+ {mimeType: image.mimeType, basis: {format: selectSupportedBasisFormat()}},
216
+ context
217
+ );
218
+
219
+ if (parsedImage && parsedImage[0]) {
220
+ parsedImage = {
221
+ compressed: true,
222
+ mipmaps: false,
223
+ width: parsedImage[0].width,
224
+ height: parsedImage[0].height,
225
+ data: parsedImage
226
+ };
227
+ }
178
228
  // TODO making sure ImageLoader is overridable by using array of loaders
179
229
  // const parsedImage = await parse(arrayBuffer, [ImageLoader]);
180
230
 
231
+ // Store the loaded image
232
+ gltf.images = gltf.images || [];
181
233
  gltf.images[index] = parsedImage;
182
234
  }