@loaders.gl/gltf 4.0.0-alpha.23 → 4.0.0-alpha.24
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.
- package/dist/dist.min.js +148 -102
- package/dist/es5/lib/extensions/EXT_mesh_features.js +3 -3
- package/dist/es5/lib/extensions/EXT_mesh_features.js.map +1 -1
- package/dist/es5/lib/extensions/EXT_structural_metadata.js +88 -86
- package/dist/es5/lib/extensions/EXT_structural_metadata.js.map +1 -1
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js +26 -13
- package/dist/es5/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
- package/dist/es5/lib/extensions/{data-processing.js → utils/3d-tiles-utils.js} +25 -6
- package/dist/es5/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
- package/dist/es5/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -1
- package/dist/es5/lib/types/gltf-json-schema.js.map +1 -1
- package/dist/es5/lib/utils/version.js +1 -1
- package/dist/esm/lib/extensions/EXT_mesh_features.js +1 -1
- package/dist/esm/lib/extensions/EXT_mesh_features.js.map +1 -1
- package/dist/esm/lib/extensions/EXT_structural_metadata.js +85 -81
- package/dist/esm/lib/extensions/EXT_structural_metadata.js.map +1 -1
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js +26 -13
- package/dist/esm/lib/extensions/deprecated/EXT_feature_metadata.js.map +1 -1
- package/dist/esm/lib/extensions/{data-processing.js → utils/3d-tiles-utils.js} +24 -6
- package/dist/esm/lib/extensions/utils/3d-tiles-utils.js.map +1 -0
- package/dist/esm/lib/types/gltf-ext-structural-metadata-schema.js.map +1 -1
- package/dist/esm/lib/types/gltf-json-schema.js.map +1 -1
- package/dist/esm/lib/utils/version.js +1 -1
- package/dist/lib/extensions/EXT_structural_metadata.d.ts +3 -3
- package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
- package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +52 -0
- package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -0
- package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +4 -4
- package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
- package/dist/lib/types/gltf-json-schema.d.ts +1 -1
- package/dist/lib/types/gltf-json-schema.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/lib/extensions/EXT_mesh_features.ts +1 -1
- package/src/lib/extensions/EXT_structural_metadata.ts +267 -200
- package/src/lib/extensions/deprecated/EXT_feature_metadata.ts +123 -19
- package/src/lib/extensions/{data-processing.ts → utils/3d-tiles-utils.ts} +95 -34
- package/src/lib/types/gltf-ext-structural-metadata-schema.ts +6 -4
- package/src/lib/types/gltf-json-schema.ts +1 -1
- package/dist/bundle.js +0 -5
- package/dist/es5/lib/extensions/data-processing.js.map +0 -1
- package/dist/esm/lib/extensions/data-processing.js.map +0 -1
- package/dist/glb-loader.js +0 -34
- package/dist/glb-writer.js +0 -35
- package/dist/gltf-loader.js +0 -50
- package/dist/gltf-writer.js +0 -32
- package/dist/index.js +0 -34
- package/dist/lib/api/gltf-extensions.js +0 -88
- package/dist/lib/api/gltf-scenegraph.js +0 -580
- package/dist/lib/api/normalize-gltf-v1.js +0 -299
- package/dist/lib/api/post-process-gltf.js +0 -433
- package/dist/lib/encoders/encode-glb.js +0 -72
- package/dist/lib/encoders/encode-gltf.js +0 -32
- package/dist/lib/extensions/EXT_mesh_features.js +0 -89
- package/dist/lib/extensions/EXT_meshopt_compression.js +0 -41
- package/dist/lib/extensions/EXT_structural_metadata.js +0 -504
- package/dist/lib/extensions/EXT_texture_webp.js +0 -36
- package/dist/lib/extensions/KHR_binary_gltf.js +0 -39
- package/dist/lib/extensions/KHR_draco_mesh_compression.js +0 -137
- package/dist/lib/extensions/KHR_texture_basisu.js +0 -29
- package/dist/lib/extensions/KHR_texture_transform.js +0 -227
- package/dist/lib/extensions/data-processing.d.ts +0 -34
- package/dist/lib/extensions/data-processing.d.ts.map +0 -1
- package/dist/lib/extensions/data-processing.js +0 -212
- package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +0 -282
- package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +0 -59
- package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +0 -44
- package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +0 -79
- package/dist/lib/gltf-utils/get-typed-array.js +0 -41
- package/dist/lib/gltf-utils/gltf-attribute-utils.js +0 -73
- package/dist/lib/gltf-utils/gltf-constants.js +0 -43
- package/dist/lib/gltf-utils/gltf-utils.js +0 -90
- package/dist/lib/gltf-utils/resolve-url.js +0 -18
- package/dist/lib/parsers/parse-glb.js +0 -166
- package/dist/lib/parsers/parse-gltf.js +0 -185
- package/dist/lib/types/glb-types.js +0 -2
- package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -2
- package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -2
- package/dist/lib/types/gltf-json-schema.js +0 -4
- package/dist/lib/types/gltf-postprocessed-schema.js +0 -4
- package/dist/lib/types/gltf-types.js +0 -3
- package/dist/lib/utils/assert.js +0 -12
- package/dist/lib/utils/version.js +0 -7
- package/dist/meshopt/meshopt-decoder.js +0 -118
- package/dist/webp/webp.js +0 -38
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getMemoryUsageGLTF = exports.getAccessorArrayTypeAndLength = exports.getComponentTypeFromArray = exports.getAccessorTypeFromSize = void 0;
|
|
4
|
-
const assert_1 = require("../utils/assert");
|
|
5
|
-
/**
|
|
6
|
-
* Memory needed to store texture and all mipmap levels 1 + 1/4 + 1/16 + 1/64 + ...
|
|
7
|
-
* Minimum 1.33, but due to GPU layout may be 1.5
|
|
8
|
-
*/
|
|
9
|
-
const MIPMAP_FACTOR = 1.33;
|
|
10
|
-
const TYPES = ['SCALAR', 'VEC2', 'VEC3', 'VEC4'];
|
|
11
|
-
const ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT = [
|
|
12
|
-
[Int8Array, 5120],
|
|
13
|
-
[Uint8Array, 5121],
|
|
14
|
-
[Int16Array, 5122],
|
|
15
|
-
[Uint16Array, 5123],
|
|
16
|
-
[Uint32Array, 5125],
|
|
17
|
-
[Float32Array, 5126],
|
|
18
|
-
[Float64Array, 5130]
|
|
19
|
-
];
|
|
20
|
-
const ARRAY_TO_COMPONENT_TYPE = new Map(ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT);
|
|
21
|
-
const ATTRIBUTE_TYPE_TO_COMPONENTS = {
|
|
22
|
-
SCALAR: 1,
|
|
23
|
-
VEC2: 2,
|
|
24
|
-
VEC3: 3,
|
|
25
|
-
VEC4: 4,
|
|
26
|
-
MAT2: 4,
|
|
27
|
-
MAT3: 9,
|
|
28
|
-
MAT4: 16
|
|
29
|
-
};
|
|
30
|
-
const ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE = {
|
|
31
|
-
5120: 1,
|
|
32
|
-
5121: 1,
|
|
33
|
-
5122: 2,
|
|
34
|
-
5123: 2,
|
|
35
|
-
5125: 4,
|
|
36
|
-
5126: 4
|
|
37
|
-
};
|
|
38
|
-
const ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY = {
|
|
39
|
-
5120: Int8Array,
|
|
40
|
-
5121: Uint8Array,
|
|
41
|
-
5122: Int16Array,
|
|
42
|
-
5123: Uint16Array,
|
|
43
|
-
5125: Uint32Array,
|
|
44
|
-
5126: Float32Array
|
|
45
|
-
};
|
|
46
|
-
function getAccessorTypeFromSize(size) {
|
|
47
|
-
const type = TYPES[size - 1];
|
|
48
|
-
return type || TYPES[0];
|
|
49
|
-
}
|
|
50
|
-
exports.getAccessorTypeFromSize = getAccessorTypeFromSize;
|
|
51
|
-
function getComponentTypeFromArray(typedArray) {
|
|
52
|
-
const componentType = ARRAY_TO_COMPONENT_TYPE.get(typedArray.constructor);
|
|
53
|
-
if (!componentType) {
|
|
54
|
-
throw new Error('Illegal typed array');
|
|
55
|
-
}
|
|
56
|
-
return componentType;
|
|
57
|
-
}
|
|
58
|
-
exports.getComponentTypeFromArray = getComponentTypeFromArray;
|
|
59
|
-
function getAccessorArrayTypeAndLength(accessor, bufferView) {
|
|
60
|
-
const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[accessor.componentType];
|
|
61
|
-
const components = ATTRIBUTE_TYPE_TO_COMPONENTS[accessor.type];
|
|
62
|
-
const bytesPerComponent = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[accessor.componentType];
|
|
63
|
-
const length = accessor.count * components;
|
|
64
|
-
const byteLength = accessor.count * components * bytesPerComponent;
|
|
65
|
-
(0, assert_1.assert)(byteLength >= 0 && byteLength <= bufferView.byteLength);
|
|
66
|
-
return { ArrayType, length, byteLength };
|
|
67
|
-
}
|
|
68
|
-
exports.getAccessorArrayTypeAndLength = getAccessorArrayTypeAndLength;
|
|
69
|
-
/**
|
|
70
|
-
* Calculate the GPU memory used by a GLTF tile, for both buffer and texture memory
|
|
71
|
-
* @param gltf - the gltf content of a GLTF tile
|
|
72
|
-
* @returns - total memory usage in bytes
|
|
73
|
-
*/
|
|
74
|
-
function getMemoryUsageGLTF(gltf) {
|
|
75
|
-
let { images, bufferViews } = gltf;
|
|
76
|
-
images = images || [];
|
|
77
|
-
bufferViews = bufferViews || [];
|
|
78
|
-
const imageBufferViews = images.map((i) => i.bufferView);
|
|
79
|
-
bufferViews = bufferViews.filter((view) => !imageBufferViews.includes(view));
|
|
80
|
-
const bufferMemory = bufferViews.reduce((acc, view) => acc + view.byteLength, 0);
|
|
81
|
-
// Assume each pixel of the texture is 4 channel with mimmaps (which add 33%)
|
|
82
|
-
// TODO correctly handle compressed textures
|
|
83
|
-
const pixelCount = images.reduce((acc, image) => {
|
|
84
|
-
// @ts-ignore
|
|
85
|
-
const { width, height } = image.image;
|
|
86
|
-
return acc + width * height;
|
|
87
|
-
}, 0);
|
|
88
|
-
return bufferMemory + Math.ceil(4 * pixelCount * MIPMAP_FACTOR);
|
|
89
|
-
}
|
|
90
|
-
exports.getMemoryUsageGLTF = getMemoryUsageGLTF;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveUrl = void 0;
|
|
4
|
-
// Resolves a relative url against a baseUrl
|
|
5
|
-
// If url is absolute, return it unchanged
|
|
6
|
-
function resolveUrl(url, options) {
|
|
7
|
-
// TODO: Use better logic to handle all protocols plus not delay on data
|
|
8
|
-
const absolute = url.startsWith('data:') || url.startsWith('http:') || url.startsWith('https:');
|
|
9
|
-
if (absolute) {
|
|
10
|
-
return url;
|
|
11
|
-
}
|
|
12
|
-
const baseUrl = options.baseUri || options.uri;
|
|
13
|
-
if (!baseUrl) {
|
|
14
|
-
throw new Error(`'baseUri' must be provided to resolve relative url ${url}`);
|
|
15
|
-
}
|
|
16
|
-
return baseUrl.substr(0, baseUrl.lastIndexOf('/') + 1) + url;
|
|
17
|
-
}
|
|
18
|
-
exports.resolveUrl = resolveUrl;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseGLBSync = exports.isGLB = void 0;
|
|
4
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
5
|
-
/** Binary GLTF is little endian. */
|
|
6
|
-
const LITTLE_ENDIAN = true;
|
|
7
|
-
/** 'glTF' in Big-Endian ASCII */
|
|
8
|
-
const MAGIC_glTF = 0x676c5446;
|
|
9
|
-
const GLB_FILE_HEADER_SIZE = 12;
|
|
10
|
-
const GLB_CHUNK_HEADER_SIZE = 8;
|
|
11
|
-
const GLB_CHUNK_TYPE_JSON = 0x4e4f534a;
|
|
12
|
-
const GLB_CHUNK_TYPE_BIN = 0x004e4942;
|
|
13
|
-
const GLB_V1_CONTENT_FORMAT_JSON = 0x0;
|
|
14
|
-
/** @deprecated - Backward compatibility for old xviz files */
|
|
15
|
-
const GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED = 0;
|
|
16
|
-
/** @deprecated - Backward compatibility for old xviz files */
|
|
17
|
-
const GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED = 1;
|
|
18
|
-
function getMagicString(dataView, byteOffset = 0) {
|
|
19
|
-
return `\
|
|
20
|
-
${String.fromCharCode(dataView.getUint8(byteOffset + 0))}\
|
|
21
|
-
${String.fromCharCode(dataView.getUint8(byteOffset + 1))}\
|
|
22
|
-
${String.fromCharCode(dataView.getUint8(byteOffset + 2))}\
|
|
23
|
-
${String.fromCharCode(dataView.getUint8(byteOffset + 3))}`;
|
|
24
|
-
}
|
|
25
|
-
/** Check if the contents of an array buffer contains GLB byte markers */
|
|
26
|
-
function isGLB(arrayBuffer, byteOffset = 0, options = {}) {
|
|
27
|
-
const dataView = new DataView(arrayBuffer);
|
|
28
|
-
// Check that GLB Header starts with the magic number
|
|
29
|
-
const { magic = MAGIC_glTF } = options;
|
|
30
|
-
const magic1 = dataView.getUint32(byteOffset, false);
|
|
31
|
-
return magic1 === magic || magic1 === MAGIC_glTF;
|
|
32
|
-
}
|
|
33
|
-
exports.isGLB = isGLB;
|
|
34
|
-
/**
|
|
35
|
-
* Synchronously parse a GLB
|
|
36
|
-
* @param glb - Target, Output is stored there
|
|
37
|
-
* @param arrayBuffer - Input data
|
|
38
|
-
* @param byteOffset - Offset into arrayBuffer to start parsing from (for "embedded" GLBs, e.g. in 3D tiles)
|
|
39
|
-
* @param options
|
|
40
|
-
* @returns
|
|
41
|
-
*/
|
|
42
|
-
function parseGLBSync(glb, arrayBuffer, byteOffset = 0, options = {}) {
|
|
43
|
-
// Check that GLB Header starts with the magic number
|
|
44
|
-
const dataView = new DataView(arrayBuffer);
|
|
45
|
-
// Compare format with GLBLoader documentation
|
|
46
|
-
const type = getMagicString(dataView, byteOffset + 0);
|
|
47
|
-
const version = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Version 2 of binary glTF container format
|
|
48
|
-
const byteLength = dataView.getUint32(byteOffset + 8, LITTLE_ENDIAN); // Total byte length of binary file
|
|
49
|
-
Object.assign(glb, {
|
|
50
|
-
// Put less important stuff in a header, to avoid clutter
|
|
51
|
-
header: {
|
|
52
|
-
byteOffset,
|
|
53
|
-
byteLength,
|
|
54
|
-
hasBinChunk: false
|
|
55
|
-
},
|
|
56
|
-
type,
|
|
57
|
-
version,
|
|
58
|
-
json: {},
|
|
59
|
-
binChunks: []
|
|
60
|
-
});
|
|
61
|
-
byteOffset += GLB_FILE_HEADER_SIZE;
|
|
62
|
-
switch (glb.version) {
|
|
63
|
-
case 1:
|
|
64
|
-
return parseGLBV1(glb, dataView, byteOffset);
|
|
65
|
-
case 2:
|
|
66
|
-
return parseGLBV2(glb, dataView, byteOffset, (options = {}));
|
|
67
|
-
default:
|
|
68
|
-
throw new Error(`Invalid GLB version ${glb.version}. Only supports version 1 and 2.`);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
exports.parseGLBSync = parseGLBSync;
|
|
72
|
-
/**
|
|
73
|
-
* Parse a V1 GLB
|
|
74
|
-
* @param glb - target, output is stored in this object
|
|
75
|
-
* @param dataView - Input, memory to be parsed
|
|
76
|
-
* @param byteOffset - Offset of first byte of GLB data in the data view
|
|
77
|
-
* @returns Number of bytes parsed (there could be additional non-GLB data after the GLB)
|
|
78
|
-
*/
|
|
79
|
-
function parseGLBV1(glb, dataView, byteOffset) {
|
|
80
|
-
// Sanity: ensure file is big enough to hold at least the headers
|
|
81
|
-
(0, loader_utils_1.assert)(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
|
|
82
|
-
// Explanation of GLB structure:
|
|
83
|
-
// https://cloud.githubusercontent.com/assets/3479527/22600725/36b87122-ea55-11e6-9d40-6fd42819fcab.png
|
|
84
|
-
const contentLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN); // Byte length of chunk
|
|
85
|
-
const contentFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Chunk format as uint32
|
|
86
|
-
byteOffset += GLB_CHUNK_HEADER_SIZE;
|
|
87
|
-
// GLB v1 only supports a single chunk type
|
|
88
|
-
(0, loader_utils_1.assert)(contentFormat === GLB_V1_CONTENT_FORMAT_JSON);
|
|
89
|
-
parseJSONChunk(glb, dataView, byteOffset, contentLength);
|
|
90
|
-
// No need to call the function padToBytes() from parseJSONChunk()
|
|
91
|
-
byteOffset += contentLength;
|
|
92
|
-
byteOffset += parseBINChunk(glb, dataView, byteOffset, glb.header.byteLength);
|
|
93
|
-
return byteOffset;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Parse a V2 GLB
|
|
97
|
-
* @param glb - target, output is stored in this object
|
|
98
|
-
* @param dataView - Input, memory to be parsed
|
|
99
|
-
* @param byteOffset - Offset of first byte of GLB data in the data view
|
|
100
|
-
* @returns Number of bytes parsed (there could be additional non-GLB data after the GLB)
|
|
101
|
-
*/
|
|
102
|
-
function parseGLBV2(glb, dataView, byteOffset, options) {
|
|
103
|
-
// Sanity: ensure file is big enough to hold at least the first chunk header
|
|
104
|
-
(0, loader_utils_1.assert)(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE);
|
|
105
|
-
parseGLBChunksSync(glb, dataView, byteOffset, options);
|
|
106
|
-
return byteOffset + glb.header.byteLength;
|
|
107
|
-
}
|
|
108
|
-
/** Iterate over GLB chunks and parse them */
|
|
109
|
-
function parseGLBChunksSync(glb, dataView, byteOffset, options) {
|
|
110
|
-
// Per spec we must iterate over chunks, ignoring all except JSON and BIN
|
|
111
|
-
// Iterate as long as there is space left for another chunk header
|
|
112
|
-
while (byteOffset + 8 <= glb.header.byteLength) {
|
|
113
|
-
const chunkLength = dataView.getUint32(byteOffset + 0, LITTLE_ENDIAN); // Byte length of chunk
|
|
114
|
-
const chunkFormat = dataView.getUint32(byteOffset + 4, LITTLE_ENDIAN); // Chunk format as uint32
|
|
115
|
-
byteOffset += GLB_CHUNK_HEADER_SIZE;
|
|
116
|
-
// Per spec we must iterate over chunks, ignoring all except JSON and BIN
|
|
117
|
-
switch (chunkFormat) {
|
|
118
|
-
case GLB_CHUNK_TYPE_JSON:
|
|
119
|
-
parseJSONChunk(glb, dataView, byteOffset, chunkLength);
|
|
120
|
-
break;
|
|
121
|
-
case GLB_CHUNK_TYPE_BIN:
|
|
122
|
-
parseBINChunk(glb, dataView, byteOffset, chunkLength);
|
|
123
|
-
break;
|
|
124
|
-
// Backward compatibility for very old xviz files
|
|
125
|
-
case GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED:
|
|
126
|
-
if (!options.strict) {
|
|
127
|
-
parseJSONChunk(glb, dataView, byteOffset, chunkLength);
|
|
128
|
-
}
|
|
129
|
-
break;
|
|
130
|
-
case GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED:
|
|
131
|
-
if (!options.strict) {
|
|
132
|
-
parseBINChunk(glb, dataView, byteOffset, chunkLength);
|
|
133
|
-
}
|
|
134
|
-
break;
|
|
135
|
-
default:
|
|
136
|
-
// Ignore, per spec
|
|
137
|
-
// console.warn(`Unknown GLB chunk type`); // eslint-disable-line
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
byteOffset += (0, loader_utils_1.padToNBytes)(chunkLength, 4);
|
|
141
|
-
}
|
|
142
|
-
return byteOffset;
|
|
143
|
-
}
|
|
144
|
-
/* Parse a GLB JSON chunk */
|
|
145
|
-
function parseJSONChunk(glb, dataView, byteOffset, chunkLength) {
|
|
146
|
-
// 1. Create a "view" of the binary encoded JSON data inside the GLB
|
|
147
|
-
const jsonChunk = new Uint8Array(dataView.buffer, byteOffset, chunkLength);
|
|
148
|
-
// 2. Decode the JSON binary array into clear text
|
|
149
|
-
const textDecoder = new TextDecoder('utf8');
|
|
150
|
-
const jsonText = textDecoder.decode(jsonChunk);
|
|
151
|
-
// 3. Parse the JSON text into a JavaScript data structure
|
|
152
|
-
glb.json = JSON.parse(jsonText);
|
|
153
|
-
return (0, loader_utils_1.padToNBytes)(chunkLength, 4);
|
|
154
|
-
}
|
|
155
|
-
/** Parse a GLB BIN chunk */
|
|
156
|
-
function parseBINChunk(glb, dataView, byteOffset, chunkLength) {
|
|
157
|
-
// Note: BIN chunk can be optional
|
|
158
|
-
glb.header.hasBinChunk = true;
|
|
159
|
-
glb.binChunks.push({
|
|
160
|
-
byteOffset,
|
|
161
|
-
byteLength: chunkLength,
|
|
162
|
-
arrayBuffer: dataView.buffer
|
|
163
|
-
// TODO - copy, or create typed array view?
|
|
164
|
-
});
|
|
165
|
-
return (0, loader_utils_1.padToNBytes)(chunkLength, 4);
|
|
166
|
-
}
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseGLTF = exports.isGLTF = void 0;
|
|
4
|
-
const loader_utils_1 = require("@loaders.gl/loader-utils");
|
|
5
|
-
const images_1 = require("@loaders.gl/images");
|
|
6
|
-
const textures_1 = require("@loaders.gl/textures");
|
|
7
|
-
const assert_1 = require("../utils/assert");
|
|
8
|
-
const parse_glb_1 = require("./parse-glb");
|
|
9
|
-
const resolve_url_1 = require("../gltf-utils/resolve-url");
|
|
10
|
-
const get_typed_array_1 = require("../gltf-utils/get-typed-array");
|
|
11
|
-
const gltf_extensions_1 = require("../api/gltf-extensions");
|
|
12
|
-
const normalize_gltf_v1_1 = require("../api/normalize-gltf-v1");
|
|
13
|
-
/** Check if an array buffer appears to contain GLTF data */
|
|
14
|
-
function isGLTF(arrayBuffer, options) {
|
|
15
|
-
const byteOffset = 0;
|
|
16
|
-
return (0, parse_glb_1.isGLB)(arrayBuffer, byteOffset, options);
|
|
17
|
-
}
|
|
18
|
-
exports.isGLTF = isGLTF;
|
|
19
|
-
async function parseGLTF(gltf, arrayBufferOrString, byteOffset = 0, options, context) {
|
|
20
|
-
parseGLTFContainerSync(gltf, arrayBufferOrString, byteOffset, options);
|
|
21
|
-
(0, normalize_gltf_v1_1.normalizeGLTFV1)(gltf, { normalize: options?.gltf?.normalize });
|
|
22
|
-
(0, gltf_extensions_1.preprocessExtensions)(gltf, options, context);
|
|
23
|
-
// Load linked buffers asynchronously and decodes base64 buffers in parallel
|
|
24
|
-
if (options?.gltf?.loadBuffers && gltf.json.buffers) {
|
|
25
|
-
await loadBuffers(gltf, options, context);
|
|
26
|
-
}
|
|
27
|
-
// loadImages and decodeExtensions should not be running in parallel, because
|
|
28
|
-
// decodeExtensions uses data from images taken during the loadImages call.
|
|
29
|
-
if (options?.gltf?.loadImages) {
|
|
30
|
-
await loadImages(gltf, options, context);
|
|
31
|
-
}
|
|
32
|
-
await (0, gltf_extensions_1.decodeExtensions)(gltf, options, context);
|
|
33
|
-
return gltf;
|
|
34
|
-
}
|
|
35
|
-
exports.parseGLTF = parseGLTF;
|
|
36
|
-
/**
|
|
37
|
-
*
|
|
38
|
-
* @param gltf
|
|
39
|
-
* @param data - can be ArrayBuffer (GLB), ArrayBuffer (Binary JSON), String (JSON), or Object (parsed JSON)
|
|
40
|
-
* @param byteOffset
|
|
41
|
-
* @param options
|
|
42
|
-
*/
|
|
43
|
-
function parseGLTFContainerSync(gltf, data, byteOffset, options) {
|
|
44
|
-
// Initialize gltf container
|
|
45
|
-
if (options.uri) {
|
|
46
|
-
gltf.baseUri = options.uri;
|
|
47
|
-
}
|
|
48
|
-
// If data is binary and starting with magic bytes, assume binary JSON text, convert to string
|
|
49
|
-
if (data instanceof ArrayBuffer && !(0, parse_glb_1.isGLB)(data, byteOffset, options)) {
|
|
50
|
-
const textDecoder = new TextDecoder();
|
|
51
|
-
data = textDecoder.decode(data);
|
|
52
|
-
}
|
|
53
|
-
if (typeof data === 'string') {
|
|
54
|
-
// If string, try to parse as JSON
|
|
55
|
-
gltf.json = (0, loader_utils_1.parseJSON)(data);
|
|
56
|
-
}
|
|
57
|
-
else if (data instanceof ArrayBuffer) {
|
|
58
|
-
// If still ArrayBuffer, parse as GLB container
|
|
59
|
-
const glb = {};
|
|
60
|
-
byteOffset = (0, parse_glb_1.parseGLBSync)(glb, data, byteOffset, options.glb);
|
|
61
|
-
(0, assert_1.assert)(glb.type === 'glTF', `Invalid GLB magic string ${glb.type}`);
|
|
62
|
-
gltf._glb = glb;
|
|
63
|
-
gltf.json = glb.json;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
(0, assert_1.assert)(false, 'GLTF: must be ArrayBuffer or string');
|
|
67
|
-
}
|
|
68
|
-
// Populate buffers
|
|
69
|
-
// Create an external buffers array to hold binary data
|
|
70
|
-
const buffers = gltf.json.buffers || [];
|
|
71
|
-
gltf.buffers = new Array(buffers.length).fill(null);
|
|
72
|
-
// Populates JSON and some bin chunk info
|
|
73
|
-
if (gltf._glb && gltf._glb.header.hasBinChunk) {
|
|
74
|
-
const { binChunks } = gltf._glb;
|
|
75
|
-
gltf.buffers[0] = {
|
|
76
|
-
arrayBuffer: binChunks[0].arrayBuffer,
|
|
77
|
-
byteOffset: binChunks[0].byteOffset,
|
|
78
|
-
byteLength: binChunks[0].byteLength
|
|
79
|
-
};
|
|
80
|
-
// TODO - this modifies JSON and is a post processing thing
|
|
81
|
-
// gltf.json.buffers[0].data = gltf.buffers[0].arrayBuffer;
|
|
82
|
-
// gltf.json.buffers[0].byteOffset = gltf.buffers[0].byteOffset;
|
|
83
|
-
}
|
|
84
|
-
// Populate images
|
|
85
|
-
const images = gltf.json.images || [];
|
|
86
|
-
gltf.images = new Array(images.length).fill({});
|
|
87
|
-
}
|
|
88
|
-
/** Asynchronously fetch and parse buffers, store in buffers array outside of json
|
|
89
|
-
* TODO - traverse gltf and determine which buffers are actually needed
|
|
90
|
-
*/
|
|
91
|
-
async function loadBuffers(gltf, options, context) {
|
|
92
|
-
// TODO
|
|
93
|
-
const buffers = gltf.json.buffers || [];
|
|
94
|
-
for (let i = 0; i < buffers.length; ++i) {
|
|
95
|
-
const buffer = buffers[i];
|
|
96
|
-
if (buffer.uri) {
|
|
97
|
-
const { fetch } = context;
|
|
98
|
-
(0, assert_1.assert)(fetch);
|
|
99
|
-
const uri = (0, resolve_url_1.resolveUrl)(buffer.uri, options);
|
|
100
|
-
const response = await context?.fetch?.(uri);
|
|
101
|
-
const arrayBuffer = await response?.arrayBuffer?.();
|
|
102
|
-
gltf.buffers[i] = {
|
|
103
|
-
arrayBuffer,
|
|
104
|
-
byteOffset: 0,
|
|
105
|
-
byteLength: arrayBuffer.byteLength
|
|
106
|
-
};
|
|
107
|
-
delete buffer.uri;
|
|
108
|
-
}
|
|
109
|
-
else if (gltf.buffers[i] === null) {
|
|
110
|
-
gltf.buffers[i] = {
|
|
111
|
-
arrayBuffer: new ArrayBuffer(buffer.byteLength),
|
|
112
|
-
byteOffset: 0,
|
|
113
|
-
byteLength: buffer.byteLength
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Loads all images
|
|
120
|
-
* TODO - traverse gltf and determine which images are actually needed
|
|
121
|
-
* @param gltf
|
|
122
|
-
* @param options
|
|
123
|
-
* @param context
|
|
124
|
-
* @returns
|
|
125
|
-
*/
|
|
126
|
-
async function loadImages(gltf, options, context) {
|
|
127
|
-
const imageIndices = getReferencesImageIndices(gltf);
|
|
128
|
-
const images = gltf.json.images || [];
|
|
129
|
-
const promises = [];
|
|
130
|
-
for (const imageIndex of imageIndices) {
|
|
131
|
-
promises.push(loadImage(gltf, images[imageIndex], imageIndex, options, context));
|
|
132
|
-
}
|
|
133
|
-
return await Promise.all(promises);
|
|
134
|
-
}
|
|
135
|
-
/** Make sure we only load images that are actually referenced by textures */
|
|
136
|
-
function getReferencesImageIndices(gltf) {
|
|
137
|
-
const imageIndices = new Set();
|
|
138
|
-
const textures = gltf.json.textures || [];
|
|
139
|
-
for (const texture of textures) {
|
|
140
|
-
if (texture.source !== undefined) {
|
|
141
|
-
imageIndices.add(texture.source);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return Array.from(imageIndices).sort();
|
|
145
|
-
}
|
|
146
|
-
/** Asynchronously fetches and parses one image, store in images array outside of json */
|
|
147
|
-
async function loadImage(gltf, image, index, options, context) {
|
|
148
|
-
let arrayBuffer;
|
|
149
|
-
if (image.uri && !image.hasOwnProperty('bufferView')) {
|
|
150
|
-
const uri = (0, resolve_url_1.resolveUrl)(image.uri, options);
|
|
151
|
-
const { fetch } = context;
|
|
152
|
-
const response = await fetch(uri);
|
|
153
|
-
arrayBuffer = await response.arrayBuffer();
|
|
154
|
-
image.bufferView = {
|
|
155
|
-
data: arrayBuffer
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
if (Number.isFinite(image.bufferView)) {
|
|
159
|
-
const array = (0, get_typed_array_1.getTypedArrayForBufferView)(gltf.json, gltf.buffers, image.bufferView);
|
|
160
|
-
arrayBuffer = (0, loader_utils_1.sliceArrayBuffer)(array.buffer, array.byteOffset, array.byteLength);
|
|
161
|
-
}
|
|
162
|
-
(0, assert_1.assert)(arrayBuffer, 'glTF image has no data');
|
|
163
|
-
// Call `parse`
|
|
164
|
-
let parsedImage = (await (0, loader_utils_1.parseFromContext)(arrayBuffer, [images_1.ImageLoader, textures_1.BasisLoader], {
|
|
165
|
-
...options,
|
|
166
|
-
mimeType: image.mimeType,
|
|
167
|
-
basis: options.basis || { format: (0, textures_1.selectSupportedBasisFormat)() }
|
|
168
|
-
}, context));
|
|
169
|
-
if (parsedImage && parsedImage[0]) {
|
|
170
|
-
parsedImage = {
|
|
171
|
-
compressed: true,
|
|
172
|
-
// @ts-expect-error
|
|
173
|
-
mipmaps: false,
|
|
174
|
-
width: parsedImage[0].width,
|
|
175
|
-
height: parsedImage[0].height,
|
|
176
|
-
data: parsedImage[0]
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
// TODO making sure ImageLoader is overridable by using array of loaders
|
|
180
|
-
// const parsedImage = await parse(arrayBuffer, [ImageLoader]);
|
|
181
|
-
// Store the loaded image
|
|
182
|
-
gltf.images = gltf.images || [];
|
|
183
|
-
// @ts-expect-error TODO - sort out image typing asap
|
|
184
|
-
gltf.images[index] = parsedImage;
|
|
185
|
-
}
|
package/dist/lib/utils/assert.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assert = void 0;
|
|
4
|
-
// Replacement for the external assert method to reduce bundle size
|
|
5
|
-
// Note: We don't use the second "message" argument in calling code,
|
|
6
|
-
// so no need to support it here
|
|
7
|
-
function assert(condition, message) {
|
|
8
|
-
if (!condition) {
|
|
9
|
-
throw new Error(message || 'assert failed: gltf');
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
exports.assert = assert;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.VERSION = void 0;
|
|
4
|
-
// Version constant cannot be imported, it needs to correspond to the build version of **this** module.
|
|
5
|
-
// __VERSION__ is injected by babel-plugin-version-inline
|
|
6
|
-
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
|
|
7
|
-
exports.VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
|