@babylonjs/loaders 5.0.0-rc.7 → 5.0.0-rc.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. package/OBJ/index.d.ts +4 -4
  2. package/OBJ/index.js +4 -4
  3. package/OBJ/mtlFileLoader.d.ts +41 -41
  4. package/OBJ/mtlFileLoader.js +232 -232
  5. package/OBJ/objFileLoader.d.ts +126 -126
  6. package/OBJ/objFileLoader.js +296 -296
  7. package/OBJ/objLoadingOptions.d.ts +43 -43
  8. package/OBJ/objLoadingOptions.js +1 -1
  9. package/OBJ/solidParser.d.ts +154 -154
  10. package/OBJ/solidParser.js +693 -693
  11. package/STL/index.d.ts +1 -1
  12. package/STL/index.js +1 -1
  13. package/STL/stlFileLoader.d.ts +64 -64
  14. package/STL/stlFileLoader.js +233 -233
  15. package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
  16. package/glTF/1.0/glTFBinaryExtension.js +60 -60
  17. package/glTF/1.0/glTFLoader.d.ts +136 -136
  18. package/glTF/1.0/glTFLoader.js +1815 -1815
  19. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
  20. package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
  21. package/glTF/1.0/glTFLoaderUtils.d.ts +62 -62
  22. package/glTF/1.0/glTFLoaderUtils.js +250 -250
  23. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
  24. package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -129
  25. package/glTF/1.0/index.d.ts +5 -5
  26. package/glTF/1.0/index.js +5 -5
  27. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -43
  28. package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -117
  29. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -36
  30. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -88
  31. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -34
  32. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -49
  33. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -29
  34. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -40
  35. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -50
  36. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -73
  37. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -39
  38. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -112
  39. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -36
  40. package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -89
  41. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -39
  42. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -94
  43. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -38
  44. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -53
  45. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -42
  46. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -62
  47. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -38
  48. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -80
  49. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -39
  50. package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -84
  51. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -38
  52. package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -74
  53. package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -39
  54. package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -83
  55. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -38
  56. package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -288
  57. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -38
  58. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -73
  59. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -87
  60. package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -254
  61. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -39
  62. package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -85
  63. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -22
  64. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -23
  65. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -29
  66. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -40
  67. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -33
  68. package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -61
  69. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -32
  70. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -49
  71. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -54
  72. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -240
  73. package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -109
  74. package/glTF/2.0/Extensions/MSFT_lod.js +369 -369
  75. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
  76. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
  77. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
  78. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
  79. package/glTF/2.0/Extensions/index.d.ts +26 -26
  80. package/glTF/2.0/Extensions/index.js +26 -26
  81. package/glTF/2.0/glTFLoader.d.ts +405 -405
  82. package/glTF/2.0/glTFLoader.js +2366 -2366
  83. package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
  84. package/glTF/2.0/glTFLoaderExtension.js +1 -1
  85. package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -211
  86. package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
  87. package/glTF/2.0/index.d.ts +4 -4
  88. package/glTF/2.0/index.js +4 -4
  89. package/glTF/glTFFileLoader.d.ts +433 -433
  90. package/glTF/glTFFileLoader.js +927 -927
  91. package/glTF/glTFValidation.d.ts +29 -29
  92. package/glTF/glTFValidation.js +123 -123
  93. package/glTF/index.d.ts +5 -5
  94. package/glTF/index.js +5 -5
  95. package/index.d.ts +3 -3
  96. package/index.js +3 -3
  97. package/legacy/legacy-glTF.d.ts +2 -2
  98. package/legacy/legacy-glTF.js +18 -18
  99. package/legacy/legacy-glTF1.d.ts +2 -2
  100. package/legacy/legacy-glTF1.js +14 -14
  101. package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
  102. package/legacy/legacy-glTF1FileLoader.js +3 -3
  103. package/legacy/legacy-glTF2.d.ts +2 -2
  104. package/legacy/legacy-glTF2.js +32 -32
  105. package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
  106. package/legacy/legacy-glTF2FileLoader.js +3 -3
  107. package/legacy/legacy-glTFFileLoader.d.ts +3 -3
  108. package/legacy/legacy-glTFFileLoader.js +4 -4
  109. package/legacy/legacy-objFileLoader.d.ts +1 -1
  110. package/legacy/legacy-objFileLoader.js +12 -12
  111. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  112. package/legacy/legacy-stlFileLoader.js +12 -12
  113. package/legacy/legacy.d.ts +6 -6
  114. package/legacy/legacy.js +7 -7
  115. package/package.json +3 -3
@@ -1,928 +1,928 @@
1
- import { Observable } from "@babylonjs/core/Misc/observable.js";
2
- import { Tools } from "@babylonjs/core/Misc/tools.js";
3
- import { SceneLoader, } from "@babylonjs/core/Loading/sceneLoader.js";
4
- import { AssetContainer } from "@babylonjs/core/assetContainer.js";
5
- import { Logger } from "@babylonjs/core/Misc/logger.js";
6
- import { DataReader } from "@babylonjs/core/Misc/dataReader.js";
7
- import { GLTFValidation } from "./glTFValidation.js";
8
- import { DecodeBase64UrlToBinary } from "@babylonjs/core/Misc/fileTools.js";
9
- import { StringTools } from "@babylonjs/core/Misc/stringTools.js";
10
- import { RuntimeError, ErrorCodes } from "@babylonjs/core/Misc/error.js";
11
- function readAsync(arrayBuffer, byteOffset, byteLength) {
12
- try {
13
- return Promise.resolve(new Uint8Array(arrayBuffer, byteOffset, byteLength));
14
- }
15
- catch (e) {
16
- return Promise.reject(e);
17
- }
18
- }
19
- /**
20
- * Mode that determines the coordinate system to use.
21
- */
22
- export var GLTFLoaderCoordinateSystemMode;
23
- (function (GLTFLoaderCoordinateSystemMode) {
24
- /**
25
- * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
26
- */
27
- GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
28
- /**
29
- * Sets the useRightHandedSystem flag on the scene.
30
- */
31
- GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
32
- })(GLTFLoaderCoordinateSystemMode || (GLTFLoaderCoordinateSystemMode = {}));
33
- /**
34
- * Mode that determines what animations will start.
35
- */
36
- export var GLTFLoaderAnimationStartMode;
37
- (function (GLTFLoaderAnimationStartMode) {
38
- /**
39
- * No animation will start.
40
- */
41
- GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
42
- /**
43
- * The first animation will start.
44
- */
45
- GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
46
- /**
47
- * All animations will start.
48
- */
49
- GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
50
- })(GLTFLoaderAnimationStartMode || (GLTFLoaderAnimationStartMode = {}));
51
- /**
52
- * Loader state.
53
- */
54
- export var GLTFLoaderState;
55
- (function (GLTFLoaderState) {
56
- /**
57
- * The asset is loading.
58
- */
59
- GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
60
- /**
61
- * The asset is ready for rendering.
62
- */
63
- GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
64
- /**
65
- * The asset is completely loaded.
66
- */
67
- GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
68
- })(GLTFLoaderState || (GLTFLoaderState = {}));
69
- /**
70
- * File loader for loading glTF files into a scene.
71
- */
72
- var GLTFFileLoader = /** @class */ (function () {
73
- function GLTFFileLoader() {
74
- // --------------
75
- // Common options
76
- // --------------
77
- /**
78
- * Raised when the asset has been parsed
79
- */
80
- this.onParsedObservable = new Observable();
81
- // ----------
82
- // V2 options
83
- // ----------
84
- /**
85
- * The coordinate system mode. Defaults to AUTO.
86
- */
87
- this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
88
- /**
89
- * The animation start mode. Defaults to FIRST.
90
- */
91
- this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
92
- /**
93
- * Defines if the loader should compile materials before raising the success callback. Defaults to false.
94
- */
95
- this.compileMaterials = false;
96
- /**
97
- * Defines if the loader should also compile materials with clip planes. Defaults to false.
98
- */
99
- this.useClipPlane = false;
100
- /**
101
- * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
102
- */
103
- this.compileShadowGenerators = false;
104
- /**
105
- * Defines if the Alpha blended materials are only applied as coverage.
106
- * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
107
- * If true, no extra effects are applied to transparent pixels.
108
- */
109
- this.transparencyAsCoverage = false;
110
- /**
111
- * Defines if the loader should use range requests when load binary glTF files from HTTP.
112
- * Enabling will disable offline support and glTF validator.
113
- * Defaults to false.
114
- */
115
- this.useRangeRequests = false;
116
- /**
117
- * Defines if the loader should create instances when multiple glTF nodes point to the same glTF mesh. Defaults to true.
118
- */
119
- this.createInstances = true;
120
- /**
121
- * Defines if the loader should always compute the bounding boxes of meshes and not use the min/max values from the position accessor. Defaults to false.
122
- */
123
- this.alwaysComputeBoundingBox = false;
124
- /**
125
- * If true, load all materials defined in the file, even if not used by any mesh. Defaults to false.
126
- */
127
- this.loadAllMaterials = false;
128
- /**
129
- * If true, load only the materials defined in the file. Defaults to false.
130
- */
131
- this.loadOnlyMaterials = false;
132
- /**
133
- * If true, do not load any materials defined in the file. Defaults to false.
134
- */
135
- this.skipMaterials = false;
136
- /**
137
- * If true, load the color (gamma encoded) textures into sRGB buffers (if supported by the GPU), which will yield more accurate results when sampling the texture. Defaults to true.
138
- */
139
- this.useSRGBBuffers = true;
140
- /**
141
- * When loading glTF animations, which are defined in seconds, target them to this FPS. Defaults to 60.
142
- */
143
- this.targetFps = 60;
144
- /**
145
- * Defines if the loader should always compute the nearest common ancestor of the skeleton joints instead of using `skin.skeleton`. Defaults to false.
146
- * Set this to true if loading assets with invalid `skin.skeleton` values.
147
- */
148
- this.alwaysComputeSkeletonRootNode = false;
149
- /**
150
- * Function called before loading a url referenced by the asset.
151
- * @param url
152
- */
153
- this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
154
- /**
155
- * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
156
- * Note that the observable is raised as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...)
157
- */
158
- this.onMeshLoadedObservable = new Observable();
159
- /**
160
- * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
161
- */
162
- this.onTextureLoadedObservable = new Observable();
163
- /**
164
- * Observable raised when the loader creates a material after parsing the glTF properties of the material.
165
- */
166
- this.onMaterialLoadedObservable = new Observable();
167
- /**
168
- * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
169
- */
170
- this.onCameraLoadedObservable = new Observable();
171
- /**
172
- * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
173
- * For assets with LODs, raised when all of the LODs are complete.
174
- * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
175
- */
176
- this.onCompleteObservable = new Observable();
177
- /**
178
- * Observable raised when an error occurs.
179
- */
180
- this.onErrorObservable = new Observable();
181
- /**
182
- * Observable raised after the loader is disposed.
183
- */
184
- this.onDisposeObservable = new Observable();
185
- /**
186
- * Observable raised after a loader extension is created.
187
- * Set additional options for a loader extension in this event.
188
- */
189
- this.onExtensionLoadedObservable = new Observable();
190
- /**
191
- * Defines if the loader should validate the asset.
192
- */
193
- this.validate = false;
194
- /**
195
- * Observable raised after validation when validate is set to true. The event data is the result of the validation.
196
- */
197
- this.onValidatedObservable = new Observable();
198
- this._loader = null;
199
- this._state = null;
200
- this._requests = new Array();
201
- /**
202
- * Name of the loader ("gltf")
203
- */
204
- this.name = "gltf";
205
- /** @hidden */
206
- this.extensions = {
207
- ".gltf": { isBinary: false },
208
- ".glb": { isBinary: true },
209
- };
210
- /**
211
- * Observable raised when the loader state changes.
212
- */
213
- this.onLoaderStateChangedObservable = new Observable();
214
- this._logIndentLevel = 0;
215
- this._loggingEnabled = false;
216
- /** @hidden */
217
- this._log = this._logDisabled;
218
- this._capturePerformanceCounters = false;
219
- /** @hidden */
220
- this._startPerformanceCounter = this._startPerformanceCounterDisabled;
221
- /** @hidden */
222
- this._endPerformanceCounter = this._endPerformanceCounterDisabled;
223
- }
224
- Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
225
- /**
226
- * Raised when the asset has been parsed
227
- */
228
- set: function (callback) {
229
- if (this._onParsedObserver) {
230
- this.onParsedObservable.remove(this._onParsedObserver);
231
- }
232
- this._onParsedObserver = this.onParsedObservable.add(callback);
233
- },
234
- enumerable: false,
235
- configurable: true
236
- });
237
- Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
238
- /**
239
- * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
240
- * Note that the callback is called as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...)
241
- */
242
- set: function (callback) {
243
- if (this._onMeshLoadedObserver) {
244
- this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
245
- }
246
- this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
247
- },
248
- enumerable: false,
249
- configurable: true
250
- });
251
- Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
252
- /**
253
- * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
254
- */
255
- set: function (callback) {
256
- if (this._onTextureLoadedObserver) {
257
- this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
258
- }
259
- this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
260
- },
261
- enumerable: false,
262
- configurable: true
263
- });
264
- Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
265
- /**
266
- * Callback raised when the loader creates a material after parsing the glTF properties of the material.
267
- */
268
- set: function (callback) {
269
- if (this._onMaterialLoadedObserver) {
270
- this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
271
- }
272
- this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
273
- },
274
- enumerable: false,
275
- configurable: true
276
- });
277
- Object.defineProperty(GLTFFileLoader.prototype, "onCameraLoaded", {
278
- /**
279
- * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
280
- */
281
- set: function (callback) {
282
- if (this._onCameraLoadedObserver) {
283
- this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver);
284
- }
285
- this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback);
286
- },
287
- enumerable: false,
288
- configurable: true
289
- });
290
- Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
291
- /**
292
- * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
293
- * For assets with LODs, raised when all of the LODs are complete.
294
- * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
295
- */
296
- set: function (callback) {
297
- if (this._onCompleteObserver) {
298
- this.onCompleteObservable.remove(this._onCompleteObserver);
299
- }
300
- this._onCompleteObserver = this.onCompleteObservable.add(callback);
301
- },
302
- enumerable: false,
303
- configurable: true
304
- });
305
- Object.defineProperty(GLTFFileLoader.prototype, "onError", {
306
- /**
307
- * Callback raised when an error occurs.
308
- */
309
- set: function (callback) {
310
- if (this._onErrorObserver) {
311
- this.onErrorObservable.remove(this._onErrorObserver);
312
- }
313
- this._onErrorObserver = this.onErrorObservable.add(callback);
314
- },
315
- enumerable: false,
316
- configurable: true
317
- });
318
- Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
319
- /**
320
- * Callback raised after the loader is disposed.
321
- */
322
- set: function (callback) {
323
- if (this._onDisposeObserver) {
324
- this.onDisposeObservable.remove(this._onDisposeObserver);
325
- }
326
- this._onDisposeObserver = this.onDisposeObservable.add(callback);
327
- },
328
- enumerable: false,
329
- configurable: true
330
- });
331
- Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
332
- /**
333
- * Callback raised after a loader extension is created.
334
- */
335
- set: function (callback) {
336
- if (this._onExtensionLoadedObserver) {
337
- this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
338
- }
339
- this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
340
- },
341
- enumerable: false,
342
- configurable: true
343
- });
344
- Object.defineProperty(GLTFFileLoader.prototype, "loggingEnabled", {
345
- /**
346
- * Defines if the loader logging is enabled.
347
- */
348
- get: function () {
349
- return this._loggingEnabled;
350
- },
351
- set: function (value) {
352
- if (this._loggingEnabled === value) {
353
- return;
354
- }
355
- this._loggingEnabled = value;
356
- if (this._loggingEnabled) {
357
- this._log = this._logEnabled;
358
- }
359
- else {
360
- this._log = this._logDisabled;
361
- }
362
- },
363
- enumerable: false,
364
- configurable: true
365
- });
366
- Object.defineProperty(GLTFFileLoader.prototype, "capturePerformanceCounters", {
367
- /**
368
- * Defines if the loader should capture performance counters.
369
- */
370
- get: function () {
371
- return this._capturePerformanceCounters;
372
- },
373
- set: function (value) {
374
- if (this._capturePerformanceCounters === value) {
375
- return;
376
- }
377
- this._capturePerformanceCounters = value;
378
- if (this._capturePerformanceCounters) {
379
- this._startPerformanceCounter = this._startPerformanceCounterEnabled;
380
- this._endPerformanceCounter = this._endPerformanceCounterEnabled;
381
- }
382
- else {
383
- this._startPerformanceCounter = this._startPerformanceCounterDisabled;
384
- this._endPerformanceCounter = this._endPerformanceCounterDisabled;
385
- }
386
- },
387
- enumerable: false,
388
- configurable: true
389
- });
390
- Object.defineProperty(GLTFFileLoader.prototype, "onValidated", {
391
- /**
392
- * Callback raised after a loader extension is created.
393
- */
394
- set: function (callback) {
395
- if (this._onValidatedObserver) {
396
- this.onValidatedObservable.remove(this._onValidatedObserver);
397
- }
398
- this._onValidatedObserver = this.onValidatedObservable.add(callback);
399
- },
400
- enumerable: false,
401
- configurable: true
402
- });
403
- /**
404
- * Disposes the loader, releases resources during load, and cancels any outstanding requests.
405
- */
406
- GLTFFileLoader.prototype.dispose = function () {
407
- if (this._loader) {
408
- this._loader.dispose();
409
- this._loader = null;
410
- }
411
- for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
412
- var request = _a[_i];
413
- request.abort();
414
- }
415
- this._requests.length = 0;
416
- delete this._progressCallback;
417
- this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
418
- this.onMeshLoadedObservable.clear();
419
- this.onTextureLoadedObservable.clear();
420
- this.onMaterialLoadedObservable.clear();
421
- this.onCameraLoadedObservable.clear();
422
- this.onCompleteObservable.clear();
423
- this.onExtensionLoadedObservable.clear();
424
- this.onDisposeObservable.notifyObservers(undefined);
425
- this.onDisposeObservable.clear();
426
- };
427
- /**
428
- * @param scene
429
- * @param fileOrUrl
430
- * @param onSuccess
431
- * @param onProgress
432
- * @param useArrayBuffer
433
- * @param onError
434
- * @hidden
435
- */
436
- GLTFFileLoader.prototype.loadFile = function (scene, fileOrUrl, onSuccess, onProgress, useArrayBuffer, onError) {
437
- var _this = this;
438
- this._progressCallback = onProgress;
439
- var rootUrl = fileOrUrl.name ? "file:" : Tools.GetFolderPath(fileOrUrl);
440
- var fileName = fileOrUrl.name || Tools.GetFilename(fileOrUrl);
441
- if (useArrayBuffer) {
442
- if (this.useRangeRequests) {
443
- if (this.validate) {
444
- Logger.Warn("glTF validation is not supported when range requests are enabled");
445
- }
446
- var fileRequest_1 = {
447
- abort: function () { },
448
- onCompleteObservable: new Observable(),
449
- };
450
- var dataBuffer = {
451
- readAsync: function (byteOffset, byteLength) {
452
- return new Promise(function (resolve, reject) {
453
- _this._loadFile(scene, fileOrUrl, function (data) {
454
- resolve(new Uint8Array(data));
455
- }, true, function (error) {
456
- reject(error);
457
- }, function (webRequest) {
458
- webRequest.setRequestHeader("Range", "bytes=".concat(byteOffset, "-").concat(byteOffset + byteLength - 1));
459
- });
460
- });
461
- },
462
- byteLength: 0,
463
- };
464
- this._unpackBinaryAsync(new DataReader(dataBuffer)).then(function (loaderData) {
465
- fileRequest_1.onCompleteObservable.notifyObservers(fileRequest_1);
466
- onSuccess(loaderData);
467
- }, onError ? function (error) { return onError(undefined, error); } : undefined);
468
- return fileRequest_1;
469
- }
470
- return this._loadFile(scene, fileOrUrl, function (data) {
471
- _this._validate(scene, data, rootUrl, fileName);
472
- _this._unpackBinaryAsync(new DataReader({
473
- readAsync: function (byteOffset, byteLength) { return readAsync(data, byteOffset, byteLength); },
474
- byteLength: data.byteLength,
475
- })).then(function (loaderData) {
476
- onSuccess(loaderData);
477
- }, onError ? function (error) { return onError(undefined, error); } : undefined);
478
- }, true, onError);
479
- }
480
- return this._loadFile(scene, fileOrUrl, function (data) {
481
- _this._validate(scene, data, rootUrl, fileName);
482
- onSuccess({ json: _this._parseJson(data) });
483
- }, useArrayBuffer, onError);
484
- };
485
- /**
486
- * @param meshesNames
487
- * @param scene
488
- * @param data
489
- * @param rootUrl
490
- * @param onProgress
491
- * @param fileName
492
- * @hidden
493
- */
494
- GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
495
- var _this = this;
496
- return Promise.resolve().then(function () {
497
- _this.onParsedObservable.notifyObservers(data);
498
- _this.onParsedObservable.clear();
499
- _this._log("Loading ".concat(fileName || ""));
500
- _this._loader = _this._getLoader(data);
501
- return _this._loader.importMeshAsync(meshesNames, scene, null, data, rootUrl, onProgress, fileName);
502
- });
503
- };
504
- /**
505
- * @param scene
506
- * @param data
507
- * @param rootUrl
508
- * @param onProgress
509
- * @param fileName
510
- * @hidden
511
- */
512
- GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
513
- var _this = this;
514
- return Promise.resolve().then(function () {
515
- _this.onParsedObservable.notifyObservers(data);
516
- _this.onParsedObservable.clear();
517
- _this._log("Loading ".concat(fileName || ""));
518
- _this._loader = _this._getLoader(data);
519
- return _this._loader.loadAsync(scene, data, rootUrl, onProgress, fileName);
520
- });
521
- };
522
- /**
523
- * @param scene
524
- * @param data
525
- * @param rootUrl
526
- * @param onProgress
527
- * @param fileName
528
- * @hidden
529
- */
530
- GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
531
- var _this = this;
532
- return Promise.resolve().then(function () {
533
- _this.onParsedObservable.notifyObservers(data);
534
- _this.onParsedObservable.clear();
535
- _this._log("Loading ".concat(fileName || ""));
536
- _this._loader = _this._getLoader(data);
537
- // Prepare the asset container.
538
- var container = new AssetContainer(scene);
539
- // Get materials/textures when loading to add to container
540
- var materials = [];
541
- _this.onMaterialLoadedObservable.add(function (material) {
542
- materials.push(material);
543
- });
544
- var textures = [];
545
- _this.onTextureLoadedObservable.add(function (texture) {
546
- textures.push(texture);
547
- });
548
- var cameras = [];
549
- _this.onCameraLoadedObservable.add(function (camera) {
550
- cameras.push(camera);
551
- });
552
- return _this._loader.importMeshAsync(null, scene, container, data, rootUrl, onProgress, fileName).then(function (result) {
553
- Array.prototype.push.apply(container.geometries, result.geometries);
554
- Array.prototype.push.apply(container.meshes, result.meshes);
555
- Array.prototype.push.apply(container.particleSystems, result.particleSystems);
556
- Array.prototype.push.apply(container.skeletons, result.skeletons);
557
- Array.prototype.push.apply(container.animationGroups, result.animationGroups);
558
- Array.prototype.push.apply(container.materials, materials);
559
- Array.prototype.push.apply(container.textures, textures);
560
- Array.prototype.push.apply(container.lights, result.lights);
561
- Array.prototype.push.apply(container.transformNodes, result.transformNodes);
562
- Array.prototype.push.apply(container.cameras, cameras);
563
- return container;
564
- });
565
- });
566
- };
567
- /**
568
- * @param data
569
- * @hidden
570
- */
571
- GLTFFileLoader.prototype.canDirectLoad = function (data) {
572
- return ((data.indexOf("asset") !== -1 && data.indexOf("version") !== -1) ||
573
- StringTools.StartsWith(data, "data:base64," + GLTFFileLoader._MagicBase64Encoded) || // this is technically incorrect, but will continue to support for backcompat.
574
- StringTools.StartsWith(data, "data:;base64," + GLTFFileLoader._MagicBase64Encoded) ||
575
- StringTools.StartsWith(data, "data:application/octet-stream;base64," + GLTFFileLoader._MagicBase64Encoded) ||
576
- StringTools.StartsWith(data, "data:model/gltf-binary;base64," + GLTFFileLoader._MagicBase64Encoded));
577
- };
578
- /**
579
- * @param scene
580
- * @param data
581
- * @hidden
582
- */
583
- GLTFFileLoader.prototype.directLoad = function (scene, data) {
584
- if (StringTools.StartsWith(data, "base64," + GLTFFileLoader._MagicBase64Encoded) || // this is technically incorrect, but will continue to support for backcompat.
585
- StringTools.StartsWith(data, ";base64," + GLTFFileLoader._MagicBase64Encoded) ||
586
- StringTools.StartsWith(data, "application/octet-stream;base64," + GLTFFileLoader._MagicBase64Encoded) ||
587
- StringTools.StartsWith(data, "model/gltf-binary;base64," + GLTFFileLoader._MagicBase64Encoded)) {
588
- var arrayBuffer_1 = DecodeBase64UrlToBinary(data);
589
- this._validate(scene, arrayBuffer_1);
590
- return this._unpackBinaryAsync(new DataReader({
591
- readAsync: function (byteOffset, byteLength) { return readAsync(arrayBuffer_1, byteOffset, byteLength); },
592
- byteLength: arrayBuffer_1.byteLength,
593
- }));
594
- }
595
- this._validate(scene, data);
596
- return Promise.resolve({ json: this._parseJson(data) });
597
- };
598
- /** @hidden */
599
- GLTFFileLoader.prototype.createPlugin = function () {
600
- return new GLTFFileLoader();
601
- };
602
- Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
603
- /**
604
- * The loader state or null if the loader is not active.
605
- */
606
- get: function () {
607
- return this._state;
608
- },
609
- enumerable: false,
610
- configurable: true
611
- });
612
- /**
613
- * Returns a promise that resolves when the asset is completely loaded.
614
- * @returns a promise that resolves when the asset is completely loaded.
615
- */
616
- GLTFFileLoader.prototype.whenCompleteAsync = function () {
617
- var _this = this;
618
- return new Promise(function (resolve, reject) {
619
- _this.onCompleteObservable.addOnce(function () {
620
- resolve();
621
- });
622
- _this.onErrorObservable.addOnce(function (reason) {
623
- reject(reason);
624
- });
625
- });
626
- };
627
- /**
628
- * @param state
629
- * @hidden
630
- */
631
- GLTFFileLoader.prototype._setState = function (state) {
632
- if (this._state === state) {
633
- return;
634
- }
635
- this._state = state;
636
- this.onLoaderStateChangedObservable.notifyObservers(this._state);
637
- this._log(GLTFLoaderState[this._state]);
638
- };
639
- /**
640
- * @param scene
641
- * @param fileOrUrl
642
- * @param onSuccess
643
- * @param useArrayBuffer
644
- * @param onError
645
- * @param onOpened
646
- * @hidden
647
- */
648
- GLTFFileLoader.prototype._loadFile = function (scene, fileOrUrl, onSuccess, useArrayBuffer, onError, onOpened) {
649
- var _this = this;
650
- var request = scene._loadFile(fileOrUrl, onSuccess, function (event) {
651
- _this._onProgress(event, request);
652
- }, true, useArrayBuffer, onError, onOpened);
653
- request.onCompleteObservable.add(function (request) {
654
- _this._requests.splice(_this._requests.indexOf(request), 1);
655
- });
656
- this._requests.push(request);
657
- return request;
658
- };
659
- GLTFFileLoader.prototype._onProgress = function (event, request) {
660
- if (!this._progressCallback) {
661
- return;
662
- }
663
- request._lengthComputable = event.lengthComputable;
664
- request._loaded = event.loaded;
665
- request._total = event.total;
666
- var lengthComputable = true;
667
- var loaded = 0;
668
- var total = 0;
669
- for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
670
- var request_1 = _a[_i];
671
- if (request_1._lengthComputable === undefined || request_1._loaded === undefined || request_1._total === undefined) {
672
- return;
673
- }
674
- lengthComputable = lengthComputable && request_1._lengthComputable;
675
- loaded += request_1._loaded;
676
- total += request_1._total;
677
- }
678
- this._progressCallback({
679
- lengthComputable: lengthComputable,
680
- loaded: loaded,
681
- total: lengthComputable ? total : 0,
682
- });
683
- };
684
- GLTFFileLoader.prototype._validate = function (scene, data, rootUrl, fileName) {
685
- var _this = this;
686
- if (rootUrl === void 0) { rootUrl = ""; }
687
- if (fileName === void 0) { fileName = ""; }
688
- if (!this.validate) {
689
- return;
690
- }
691
- this._startPerformanceCounter("Validate JSON");
692
- GLTFValidation.ValidateAsync(data, rootUrl, fileName, function (uri) {
693
- return _this.preprocessUrlAsync(rootUrl + uri).then(function (url) { return scene._loadFileAsync(url, undefined, true, true); });
694
- }).then(function (result) {
695
- _this._endPerformanceCounter("Validate JSON");
696
- _this.onValidatedObservable.notifyObservers(result);
697
- _this.onValidatedObservable.clear();
698
- }, function (reason) {
699
- _this._endPerformanceCounter("Validate JSON");
700
- Tools.Warn("Failed to validate: ".concat(reason.message));
701
- _this.onValidatedObservable.clear();
702
- });
703
- };
704
- GLTFFileLoader.prototype._getLoader = function (loaderData) {
705
- var asset = loaderData.json.asset || {};
706
- this._log("Asset version: ".concat(asset.version));
707
- asset.minVersion && this._log("Asset minimum version: ".concat(asset.minVersion));
708
- asset.generator && this._log("Asset generator: ".concat(asset.generator));
709
- var version = GLTFFileLoader._parseVersion(asset.version);
710
- if (!version) {
711
- throw new Error("Invalid version: " + asset.version);
712
- }
713
- if (asset.minVersion !== undefined) {
714
- var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
715
- if (!minVersion) {
716
- throw new Error("Invalid minimum version: " + asset.minVersion);
717
- }
718
- if (GLTFFileLoader._compareVersion(minVersion, { major: 2, minor: 0 }) > 0) {
719
- throw new Error("Incompatible minimum version: " + asset.minVersion);
720
- }
721
- }
722
- var createLoaders = {
723
- 1: GLTFFileLoader._CreateGLTF1Loader,
724
- 2: GLTFFileLoader._CreateGLTF2Loader,
725
- };
726
- var createLoader = createLoaders[version.major];
727
- if (!createLoader) {
728
- throw new Error("Unsupported version: " + asset.version);
729
- }
730
- return createLoader(this);
731
- };
732
- GLTFFileLoader.prototype._parseJson = function (json) {
733
- this._startPerformanceCounter("Parse JSON");
734
- this._log("JSON length: ".concat(json.length));
735
- var parsed = JSON.parse(json);
736
- this._endPerformanceCounter("Parse JSON");
737
- return parsed;
738
- };
739
- GLTFFileLoader.prototype._unpackBinaryAsync = function (dataReader) {
740
- var _this = this;
741
- this._startPerformanceCounter("Unpack Binary");
742
- // Read magic + version + length + json length + json format
743
- return dataReader.loadAsync(20).then(function () {
744
- var Binary = {
745
- Magic: 0x46546c67,
746
- };
747
- var magic = dataReader.readUint32();
748
- if (magic !== Binary.Magic) {
749
- throw new RuntimeError("Unexpected magic: " + magic, ErrorCodes.GLTFLoaderUnexpectedMagicError);
750
- }
751
- var version = dataReader.readUint32();
752
- if (_this.loggingEnabled) {
753
- _this._log("Binary version: ".concat(version));
754
- }
755
- var length = dataReader.readUint32();
756
- if (dataReader.buffer.byteLength !== 0 && length !== dataReader.buffer.byteLength) {
757
- throw new Error("Length in header does not match actual data length: ".concat(length, " != ").concat(dataReader.buffer.byteLength));
758
- }
759
- var unpacked;
760
- switch (version) {
761
- case 1: {
762
- unpacked = _this._unpackBinaryV1Async(dataReader, length);
763
- break;
764
- }
765
- case 2: {
766
- unpacked = _this._unpackBinaryV2Async(dataReader, length);
767
- break;
768
- }
769
- default: {
770
- throw new Error("Unsupported version: " + version);
771
- }
772
- }
773
- _this._endPerformanceCounter("Unpack Binary");
774
- return unpacked;
775
- });
776
- };
777
- GLTFFileLoader.prototype._unpackBinaryV1Async = function (dataReader, length) {
778
- var ContentFormat = {
779
- JSON: 0,
780
- };
781
- var contentLength = dataReader.readUint32();
782
- var contentFormat = dataReader.readUint32();
783
- if (contentFormat !== ContentFormat.JSON) {
784
- throw new Error("Unexpected content format: ".concat(contentFormat));
785
- }
786
- var bodyLength = length - dataReader.byteOffset;
787
- var data = { json: this._parseJson(dataReader.readString(contentLength)), bin: null };
788
- if (bodyLength !== 0) {
789
- var startByteOffset_1 = dataReader.byteOffset;
790
- data.bin = {
791
- readAsync: function (byteOffset, byteLength) { return dataReader.buffer.readAsync(startByteOffset_1 + byteOffset, byteLength); },
792
- byteLength: bodyLength,
793
- };
794
- }
795
- return Promise.resolve(data);
796
- };
797
- GLTFFileLoader.prototype._unpackBinaryV2Async = function (dataReader, length) {
798
- var _this = this;
799
- var ChunkFormat = {
800
- JSON: 0x4e4f534a,
801
- BIN: 0x004e4942,
802
- };
803
- // Read the JSON chunk header.
804
- var chunkLength = dataReader.readUint32();
805
- var chunkFormat = dataReader.readUint32();
806
- if (chunkFormat !== ChunkFormat.JSON) {
807
- throw new Error("First chunk format is not JSON");
808
- }
809
- // Bail if there are no other chunks.
810
- if (dataReader.byteOffset + chunkLength === length) {
811
- return dataReader.loadAsync(chunkLength).then(function () {
812
- return { json: _this._parseJson(dataReader.readString(chunkLength)), bin: null };
813
- });
814
- }
815
- // Read the JSON chunk and the length and type of the next chunk.
816
- return dataReader.loadAsync(chunkLength + 8).then(function () {
817
- var data = { json: _this._parseJson(dataReader.readString(chunkLength)), bin: null };
818
- var readAsync = function () {
819
- var chunkLength = dataReader.readUint32();
820
- var chunkFormat = dataReader.readUint32();
821
- switch (chunkFormat) {
822
- case ChunkFormat.JSON: {
823
- throw new Error("Unexpected JSON chunk");
824
- }
825
- case ChunkFormat.BIN: {
826
- var startByteOffset_2 = dataReader.byteOffset;
827
- data.bin = {
828
- readAsync: function (byteOffset, byteLength) { return dataReader.buffer.readAsync(startByteOffset_2 + byteOffset, byteLength); },
829
- byteLength: chunkLength,
830
- };
831
- dataReader.skipBytes(chunkLength);
832
- break;
833
- }
834
- default: {
835
- // ignore unrecognized chunkFormat
836
- dataReader.skipBytes(chunkLength);
837
- break;
838
- }
839
- }
840
- if (dataReader.byteOffset !== length) {
841
- return dataReader.loadAsync(8).then(readAsync);
842
- }
843
- return Promise.resolve(data);
844
- };
845
- return readAsync();
846
- });
847
- };
848
- GLTFFileLoader._parseVersion = function (version) {
849
- if (version === "1.0" || version === "1.0.1") {
850
- return {
851
- major: 1,
852
- minor: 0,
853
- };
854
- }
855
- var match = (version + "").match(/^(\d+)\.(\d+)/);
856
- if (!match) {
857
- return null;
858
- }
859
- return {
860
- major: parseInt(match[1]),
861
- minor: parseInt(match[2]),
862
- };
863
- };
864
- GLTFFileLoader._compareVersion = function (a, b) {
865
- if (a.major > b.major) {
866
- return 1;
867
- }
868
- if (a.major < b.major) {
869
- return -1;
870
- }
871
- if (a.minor > b.minor) {
872
- return 1;
873
- }
874
- if (a.minor < b.minor) {
875
- return -1;
876
- }
877
- return 0;
878
- };
879
- /**
880
- * @param message
881
- * @hidden
882
- */
883
- GLTFFileLoader.prototype._logOpen = function (message) {
884
- this._log(message);
885
- this._logIndentLevel++;
886
- };
887
- /** @hidden */
888
- GLTFFileLoader.prototype._logClose = function () {
889
- --this._logIndentLevel;
890
- };
891
- GLTFFileLoader.prototype._logEnabled = function (message) {
892
- var spaces = GLTFFileLoader._logSpaces.substr(0, this._logIndentLevel * 2);
893
- Logger.Log("".concat(spaces).concat(message));
894
- };
895
- GLTFFileLoader.prototype._logDisabled = function (message) { };
896
- GLTFFileLoader.prototype._startPerformanceCounterEnabled = function (counterName) {
897
- Tools.StartPerformanceCounter(counterName);
898
- };
899
- GLTFFileLoader.prototype._startPerformanceCounterDisabled = function (counterName) { };
900
- GLTFFileLoader.prototype._endPerformanceCounterEnabled = function (counterName) {
901
- Tools.EndPerformanceCounter(counterName);
902
- };
903
- GLTFFileLoader.prototype._endPerformanceCounterDisabled = function (counterName) { };
904
- // ----------
905
- // V1 options
906
- // ----------
907
- /**
908
- * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders.
909
- * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
910
- * Defaults to true.
911
- * @hidden
912
- */
913
- GLTFFileLoader.IncrementalLoading = true;
914
- /**
915
- * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
916
- * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
917
- * @hidden
918
- */
919
- GLTFFileLoader.HomogeneousCoordinates = false;
920
- GLTFFileLoader._MagicBase64Encoded = "Z2xURg"; // "glTF" base64 encoded (without the quotes!)
921
- GLTFFileLoader._logSpaces = " ";
922
- return GLTFFileLoader;
923
- }());
924
- export { GLTFFileLoader };
925
- if (SceneLoader) {
926
- SceneLoader.RegisterPlugin(new GLTFFileLoader());
927
- }
1
+ import { Observable } from "@babylonjs/core/Misc/observable.js";
2
+ import { Tools } from "@babylonjs/core/Misc/tools.js";
3
+ import { SceneLoader, } from "@babylonjs/core/Loading/sceneLoader.js";
4
+ import { AssetContainer } from "@babylonjs/core/assetContainer.js";
5
+ import { Logger } from "@babylonjs/core/Misc/logger.js";
6
+ import { DataReader } from "@babylonjs/core/Misc/dataReader.js";
7
+ import { GLTFValidation } from "./glTFValidation";
8
+ import { DecodeBase64UrlToBinary } from "@babylonjs/core/Misc/fileTools.js";
9
+ import { StringTools } from "@babylonjs/core/Misc/stringTools.js";
10
+ import { RuntimeError, ErrorCodes } from "@babylonjs/core/Misc/error.js";
11
+ function readAsync(arrayBuffer, byteOffset, byteLength) {
12
+ try {
13
+ return Promise.resolve(new Uint8Array(arrayBuffer, byteOffset, byteLength));
14
+ }
15
+ catch (e) {
16
+ return Promise.reject(e);
17
+ }
18
+ }
19
+ /**
20
+ * Mode that determines the coordinate system to use.
21
+ */
22
+ export var GLTFLoaderCoordinateSystemMode;
23
+ (function (GLTFLoaderCoordinateSystemMode) {
24
+ /**
25
+ * Automatically convert the glTF right-handed data to the appropriate system based on the current coordinate system mode of the scene.
26
+ */
27
+ GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["AUTO"] = 0] = "AUTO";
28
+ /**
29
+ * Sets the useRightHandedSystem flag on the scene.
30
+ */
31
+ GLTFLoaderCoordinateSystemMode[GLTFLoaderCoordinateSystemMode["FORCE_RIGHT_HANDED"] = 1] = "FORCE_RIGHT_HANDED";
32
+ })(GLTFLoaderCoordinateSystemMode || (GLTFLoaderCoordinateSystemMode = {}));
33
+ /**
34
+ * Mode that determines what animations will start.
35
+ */
36
+ export var GLTFLoaderAnimationStartMode;
37
+ (function (GLTFLoaderAnimationStartMode) {
38
+ /**
39
+ * No animation will start.
40
+ */
41
+ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["NONE"] = 0] = "NONE";
42
+ /**
43
+ * The first animation will start.
44
+ */
45
+ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["FIRST"] = 1] = "FIRST";
46
+ /**
47
+ * All animations will start.
48
+ */
49
+ GLTFLoaderAnimationStartMode[GLTFLoaderAnimationStartMode["ALL"] = 2] = "ALL";
50
+ })(GLTFLoaderAnimationStartMode || (GLTFLoaderAnimationStartMode = {}));
51
+ /**
52
+ * Loader state.
53
+ */
54
+ export var GLTFLoaderState;
55
+ (function (GLTFLoaderState) {
56
+ /**
57
+ * The asset is loading.
58
+ */
59
+ GLTFLoaderState[GLTFLoaderState["LOADING"] = 0] = "LOADING";
60
+ /**
61
+ * The asset is ready for rendering.
62
+ */
63
+ GLTFLoaderState[GLTFLoaderState["READY"] = 1] = "READY";
64
+ /**
65
+ * The asset is completely loaded.
66
+ */
67
+ GLTFLoaderState[GLTFLoaderState["COMPLETE"] = 2] = "COMPLETE";
68
+ })(GLTFLoaderState || (GLTFLoaderState = {}));
69
+ /**
70
+ * File loader for loading glTF files into a scene.
71
+ */
72
+ var GLTFFileLoader = /** @class */ (function () {
73
+ function GLTFFileLoader() {
74
+ // --------------
75
+ // Common options
76
+ // --------------
77
+ /**
78
+ * Raised when the asset has been parsed
79
+ */
80
+ this.onParsedObservable = new Observable();
81
+ // ----------
82
+ // V2 options
83
+ // ----------
84
+ /**
85
+ * The coordinate system mode. Defaults to AUTO.
86
+ */
87
+ this.coordinateSystemMode = GLTFLoaderCoordinateSystemMode.AUTO;
88
+ /**
89
+ * The animation start mode. Defaults to FIRST.
90
+ */
91
+ this.animationStartMode = GLTFLoaderAnimationStartMode.FIRST;
92
+ /**
93
+ * Defines if the loader should compile materials before raising the success callback. Defaults to false.
94
+ */
95
+ this.compileMaterials = false;
96
+ /**
97
+ * Defines if the loader should also compile materials with clip planes. Defaults to false.
98
+ */
99
+ this.useClipPlane = false;
100
+ /**
101
+ * Defines if the loader should compile shadow generators before raising the success callback. Defaults to false.
102
+ */
103
+ this.compileShadowGenerators = false;
104
+ /**
105
+ * Defines if the Alpha blended materials are only applied as coverage.
106
+ * If false, (default) The luminance of each pixel will reduce its opacity to simulate the behaviour of most physical materials.
107
+ * If true, no extra effects are applied to transparent pixels.
108
+ */
109
+ this.transparencyAsCoverage = false;
110
+ /**
111
+ * Defines if the loader should use range requests when load binary glTF files from HTTP.
112
+ * Enabling will disable offline support and glTF validator.
113
+ * Defaults to false.
114
+ */
115
+ this.useRangeRequests = false;
116
+ /**
117
+ * Defines if the loader should create instances when multiple glTF nodes point to the same glTF mesh. Defaults to true.
118
+ */
119
+ this.createInstances = true;
120
+ /**
121
+ * Defines if the loader should always compute the bounding boxes of meshes and not use the min/max values from the position accessor. Defaults to false.
122
+ */
123
+ this.alwaysComputeBoundingBox = false;
124
+ /**
125
+ * If true, load all materials defined in the file, even if not used by any mesh. Defaults to false.
126
+ */
127
+ this.loadAllMaterials = false;
128
+ /**
129
+ * If true, load only the materials defined in the file. Defaults to false.
130
+ */
131
+ this.loadOnlyMaterials = false;
132
+ /**
133
+ * If true, do not load any materials defined in the file. Defaults to false.
134
+ */
135
+ this.skipMaterials = false;
136
+ /**
137
+ * If true, load the color (gamma encoded) textures into sRGB buffers (if supported by the GPU), which will yield more accurate results when sampling the texture. Defaults to true.
138
+ */
139
+ this.useSRGBBuffers = true;
140
+ /**
141
+ * When loading glTF animations, which are defined in seconds, target them to this FPS. Defaults to 60.
142
+ */
143
+ this.targetFps = 60;
144
+ /**
145
+ * Defines if the loader should always compute the nearest common ancestor of the skeleton joints instead of using `skin.skeleton`. Defaults to false.
146
+ * Set this to true if loading assets with invalid `skin.skeleton` values.
147
+ */
148
+ this.alwaysComputeSkeletonRootNode = false;
149
+ /**
150
+ * Function called before loading a url referenced by the asset.
151
+ * @param url
152
+ */
153
+ this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
154
+ /**
155
+ * Observable raised when the loader creates a mesh after parsing the glTF properties of the mesh.
156
+ * Note that the observable is raised as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...)
157
+ */
158
+ this.onMeshLoadedObservable = new Observable();
159
+ /**
160
+ * Observable raised when the loader creates a texture after parsing the glTF properties of the texture.
161
+ */
162
+ this.onTextureLoadedObservable = new Observable();
163
+ /**
164
+ * Observable raised when the loader creates a material after parsing the glTF properties of the material.
165
+ */
166
+ this.onMaterialLoadedObservable = new Observable();
167
+ /**
168
+ * Observable raised when the loader creates a camera after parsing the glTF properties of the camera.
169
+ */
170
+ this.onCameraLoadedObservable = new Observable();
171
+ /**
172
+ * Observable raised when the asset is completely loaded, immediately before the loader is disposed.
173
+ * For assets with LODs, raised when all of the LODs are complete.
174
+ * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
175
+ */
176
+ this.onCompleteObservable = new Observable();
177
+ /**
178
+ * Observable raised when an error occurs.
179
+ */
180
+ this.onErrorObservable = new Observable();
181
+ /**
182
+ * Observable raised after the loader is disposed.
183
+ */
184
+ this.onDisposeObservable = new Observable();
185
+ /**
186
+ * Observable raised after a loader extension is created.
187
+ * Set additional options for a loader extension in this event.
188
+ */
189
+ this.onExtensionLoadedObservable = new Observable();
190
+ /**
191
+ * Defines if the loader should validate the asset.
192
+ */
193
+ this.validate = false;
194
+ /**
195
+ * Observable raised after validation when validate is set to true. The event data is the result of the validation.
196
+ */
197
+ this.onValidatedObservable = new Observable();
198
+ this._loader = null;
199
+ this._state = null;
200
+ this._requests = new Array();
201
+ /**
202
+ * Name of the loader ("gltf")
203
+ */
204
+ this.name = "gltf";
205
+ /** @hidden */
206
+ this.extensions = {
207
+ ".gltf": { isBinary: false },
208
+ ".glb": { isBinary: true },
209
+ };
210
+ /**
211
+ * Observable raised when the loader state changes.
212
+ */
213
+ this.onLoaderStateChangedObservable = new Observable();
214
+ this._logIndentLevel = 0;
215
+ this._loggingEnabled = false;
216
+ /** @hidden */
217
+ this._log = this._logDisabled;
218
+ this._capturePerformanceCounters = false;
219
+ /** @hidden */
220
+ this._startPerformanceCounter = this._startPerformanceCounterDisabled;
221
+ /** @hidden */
222
+ this._endPerformanceCounter = this._endPerformanceCounterDisabled;
223
+ }
224
+ Object.defineProperty(GLTFFileLoader.prototype, "onParsed", {
225
+ /**
226
+ * Raised when the asset has been parsed
227
+ */
228
+ set: function (callback) {
229
+ if (this._onParsedObserver) {
230
+ this.onParsedObservable.remove(this._onParsedObserver);
231
+ }
232
+ this._onParsedObserver = this.onParsedObservable.add(callback);
233
+ },
234
+ enumerable: false,
235
+ configurable: true
236
+ });
237
+ Object.defineProperty(GLTFFileLoader.prototype, "onMeshLoaded", {
238
+ /**
239
+ * Callback raised when the loader creates a mesh after parsing the glTF properties of the mesh.
240
+ * Note that the callback is called as soon as the mesh object is created, meaning some data may not have been setup yet for this mesh (vertex data, morph targets, material, ...)
241
+ */
242
+ set: function (callback) {
243
+ if (this._onMeshLoadedObserver) {
244
+ this.onMeshLoadedObservable.remove(this._onMeshLoadedObserver);
245
+ }
246
+ this._onMeshLoadedObserver = this.onMeshLoadedObservable.add(callback);
247
+ },
248
+ enumerable: false,
249
+ configurable: true
250
+ });
251
+ Object.defineProperty(GLTFFileLoader.prototype, "onTextureLoaded", {
252
+ /**
253
+ * Callback raised when the loader creates a texture after parsing the glTF properties of the texture.
254
+ */
255
+ set: function (callback) {
256
+ if (this._onTextureLoadedObserver) {
257
+ this.onTextureLoadedObservable.remove(this._onTextureLoadedObserver);
258
+ }
259
+ this._onTextureLoadedObserver = this.onTextureLoadedObservable.add(callback);
260
+ },
261
+ enumerable: false,
262
+ configurable: true
263
+ });
264
+ Object.defineProperty(GLTFFileLoader.prototype, "onMaterialLoaded", {
265
+ /**
266
+ * Callback raised when the loader creates a material after parsing the glTF properties of the material.
267
+ */
268
+ set: function (callback) {
269
+ if (this._onMaterialLoadedObserver) {
270
+ this.onMaterialLoadedObservable.remove(this._onMaterialLoadedObserver);
271
+ }
272
+ this._onMaterialLoadedObserver = this.onMaterialLoadedObservable.add(callback);
273
+ },
274
+ enumerable: false,
275
+ configurable: true
276
+ });
277
+ Object.defineProperty(GLTFFileLoader.prototype, "onCameraLoaded", {
278
+ /**
279
+ * Callback raised when the loader creates a camera after parsing the glTF properties of the camera.
280
+ */
281
+ set: function (callback) {
282
+ if (this._onCameraLoadedObserver) {
283
+ this.onCameraLoadedObservable.remove(this._onCameraLoadedObserver);
284
+ }
285
+ this._onCameraLoadedObserver = this.onCameraLoadedObservable.add(callback);
286
+ },
287
+ enumerable: false,
288
+ configurable: true
289
+ });
290
+ Object.defineProperty(GLTFFileLoader.prototype, "onComplete", {
291
+ /**
292
+ * Callback raised when the asset is completely loaded, immediately before the loader is disposed.
293
+ * For assets with LODs, raised when all of the LODs are complete.
294
+ * For assets without LODs, raised when the model is complete, immediately after the loader resolves the returned promise.
295
+ */
296
+ set: function (callback) {
297
+ if (this._onCompleteObserver) {
298
+ this.onCompleteObservable.remove(this._onCompleteObserver);
299
+ }
300
+ this._onCompleteObserver = this.onCompleteObservable.add(callback);
301
+ },
302
+ enumerable: false,
303
+ configurable: true
304
+ });
305
+ Object.defineProperty(GLTFFileLoader.prototype, "onError", {
306
+ /**
307
+ * Callback raised when an error occurs.
308
+ */
309
+ set: function (callback) {
310
+ if (this._onErrorObserver) {
311
+ this.onErrorObservable.remove(this._onErrorObserver);
312
+ }
313
+ this._onErrorObserver = this.onErrorObservable.add(callback);
314
+ },
315
+ enumerable: false,
316
+ configurable: true
317
+ });
318
+ Object.defineProperty(GLTFFileLoader.prototype, "onDispose", {
319
+ /**
320
+ * Callback raised after the loader is disposed.
321
+ */
322
+ set: function (callback) {
323
+ if (this._onDisposeObserver) {
324
+ this.onDisposeObservable.remove(this._onDisposeObserver);
325
+ }
326
+ this._onDisposeObserver = this.onDisposeObservable.add(callback);
327
+ },
328
+ enumerable: false,
329
+ configurable: true
330
+ });
331
+ Object.defineProperty(GLTFFileLoader.prototype, "onExtensionLoaded", {
332
+ /**
333
+ * Callback raised after a loader extension is created.
334
+ */
335
+ set: function (callback) {
336
+ if (this._onExtensionLoadedObserver) {
337
+ this.onExtensionLoadedObservable.remove(this._onExtensionLoadedObserver);
338
+ }
339
+ this._onExtensionLoadedObserver = this.onExtensionLoadedObservable.add(callback);
340
+ },
341
+ enumerable: false,
342
+ configurable: true
343
+ });
344
+ Object.defineProperty(GLTFFileLoader.prototype, "loggingEnabled", {
345
+ /**
346
+ * Defines if the loader logging is enabled.
347
+ */
348
+ get: function () {
349
+ return this._loggingEnabled;
350
+ },
351
+ set: function (value) {
352
+ if (this._loggingEnabled === value) {
353
+ return;
354
+ }
355
+ this._loggingEnabled = value;
356
+ if (this._loggingEnabled) {
357
+ this._log = this._logEnabled;
358
+ }
359
+ else {
360
+ this._log = this._logDisabled;
361
+ }
362
+ },
363
+ enumerable: false,
364
+ configurable: true
365
+ });
366
+ Object.defineProperty(GLTFFileLoader.prototype, "capturePerformanceCounters", {
367
+ /**
368
+ * Defines if the loader should capture performance counters.
369
+ */
370
+ get: function () {
371
+ return this._capturePerformanceCounters;
372
+ },
373
+ set: function (value) {
374
+ if (this._capturePerformanceCounters === value) {
375
+ return;
376
+ }
377
+ this._capturePerformanceCounters = value;
378
+ if (this._capturePerformanceCounters) {
379
+ this._startPerformanceCounter = this._startPerformanceCounterEnabled;
380
+ this._endPerformanceCounter = this._endPerformanceCounterEnabled;
381
+ }
382
+ else {
383
+ this._startPerformanceCounter = this._startPerformanceCounterDisabled;
384
+ this._endPerformanceCounter = this._endPerformanceCounterDisabled;
385
+ }
386
+ },
387
+ enumerable: false,
388
+ configurable: true
389
+ });
390
+ Object.defineProperty(GLTFFileLoader.prototype, "onValidated", {
391
+ /**
392
+ * Callback raised after a loader extension is created.
393
+ */
394
+ set: function (callback) {
395
+ if (this._onValidatedObserver) {
396
+ this.onValidatedObservable.remove(this._onValidatedObserver);
397
+ }
398
+ this._onValidatedObserver = this.onValidatedObservable.add(callback);
399
+ },
400
+ enumerable: false,
401
+ configurable: true
402
+ });
403
+ /**
404
+ * Disposes the loader, releases resources during load, and cancels any outstanding requests.
405
+ */
406
+ GLTFFileLoader.prototype.dispose = function () {
407
+ if (this._loader) {
408
+ this._loader.dispose();
409
+ this._loader = null;
410
+ }
411
+ for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
412
+ var request = _a[_i];
413
+ request.abort();
414
+ }
415
+ this._requests.length = 0;
416
+ delete this._progressCallback;
417
+ this.preprocessUrlAsync = function (url) { return Promise.resolve(url); };
418
+ this.onMeshLoadedObservable.clear();
419
+ this.onTextureLoadedObservable.clear();
420
+ this.onMaterialLoadedObservable.clear();
421
+ this.onCameraLoadedObservable.clear();
422
+ this.onCompleteObservable.clear();
423
+ this.onExtensionLoadedObservable.clear();
424
+ this.onDisposeObservable.notifyObservers(undefined);
425
+ this.onDisposeObservable.clear();
426
+ };
427
+ /**
428
+ * @param scene
429
+ * @param fileOrUrl
430
+ * @param onSuccess
431
+ * @param onProgress
432
+ * @param useArrayBuffer
433
+ * @param onError
434
+ * @hidden
435
+ */
436
+ GLTFFileLoader.prototype.loadFile = function (scene, fileOrUrl, onSuccess, onProgress, useArrayBuffer, onError) {
437
+ var _this = this;
438
+ this._progressCallback = onProgress;
439
+ var rootUrl = fileOrUrl.name ? "file:" : Tools.GetFolderPath(fileOrUrl);
440
+ var fileName = fileOrUrl.name || Tools.GetFilename(fileOrUrl);
441
+ if (useArrayBuffer) {
442
+ if (this.useRangeRequests) {
443
+ if (this.validate) {
444
+ Logger.Warn("glTF validation is not supported when range requests are enabled");
445
+ }
446
+ var fileRequest_1 = {
447
+ abort: function () { },
448
+ onCompleteObservable: new Observable(),
449
+ };
450
+ var dataBuffer = {
451
+ readAsync: function (byteOffset, byteLength) {
452
+ return new Promise(function (resolve, reject) {
453
+ _this._loadFile(scene, fileOrUrl, function (data) {
454
+ resolve(new Uint8Array(data));
455
+ }, true, function (error) {
456
+ reject(error);
457
+ }, function (webRequest) {
458
+ webRequest.setRequestHeader("Range", "bytes=".concat(byteOffset, "-").concat(byteOffset + byteLength - 1));
459
+ });
460
+ });
461
+ },
462
+ byteLength: 0,
463
+ };
464
+ this._unpackBinaryAsync(new DataReader(dataBuffer)).then(function (loaderData) {
465
+ fileRequest_1.onCompleteObservable.notifyObservers(fileRequest_1);
466
+ onSuccess(loaderData);
467
+ }, onError ? function (error) { return onError(undefined, error); } : undefined);
468
+ return fileRequest_1;
469
+ }
470
+ return this._loadFile(scene, fileOrUrl, function (data) {
471
+ _this._validate(scene, data, rootUrl, fileName);
472
+ _this._unpackBinaryAsync(new DataReader({
473
+ readAsync: function (byteOffset, byteLength) { return readAsync(data, byteOffset, byteLength); },
474
+ byteLength: data.byteLength,
475
+ })).then(function (loaderData) {
476
+ onSuccess(loaderData);
477
+ }, onError ? function (error) { return onError(undefined, error); } : undefined);
478
+ }, true, onError);
479
+ }
480
+ return this._loadFile(scene, fileOrUrl, function (data) {
481
+ _this._validate(scene, data, rootUrl, fileName);
482
+ onSuccess({ json: _this._parseJson(data) });
483
+ }, useArrayBuffer, onError);
484
+ };
485
+ /**
486
+ * @param meshesNames
487
+ * @param scene
488
+ * @param data
489
+ * @param rootUrl
490
+ * @param onProgress
491
+ * @param fileName
492
+ * @hidden
493
+ */
494
+ GLTFFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
495
+ var _this = this;
496
+ return Promise.resolve().then(function () {
497
+ _this.onParsedObservable.notifyObservers(data);
498
+ _this.onParsedObservable.clear();
499
+ _this._log("Loading ".concat(fileName || ""));
500
+ _this._loader = _this._getLoader(data);
501
+ return _this._loader.importMeshAsync(meshesNames, scene, null, data, rootUrl, onProgress, fileName);
502
+ });
503
+ };
504
+ /**
505
+ * @param scene
506
+ * @param data
507
+ * @param rootUrl
508
+ * @param onProgress
509
+ * @param fileName
510
+ * @hidden
511
+ */
512
+ GLTFFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
513
+ var _this = this;
514
+ return Promise.resolve().then(function () {
515
+ _this.onParsedObservable.notifyObservers(data);
516
+ _this.onParsedObservable.clear();
517
+ _this._log("Loading ".concat(fileName || ""));
518
+ _this._loader = _this._getLoader(data);
519
+ return _this._loader.loadAsync(scene, data, rootUrl, onProgress, fileName);
520
+ });
521
+ };
522
+ /**
523
+ * @param scene
524
+ * @param data
525
+ * @param rootUrl
526
+ * @param onProgress
527
+ * @param fileName
528
+ * @hidden
529
+ */
530
+ GLTFFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
531
+ var _this = this;
532
+ return Promise.resolve().then(function () {
533
+ _this.onParsedObservable.notifyObservers(data);
534
+ _this.onParsedObservable.clear();
535
+ _this._log("Loading ".concat(fileName || ""));
536
+ _this._loader = _this._getLoader(data);
537
+ // Prepare the asset container.
538
+ var container = new AssetContainer(scene);
539
+ // Get materials/textures when loading to add to container
540
+ var materials = [];
541
+ _this.onMaterialLoadedObservable.add(function (material) {
542
+ materials.push(material);
543
+ });
544
+ var textures = [];
545
+ _this.onTextureLoadedObservable.add(function (texture) {
546
+ textures.push(texture);
547
+ });
548
+ var cameras = [];
549
+ _this.onCameraLoadedObservable.add(function (camera) {
550
+ cameras.push(camera);
551
+ });
552
+ return _this._loader.importMeshAsync(null, scene, container, data, rootUrl, onProgress, fileName).then(function (result) {
553
+ Array.prototype.push.apply(container.geometries, result.geometries);
554
+ Array.prototype.push.apply(container.meshes, result.meshes);
555
+ Array.prototype.push.apply(container.particleSystems, result.particleSystems);
556
+ Array.prototype.push.apply(container.skeletons, result.skeletons);
557
+ Array.prototype.push.apply(container.animationGroups, result.animationGroups);
558
+ Array.prototype.push.apply(container.materials, materials);
559
+ Array.prototype.push.apply(container.textures, textures);
560
+ Array.prototype.push.apply(container.lights, result.lights);
561
+ Array.prototype.push.apply(container.transformNodes, result.transformNodes);
562
+ Array.prototype.push.apply(container.cameras, cameras);
563
+ return container;
564
+ });
565
+ });
566
+ };
567
+ /**
568
+ * @param data
569
+ * @hidden
570
+ */
571
+ GLTFFileLoader.prototype.canDirectLoad = function (data) {
572
+ return ((data.indexOf("asset") !== -1 && data.indexOf("version") !== -1) ||
573
+ StringTools.StartsWith(data, "data:base64," + GLTFFileLoader._MagicBase64Encoded) || // this is technically incorrect, but will continue to support for backcompat.
574
+ StringTools.StartsWith(data, "data:;base64," + GLTFFileLoader._MagicBase64Encoded) ||
575
+ StringTools.StartsWith(data, "data:application/octet-stream;base64," + GLTFFileLoader._MagicBase64Encoded) ||
576
+ StringTools.StartsWith(data, "data:model/gltf-binary;base64," + GLTFFileLoader._MagicBase64Encoded));
577
+ };
578
+ /**
579
+ * @param scene
580
+ * @param data
581
+ * @hidden
582
+ */
583
+ GLTFFileLoader.prototype.directLoad = function (scene, data) {
584
+ if (StringTools.StartsWith(data, "base64," + GLTFFileLoader._MagicBase64Encoded) || // this is technically incorrect, but will continue to support for backcompat.
585
+ StringTools.StartsWith(data, ";base64," + GLTFFileLoader._MagicBase64Encoded) ||
586
+ StringTools.StartsWith(data, "application/octet-stream;base64," + GLTFFileLoader._MagicBase64Encoded) ||
587
+ StringTools.StartsWith(data, "model/gltf-binary;base64," + GLTFFileLoader._MagicBase64Encoded)) {
588
+ var arrayBuffer_1 = DecodeBase64UrlToBinary(data);
589
+ this._validate(scene, arrayBuffer_1);
590
+ return this._unpackBinaryAsync(new DataReader({
591
+ readAsync: function (byteOffset, byteLength) { return readAsync(arrayBuffer_1, byteOffset, byteLength); },
592
+ byteLength: arrayBuffer_1.byteLength,
593
+ }));
594
+ }
595
+ this._validate(scene, data);
596
+ return Promise.resolve({ json: this._parseJson(data) });
597
+ };
598
+ /** @hidden */
599
+ GLTFFileLoader.prototype.createPlugin = function () {
600
+ return new GLTFFileLoader();
601
+ };
602
+ Object.defineProperty(GLTFFileLoader.prototype, "loaderState", {
603
+ /**
604
+ * The loader state or null if the loader is not active.
605
+ */
606
+ get: function () {
607
+ return this._state;
608
+ },
609
+ enumerable: false,
610
+ configurable: true
611
+ });
612
+ /**
613
+ * Returns a promise that resolves when the asset is completely loaded.
614
+ * @returns a promise that resolves when the asset is completely loaded.
615
+ */
616
+ GLTFFileLoader.prototype.whenCompleteAsync = function () {
617
+ var _this = this;
618
+ return new Promise(function (resolve, reject) {
619
+ _this.onCompleteObservable.addOnce(function () {
620
+ resolve();
621
+ });
622
+ _this.onErrorObservable.addOnce(function (reason) {
623
+ reject(reason);
624
+ });
625
+ });
626
+ };
627
+ /**
628
+ * @param state
629
+ * @hidden
630
+ */
631
+ GLTFFileLoader.prototype._setState = function (state) {
632
+ if (this._state === state) {
633
+ return;
634
+ }
635
+ this._state = state;
636
+ this.onLoaderStateChangedObservable.notifyObservers(this._state);
637
+ this._log(GLTFLoaderState[this._state]);
638
+ };
639
+ /**
640
+ * @param scene
641
+ * @param fileOrUrl
642
+ * @param onSuccess
643
+ * @param useArrayBuffer
644
+ * @param onError
645
+ * @param onOpened
646
+ * @hidden
647
+ */
648
+ GLTFFileLoader.prototype._loadFile = function (scene, fileOrUrl, onSuccess, useArrayBuffer, onError, onOpened) {
649
+ var _this = this;
650
+ var request = scene._loadFile(fileOrUrl, onSuccess, function (event) {
651
+ _this._onProgress(event, request);
652
+ }, true, useArrayBuffer, onError, onOpened);
653
+ request.onCompleteObservable.add(function (request) {
654
+ _this._requests.splice(_this._requests.indexOf(request), 1);
655
+ });
656
+ this._requests.push(request);
657
+ return request;
658
+ };
659
+ GLTFFileLoader.prototype._onProgress = function (event, request) {
660
+ if (!this._progressCallback) {
661
+ return;
662
+ }
663
+ request._lengthComputable = event.lengthComputable;
664
+ request._loaded = event.loaded;
665
+ request._total = event.total;
666
+ var lengthComputable = true;
667
+ var loaded = 0;
668
+ var total = 0;
669
+ for (var _i = 0, _a = this._requests; _i < _a.length; _i++) {
670
+ var request_1 = _a[_i];
671
+ if (request_1._lengthComputable === undefined || request_1._loaded === undefined || request_1._total === undefined) {
672
+ return;
673
+ }
674
+ lengthComputable = lengthComputable && request_1._lengthComputable;
675
+ loaded += request_1._loaded;
676
+ total += request_1._total;
677
+ }
678
+ this._progressCallback({
679
+ lengthComputable: lengthComputable,
680
+ loaded: loaded,
681
+ total: lengthComputable ? total : 0,
682
+ });
683
+ };
684
+ GLTFFileLoader.prototype._validate = function (scene, data, rootUrl, fileName) {
685
+ var _this = this;
686
+ if (rootUrl === void 0) { rootUrl = ""; }
687
+ if (fileName === void 0) { fileName = ""; }
688
+ if (!this.validate) {
689
+ return;
690
+ }
691
+ this._startPerformanceCounter("Validate JSON");
692
+ GLTFValidation.ValidateAsync(data, rootUrl, fileName, function (uri) {
693
+ return _this.preprocessUrlAsync(rootUrl + uri).then(function (url) { return scene._loadFileAsync(url, undefined, true, true); });
694
+ }).then(function (result) {
695
+ _this._endPerformanceCounter("Validate JSON");
696
+ _this.onValidatedObservable.notifyObservers(result);
697
+ _this.onValidatedObservable.clear();
698
+ }, function (reason) {
699
+ _this._endPerformanceCounter("Validate JSON");
700
+ Tools.Warn("Failed to validate: ".concat(reason.message));
701
+ _this.onValidatedObservable.clear();
702
+ });
703
+ };
704
+ GLTFFileLoader.prototype._getLoader = function (loaderData) {
705
+ var asset = loaderData.json.asset || {};
706
+ this._log("Asset version: ".concat(asset.version));
707
+ asset.minVersion && this._log("Asset minimum version: ".concat(asset.minVersion));
708
+ asset.generator && this._log("Asset generator: ".concat(asset.generator));
709
+ var version = GLTFFileLoader._parseVersion(asset.version);
710
+ if (!version) {
711
+ throw new Error("Invalid version: " + asset.version);
712
+ }
713
+ if (asset.minVersion !== undefined) {
714
+ var minVersion = GLTFFileLoader._parseVersion(asset.minVersion);
715
+ if (!minVersion) {
716
+ throw new Error("Invalid minimum version: " + asset.minVersion);
717
+ }
718
+ if (GLTFFileLoader._compareVersion(minVersion, { major: 2, minor: 0 }) > 0) {
719
+ throw new Error("Incompatible minimum version: " + asset.minVersion);
720
+ }
721
+ }
722
+ var createLoaders = {
723
+ 1: GLTFFileLoader._CreateGLTF1Loader,
724
+ 2: GLTFFileLoader._CreateGLTF2Loader,
725
+ };
726
+ var createLoader = createLoaders[version.major];
727
+ if (!createLoader) {
728
+ throw new Error("Unsupported version: " + asset.version);
729
+ }
730
+ return createLoader(this);
731
+ };
732
+ GLTFFileLoader.prototype._parseJson = function (json) {
733
+ this._startPerformanceCounter("Parse JSON");
734
+ this._log("JSON length: ".concat(json.length));
735
+ var parsed = JSON.parse(json);
736
+ this._endPerformanceCounter("Parse JSON");
737
+ return parsed;
738
+ };
739
+ GLTFFileLoader.prototype._unpackBinaryAsync = function (dataReader) {
740
+ var _this = this;
741
+ this._startPerformanceCounter("Unpack Binary");
742
+ // Read magic + version + length + json length + json format
743
+ return dataReader.loadAsync(20).then(function () {
744
+ var Binary = {
745
+ Magic: 0x46546c67,
746
+ };
747
+ var magic = dataReader.readUint32();
748
+ if (magic !== Binary.Magic) {
749
+ throw new RuntimeError("Unexpected magic: " + magic, ErrorCodes.GLTFLoaderUnexpectedMagicError);
750
+ }
751
+ var version = dataReader.readUint32();
752
+ if (_this.loggingEnabled) {
753
+ _this._log("Binary version: ".concat(version));
754
+ }
755
+ var length = dataReader.readUint32();
756
+ if (dataReader.buffer.byteLength !== 0 && length !== dataReader.buffer.byteLength) {
757
+ throw new Error("Length in header does not match actual data length: ".concat(length, " != ").concat(dataReader.buffer.byteLength));
758
+ }
759
+ var unpacked;
760
+ switch (version) {
761
+ case 1: {
762
+ unpacked = _this._unpackBinaryV1Async(dataReader, length);
763
+ break;
764
+ }
765
+ case 2: {
766
+ unpacked = _this._unpackBinaryV2Async(dataReader, length);
767
+ break;
768
+ }
769
+ default: {
770
+ throw new Error("Unsupported version: " + version);
771
+ }
772
+ }
773
+ _this._endPerformanceCounter("Unpack Binary");
774
+ return unpacked;
775
+ });
776
+ };
777
+ GLTFFileLoader.prototype._unpackBinaryV1Async = function (dataReader, length) {
778
+ var ContentFormat = {
779
+ JSON: 0,
780
+ };
781
+ var contentLength = dataReader.readUint32();
782
+ var contentFormat = dataReader.readUint32();
783
+ if (contentFormat !== ContentFormat.JSON) {
784
+ throw new Error("Unexpected content format: ".concat(contentFormat));
785
+ }
786
+ var bodyLength = length - dataReader.byteOffset;
787
+ var data = { json: this._parseJson(dataReader.readString(contentLength)), bin: null };
788
+ if (bodyLength !== 0) {
789
+ var startByteOffset_1 = dataReader.byteOffset;
790
+ data.bin = {
791
+ readAsync: function (byteOffset, byteLength) { return dataReader.buffer.readAsync(startByteOffset_1 + byteOffset, byteLength); },
792
+ byteLength: bodyLength,
793
+ };
794
+ }
795
+ return Promise.resolve(data);
796
+ };
797
+ GLTFFileLoader.prototype._unpackBinaryV2Async = function (dataReader, length) {
798
+ var _this = this;
799
+ var ChunkFormat = {
800
+ JSON: 0x4e4f534a,
801
+ BIN: 0x004e4942,
802
+ };
803
+ // Read the JSON chunk header.
804
+ var chunkLength = dataReader.readUint32();
805
+ var chunkFormat = dataReader.readUint32();
806
+ if (chunkFormat !== ChunkFormat.JSON) {
807
+ throw new Error("First chunk format is not JSON");
808
+ }
809
+ // Bail if there are no other chunks.
810
+ if (dataReader.byteOffset + chunkLength === length) {
811
+ return dataReader.loadAsync(chunkLength).then(function () {
812
+ return { json: _this._parseJson(dataReader.readString(chunkLength)), bin: null };
813
+ });
814
+ }
815
+ // Read the JSON chunk and the length and type of the next chunk.
816
+ return dataReader.loadAsync(chunkLength + 8).then(function () {
817
+ var data = { json: _this._parseJson(dataReader.readString(chunkLength)), bin: null };
818
+ var readAsync = function () {
819
+ var chunkLength = dataReader.readUint32();
820
+ var chunkFormat = dataReader.readUint32();
821
+ switch (chunkFormat) {
822
+ case ChunkFormat.JSON: {
823
+ throw new Error("Unexpected JSON chunk");
824
+ }
825
+ case ChunkFormat.BIN: {
826
+ var startByteOffset_2 = dataReader.byteOffset;
827
+ data.bin = {
828
+ readAsync: function (byteOffset, byteLength) { return dataReader.buffer.readAsync(startByteOffset_2 + byteOffset, byteLength); },
829
+ byteLength: chunkLength,
830
+ };
831
+ dataReader.skipBytes(chunkLength);
832
+ break;
833
+ }
834
+ default: {
835
+ // ignore unrecognized chunkFormat
836
+ dataReader.skipBytes(chunkLength);
837
+ break;
838
+ }
839
+ }
840
+ if (dataReader.byteOffset !== length) {
841
+ return dataReader.loadAsync(8).then(readAsync);
842
+ }
843
+ return Promise.resolve(data);
844
+ };
845
+ return readAsync();
846
+ });
847
+ };
848
+ GLTFFileLoader._parseVersion = function (version) {
849
+ if (version === "1.0" || version === "1.0.1") {
850
+ return {
851
+ major: 1,
852
+ minor: 0,
853
+ };
854
+ }
855
+ var match = (version + "").match(/^(\d+)\.(\d+)/);
856
+ if (!match) {
857
+ return null;
858
+ }
859
+ return {
860
+ major: parseInt(match[1]),
861
+ minor: parseInt(match[2]),
862
+ };
863
+ };
864
+ GLTFFileLoader._compareVersion = function (a, b) {
865
+ if (a.major > b.major) {
866
+ return 1;
867
+ }
868
+ if (a.major < b.major) {
869
+ return -1;
870
+ }
871
+ if (a.minor > b.minor) {
872
+ return 1;
873
+ }
874
+ if (a.minor < b.minor) {
875
+ return -1;
876
+ }
877
+ return 0;
878
+ };
879
+ /**
880
+ * @param message
881
+ * @hidden
882
+ */
883
+ GLTFFileLoader.prototype._logOpen = function (message) {
884
+ this._log(message);
885
+ this._logIndentLevel++;
886
+ };
887
+ /** @hidden */
888
+ GLTFFileLoader.prototype._logClose = function () {
889
+ --this._logIndentLevel;
890
+ };
891
+ GLTFFileLoader.prototype._logEnabled = function (message) {
892
+ var spaces = GLTFFileLoader._logSpaces.substr(0, this._logIndentLevel * 2);
893
+ Logger.Log("".concat(spaces).concat(message));
894
+ };
895
+ GLTFFileLoader.prototype._logDisabled = function (message) { };
896
+ GLTFFileLoader.prototype._startPerformanceCounterEnabled = function (counterName) {
897
+ Tools.StartPerformanceCounter(counterName);
898
+ };
899
+ GLTFFileLoader.prototype._startPerformanceCounterDisabled = function (counterName) { };
900
+ GLTFFileLoader.prototype._endPerformanceCounterEnabled = function (counterName) {
901
+ Tools.EndPerformanceCounter(counterName);
902
+ };
903
+ GLTFFileLoader.prototype._endPerformanceCounterDisabled = function (counterName) { };
904
+ // ----------
905
+ // V1 options
906
+ // ----------
907
+ /**
908
+ * Set this property to false to disable incremental loading which delays the loader from calling the success callback until after loading the meshes and shaders.
909
+ * Textures always loads asynchronously. For example, the success callback can compute the bounding information of the loaded meshes when incremental loading is disabled.
910
+ * Defaults to true.
911
+ * @hidden
912
+ */
913
+ GLTFFileLoader.IncrementalLoading = true;
914
+ /**
915
+ * Set this property to true in order to work with homogeneous coordinates, available with some converters and exporters.
916
+ * Defaults to false. See https://en.wikipedia.org/wiki/Homogeneous_coordinates.
917
+ * @hidden
918
+ */
919
+ GLTFFileLoader.HomogeneousCoordinates = false;
920
+ GLTFFileLoader._MagicBase64Encoded = "Z2xURg"; // "glTF" base64 encoded (without the quotes!)
921
+ GLTFFileLoader._logSpaces = " ";
922
+ return GLTFFileLoader;
923
+ }());
924
+ export { GLTFFileLoader };
925
+ if (SceneLoader) {
926
+ SceneLoader.RegisterPlugin(new GLTFFileLoader());
927
+ }
928
928
  //# sourceMappingURL=glTFFileLoader.js.map