-
Notifications
You must be signed in to change notification settings - Fork 32
/
GtlfUtilities.ts
204 lines (195 loc) · 6.84 KB
/
GtlfUtilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import GltfPipeline from "gltf-pipeline";
import { Buffers } from "../base/Buffers";
import { TileFormatError } from "../tileFormats/TileFormatError";
import { Extensions } from "../tilesets/Extensions";
import { GltfPipelineLegacy } from "./GltfPipelineLegacy";
/**
* Internal utility methods related to glTF/GLB data.
*/
export class GltfUtilities {
/**
* Upgrades the binary glTF (GLB) data in the given buffer.
*
* The exact pre- and postconditions of this is are intentionally
* not specified. But it is mainly used for updating a limited
* subset of glTF 1.0 binary data to glTF 2.0.
*
* @param glbBuffer - The buffer containing the GLB
* @param options - Options for the upgrade that are passed to
* `gltf-pipeline`
* @returns A promise that resolves with the upgraded GLB.
*/
static async upgradeGlb(glbBuffer: Buffer, options: any): Promise<Buffer> {
const result = await GltfPipeline.processGlb(glbBuffer, options);
return result.glb;
}
/**
* Extract the JSON part from the given GLB buffer and return it
* as a buffer.
*
* The given buffer may contain glTF 2.0 binary data, or glTF 1.0
* binary data.
*
* Note that this does NOT convert the input data. It only extracts
* the data, as-it-is.
*
* @param glbBuffer - The buffer containing the GLB
* @returns The JSON buffer
* @throws TileFormatError If the input does not contain valid GLB data.
*/
static extractJsonFromGlb(glbBuffer: Buffer): Buffer {
const magic = Buffers.getMagicString(glbBuffer);
if (magic !== "glTF") {
throw new TileFormatError(
`Expected magic header to be 'gltf', but found ${magic}`
);
}
if (glbBuffer.length < 12) {
throw new TileFormatError(
`Expected at least 12 bytes, but only got ${glbBuffer.length}`
);
}
const version = glbBuffer.readUInt32LE(4);
const length = glbBuffer.readUInt32LE(8);
if (length > glbBuffer.length) {
throw new TileFormatError(
`Header indicates ${length} bytes, but input has ${glbBuffer.length} bytes`
);
}
if (version === 1) {
if (glbBuffer.length < 20) {
throw new TileFormatError(
`Expected at least 20 bytes, but only got ${glbBuffer.length}`
);
}
const contentLength = glbBuffer.readUint32LE(12);
const contentFormat = glbBuffer.readUint32LE(16);
if (contentFormat !== 0) {
throw new TileFormatError(
`Expected content format to be 0, but found ${contentFormat}`
);
}
const contentStart = 20;
const contentEnd = contentStart + contentLength;
if (glbBuffer.length < contentEnd) {
throw new TileFormatError(
`Expected at least ${contentEnd} bytes, but only got ${glbBuffer.length}`
);
}
const contentData = glbBuffer.subarray(contentStart, contentEnd);
return contentData;
} else if (version === 2) {
if (glbBuffer.length < 20) {
throw new TileFormatError(
`Expected at least 20 bytes, but only got ${glbBuffer.length}`
);
}
const chunkLength = glbBuffer.readUint32LE(12);
const chunkType = glbBuffer.readUint32LE(16);
const jsonChunkType = 0x4e4f534a; // ASCII string for "JSON"
if (chunkType !== jsonChunkType) {
throw new TileFormatError(
`Expected chunk type to be ${jsonChunkType}, but found ${chunkType}`
);
}
const chunkStart = 20;
const chunkEnd = chunkStart + chunkLength;
if (glbBuffer.length < chunkEnd) {
throw new TileFormatError(
`Expected at least ${chunkEnd} bytes, but only got ${glbBuffer.length}`
);
}
const chunkData = glbBuffer.subarray(chunkStart, chunkEnd);
return chunkData;
} else {
throw new TileFormatError(`Expected version 1 or 2, but got ${version}`);
}
}
/**
* Given an input buffer containing a binary glTF asset, optimize it
* using gltf-pipeline with the provided options.
*
* This method also performs a few updates of certain legacy
* features that are specific for the GLB data that is part
* of I3DM and B3DM. Details are not specified here.
*
* @param glbBuffer The buffer containing the binary glTF.
* @param options Options specifying custom gltf-pipeline behavior.
* @returns A promise that resolves to the optimized binary glTF.
*/
static async optimizeGlb(glbBuffer: Buffer, options: any): Promise<Buffer> {
options = options ?? {};
if (!options.customStages) {
options.customStages = [];
}
const customStage = (gltf: any) => {
GltfPipelineLegacy.process(gltf);
return gltf;
};
options.customStages.push(customStage);
const result = await GltfPipeline.processGlb(glbBuffer, options);
return result.glb;
}
/**
* Given an input buffer containing a binary glTF asset, remove
* its use of the `CESIUM_RTC` extension by inserting new nodes
* (above the former root nodes) that contain the RTC center as
* their translation.
*
* @param glbBuffer The buffer containing the binary glTF.
* @returns A promise that resolves to the resulting binary glTF.
*/
static async replaceCesiumRtcExtension(glbBuffer: Buffer) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const customStage = (gltf: any, options: any) => {
GltfUtilities.replaceCesiumRtcExtensionInternal(gltf);
return gltf;
};
const options = {
customStages: [customStage],
keepUnusedElements: true,
};
const result = await GltfPipeline.processGlb(glbBuffer, options);
return result.glb;
}
/**
* Replaces the `CESIUM_RTC` extension in the given glTF object.
*
* This will insert a new parent node above each root node of
* a scene. These new parent nodes will have a `translation`
* that is directly taken from the `CESIUM_RTC` `center`.
*
* The `CESIUM_RTC` extension object and its used/required
* usage declarations will be removed.
*
* @param gltf - The glTF object
*/
private static replaceCesiumRtcExtensionInternal(gltf: any) {
const rtcExtension = gltf.extensions["CESIUM_RTC"];
if (!rtcExtension) {
return;
}
const rtcTranslation = rtcExtension.center;
const scenes = gltf.scenes;
if (!scenes) {
return;
}
for (const scene of scenes) {
const sceneNodes = scene.nodes;
if (sceneNodes) {
for (let i = 0; i < sceneNodes.length; i++) {
const nodeIndex = sceneNodes[i];
const newParent = {
translation: rtcTranslation,
children: [nodeIndex],
};
const newParentIndex = gltf.nodes.length;
gltf.nodes.push(newParent);
sceneNodes[i] = newParentIndex;
}
}
}
Extensions.removeExtensionUsed(gltf, "CESIUM_RTC");
Extensions.removeExtension(gltf, "CESIUM_RTC");
}
}