@loaders.gl/gltf 4.0.0-alpha.9 → 4.0.0-beta.2

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