@babylonjs/serializers 5.0.0-beta.8 → 5.0.0-beta.8-snapshot

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 (79) hide show
  1. package/package.json +20 -105
  2. package/OBJ/index.d.ts +0 -1
  3. package/OBJ/index.js +0 -2
  4. package/OBJ/index.js.map +0 -1
  5. package/OBJ/objSerializer.d.ts +0 -21
  6. package/OBJ/objSerializer.js +0 -160
  7. package/OBJ/objSerializer.js.map +0 -1
  8. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +0 -38
  9. package/glTF/2.0/Extensions/KHR_lights_punctual.js +0 -174
  10. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +0 -1
  11. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +0 -24
  12. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +0 -91
  13. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +0 -1
  14. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +0 -24
  15. package/glTF/2.0/Extensions/KHR_materials_sheen.js +0 -74
  16. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +0 -1
  17. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +0 -21
  18. package/glTF/2.0/Extensions/KHR_materials_unlit.js +0 -52
  19. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +0 -1
  20. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +0 -34
  21. package/glTF/2.0/Extensions/KHR_texture_transform.js +0 -137
  22. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +0 -1
  23. package/glTF/2.0/Extensions/index.d.ts +0 -5
  24. package/glTF/2.0/Extensions/index.js +0 -6
  25. package/glTF/2.0/Extensions/index.js.map +0 -1
  26. package/glTF/2.0/glTFAnimation.d.ts +0 -200
  27. package/glTF/2.0/glTFAnimation.js +0 -840
  28. package/glTF/2.0/glTFAnimation.js.map +0 -1
  29. package/glTF/2.0/glTFData.d.ts +0 -19
  30. package/glTF/2.0/glTFData.js +0 -53
  31. package/glTF/2.0/glTFData.js.map +0 -1
  32. package/glTF/2.0/glTFExporter.d.ts +0 -445
  33. package/glTF/2.0/glTFExporter.js +0 -1943
  34. package/glTF/2.0/glTFExporter.js.map +0 -1
  35. package/glTF/2.0/glTFExporterExtension.d.ts +0 -74
  36. package/glTF/2.0/glTFExporterExtension.js +0 -3
  37. package/glTF/2.0/glTFExporterExtension.js.map +0 -1
  38. package/glTF/2.0/glTFMaterialExporter.d.ts +0 -230
  39. package/glTF/2.0/glTFMaterialExporter.js +0 -1143
  40. package/glTF/2.0/glTFMaterialExporter.js.map +0 -1
  41. package/glTF/2.0/glTFSerializer.d.ts +0 -60
  42. package/glTF/2.0/glTFSerializer.js +0 -63
  43. package/glTF/2.0/glTFSerializer.js.map +0 -1
  44. package/glTF/2.0/glTFUtilities.d.ts +0 -96
  45. package/glTF/2.0/glTFUtilities.js +0 -196
  46. package/glTF/2.0/glTFUtilities.js.map +0 -1
  47. package/glTF/2.0/index.d.ts +0 -8
  48. package/glTF/2.0/index.js +0 -9
  49. package/glTF/2.0/index.js.map +0 -1
  50. package/glTF/2.0/shaders/textureTransform.fragment.d.ts +0 -5
  51. package/glTF/2.0/shaders/textureTransform.fragment.js +0 -7
  52. package/glTF/2.0/shaders/textureTransform.fragment.js.map +0 -1
  53. package/glTF/glTFFileExporter.d.ts +0 -20
  54. package/glTF/glTFFileExporter.js +0 -3
  55. package/glTF/glTFFileExporter.js.map +0 -1
  56. package/glTF/index.d.ts +0 -2
  57. package/glTF/index.js +0 -3
  58. package/glTF/index.js.map +0 -1
  59. package/index.d.ts +0 -3
  60. package/index.js.map +0 -1
  61. package/legacy/legacy-glTF2Serializer.d.ts +0 -2
  62. package/legacy/legacy-glTF2Serializer.js +0 -44
  63. package/legacy/legacy-glTF2Serializer.js.map +0 -1
  64. package/legacy/legacy-objSerializer.d.ts +0 -1
  65. package/legacy/legacy-objSerializer.js +0 -13
  66. package/legacy/legacy-objSerializer.js.map +0 -1
  67. package/legacy/legacy-stlSerializer.d.ts +0 -1
  68. package/legacy/legacy-stlSerializer.js +0 -13
  69. package/legacy/legacy-stlSerializer.js.map +0 -1
  70. package/legacy/legacy.d.ts +0 -4
  71. package/legacy/legacy.js +0 -5
  72. package/legacy/legacy.js.map +0 -1
  73. package/readme.md +0 -29
  74. package/stl/index.d.ts +0 -1
  75. package/stl/index.js +0 -2
  76. package/stl/index.js.map +0 -1
  77. package/stl/stlSerializer.d.ts +0 -17
  78. package/stl/stlSerializer.js +0 -108
  79. package/stl/stlSerializer.js.map +0 -1
@@ -1,1943 +0,0 @@
1
- import { __spreadArray } from "tslib";
2
- import { Vector2, Vector3, Vector4, Quaternion } from "@babylonjs/core/Maths/math.vector.js";
3
- import { Color3, Color4 } from "@babylonjs/core/Maths/math.color.js";
4
- import { Tools } from "@babylonjs/core/Misc/tools.js";
5
- import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
6
- import { TransformNode } from "@babylonjs/core/Meshes/transformNode.js";
7
- import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
8
- import { LinesMesh } from "@babylonjs/core/Meshes/linesMesh.js";
9
- import { InstancedMesh } from "@babylonjs/core/Meshes/instancedMesh.js";
10
- import { Material } from "@babylonjs/core/Materials/material.js";
11
- import { _GLTFMaterialExporter } from "./glTFMaterialExporter.js";
12
- import { _GLTFUtilities } from "./glTFUtilities.js";
13
- import { GLTFData } from "./glTFData.js";
14
- import { _GLTFAnimation } from "./glTFAnimation.js";
15
- import { Camera } from "@babylonjs/core/Cameras/camera.js";
16
- import { EngineStore } from "@babylonjs/core/Engines/engineStore.js";
17
- /**
18
- * Converts Babylon Scene into glTF 2.0.
19
- * @hidden
20
- */
21
- var _Exporter = /** @class */ (function () {
22
- /**
23
- * Creates a glTF Exporter instance, which can accept optional exporter options
24
- * @param babylonScene Babylon scene object
25
- * @param options Options to modify the behavior of the exporter
26
- */
27
- function _Exporter(babylonScene, options) {
28
- /*
29
- * Specifies if root Babylon empty nodes that act as a coordinate space transform should be included in export
30
- */
31
- this._includeCoordinateSystemConversionNodes = false;
32
- this._extensions = {};
33
- this._glTF = {
34
- asset: { generator: "BabylonJS", version: "2.0" }
35
- };
36
- babylonScene = babylonScene || EngineStore.LastCreatedScene;
37
- if (!babylonScene) {
38
- return;
39
- }
40
- this._babylonScene = babylonScene;
41
- this._bufferViews = [];
42
- this._accessors = [];
43
- this._meshes = [];
44
- this._scenes = [];
45
- this._cameras = [];
46
- this._nodes = [];
47
- this._images = [];
48
- this._materials = [];
49
- this._materialMap = [];
50
- this._textures = [];
51
- this._samplers = [];
52
- this._skins = [];
53
- this._animations = [];
54
- this._imageData = {};
55
- this._orderedImageData = [];
56
- this._options = options || {};
57
- this._animationSampleRate = options && options.animationSampleRate ? options.animationSampleRate : 1 / 60;
58
- this._includeCoordinateSystemConversionNodes = options && options.includeCoordinateSystemConversionNodes ? true : false;
59
- this._glTFMaterialExporter = new _GLTFMaterialExporter(this);
60
- this._loadExtensions();
61
- }
62
- _Exporter.prototype._applyExtension = function (node, extensions, index, actionAsync) {
63
- var _this = this;
64
- if (index >= extensions.length) {
65
- return Promise.resolve(node);
66
- }
67
- var currentPromise = actionAsync(extensions[index], node);
68
- if (!currentPromise) {
69
- return this._applyExtension(node, extensions, index + 1, actionAsync);
70
- }
71
- return currentPromise.then(function (newNode) { return _this._applyExtension(newNode, extensions, index + 1, actionAsync); });
72
- };
73
- _Exporter.prototype._applyExtensions = function (node, actionAsync) {
74
- var extensions = [];
75
- for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
76
- var name_1 = _a[_i];
77
- extensions.push(this._extensions[name_1]);
78
- }
79
- return this._applyExtension(node, extensions, 0, actionAsync);
80
- };
81
- _Exporter.prototype._extensionsPreExportTextureAsync = function (context, babylonTexture, mimeType) {
82
- return this._applyExtensions(babylonTexture, function (extension, node) { return extension.preExportTextureAsync && extension.preExportTextureAsync(context, node, mimeType); });
83
- };
84
- _Exporter.prototype._extensionsPostExportMeshPrimitiveAsync = function (context, meshPrimitive, babylonSubMesh, binaryWriter) {
85
- return this._applyExtensions(meshPrimitive, function (extension, node) { return extension.postExportMeshPrimitiveAsync && extension.postExportMeshPrimitiveAsync(context, node, babylonSubMesh, binaryWriter); });
86
- };
87
- _Exporter.prototype._extensionsPostExportNodeAsync = function (context, node, babylonNode, nodeMap) {
88
- return this._applyExtensions(node, function (extension, node) { return extension.postExportNodeAsync && extension.postExportNodeAsync(context, node, babylonNode, nodeMap); });
89
- };
90
- _Exporter.prototype._extensionsPostExportMaterialAsync = function (context, material, babylonMaterial) {
91
- return this._applyExtensions(material, function (extension, node) { return extension.postExportMaterialAsync && extension.postExportMaterialAsync(context, node, babylonMaterial); });
92
- };
93
- _Exporter.prototype._extensionsPostExportMaterialAdditionalTextures = function (context, material, babylonMaterial) {
94
- var output = [];
95
- for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
96
- var name_2 = _a[_i];
97
- var extension = this._extensions[name_2];
98
- if (extension.postExportMaterialAdditionalTextures) {
99
- output.push.apply(output, extension.postExportMaterialAdditionalTextures(context, material, babylonMaterial));
100
- }
101
- }
102
- return output;
103
- };
104
- _Exporter.prototype._extensionsPostExportTextures = function (context, textureInfo, babylonTexture) {
105
- for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
106
- var name_3 = _a[_i];
107
- var extension = this._extensions[name_3];
108
- if (extension.postExportTexture) {
109
- extension.postExportTexture(context, textureInfo, babylonTexture);
110
- }
111
- }
112
- };
113
- _Exporter.prototype._forEachExtensions = function (action) {
114
- for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
115
- var name_4 = _a[_i];
116
- var extension = this._extensions[name_4];
117
- if (extension.enabled) {
118
- action(extension);
119
- }
120
- }
121
- };
122
- _Exporter.prototype._extensionsOnExporting = function () {
123
- var _this = this;
124
- this._forEachExtensions(function (extension) {
125
- if (extension.wasUsed) {
126
- if (_this._glTF.extensionsUsed == null) {
127
- _this._glTF.extensionsUsed = [];
128
- }
129
- if (_this._glTF.extensionsUsed.indexOf(extension.name) === -1) {
130
- _this._glTF.extensionsUsed.push(extension.name);
131
- }
132
- if (extension.required) {
133
- if (_this._glTF.extensionsRequired == null) {
134
- _this._glTF.extensionsRequired = [];
135
- }
136
- if (_this._glTF.extensionsRequired.indexOf(extension.name) === -1) {
137
- _this._glTF.extensionsRequired.push(extension.name);
138
- }
139
- }
140
- if (_this._glTF.extensions == null) {
141
- _this._glTF.extensions = {};
142
- }
143
- if (extension.onExporting) {
144
- extension.onExporting();
145
- }
146
- }
147
- });
148
- };
149
- /**
150
- * Load glTF serializer extensions
151
- */
152
- _Exporter.prototype._loadExtensions = function () {
153
- for (var _i = 0, _a = _Exporter._ExtensionNames; _i < _a.length; _i++) {
154
- var name_5 = _a[_i];
155
- var extension = _Exporter._ExtensionFactories[name_5](this);
156
- this._extensions[name_5] = extension;
157
- }
158
- };
159
- _Exporter.prototype.dispose = function () {
160
- for (var extensionKey in this._extensions) {
161
- var extension = this._extensions[extensionKey];
162
- extension.dispose();
163
- }
164
- };
165
- /**
166
- * Registers a glTF exporter extension
167
- * @param name Name of the extension to export
168
- * @param factory The factory function that creates the exporter extension
169
- */
170
- _Exporter.RegisterExtension = function (name, factory) {
171
- if (_Exporter.UnregisterExtension(name)) {
172
- Tools.Warn("Extension with the name ".concat(name, " already exists"));
173
- }
174
- _Exporter._ExtensionFactories[name] = factory;
175
- _Exporter._ExtensionNames.push(name);
176
- };
177
- /**
178
- * Un-registers an exporter extension
179
- * @param name The name fo the exporter extension
180
- * @returns A boolean indicating whether the extension has been un-registered
181
- */
182
- _Exporter.UnregisterExtension = function (name) {
183
- if (!_Exporter._ExtensionFactories[name]) {
184
- return false;
185
- }
186
- delete _Exporter._ExtensionFactories[name];
187
- var index = _Exporter._ExtensionNames.indexOf(name);
188
- if (index !== -1) {
189
- _Exporter._ExtensionNames.splice(index, 1);
190
- }
191
- return true;
192
- };
193
- _Exporter.prototype.reorderIndicesBasedOnPrimitiveMode = function (submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter) {
194
- switch (primitiveMode) {
195
- case Material.TriangleFillMode: {
196
- if (!byteOffset) {
197
- byteOffset = 0;
198
- }
199
- for (var i = submesh.indexStart, length_1 = submesh.indexStart + submesh.indexCount; i < length_1; i = i + 3) {
200
- var index = byteOffset + i * 4;
201
- // swap the second and third indices
202
- var secondIndex = binaryWriter.getUInt32(index + 4);
203
- var thirdIndex = binaryWriter.getUInt32(index + 8);
204
- binaryWriter.setUInt32(thirdIndex, index + 4);
205
- binaryWriter.setUInt32(secondIndex, index + 8);
206
- }
207
- break;
208
- }
209
- case Material.TriangleFanDrawMode: {
210
- for (var i = submesh.indexStart + submesh.indexCount - 1, start = submesh.indexStart; i >= start; --i) {
211
- binaryWriter.setUInt32(babylonIndices[i], byteOffset);
212
- byteOffset += 4;
213
- }
214
- break;
215
- }
216
- case Material.TriangleStripDrawMode: {
217
- if (submesh.indexCount >= 3) {
218
- binaryWriter.setUInt32(babylonIndices[submesh.indexStart + 2], byteOffset + 4);
219
- binaryWriter.setUInt32(babylonIndices[submesh.indexStart + 1], byteOffset + 8);
220
- }
221
- break;
222
- }
223
- }
224
- };
225
- /**
226
- * Reorders the vertex attribute data based on the primitive mode. This is necessary when indices are not available and the winding order is
227
- * clock-wise during export to glTF
228
- * @param submesh BabylonJS submesh
229
- * @param primitiveMode Primitive mode of the mesh
230
- * @param sideOrientation the winding order of the submesh
231
- * @param vertexBufferKind The type of vertex attribute
232
- * @param meshAttributeArray The vertex attribute data
233
- * @param byteOffset The offset to the binary data
234
- * @param binaryWriter The binary data for the glTF file
235
- * @param convertToRightHandedSystem Converts the values to right-handed
236
- */
237
- _Exporter.prototype.reorderVertexAttributeDataBasedOnPrimitiveMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem) {
238
- if (convertToRightHandedSystem && sideOrientation === Material.ClockWiseSideOrientation) {
239
- switch (primitiveMode) {
240
- case Material.TriangleFillMode: {
241
- this.reorderTriangleFillMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem);
242
- break;
243
- }
244
- case Material.TriangleStripDrawMode: {
245
- this.reorderTriangleStripDrawMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem);
246
- break;
247
- }
248
- case Material.TriangleFanDrawMode: {
249
- this.reorderTriangleFanMode(submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem);
250
- break;
251
- }
252
- }
253
- }
254
- };
255
- /**
256
- * Reorders the vertex attributes in the correct triangle mode order . This is necessary when indices are not available and the winding order is
257
- * clock-wise during export to glTF
258
- * @param submesh BabylonJS submesh
259
- * @param primitiveMode Primitive mode of the mesh
260
- * @param sideOrientation the winding order of the submesh
261
- * @param vertexBufferKind The type of vertex attribute
262
- * @param meshAttributeArray The vertex attribute data
263
- * @param byteOffset The offset to the binary data
264
- * @param binaryWriter The binary data for the glTF file
265
- * @param convertToRightHandedSystem Converts the values to right-handed
266
- */
267
- _Exporter.prototype.reorderTriangleFillMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem) {
268
- var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
269
- if (vertexBuffer) {
270
- var stride = vertexBuffer.byteStride / VertexBuffer.GetTypeByteLength(vertexBuffer.type);
271
- if (submesh.verticesCount % 3 !== 0) {
272
- Tools.Error('The submesh vertices for the triangle fill mode is not divisible by 3!');
273
- }
274
- else {
275
- var vertexData = [];
276
- var index = 0;
277
- switch (vertexBufferKind) {
278
- case VertexBuffer.PositionKind:
279
- case VertexBuffer.NormalKind: {
280
- for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
281
- index = x * stride;
282
- vertexData.push(Vector3.FromArray(meshAttributeArray, index));
283
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + 2 * stride));
284
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + stride));
285
- }
286
- break;
287
- }
288
- case VertexBuffer.TangentKind: {
289
- for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
290
- index = x * stride;
291
- vertexData.push(Vector4.FromArray(meshAttributeArray, index));
292
- vertexData.push(Vector4.FromArray(meshAttributeArray, index + 2 * stride));
293
- vertexData.push(Vector4.FromArray(meshAttributeArray, index + stride));
294
- }
295
- break;
296
- }
297
- case VertexBuffer.ColorKind: {
298
- var size = vertexBuffer.getSize();
299
- for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + size) {
300
- index = x * stride;
301
- if (size === 4) {
302
- vertexData.push(Vector4.FromArray(meshAttributeArray, index));
303
- vertexData.push(Vector4.FromArray(meshAttributeArray, index + 2 * stride));
304
- vertexData.push(Vector4.FromArray(meshAttributeArray, index + stride));
305
- }
306
- else {
307
- vertexData.push(Vector3.FromArray(meshAttributeArray, index));
308
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + 2 * stride));
309
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + stride));
310
- }
311
- }
312
- break;
313
- }
314
- case VertexBuffer.UVKind:
315
- case VertexBuffer.UV2Kind: {
316
- for (var x = submesh.verticesStart; x < submesh.verticesStart + submesh.verticesCount; x = x + 3) {
317
- index = x * stride;
318
- vertexData.push(Vector2.FromArray(meshAttributeArray, index));
319
- vertexData.push(Vector2.FromArray(meshAttributeArray, index + 2 * stride));
320
- vertexData.push(Vector2.FromArray(meshAttributeArray, index + stride));
321
- }
322
- break;
323
- }
324
- default: {
325
- Tools.Error("Unsupported Vertex Buffer type: ".concat(vertexBufferKind));
326
- }
327
- }
328
- this.writeVertexAttributeData(vertexData, byteOffset, vertexBufferKind, meshAttributeArray, binaryWriter, convertToRightHandedSystem);
329
- }
330
- }
331
- else {
332
- Tools.Warn("reorderTriangleFillMode: Vertex Buffer Kind ".concat(vertexBufferKind, " not present!"));
333
- }
334
- };
335
- /**
336
- * Reorders the vertex attributes in the correct triangle strip order. This is necessary when indices are not available and the winding order is
337
- * clock-wise during export to glTF
338
- * @param submesh BabylonJS submesh
339
- * @param primitiveMode Primitive mode of the mesh
340
- * @param sideOrientation the winding order of the submesh
341
- * @param vertexBufferKind The type of vertex attribute
342
- * @param meshAttributeArray The vertex attribute data
343
- * @param byteOffset The offset to the binary data
344
- * @param binaryWriter The binary data for the glTF file
345
- * @param convertToRightHandedSystem Converts the values to right-handed
346
- */
347
- _Exporter.prototype.reorderTriangleStripDrawMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem) {
348
- var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
349
- if (vertexBuffer) {
350
- var stride = vertexBuffer.byteStride / VertexBuffer.GetTypeByteLength(vertexBuffer.type);
351
- var vertexData = [];
352
- var index = 0;
353
- switch (vertexBufferKind) {
354
- case VertexBuffer.PositionKind:
355
- case VertexBuffer.NormalKind: {
356
- index = submesh.verticesStart;
357
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + 2 * stride));
358
- vertexData.push(Vector3.FromArray(meshAttributeArray, index + stride));
359
- break;
360
- }
361
- case VertexBuffer.TangentKind: {
362
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
363
- index = x * stride;
364
- vertexData.push(Vector4.FromArray(meshAttributeArray, index));
365
- }
366
- break;
367
- }
368
- case VertexBuffer.ColorKind: {
369
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
370
- index = x * stride;
371
- vertexBuffer.getSize() === 4 ? vertexData.push(Vector4.FromArray(meshAttributeArray, index)) : vertexData.push(Vector3.FromArray(meshAttributeArray, index));
372
- }
373
- break;
374
- }
375
- case VertexBuffer.UVKind:
376
- case VertexBuffer.UV2Kind: {
377
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
378
- index = x * stride;
379
- vertexData.push(Vector2.FromArray(meshAttributeArray, index));
380
- }
381
- break;
382
- }
383
- default: {
384
- Tools.Error("Unsupported Vertex Buffer type: ".concat(vertexBufferKind));
385
- }
386
- }
387
- this.writeVertexAttributeData(vertexData, byteOffset + 12, vertexBufferKind, meshAttributeArray, binaryWriter, convertToRightHandedSystem);
388
- }
389
- else {
390
- Tools.Warn("reorderTriangleStripDrawMode: Vertex buffer kind ".concat(vertexBufferKind, " not present!"));
391
- }
392
- };
393
- /**
394
- * Reorders the vertex attributes in the correct triangle fan order. This is necessary when indices are not available and the winding order is
395
- * clock-wise during export to glTF
396
- * @param submesh BabylonJS submesh
397
- * @param primitiveMode Primitive mode of the mesh
398
- * @param sideOrientation the winding order of the submesh
399
- * @param vertexBufferKind The type of vertex attribute
400
- * @param meshAttributeArray The vertex attribute data
401
- * @param byteOffset The offset to the binary data
402
- * @param binaryWriter The binary data for the glTF file
403
- * @param convertToRightHandedSystem Converts the values to right-handed
404
- */
405
- _Exporter.prototype.reorderTriangleFanMode = function (submesh, primitiveMode, sideOrientation, vertexBufferKind, meshAttributeArray, byteOffset, binaryWriter, convertToRightHandedSystem) {
406
- var vertexBuffer = this.getVertexBufferFromMesh(vertexBufferKind, submesh.getMesh());
407
- if (vertexBuffer) {
408
- var stride = vertexBuffer.byteStride / VertexBuffer.GetTypeByteLength(vertexBuffer.type);
409
- var vertexData = [];
410
- var index = 0;
411
- switch (vertexBufferKind) {
412
- case VertexBuffer.PositionKind:
413
- case VertexBuffer.NormalKind: {
414
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
415
- index = x * stride;
416
- vertexData.push(Vector3.FromArray(meshAttributeArray, index));
417
- }
418
- break;
419
- }
420
- case VertexBuffer.TangentKind: {
421
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
422
- index = x * stride;
423
- vertexData.push(Vector4.FromArray(meshAttributeArray, index));
424
- }
425
- break;
426
- }
427
- case VertexBuffer.ColorKind: {
428
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
429
- index = x * stride;
430
- vertexData.push(Vector4.FromArray(meshAttributeArray, index));
431
- vertexBuffer.getSize() === 4 ? vertexData.push(Vector4.FromArray(meshAttributeArray, index)) : vertexData.push(Vector3.FromArray(meshAttributeArray, index));
432
- }
433
- break;
434
- }
435
- case VertexBuffer.UVKind:
436
- case VertexBuffer.UV2Kind: {
437
- for (var x = submesh.verticesStart + submesh.verticesCount - 1; x >= submesh.verticesStart; --x) {
438
- index = x * stride;
439
- vertexData.push(Vector2.FromArray(meshAttributeArray, index));
440
- }
441
- break;
442
- }
443
- default: {
444
- Tools.Error("Unsupported Vertex Buffer type: ".concat(vertexBufferKind));
445
- }
446
- }
447
- this.writeVertexAttributeData(vertexData, byteOffset, vertexBufferKind, meshAttributeArray, binaryWriter, convertToRightHandedSystem);
448
- }
449
- else {
450
- Tools.Warn("reorderTriangleFanMode: Vertex buffer kind ".concat(vertexBufferKind, " not present!"));
451
- }
452
- };
453
- /**
454
- * Writes the vertex attribute data to binary
455
- * @param vertices The vertices to write to the binary writer
456
- * @param byteOffset The offset into the binary writer to overwrite binary data
457
- * @param vertexAttributeKind The vertex attribute type
458
- * @param meshAttributeArray The vertex attribute data
459
- * @param binaryWriter The writer containing the binary data
460
- * @param convertToRightHandedSystem Converts the values to right-handed
461
- */
462
- _Exporter.prototype.writeVertexAttributeData = function (vertices, byteOffset, vertexAttributeKind, meshAttributeArray, binaryWriter, convertToRightHandedSystem) {
463
- for (var _i = 0, vertices_1 = vertices; _i < vertices_1.length; _i++) {
464
- var vertex = vertices_1[_i];
465
- if (convertToRightHandedSystem && !(vertexAttributeKind === VertexBuffer.ColorKind) && !(vertex instanceof Vector2)) {
466
- if (vertex instanceof Vector3) {
467
- if (vertexAttributeKind === VertexBuffer.NormalKind) {
468
- _GLTFUtilities._GetRightHandedNormalVector3FromRef(vertex);
469
- }
470
- else if (vertexAttributeKind === VertexBuffer.PositionKind) {
471
- _GLTFUtilities._GetRightHandedPositionVector3FromRef(vertex);
472
- }
473
- else {
474
- Tools.Error('Unsupported vertex attribute kind!');
475
- }
476
- }
477
- else {
478
- _GLTFUtilities._GetRightHandedVector4FromRef(vertex);
479
- }
480
- }
481
- if (vertexAttributeKind === VertexBuffer.NormalKind) {
482
- vertex.normalize();
483
- }
484
- else if (vertexAttributeKind === VertexBuffer.TangentKind && vertex instanceof Vector4) {
485
- _GLTFUtilities._NormalizeTangentFromRef(vertex);
486
- }
487
- for (var _a = 0, _b = vertex.asArray(); _a < _b.length; _a++) {
488
- var component = _b[_a];
489
- binaryWriter.setFloat32(component, byteOffset);
490
- byteOffset += 4;
491
- }
492
- }
493
- };
494
- /**
495
- * Writes mesh attribute data to a data buffer
496
- * Returns the bytelength of the data
497
- * @param vertexBufferKind Indicates what kind of vertex data is being passed in
498
- * @param meshAttributeArray Array containing the attribute data
499
- * @param byteStride Specifies the space between data
500
- * @param binaryWriter The buffer to write the binary data to
501
- * @param convertToRightHandedSystem Converts the values to right-handed
502
- */
503
- _Exporter.prototype.writeAttributeData = function (vertexBufferKind, attributeComponentKind, meshAttributeArray, stride, binaryWriter, convertToRightHandedSystem, babylonTransformNode) {
504
- var vertexAttributes = [];
505
- var index;
506
- switch (vertexBufferKind) {
507
- case VertexBuffer.PositionKind: {
508
- for (var k = 0, length_2 = meshAttributeArray.length / stride; k < length_2; ++k) {
509
- index = k * stride;
510
- var vertexData = Vector3.FromArray(meshAttributeArray, index);
511
- if (convertToRightHandedSystem) {
512
- _GLTFUtilities._GetRightHandedPositionVector3FromRef(vertexData);
513
- }
514
- vertexAttributes.push(vertexData.asArray());
515
- }
516
- break;
517
- }
518
- case VertexBuffer.NormalKind: {
519
- for (var k = 0, length_3 = meshAttributeArray.length / stride; k < length_3; ++k) {
520
- index = k * stride;
521
- var vertexData = Vector3.FromArray(meshAttributeArray, index);
522
- if (convertToRightHandedSystem) {
523
- _GLTFUtilities._GetRightHandedNormalVector3FromRef(vertexData);
524
- }
525
- vertexData.normalize();
526
- vertexAttributes.push(vertexData.asArray());
527
- }
528
- break;
529
- }
530
- case VertexBuffer.TangentKind: {
531
- for (var k = 0, length_4 = meshAttributeArray.length / stride; k < length_4; ++k) {
532
- index = k * stride;
533
- var vertexData = Vector4.FromArray(meshAttributeArray, index);
534
- if (convertToRightHandedSystem) {
535
- _GLTFUtilities._GetRightHandedVector4FromRef(vertexData);
536
- }
537
- _GLTFUtilities._NormalizeTangentFromRef(vertexData);
538
- vertexAttributes.push(vertexData.asArray());
539
- }
540
- break;
541
- }
542
- case VertexBuffer.ColorKind: {
543
- var meshMaterial = babylonTransformNode.material;
544
- var convertToLinear = meshMaterial ? (meshMaterial.getClassName() === "StandardMaterial") : true;
545
- var vertexData = stride === 3 ? new Color3() : new Color4();
546
- for (var k = 0, length_5 = meshAttributeArray.length / stride; k < length_5; ++k) {
547
- index = k * stride;
548
- if (stride === 3) {
549
- Color3.FromArrayToRef(meshAttributeArray, index, vertexData);
550
- if (convertToLinear) {
551
- vertexData.toLinearSpaceToRef(vertexData);
552
- }
553
- }
554
- else {
555
- Color4.FromArrayToRef(meshAttributeArray, index, vertexData);
556
- if (convertToLinear) {
557
- vertexData.toLinearSpaceToRef(vertexData);
558
- }
559
- }
560
- vertexAttributes.push(vertexData.asArray());
561
- }
562
- break;
563
- }
564
- case VertexBuffer.UVKind:
565
- case VertexBuffer.UV2Kind: {
566
- for (var k = 0, length_6 = meshAttributeArray.length / stride; k < length_6; ++k) {
567
- index = k * stride;
568
- vertexAttributes.push(convertToRightHandedSystem ? [meshAttributeArray[index], meshAttributeArray[index + 1]] : [meshAttributeArray[index], meshAttributeArray[index + 1]]);
569
- }
570
- break;
571
- }
572
- case VertexBuffer.MatricesIndicesKind:
573
- case VertexBuffer.MatricesIndicesExtraKind: {
574
- for (var k = 0, length_7 = meshAttributeArray.length / stride; k < length_7; ++k) {
575
- index = k * stride;
576
- var vertexData = Vector4.FromArray(meshAttributeArray, index);
577
- vertexAttributes.push(vertexData.asArray());
578
- }
579
- break;
580
- }
581
- case VertexBuffer.MatricesWeightsKind:
582
- case VertexBuffer.MatricesWeightsExtraKind: {
583
- for (var k = 0, length_8 = meshAttributeArray.length / stride; k < length_8; ++k) {
584
- index = k * stride;
585
- var vertexData = Vector4.FromArray(meshAttributeArray, index);
586
- vertexAttributes.push(vertexData.asArray());
587
- }
588
- break;
589
- }
590
- default: {
591
- Tools.Warn("Unsupported Vertex Buffer Type: " + vertexBufferKind);
592
- vertexAttributes = [];
593
- }
594
- }
595
- var writeBinaryFunc;
596
- switch (attributeComponentKind) {
597
- case 5121 /* UNSIGNED_BYTE */: {
598
- writeBinaryFunc = binaryWriter.setUInt8.bind(binaryWriter);
599
- break;
600
- }
601
- case 5123 /* UNSIGNED_SHORT */: {
602
- writeBinaryFunc = binaryWriter.setUInt16.bind(binaryWriter);
603
- break;
604
- }
605
- case 5125 /* UNSIGNED_INT */: {
606
- writeBinaryFunc = binaryWriter.setUInt32.bind(binaryWriter);
607
- }
608
- case 5126 /* FLOAT */: {
609
- writeBinaryFunc = binaryWriter.setFloat32.bind(binaryWriter);
610
- break;
611
- }
612
- default: {
613
- Tools.Warn("Unsupported Attribute Component kind: " + attributeComponentKind);
614
- return;
615
- }
616
- }
617
- for (var _i = 0, vertexAttributes_1 = vertexAttributes; _i < vertexAttributes_1.length; _i++) {
618
- var vertexAttribute = vertexAttributes_1[_i];
619
- for (var _a = 0, vertexAttribute_1 = vertexAttribute; _a < vertexAttribute_1.length; _a++) {
620
- var component = vertexAttribute_1[_a];
621
- writeBinaryFunc(component);
622
- }
623
- }
624
- };
625
- /**
626
- * Writes mesh attribute data to a data buffer
627
- * Returns the bytelength of the data
628
- * @param vertexBufferKind Indicates what kind of vertex data is being passed in
629
- * @param meshAttributeArray Array containing the attribute data
630
- * @param byteStride Specifies the space between data
631
- * @param binaryWriter The buffer to write the binary data to
632
- * @param convertToRightHandedSystem Converts the values to right-handed
633
- */
634
- _Exporter.prototype.writeMorphTargetAttributeData = function (vertexBufferKind, attributeComponentKind, meshPrimitive, morphTarget, meshAttributeArray, morphTargetAttributeArray, stride, binaryWriter, convertToRightHandedSystem, minMax) {
635
- var vertexAttributes = [];
636
- var index;
637
- var difference = new Vector3();
638
- var difference4 = new Vector4(0, 0, 0, 0);
639
- switch (vertexBufferKind) {
640
- case VertexBuffer.PositionKind: {
641
- for (var k = meshPrimitive.verticesStart; k < meshPrimitive.verticesCount; ++k) {
642
- index = meshPrimitive.indexStart + k * stride;
643
- var vertexData = Vector3.FromArray(meshAttributeArray, index);
644
- var morphData = Vector3.FromArray(morphTargetAttributeArray, index);
645
- difference = morphData.subtractToRef(vertexData, difference);
646
- if (convertToRightHandedSystem) {
647
- _GLTFUtilities._GetRightHandedPositionVector3FromRef(difference);
648
- }
649
- if (minMax) {
650
- minMax.min.copyFromFloats(Math.min(difference.x, minMax.min.x), Math.min(difference.y, minMax.min.y), Math.min(difference.z, minMax.min.z));
651
- minMax.max.copyFromFloats(Math.max(difference.x, minMax.max.x), Math.max(difference.y, minMax.max.y), Math.max(difference.z, minMax.max.z));
652
- }
653
- vertexAttributes.push(difference.asArray());
654
- }
655
- break;
656
- }
657
- case VertexBuffer.NormalKind: {
658
- for (var k = meshPrimitive.verticesStart; k < meshPrimitive.verticesCount; ++k) {
659
- index = meshPrimitive.indexStart + k * stride;
660
- var vertexData = Vector3.FromArray(meshAttributeArray, index);
661
- vertexData.normalize();
662
- var morphData = Vector3.FromArray(morphTargetAttributeArray, index);
663
- morphData.normalize();
664
- difference = morphData.subtractToRef(vertexData, difference);
665
- if (convertToRightHandedSystem) {
666
- _GLTFUtilities._GetRightHandedNormalVector3FromRef(difference);
667
- }
668
- vertexAttributes.push(difference.asArray());
669
- }
670
- break;
671
- }
672
- case VertexBuffer.TangentKind: {
673
- for (var k = meshPrimitive.verticesStart; k < meshPrimitive.verticesCount; ++k) {
674
- index = meshPrimitive.indexStart + k * (stride + 1);
675
- var vertexData = Vector4.FromArray(meshAttributeArray, index);
676
- _GLTFUtilities._NormalizeTangentFromRef(vertexData);
677
- var morphData = Vector4.FromArray(morphTargetAttributeArray, index);
678
- _GLTFUtilities._NormalizeTangentFromRef(morphData);
679
- difference4 = morphData.subtractToRef(vertexData, difference4);
680
- if (convertToRightHandedSystem) {
681
- _GLTFUtilities._GetRightHandedVector4FromRef(difference4);
682
- }
683
- vertexAttributes.push([difference4.x, difference4.y, difference4.z]);
684
- }
685
- break;
686
- }
687
- default: {
688
- Tools.Warn("Unsupported Vertex Buffer Type: " + vertexBufferKind);
689
- vertexAttributes = [];
690
- }
691
- }
692
- var writeBinaryFunc;
693
- switch (attributeComponentKind) {
694
- case 5121 /* UNSIGNED_BYTE */: {
695
- writeBinaryFunc = binaryWriter.setUInt8.bind(binaryWriter);
696
- break;
697
- }
698
- case 5123 /* UNSIGNED_SHORT */: {
699
- writeBinaryFunc = binaryWriter.setUInt16.bind(binaryWriter);
700
- break;
701
- }
702
- case 5125 /* UNSIGNED_INT */: {
703
- writeBinaryFunc = binaryWriter.setUInt32.bind(binaryWriter);
704
- }
705
- case 5126 /* FLOAT */: {
706
- writeBinaryFunc = binaryWriter.setFloat32.bind(binaryWriter);
707
- break;
708
- }
709
- default: {
710
- Tools.Warn("Unsupported Attribute Component kind: " + attributeComponentKind);
711
- return;
712
- }
713
- }
714
- for (var _i = 0, vertexAttributes_2 = vertexAttributes; _i < vertexAttributes_2.length; _i++) {
715
- var vertexAttribute = vertexAttributes_2[_i];
716
- for (var _a = 0, vertexAttribute_2 = vertexAttribute; _a < vertexAttribute_2.length; _a++) {
717
- var component = vertexAttribute_2[_a];
718
- writeBinaryFunc(component);
719
- }
720
- }
721
- };
722
- /**
723
- * Generates glTF json data
724
- * @param shouldUseGlb Indicates whether the json should be written for a glb file
725
- * @param glTFPrefix Text to use when prefixing a glTF file
726
- * @param prettyPrint Indicates whether the json file should be pretty printed (true) or not (false)
727
- * @returns json data as string
728
- */
729
- _Exporter.prototype.generateJSON = function (shouldUseGlb, glTFPrefix, prettyPrint) {
730
- var _this = this;
731
- var buffer = { byteLength: this._totalByteLength };
732
- var imageName;
733
- var imageData;
734
- var bufferView;
735
- var byteOffset = this._totalByteLength;
736
- if (buffer.byteLength) {
737
- this._glTF.buffers = [buffer];
738
- }
739
- if (this._nodes && this._nodes.length) {
740
- this._glTF.nodes = this._nodes;
741
- }
742
- if (this._meshes && this._meshes.length) {
743
- this._glTF.meshes = this._meshes;
744
- }
745
- if (this._scenes && this._scenes.length) {
746
- this._glTF.scenes = this._scenes;
747
- this._glTF.scene = 0;
748
- }
749
- if (this._cameras && this._cameras.length) {
750
- this._glTF.cameras = this._cameras;
751
- }
752
- if (this._bufferViews && this._bufferViews.length) {
753
- this._glTF.bufferViews = this._bufferViews;
754
- }
755
- if (this._accessors && this._accessors.length) {
756
- this._glTF.accessors = this._accessors;
757
- }
758
- if (this._animations && this._animations.length) {
759
- this._glTF.animations = this._animations;
760
- }
761
- if (this._materials && this._materials.length) {
762
- this._glTF.materials = this._materials;
763
- }
764
- if (this._textures && this._textures.length) {
765
- this._glTF.textures = this._textures;
766
- }
767
- if (this._samplers && this._samplers.length) {
768
- this._glTF.samplers = this._samplers;
769
- }
770
- if (this._skins && this._skins.length) {
771
- this._glTF.skins = this._skins;
772
- }
773
- if (this._images && this._images.length) {
774
- if (!shouldUseGlb) {
775
- this._glTF.images = this._images;
776
- }
777
- else {
778
- this._glTF.images = [];
779
- this._images.forEach(function (image) {
780
- if (image.uri) {
781
- imageData = _this._imageData[image.uri];
782
- _this._orderedImageData.push(imageData);
783
- imageName = image.uri.split('.')[0] + " image";
784
- bufferView = _GLTFUtilities._CreateBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
785
- byteOffset += imageData.data.buffer.byteLength;
786
- _this._bufferViews.push(bufferView);
787
- image.bufferView = _this._bufferViews.length - 1;
788
- image.name = imageName;
789
- image.mimeType = imageData.mimeType;
790
- image.uri = undefined;
791
- if (!_this._glTF.images) {
792
- _this._glTF.images = [];
793
- }
794
- _this._glTF.images.push(image);
795
- }
796
- });
797
- // Replace uri with bufferview and mime type for glb
798
- buffer.byteLength = byteOffset;
799
- }
800
- }
801
- if (!shouldUseGlb) {
802
- buffer.uri = glTFPrefix + ".bin";
803
- }
804
- var jsonText = prettyPrint ? JSON.stringify(this._glTF, null, 2) : JSON.stringify(this._glTF);
805
- return jsonText;
806
- };
807
- /**
808
- * Generates data for .gltf and .bin files based on the glTF prefix string
809
- * @param glTFPrefix Text to use when prefixing a glTF file
810
- * @param dispose Dispose the exporter
811
- * @returns GLTFData with glTF file data
812
- */
813
- _Exporter.prototype._generateGLTFAsync = function (glTFPrefix, dispose) {
814
- var _this = this;
815
- if (dispose === void 0) { dispose = true; }
816
- return this._generateBinaryAsync().then(function (binaryBuffer) {
817
- _this._extensionsOnExporting();
818
- var jsonText = _this.generateJSON(false, glTFPrefix, true);
819
- var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
820
- var glTFFileName = glTFPrefix + '.gltf';
821
- var glTFBinFile = glTFPrefix + '.bin';
822
- var container = new GLTFData();
823
- container.glTFFiles[glTFFileName] = jsonText;
824
- container.glTFFiles[glTFBinFile] = bin;
825
- if (_this._imageData) {
826
- for (var image in _this._imageData) {
827
- container.glTFFiles[image] = new Blob([_this._imageData[image].data], { type: _this._imageData[image].mimeType });
828
- }
829
- }
830
- if (dispose) {
831
- _this.dispose();
832
- }
833
- return container;
834
- });
835
- };
836
- /**
837
- * Creates a binary buffer for glTF
838
- * @returns array buffer for binary data
839
- */
840
- _Exporter.prototype._generateBinaryAsync = function () {
841
- var _this = this;
842
- var binaryWriter = new _BinaryWriter(4);
843
- return this.createSceneAsync(this._babylonScene, binaryWriter).then(function () {
844
- if (_this._localEngine) {
845
- _this._localEngine.dispose();
846
- }
847
- return binaryWriter.getArrayBuffer();
848
- });
849
- };
850
- /**
851
- * Pads the number to a multiple of 4
852
- * @param num number to pad
853
- * @returns padded number
854
- */
855
- _Exporter.prototype._getPadding = function (num) {
856
- var remainder = num % 4;
857
- var padding = remainder === 0 ? remainder : 4 - remainder;
858
- return padding;
859
- };
860
- /**
861
- * @hidden
862
- */
863
- _Exporter.prototype._generateGLBAsync = function (glTFPrefix, dispose) {
864
- var _this = this;
865
- if (dispose === void 0) { dispose = true; }
866
- return this._generateBinaryAsync().then(function (binaryBuffer) {
867
- _this._extensionsOnExporting();
868
- var jsonText = _this.generateJSON(true);
869
- var glbFileName = glTFPrefix + '.glb';
870
- var headerLength = 12;
871
- var chunkLengthPrefix = 8;
872
- var jsonLength = jsonText.length;
873
- var encodedJsonText;
874
- var imageByteLength = 0;
875
- // make use of TextEncoder when available
876
- if (typeof TextEncoder !== "undefined") {
877
- var encoder = new TextEncoder();
878
- encodedJsonText = encoder.encode(jsonText);
879
- jsonLength = encodedJsonText.length;
880
- }
881
- for (var i = 0; i < _this._orderedImageData.length; ++i) {
882
- imageByteLength += _this._orderedImageData[i].data.byteLength;
883
- }
884
- var jsonPadding = _this._getPadding(jsonLength);
885
- var binPadding = _this._getPadding(binaryBuffer.byteLength);
886
- var imagePadding = _this._getPadding(imageByteLength);
887
- var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding + imageByteLength + imagePadding;
888
- //header
889
- var headerBuffer = new ArrayBuffer(headerLength);
890
- var headerBufferView = new DataView(headerBuffer);
891
- headerBufferView.setUint32(0, 0x46546C67, true); //glTF
892
- headerBufferView.setUint32(4, 2, true); // version
893
- headerBufferView.setUint32(8, byteLength, true); // total bytes in file
894
- //json chunk
895
- var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
896
- var jsonChunkBufferView = new DataView(jsonChunkBuffer);
897
- jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
898
- jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
899
- //json chunk bytes
900
- var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
901
- // if TextEncoder was available, we can simply copy the encoded array
902
- if (encodedJsonText) {
903
- jsonData.set(encodedJsonText);
904
- }
905
- else {
906
- var blankCharCode = "_".charCodeAt(0);
907
- for (var i = 0; i < jsonLength; ++i) {
908
- var charCode = jsonText.charCodeAt(i);
909
- // if the character doesn't fit into a single UTF-16 code unit, just put a blank character
910
- if (charCode != jsonText.codePointAt(i)) {
911
- jsonData[i] = blankCharCode;
912
- }
913
- else {
914
- jsonData[i] = charCode;
915
- }
916
- }
917
- }
918
- //json padding
919
- var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
920
- for (var i = 0; i < jsonPadding; ++i) {
921
- jsonPaddingView[i] = 0x20;
922
- }
923
- //binary chunk
924
- var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
925
- var binaryChunkBufferView = new DataView(binaryChunkBuffer);
926
- binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength + imageByteLength + imagePadding, true);
927
- binaryChunkBufferView.setUint32(4, 0x004E4942, true);
928
- // binary padding
929
- var binPaddingBuffer = new ArrayBuffer(binPadding);
930
- var binPaddingView = new Uint8Array(binPaddingBuffer);
931
- for (var i = 0; i < binPadding; ++i) {
932
- binPaddingView[i] = 0;
933
- }
934
- var imagePaddingBuffer = new ArrayBuffer(imagePadding);
935
- var imagePaddingView = new Uint8Array(imagePaddingBuffer);
936
- for (var i = 0; i < imagePadding; ++i) {
937
- imagePaddingView[i] = 0;
938
- }
939
- var glbData = [headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer];
940
- // binary data
941
- for (var i = 0; i < _this._orderedImageData.length; ++i) {
942
- glbData.push(_this._orderedImageData[i].data.buffer);
943
- }
944
- glbData.push(binPaddingBuffer);
945
- glbData.push(imagePaddingBuffer);
946
- var glbFile = new Blob(glbData, { type: 'application/octet-stream' });
947
- var container = new GLTFData();
948
- container.glTFFiles[glbFileName] = glbFile;
949
- if (_this._localEngine != null) {
950
- _this._localEngine.dispose();
951
- }
952
- if (dispose) {
953
- _this.dispose();
954
- }
955
- return container;
956
- });
957
- };
958
- /**
959
- * Sets the TRS for each node
960
- * @param node glTF Node for storing the transformation data
961
- * @param babylonTransformNode Babylon mesh used as the source for the transformation data
962
- * @param convertToRightHandedSystem Converts the values to right-handed
963
- */
964
- _Exporter.prototype.setNodeTransformation = function (node, babylonTransformNode, convertToRightHandedSystem) {
965
- if (!babylonTransformNode.getPivotPoint().equalsToFloats(0, 0, 0)) {
966
- Tools.Warn("Pivot points are not supported in the glTF serializer");
967
- }
968
- if (!babylonTransformNode.position.equalsToFloats(0, 0, 0)) {
969
- node.translation = convertToRightHandedSystem ? _GLTFUtilities._GetRightHandedPositionVector3(babylonTransformNode.position).asArray() : babylonTransformNode.position.asArray();
970
- }
971
- if (!babylonTransformNode.scaling.equalsToFloats(1, 1, 1)) {
972
- node.scale = babylonTransformNode.scaling.asArray();
973
- }
974
- var rotationQuaternion = Quaternion.RotationYawPitchRoll(babylonTransformNode.rotation.y, babylonTransformNode.rotation.x, babylonTransformNode.rotation.z);
975
- if (babylonTransformNode.rotationQuaternion) {
976
- rotationQuaternion.multiplyInPlace(babylonTransformNode.rotationQuaternion);
977
- }
978
- if (!Quaternion.IsIdentity(rotationQuaternion)) {
979
- if (convertToRightHandedSystem) {
980
- _GLTFUtilities._GetRightHandedQuaternionFromRef(rotationQuaternion);
981
- }
982
- node.rotation = rotationQuaternion.normalize().asArray();
983
- }
984
- };
985
- _Exporter.prototype.setCameraTransformation = function (node, babylonCamera, convertToRightHandedSystem) {
986
- if (!babylonCamera.position.equalsToFloats(0, 0, 0)) {
987
- node.translation = convertToRightHandedSystem ? _GLTFUtilities._GetRightHandedPositionVector3(babylonCamera.position).asArray() : babylonCamera.position.asArray();
988
- }
989
- var rotationQuaternion = babylonCamera.absoluteRotation;
990
- if (!Quaternion.IsIdentity(rotationQuaternion)) {
991
- if (convertToRightHandedSystem) {
992
- _GLTFUtilities._GetRightHandedQuaternionFromRef(rotationQuaternion);
993
- }
994
- node.rotation = rotationQuaternion.normalize().asArray();
995
- }
996
- };
997
- _Exporter.prototype.getVertexBufferFromMesh = function (attributeKind, bufferMesh) {
998
- if (bufferMesh.isVerticesDataPresent(attributeKind)) {
999
- var vertexBuffer = bufferMesh.getVertexBuffer(attributeKind);
1000
- if (vertexBuffer) {
1001
- return vertexBuffer;
1002
- }
1003
- }
1004
- return null;
1005
- };
1006
- /**
1007
- * Creates a bufferview based on the vertices type for the Babylon mesh
1008
- * @param kind Indicates the type of vertices data
1009
- * @param componentType Indicates the numerical type used to store the data
1010
- * @param babylonTransformNode The Babylon mesh to get the vertices data from
1011
- * @param binaryWriter The buffer to write the bufferview data to
1012
- * @param convertToRightHandedSystem Converts the values to right-handed
1013
- */
1014
- _Exporter.prototype.createBufferViewKind = function (kind, attributeComponentKind, babylonTransformNode, binaryWriter, byteStride, convertToRightHandedSystem) {
1015
- var bufferMesh = babylonTransformNode instanceof Mesh ?
1016
- babylonTransformNode : babylonTransformNode instanceof InstancedMesh ?
1017
- babylonTransformNode.sourceMesh : null;
1018
- if (bufferMesh) {
1019
- var vertexBuffer = bufferMesh.getVertexBuffer(kind);
1020
- var vertexData = bufferMesh.getVerticesData(kind);
1021
- if (vertexBuffer && vertexData) {
1022
- var typeByteLength = VertexBuffer.GetTypeByteLength(attributeComponentKind);
1023
- var byteLength = vertexData.length * typeByteLength;
1024
- var bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, kind + " - " + bufferMesh.name);
1025
- this._bufferViews.push(bufferView);
1026
- this.writeAttributeData(kind, attributeComponentKind, vertexData, byteStride / typeByteLength, binaryWriter, convertToRightHandedSystem, babylonTransformNode);
1027
- }
1028
- }
1029
- };
1030
- /**
1031
- * Creates a bufferview based on the vertices type for the Babylon mesh
1032
- * @param babylonSubMesh The Babylon submesh that the morph target is applied to
1033
- * @param babylonMorphTarget the morph target to be exported
1034
- * @param binaryWriter The buffer to write the bufferview data to
1035
- * @param convertToRightHandedSystem Converts the values to right-handed
1036
- */
1037
- _Exporter.prototype.setMorphTargetAttributes = function (babylonSubMesh, meshPrimitive, babylonMorphTarget, binaryWriter, convertToRightHandedSystem) {
1038
- if (babylonMorphTarget) {
1039
- if (!meshPrimitive.targets) {
1040
- meshPrimitive.targets = [];
1041
- }
1042
- var target = {};
1043
- if (babylonMorphTarget.hasNormals) {
1044
- var vertexNormals = babylonSubMesh.getMesh().getVerticesData(VertexBuffer.NormalKind);
1045
- var morphNormals = babylonMorphTarget.getNormals();
1046
- var count = babylonSubMesh.verticesCount;
1047
- var byteStride = 12; // 3 x 4 byte floats
1048
- var byteLength = count * byteStride;
1049
- var bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, babylonMorphTarget.name + "_NORMAL");
1050
- this._bufferViews.push(bufferView);
1051
- var bufferViewIndex = this._bufferViews.length - 1;
1052
- var accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, babylonMorphTarget.name + " - " + "NORMAL", "VEC3" /* VEC3 */, 5126 /* FLOAT */, count, 0, null, null);
1053
- this._accessors.push(accessor);
1054
- target.NORMAL = this._accessors.length - 1;
1055
- this.writeMorphTargetAttributeData(VertexBuffer.NormalKind, 5126 /* FLOAT */, babylonSubMesh, babylonMorphTarget, vertexNormals, morphNormals, byteStride / 4, binaryWriter, convertToRightHandedSystem);
1056
- }
1057
- if (babylonMorphTarget.hasPositions) {
1058
- var vertexPositions = babylonSubMesh.getMesh().getVerticesData(VertexBuffer.PositionKind);
1059
- var morphPositions = babylonMorphTarget.getPositions();
1060
- var count = babylonSubMesh.verticesCount;
1061
- var byteStride = 12; // 3 x 4 byte floats
1062
- var byteLength = count * byteStride;
1063
- var bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, babylonMorphTarget.name + "_POSITION");
1064
- this._bufferViews.push(bufferView);
1065
- var bufferViewIndex = this._bufferViews.length - 1;
1066
- var minMax = { min: new Vector3(Infinity, Infinity, Infinity), max: new Vector3(-Infinity, -Infinity, -Infinity) };
1067
- var accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, babylonMorphTarget.name + " - " + "POSITION", "VEC3" /* VEC3 */, 5126 /* FLOAT */, count, 0, null, null);
1068
- this._accessors.push(accessor);
1069
- target.POSITION = this._accessors.length - 1;
1070
- this.writeMorphTargetAttributeData(VertexBuffer.PositionKind, 5126 /* FLOAT */, babylonSubMesh, babylonMorphTarget, vertexPositions, morphPositions, byteStride / 4, binaryWriter, convertToRightHandedSystem, minMax);
1071
- accessor.min = minMax.min.asArray();
1072
- accessor.max = minMax.max.asArray();
1073
- }
1074
- if (babylonMorphTarget.hasTangents) {
1075
- var vertexTangents = babylonSubMesh.getMesh().getVerticesData(VertexBuffer.TangentKind);
1076
- var morphTangents = babylonMorphTarget.getTangents();
1077
- var count = babylonSubMesh.verticesCount;
1078
- var byteStride = 12; // 3 x 4 byte floats
1079
- var byteLength = count * byteStride;
1080
- var bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, byteStride, babylonMorphTarget.name + "_NORMAL");
1081
- this._bufferViews.push(bufferView);
1082
- var bufferViewIndex = this._bufferViews.length - 1;
1083
- var accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, babylonMorphTarget.name + " - " + "TANGENT", "VEC3" /* VEC3 */, 5126 /* FLOAT */, count, 0, null, null);
1084
- this._accessors.push(accessor);
1085
- target.TANGENT = this._accessors.length - 1;
1086
- this.writeMorphTargetAttributeData(VertexBuffer.TangentKind, 5126 /* FLOAT */, babylonSubMesh, babylonMorphTarget, vertexTangents, morphTangents, byteStride / 4, binaryWriter, convertToRightHandedSystem);
1087
- }
1088
- meshPrimitive.targets.push(target);
1089
- }
1090
- };
1091
- /**
1092
- * The primitive mode of the Babylon mesh
1093
- * @param babylonMesh The BabylonJS mesh
1094
- */
1095
- _Exporter.prototype.getMeshPrimitiveMode = function (babylonMesh) {
1096
- if (babylonMesh instanceof LinesMesh) {
1097
- return Material.LineListDrawMode;
1098
- }
1099
- return babylonMesh.material ? babylonMesh.material.fillMode : Material.TriangleFillMode;
1100
- };
1101
- /**
1102
- * Sets the primitive mode of the glTF mesh primitive
1103
- * @param meshPrimitive glTF mesh primitive
1104
- * @param primitiveMode The primitive mode
1105
- */
1106
- _Exporter.prototype.setPrimitiveMode = function (meshPrimitive, primitiveMode) {
1107
- switch (primitiveMode) {
1108
- case Material.TriangleFillMode: {
1109
- // glTF defaults to using Triangle Mode
1110
- break;
1111
- }
1112
- case Material.TriangleStripDrawMode: {
1113
- meshPrimitive.mode = 5 /* TRIANGLE_STRIP */;
1114
- break;
1115
- }
1116
- case Material.TriangleFanDrawMode: {
1117
- meshPrimitive.mode = 6 /* TRIANGLE_FAN */;
1118
- break;
1119
- }
1120
- case Material.PointListDrawMode: {
1121
- meshPrimitive.mode = 0 /* POINTS */;
1122
- }
1123
- case Material.PointFillMode: {
1124
- meshPrimitive.mode = 0 /* POINTS */;
1125
- break;
1126
- }
1127
- case Material.LineLoopDrawMode: {
1128
- meshPrimitive.mode = 2 /* LINE_LOOP */;
1129
- break;
1130
- }
1131
- case Material.LineListDrawMode: {
1132
- meshPrimitive.mode = 1 /* LINES */;
1133
- break;
1134
- }
1135
- case Material.LineStripDrawMode: {
1136
- meshPrimitive.mode = 3 /* LINE_STRIP */;
1137
- break;
1138
- }
1139
- }
1140
- };
1141
- /**
1142
- * Sets the vertex attribute accessor based of the glTF mesh primitive
1143
- * @param meshPrimitive glTF mesh primitive
1144
- * @param attributeKind vertex attribute
1145
- * @returns boolean specifying if uv coordinates are present
1146
- */
1147
- _Exporter.prototype.setAttributeKind = function (meshPrimitive, attributeKind) {
1148
- switch (attributeKind) {
1149
- case VertexBuffer.PositionKind: {
1150
- meshPrimitive.attributes.POSITION = this._accessors.length - 1;
1151
- break;
1152
- }
1153
- case VertexBuffer.NormalKind: {
1154
- meshPrimitive.attributes.NORMAL = this._accessors.length - 1;
1155
- break;
1156
- }
1157
- case VertexBuffer.ColorKind: {
1158
- meshPrimitive.attributes.COLOR_0 = this._accessors.length - 1;
1159
- break;
1160
- }
1161
- case VertexBuffer.TangentKind: {
1162
- meshPrimitive.attributes.TANGENT = this._accessors.length - 1;
1163
- break;
1164
- }
1165
- case VertexBuffer.UVKind: {
1166
- meshPrimitive.attributes.TEXCOORD_0 = this._accessors.length - 1;
1167
- break;
1168
- }
1169
- case VertexBuffer.UV2Kind: {
1170
- meshPrimitive.attributes.TEXCOORD_1 = this._accessors.length - 1;
1171
- break;
1172
- }
1173
- case VertexBuffer.MatricesIndicesKind: {
1174
- meshPrimitive.attributes.JOINTS_0 = this._accessors.length - 1;
1175
- break;
1176
- }
1177
- case VertexBuffer.MatricesIndicesExtraKind: {
1178
- meshPrimitive.attributes.JOINTS_1 = this._accessors.length - 1;
1179
- break;
1180
- }
1181
- case VertexBuffer.MatricesWeightsKind: {
1182
- meshPrimitive.attributes.WEIGHTS_0 = this._accessors.length - 1;
1183
- break;
1184
- }
1185
- case VertexBuffer.MatricesWeightsExtraKind: {
1186
- meshPrimitive.attributes.WEIGHTS_1 = this._accessors.length - 1;
1187
- break;
1188
- }
1189
- default: {
1190
- Tools.Warn("Unsupported Vertex Buffer Type: " + attributeKind);
1191
- }
1192
- }
1193
- };
1194
- /**
1195
- * Sets data for the primitive attributes of each submesh
1196
- * @param mesh glTF Mesh object to store the primitive attribute information
1197
- * @param babylonTransformNode Babylon mesh to get the primitive attribute data from
1198
- * @param binaryWriter Buffer to write the attribute data to
1199
- * @param convertToRightHandedSystem Converts the values to right-handed
1200
- */
1201
- _Exporter.prototype.setPrimitiveAttributesAsync = function (mesh, babylonTransformNode, binaryWriter, convertToRightHandedSystem) {
1202
- var _a;
1203
- var promises = [];
1204
- var bufferMesh = null;
1205
- var bufferView;
1206
- var minMax;
1207
- if (babylonTransformNode instanceof Mesh) {
1208
- bufferMesh = babylonTransformNode;
1209
- }
1210
- else if (babylonTransformNode instanceof InstancedMesh) {
1211
- bufferMesh = babylonTransformNode.sourceMesh;
1212
- }
1213
- var attributeData = [
1214
- { kind: VertexBuffer.PositionKind, accessorType: "VEC3" /* VEC3 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 12 },
1215
- { kind: VertexBuffer.NormalKind, accessorType: "VEC3" /* VEC3 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 12 },
1216
- { kind: VertexBuffer.ColorKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 16 },
1217
- { kind: VertexBuffer.TangentKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 16 },
1218
- { kind: VertexBuffer.UVKind, accessorType: "VEC2" /* VEC2 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 8 },
1219
- { kind: VertexBuffer.UV2Kind, accessorType: "VEC2" /* VEC2 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 8 },
1220
- { kind: VertexBuffer.MatricesIndicesKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5123 /* UNSIGNED_SHORT */, byteStride: 8 },
1221
- { kind: VertexBuffer.MatricesIndicesExtraKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5123 /* UNSIGNED_SHORT */, byteStride: 8 },
1222
- { kind: VertexBuffer.MatricesWeightsKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 16 },
1223
- { kind: VertexBuffer.MatricesWeightsExtraKind, accessorType: "VEC4" /* VEC4 */, accessorComponentType: 5126 /* FLOAT */, byteStride: 16 },
1224
- ];
1225
- if (bufferMesh) {
1226
- var indexBufferViewIndex = null;
1227
- var primitiveMode = this.getMeshPrimitiveMode(bufferMesh);
1228
- var vertexAttributeBufferViews = {};
1229
- var morphTargetManager = bufferMesh.morphTargetManager;
1230
- // For each BabylonMesh, create bufferviews for each 'kind'
1231
- for (var _i = 0, attributeData_1 = attributeData; _i < attributeData_1.length; _i++) {
1232
- var attribute = attributeData_1[_i];
1233
- var attributeKind = attribute.kind;
1234
- var attributeComponentKind = attribute.accessorComponentType;
1235
- if (bufferMesh.isVerticesDataPresent(attributeKind)) {
1236
- var vertexBuffer = this.getVertexBufferFromMesh(attributeKind, bufferMesh);
1237
- attribute.byteStride = vertexBuffer ? vertexBuffer.getSize() * VertexBuffer.GetTypeByteLength(attribute.accessorComponentType) : VertexBuffer.DeduceStride(attributeKind) * 4;
1238
- if (attribute.byteStride === 12) {
1239
- attribute.accessorType = "VEC3" /* VEC3 */;
1240
- }
1241
- this.createBufferViewKind(attributeKind, attributeComponentKind, babylonTransformNode, binaryWriter, attribute.byteStride, convertToRightHandedSystem);
1242
- attribute.bufferViewIndex = this._bufferViews.length - 1;
1243
- vertexAttributeBufferViews[attributeKind] = attribute.bufferViewIndex;
1244
- }
1245
- }
1246
- if (bufferMesh.getTotalIndices()) {
1247
- var indices = bufferMesh.getIndices();
1248
- if (indices) {
1249
- var byteLength = indices.length * 4;
1250
- bufferView = _GLTFUtilities._CreateBufferView(0, binaryWriter.getByteOffset(), byteLength, undefined, "Indices - " + bufferMesh.name);
1251
- this._bufferViews.push(bufferView);
1252
- indexBufferViewIndex = this._bufferViews.length - 1;
1253
- for (var k = 0, length_9 = indices.length; k < length_9; ++k) {
1254
- binaryWriter.setUInt32(indices[k]);
1255
- }
1256
- }
1257
- }
1258
- if (bufferMesh.subMeshes) {
1259
- // go through all mesh primitives (submeshes)
1260
- for (var _b = 0, _c = bufferMesh.subMeshes; _b < _c.length; _b++) {
1261
- var submesh = _c[_b];
1262
- var babylonMaterial = submesh.getMaterial() || bufferMesh.getScene().defaultMaterial;
1263
- var materialIndex = null;
1264
- if (babylonMaterial) {
1265
- if (bufferMesh instanceof LinesMesh) {
1266
- // get the color from the lines mesh and set it in the material
1267
- var material = {
1268
- name: bufferMesh.name + ' material'
1269
- };
1270
- if (!bufferMesh.color.equals(Color3.White()) || bufferMesh.alpha < 1) {
1271
- material.pbrMetallicRoughness = {
1272
- baseColorFactor: bufferMesh.color.asArray().concat([bufferMesh.alpha])
1273
- };
1274
- }
1275
- this._materials.push(material);
1276
- materialIndex = this._materials.length - 1;
1277
- }
1278
- else if (babylonMaterial.getClassName() === "MultiMaterial") {
1279
- var subMaterial = babylonMaterial.subMaterials[submesh.materialIndex];
1280
- if (subMaterial) {
1281
- babylonMaterial = subMaterial;
1282
- materialIndex = this._materialMap[babylonMaterial.uniqueId];
1283
- }
1284
- }
1285
- else {
1286
- materialIndex = this._materialMap[babylonMaterial.uniqueId];
1287
- }
1288
- }
1289
- var glTFMaterial = materialIndex != null ? this._materials[materialIndex] : null;
1290
- var meshPrimitive = { attributes: {} };
1291
- this.setPrimitiveMode(meshPrimitive, primitiveMode);
1292
- for (var _d = 0, attributeData_2 = attributeData; _d < attributeData_2.length; _d++) {
1293
- var attribute = attributeData_2[_d];
1294
- var attributeKind = attribute.kind;
1295
- if ((attributeKind === VertexBuffer.UVKind || attributeKind === VertexBuffer.UV2Kind) && !this._options.exportUnusedUVs) {
1296
- if (!glTFMaterial || !this._glTFMaterialExporter._hasTexturesPresent(glTFMaterial)) {
1297
- continue;
1298
- }
1299
- }
1300
- var vertexData = bufferMesh.getVerticesData(attributeKind);
1301
- if (vertexData) {
1302
- var vertexBuffer = this.getVertexBufferFromMesh(attributeKind, bufferMesh);
1303
- if (vertexBuffer) {
1304
- var stride = vertexBuffer.getSize();
1305
- var bufferViewIndex = attribute.bufferViewIndex;
1306
- if (bufferViewIndex != undefined) { // check to see if bufferviewindex has a numeric value assigned.
1307
- minMax = { min: null, max: null };
1308
- if (attributeKind == VertexBuffer.PositionKind) {
1309
- minMax = _GLTFUtilities._CalculateMinMaxPositions(vertexData, 0, vertexData.length / stride, convertToRightHandedSystem);
1310
- }
1311
- var accessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, attributeKind + " - " + babylonTransformNode.name, attribute.accessorType, attribute.accessorComponentType, vertexData.length / stride, 0, minMax.min, minMax.max);
1312
- this._accessors.push(accessor);
1313
- this.setAttributeKind(meshPrimitive, attributeKind);
1314
- }
1315
- }
1316
- }
1317
- }
1318
- if (indexBufferViewIndex) {
1319
- // Create accessor
1320
- var accessor = _GLTFUtilities._CreateAccessor(indexBufferViewIndex, "indices - " + babylonTransformNode.name, "SCALAR" /* SCALAR */, 5125 /* UNSIGNED_INT */, submesh.indexCount, submesh.indexStart * 4, null, null);
1321
- this._accessors.push(accessor);
1322
- meshPrimitive.indices = this._accessors.length - 1;
1323
- }
1324
- if (materialIndex != null && Object.keys(meshPrimitive.attributes).length > 0) {
1325
- var sideOrientation = bufferMesh.overrideMaterialSideOrientation !== null ? bufferMesh.overrideMaterialSideOrientation : babylonMaterial.sideOrientation;
1326
- if ((sideOrientation == Material.ClockWiseSideOrientation && this._babylonScene.useRightHandedSystem)
1327
- || (sideOrientation == Material.ClockWiseSideOrientation && convertToRightHandedSystem && bufferMesh.overrideMaterialSideOrientation !== ((_a = bufferMesh.material) === null || _a === void 0 ? void 0 : _a.sideOrientation))) {
1328
- var byteOffset = indexBufferViewIndex != null ? this._bufferViews[indexBufferViewIndex].byteOffset : null;
1329
- if (byteOffset == null) {
1330
- byteOffset = 0;
1331
- }
1332
- var babylonIndices = null;
1333
- if (indexBufferViewIndex != null) {
1334
- babylonIndices = bufferMesh.getIndices();
1335
- }
1336
- if (babylonIndices) {
1337
- this.reorderIndicesBasedOnPrimitiveMode(submesh, primitiveMode, babylonIndices, byteOffset, binaryWriter);
1338
- }
1339
- else {
1340
- for (var _e = 0, attributeData_3 = attributeData; _e < attributeData_3.length; _e++) {
1341
- var attribute = attributeData_3[_e];
1342
- var vertexData = bufferMesh.getVerticesData(attribute.kind);
1343
- if (vertexData) {
1344
- var byteOffset_1 = this._bufferViews[vertexAttributeBufferViews[attribute.kind]].byteOffset;
1345
- if (!byteOffset_1) {
1346
- byteOffset_1 = 0;
1347
- }
1348
- this.reorderVertexAttributeDataBasedOnPrimitiveMode(submesh, primitiveMode, sideOrientation, attribute.kind, vertexData, byteOffset_1, binaryWriter, convertToRightHandedSystem);
1349
- }
1350
- }
1351
- }
1352
- }
1353
- meshPrimitive.material = materialIndex;
1354
- }
1355
- if (morphTargetManager) {
1356
- var target = void 0;
1357
- for (var i = 0; i < morphTargetManager.numTargets; ++i) {
1358
- target = morphTargetManager.getTarget(i);
1359
- this.setMorphTargetAttributes(submesh, meshPrimitive, target, binaryWriter, convertToRightHandedSystem);
1360
- }
1361
- }
1362
- mesh.primitives.push(meshPrimitive);
1363
- this._extensionsPostExportMeshPrimitiveAsync("postExport", meshPrimitive, submesh, binaryWriter);
1364
- promises.push();
1365
- }
1366
- }
1367
- }
1368
- return Promise.all(promises).then(function () {
1369
- /* do nothing */
1370
- });
1371
- };
1372
- /**
1373
- * Check if the node is used to convert its descendants from a right handed coordinate system to the Babylon scene's coordinate system.
1374
- * @param node The node to check
1375
- * @returns True if the node is used to convert its descendants from right-handed to left-handed. False otherwise
1376
- */
1377
- _Exporter.prototype.isBabylonCoordinateSystemConvertingNode = function (node) {
1378
- if (node instanceof TransformNode) {
1379
- if (node.name !== "__root__") {
1380
- return false;
1381
- }
1382
- // Transform
1383
- var matrix = node.getWorldMatrix();
1384
- if (matrix.determinant() === 1) {
1385
- return false;
1386
- }
1387
- // Geometry
1388
- if ((node instanceof Mesh && node.geometry !== null) ||
1389
- (node instanceof InstancedMesh && node.sourceMesh.geometry !== null)) {
1390
- return false;
1391
- }
1392
- if (this._includeCoordinateSystemConversionNodes) {
1393
- return false;
1394
- }
1395
- return true;
1396
- }
1397
- return false;
1398
- };
1399
- /**
1400
- * Creates a glTF scene based on the array of meshes
1401
- * Returns the the total byte offset
1402
- * @param babylonScene Babylon scene to get the mesh data from
1403
- * @param binaryWriter Buffer to write binary data to
1404
- */
1405
- _Exporter.prototype.createSceneAsync = function (babylonScene, binaryWriter) {
1406
- var _this = this;
1407
- var scene = { nodes: [] };
1408
- var glTFNodeIndex;
1409
- var glTFNode;
1410
- var directDescendents;
1411
- var nodes = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], babylonScene.transformNodes, true), babylonScene.meshes, true), babylonScene.lights, true), babylonScene.cameras, true);
1412
- var rootNodesToLeftHanded = [];
1413
- this._convertToRightHandedSystem = !babylonScene.useRightHandedSystem;
1414
- this._convertToRightHandedSystemMap = {};
1415
- // Set default values for all nodes
1416
- babylonScene.rootNodes.forEach(function (rootNode) {
1417
- _this._convertToRightHandedSystemMap[rootNode.uniqueId] = _this._convertToRightHandedSystem;
1418
- rootNode.getDescendants(false).forEach(function (descendant) {
1419
- _this._convertToRightHandedSystemMap[descendant.uniqueId] = _this._convertToRightHandedSystem;
1420
- });
1421
- });
1422
- // Check if root nodes converting to left-handed are present
1423
- babylonScene.rootNodes.forEach(function (rootNode) {
1424
- if (_this.isBabylonCoordinateSystemConvertingNode(rootNode)) {
1425
- rootNodesToLeftHanded.push(rootNode);
1426
- // Exclude the node from list of nodes to export
1427
- var indexRootNode = nodes.indexOf(rootNode);
1428
- if (indexRootNode !== -1) { // should always be true
1429
- nodes.splice(indexRootNode, 1);
1430
- }
1431
- // Cancel conversion to right handed system
1432
- rootNode.getDescendants(false).forEach(function (descendant) {
1433
- _this._convertToRightHandedSystemMap[descendant.uniqueId] = false;
1434
- });
1435
- }
1436
- });
1437
- // Export babylon cameras to glTFCamera
1438
- var cameraHash = new Map();
1439
- babylonScene.cameras.forEach(function (camera) {
1440
- var glTFCamera = {
1441
- type: camera.mode === Camera.PERSPECTIVE_CAMERA ? "perspective" /* PERSPECTIVE */ : "orthographic" /* ORTHOGRAPHIC */
1442
- };
1443
- if (camera.name) {
1444
- glTFCamera.name = camera.name;
1445
- }
1446
- if (glTFCamera.type === "perspective" /* PERSPECTIVE */) {
1447
- glTFCamera.perspective = {
1448
- aspectRatio: camera.getEngine().getAspectRatio(camera),
1449
- yfov: camera._cache.fovMode === Camera.FOVMODE_VERTICAL_FIXED ? camera.fov : camera.fov * camera._cache.aspectRatio,
1450
- znear: camera.minZ, zfar: camera.maxZ
1451
- };
1452
- }
1453
- else if (glTFCamera.type === "orthographic" /* ORTHOGRAPHIC */) {
1454
- var halfWidth = camera.orthoLeft && camera.orthoRight ?
1455
- 0.5 * (camera.orthoRight - camera.orthoLeft) : camera.getEngine().getRenderWidth() * 0.5;
1456
- var halfHeight = camera.orthoBottom && camera.orthoTop ?
1457
- 0.5 * (camera.orthoTop - camera.orthoBottom) : camera.getEngine().getRenderHeight() * 0.5;
1458
- glTFCamera.orthographic = {
1459
- xmag: halfWidth, ymag: halfHeight,
1460
- znear: camera.minZ, zfar: camera.maxZ
1461
- };
1462
- }
1463
- cameraHash.set(camera, _this._cameras.length);
1464
- _this._cameras.push(glTFCamera);
1465
- });
1466
- var _a = this.getExportNodes(nodes), exportNodes = _a[0], exportMaterials = _a[1];
1467
- return this._glTFMaterialExporter._convertMaterialsToGLTFAsync(exportMaterials, "image/png" /* PNG */, true).then(function () {
1468
- return _this.createNodeMapAndAnimationsAsync(babylonScene, exportNodes, binaryWriter).then(function (nodeMap) {
1469
- return _this.createSkinsAsync(babylonScene, nodeMap, binaryWriter).then(function (skinMap) {
1470
- _this._nodeMap = nodeMap;
1471
- _this._totalByteLength = binaryWriter.getByteOffset();
1472
- if (_this._totalByteLength == undefined) {
1473
- throw new Error("undefined byte length!");
1474
- }
1475
- // Build Hierarchy with the node map.
1476
- for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
1477
- var babylonNode = nodes_1[_i];
1478
- glTFNodeIndex = _this._nodeMap[babylonNode.uniqueId];
1479
- if (glTFNodeIndex !== undefined) {
1480
- glTFNode = _this._nodes[glTFNodeIndex];
1481
- if (babylonNode.metadata) {
1482
- if (_this._options.metadataSelector) {
1483
- glTFNode.extras = _this._options.metadataSelector(babylonNode.metadata);
1484
- }
1485
- else if (babylonNode.metadata.gltf) {
1486
- glTFNode.extras = babylonNode.metadata.gltf.extras;
1487
- }
1488
- }
1489
- if (babylonNode instanceof Camera) {
1490
- glTFNode.camera = cameraHash.get(babylonNode);
1491
- }
1492
- if (!babylonNode.parent || rootNodesToLeftHanded.indexOf(babylonNode.parent) !== -1) {
1493
- if (_this._options.shouldExportNode && !_this._options.shouldExportNode(babylonNode)) {
1494
- Tools.Log("Omitting " + babylonNode.name + " from scene.");
1495
- }
1496
- else {
1497
- var convertToRightHandedSystem = _this._convertToRightHandedSystemMap[babylonNode.uniqueId];
1498
- if (convertToRightHandedSystem) {
1499
- if (glTFNode.translation) {
1500
- glTFNode.translation[2] *= -1;
1501
- glTFNode.translation[0] *= -1;
1502
- }
1503
- glTFNode.rotation = glTFNode.rotation ? Quaternion.FromArray([0, 1, 0, 0]).multiply(Quaternion.FromArray(glTFNode.rotation)).asArray() : (Quaternion.FromArray([0, 1, 0, 0])).asArray();
1504
- }
1505
- scene.nodes.push(glTFNodeIndex);
1506
- }
1507
- }
1508
- if (babylonNode instanceof Mesh) {
1509
- var babylonMesh = babylonNode;
1510
- if (babylonMesh.skeleton) {
1511
- glTFNode.skin = skinMap[babylonMesh.skeleton.uniqueId];
1512
- }
1513
- }
1514
- directDescendents = babylonNode.getDescendants(true);
1515
- if (!glTFNode.children && directDescendents && directDescendents.length) {
1516
- var children = [];
1517
- for (var _a = 0, directDescendents_1 = directDescendents; _a < directDescendents_1.length; _a++) {
1518
- var descendent = directDescendents_1[_a];
1519
- if (_this._nodeMap[descendent.uniqueId] != null) {
1520
- children.push(_this._nodeMap[descendent.uniqueId]);
1521
- }
1522
- }
1523
- if (children.length) {
1524
- glTFNode.children = children;
1525
- }
1526
- }
1527
- }
1528
- }
1529
- if (scene.nodes.length) {
1530
- _this._scenes.push(scene);
1531
- }
1532
- });
1533
- });
1534
- });
1535
- };
1536
- /**
1537
- * Getting the nodes and materials that would be exported.
1538
- * @param nodes Babylon transform nodes
1539
- * @returns Array of nodes which would be exported.
1540
- * @returns Set of materials which would be exported.
1541
- */
1542
- _Exporter.prototype.getExportNodes = function (nodes) {
1543
- var exportNodes = [];
1544
- var exportMaterials = new Set();
1545
- for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
1546
- var babylonNode = nodes_2[_i];
1547
- if (!this._options.shouldExportNode || this._options.shouldExportNode(babylonNode)) {
1548
- exportNodes.push(babylonNode);
1549
- if (babylonNode.getClassName() === "Mesh") {
1550
- var mesh = babylonNode;
1551
- if (mesh.material) {
1552
- exportMaterials.add(mesh.material);
1553
- }
1554
- }
1555
- else {
1556
- var meshes = babylonNode.getChildMeshes(false);
1557
- for (var _a = 0, meshes_1 = meshes; _a < meshes_1.length; _a++) {
1558
- var mesh = meshes_1[_a];
1559
- if (mesh.material) {
1560
- exportMaterials.add(mesh.material);
1561
- }
1562
- }
1563
- }
1564
- }
1565
- else {
1566
- "Excluding node ".concat(babylonNode.name);
1567
- }
1568
- }
1569
- return [exportNodes, exportMaterials];
1570
- };
1571
- /**
1572
- * Creates a mapping of Node unique id to node index and handles animations
1573
- * @param babylonScene Babylon Scene
1574
- * @param nodes Babylon transform nodes
1575
- * @param binaryWriter Buffer to write binary data to
1576
- * @returns Node mapping of unique id to index
1577
- */
1578
- _Exporter.prototype.createNodeMapAndAnimationsAsync = function (babylonScene, nodes, binaryWriter) {
1579
- var _this = this;
1580
- var promiseChain = Promise.resolve();
1581
- var nodeMap = {};
1582
- var nodeIndex;
1583
- var runtimeGLTFAnimation = {
1584
- name: 'runtime animations',
1585
- channels: [],
1586
- samplers: []
1587
- };
1588
- var idleGLTFAnimations = [];
1589
- var _loop_1 = function (babylonNode) {
1590
- promiseChain = promiseChain.then(function () {
1591
- var convertToRightHandedSystem = _this._convertToRightHandedSystemMap[babylonNode.uniqueId];
1592
- return _this.createNodeAsync(babylonNode, binaryWriter, convertToRightHandedSystem, nodeMap).then(function (node) {
1593
- var promise = _this._extensionsPostExportNodeAsync("createNodeAsync", node, babylonNode, nodeMap);
1594
- if (promise == null) {
1595
- Tools.Warn("Not exporting node ".concat(babylonNode.name));
1596
- return Promise.resolve();
1597
- }
1598
- else {
1599
- return promise.then(function (node) {
1600
- if (!node) {
1601
- return;
1602
- }
1603
- _this._nodes.push(node);
1604
- nodeIndex = _this._nodes.length - 1;
1605
- nodeMap[babylonNode.uniqueId] = nodeIndex;
1606
- if (!babylonScene.animationGroups.length) {
1607
- _GLTFAnimation._CreateMorphTargetAnimationFromMorphTargetAnimations(babylonNode, runtimeGLTFAnimation, idleGLTFAnimations, nodeMap, _this._nodes, binaryWriter, _this._bufferViews, _this._accessors, convertToRightHandedSystem, _this._animationSampleRate);
1608
- if (babylonNode.animations.length) {
1609
- _GLTFAnimation._CreateNodeAnimationFromNodeAnimations(babylonNode, runtimeGLTFAnimation, idleGLTFAnimations, nodeMap, _this._nodes, binaryWriter, _this._bufferViews, _this._accessors, convertToRightHandedSystem, _this._animationSampleRate);
1610
- }
1611
- }
1612
- });
1613
- }
1614
- });
1615
- });
1616
- };
1617
- for (var _i = 0, nodes_3 = nodes; _i < nodes_3.length; _i++) {
1618
- var babylonNode = nodes_3[_i];
1619
- _loop_1(babylonNode);
1620
- }
1621
- return promiseChain.then(function () {
1622
- if (runtimeGLTFAnimation.channels.length && runtimeGLTFAnimation.samplers.length) {
1623
- _this._animations.push(runtimeGLTFAnimation);
1624
- }
1625
- idleGLTFAnimations.forEach(function (idleGLTFAnimation) {
1626
- if (idleGLTFAnimation.channels.length && idleGLTFAnimation.samplers.length) {
1627
- _this._animations.push(idleGLTFAnimation);
1628
- }
1629
- });
1630
- if (babylonScene.animationGroups.length) {
1631
- _GLTFAnimation._CreateNodeAndMorphAnimationFromAnimationGroups(babylonScene, _this._animations, nodeMap, _this._nodes, binaryWriter, _this._bufferViews, _this._accessors, _this._convertToRightHandedSystemMap, _this._animationSampleRate);
1632
- }
1633
- return nodeMap;
1634
- });
1635
- };
1636
- /**
1637
- * Creates a glTF node from a Babylon mesh
1638
- * @param babylonMesh Source Babylon mesh
1639
- * @param binaryWriter Buffer for storing geometry data
1640
- * @param convertToRightHandedSystem Converts the values to right-handed
1641
- * @param nodeMap Node mapping of unique id to glTF node index
1642
- * @returns glTF node
1643
- */
1644
- _Exporter.prototype.createNodeAsync = function (babylonNode, binaryWriter, convertToRightHandedSystem, nodeMap) {
1645
- var _this = this;
1646
- return Promise.resolve().then(function () {
1647
- // create node to hold translation/rotation/scale and the mesh
1648
- var node = {};
1649
- // create mesh
1650
- var mesh = { primitives: [] };
1651
- if (babylonNode.name) {
1652
- node.name = babylonNode.name;
1653
- }
1654
- if (babylonNode instanceof TransformNode) {
1655
- // Set transformation
1656
- _this.setNodeTransformation(node, babylonNode, convertToRightHandedSystem);
1657
- if (babylonNode instanceof Mesh) {
1658
- var morphTargetManager = babylonNode.morphTargetManager;
1659
- if (morphTargetManager && morphTargetManager.numTargets > 0) {
1660
- mesh.weights = [];
1661
- for (var i = 0; i < morphTargetManager.numTargets; ++i) {
1662
- mesh.weights.push(morphTargetManager.getTarget(i).influence);
1663
- }
1664
- }
1665
- }
1666
- return _this.setPrimitiveAttributesAsync(mesh, babylonNode, binaryWriter, convertToRightHandedSystem).then(function () {
1667
- if (mesh.primitives.length) {
1668
- _this._meshes.push(mesh);
1669
- node.mesh = _this._meshes.length - 1;
1670
- }
1671
- return node;
1672
- });
1673
- }
1674
- else if (babylonNode instanceof Camera) {
1675
- _this.setCameraTransformation(node, babylonNode, convertToRightHandedSystem);
1676
- return node;
1677
- }
1678
- else {
1679
- return node;
1680
- }
1681
- });
1682
- };
1683
- /**
1684
- * Creates a glTF skin from a Babylon skeleton
1685
- * @param babylonScene Babylon Scene
1686
- * @param nodes Babylon transform nodes
1687
- * @param binaryWriter Buffer to write binary data to
1688
- * @returns Node mapping of unique id to index
1689
- */
1690
- _Exporter.prototype.createSkinsAsync = function (babylonScene, nodeMap, binaryWriter) {
1691
- var _a;
1692
- var promiseChain = Promise.resolve();
1693
- var skinMap = {};
1694
- for (var _i = 0, _b = babylonScene.skeletons; _i < _b.length; _i++) {
1695
- var skeleton = _b[_i];
1696
- // create skin
1697
- var skin = { joints: [] };
1698
- var inverseBindMatrices = [];
1699
- var boneIndexMap = {};
1700
- var maxBoneIndex = -1;
1701
- for (var i = 0; i < skeleton.bones.length; ++i) {
1702
- var bone = skeleton.bones[i];
1703
- var boneIndex = (_a = bone.getIndex()) !== null && _a !== void 0 ? _a : i;
1704
- if (boneIndex !== -1) {
1705
- boneIndexMap[boneIndex] = bone;
1706
- if (boneIndex > maxBoneIndex) {
1707
- maxBoneIndex = boneIndex;
1708
- }
1709
- }
1710
- }
1711
- for (var boneIndex = 0; boneIndex <= maxBoneIndex; ++boneIndex) {
1712
- var bone = boneIndexMap[boneIndex];
1713
- inverseBindMatrices.push(bone.getInvertedAbsoluteTransform());
1714
- var transformNode = bone.getTransformNode();
1715
- if (transformNode) {
1716
- skin.joints.push(nodeMap[transformNode.uniqueId]);
1717
- }
1718
- else {
1719
- Tools.Warn("Exporting a bone without a linked transform node is currently unsupported");
1720
- }
1721
- }
1722
- // create buffer view for inverse bind matrices
1723
- var byteStride = 64; // 4 x 4 matrix of 32 bit float
1724
- var byteLength = inverseBindMatrices.length * byteStride;
1725
- var bufferViewOffset = binaryWriter.getByteOffset();
1726
- var bufferView = _GLTFUtilities._CreateBufferView(0, bufferViewOffset, byteLength, undefined, "InverseBindMatrices" + " - " + skeleton.name);
1727
- this._bufferViews.push(bufferView);
1728
- var bufferViewIndex = this._bufferViews.length - 1;
1729
- var bindMatrixAccessor = _GLTFUtilities._CreateAccessor(bufferViewIndex, "InverseBindMatrices" + " - " + skeleton.name, "MAT4" /* MAT4 */, 5126 /* FLOAT */, inverseBindMatrices.length, null, null, null);
1730
- var inverseBindAccessorIndex = this._accessors.push(bindMatrixAccessor) - 1;
1731
- skin.inverseBindMatrices = inverseBindAccessorIndex;
1732
- this._skins.push(skin);
1733
- skinMap[skeleton.uniqueId] = this._skins.length - 1;
1734
- inverseBindMatrices.forEach(function (mat) {
1735
- mat.m.forEach(function (cell) {
1736
- binaryWriter.setFloat32(cell);
1737
- });
1738
- });
1739
- }
1740
- return promiseChain.then(function () {
1741
- return skinMap;
1742
- });
1743
- };
1744
- _Exporter._ExtensionNames = new Array();
1745
- _Exporter._ExtensionFactories = {};
1746
- return _Exporter;
1747
- }());
1748
- export { _Exporter };
1749
- /**
1750
- * @hidden
1751
- *
1752
- * Stores glTF binary data. If the array buffer byte length is exceeded, it doubles in size dynamically
1753
- */
1754
- var _BinaryWriter = /** @class */ (function () {
1755
- /**
1756
- * Initialize binary writer with an initial byte length
1757
- * @param byteLength Initial byte length of the array buffer
1758
- */
1759
- function _BinaryWriter(byteLength) {
1760
- this._arrayBuffer = new ArrayBuffer(byteLength);
1761
- this._dataView = new DataView(this._arrayBuffer);
1762
- this._byteOffset = 0;
1763
- }
1764
- /**
1765
- * Resize the array buffer to the specified byte length
1766
- * @param byteLength
1767
- */
1768
- _BinaryWriter.prototype.resizeBuffer = function (byteLength) {
1769
- var newBuffer = new ArrayBuffer(byteLength);
1770
- var oldUint8Array = new Uint8Array(this._arrayBuffer);
1771
- var newUint8Array = new Uint8Array(newBuffer);
1772
- for (var i = 0, length_10 = newUint8Array.byteLength; i < length_10; ++i) {
1773
- newUint8Array[i] = oldUint8Array[i];
1774
- }
1775
- this._arrayBuffer = newBuffer;
1776
- this._dataView = new DataView(this._arrayBuffer);
1777
- return newBuffer;
1778
- };
1779
- /**
1780
- * Get an array buffer with the length of the byte offset
1781
- * @returns ArrayBuffer resized to the byte offset
1782
- */
1783
- _BinaryWriter.prototype.getArrayBuffer = function () {
1784
- return this.resizeBuffer(this.getByteOffset());
1785
- };
1786
- /**
1787
- * Get the byte offset of the array buffer
1788
- * @returns byte offset
1789
- */
1790
- _BinaryWriter.prototype.getByteOffset = function () {
1791
- if (this._byteOffset == undefined) {
1792
- throw new Error("Byte offset is undefined!");
1793
- }
1794
- return this._byteOffset;
1795
- };
1796
- /**
1797
- * Stores an UInt8 in the array buffer
1798
- * @param entry
1799
- * @param byteOffset If defined, specifies where to set the value as an offset.
1800
- */
1801
- _BinaryWriter.prototype.setUInt8 = function (entry, byteOffset) {
1802
- if (byteOffset != null) {
1803
- if (byteOffset < this._byteOffset) {
1804
- this._dataView.setUint8(byteOffset, entry);
1805
- }
1806
- else {
1807
- Tools.Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
1808
- }
1809
- }
1810
- else {
1811
- if (this._byteOffset + 1 > this._arrayBuffer.byteLength) {
1812
- this.resizeBuffer(this._arrayBuffer.byteLength * 2);
1813
- }
1814
- this._dataView.setUint8(this._byteOffset, entry);
1815
- this._byteOffset += 1;
1816
- }
1817
- };
1818
- /**
1819
- * Stores an UInt16 in the array buffer
1820
- * @param entry
1821
- * @param byteOffset If defined, specifies where to set the value as an offset.
1822
- */
1823
- _BinaryWriter.prototype.setUInt16 = function (entry, byteOffset) {
1824
- if (byteOffset != null) {
1825
- if (byteOffset < this._byteOffset) {
1826
- this._dataView.setUint16(byteOffset, entry, true);
1827
- }
1828
- else {
1829
- Tools.Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
1830
- }
1831
- }
1832
- else {
1833
- if (this._byteOffset + 2 > this._arrayBuffer.byteLength) {
1834
- this.resizeBuffer(this._arrayBuffer.byteLength * 2);
1835
- }
1836
- this._dataView.setUint16(this._byteOffset, entry, true);
1837
- this._byteOffset += 2;
1838
- }
1839
- };
1840
- /**
1841
- * Gets an UInt32 in the array buffer
1842
- * @param entry
1843
- * @param byteOffset If defined, specifies where to set the value as an offset.
1844
- */
1845
- _BinaryWriter.prototype.getUInt32 = function (byteOffset) {
1846
- if (byteOffset < this._byteOffset) {
1847
- return this._dataView.getUint32(byteOffset, true);
1848
- }
1849
- else {
1850
- Tools.Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
1851
- throw new Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
1852
- }
1853
- };
1854
- _BinaryWriter.prototype.getVector3Float32FromRef = function (vector3, byteOffset) {
1855
- if (byteOffset + 8 > this._byteOffset) {
1856
- Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
1857
- }
1858
- else {
1859
- vector3.x = this._dataView.getFloat32(byteOffset, true);
1860
- vector3.y = this._dataView.getFloat32(byteOffset + 4, true);
1861
- vector3.z = this._dataView.getFloat32(byteOffset + 8, true);
1862
- }
1863
- };
1864
- _BinaryWriter.prototype.setVector3Float32FromRef = function (vector3, byteOffset) {
1865
- if (byteOffset + 8 > this._byteOffset) {
1866
- Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
1867
- }
1868
- else {
1869
- this._dataView.setFloat32(byteOffset, vector3.x, true);
1870
- this._dataView.setFloat32(byteOffset + 4, vector3.y, true);
1871
- this._dataView.setFloat32(byteOffset + 8, vector3.z, true);
1872
- }
1873
- };
1874
- _BinaryWriter.prototype.getVector4Float32FromRef = function (vector4, byteOffset) {
1875
- if (byteOffset + 12 > this._byteOffset) {
1876
- Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
1877
- }
1878
- else {
1879
- vector4.x = this._dataView.getFloat32(byteOffset, true);
1880
- vector4.y = this._dataView.getFloat32(byteOffset + 4, true);
1881
- vector4.z = this._dataView.getFloat32(byteOffset + 8, true);
1882
- vector4.w = this._dataView.getFloat32(byteOffset + 12, true);
1883
- }
1884
- };
1885
- _BinaryWriter.prototype.setVector4Float32FromRef = function (vector4, byteOffset) {
1886
- if (byteOffset + 12 > this._byteOffset) {
1887
- Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!");
1888
- }
1889
- else {
1890
- this._dataView.setFloat32(byteOffset, vector4.x, true);
1891
- this._dataView.setFloat32(byteOffset + 4, vector4.y, true);
1892
- this._dataView.setFloat32(byteOffset + 8, vector4.z, true);
1893
- this._dataView.setFloat32(byteOffset + 12, vector4.w, true);
1894
- }
1895
- };
1896
- /**
1897
- * Stores a Float32 in the array buffer
1898
- * @param entry
1899
- */
1900
- _BinaryWriter.prototype.setFloat32 = function (entry, byteOffset) {
1901
- if (isNaN(entry)) {
1902
- Tools.Error('Invalid data being written!');
1903
- }
1904
- if (byteOffset != null) {
1905
- if (byteOffset < this._byteOffset) {
1906
- this._dataView.setFloat32(byteOffset, entry, true);
1907
- }
1908
- else {
1909
- Tools.Error('BinaryWriter: byteoffset is greater than the current binary length!');
1910
- }
1911
- }
1912
- if (this._byteOffset + 4 > this._arrayBuffer.byteLength) {
1913
- this.resizeBuffer(this._arrayBuffer.byteLength * 2);
1914
- }
1915
- this._dataView.setFloat32(this._byteOffset, entry, true);
1916
- this._byteOffset += 4;
1917
- };
1918
- /**
1919
- * Stores an UInt32 in the array buffer
1920
- * @param entry
1921
- * @param byteOffset If defined, specifies where to set the value as an offset.
1922
- */
1923
- _BinaryWriter.prototype.setUInt32 = function (entry, byteOffset) {
1924
- if (byteOffset != null) {
1925
- if (byteOffset < this._byteOffset) {
1926
- this._dataView.setUint32(byteOffset, entry, true);
1927
- }
1928
- else {
1929
- Tools.Error('BinaryWriter: byteoffset is greater than the current binary buffer length!');
1930
- }
1931
- }
1932
- else {
1933
- if (this._byteOffset + 4 > this._arrayBuffer.byteLength) {
1934
- this.resizeBuffer(this._arrayBuffer.byteLength * 2);
1935
- }
1936
- this._dataView.setUint32(this._byteOffset, entry, true);
1937
- this._byteOffset += 4;
1938
- }
1939
- };
1940
- return _BinaryWriter;
1941
- }());
1942
- export { _BinaryWriter };
1943
- //# sourceMappingURL=glTFExporter.js.map