@babylonjs/loaders 5.0.0-rc.1 → 5.0.0-rc.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. package/OBJ/index.d.ts +4 -4
  2. package/OBJ/index.js +4 -4
  3. package/OBJ/index.js.map +1 -1
  4. package/OBJ/mtlFileLoader.d.ts +41 -40
  5. package/OBJ/mtlFileLoader.js +232 -231
  6. package/OBJ/mtlFileLoader.js.map +1 -1
  7. package/OBJ/objFileLoader.d.ts +126 -133
  8. package/OBJ/objFileLoader.js +296 -300
  9. package/OBJ/objFileLoader.js.map +1 -1
  10. package/OBJ/objLoadingOptions.d.ts +43 -43
  11. package/OBJ/objLoadingOptions.js +1 -1
  12. package/OBJ/objLoadingOptions.js.map +1 -1
  13. package/OBJ/solidParser.d.ts +154 -153
  14. package/OBJ/solidParser.js +693 -699
  15. package/OBJ/solidParser.js.map +1 -1
  16. package/STL/index.d.ts +1 -1
  17. package/STL/index.js +1 -1
  18. package/STL/index.js.map +1 -1
  19. package/STL/stlFileLoader.d.ts +64 -71
  20. package/STL/stlFileLoader.js +233 -239
  21. package/STL/stlFileLoader.js.map +1 -1
  22. package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
  23. package/glTF/1.0/glTFBinaryExtension.js +60 -61
  24. package/glTF/1.0/glTFBinaryExtension.js.map +1 -1
  25. package/glTF/1.0/glTFLoader.d.ts +136 -106
  26. package/glTF/1.0/glTFLoader.js +1814 -1696
  27. package/glTF/1.0/glTFLoader.js.map +1 -1
  28. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
  29. package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
  30. package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -1
  31. package/glTF/1.0/glTFLoaderUtils.d.ts +63 -62
  32. package/glTF/1.0/glTFLoaderUtils.js +250 -232
  33. package/glTF/1.0/glTFLoaderUtils.js.map +1 -1
  34. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
  35. package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -127
  36. package/glTF/1.0/glTFMaterialsCommonExtension.js.map +1 -1
  37. package/glTF/1.0/index.d.ts +5 -5
  38. package/glTF/1.0/index.js +5 -5
  39. package/glTF/1.0/index.js.map +1 -1
  40. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -36
  41. package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -110
  42. package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -1
  43. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -28
  44. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -80
  45. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -1
  46. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -27
  47. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -42
  48. package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -1
  49. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -21
  50. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -32
  51. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -1
  52. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -32
  53. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -55
  54. package/glTF/2.0/Extensions/ExtrasAsMetadata.js.map +1 -1
  55. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -31
  56. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -101
  57. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -1
  58. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -28
  59. package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -81
  60. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -1
  61. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -31
  62. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -86
  63. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -1
  64. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -30
  65. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -45
  66. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js.map +1 -1
  67. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -34
  68. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -54
  69. package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -1
  70. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -30
  71. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -72
  72. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -1
  73. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -31
  74. package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -76
  75. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -1
  76. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -30
  77. package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -66
  78. package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -1
  79. package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -31
  80. package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -76
  81. package/glTF/2.0/Extensions/KHR_materials_translucency.js.map +1 -1
  82. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -30
  83. package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -277
  84. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
  85. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -30
  86. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -65
  87. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +1 -1
  88. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -76
  89. package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -243
  90. package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -1
  91. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -31
  92. package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -78
  93. package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -1
  94. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -19
  95. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -21
  96. package/glTF/2.0/Extensions/KHR_mesh_quantization.js.map +1 -1
  97. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -21
  98. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -32
  99. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -1
  100. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -25
  101. package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -53
  102. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +1 -1
  103. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -29
  104. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -46
  105. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -1
  106. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -38
  107. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -216
  108. package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -1
  109. package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -75
  110. package/glTF/2.0/Extensions/MSFT_lod.js +369 -333
  111. package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -1
  112. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
  113. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
  114. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js.map +1 -1
  115. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
  116. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
  117. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js.map +1 -1
  118. package/glTF/2.0/Extensions/index.d.ts +26 -26
  119. package/glTF/2.0/Extensions/index.js +26 -26
  120. package/glTF/2.0/Extensions/index.js.map +1 -1
  121. package/glTF/2.0/glTFLoader.d.ts +406 -357
  122. package/glTF/2.0/glTFLoader.js +2366 -2266
  123. package/glTF/2.0/glTFLoader.js.map +1 -1
  124. package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
  125. package/glTF/2.0/glTFLoaderExtension.js +1 -1
  126. package/glTF/2.0/glTFLoaderExtension.js.map +1 -1
  127. package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -209
  128. package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
  129. package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -1
  130. package/glTF/2.0/index.d.ts +4 -4
  131. package/glTF/2.0/index.js +5 -4
  132. package/glTF/2.0/index.js.map +1 -1
  133. package/glTF/glTFFileLoader.d.ts +433 -373
  134. package/glTF/glTFFileLoader.js +927 -870
  135. package/glTF/glTFFileLoader.js.map +1 -1
  136. package/glTF/glTFValidation.d.ts +29 -29
  137. package/glTF/glTFValidation.js +123 -121
  138. package/glTF/glTFValidation.js.map +1 -1
  139. package/glTF/index.d.ts +5 -5
  140. package/glTF/index.js +6 -5
  141. package/glTF/index.js.map +1 -1
  142. package/index.d.ts +3 -3
  143. package/index.js +4 -3
  144. package/index.js.map +1 -1
  145. package/legacy/legacy-glTF.d.ts +2 -2
  146. package/legacy/legacy-glTF.js +18 -18
  147. package/legacy/legacy-glTF.js.map +1 -1
  148. package/legacy/legacy-glTF1.d.ts +2 -2
  149. package/legacy/legacy-glTF1.js +15 -14
  150. package/legacy/legacy-glTF1.js.map +1 -1
  151. package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
  152. package/legacy/legacy-glTF1FileLoader.js +3 -2
  153. package/legacy/legacy-glTF1FileLoader.js.map +1 -1
  154. package/legacy/legacy-glTF2.d.ts +2 -2
  155. package/legacy/legacy-glTF2.js +33 -32
  156. package/legacy/legacy-glTF2.js.map +1 -1
  157. package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
  158. package/legacy/legacy-glTF2FileLoader.js +3 -2
  159. package/legacy/legacy-glTF2FileLoader.js.map +1 -1
  160. package/legacy/legacy-glTFFileLoader.d.ts +3 -3
  161. package/legacy/legacy-glTFFileLoader.js +4 -3
  162. package/legacy/legacy-glTFFileLoader.js.map +1 -1
  163. package/legacy/legacy-objFileLoader.d.ts +1 -1
  164. package/legacy/legacy-objFileLoader.js +13 -12
  165. package/legacy/legacy-objFileLoader.js.map +1 -1
  166. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  167. package/legacy/legacy-stlFileLoader.js +13 -12
  168. package/legacy/legacy-stlFileLoader.js.map +1 -1
  169. package/legacy/legacy.d.ts +6 -6
  170. package/legacy/legacy.js +8 -6
  171. package/legacy/legacy.js.map +1 -1
  172. package/package.json +23 -198
  173. package/readme.md +2 -2
@@ -1,2267 +1,2367 @@
1
- import { Deferred } from "@babylonjs/core/Misc/deferred.js";
2
- import { Quaternion, Vector3, Matrix } from "@babylonjs/core/Maths/math.vector.js";
3
- import { Color3 } from '@babylonjs/core/Maths/math.color.js';
4
- import { Tools } from "@babylonjs/core/Misc/tools.js";
5
- import { Camera } from "@babylonjs/core/Cameras/camera.js";
6
- import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera.js";
7
- import { AnimationGroup } from "@babylonjs/core/Animations/animationGroup.js";
8
- import { Animation } from "@babylonjs/core/Animations/animation.js";
9
- import { Bone } from "@babylonjs/core/Bones/bone.js";
10
- import { Skeleton } from "@babylonjs/core/Bones/skeleton.js";
11
- import { Material } from "@babylonjs/core/Materials/material.js";
12
- import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
13
- import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
14
- import { TransformNode } from "@babylonjs/core/Meshes/transformNode.js";
15
- import { Buffer, VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
16
- import { Geometry } from "@babylonjs/core/Meshes/geometry.js";
17
- import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
18
- import { MorphTarget } from "@babylonjs/core/Morph/morphTarget.js";
19
- import { MorphTargetManager } from "@babylonjs/core/Morph/morphTargetManager.js";
20
- import { GLTFFileLoader, GLTFLoaderState, GLTFLoaderCoordinateSystemMode, GLTFLoaderAnimationStartMode } from "../glTFFileLoader.js";
21
- import { AnimationKeyInterpolation } from '@babylonjs/core/Animations/animationKey.js';
22
- import { DecodeBase64UrlToBinary, IsBase64DataUrl, LoadFileError } from '@babylonjs/core/Misc/fileTools.js';
23
- import { Logger } from '@babylonjs/core/Misc/logger.js';
24
- import { TmpVectors } from '@babylonjs/core/Maths/math.vector.js';
25
- import { BoundingInfo } from '@babylonjs/core/Culling/boundingInfo.js';
26
- import { StringTools } from '@babylonjs/core/Misc/stringTools.js';
27
- /**
28
- * Helper class for working with arrays when loading the glTF asset
29
- */
30
- var ArrayItem = /** @class */ (function () {
31
- function ArrayItem() {
32
- }
33
- /**
34
- * Gets an item from the given array.
35
- * @param context The context when loading the asset
36
- * @param array The array to get the item from
37
- * @param index The index to the array
38
- * @returns The array item
39
- */
40
- ArrayItem.Get = function (context, array, index) {
41
- if (!array || index == undefined || !array[index]) {
42
- throw new Error("".concat(context, ": Failed to find index (").concat(index, ")"));
43
- }
44
- return array[index];
45
- };
46
- /**
47
- * Assign an `index` field to each item of the given array.
48
- * @param array The array of items
49
- */
50
- ArrayItem.Assign = function (array) {
51
- if (array) {
52
- for (var index = 0; index < array.length; index++) {
53
- array[index].index = index;
54
- }
55
- }
56
- };
57
- return ArrayItem;
58
- }());
59
- export { ArrayItem };
60
- /**
61
- * The glTF 2.0 loader
62
- */
63
- var GLTFLoader = /** @class */ (function () {
64
- /** @hidden */
65
- function GLTFLoader(parent) {
66
- /** @hidden */
67
- this._completePromises = new Array();
68
- /** @hidden */
69
- this._assetContainer = null;
70
- /** Storage */
71
- this._babylonLights = [];
72
- /** @hidden */
73
- this._disableInstancedMesh = 0;
74
- this._extensions = new Array();
75
- this._disposed = false;
76
- this._rootUrl = null;
77
- this._fileName = null;
78
- this._uniqueRootUrl = null;
79
- this._bin = null;
80
- this._rootBabylonMesh = null;
81
- this._defaultBabylonMaterialData = {};
82
- this._postSceneLoadActions = new Array();
83
- this._parent = parent;
84
- }
85
- /**
86
- * Registers a loader extension.
87
- * @param name The name of the loader extension.
88
- * @param factory The factory function that creates the loader extension.
89
- */
90
- GLTFLoader.RegisterExtension = function (name, factory) {
91
- if (GLTFLoader.UnregisterExtension(name)) {
92
- Logger.Warn("Extension with the name '".concat(name, "' already exists"));
93
- }
94
- GLTFLoader._RegisteredExtensions[name] = {
95
- factory: factory
96
- };
97
- };
98
- /**
99
- * Unregisters a loader extension.
100
- * @param name The name of the loader extension.
101
- * @returns A boolean indicating whether the extension has been unregistered
102
- */
103
- GLTFLoader.UnregisterExtension = function (name) {
104
- if (!GLTFLoader._RegisteredExtensions[name]) {
105
- return false;
106
- }
107
- delete GLTFLoader._RegisteredExtensions[name];
108
- return true;
109
- };
110
- Object.defineProperty(GLTFLoader.prototype, "gltf", {
111
- /**
112
- * The object that represents the glTF JSON.
113
- */
114
- get: function () {
115
- if (!this._gltf) {
116
- throw new Error("glTF JSON is not available");
117
- }
118
- return this._gltf;
119
- },
120
- enumerable: false,
121
- configurable: true
122
- });
123
- Object.defineProperty(GLTFLoader.prototype, "bin", {
124
- /**
125
- * The BIN chunk of a binary glTF.
126
- */
127
- get: function () {
128
- return this._bin;
129
- },
130
- enumerable: false,
131
- configurable: true
132
- });
133
- Object.defineProperty(GLTFLoader.prototype, "parent", {
134
- /**
135
- * The parent file loader.
136
- */
137
- get: function () {
138
- return this._parent;
139
- },
140
- enumerable: false,
141
- configurable: true
142
- });
143
- Object.defineProperty(GLTFLoader.prototype, "babylonScene", {
144
- /**
145
- * The Babylon scene when loading the asset.
146
- */
147
- get: function () {
148
- if (!this._babylonScene) {
149
- throw new Error("Scene is not available");
150
- }
151
- return this._babylonScene;
152
- },
153
- enumerable: false,
154
- configurable: true
155
- });
156
- Object.defineProperty(GLTFLoader.prototype, "rootBabylonMesh", {
157
- /**
158
- * The root Babylon mesh when loading the asset.
159
- */
160
- get: function () {
161
- return this._rootBabylonMesh;
162
- },
163
- enumerable: false,
164
- configurable: true
165
- });
166
- /** @hidden */
167
- GLTFLoader.prototype.dispose = function () {
168
- if (this._disposed) {
169
- return;
170
- }
171
- this._disposed = true;
172
- this._completePromises.length = 0;
173
- this._extensions.forEach(function (extension) { return extension.dispose && extension.dispose(); });
174
- this._extensions.length = 0;
175
- this._gltf = null; // TODO
176
- this._bin = null;
177
- this._babylonScene = null; // TODO
178
- this._rootBabylonMesh = null;
179
- this._defaultBabylonMaterialData = {};
180
- this._postSceneLoadActions.length = 0;
181
- this._parent.dispose();
182
- };
183
- /** @hidden */
184
- GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, container, data, rootUrl, onProgress, fileName) {
185
- var _this = this;
186
- if (fileName === void 0) { fileName = ""; }
187
- return Promise.resolve().then(function () {
188
- _this._babylonScene = scene;
189
- _this._assetContainer = container;
190
- _this._loadData(data);
191
- var nodes = null;
192
- if (meshesNames) {
193
- var nodeMap_1 = {};
194
- if (_this._gltf.nodes) {
195
- for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
196
- var node = _a[_i];
197
- if (node.name) {
198
- nodeMap_1[node.name] = node.index;
199
- }
200
- }
201
- }
202
- var names = (meshesNames instanceof Array) ? meshesNames : [meshesNames];
203
- nodes = names.map(function (name) {
204
- var node = nodeMap_1[name];
205
- if (node === undefined) {
206
- throw new Error("Failed to find node '".concat(name, "'"));
207
- }
208
- return node;
209
- });
210
- }
211
- return _this._loadAsync(rootUrl, fileName, nodes, function () {
212
- return {
213
- meshes: _this._getMeshes(),
214
- particleSystems: [],
215
- skeletons: _this._getSkeletons(),
216
- animationGroups: _this._getAnimationGroups(),
217
- lights: _this._babylonLights,
218
- transformNodes: _this._getTransformNodes(),
219
- geometries: _this._getGeometries()
220
- };
221
- });
222
- });
223
- };
224
- /** @hidden */
225
- GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
226
- var _this = this;
227
- if (fileName === void 0) { fileName = ""; }
228
- return Promise.resolve().then(function () {
229
- _this._babylonScene = scene;
230
- _this._loadData(data);
231
- return _this._loadAsync(rootUrl, fileName, null, function () { return undefined; });
232
- });
233
- };
234
- GLTFLoader.prototype._loadAsync = function (rootUrl, fileName, nodes, resultFunc) {
235
- var _this = this;
236
- return Promise.resolve().then(function () {
237
- _this._rootUrl = rootUrl;
238
- _this._uniqueRootUrl = (!StringTools.StartsWith(rootUrl, "file:") && fileName) ? rootUrl : "".concat(rootUrl).concat(Date.now(), "/");
239
- _this._fileName = fileName;
240
- _this._loadExtensions();
241
- _this._checkExtensions();
242
- var loadingToReadyCounterName = "".concat(GLTFLoaderState[GLTFLoaderState.LOADING], " => ").concat(GLTFLoaderState[GLTFLoaderState.READY]);
243
- var loadingToCompleteCounterName = "".concat(GLTFLoaderState[GLTFLoaderState.LOADING], " => ").concat(GLTFLoaderState[GLTFLoaderState.COMPLETE]);
244
- _this._parent._startPerformanceCounter(loadingToReadyCounterName);
245
- _this._parent._startPerformanceCounter(loadingToCompleteCounterName);
246
- _this._parent._setState(GLTFLoaderState.LOADING);
247
- _this._extensionsOnLoading();
248
- var promises = new Array();
249
- // Block the marking of materials dirty until the scene is loaded.
250
- var oldBlockMaterialDirtyMechanism = _this._babylonScene.blockMaterialDirtyMechanism;
251
- _this._babylonScene.blockMaterialDirtyMechanism = true;
252
- if (nodes) {
253
- promises.push(_this.loadSceneAsync("/nodes", { nodes: nodes, index: -1 }));
254
- }
255
- else if (_this._gltf.scene != undefined || (_this._gltf.scenes && _this._gltf.scenes[0])) {
256
- var scene = ArrayItem.Get("/scene", _this._gltf.scenes, _this._gltf.scene || 0);
257
- promises.push(_this.loadSceneAsync("/scenes/".concat(scene.index), scene));
258
- }
259
- if (_this.parent.loadAllMaterials && _this._gltf.materials) {
260
- for (var m = 0; m < _this._gltf.materials.length; ++m) {
261
- var material = _this._gltf.materials[m];
262
- var context_1 = "/materials/" + m;
263
- var babylonDrawMode = Material.TriangleFillMode;
264
- promises.push(_this._loadMaterialAsync(context_1, material, null, babylonDrawMode, function (material) { }));
265
- }
266
- }
267
- // Restore the blocking of material dirty.
268
- _this._babylonScene.blockMaterialDirtyMechanism = oldBlockMaterialDirtyMechanism;
269
- if (_this._parent.compileMaterials) {
270
- promises.push(_this._compileMaterialsAsync());
271
- }
272
- if (_this._parent.compileShadowGenerators) {
273
- promises.push(_this._compileShadowGeneratorsAsync());
274
- }
275
- var resultPromise = Promise.all(promises).then(function () {
276
- if (_this._rootBabylonMesh) {
277
- _this._rootBabylonMesh.setEnabled(true);
278
- }
279
- _this._extensionsOnReady();
280
- _this._parent._setState(GLTFLoaderState.READY);
281
- _this._startAnimations();
282
- return resultFunc();
283
- });
284
- return resultPromise.then(function (result) {
285
- _this._parent._endPerformanceCounter(loadingToReadyCounterName);
286
- Tools.SetImmediate(function () {
287
- if (!_this._disposed) {
288
- Promise.all(_this._completePromises).then(function () {
289
- _this._parent._endPerformanceCounter(loadingToCompleteCounterName);
290
- _this._parent._setState(GLTFLoaderState.COMPLETE);
291
- _this._parent.onCompleteObservable.notifyObservers(undefined);
292
- _this._parent.onCompleteObservable.clear();
293
- _this.dispose();
294
- }, function (error) {
295
- _this._parent.onErrorObservable.notifyObservers(error);
296
- _this._parent.onErrorObservable.clear();
297
- _this.dispose();
298
- });
299
- }
300
- });
301
- return result;
302
- });
303
- }).catch(function (error) {
304
- if (!_this._disposed) {
305
- _this._parent.onErrorObservable.notifyObservers(error);
306
- _this._parent.onErrorObservable.clear();
307
- _this.dispose();
308
- }
309
- throw error;
310
- });
311
- };
312
- GLTFLoader.prototype._loadData = function (data) {
313
- this._gltf = data.json;
314
- this._setupData();
315
- if (data.bin) {
316
- var buffers = this._gltf.buffers;
317
- if (buffers && buffers[0] && !buffers[0].uri) {
318
- var binaryBuffer = buffers[0];
319
- if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
320
- Logger.Warn("Binary buffer length (".concat(binaryBuffer.byteLength, ") from JSON does not match chunk length (").concat(data.bin.byteLength, ")"));
321
- }
322
- this._bin = data.bin;
323
- }
324
- else {
325
- Logger.Warn("Unexpected BIN chunk");
326
- }
327
- }
328
- };
329
- GLTFLoader.prototype._setupData = function () {
330
- ArrayItem.Assign(this._gltf.accessors);
331
- ArrayItem.Assign(this._gltf.animations);
332
- ArrayItem.Assign(this._gltf.buffers);
333
- ArrayItem.Assign(this._gltf.bufferViews);
334
- ArrayItem.Assign(this._gltf.cameras);
335
- ArrayItem.Assign(this._gltf.images);
336
- ArrayItem.Assign(this._gltf.materials);
337
- ArrayItem.Assign(this._gltf.meshes);
338
- ArrayItem.Assign(this._gltf.nodes);
339
- ArrayItem.Assign(this._gltf.samplers);
340
- ArrayItem.Assign(this._gltf.scenes);
341
- ArrayItem.Assign(this._gltf.skins);
342
- ArrayItem.Assign(this._gltf.textures);
343
- if (this._gltf.nodes) {
344
- var nodeParents = {};
345
- for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
346
- var node = _a[_i];
347
- if (node.children) {
348
- for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
349
- var index = _c[_b];
350
- nodeParents[index] = node.index;
351
- }
352
- }
353
- }
354
- var rootNode = this._createRootNode();
355
- for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
356
- var node = _e[_d];
357
- var parentIndex = nodeParents[node.index];
358
- node.parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
359
- }
360
- }
361
- };
362
- GLTFLoader.prototype._loadExtensions = function () {
363
- for (var name_1 in GLTFLoader._RegisteredExtensions) {
364
- var extension = GLTFLoader._RegisteredExtensions[name_1].factory(this);
365
- if (extension.name !== name_1) {
366
- Logger.Warn("The name of the glTF loader extension instance does not match the registered name: ".concat(extension.name, " !== ").concat(name_1));
367
- }
368
- this._extensions.push(extension);
369
- this._parent.onExtensionLoadedObservable.notifyObservers(extension);
370
- }
371
- this._extensions.sort(function (a, b) { return (a.order || Number.MAX_VALUE) - (b.order || Number.MAX_VALUE); });
372
- this._parent.onExtensionLoadedObservable.clear();
373
- };
374
- GLTFLoader.prototype._checkExtensions = function () {
375
- if (this._gltf.extensionsRequired) {
376
- var _loop_1 = function (name_2) {
377
- var available = this_1._extensions.some(function (extension) { return extension.name === name_2 && extension.enabled; });
378
- if (!available) {
379
- throw new Error("Require extension ".concat(name_2, " is not available"));
380
- }
381
- };
382
- var this_1 = this;
383
- for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
384
- var name_2 = _a[_i];
385
- _loop_1(name_2);
386
- }
387
- }
388
- };
389
- GLTFLoader.prototype._createRootNode = function () {
390
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
391
- this._rootBabylonMesh = new Mesh("__root__", this._babylonScene);
392
- this._rootBabylonMesh._parentContainer = this._assetContainer;
393
- this._babylonScene._blockEntityCollection = false;
394
- this._rootBabylonMesh.setEnabled(false);
395
- var rootNode = {
396
- _babylonTransformNode: this._rootBabylonMesh,
397
- index: -1
398
- };
399
- switch (this._parent.coordinateSystemMode) {
400
- case GLTFLoaderCoordinateSystemMode.AUTO: {
401
- if (!this._babylonScene.useRightHandedSystem) {
402
- rootNode.rotation = [0, 1, 0, 0];
403
- rootNode.scale = [1, 1, -1];
404
- GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
405
- }
406
- break;
407
- }
408
- case GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
409
- this._babylonScene.useRightHandedSystem = true;
410
- break;
411
- }
412
- default: {
413
- throw new Error("Invalid coordinate system mode (".concat(this._parent.coordinateSystemMode, ")"));
414
- }
415
- }
416
- this._parent.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
417
- return rootNode;
418
- };
419
- /**
420
- * Loads a glTF scene.
421
- * @param context The context when loading the asset
422
- * @param scene The glTF scene property
423
- * @returns A promise that resolves when the load is complete
424
- */
425
- GLTFLoader.prototype.loadSceneAsync = function (context, scene) {
426
- var _this = this;
427
- var extensionPromise = this._extensionsLoadSceneAsync(context, scene);
428
- if (extensionPromise) {
429
- return extensionPromise;
430
- }
431
- var promises = new Array();
432
- this.logOpen("".concat(context, " ").concat(scene.name || ""));
433
- if (scene.nodes) {
434
- for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
435
- var index = _a[_i];
436
- var node = ArrayItem.Get("".concat(context, "/nodes/").concat(index), this._gltf.nodes, index);
437
- promises.push(this.loadNodeAsync("/nodes/".concat(node.index), node, function (babylonMesh) {
438
- babylonMesh.parent = _this._rootBabylonMesh;
439
- }));
440
- }
441
- }
442
- for (var _b = 0, _c = this._postSceneLoadActions; _b < _c.length; _b++) {
443
- var action = _c[_b];
444
- action();
445
- }
446
- promises.push(this._loadAnimationsAsync());
447
- this.logClose();
448
- return Promise.all(promises).then(function () { });
449
- };
450
- GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
451
- if (node._primitiveBabylonMeshes) {
452
- for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
453
- var babylonMesh = _a[_i];
454
- callback(babylonMesh);
455
- }
456
- }
457
- };
458
- GLTFLoader.prototype._getGeometries = function () {
459
- var geometries = new Array();
460
- var nodes = this._gltf.nodes;
461
- if (nodes) {
462
- for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
463
- var node = nodes_1[_i];
464
- this._forEachPrimitive(node, function (babylonMesh) {
465
- var geometry = babylonMesh.geometry;
466
- if (geometry && geometries.indexOf(geometry) === -1) {
467
- geometries.push(geometry);
468
- }
469
- });
470
- }
471
- }
472
- return geometries;
473
- };
474
- GLTFLoader.prototype._getMeshes = function () {
475
- var meshes = new Array();
476
- // Root mesh is always first, if available.
477
- if (this._rootBabylonMesh) {
478
- meshes.push(this._rootBabylonMesh);
479
- }
480
- var nodes = this._gltf.nodes;
481
- if (nodes) {
482
- for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
483
- var node = nodes_2[_i];
484
- this._forEachPrimitive(node, function (babylonMesh) {
485
- meshes.push(babylonMesh);
486
- });
487
- }
488
- }
489
- return meshes;
490
- };
491
- GLTFLoader.prototype._getTransformNodes = function () {
492
- var transformNodes = new Array();
493
- var nodes = this._gltf.nodes;
494
- if (nodes) {
495
- for (var _i = 0, nodes_3 = nodes; _i < nodes_3.length; _i++) {
496
- var node = nodes_3[_i];
497
- if (node._babylonTransformNode && node._babylonTransformNode.getClassName() === "TransformNode") {
498
- transformNodes.push(node._babylonTransformNode);
499
- }
500
- }
501
- }
502
- return transformNodes;
503
- };
504
- GLTFLoader.prototype._getSkeletons = function () {
505
- var skeletons = new Array();
506
- var skins = this._gltf.skins;
507
- if (skins) {
508
- for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
509
- var skin = skins_1[_i];
510
- if (skin._data) {
511
- skeletons.push(skin._data.babylonSkeleton);
512
- }
513
- }
514
- }
515
- return skeletons;
516
- };
517
- GLTFLoader.prototype._getAnimationGroups = function () {
518
- var animationGroups = new Array();
519
- var animations = this._gltf.animations;
520
- if (animations) {
521
- for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
522
- var animation = animations_1[_i];
523
- if (animation._babylonAnimationGroup) {
524
- animationGroups.push(animation._babylonAnimationGroup);
525
- }
526
- }
527
- }
528
- return animationGroups;
529
- };
530
- GLTFLoader.prototype._startAnimations = function () {
531
- switch (this._parent.animationStartMode) {
532
- case GLTFLoaderAnimationStartMode.NONE: {
533
- // do nothing
534
- break;
535
- }
536
- case GLTFLoaderAnimationStartMode.FIRST: {
537
- var babylonAnimationGroups = this._getAnimationGroups();
538
- if (babylonAnimationGroups.length !== 0) {
539
- babylonAnimationGroups[0].start(true);
540
- }
541
- break;
542
- }
543
- case GLTFLoaderAnimationStartMode.ALL: {
544
- var babylonAnimationGroups = this._getAnimationGroups();
545
- for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
546
- var babylonAnimationGroup = babylonAnimationGroups_1[_i];
547
- babylonAnimationGroup.start(true);
548
- }
549
- break;
550
- }
551
- default: {
552
- Logger.Error("Invalid animation start mode (".concat(this._parent.animationStartMode, ")"));
553
- return;
554
- }
555
- }
556
- };
557
- /**
558
- * Loads a glTF node.
559
- * @param context The context when loading the asset
560
- * @param node The glTF node property
561
- * @param assign A function called synchronously after parsing the glTF properties
562
- * @returns A promise that resolves with the loaded Babylon mesh when the load is complete
563
- */
564
- GLTFLoader.prototype.loadNodeAsync = function (context, node, assign) {
565
- var _this = this;
566
- if (assign === void 0) { assign = function () { }; }
567
- var extensionPromise = this._extensionsLoadNodeAsync(context, node, assign);
568
- if (extensionPromise) {
569
- return extensionPromise;
570
- }
571
- if (node._babylonTransformNode) {
572
- throw new Error("".concat(context, ": Invalid recursive node hierarchy"));
573
- }
574
- var promises = new Array();
575
- this.logOpen("".concat(context, " ").concat(node.name || ""));
576
- var loadNode = function (babylonTransformNode) {
577
- GLTFLoader.AddPointerMetadata(babylonTransformNode, context);
578
- GLTFLoader._LoadTransform(node, babylonTransformNode);
579
- if (node.camera != undefined) {
580
- var camera = ArrayItem.Get("".concat(context, "/camera"), _this._gltf.cameras, node.camera);
581
- promises.push(_this.loadCameraAsync("/cameras/".concat(camera.index), camera, function (babylonCamera) {
582
- babylonCamera.parent = babylonTransformNode;
583
- }));
584
- }
585
- if (node.children) {
586
- for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
587
- var index = _a[_i];
588
- var childNode = ArrayItem.Get("".concat(context, "/children/").concat(index), _this._gltf.nodes, index);
589
- promises.push(_this.loadNodeAsync("/nodes/".concat(childNode.index), childNode, function (childBabylonMesh) {
590
- childBabylonMesh.parent = babylonTransformNode;
591
- }));
592
- }
593
- }
594
- assign(babylonTransformNode);
595
- };
596
- if (node.mesh == undefined || node.skin != undefined) {
597
- var nodeName = node.name || "node".concat(node.index);
598
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
599
- node._babylonTransformNode = new TransformNode(nodeName, this._babylonScene);
600
- node._babylonTransformNode._parentContainer = this._assetContainer;
601
- this._babylonScene._blockEntityCollection = false;
602
- loadNode(node._babylonTransformNode);
603
- }
604
- if (node.mesh != undefined) {
605
- if (node.skin == undefined) {
606
- var mesh = ArrayItem.Get("".concat(context, "/mesh"), this._gltf.meshes, node.mesh);
607
- promises.push(this._loadMeshAsync("/meshes/".concat(mesh.index), node, mesh, loadNode));
608
- }
609
- else {
610
- // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
611
- // This code path will place the skinned mesh as a sibling of the skeleton root node without loading the
612
- // transform, which effectively ignores the transform of the skinned mesh, as per spec.
613
- var mesh = ArrayItem.Get("".concat(context, "/mesh"), this._gltf.meshes, node.mesh);
614
- promises.push(this._loadMeshAsync("/meshes/".concat(mesh.index), node, mesh, function (babylonTransformNode) {
615
- GLTFLoader.AddPointerMetadata(babylonTransformNode, context);
616
- var skin = ArrayItem.Get("".concat(context, "/skin"), _this._gltf.skins, node.skin);
617
- promises.push(_this._loadSkinAsync("/skins/".concat(skin.index), node, skin, function (babylonSkeleton) {
618
- _this._forEachPrimitive(node, function (babylonMesh) {
619
- babylonMesh.skeleton = babylonSkeleton;
620
- });
621
- // Wait until the scene is loaded to ensure the skeleton root node has been loaded.
622
- _this._postSceneLoadActions.push(function () {
623
- if (skin.skeleton != undefined) {
624
- // Place the skinned mesh node as a sibling of the skeleton root node.
625
- var skeletonRootNode = ArrayItem.Get("/skins/".concat(skin.index, "/skeleton"), _this._gltf.nodes, skin.skeleton);
626
- babylonTransformNode.parent = skeletonRootNode.parent._babylonTransformNode;
627
- }
628
- else {
629
- babylonTransformNode.parent = _this._rootBabylonMesh;
630
- }
631
- });
632
- }));
633
- }));
634
- }
635
- }
636
- this.logClose();
637
- return Promise.all(promises).then(function () {
638
- _this._forEachPrimitive(node, function (babylonMesh) {
639
- if (babylonMesh.geometry && babylonMesh.geometry.useBoundingInfoFromGeometry) {
640
- // simply apply the world matrices to the bounding info - the extends are already ok
641
- babylonMesh._updateBoundingInfo();
642
- }
643
- else {
644
- babylonMesh.refreshBoundingInfo(true);
645
- }
646
- });
647
- return node._babylonTransformNode;
648
- });
649
- };
650
- GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, assign) {
651
- var primitives = mesh.primitives;
652
- if (!primitives || !primitives.length) {
653
- throw new Error("".concat(context, ": Primitives are missing"));
654
- }
655
- if (primitives[0].index == undefined) {
656
- ArrayItem.Assign(primitives);
657
- }
658
- var promises = new Array();
659
- this.logOpen("".concat(context, " ").concat(mesh.name || ""));
660
- var name = node.name || "node".concat(node.index);
661
- if (primitives.length === 1) {
662
- var primitive = mesh.primitives[0];
663
- promises.push(this._loadMeshPrimitiveAsync("".concat(context, "/primitives/").concat(primitive.index), name, node, mesh, primitive, function (babylonMesh) {
664
- node._babylonTransformNode = babylonMesh;
665
- node._primitiveBabylonMeshes = [babylonMesh];
666
- }));
667
- }
668
- else {
669
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
670
- node._babylonTransformNode = new TransformNode(name, this._babylonScene);
671
- node._babylonTransformNode._parentContainer = this._assetContainer;
672
- this._babylonScene._blockEntityCollection = false;
673
- node._primitiveBabylonMeshes = [];
674
- for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
675
- var primitive = primitives_1[_i];
676
- promises.push(this._loadMeshPrimitiveAsync("".concat(context, "/primitives/").concat(primitive.index), "".concat(name, "_primitive").concat(primitive.index), node, mesh, primitive, function (babylonMesh) {
677
- babylonMesh.parent = node._babylonTransformNode;
678
- node._primitiveBabylonMeshes.push(babylonMesh);
679
- }));
680
- }
681
- }
682
- assign(node._babylonTransformNode);
683
- this.logClose();
684
- return Promise.all(promises).then(function () {
685
- return node._babylonTransformNode;
686
- });
687
- };
688
- /**
689
- * @hidden Define this method to modify the default behavior when loading data for mesh primitives.
690
- * @param context The context when loading the asset
691
- * @param name The mesh name when loading the asset
692
- * @param node The glTF node when loading the asset
693
- * @param mesh The glTF mesh when loading the asset
694
- * @param primitive The glTF mesh primitive property
695
- * @param assign A function called synchronously after parsing the glTF properties
696
- * @returns A promise that resolves with the loaded mesh when the load is complete or null if not handled
697
- */
698
- GLTFLoader.prototype._loadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
699
- var _this = this;
700
- var extensionPromise = this._extensionsLoadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign);
701
- if (extensionPromise) {
702
- return extensionPromise;
703
- }
704
- this.logOpen("".concat(context));
705
- var shouldInstance = (this._disableInstancedMesh === 0) && this._parent.createInstances && (node.skin == undefined && !mesh.primitives[0].targets);
706
- var babylonAbstractMesh;
707
- var promise;
708
- if (shouldInstance && primitive._instanceData) {
709
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
710
- babylonAbstractMesh = primitive._instanceData.babylonSourceMesh.createInstance(name);
711
- babylonAbstractMesh._parentContainer = this._assetContainer;
712
- this._babylonScene._blockEntityCollection = false;
713
- promise = primitive._instanceData.promise;
714
- }
715
- else {
716
- var promises = new Array();
717
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
718
- var babylonMesh_1 = new Mesh(name, this._babylonScene);
719
- babylonMesh_1._parentContainer = this._assetContainer;
720
- this._babylonScene._blockEntityCollection = false;
721
- babylonMesh_1.overrideMaterialSideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
722
- this._createMorphTargets(context, node, mesh, primitive, babylonMesh_1);
723
- promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh_1).then(function (babylonGeometry) {
724
- return _this._loadMorphTargetsAsync(context, primitive, babylonMesh_1, babylonGeometry).then(function () {
725
- _this._babylonScene._blockEntityCollection = !!_this._assetContainer;
726
- babylonGeometry.applyToMesh(babylonMesh_1);
727
- babylonGeometry._parentContainer = _this._assetContainer;
728
- _this._babylonScene._blockEntityCollection = false;
729
- });
730
- }));
731
- var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
732
- if (primitive.material == undefined) {
733
- var babylonMaterial = this._defaultBabylonMaterialData[babylonDrawMode];
734
- if (!babylonMaterial) {
735
- babylonMaterial = this._createDefaultMaterial("__GLTFLoader._default", babylonDrawMode);
736
- this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
737
- this._defaultBabylonMaterialData[babylonDrawMode] = babylonMaterial;
738
- }
739
- babylonMesh_1.material = babylonMaterial;
740
- }
741
- else {
742
- var material = ArrayItem.Get("".concat(context, "/material"), this._gltf.materials, primitive.material);
743
- promises.push(this._loadMaterialAsync("/materials/".concat(material.index), material, babylonMesh_1, babylonDrawMode, function (babylonMaterial) {
744
- babylonMesh_1.material = babylonMaterial;
745
- }));
746
- }
747
- promise = Promise.all(promises);
748
- if (shouldInstance) {
749
- primitive._instanceData = {
750
- babylonSourceMesh: babylonMesh_1,
751
- promise: promise
752
- };
753
- }
754
- babylonAbstractMesh = babylonMesh_1;
755
- }
756
- GLTFLoader.AddPointerMetadata(babylonAbstractMesh, context);
757
- this._parent.onMeshLoadedObservable.notifyObservers(babylonAbstractMesh);
758
- assign(babylonAbstractMesh);
759
- this.logClose();
760
- return promise.then(function () {
761
- return babylonAbstractMesh;
762
- });
763
- };
764
- GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
765
- var _this = this;
766
- var extensionPromise = this._extensionsLoadVertexDataAsync(context, primitive, babylonMesh);
767
- if (extensionPromise) {
768
- return extensionPromise;
769
- }
770
- var attributes = primitive.attributes;
771
- if (!attributes) {
772
- throw new Error("".concat(context, ": Attributes are missing"));
773
- }
774
- var promises = new Array();
775
- var babylonGeometry = new Geometry(babylonMesh.name, this._babylonScene);
776
- if (primitive.indices == undefined) {
777
- babylonMesh.isUnIndexed = true;
778
- }
779
- else {
780
- var accessor = ArrayItem.Get("".concat(context, "/indices"), this._gltf.accessors, primitive.indices);
781
- promises.push(this._loadIndicesAccessorAsync("/accessors/".concat(accessor.index), accessor).then(function (data) {
782
- babylonGeometry.setIndices(data);
783
- }));
784
- }
785
- var loadAttribute = function (attribute, kind, callback) {
786
- if (attributes[attribute] == undefined) {
787
- return;
788
- }
789
- babylonMesh._delayInfo = babylonMesh._delayInfo || [];
790
- if (babylonMesh._delayInfo.indexOf(kind) === -1) {
791
- babylonMesh._delayInfo.push(kind);
792
- }
793
- var accessor = ArrayItem.Get("".concat(context, "/attributes/").concat(attribute), _this._gltf.accessors, attributes[attribute]);
794
- promises.push(_this._loadVertexAccessorAsync("/accessors/".concat(accessor.index), accessor, kind).then(function (babylonVertexBuffer) {
795
- if (babylonVertexBuffer.getKind() === VertexBuffer.PositionKind && !_this.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton) {
796
- var mmin = accessor.min, mmax = accessor.max;
797
- if (mmin !== undefined && mmax !== undefined) {
798
- if (accessor.normalized && accessor.componentType !== 5126 /* FLOAT */) {
799
- var divider = 1;
800
- switch (accessor.componentType) {
801
- case 5120 /* BYTE */:
802
- divider = 127.0;
803
- break;
804
- case 5121 /* UNSIGNED_BYTE */:
805
- divider = 255.0;
806
- break;
807
- case 5122 /* SHORT */:
808
- divider = 32767.0;
809
- break;
810
- case 5123 /* UNSIGNED_SHORT */:
811
- divider = 65535.0;
812
- break;
813
- }
814
- for (var i = 0; i < 3; ++i) {
815
- mmin[i] = Math.max(mmin[i] / divider, -1.0);
816
- mmax[i] = Math.max(mmax[i] / divider, -1.0);
817
- }
818
- }
819
- var min = TmpVectors.Vector3[0], max = TmpVectors.Vector3[1];
820
- min.copyFromFloats.apply(min, mmin);
821
- max.copyFromFloats.apply(max, mmax);
822
- babylonGeometry._boundingInfo = new BoundingInfo(min, max);
823
- babylonGeometry.useBoundingInfoFromGeometry = true;
824
- }
825
- }
826
- babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
827
- }));
828
- if (kind == VertexBuffer.MatricesIndicesExtraKind) {
829
- babylonMesh.numBoneInfluencers = 8;
830
- }
831
- if (callback) {
832
- callback(accessor);
833
- }
834
- };
835
- loadAttribute("POSITION", VertexBuffer.PositionKind);
836
- loadAttribute("NORMAL", VertexBuffer.NormalKind);
837
- loadAttribute("TANGENT", VertexBuffer.TangentKind);
838
- loadAttribute("TEXCOORD_0", VertexBuffer.UVKind);
839
- loadAttribute("TEXCOORD_1", VertexBuffer.UV2Kind);
840
- loadAttribute("TEXCOORD_2", VertexBuffer.UV3Kind);
841
- loadAttribute("TEXCOORD_3", VertexBuffer.UV4Kind);
842
- loadAttribute("TEXCOORD_4", VertexBuffer.UV5Kind);
843
- loadAttribute("TEXCOORD_5", VertexBuffer.UV6Kind);
844
- loadAttribute("JOINTS_0", VertexBuffer.MatricesIndicesKind);
845
- loadAttribute("WEIGHTS_0", VertexBuffer.MatricesWeightsKind);
846
- loadAttribute("JOINTS_1", VertexBuffer.MatricesIndicesExtraKind);
847
- loadAttribute("WEIGHTS_1", VertexBuffer.MatricesWeightsExtraKind);
848
- loadAttribute("COLOR_0", VertexBuffer.ColorKind, function (accessor) {
849
- if (accessor.type === "VEC4" /* VEC4 */) {
850
- babylonMesh.hasVertexAlpha = true;
851
- }
852
- });
853
- return Promise.all(promises).then(function () {
854
- return babylonGeometry;
855
- });
856
- };
857
- GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
858
- if (!primitive.targets) {
859
- return;
860
- }
861
- if (node._numMorphTargets == undefined) {
862
- node._numMorphTargets = primitive.targets.length;
863
- }
864
- else if (primitive.targets.length !== node._numMorphTargets) {
865
- throw new Error("".concat(context, ": Primitives do not have the same number of targets"));
866
- }
867
- var targetNames = mesh.extras ? mesh.extras.targetNames : null;
868
- babylonMesh.morphTargetManager = new MorphTargetManager(babylonMesh.getScene());
869
- babylonMesh.morphTargetManager.areUpdatesFrozen = true;
870
- for (var index = 0; index < primitive.targets.length; index++) {
871
- var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
872
- var name_3 = targetNames ? targetNames[index] : "morphTarget".concat(index);
873
- babylonMesh.morphTargetManager.addTarget(new MorphTarget(name_3, weight, babylonMesh.getScene()));
874
- // TODO: tell the target whether it has positions, normals, tangents
875
- }
876
- };
877
- GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
878
- if (!primitive.targets) {
879
- return Promise.resolve();
880
- }
881
- var promises = new Array();
882
- var morphTargetManager = babylonMesh.morphTargetManager;
883
- for (var index = 0; index < morphTargetManager.numTargets; index++) {
884
- var babylonMorphTarget = morphTargetManager.getTarget(index);
885
- promises.push(this._loadMorphTargetVertexDataAsync("".concat(context, "/targets/").concat(index), babylonGeometry, primitive.targets[index], babylonMorphTarget));
886
- }
887
- return Promise.all(promises).then(function () {
888
- morphTargetManager.areUpdatesFrozen = false;
889
- });
890
- };
891
- GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
892
- var _this = this;
893
- var promises = new Array();
894
- var loadAttribute = function (attribute, kind, setData) {
895
- if (attributes[attribute] == undefined) {
896
- return;
897
- }
898
- var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
899
- if (!babylonVertexBuffer) {
900
- return;
901
- }
902
- var accessor = ArrayItem.Get("".concat(context, "/").concat(attribute), _this._gltf.accessors, attributes[attribute]);
903
- promises.push(_this._loadFloatAccessorAsync("/accessors/".concat(accessor.index), accessor).then(function (data) {
904
- setData(babylonVertexBuffer, data);
905
- }));
906
- };
907
- loadAttribute("POSITION", VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
908
- var positions = new Float32Array(data.length);
909
- babylonVertexBuffer.forEach(data.length, function (value, index) {
910
- positions[index] = data[index] + value;
911
- });
912
- babylonMorphTarget.setPositions(positions);
913
- });
914
- loadAttribute("NORMAL", VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
915
- var normals = new Float32Array(data.length);
916
- babylonVertexBuffer.forEach(normals.length, function (value, index) {
917
- normals[index] = data[index] + value;
918
- });
919
- babylonMorphTarget.setNormals(normals);
920
- });
921
- loadAttribute("TANGENT", VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
922
- var tangents = new Float32Array(data.length / 3 * 4);
923
- var dataIndex = 0;
924
- babylonVertexBuffer.forEach(data.length / 3 * 4, function (value, index) {
925
- // Tangent data for morph targets is stored as xyz delta.
926
- // The vertexData.tangent is stored as xyzw.
927
- // So we need to skip every fourth vertexData.tangent.
928
- if (((index + 1) % 4) !== 0) {
929
- tangents[dataIndex] = data[dataIndex] + value;
930
- dataIndex++;
931
- }
932
- });
933
- babylonMorphTarget.setTangents(tangents);
934
- });
935
- return Promise.all(promises).then(function () { });
936
- };
937
- GLTFLoader._LoadTransform = function (node, babylonNode) {
938
- // Ignore the TRS of skinned nodes.
939
- // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
940
- if (node.skin != undefined) {
941
- return;
942
- }
943
- var position = Vector3.Zero();
944
- var rotation = Quaternion.Identity();
945
- var scaling = Vector3.One();
946
- if (node.matrix) {
947
- var matrix = Matrix.FromArray(node.matrix);
948
- matrix.decompose(scaling, rotation, position);
949
- }
950
- else {
951
- if (node.translation) {
952
- position = Vector3.FromArray(node.translation);
953
- }
954
- if (node.rotation) {
955
- rotation = Quaternion.FromArray(node.rotation);
956
- }
957
- if (node.scale) {
958
- scaling = Vector3.FromArray(node.scale);
959
- }
960
- }
961
- babylonNode.position = position;
962
- babylonNode.rotationQuaternion = rotation;
963
- babylonNode.scaling = scaling;
964
- };
965
- GLTFLoader.prototype._loadSkinAsync = function (context, node, skin, assign) {
966
- var _this = this;
967
- var extensionPromise = this._extensionsLoadSkinAsync(context, node, skin);
968
- if (extensionPromise) {
969
- return extensionPromise;
970
- }
971
- if (skin._data) {
972
- assign(skin._data.babylonSkeleton);
973
- return skin._data.promise;
974
- }
975
- var skeletonId = "skeleton".concat(skin.index);
976
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
977
- var babylonSkeleton = new Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
978
- babylonSkeleton._parentContainer = this._assetContainer;
979
- this._babylonScene._blockEntityCollection = false;
980
- this._loadBones(context, skin, babylonSkeleton);
981
- var promise = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
982
- _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
983
- });
984
- skin._data = {
985
- babylonSkeleton: babylonSkeleton,
986
- promise: promise
987
- };
988
- assign(babylonSkeleton);
989
- return promise;
990
- };
991
- GLTFLoader.prototype._loadBones = function (context, skin, babylonSkeleton) {
992
- if (skin.skeleton == undefined || this._parent.alwaysComputeSkeletonRootNode) {
993
- var rootNode = this._findSkeletonRootNode("".concat(context, "/joints"), skin.joints);
994
- if (rootNode) {
995
- if (skin.skeleton === undefined) {
996
- skin.skeleton = rootNode.index;
997
- }
998
- else {
999
- var isParent = function (a, b) {
1000
- for (; b.parent; b = b.parent) {
1001
- if (b.parent === a) {
1002
- return true;
1003
- }
1004
- }
1005
- return false;
1006
- };
1007
- var skeletonNode = ArrayItem.Get("".concat(context, "/skeleton"), this._gltf.nodes, skin.skeleton);
1008
- if (skeletonNode !== rootNode && !isParent(skeletonNode, rootNode)) {
1009
- Logger.Warn("".concat(context, "/skeleton: Overriding with nearest common ancestor as skeleton node is not a common root"));
1010
- skin.skeleton = rootNode.index;
1011
- }
1012
- }
1013
- }
1014
- else {
1015
- Logger.Warn("".concat(context, ": Failed to find common root"));
1016
- }
1017
- }
1018
- var babylonBones = {};
1019
- for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
1020
- var index = _a[_i];
1021
- var node = ArrayItem.Get("".concat(context, "/joints/").concat(index), this._gltf.nodes, index);
1022
- this._loadBone(node, skin, babylonSkeleton, babylonBones);
1023
- }
1024
- };
1025
- GLTFLoader.prototype._findSkeletonRootNode = function (context, joints) {
1026
- var paths = {};
1027
- for (var _i = 0, joints_1 = joints; _i < joints_1.length; _i++) {
1028
- var index = joints_1[_i];
1029
- var path = new Array();
1030
- var node = ArrayItem.Get("".concat(context, "/").concat(index), this._gltf.nodes, index);
1031
- while (node.index !== -1) {
1032
- path.unshift(node);
1033
- node = node.parent;
1034
- }
1035
- paths[index] = path;
1036
- }
1037
- var rootNode = null;
1038
- for (var i = 0;; ++i) {
1039
- var path = paths[joints[0]];
1040
- if (i >= path.length) {
1041
- return rootNode;
1042
- }
1043
- var node = path[i];
1044
- for (var j = 1; j < joints.length; ++j) {
1045
- path = paths[joints[j]];
1046
- if (i >= path.length || node !== path[i]) {
1047
- return rootNode;
1048
- }
1049
- }
1050
- rootNode = node;
1051
- }
1052
- };
1053
- GLTFLoader.prototype._loadBone = function (node, skin, babylonSkeleton, babylonBones) {
1054
- var babylonBone = babylonBones[node.index];
1055
- if (babylonBone) {
1056
- return babylonBone;
1057
- }
1058
- var parentBabylonBone = null;
1059
- if (node.index !== skin.skeleton) {
1060
- if (node.parent && node.parent.index !== -1) {
1061
- parentBabylonBone = this._loadBone(node.parent, skin, babylonSkeleton, babylonBones);
1062
- }
1063
- else if (skin.skeleton !== undefined) {
1064
- Logger.Warn("/skins/".concat(skin.index, "/skeleton: Skeleton node is not a common root"));
1065
- }
1066
- }
1067
- var boneIndex = skin.joints.indexOf(node.index);
1068
- babylonBone = new Bone(node.name || "joint".concat(node.index), babylonSkeleton, parentBabylonBone, this._getNodeMatrix(node), null, null, boneIndex);
1069
- babylonBones[node.index] = babylonBone;
1070
- // Wait until the scene is loaded to ensure the transform nodes are loaded.
1071
- this._postSceneLoadActions.push(function () {
1072
- // Link the Babylon bone with the corresponding Babylon transform node.
1073
- // A glTF joint is a pointer to a glTF node in the glTF node hierarchy similar to Unity3D.
1074
- babylonBone.linkTransformNode(node._babylonTransformNode);
1075
- });
1076
- return babylonBone;
1077
- };
1078
- GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
1079
- if (skin.inverseBindMatrices == undefined) {
1080
- return Promise.resolve(null);
1081
- }
1082
- var accessor = ArrayItem.Get("".concat(context, "/inverseBindMatrices"), this._gltf.accessors, skin.inverseBindMatrices);
1083
- return this._loadFloatAccessorAsync("/accessors/".concat(accessor.index), accessor);
1084
- };
1085
- GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
1086
- for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
1087
- var babylonBone = _a[_i];
1088
- var baseMatrix = Matrix.Identity();
1089
- var boneIndex = babylonBone._index;
1090
- if (inverseBindMatricesData && boneIndex !== -1) {
1091
- Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
1092
- baseMatrix.invertToRef(baseMatrix);
1093
- }
1094
- var babylonParentBone = babylonBone.getParent();
1095
- if (babylonParentBone) {
1096
- baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
1097
- }
1098
- babylonBone.updateMatrix(baseMatrix, false, false);
1099
- babylonBone._updateDifferenceMatrix(undefined, false);
1100
- }
1101
- };
1102
- GLTFLoader.prototype._getNodeMatrix = function (node) {
1103
- return node.matrix ?
1104
- Matrix.FromArray(node.matrix) :
1105
- Matrix.Compose(node.scale ? Vector3.FromArray(node.scale) : Vector3.One(), node.rotation ? Quaternion.FromArray(node.rotation) : Quaternion.Identity(), node.translation ? Vector3.FromArray(node.translation) : Vector3.Zero());
1106
- };
1107
- /**
1108
- * Loads a glTF camera.
1109
- * @param context The context when loading the asset
1110
- * @param camera The glTF camera property
1111
- * @param assign A function called synchronously after parsing the glTF properties
1112
- * @returns A promise that resolves with the loaded Babylon camera when the load is complete
1113
- */
1114
- GLTFLoader.prototype.loadCameraAsync = function (context, camera, assign) {
1115
- if (assign === void 0) { assign = function () { }; }
1116
- var extensionPromise = this._extensionsLoadCameraAsync(context, camera, assign);
1117
- if (extensionPromise) {
1118
- return extensionPromise;
1119
- }
1120
- var promises = new Array();
1121
- this.logOpen("".concat(context, " ").concat(camera.name || ""));
1122
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
1123
- var babylonCamera = new FreeCamera(camera.name || "camera".concat(camera.index), Vector3.Zero(), this._babylonScene, false);
1124
- babylonCamera._parentContainer = this._assetContainer;
1125
- this._babylonScene._blockEntityCollection = false;
1126
- babylonCamera.ignoreParentScaling = true;
1127
- babylonCamera.rotation = new Vector3(0, Math.PI, 0);
1128
- switch (camera.type) {
1129
- case "perspective" /* PERSPECTIVE */: {
1130
- var perspective = camera.perspective;
1131
- if (!perspective) {
1132
- throw new Error("".concat(context, ": Camera perspective properties are missing"));
1133
- }
1134
- babylonCamera.fov = perspective.yfov;
1135
- babylonCamera.minZ = perspective.znear;
1136
- babylonCamera.maxZ = perspective.zfar || 0;
1137
- break;
1138
- }
1139
- case "orthographic" /* ORTHOGRAPHIC */: {
1140
- if (!camera.orthographic) {
1141
- throw new Error("".concat(context, ": Camera orthographic properties are missing"));
1142
- }
1143
- babylonCamera.mode = Camera.ORTHOGRAPHIC_CAMERA;
1144
- babylonCamera.orthoLeft = -camera.orthographic.xmag;
1145
- babylonCamera.orthoRight = camera.orthographic.xmag;
1146
- babylonCamera.orthoBottom = -camera.orthographic.ymag;
1147
- babylonCamera.orthoTop = camera.orthographic.ymag;
1148
- babylonCamera.minZ = camera.orthographic.znear;
1149
- babylonCamera.maxZ = camera.orthographic.zfar;
1150
- break;
1151
- }
1152
- default: {
1153
- throw new Error("".concat(context, ": Invalid camera type (").concat(camera.type, ")"));
1154
- }
1155
- }
1156
- GLTFLoader.AddPointerMetadata(babylonCamera, context);
1157
- this._parent.onCameraLoadedObservable.notifyObservers(babylonCamera);
1158
- assign(babylonCamera);
1159
- this.logClose();
1160
- return Promise.all(promises).then(function () {
1161
- return babylonCamera;
1162
- });
1163
- };
1164
- GLTFLoader.prototype._loadAnimationsAsync = function () {
1165
- var animations = this._gltf.animations;
1166
- if (!animations) {
1167
- return Promise.resolve();
1168
- }
1169
- var promises = new Array();
1170
- for (var index = 0; index < animations.length; index++) {
1171
- var animation = animations[index];
1172
- promises.push(this.loadAnimationAsync("/animations/".concat(animation.index), animation).then(function (animationGroup) {
1173
- // Delete the animation group if it ended up not having any animations in it.
1174
- if (animationGroup.targetedAnimations.length === 0) {
1175
- animationGroup.dispose();
1176
- }
1177
- }));
1178
- }
1179
- return Promise.all(promises).then(function () { });
1180
- };
1181
- /**
1182
- * Loads a glTF animation.
1183
- * @param context The context when loading the asset
1184
- * @param animation The glTF animation property
1185
- * @returns A promise that resolves with the loaded Babylon animation group when the load is complete
1186
- */
1187
- GLTFLoader.prototype.loadAnimationAsync = function (context, animation) {
1188
- var promise = this._extensionsLoadAnimationAsync(context, animation);
1189
- if (promise) {
1190
- return promise;
1191
- }
1192
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
1193
- var babylonAnimationGroup = new AnimationGroup(animation.name || "animation".concat(animation.index), this._babylonScene);
1194
- babylonAnimationGroup._parentContainer = this._assetContainer;
1195
- this._babylonScene._blockEntityCollection = false;
1196
- animation._babylonAnimationGroup = babylonAnimationGroup;
1197
- var promises = new Array();
1198
- ArrayItem.Assign(animation.channels);
1199
- ArrayItem.Assign(animation.samplers);
1200
- for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
1201
- var channel = _a[_i];
1202
- promises.push(this._loadAnimationChannelAsync("".concat(context, "/channels/").concat(channel.index), context, animation, channel, babylonAnimationGroup));
1203
- }
1204
- return Promise.all(promises).then(function () {
1205
- babylonAnimationGroup.normalize(0);
1206
- return babylonAnimationGroup;
1207
- });
1208
- };
1209
- /**
1210
- * @hidden Loads a glTF animation channel.
1211
- * @param context The context when loading the asset
1212
- * @param animationContext The context of the animation when loading the asset
1213
- * @param animation The glTF animation property
1214
- * @param channel The glTF animation channel property
1215
- * @param babylonAnimationGroup The babylon animation group property
1216
- * @param animationTargetOverride The babylon animation channel target override property. My be null.
1217
- * @returns A void promise when the channel load is complete
1218
- */
1219
- GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup, animationTargetOverride) {
1220
- var _this = this;
1221
- if (animationTargetOverride === void 0) { animationTargetOverride = null; }
1222
- if (channel.target.node == undefined) {
1223
- return Promise.resolve();
1224
- }
1225
- var targetNode = ArrayItem.Get("".concat(context, "/target/node"), this._gltf.nodes, channel.target.node);
1226
- // Ignore animations that have no animation targets.
1227
- if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
1228
- (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonTransformNode)) {
1229
- return Promise.resolve();
1230
- }
1231
- var sampler = ArrayItem.Get("".concat(context, "/sampler"), animation.samplers, channel.sampler);
1232
- return this._loadAnimationSamplerAsync("".concat(animationContext, "/samplers/").concat(channel.sampler), sampler).then(function (data) {
1233
- var targetPath;
1234
- var animationType;
1235
- switch (channel.target.path) {
1236
- case "translation" /* TRANSLATION */: {
1237
- targetPath = "position";
1238
- animationType = Animation.ANIMATIONTYPE_VECTOR3;
1239
- break;
1240
- }
1241
- case "rotation" /* ROTATION */: {
1242
- targetPath = "rotationQuaternion";
1243
- animationType = Animation.ANIMATIONTYPE_QUATERNION;
1244
- break;
1245
- }
1246
- case "scale" /* SCALE */: {
1247
- targetPath = "scaling";
1248
- animationType = Animation.ANIMATIONTYPE_VECTOR3;
1249
- break;
1250
- }
1251
- case "weights" /* WEIGHTS */: {
1252
- targetPath = "influence";
1253
- animationType = Animation.ANIMATIONTYPE_FLOAT;
1254
- break;
1255
- }
1256
- default: {
1257
- throw new Error("".concat(context, "/target/path: Invalid value (").concat(channel.target.path, ")"));
1258
- }
1259
- }
1260
- var outputBufferOffset = 0;
1261
- var getNextOutputValue;
1262
- switch (targetPath) {
1263
- case "position": {
1264
- getNextOutputValue = function (scale) {
1265
- var value = Vector3.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1266
- outputBufferOffset += 3;
1267
- return value;
1268
- };
1269
- break;
1270
- }
1271
- case "rotationQuaternion": {
1272
- getNextOutputValue = function (scale) {
1273
- var value = Quaternion.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1274
- outputBufferOffset += 4;
1275
- return value;
1276
- };
1277
- break;
1278
- }
1279
- case "scaling": {
1280
- getNextOutputValue = function (scale) {
1281
- var value = Vector3.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1282
- outputBufferOffset += 3;
1283
- return value;
1284
- };
1285
- break;
1286
- }
1287
- case "influence": {
1288
- getNextOutputValue = function (scale) {
1289
- var value = new Array(targetNode._numMorphTargets);
1290
- for (var i = 0; i < targetNode._numMorphTargets; i++) {
1291
- value[i] = data.output[outputBufferOffset++] * scale;
1292
- }
1293
- return value;
1294
- };
1295
- break;
1296
- }
1297
- }
1298
- var getNextKey;
1299
- switch (data.interpolation) {
1300
- case "STEP" /* STEP */: {
1301
- getNextKey = function (frameIndex) { return ({
1302
- frame: data.input[frameIndex] * _this.parent.targetFps,
1303
- value: getNextOutputValue(1),
1304
- interpolation: AnimationKeyInterpolation.STEP
1305
- }); };
1306
- break;
1307
- }
1308
- case "LINEAR" /* LINEAR */: {
1309
- getNextKey = function (frameIndex) { return ({
1310
- frame: data.input[frameIndex] * _this.parent.targetFps,
1311
- value: getNextOutputValue(1)
1312
- }); };
1313
- break;
1314
- }
1315
- case "CUBICSPLINE" /* CUBICSPLINE */: {
1316
- var invTargetFps_1 = 1 / _this.parent.targetFps;
1317
- getNextKey = function (frameIndex) { return ({
1318
- frame: data.input[frameIndex] * _this.parent.targetFps,
1319
- inTangent: getNextOutputValue(invTargetFps_1),
1320
- value: getNextOutputValue(1),
1321
- outTangent: getNextOutputValue(invTargetFps_1)
1322
- }); };
1323
- break;
1324
- }
1325
- }
1326
- var keys = new Array(data.input.length);
1327
- for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
1328
- keys[frameIndex] = getNextKey(frameIndex);
1329
- }
1330
- if (targetPath === "influence") {
1331
- var _loop_2 = function (targetIndex) {
1332
- var animationName = "".concat(babylonAnimationGroup.name, "_channel").concat(babylonAnimationGroup.targetedAnimations.length);
1333
- var babylonAnimation = new Animation(animationName, targetPath, _this.parent.targetFps, animationType);
1334
- babylonAnimation.setKeys(keys.map(function (key) { return ({
1335
- frame: key.frame,
1336
- inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
1337
- value: key.value[targetIndex],
1338
- outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined
1339
- }); }));
1340
- _this._forEachPrimitive(targetNode, function (babylonAbstractMesh) {
1341
- var babylonMesh = babylonAbstractMesh;
1342
- var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
1343
- var babylonAnimationClone = babylonAnimation.clone();
1344
- morphTarget.animations.push(babylonAnimationClone);
1345
- babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
1346
- });
1347
- };
1348
- for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
1349
- _loop_2(targetIndex);
1350
- }
1351
- }
1352
- else {
1353
- var animationName = "".concat(babylonAnimationGroup.name, "_channel").concat(babylonAnimationGroup.targetedAnimations.length);
1354
- var babylonAnimation = new Animation(animationName, targetPath, _this.parent.targetFps, animationType);
1355
- babylonAnimation.setKeys(keys);
1356
- if (animationTargetOverride != null && animationTargetOverride.animations != null) {
1357
- animationTargetOverride.animations.push(babylonAnimation);
1358
- babylonAnimationGroup.addTargetedAnimation(babylonAnimation, animationTargetOverride);
1359
- }
1360
- else {
1361
- targetNode._babylonTransformNode.animations.push(babylonAnimation);
1362
- babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonTransformNode);
1363
- }
1364
- }
1365
- });
1366
- };
1367
- GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
1368
- if (sampler._data) {
1369
- return sampler._data;
1370
- }
1371
- var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
1372
- switch (interpolation) {
1373
- case "STEP" /* STEP */:
1374
- case "LINEAR" /* LINEAR */:
1375
- case "CUBICSPLINE" /* CUBICSPLINE */: {
1376
- break;
1377
- }
1378
- default: {
1379
- throw new Error("".concat(context, "/interpolation: Invalid value (").concat(sampler.interpolation, ")"));
1380
- }
1381
- }
1382
- var inputAccessor = ArrayItem.Get("".concat(context, "/input"), this._gltf.accessors, sampler.input);
1383
- var outputAccessor = ArrayItem.Get("".concat(context, "/output"), this._gltf.accessors, sampler.output);
1384
- sampler._data = Promise.all([
1385
- this._loadFloatAccessorAsync("/accessors/".concat(inputAccessor.index), inputAccessor),
1386
- this._loadFloatAccessorAsync("/accessors/".concat(outputAccessor.index), outputAccessor)
1387
- ]).then(function (_a) {
1388
- var inputData = _a[0], outputData = _a[1];
1389
- return {
1390
- input: inputData,
1391
- interpolation: interpolation,
1392
- output: outputData,
1393
- };
1394
- });
1395
- return sampler._data;
1396
- };
1397
- /**
1398
- * Loads a glTF buffer.
1399
- * @param context The context when loading the asset
1400
- * @param buffer The glTF buffer property
1401
- * @param byteOffset The byte offset to use
1402
- * @param byteLength The byte length to use
1403
- * @returns A promise that resolves with the loaded data when the load is complete
1404
- */
1405
- GLTFLoader.prototype.loadBufferAsync = function (context, buffer, byteOffset, byteLength) {
1406
- var extensionPromise = this._extensionsLoadBufferAsync(context, buffer, byteOffset, byteLength);
1407
- if (extensionPromise) {
1408
- return extensionPromise;
1409
- }
1410
- if (!buffer._data) {
1411
- if (buffer.uri) {
1412
- buffer._data = this.loadUriAsync("".concat(context, "/uri"), buffer, buffer.uri);
1413
- }
1414
- else {
1415
- if (!this._bin) {
1416
- throw new Error("".concat(context, ": Uri is missing or the binary glTF is missing its binary chunk"));
1417
- }
1418
- buffer._data = this._bin.readAsync(0, buffer.byteLength);
1419
- }
1420
- }
1421
- return buffer._data.then(function (data) {
1422
- try {
1423
- return new Uint8Array(data.buffer, data.byteOffset + byteOffset, byteLength);
1424
- }
1425
- catch (e) {
1426
- throw new Error("".concat(context, ": ").concat(e.message));
1427
- }
1428
- });
1429
- };
1430
- /**
1431
- * Loads a glTF buffer view.
1432
- * @param context The context when loading the asset
1433
- * @param bufferView The glTF buffer view property
1434
- * @returns A promise that resolves with the loaded data when the load is complete
1435
- */
1436
- GLTFLoader.prototype.loadBufferViewAsync = function (context, bufferView) {
1437
- var extensionPromise = this._extensionsLoadBufferViewAsync(context, bufferView);
1438
- if (extensionPromise) {
1439
- return extensionPromise;
1440
- }
1441
- if (bufferView._data) {
1442
- return bufferView._data;
1443
- }
1444
- var buffer = ArrayItem.Get("".concat(context, "/buffer"), this._gltf.buffers, bufferView.buffer);
1445
- bufferView._data = this.loadBufferAsync("/buffers/".concat(buffer.index), buffer, (bufferView.byteOffset || 0), bufferView.byteLength);
1446
- return bufferView._data;
1447
- };
1448
- GLTFLoader.prototype._loadAccessorAsync = function (context, accessor, constructor) {
1449
- var _this = this;
1450
- if (accessor._data) {
1451
- return accessor._data;
1452
- }
1453
- var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
1454
- var byteStride = numComponents * VertexBuffer.GetTypeByteLength(accessor.componentType);
1455
- var length = numComponents * accessor.count;
1456
- if (accessor.bufferView == undefined) {
1457
- accessor._data = Promise.resolve(new constructor(length));
1458
- }
1459
- else {
1460
- var bufferView_1 = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1461
- accessor._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView_1.index), bufferView_1).then(function (data) {
1462
- if (accessor.componentType === 5126 /* FLOAT */ && !accessor.normalized && (!bufferView_1.byteStride || bufferView_1.byteStride === byteStride)) {
1463
- return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
1464
- }
1465
- else {
1466
- var typedArray_1 = new constructor(length);
1467
- VertexBuffer.ForEach(data, accessor.byteOffset || 0, bufferView_1.byteStride || byteStride, numComponents, accessor.componentType, typedArray_1.length, accessor.normalized || false, function (value, index) {
1468
- typedArray_1[index] = value;
1469
- });
1470
- return typedArray_1;
1471
- }
1472
- });
1473
- }
1474
- if (accessor.sparse) {
1475
- var sparse_1 = accessor.sparse;
1476
- accessor._data = accessor._data.then(function (data) {
1477
- var typedArray = data;
1478
- var indicesBufferView = ArrayItem.Get("".concat(context, "/sparse/indices/bufferView"), _this._gltf.bufferViews, sparse_1.indices.bufferView);
1479
- var valuesBufferView = ArrayItem.Get("".concat(context, "/sparse/values/bufferView"), _this._gltf.bufferViews, sparse_1.values.bufferView);
1480
- return Promise.all([
1481
- _this.loadBufferViewAsync("/bufferViews/".concat(indicesBufferView.index), indicesBufferView),
1482
- _this.loadBufferViewAsync("/bufferViews/".concat(valuesBufferView.index), valuesBufferView)
1483
- ]).then(function (_a) {
1484
- var indicesData = _a[0], valuesData = _a[1];
1485
- var indices = GLTFLoader._GetTypedArray("".concat(context, "/sparse/indices"), sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
1486
- var sparseLength = numComponents * sparse_1.count;
1487
- var values;
1488
- if (accessor.componentType === 5126 /* FLOAT */ && !accessor.normalized) {
1489
- values = GLTFLoader._GetTypedArray("".concat(context, "/sparse/values"), accessor.componentType, valuesData, sparse_1.values.byteOffset, sparseLength);
1490
- }
1491
- else {
1492
- var sparseData = GLTFLoader._GetTypedArray("".concat(context, "/sparse/values"), accessor.componentType, valuesData, sparse_1.values.byteOffset, sparseLength);
1493
- values = new constructor(sparseLength);
1494
- VertexBuffer.ForEach(sparseData, 0, byteStride, numComponents, accessor.componentType, values.length, accessor.normalized || false, function (value, index) {
1495
- values[index] = value;
1496
- });
1497
- }
1498
- var valuesIndex = 0;
1499
- for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
1500
- var dataIndex = indices[indicesIndex] * numComponents;
1501
- for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
1502
- typedArray[dataIndex++] = values[valuesIndex++];
1503
- }
1504
- }
1505
- return typedArray;
1506
- });
1507
- });
1508
- }
1509
- return accessor._data;
1510
- };
1511
- /** @hidden */
1512
- GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
1513
- return this._loadAccessorAsync(context, accessor, Float32Array);
1514
- };
1515
- GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
1516
- if (accessor.type !== "SCALAR" /* SCALAR */) {
1517
- throw new Error("".concat(context, "/type: Invalid value ").concat(accessor.type));
1518
- }
1519
- if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
1520
- accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
1521
- accessor.componentType !== 5125 /* UNSIGNED_INT */) {
1522
- throw new Error("".concat(context, "/componentType: Invalid value ").concat(accessor.componentType));
1523
- }
1524
- if (accessor._data) {
1525
- return accessor._data;
1526
- }
1527
- if (accessor.sparse) {
1528
- var constructor = GLTFLoader._GetTypedArrayConstructor("".concat(context, "/componentType"), accessor.componentType);
1529
- accessor._data = this._loadAccessorAsync(context, accessor, constructor);
1530
- }
1531
- else {
1532
- var bufferView = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1533
- accessor._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView).then(function (data) {
1534
- return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
1535
- });
1536
- }
1537
- return accessor._data;
1538
- };
1539
- GLTFLoader.prototype._loadVertexBufferViewAsync = function (bufferView, kind) {
1540
- var _this = this;
1541
- if (bufferView._babylonBuffer) {
1542
- return bufferView._babylonBuffer;
1543
- }
1544
- bufferView._babylonBuffer = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView).then(function (data) {
1545
- return new Buffer(_this._babylonScene.getEngine(), data, false);
1546
- });
1547
- return bufferView._babylonBuffer;
1548
- };
1549
- GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
1550
- var _this = this;
1551
- var _a;
1552
- if ((_a = accessor._babylonVertexBuffer) === null || _a === void 0 ? void 0 : _a[kind]) {
1553
- return accessor._babylonVertexBuffer[kind];
1554
- }
1555
- if (!accessor._babylonVertexBuffer) {
1556
- accessor._babylonVertexBuffer = {};
1557
- }
1558
- if (accessor.sparse) {
1559
- accessor._babylonVertexBuffer[kind] = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
1560
- return new VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
1561
- });
1562
- }
1563
- // Load joint indices as a float array since the shaders expect float data but glTF uses unsigned byte/short.
1564
- // This prevents certain platforms (e.g. D3D) from having to convert the data to float on the fly.
1565
- else if (kind === VertexBuffer.MatricesIndicesKind || kind === VertexBuffer.MatricesIndicesExtraKind) {
1566
- accessor._babylonVertexBuffer[kind] = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
1567
- return new VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
1568
- });
1569
- }
1570
- else {
1571
- var bufferView_2 = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1572
- accessor._babylonVertexBuffer[kind] = this._loadVertexBufferViewAsync(bufferView_2, kind).then(function (babylonBuffer) {
1573
- var size = GLTFLoader._GetNumComponents(context, accessor.type);
1574
- return new VertexBuffer(_this._babylonScene.getEngine(), babylonBuffer, kind, false, false, bufferView_2.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true, 1, true);
1575
- });
1576
- }
1577
- return accessor._babylonVertexBuffer[kind];
1578
- };
1579
- GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, properties, babylonMaterial) {
1580
- if (!(babylonMaterial instanceof PBRMaterial)) {
1581
- throw new Error("".concat(context, ": Material type not supported"));
1582
- }
1583
- var promises = new Array();
1584
- if (properties) {
1585
- if (properties.baseColorFactor) {
1586
- babylonMaterial.albedoColor = Color3.FromArray(properties.baseColorFactor);
1587
- babylonMaterial.alpha = properties.baseColorFactor[3];
1588
- }
1589
- else {
1590
- babylonMaterial.albedoColor = Color3.White();
1591
- }
1592
- babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
1593
- babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
1594
- if (properties.baseColorTexture) {
1595
- promises.push(this.loadTextureInfoAsync("".concat(context, "/baseColorTexture"), properties.baseColorTexture, function (texture) {
1596
- texture.name = "".concat(babylonMaterial.name, " (Base Color)");
1597
- babylonMaterial.albedoTexture = texture;
1598
- }));
1599
- }
1600
- if (properties.metallicRoughnessTexture) {
1601
- properties.metallicRoughnessTexture.nonColorData = true;
1602
- promises.push(this.loadTextureInfoAsync("".concat(context, "/metallicRoughnessTexture"), properties.metallicRoughnessTexture, function (texture) {
1603
- texture.name = "".concat(babylonMaterial.name, " (Metallic Roughness)");
1604
- babylonMaterial.metallicTexture = texture;
1605
- }));
1606
- babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
1607
- babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
1608
- babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
1609
- }
1610
- }
1611
- return Promise.all(promises).then(function () { });
1612
- };
1613
- /** @hidden */
1614
- GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
1615
- if (assign === void 0) { assign = function () { }; }
1616
- var extensionPromise = this._extensionsLoadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign);
1617
- if (extensionPromise) {
1618
- return extensionPromise;
1619
- }
1620
- material._data = material._data || {};
1621
- var babylonData = material._data[babylonDrawMode];
1622
- if (!babylonData) {
1623
- this.logOpen("".concat(context, " ").concat(material.name || ""));
1624
- var babylonMaterial = this.createMaterial(context, material, babylonDrawMode);
1625
- babylonData = {
1626
- babylonMaterial: babylonMaterial,
1627
- babylonMeshes: [],
1628
- promise: this.loadMaterialPropertiesAsync(context, material, babylonMaterial)
1629
- };
1630
- material._data[babylonDrawMode] = babylonData;
1631
- GLTFLoader.AddPointerMetadata(babylonMaterial, context);
1632
- this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
1633
- this.logClose();
1634
- }
1635
- if (babylonMesh) {
1636
- babylonData.babylonMeshes.push(babylonMesh);
1637
- babylonMesh.onDisposeObservable.addOnce(function () {
1638
- var index = babylonData.babylonMeshes.indexOf(babylonMesh);
1639
- if (index !== -1) {
1640
- babylonData.babylonMeshes.splice(index, 1);
1641
- }
1642
- });
1643
- }
1644
- assign(babylonData.babylonMaterial);
1645
- return babylonData.promise.then(function () {
1646
- return babylonData.babylonMaterial;
1647
- });
1648
- };
1649
- GLTFLoader.prototype._createDefaultMaterial = function (name, babylonDrawMode) {
1650
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
1651
- var babylonMaterial = new PBRMaterial(name, this._babylonScene);
1652
- babylonMaterial._parentContainer = this._assetContainer;
1653
- this._babylonScene._blockEntityCollection = false;
1654
- // Moved to mesh so user can change materials on gltf meshes: babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
1655
- babylonMaterial.fillMode = babylonDrawMode;
1656
- babylonMaterial.enableSpecularAntiAliasing = true;
1657
- babylonMaterial.useRadianceOverAlpha = !this._parent.transparencyAsCoverage;
1658
- babylonMaterial.useSpecularOverAlpha = !this._parent.transparencyAsCoverage;
1659
- babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1660
- babylonMaterial.metallic = 1;
1661
- babylonMaterial.roughness = 1;
1662
- return babylonMaterial;
1663
- };
1664
- /**
1665
- * Creates a Babylon material from a glTF material.
1666
- * @param context The context when loading the asset
1667
- * @param material The glTF material property
1668
- * @param babylonDrawMode The draw mode for the Babylon material
1669
- * @returns The Babylon material
1670
- */
1671
- GLTFLoader.prototype.createMaterial = function (context, material, babylonDrawMode) {
1672
- var extensionPromise = this._extensionsCreateMaterial(context, material, babylonDrawMode);
1673
- if (extensionPromise) {
1674
- return extensionPromise;
1675
- }
1676
- var name = material.name || "material".concat(material.index);
1677
- var babylonMaterial = this._createDefaultMaterial(name, babylonDrawMode);
1678
- return babylonMaterial;
1679
- };
1680
- /**
1681
- * Loads properties from a glTF material into a Babylon material.
1682
- * @param context The context when loading the asset
1683
- * @param material The glTF material property
1684
- * @param babylonMaterial The Babylon material
1685
- * @returns A promise that resolves when the load is complete
1686
- */
1687
- GLTFLoader.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
1688
- var extensionPromise = this._extensionsLoadMaterialPropertiesAsync(context, material, babylonMaterial);
1689
- if (extensionPromise) {
1690
- return extensionPromise;
1691
- }
1692
- var promises = new Array();
1693
- promises.push(this.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
1694
- if (material.pbrMetallicRoughness) {
1695
- promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync("".concat(context, "/pbrMetallicRoughness"), material.pbrMetallicRoughness, babylonMaterial));
1696
- }
1697
- this.loadMaterialAlphaProperties(context, material, babylonMaterial);
1698
- return Promise.all(promises).then(function () { });
1699
- };
1700
- /**
1701
- * Loads the normal, occlusion, and emissive properties from a glTF material into a Babylon material.
1702
- * @param context The context when loading the asset
1703
- * @param material The glTF material property
1704
- * @param babylonMaterial The Babylon material
1705
- * @returns A promise that resolves when the load is complete
1706
- */
1707
- GLTFLoader.prototype.loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
1708
- if (!(babylonMaterial instanceof PBRMaterial)) {
1709
- throw new Error("".concat(context, ": Material type not supported"));
1710
- }
1711
- var promises = new Array();
1712
- babylonMaterial.emissiveColor = material.emissiveFactor ? Color3.FromArray(material.emissiveFactor) : new Color3(0, 0, 0);
1713
- if (material.doubleSided) {
1714
- babylonMaterial.backFaceCulling = false;
1715
- babylonMaterial.twoSidedLighting = true;
1716
- }
1717
- if (material.normalTexture) {
1718
- material.normalTexture.nonColorData = true;
1719
- promises.push(this.loadTextureInfoAsync("".concat(context, "/normalTexture"), material.normalTexture, function (texture) {
1720
- texture.name = "".concat(babylonMaterial.name, " (Normal)");
1721
- babylonMaterial.bumpTexture = texture;
1722
- }));
1723
- babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
1724
- babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
1725
- if (material.normalTexture.scale != undefined) {
1726
- babylonMaterial.bumpTexture.level = material.normalTexture.scale;
1727
- }
1728
- babylonMaterial.forceIrradianceInFragment = true;
1729
- }
1730
- if (material.occlusionTexture) {
1731
- material.occlusionTexture.nonColorData = true;
1732
- promises.push(this.loadTextureInfoAsync("".concat(context, "/occlusionTexture"), material.occlusionTexture, function (texture) {
1733
- texture.name = "".concat(babylonMaterial.name, " (Occlusion)");
1734
- babylonMaterial.ambientTexture = texture;
1735
- }));
1736
- babylonMaterial.useAmbientInGrayScale = true;
1737
- if (material.occlusionTexture.strength != undefined) {
1738
- babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
1739
- }
1740
- }
1741
- if (material.emissiveTexture) {
1742
- promises.push(this.loadTextureInfoAsync("".concat(context, "/emissiveTexture"), material.emissiveTexture, function (texture) {
1743
- texture.name = "".concat(babylonMaterial.name, " (Emissive)");
1744
- babylonMaterial.emissiveTexture = texture;
1745
- }));
1746
- }
1747
- return Promise.all(promises).then(function () { });
1748
- };
1749
- /**
1750
- * Loads the alpha properties from a glTF material into a Babylon material.
1751
- * Must be called after the setting the albedo texture of the Babylon material when the material has an albedo texture.
1752
- * @param context The context when loading the asset
1753
- * @param material The glTF material property
1754
- * @param babylonMaterial The Babylon material
1755
- */
1756
- GLTFLoader.prototype.loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
1757
- if (!(babylonMaterial instanceof PBRMaterial)) {
1758
- throw new Error("".concat(context, ": Material type not supported"));
1759
- }
1760
- var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
1761
- switch (alphaMode) {
1762
- case "OPAQUE" /* OPAQUE */: {
1763
- babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1764
- break;
1765
- }
1766
- case "MASK" /* MASK */: {
1767
- babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHATEST;
1768
- babylonMaterial.alphaCutOff = (material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff);
1769
- if (babylonMaterial.albedoTexture) {
1770
- babylonMaterial.albedoTexture.hasAlpha = true;
1771
- }
1772
- break;
1773
- }
1774
- case "BLEND" /* BLEND */: {
1775
- babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
1776
- if (babylonMaterial.albedoTexture) {
1777
- babylonMaterial.albedoTexture.hasAlpha = true;
1778
- babylonMaterial.useAlphaFromAlbedoTexture = true;
1779
- }
1780
- break;
1781
- }
1782
- default: {
1783
- throw new Error("".concat(context, "/alphaMode: Invalid value (").concat(material.alphaMode, ")"));
1784
- }
1785
- }
1786
- };
1787
- /**
1788
- * Loads a glTF texture info.
1789
- * @param context The context when loading the asset
1790
- * @param textureInfo The glTF texture info property
1791
- * @param assign A function called synchronously after parsing the glTF properties
1792
- * @returns A promise that resolves with the loaded Babylon texture when the load is complete
1793
- */
1794
- GLTFLoader.prototype.loadTextureInfoAsync = function (context, textureInfo, assign) {
1795
- var _this = this;
1796
- if (assign === void 0) { assign = function () { }; }
1797
- var extensionPromise = this._extensionsLoadTextureInfoAsync(context, textureInfo, assign);
1798
- if (extensionPromise) {
1799
- return extensionPromise;
1800
- }
1801
- this.logOpen("".concat(context));
1802
- if (textureInfo.texCoord >= 6) {
1803
- throw new Error("".concat(context, "/texCoord: Invalid value (").concat(textureInfo.texCoord, ")"));
1804
- }
1805
- var texture = ArrayItem.Get("".concat(context, "/index"), this._gltf.textures, textureInfo.index);
1806
- texture._textureInfo = textureInfo;
1807
- var promise = this._loadTextureAsync("/textures/".concat(textureInfo.index), texture, function (babylonTexture) {
1808
- babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
1809
- GLTFLoader.AddPointerMetadata(babylonTexture, context);
1810
- _this._parent.onTextureLoadedObservable.notifyObservers(babylonTexture);
1811
- assign(babylonTexture);
1812
- });
1813
- this.logClose();
1814
- return promise;
1815
- };
1816
- /** @hidden */
1817
- GLTFLoader.prototype._loadTextureAsync = function (context, texture, assign) {
1818
- if (assign === void 0) { assign = function () { }; }
1819
- var extensionPromise = this._extensionsLoadTextureAsync(context, texture, assign);
1820
- if (extensionPromise) {
1821
- return extensionPromise;
1822
- }
1823
- this.logOpen("".concat(context, " ").concat(texture.name || ""));
1824
- var sampler = (texture.sampler == undefined ? GLTFLoader.DefaultSampler : ArrayItem.Get("".concat(context, "/sampler"), this._gltf.samplers, texture.sampler));
1825
- var image = ArrayItem.Get("".concat(context, "/source"), this._gltf.images, texture.source);
1826
- var promise = this._createTextureAsync(context, sampler, image, assign, undefined, !texture._textureInfo.nonColorData);
1827
- this.logClose();
1828
- return promise;
1829
- };
1830
- /** @hidden */
1831
- GLTFLoader.prototype._createTextureAsync = function (context, sampler, image, assign, textureLoaderOptions, useSRGBBuffer) {
1832
- var _this = this;
1833
- if (assign === void 0) { assign = function () { }; }
1834
- var samplerData = this._loadSampler("/samplers/".concat(sampler.index), sampler);
1835
- var promises = new Array();
1836
- var deferred = new Deferred();
1837
- this._babylonScene._blockEntityCollection = !!this._assetContainer;
1838
- var textureCreationOptions = {
1839
- noMipmap: samplerData.noMipMaps,
1840
- invertY: false,
1841
- samplingMode: samplerData.samplingMode,
1842
- onLoad: function () {
1843
- if (!_this._disposed) {
1844
- deferred.resolve();
1845
- }
1846
- },
1847
- onError: function (message, exception) {
1848
- if (!_this._disposed) {
1849
- deferred.reject(new Error("".concat(context, ": ").concat((exception && exception.message) ? exception.message : message || "Failed to load texture")));
1850
- }
1851
- },
1852
- mimeType: image.mimeType,
1853
- loaderOptions: textureLoaderOptions,
1854
- useSRGBBuffer: !!useSRGBBuffer && this._parent.useSRGBBuffers,
1855
- };
1856
- var babylonTexture = new Texture(null, this._babylonScene, textureCreationOptions);
1857
- babylonTexture._parentContainer = this._assetContainer;
1858
- this._babylonScene._blockEntityCollection = false;
1859
- promises.push(deferred.promise);
1860
- promises.push(this.loadImageAsync("/images/".concat(image.index), image).then(function (data) {
1861
- var name = image.uri || "".concat(_this._fileName, "#image").concat(image.index);
1862
- var dataUrl = "data:".concat(_this._uniqueRootUrl).concat(name);
1863
- babylonTexture.updateURL(dataUrl, data);
1864
- }));
1865
- babylonTexture.wrapU = samplerData.wrapU;
1866
- babylonTexture.wrapV = samplerData.wrapV;
1867
- assign(babylonTexture);
1868
- return Promise.all(promises).then(function () {
1869
- return babylonTexture;
1870
- });
1871
- };
1872
- GLTFLoader.prototype._loadSampler = function (context, sampler) {
1873
- if (!sampler._data) {
1874
- sampler._data = {
1875
- noMipMaps: (sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */),
1876
- samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler),
1877
- wrapU: GLTFLoader._GetTextureWrapMode("".concat(context, "/wrapS"), sampler.wrapS),
1878
- wrapV: GLTFLoader._GetTextureWrapMode("".concat(context, "/wrapT"), sampler.wrapT)
1879
- };
1880
- }
1881
- return sampler._data;
1882
- };
1883
- /**
1884
- * Loads a glTF image.
1885
- * @param context The context when loading the asset
1886
- * @param image The glTF image property
1887
- * @returns A promise that resolves with the loaded data when the load is complete
1888
- */
1889
- GLTFLoader.prototype.loadImageAsync = function (context, image) {
1890
- if (!image._data) {
1891
- this.logOpen("".concat(context, " ").concat(image.name || ""));
1892
- if (image.uri) {
1893
- image._data = this.loadUriAsync("".concat(context, "/uri"), image, image.uri);
1894
- }
1895
- else {
1896
- var bufferView = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, image.bufferView);
1897
- image._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView);
1898
- }
1899
- this.logClose();
1900
- }
1901
- return image._data;
1902
- };
1903
- /**
1904
- * Loads a glTF uri.
1905
- * @param context The context when loading the asset
1906
- * @param property The glTF property associated with the uri
1907
- * @param uri The base64 or relative uri
1908
- * @returns A promise that resolves with the loaded data when the load is complete
1909
- */
1910
- GLTFLoader.prototype.loadUriAsync = function (context, property, uri) {
1911
- var _this = this;
1912
- var extensionPromise = this._extensionsLoadUriAsync(context, property, uri);
1913
- if (extensionPromise) {
1914
- return extensionPromise;
1915
- }
1916
- if (!GLTFLoader._ValidateUri(uri)) {
1917
- throw new Error("".concat(context, ": '").concat(uri, "' is invalid"));
1918
- }
1919
- if (IsBase64DataUrl(uri)) {
1920
- var data = new Uint8Array(DecodeBase64UrlToBinary(uri));
1921
- this.log("".concat(context, ": Decoded ").concat(uri.substr(0, 64), "... (").concat(data.length, " bytes)"));
1922
- return Promise.resolve(data);
1923
- }
1924
- this.log("".concat(context, ": Loading ").concat(uri));
1925
- return this._parent.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
1926
- return new Promise(function (resolve, reject) {
1927
- _this._parent._loadFile(_this._babylonScene, url, function (data) {
1928
- if (!_this._disposed) {
1929
- _this.log("".concat(context, ": Loaded ").concat(uri, " (").concat(data.byteLength, " bytes)"));
1930
- resolve(new Uint8Array(data));
1931
- }
1932
- }, true, function (request) {
1933
- reject(new LoadFileError("".concat(context, ": Failed to load '").concat(uri, "'").concat(request ? ": " + request.status + " " + request.statusText : ""), request));
1934
- });
1935
- });
1936
- });
1937
- };
1938
- /**
1939
- * Adds a JSON pointer to the metadata of the Babylon object at `<object>.metadata.gltf.pointers`.
1940
- * @param babylonObject the Babylon object with metadata
1941
- * @param pointer the JSON pointer
1942
- */
1943
- GLTFLoader.AddPointerMetadata = function (babylonObject, pointer) {
1944
- var metadata = (babylonObject.metadata = babylonObject.metadata || {});
1945
- var gltf = (metadata.gltf = metadata.gltf || {});
1946
- var pointers = (gltf.pointers = gltf.pointers || []);
1947
- pointers.push(pointer);
1948
- };
1949
- GLTFLoader._GetTextureWrapMode = function (context, mode) {
1950
- // Set defaults if undefined
1951
- mode = mode == undefined ? 10497 /* REPEAT */ : mode;
1952
- switch (mode) {
1953
- case 33071 /* CLAMP_TO_EDGE */: return Texture.CLAMP_ADDRESSMODE;
1954
- case 33648 /* MIRRORED_REPEAT */: return Texture.MIRROR_ADDRESSMODE;
1955
- case 10497 /* REPEAT */: return Texture.WRAP_ADDRESSMODE;
1956
- default:
1957
- Logger.Warn("".concat(context, ": Invalid value (").concat(mode, ")"));
1958
- return Texture.WRAP_ADDRESSMODE;
1959
- }
1960
- };
1961
- GLTFLoader._GetTextureSamplingMode = function (context, sampler) {
1962
- // Set defaults if undefined
1963
- var magFilter = sampler.magFilter == undefined ? 9729 /* LINEAR */ : sampler.magFilter;
1964
- var minFilter = sampler.minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : sampler.minFilter;
1965
- if (magFilter === 9729 /* LINEAR */) {
1966
- switch (minFilter) {
1967
- case 9728 /* NEAREST */: return Texture.LINEAR_NEAREST;
1968
- case 9729 /* LINEAR */: return Texture.LINEAR_LINEAR;
1969
- case 9984 /* NEAREST_MIPMAP_NEAREST */: return Texture.LINEAR_NEAREST_MIPNEAREST;
1970
- case 9985 /* LINEAR_MIPMAP_NEAREST */: return Texture.LINEAR_LINEAR_MIPNEAREST;
1971
- case 9986 /* NEAREST_MIPMAP_LINEAR */: return Texture.LINEAR_NEAREST_MIPLINEAR;
1972
- case 9987 /* LINEAR_MIPMAP_LINEAR */: return Texture.LINEAR_LINEAR_MIPLINEAR;
1973
- default:
1974
- Logger.Warn("".concat(context, "/minFilter: Invalid value (").concat(minFilter, ")"));
1975
- return Texture.LINEAR_LINEAR_MIPLINEAR;
1976
- }
1977
- }
1978
- else {
1979
- if (magFilter !== 9728 /* NEAREST */) {
1980
- Logger.Warn("".concat(context, "/magFilter: Invalid value (").concat(magFilter, ")"));
1981
- }
1982
- switch (minFilter) {
1983
- case 9728 /* NEAREST */: return Texture.NEAREST_NEAREST;
1984
- case 9729 /* LINEAR */: return Texture.NEAREST_LINEAR;
1985
- case 9984 /* NEAREST_MIPMAP_NEAREST */: return Texture.NEAREST_NEAREST_MIPNEAREST;
1986
- case 9985 /* LINEAR_MIPMAP_NEAREST */: return Texture.NEAREST_LINEAR_MIPNEAREST;
1987
- case 9986 /* NEAREST_MIPMAP_LINEAR */: return Texture.NEAREST_NEAREST_MIPLINEAR;
1988
- case 9987 /* LINEAR_MIPMAP_LINEAR */: return Texture.NEAREST_LINEAR_MIPLINEAR;
1989
- default:
1990
- Logger.Warn("".concat(context, "/minFilter: Invalid value (").concat(minFilter, ")"));
1991
- return Texture.NEAREST_NEAREST_MIPNEAREST;
1992
- }
1993
- }
1994
- };
1995
- GLTFLoader._GetTypedArrayConstructor = function (context, componentType) {
1996
- switch (componentType) {
1997
- case 5120 /* BYTE */: return Int8Array;
1998
- case 5121 /* UNSIGNED_BYTE */: return Uint8Array;
1999
- case 5122 /* SHORT */: return Int16Array;
2000
- case 5123 /* UNSIGNED_SHORT */: return Uint16Array;
2001
- case 5125 /* UNSIGNED_INT */: return Uint32Array;
2002
- case 5126 /* FLOAT */: return Float32Array;
2003
- default: throw new Error("".concat(context, ": Invalid component type ").concat(componentType));
2004
- }
2005
- };
2006
- GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
2007
- var buffer = bufferView.buffer;
2008
- byteOffset = bufferView.byteOffset + (byteOffset || 0);
2009
- var constructor = GLTFLoader._GetTypedArrayConstructor("".concat(context, "/componentType"), componentType);
2010
- var componentTypeLength = VertexBuffer.GetTypeByteLength(componentType);
2011
- if (byteOffset % componentTypeLength !== 0) {
2012
- // HACK: Copy the buffer if byte offset is not a multiple of component type byte length.
2013
- Logger.Warn("".concat(context, ": Copying buffer as byte offset (").concat(byteOffset, ") is not a multiple of component type byte length (").concat(componentTypeLength, ")"));
2014
- return new constructor(buffer.slice(byteOffset, byteOffset + length * componentTypeLength), 0);
2015
- }
2016
- return new constructor(buffer, byteOffset, length);
2017
- };
2018
- GLTFLoader._GetNumComponents = function (context, type) {
2019
- switch (type) {
2020
- case "SCALAR": return 1;
2021
- case "VEC2": return 2;
2022
- case "VEC3": return 3;
2023
- case "VEC4": return 4;
2024
- case "MAT2": return 4;
2025
- case "MAT3": return 9;
2026
- case "MAT4": return 16;
2027
- }
2028
- throw new Error("".concat(context, ": Invalid type (").concat(type, ")"));
2029
- };
2030
- GLTFLoader._ValidateUri = function (uri) {
2031
- return (Tools.IsBase64(uri) || uri.indexOf("..") === -1);
2032
- };
2033
- /** @hidden */
2034
- GLTFLoader._GetDrawMode = function (context, mode) {
2035
- if (mode == undefined) {
2036
- mode = 4 /* TRIANGLES */;
2037
- }
2038
- switch (mode) {
2039
- case 0 /* POINTS */: return Material.PointListDrawMode;
2040
- case 1 /* LINES */: return Material.LineListDrawMode;
2041
- case 2 /* LINE_LOOP */: return Material.LineLoopDrawMode;
2042
- case 3 /* LINE_STRIP */: return Material.LineStripDrawMode;
2043
- case 4 /* TRIANGLES */: return Material.TriangleFillMode;
2044
- case 5 /* TRIANGLE_STRIP */: return Material.TriangleStripDrawMode;
2045
- case 6 /* TRIANGLE_FAN */: return Material.TriangleFanDrawMode;
2046
- }
2047
- throw new Error("".concat(context, ": Invalid mesh primitive mode (").concat(mode, ")"));
2048
- };
2049
- GLTFLoader.prototype._compileMaterialsAsync = function () {
2050
- var _this = this;
2051
- this._parent._startPerformanceCounter("Compile materials");
2052
- var promises = new Array();
2053
- if (this._gltf.materials) {
2054
- for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
2055
- var material = _a[_i];
2056
- if (material._data) {
2057
- for (var babylonDrawMode in material._data) {
2058
- var babylonData = material._data[babylonDrawMode];
2059
- for (var _b = 0, _c = babylonData.babylonMeshes; _b < _c.length; _b++) {
2060
- var babylonMesh = _c[_b];
2061
- // Ensure nonUniformScaling is set if necessary.
2062
- babylonMesh.computeWorldMatrix(true);
2063
- var babylonMaterial = babylonData.babylonMaterial;
2064
- promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
2065
- promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { useInstances: true }));
2066
- if (this._parent.useClipPlane) {
2067
- promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
2068
- promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true, useInstances: true }));
2069
- }
2070
- }
2071
- }
2072
- }
2073
- }
2074
- }
2075
- return Promise.all(promises).then(function () {
2076
- _this._parent._endPerformanceCounter("Compile materials");
2077
- });
2078
- };
2079
- GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
2080
- var _this = this;
2081
- this._parent._startPerformanceCounter("Compile shadow generators");
2082
- var promises = new Array();
2083
- var lights = this._babylonScene.lights;
2084
- for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
2085
- var light = lights_1[_i];
2086
- var generator = light.getShadowGenerator();
2087
- if (generator) {
2088
- promises.push(generator.forceCompilationAsync());
2089
- }
2090
- }
2091
- return Promise.all(promises).then(function () {
2092
- _this._parent._endPerformanceCounter("Compile shadow generators");
2093
- });
2094
- };
2095
- GLTFLoader.prototype._forEachExtensions = function (action) {
2096
- for (var _i = 0, _a = this._extensions; _i < _a.length; _i++) {
2097
- var extension = _a[_i];
2098
- if (extension.enabled) {
2099
- action(extension);
2100
- }
2101
- }
2102
- };
2103
- GLTFLoader.prototype._applyExtensions = function (property, functionName, actionAsync) {
2104
- for (var _i = 0, _a = this._extensions; _i < _a.length; _i++) {
2105
- var extension = _a[_i];
2106
- if (extension.enabled) {
2107
- var id = "".concat(extension.name, ".").concat(functionName);
2108
- var loaderProperty = property;
2109
- loaderProperty._activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions || {};
2110
- var activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions;
2111
- if (!activeLoaderExtensionFunctions[id]) {
2112
- activeLoaderExtensionFunctions[id] = true;
2113
- try {
2114
- var result = actionAsync(extension);
2115
- if (result) {
2116
- return result;
2117
- }
2118
- }
2119
- finally {
2120
- delete activeLoaderExtensionFunctions[id];
2121
- }
2122
- }
2123
- }
2124
- }
2125
- return null;
2126
- };
2127
- GLTFLoader.prototype._extensionsOnLoading = function () {
2128
- this._forEachExtensions(function (extension) { return extension.onLoading && extension.onLoading(); });
2129
- };
2130
- GLTFLoader.prototype._extensionsOnReady = function () {
2131
- this._forEachExtensions(function (extension) { return extension.onReady && extension.onReady(); });
2132
- };
2133
- GLTFLoader.prototype._extensionsLoadSceneAsync = function (context, scene) {
2134
- return this._applyExtensions(scene, "loadScene", function (extension) { return extension.loadSceneAsync && extension.loadSceneAsync(context, scene); });
2135
- };
2136
- GLTFLoader.prototype._extensionsLoadNodeAsync = function (context, node, assign) {
2137
- return this._applyExtensions(node, "loadNode", function (extension) { return extension.loadNodeAsync && extension.loadNodeAsync(context, node, assign); });
2138
- };
2139
- GLTFLoader.prototype._extensionsLoadCameraAsync = function (context, camera, assign) {
2140
- return this._applyExtensions(camera, "loadCamera", function (extension) { return extension.loadCameraAsync && extension.loadCameraAsync(context, camera, assign); });
2141
- };
2142
- GLTFLoader.prototype._extensionsLoadVertexDataAsync = function (context, primitive, babylonMesh) {
2143
- return this._applyExtensions(primitive, "loadVertexData", function (extension) { return extension._loadVertexDataAsync && extension._loadVertexDataAsync(context, primitive, babylonMesh); });
2144
- };
2145
- GLTFLoader.prototype._extensionsLoadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
2146
- return this._applyExtensions(primitive, "loadMeshPrimitive", function (extension) { return extension._loadMeshPrimitiveAsync && extension._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign); });
2147
- };
2148
- GLTFLoader.prototype._extensionsLoadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
2149
- return this._applyExtensions(material, "loadMaterial", function (extension) { return extension._loadMaterialAsync && extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
2150
- };
2151
- GLTFLoader.prototype._extensionsCreateMaterial = function (context, material, babylonDrawMode) {
2152
- return this._applyExtensions(material, "createMaterial", function (extension) { return extension.createMaterial && extension.createMaterial(context, material, babylonDrawMode); });
2153
- };
2154
- GLTFLoader.prototype._extensionsLoadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
2155
- return this._applyExtensions(material, "loadMaterialProperties", function (extension) { return extension.loadMaterialPropertiesAsync && extension.loadMaterialPropertiesAsync(context, material, babylonMaterial); });
2156
- };
2157
- GLTFLoader.prototype._extensionsLoadTextureInfoAsync = function (context, textureInfo, assign) {
2158
- return this._applyExtensions(textureInfo, "loadTextureInfo", function (extension) { return extension.loadTextureInfoAsync && extension.loadTextureInfoAsync(context, textureInfo, assign); });
2159
- };
2160
- GLTFLoader.prototype._extensionsLoadTextureAsync = function (context, texture, assign) {
2161
- return this._applyExtensions(texture, "loadTexture", function (extension) { return extension._loadTextureAsync && extension._loadTextureAsync(context, texture, assign); });
2162
- };
2163
- GLTFLoader.prototype._extensionsLoadAnimationAsync = function (context, animation) {
2164
- return this._applyExtensions(animation, "loadAnimation", function (extension) { return extension.loadAnimationAsync && extension.loadAnimationAsync(context, animation); });
2165
- };
2166
- GLTFLoader.prototype._extensionsLoadSkinAsync = function (context, node, skin) {
2167
- return this._applyExtensions(skin, "loadSkin", function (extension) { return extension._loadSkinAsync && extension._loadSkinAsync(context, node, skin); });
2168
- };
2169
- GLTFLoader.prototype._extensionsLoadUriAsync = function (context, property, uri) {
2170
- return this._applyExtensions(property, "loadUri", function (extension) { return extension._loadUriAsync && extension._loadUriAsync(context, property, uri); });
2171
- };
2172
- GLTFLoader.prototype._extensionsLoadBufferViewAsync = function (context, bufferView) {
2173
- return this._applyExtensions(bufferView, "loadBufferView", function (extension) { return extension.loadBufferViewAsync && extension.loadBufferViewAsync(context, bufferView); });
2174
- };
2175
- GLTFLoader.prototype._extensionsLoadBufferAsync = function (context, buffer, byteOffset, byteLength) {
2176
- return this._applyExtensions(buffer, "loadBuffer", function (extension) { return extension.loadBufferAsync && extension.loadBufferAsync(context, buffer, byteOffset, byteLength); });
2177
- };
2178
- /**
2179
- * Helper method called by a loader extension to load an glTF extension.
2180
- * @param context The context when loading the asset
2181
- * @param property The glTF property to load the extension from
2182
- * @param extensionName The name of the extension to load
2183
- * @param actionAsync The action to run
2184
- * @returns The promise returned by actionAsync or null if the extension does not exist
2185
- */
2186
- GLTFLoader.LoadExtensionAsync = function (context, property, extensionName, actionAsync) {
2187
- if (!property.extensions) {
2188
- return null;
2189
- }
2190
- var extensions = property.extensions;
2191
- var extension = extensions[extensionName];
2192
- if (!extension) {
2193
- return null;
2194
- }
2195
- return actionAsync("".concat(context, "/extensions/").concat(extensionName), extension);
2196
- };
2197
- /**
2198
- * Helper method called by a loader extension to load a glTF extra.
2199
- * @param context The context when loading the asset
2200
- * @param property The glTF property to load the extra from
2201
- * @param extensionName The name of the extension to load
2202
- * @param actionAsync The action to run
2203
- * @returns The promise returned by actionAsync or null if the extra does not exist
2204
- */
2205
- GLTFLoader.LoadExtraAsync = function (context, property, extensionName, actionAsync) {
2206
- if (!property.extras) {
2207
- return null;
2208
- }
2209
- var extras = property.extras;
2210
- var extra = extras[extensionName];
2211
- if (!extra) {
2212
- return null;
2213
- }
2214
- return actionAsync("".concat(context, "/extras/").concat(extensionName), extra);
2215
- };
2216
- /**
2217
- * Checks for presence of an extension.
2218
- * @param name The name of the extension to check
2219
- * @returns A boolean indicating the presence of the given extension name in `extensionsUsed`
2220
- */
2221
- GLTFLoader.prototype.isExtensionUsed = function (name) {
2222
- return !!this._gltf.extensionsUsed && this._gltf.extensionsUsed.indexOf(name) !== -1;
2223
- };
2224
- /**
2225
- * Increments the indentation level and logs a message.
2226
- * @param message The message to log
2227
- */
2228
- GLTFLoader.prototype.logOpen = function (message) {
2229
- this._parent._logOpen(message);
2230
- };
2231
- /**
2232
- * Decrements the indentation level.
2233
- */
2234
- GLTFLoader.prototype.logClose = function () {
2235
- this._parent._logClose();
2236
- };
2237
- /**
2238
- * Logs a message
2239
- * @param message The message to log
2240
- */
2241
- GLTFLoader.prototype.log = function (message) {
2242
- this._parent._log(message);
2243
- };
2244
- /**
2245
- * Starts a performance counter.
2246
- * @param counterName The name of the performance counter
2247
- */
2248
- GLTFLoader.prototype.startPerformanceCounter = function (counterName) {
2249
- this._parent._startPerformanceCounter(counterName);
2250
- };
2251
- /**
2252
- * Ends a performance counter.
2253
- * @param counterName The name of the performance counter
2254
- */
2255
- GLTFLoader.prototype.endPerformanceCounter = function (counterName) {
2256
- this._parent._endPerformanceCounter(counterName);
2257
- };
2258
- GLTFLoader._RegisteredExtensions = {};
2259
- /**
2260
- * The default glTF sampler.
2261
- */
2262
- GLTFLoader.DefaultSampler = { index: -1 };
2263
- return GLTFLoader;
2264
- }());
2265
- export { GLTFLoader };
2266
- GLTFFileLoader._CreateGLTF2Loader = function (parent) { return new GLTFLoader(parent); };
1
+ import { Deferred } from "@babylonjs/core/Misc/deferred.js";
2
+ import { Quaternion, Vector3, Matrix, TmpVectors } from "@babylonjs/core/Maths/math.vector.js";
3
+ import { Color3 } from "@babylonjs/core/Maths/math.color.js";
4
+ import { Tools } from "@babylonjs/core/Misc/tools.js";
5
+ import { Camera } from "@babylonjs/core/Cameras/camera.js";
6
+ import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera.js";
7
+ import { AnimationGroup } from "@babylonjs/core/Animations/animationGroup.js";
8
+ import { Animation } from "@babylonjs/core/Animations/animation.js";
9
+ import { Bone } from "@babylonjs/core/Bones/bone.js";
10
+ import { Skeleton } from "@babylonjs/core/Bones/skeleton.js";
11
+ import { Material } from "@babylonjs/core/Materials/material.js";
12
+ import { PBRMaterial } from "@babylonjs/core/Materials/PBR/pbrMaterial.js";
13
+ import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
14
+ import { TransformNode } from "@babylonjs/core/Meshes/transformNode.js";
15
+ import { Buffer, VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
16
+ import { Geometry } from "@babylonjs/core/Meshes/geometry.js";
17
+ import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
18
+ import { MorphTarget } from "@babylonjs/core/Morph/morphTarget.js";
19
+ import { MorphTargetManager } from "@babylonjs/core/Morph/morphTargetManager.js";
20
+ import { GLTFFileLoader, GLTFLoaderState, GLTFLoaderCoordinateSystemMode, GLTFLoaderAnimationStartMode } from "../glTFFileLoader";
21
+ import { AnimationKeyInterpolation } from "@babylonjs/core/Animations/animationKey.js";
22
+ import { DecodeBase64UrlToBinary, IsBase64DataUrl, LoadFileError } from "@babylonjs/core/Misc/fileTools.js";
23
+ import { Logger } from "@babylonjs/core/Misc/logger.js";
24
+ import { BoundingInfo } from "@babylonjs/core/Culling/boundingInfo.js";
25
+ import { StringTools } from "@babylonjs/core/Misc/stringTools.js";
26
+ /**
27
+ * Helper class for working with arrays when loading the glTF asset
28
+ */
29
+ var ArrayItem = /** @class */ (function () {
30
+ function ArrayItem() {
31
+ }
32
+ /**
33
+ * Gets an item from the given array.
34
+ * @param context The context when loading the asset
35
+ * @param array The array to get the item from
36
+ * @param index The index to the array
37
+ * @returns The array item
38
+ */
39
+ ArrayItem.Get = function (context, array, index) {
40
+ if (!array || index == undefined || !array[index]) {
41
+ throw new Error("".concat(context, ": Failed to find index (").concat(index, ")"));
42
+ }
43
+ return array[index];
44
+ };
45
+ /**
46
+ * Assign an `index` field to each item of the given array.
47
+ * @param array The array of items
48
+ */
49
+ ArrayItem.Assign = function (array) {
50
+ if (array) {
51
+ for (var index = 0; index < array.length; index++) {
52
+ array[index].index = index;
53
+ }
54
+ }
55
+ };
56
+ return ArrayItem;
57
+ }());
58
+ export { ArrayItem };
59
+ /**
60
+ * The glTF 2.0 loader
61
+ */
62
+ var GLTFLoader = /** @class */ (function () {
63
+ /**
64
+ * @param parent
65
+ * @hidden
66
+ */
67
+ function GLTFLoader(parent) {
68
+ /** @hidden */
69
+ this._completePromises = new Array();
70
+ /** @hidden */
71
+ this._assetContainer = null;
72
+ /** Storage */
73
+ this._babylonLights = [];
74
+ /** @hidden */
75
+ this._disableInstancedMesh = 0;
76
+ this._extensions = new Array();
77
+ this._disposed = false;
78
+ this._rootUrl = null;
79
+ this._fileName = null;
80
+ this._uniqueRootUrl = null;
81
+ this._bin = null;
82
+ this._rootBabylonMesh = null;
83
+ this._defaultBabylonMaterialData = {};
84
+ this._postSceneLoadActions = new Array();
85
+ this._parent = parent;
86
+ }
87
+ /**
88
+ * Registers a loader extension.
89
+ * @param name The name of the loader extension.
90
+ * @param factory The factory function that creates the loader extension.
91
+ */
92
+ GLTFLoader.RegisterExtension = function (name, factory) {
93
+ if (GLTFLoader.UnregisterExtension(name)) {
94
+ Logger.Warn("Extension with the name '".concat(name, "' already exists"));
95
+ }
96
+ GLTFLoader._RegisteredExtensions[name] = {
97
+ factory: factory,
98
+ };
99
+ };
100
+ /**
101
+ * Unregisters a loader extension.
102
+ * @param name The name of the loader extension.
103
+ * @returns A boolean indicating whether the extension has been unregistered
104
+ */
105
+ GLTFLoader.UnregisterExtension = function (name) {
106
+ if (!GLTFLoader._RegisteredExtensions[name]) {
107
+ return false;
108
+ }
109
+ delete GLTFLoader._RegisteredExtensions[name];
110
+ return true;
111
+ };
112
+ Object.defineProperty(GLTFLoader.prototype, "gltf", {
113
+ /**
114
+ * The object that represents the glTF JSON.
115
+ */
116
+ get: function () {
117
+ if (!this._gltf) {
118
+ throw new Error("glTF JSON is not available");
119
+ }
120
+ return this._gltf;
121
+ },
122
+ enumerable: false,
123
+ configurable: true
124
+ });
125
+ Object.defineProperty(GLTFLoader.prototype, "bin", {
126
+ /**
127
+ * The BIN chunk of a binary glTF.
128
+ */
129
+ get: function () {
130
+ return this._bin;
131
+ },
132
+ enumerable: false,
133
+ configurable: true
134
+ });
135
+ Object.defineProperty(GLTFLoader.prototype, "parent", {
136
+ /**
137
+ * The parent file loader.
138
+ */
139
+ get: function () {
140
+ return this._parent;
141
+ },
142
+ enumerable: false,
143
+ configurable: true
144
+ });
145
+ Object.defineProperty(GLTFLoader.prototype, "babylonScene", {
146
+ /**
147
+ * The Babylon scene when loading the asset.
148
+ */
149
+ get: function () {
150
+ if (!this._babylonScene) {
151
+ throw new Error("Scene is not available");
152
+ }
153
+ return this._babylonScene;
154
+ },
155
+ enumerable: false,
156
+ configurable: true
157
+ });
158
+ Object.defineProperty(GLTFLoader.prototype, "rootBabylonMesh", {
159
+ /**
160
+ * The root Babylon mesh when loading the asset.
161
+ */
162
+ get: function () {
163
+ return this._rootBabylonMesh;
164
+ },
165
+ enumerable: false,
166
+ configurable: true
167
+ });
168
+ /** @hidden */
169
+ GLTFLoader.prototype.dispose = function () {
170
+ if (this._disposed) {
171
+ return;
172
+ }
173
+ this._disposed = true;
174
+ this._completePromises.length = 0;
175
+ this._extensions.forEach(function (extension) { return extension.dispose && extension.dispose(); });
176
+ this._extensions.length = 0;
177
+ this._gltf = null; // TODO
178
+ this._bin = null;
179
+ this._babylonScene = null; // TODO
180
+ this._rootBabylonMesh = null;
181
+ this._defaultBabylonMaterialData = {};
182
+ this._postSceneLoadActions.length = 0;
183
+ this._parent.dispose();
184
+ };
185
+ /**
186
+ * @param meshesNames
187
+ * @param scene
188
+ * @param container
189
+ * @param data
190
+ * @param rootUrl
191
+ * @param onProgress
192
+ * @param fileName
193
+ * @hidden
194
+ */
195
+ GLTFLoader.prototype.importMeshAsync = function (meshesNames, scene, container, data, rootUrl, onProgress, fileName) {
196
+ var _this = this;
197
+ if (fileName === void 0) { fileName = ""; }
198
+ return Promise.resolve().then(function () {
199
+ _this._babylonScene = scene;
200
+ _this._assetContainer = container;
201
+ _this._loadData(data);
202
+ var nodes = null;
203
+ if (meshesNames) {
204
+ var nodeMap_1 = {};
205
+ if (_this._gltf.nodes) {
206
+ for (var _i = 0, _a = _this._gltf.nodes; _i < _a.length; _i++) {
207
+ var node = _a[_i];
208
+ if (node.name) {
209
+ nodeMap_1[node.name] = node.index;
210
+ }
211
+ }
212
+ }
213
+ var names = meshesNames instanceof Array ? meshesNames : [meshesNames];
214
+ nodes = names.map(function (name) {
215
+ var node = nodeMap_1[name];
216
+ if (node === undefined) {
217
+ throw new Error("Failed to find node '".concat(name, "'"));
218
+ }
219
+ return node;
220
+ });
221
+ }
222
+ return _this._loadAsync(rootUrl, fileName, nodes, function () {
223
+ return {
224
+ meshes: _this._getMeshes(),
225
+ particleSystems: [],
226
+ skeletons: _this._getSkeletons(),
227
+ animationGroups: _this._getAnimationGroups(),
228
+ lights: _this._babylonLights,
229
+ transformNodes: _this._getTransformNodes(),
230
+ geometries: _this._getGeometries(),
231
+ };
232
+ });
233
+ });
234
+ };
235
+ /**
236
+ * @param scene
237
+ * @param data
238
+ * @param rootUrl
239
+ * @param onProgress
240
+ * @param fileName
241
+ * @hidden
242
+ */
243
+ GLTFLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
244
+ var _this = this;
245
+ if (fileName === void 0) { fileName = ""; }
246
+ return Promise.resolve().then(function () {
247
+ _this._babylonScene = scene;
248
+ _this._loadData(data);
249
+ return _this._loadAsync(rootUrl, fileName, null, function () { return undefined; });
250
+ });
251
+ };
252
+ GLTFLoader.prototype._loadAsync = function (rootUrl, fileName, nodes, resultFunc) {
253
+ var _this = this;
254
+ return Promise.resolve()
255
+ .then(function () {
256
+ _this._rootUrl = rootUrl;
257
+ _this._uniqueRootUrl = !StringTools.StartsWith(rootUrl, "file:") && fileName ? rootUrl : "".concat(rootUrl).concat(Date.now(), "/");
258
+ _this._fileName = fileName;
259
+ _this._loadExtensions();
260
+ _this._checkExtensions();
261
+ var loadingToReadyCounterName = "".concat(GLTFLoaderState[GLTFLoaderState.LOADING], " => ").concat(GLTFLoaderState[GLTFLoaderState.READY]);
262
+ var loadingToCompleteCounterName = "".concat(GLTFLoaderState[GLTFLoaderState.LOADING], " => ").concat(GLTFLoaderState[GLTFLoaderState.COMPLETE]);
263
+ _this._parent._startPerformanceCounter(loadingToReadyCounterName);
264
+ _this._parent._startPerformanceCounter(loadingToCompleteCounterName);
265
+ _this._parent._setState(GLTFLoaderState.LOADING);
266
+ _this._extensionsOnLoading();
267
+ var promises = new Array();
268
+ // Block the marking of materials dirty until the scene is loaded.
269
+ var oldBlockMaterialDirtyMechanism = _this._babylonScene.blockMaterialDirtyMechanism;
270
+ _this._babylonScene.blockMaterialDirtyMechanism = true;
271
+ if (!_this.parent.loadOnlyMaterials) {
272
+ if (nodes) {
273
+ promises.push(_this.loadSceneAsync("/nodes", { nodes: nodes, index: -1 }));
274
+ }
275
+ else if (_this._gltf.scene != undefined || (_this._gltf.scenes && _this._gltf.scenes[0])) {
276
+ var scene = ArrayItem.Get("/scene", _this._gltf.scenes, _this._gltf.scene || 0);
277
+ promises.push(_this.loadSceneAsync("/scenes/".concat(scene.index), scene));
278
+ }
279
+ }
280
+ if (!_this.parent.skipMaterials && _this.parent.loadAllMaterials && _this._gltf.materials) {
281
+ for (var m = 0; m < _this._gltf.materials.length; ++m) {
282
+ var material = _this._gltf.materials[m];
283
+ var context_1 = "/materials/" + m;
284
+ var babylonDrawMode = Material.TriangleFillMode;
285
+ promises.push(_this._loadMaterialAsync(context_1, material, null, babylonDrawMode, function () { }));
286
+ }
287
+ }
288
+ // Restore the blocking of material dirty.
289
+ _this._babylonScene.blockMaterialDirtyMechanism = oldBlockMaterialDirtyMechanism;
290
+ if (_this._parent.compileMaterials) {
291
+ promises.push(_this._compileMaterialsAsync());
292
+ }
293
+ if (_this._parent.compileShadowGenerators) {
294
+ promises.push(_this._compileShadowGeneratorsAsync());
295
+ }
296
+ var resultPromise = Promise.all(promises).then(function () {
297
+ if (_this._rootBabylonMesh) {
298
+ _this._rootBabylonMesh.setEnabled(true);
299
+ }
300
+ _this._extensionsOnReady();
301
+ _this._parent._setState(GLTFLoaderState.READY);
302
+ _this._startAnimations();
303
+ return resultFunc();
304
+ });
305
+ return resultPromise.then(function (result) {
306
+ _this._parent._endPerformanceCounter(loadingToReadyCounterName);
307
+ Tools.SetImmediate(function () {
308
+ if (!_this._disposed) {
309
+ Promise.all(_this._completePromises).then(function () {
310
+ _this._parent._endPerformanceCounter(loadingToCompleteCounterName);
311
+ _this._parent._setState(GLTFLoaderState.COMPLETE);
312
+ _this._parent.onCompleteObservable.notifyObservers(undefined);
313
+ _this._parent.onCompleteObservable.clear();
314
+ _this.dispose();
315
+ }, function (error) {
316
+ _this._parent.onErrorObservable.notifyObservers(error);
317
+ _this._parent.onErrorObservable.clear();
318
+ _this.dispose();
319
+ });
320
+ }
321
+ });
322
+ return result;
323
+ });
324
+ })
325
+ .catch(function (error) {
326
+ if (!_this._disposed) {
327
+ _this._parent.onErrorObservable.notifyObservers(error);
328
+ _this._parent.onErrorObservable.clear();
329
+ _this.dispose();
330
+ }
331
+ throw error;
332
+ });
333
+ };
334
+ GLTFLoader.prototype._loadData = function (data) {
335
+ this._gltf = data.json;
336
+ this._setupData();
337
+ if (data.bin) {
338
+ var buffers = this._gltf.buffers;
339
+ if (buffers && buffers[0] && !buffers[0].uri) {
340
+ var binaryBuffer = buffers[0];
341
+ if (binaryBuffer.byteLength < data.bin.byteLength - 3 || binaryBuffer.byteLength > data.bin.byteLength) {
342
+ Logger.Warn("Binary buffer length (".concat(binaryBuffer.byteLength, ") from JSON does not match chunk length (").concat(data.bin.byteLength, ")"));
343
+ }
344
+ this._bin = data.bin;
345
+ }
346
+ else {
347
+ Logger.Warn("Unexpected BIN chunk");
348
+ }
349
+ }
350
+ };
351
+ GLTFLoader.prototype._setupData = function () {
352
+ ArrayItem.Assign(this._gltf.accessors);
353
+ ArrayItem.Assign(this._gltf.animations);
354
+ ArrayItem.Assign(this._gltf.buffers);
355
+ ArrayItem.Assign(this._gltf.bufferViews);
356
+ ArrayItem.Assign(this._gltf.cameras);
357
+ ArrayItem.Assign(this._gltf.images);
358
+ ArrayItem.Assign(this._gltf.materials);
359
+ ArrayItem.Assign(this._gltf.meshes);
360
+ ArrayItem.Assign(this._gltf.nodes);
361
+ ArrayItem.Assign(this._gltf.samplers);
362
+ ArrayItem.Assign(this._gltf.scenes);
363
+ ArrayItem.Assign(this._gltf.skins);
364
+ ArrayItem.Assign(this._gltf.textures);
365
+ if (this._gltf.nodes) {
366
+ var nodeParents = {};
367
+ for (var _i = 0, _a = this._gltf.nodes; _i < _a.length; _i++) {
368
+ var node = _a[_i];
369
+ if (node.children) {
370
+ for (var _b = 0, _c = node.children; _b < _c.length; _b++) {
371
+ var index = _c[_b];
372
+ nodeParents[index] = node.index;
373
+ }
374
+ }
375
+ }
376
+ var rootNode = this._createRootNode();
377
+ for (var _d = 0, _e = this._gltf.nodes; _d < _e.length; _d++) {
378
+ var node = _e[_d];
379
+ var parentIndex = nodeParents[node.index];
380
+ node.parent = parentIndex === undefined ? rootNode : this._gltf.nodes[parentIndex];
381
+ }
382
+ }
383
+ };
384
+ GLTFLoader.prototype._loadExtensions = function () {
385
+ for (var name_1 in GLTFLoader._RegisteredExtensions) {
386
+ var extension = GLTFLoader._RegisteredExtensions[name_1].factory(this);
387
+ if (extension.name !== name_1) {
388
+ Logger.Warn("The name of the glTF loader extension instance does not match the registered name: ".concat(extension.name, " !== ").concat(name_1));
389
+ }
390
+ this._extensions.push(extension);
391
+ this._parent.onExtensionLoadedObservable.notifyObservers(extension);
392
+ }
393
+ this._extensions.sort(function (a, b) { return (a.order || Number.MAX_VALUE) - (b.order || Number.MAX_VALUE); });
394
+ this._parent.onExtensionLoadedObservable.clear();
395
+ };
396
+ GLTFLoader.prototype._checkExtensions = function () {
397
+ if (this._gltf.extensionsRequired) {
398
+ var _loop_1 = function (name_2) {
399
+ var available = this_1._extensions.some(function (extension) { return extension.name === name_2 && extension.enabled; });
400
+ if (!available) {
401
+ throw new Error("Require extension ".concat(name_2, " is not available"));
402
+ }
403
+ };
404
+ var this_1 = this;
405
+ for (var _i = 0, _a = this._gltf.extensionsRequired; _i < _a.length; _i++) {
406
+ var name_2 = _a[_i];
407
+ _loop_1(name_2);
408
+ }
409
+ }
410
+ };
411
+ GLTFLoader.prototype._createRootNode = function () {
412
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
413
+ this._rootBabylonMesh = new Mesh("__root__", this._babylonScene);
414
+ this._rootBabylonMesh._parentContainer = this._assetContainer;
415
+ this._babylonScene._blockEntityCollection = false;
416
+ this._rootBabylonMesh.setEnabled(false);
417
+ var rootNode = {
418
+ _babylonTransformNode: this._rootBabylonMesh,
419
+ index: -1,
420
+ };
421
+ switch (this._parent.coordinateSystemMode) {
422
+ case GLTFLoaderCoordinateSystemMode.AUTO: {
423
+ if (!this._babylonScene.useRightHandedSystem) {
424
+ rootNode.rotation = [0, 1, 0, 0];
425
+ rootNode.scale = [1, 1, -1];
426
+ GLTFLoader._LoadTransform(rootNode, this._rootBabylonMesh);
427
+ }
428
+ break;
429
+ }
430
+ case GLTFLoaderCoordinateSystemMode.FORCE_RIGHT_HANDED: {
431
+ this._babylonScene.useRightHandedSystem = true;
432
+ break;
433
+ }
434
+ default: {
435
+ throw new Error("Invalid coordinate system mode (".concat(this._parent.coordinateSystemMode, ")"));
436
+ }
437
+ }
438
+ this._parent.onMeshLoadedObservable.notifyObservers(this._rootBabylonMesh);
439
+ return rootNode;
440
+ };
441
+ /**
442
+ * Loads a glTF scene.
443
+ * @param context The context when loading the asset
444
+ * @param scene The glTF scene property
445
+ * @returns A promise that resolves when the load is complete
446
+ */
447
+ GLTFLoader.prototype.loadSceneAsync = function (context, scene) {
448
+ var _this = this;
449
+ var extensionPromise = this._extensionsLoadSceneAsync(context, scene);
450
+ if (extensionPromise) {
451
+ return extensionPromise;
452
+ }
453
+ var promises = new Array();
454
+ this.logOpen("".concat(context, " ").concat(scene.name || ""));
455
+ if (scene.nodes) {
456
+ for (var _i = 0, _a = scene.nodes; _i < _a.length; _i++) {
457
+ var index = _a[_i];
458
+ var node = ArrayItem.Get("".concat(context, "/nodes/").concat(index), this._gltf.nodes, index);
459
+ promises.push(this.loadNodeAsync("/nodes/".concat(node.index), node, function (babylonMesh) {
460
+ babylonMesh.parent = _this._rootBabylonMesh;
461
+ }));
462
+ }
463
+ }
464
+ for (var _b = 0, _c = this._postSceneLoadActions; _b < _c.length; _b++) {
465
+ var action = _c[_b];
466
+ action();
467
+ }
468
+ promises.push(this._loadAnimationsAsync());
469
+ this.logClose();
470
+ return Promise.all(promises).then(function () { });
471
+ };
472
+ GLTFLoader.prototype._forEachPrimitive = function (node, callback) {
473
+ if (node._primitiveBabylonMeshes) {
474
+ for (var _i = 0, _a = node._primitiveBabylonMeshes; _i < _a.length; _i++) {
475
+ var babylonMesh = _a[_i];
476
+ callback(babylonMesh);
477
+ }
478
+ }
479
+ };
480
+ GLTFLoader.prototype._getGeometries = function () {
481
+ var geometries = new Array();
482
+ var nodes = this._gltf.nodes;
483
+ if (nodes) {
484
+ for (var _i = 0, nodes_1 = nodes; _i < nodes_1.length; _i++) {
485
+ var node = nodes_1[_i];
486
+ this._forEachPrimitive(node, function (babylonMesh) {
487
+ var geometry = babylonMesh.geometry;
488
+ if (geometry && geometries.indexOf(geometry) === -1) {
489
+ geometries.push(geometry);
490
+ }
491
+ });
492
+ }
493
+ }
494
+ return geometries;
495
+ };
496
+ GLTFLoader.prototype._getMeshes = function () {
497
+ var meshes = new Array();
498
+ // Root mesh is always first, if available.
499
+ if (this._rootBabylonMesh) {
500
+ meshes.push(this._rootBabylonMesh);
501
+ }
502
+ var nodes = this._gltf.nodes;
503
+ if (nodes) {
504
+ for (var _i = 0, nodes_2 = nodes; _i < nodes_2.length; _i++) {
505
+ var node = nodes_2[_i];
506
+ this._forEachPrimitive(node, function (babylonMesh) {
507
+ meshes.push(babylonMesh);
508
+ });
509
+ }
510
+ }
511
+ return meshes;
512
+ };
513
+ GLTFLoader.prototype._getTransformNodes = function () {
514
+ var transformNodes = new Array();
515
+ var nodes = this._gltf.nodes;
516
+ if (nodes) {
517
+ for (var _i = 0, nodes_3 = nodes; _i < nodes_3.length; _i++) {
518
+ var node = nodes_3[_i];
519
+ if (node._babylonTransformNode && node._babylonTransformNode.getClassName() === "TransformNode") {
520
+ transformNodes.push(node._babylonTransformNode);
521
+ }
522
+ if (node._babylonTransformNodeForSkin) {
523
+ transformNodes.push(node._babylonTransformNodeForSkin);
524
+ }
525
+ }
526
+ }
527
+ return transformNodes;
528
+ };
529
+ GLTFLoader.prototype._getSkeletons = function () {
530
+ var skeletons = new Array();
531
+ var skins = this._gltf.skins;
532
+ if (skins) {
533
+ for (var _i = 0, skins_1 = skins; _i < skins_1.length; _i++) {
534
+ var skin = skins_1[_i];
535
+ if (skin._data) {
536
+ skeletons.push(skin._data.babylonSkeleton);
537
+ }
538
+ }
539
+ }
540
+ return skeletons;
541
+ };
542
+ GLTFLoader.prototype._getAnimationGroups = function () {
543
+ var animationGroups = new Array();
544
+ var animations = this._gltf.animations;
545
+ if (animations) {
546
+ for (var _i = 0, animations_1 = animations; _i < animations_1.length; _i++) {
547
+ var animation = animations_1[_i];
548
+ if (animation._babylonAnimationGroup) {
549
+ animationGroups.push(animation._babylonAnimationGroup);
550
+ }
551
+ }
552
+ }
553
+ return animationGroups;
554
+ };
555
+ GLTFLoader.prototype._startAnimations = function () {
556
+ switch (this._parent.animationStartMode) {
557
+ case GLTFLoaderAnimationStartMode.NONE: {
558
+ // do nothing
559
+ break;
560
+ }
561
+ case GLTFLoaderAnimationStartMode.FIRST: {
562
+ var babylonAnimationGroups = this._getAnimationGroups();
563
+ if (babylonAnimationGroups.length !== 0) {
564
+ babylonAnimationGroups[0].start(true);
565
+ }
566
+ break;
567
+ }
568
+ case GLTFLoaderAnimationStartMode.ALL: {
569
+ var babylonAnimationGroups = this._getAnimationGroups();
570
+ for (var _i = 0, babylonAnimationGroups_1 = babylonAnimationGroups; _i < babylonAnimationGroups_1.length; _i++) {
571
+ var babylonAnimationGroup = babylonAnimationGroups_1[_i];
572
+ babylonAnimationGroup.start(true);
573
+ }
574
+ break;
575
+ }
576
+ default: {
577
+ Logger.Error("Invalid animation start mode (".concat(this._parent.animationStartMode, ")"));
578
+ return;
579
+ }
580
+ }
581
+ };
582
+ /**
583
+ * Loads a glTF node.
584
+ * @param context The context when loading the asset
585
+ * @param node The glTF node property
586
+ * @param assign A function called synchronously after parsing the glTF properties
587
+ * @returns A promise that resolves with the loaded Babylon mesh when the load is complete
588
+ */
589
+ GLTFLoader.prototype.loadNodeAsync = function (context, node, assign) {
590
+ var _this = this;
591
+ if (assign === void 0) { assign = function () { }; }
592
+ var extensionPromise = this._extensionsLoadNodeAsync(context, node, assign);
593
+ if (extensionPromise) {
594
+ return extensionPromise;
595
+ }
596
+ if (node._babylonTransformNode) {
597
+ throw new Error("".concat(context, ": Invalid recursive node hierarchy"));
598
+ }
599
+ var promises = new Array();
600
+ this.logOpen("".concat(context, " ").concat(node.name || ""));
601
+ var loadNode = function (babylonTransformNode) {
602
+ GLTFLoader.AddPointerMetadata(babylonTransformNode, context);
603
+ GLTFLoader._LoadTransform(node, babylonTransformNode);
604
+ if (node.camera != undefined) {
605
+ var camera = ArrayItem.Get("".concat(context, "/camera"), _this._gltf.cameras, node.camera);
606
+ promises.push(_this.loadCameraAsync("/cameras/".concat(camera.index), camera, function (babylonCamera) {
607
+ babylonCamera.parent = babylonTransformNode;
608
+ }));
609
+ }
610
+ if (node.children) {
611
+ for (var _i = 0, _a = node.children; _i < _a.length; _i++) {
612
+ var index = _a[_i];
613
+ var childNode = ArrayItem.Get("".concat(context, "/children/").concat(index), _this._gltf.nodes, index);
614
+ promises.push(_this.loadNodeAsync("/nodes/".concat(childNode.index), childNode, function (childBabylonMesh) {
615
+ childBabylonMesh.parent = babylonTransformNode;
616
+ }));
617
+ }
618
+ }
619
+ assign(babylonTransformNode);
620
+ };
621
+ if (node.mesh == undefined || node.skin != undefined) {
622
+ var nodeName = node.name || "node".concat(node.index);
623
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
624
+ var transformNode = new TransformNode(nodeName, this._babylonScene);
625
+ transformNode._parentContainer = this._assetContainer;
626
+ this._babylonScene._blockEntityCollection = false;
627
+ if (node.mesh == undefined) {
628
+ node._babylonTransformNode = transformNode;
629
+ }
630
+ else {
631
+ node._babylonTransformNodeForSkin = transformNode;
632
+ }
633
+ loadNode(transformNode);
634
+ }
635
+ if (node.mesh != undefined) {
636
+ if (node.skin == undefined) {
637
+ var mesh = ArrayItem.Get("".concat(context, "/mesh"), this._gltf.meshes, node.mesh);
638
+ promises.push(this._loadMeshAsync("/meshes/".concat(mesh.index), node, mesh, loadNode));
639
+ }
640
+ else {
641
+ // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
642
+ // This code path will place the skinned mesh as a sibling of the skeleton root node without loading the
643
+ // transform, which effectively ignores the transform of the skinned mesh, as per spec.
644
+ var mesh = ArrayItem.Get("".concat(context, "/mesh"), this._gltf.meshes, node.mesh);
645
+ promises.push(this._loadMeshAsync("/meshes/".concat(mesh.index), node, mesh, function (babylonTransformNode) {
646
+ // Duplicate the metadata from the skin node to the skinned mesh in case any loader extension added metadata.
647
+ babylonTransformNode.metadata = node._babylonTransformNodeForSkin.metadata;
648
+ var skin = ArrayItem.Get("".concat(context, "/skin"), _this._gltf.skins, node.skin);
649
+ promises.push(_this._loadSkinAsync("/skins/".concat(skin.index), node, skin, function (babylonSkeleton) {
650
+ _this._forEachPrimitive(node, function (babylonMesh) {
651
+ babylonMesh.skeleton = babylonSkeleton;
652
+ });
653
+ // Wait until the scene is loaded to ensure the skeleton root node has been loaded.
654
+ _this._postSceneLoadActions.push(function () {
655
+ if (skin.skeleton != undefined) {
656
+ // Place the skinned mesh node as a sibling of the skeleton root node.
657
+ var skeletonRootNode = ArrayItem.Get("/skins/".concat(skin.index, "/skeleton"), _this._gltf.nodes, skin.skeleton);
658
+ babylonTransformNode.parent = skeletonRootNode.parent._babylonTransformNode;
659
+ }
660
+ else {
661
+ babylonTransformNode.parent = _this._rootBabylonMesh;
662
+ }
663
+ });
664
+ }));
665
+ }));
666
+ }
667
+ }
668
+ this.logClose();
669
+ return Promise.all(promises).then(function () {
670
+ _this._forEachPrimitive(node, function (babylonMesh) {
671
+ if (babylonMesh.geometry && babylonMesh.geometry.useBoundingInfoFromGeometry) {
672
+ // simply apply the world matrices to the bounding info - the extends are already ok
673
+ babylonMesh._updateBoundingInfo();
674
+ }
675
+ else {
676
+ babylonMesh.refreshBoundingInfo(true);
677
+ }
678
+ });
679
+ return node._babylonTransformNode;
680
+ });
681
+ };
682
+ GLTFLoader.prototype._loadMeshAsync = function (context, node, mesh, assign) {
683
+ var primitives = mesh.primitives;
684
+ if (!primitives || !primitives.length) {
685
+ throw new Error("".concat(context, ": Primitives are missing"));
686
+ }
687
+ if (primitives[0].index == undefined) {
688
+ ArrayItem.Assign(primitives);
689
+ }
690
+ var promises = new Array();
691
+ this.logOpen("".concat(context, " ").concat(mesh.name || ""));
692
+ var name = node.name || "node".concat(node.index);
693
+ if (primitives.length === 1) {
694
+ var primitive = mesh.primitives[0];
695
+ promises.push(this._loadMeshPrimitiveAsync("".concat(context, "/primitives/").concat(primitive.index), name, node, mesh, primitive, function (babylonMesh) {
696
+ node._babylonTransformNode = babylonMesh;
697
+ node._primitiveBabylonMeshes = [babylonMesh];
698
+ }));
699
+ }
700
+ else {
701
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
702
+ node._babylonTransformNode = new TransformNode(name, this._babylonScene);
703
+ node._babylonTransformNode._parentContainer = this._assetContainer;
704
+ this._babylonScene._blockEntityCollection = false;
705
+ node._primitiveBabylonMeshes = [];
706
+ for (var _i = 0, primitives_1 = primitives; _i < primitives_1.length; _i++) {
707
+ var primitive = primitives_1[_i];
708
+ promises.push(this._loadMeshPrimitiveAsync("".concat(context, "/primitives/").concat(primitive.index), "".concat(name, "_primitive").concat(primitive.index), node, mesh, primitive, function (babylonMesh) {
709
+ babylonMesh.parent = node._babylonTransformNode;
710
+ node._primitiveBabylonMeshes.push(babylonMesh);
711
+ }));
712
+ }
713
+ }
714
+ assign(node._babylonTransformNode);
715
+ this.logClose();
716
+ return Promise.all(promises).then(function () {
717
+ return node._babylonTransformNode;
718
+ });
719
+ };
720
+ /**
721
+ * @hidden Define this method to modify the default behavior when loading data for mesh primitives.
722
+ * @param context The context when loading the asset
723
+ * @param name The mesh name when loading the asset
724
+ * @param node The glTF node when loading the asset
725
+ * @param mesh The glTF mesh when loading the asset
726
+ * @param primitive The glTF mesh primitive property
727
+ * @param assign A function called synchronously after parsing the glTF properties
728
+ * @returns A promise that resolves with the loaded mesh when the load is complete or null if not handled
729
+ */
730
+ GLTFLoader.prototype._loadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
731
+ var _this = this;
732
+ var extensionPromise = this._extensionsLoadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign);
733
+ if (extensionPromise) {
734
+ return extensionPromise;
735
+ }
736
+ this.logOpen("".concat(context));
737
+ var shouldInstance = this._disableInstancedMesh === 0 && this._parent.createInstances && node.skin == undefined && !mesh.primitives[0].targets;
738
+ var babylonAbstractMesh;
739
+ var promise;
740
+ if (shouldInstance && primitive._instanceData) {
741
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
742
+ babylonAbstractMesh = primitive._instanceData.babylonSourceMesh.createInstance(name);
743
+ babylonAbstractMesh._parentContainer = this._assetContainer;
744
+ this._babylonScene._blockEntityCollection = false;
745
+ promise = primitive._instanceData.promise;
746
+ }
747
+ else {
748
+ var promises = new Array();
749
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
750
+ var babylonMesh_1 = new Mesh(name, this._babylonScene);
751
+ babylonMesh_1._parentContainer = this._assetContainer;
752
+ this._babylonScene._blockEntityCollection = false;
753
+ babylonMesh_1.overrideMaterialSideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
754
+ this._createMorphTargets(context, node, mesh, primitive, babylonMesh_1);
755
+ promises.push(this._loadVertexDataAsync(context, primitive, babylonMesh_1).then(function (babylonGeometry) {
756
+ return _this._loadMorphTargetsAsync(context, primitive, babylonMesh_1, babylonGeometry).then(function () {
757
+ _this._babylonScene._blockEntityCollection = !!_this._assetContainer;
758
+ babylonGeometry.applyToMesh(babylonMesh_1);
759
+ babylonGeometry._parentContainer = _this._assetContainer;
760
+ _this._babylonScene._blockEntityCollection = false;
761
+ });
762
+ }));
763
+ var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
764
+ if (primitive.material == undefined) {
765
+ var babylonMaterial = this._defaultBabylonMaterialData[babylonDrawMode];
766
+ if (!babylonMaterial) {
767
+ babylonMaterial = this._createDefaultMaterial("__GLTFLoader._default", babylonDrawMode);
768
+ this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
769
+ this._defaultBabylonMaterialData[babylonDrawMode] = babylonMaterial;
770
+ }
771
+ babylonMesh_1.material = babylonMaterial;
772
+ }
773
+ else if (!this.parent.skipMaterials) {
774
+ var material = ArrayItem.Get("".concat(context, "/material"), this._gltf.materials, primitive.material);
775
+ promises.push(this._loadMaterialAsync("/materials/".concat(material.index), material, babylonMesh_1, babylonDrawMode, function (babylonMaterial) {
776
+ babylonMesh_1.material = babylonMaterial;
777
+ }));
778
+ }
779
+ promise = Promise.all(promises);
780
+ if (shouldInstance) {
781
+ primitive._instanceData = {
782
+ babylonSourceMesh: babylonMesh_1,
783
+ promise: promise,
784
+ };
785
+ }
786
+ babylonAbstractMesh = babylonMesh_1;
787
+ }
788
+ GLTFLoader.AddPointerMetadata(babylonAbstractMesh, context);
789
+ this._parent.onMeshLoadedObservable.notifyObservers(babylonAbstractMesh);
790
+ assign(babylonAbstractMesh);
791
+ this.logClose();
792
+ return promise.then(function () {
793
+ return babylonAbstractMesh;
794
+ });
795
+ };
796
+ GLTFLoader.prototype._loadVertexDataAsync = function (context, primitive, babylonMesh) {
797
+ var _this = this;
798
+ var extensionPromise = this._extensionsLoadVertexDataAsync(context, primitive, babylonMesh);
799
+ if (extensionPromise) {
800
+ return extensionPromise;
801
+ }
802
+ var attributes = primitive.attributes;
803
+ if (!attributes) {
804
+ throw new Error("".concat(context, ": Attributes are missing"));
805
+ }
806
+ var promises = new Array();
807
+ var babylonGeometry = new Geometry(babylonMesh.name, this._babylonScene);
808
+ if (primitive.indices == undefined) {
809
+ babylonMesh.isUnIndexed = true;
810
+ }
811
+ else {
812
+ var accessor = ArrayItem.Get("".concat(context, "/indices"), this._gltf.accessors, primitive.indices);
813
+ promises.push(this._loadIndicesAccessorAsync("/accessors/".concat(accessor.index), accessor).then(function (data) {
814
+ babylonGeometry.setIndices(data);
815
+ }));
816
+ }
817
+ var loadAttribute = function (attribute, kind, callback) {
818
+ if (attributes[attribute] == undefined) {
819
+ return;
820
+ }
821
+ babylonMesh._delayInfo = babylonMesh._delayInfo || [];
822
+ if (babylonMesh._delayInfo.indexOf(kind) === -1) {
823
+ babylonMesh._delayInfo.push(kind);
824
+ }
825
+ var accessor = ArrayItem.Get("".concat(context, "/attributes/").concat(attribute), _this._gltf.accessors, attributes[attribute]);
826
+ promises.push(_this._loadVertexAccessorAsync("/accessors/".concat(accessor.index), accessor, kind).then(function (babylonVertexBuffer) {
827
+ if (babylonVertexBuffer.getKind() === VertexBuffer.PositionKind && !_this.parent.alwaysComputeBoundingBox && !babylonMesh.skeleton) {
828
+ var mmin = accessor.min, mmax = accessor.max;
829
+ if (mmin !== undefined && mmax !== undefined) {
830
+ if (accessor.normalized && accessor.componentType !== 5126 /* FLOAT */) {
831
+ var divider = 1;
832
+ switch (accessor.componentType) {
833
+ case 5120 /* BYTE */:
834
+ divider = 127.0;
835
+ break;
836
+ case 5121 /* UNSIGNED_BYTE */:
837
+ divider = 255.0;
838
+ break;
839
+ case 5122 /* SHORT */:
840
+ divider = 32767.0;
841
+ break;
842
+ case 5123 /* UNSIGNED_SHORT */:
843
+ divider = 65535.0;
844
+ break;
845
+ }
846
+ for (var i = 0; i < 3; ++i) {
847
+ mmin[i] = Math.max(mmin[i] / divider, -1.0);
848
+ mmax[i] = Math.max(mmax[i] / divider, -1.0);
849
+ }
850
+ }
851
+ var min = TmpVectors.Vector3[0], max = TmpVectors.Vector3[1];
852
+ min.copyFromFloats.apply(min, mmin);
853
+ max.copyFromFloats.apply(max, mmax);
854
+ babylonGeometry._boundingInfo = new BoundingInfo(min, max);
855
+ babylonGeometry.useBoundingInfoFromGeometry = true;
856
+ }
857
+ }
858
+ babylonGeometry.setVerticesBuffer(babylonVertexBuffer, accessor.count);
859
+ }));
860
+ if (kind == VertexBuffer.MatricesIndicesExtraKind) {
861
+ babylonMesh.numBoneInfluencers = 8;
862
+ }
863
+ if (callback) {
864
+ callback(accessor);
865
+ }
866
+ };
867
+ loadAttribute("POSITION", VertexBuffer.PositionKind);
868
+ loadAttribute("NORMAL", VertexBuffer.NormalKind);
869
+ loadAttribute("TANGENT", VertexBuffer.TangentKind);
870
+ loadAttribute("TEXCOORD_0", VertexBuffer.UVKind);
871
+ loadAttribute("TEXCOORD_1", VertexBuffer.UV2Kind);
872
+ loadAttribute("TEXCOORD_2", VertexBuffer.UV3Kind);
873
+ loadAttribute("TEXCOORD_3", VertexBuffer.UV4Kind);
874
+ loadAttribute("TEXCOORD_4", VertexBuffer.UV5Kind);
875
+ loadAttribute("TEXCOORD_5", VertexBuffer.UV6Kind);
876
+ loadAttribute("JOINTS_0", VertexBuffer.MatricesIndicesKind);
877
+ loadAttribute("WEIGHTS_0", VertexBuffer.MatricesWeightsKind);
878
+ loadAttribute("JOINTS_1", VertexBuffer.MatricesIndicesExtraKind);
879
+ loadAttribute("WEIGHTS_1", VertexBuffer.MatricesWeightsExtraKind);
880
+ loadAttribute("COLOR_0", VertexBuffer.ColorKind, function (accessor) {
881
+ if (accessor.type === "VEC4" /* VEC4 */) {
882
+ babylonMesh.hasVertexAlpha = true;
883
+ }
884
+ });
885
+ return Promise.all(promises).then(function () {
886
+ return babylonGeometry;
887
+ });
888
+ };
889
+ GLTFLoader.prototype._createMorphTargets = function (context, node, mesh, primitive, babylonMesh) {
890
+ if (!primitive.targets) {
891
+ return;
892
+ }
893
+ if (node._numMorphTargets == undefined) {
894
+ node._numMorphTargets = primitive.targets.length;
895
+ }
896
+ else if (primitive.targets.length !== node._numMorphTargets) {
897
+ throw new Error("".concat(context, ": Primitives do not have the same number of targets"));
898
+ }
899
+ var targetNames = mesh.extras ? mesh.extras.targetNames : null;
900
+ babylonMesh.morphTargetManager = new MorphTargetManager(babylonMesh.getScene());
901
+ babylonMesh.morphTargetManager.areUpdatesFrozen = true;
902
+ for (var index = 0; index < primitive.targets.length; index++) {
903
+ var weight = node.weights ? node.weights[index] : mesh.weights ? mesh.weights[index] : 0;
904
+ var name_3 = targetNames ? targetNames[index] : "morphTarget".concat(index);
905
+ babylonMesh.morphTargetManager.addTarget(new MorphTarget(name_3, weight, babylonMesh.getScene()));
906
+ // TODO: tell the target whether it has positions, normals, tangents
907
+ }
908
+ };
909
+ GLTFLoader.prototype._loadMorphTargetsAsync = function (context, primitive, babylonMesh, babylonGeometry) {
910
+ if (!primitive.targets) {
911
+ return Promise.resolve();
912
+ }
913
+ var promises = new Array();
914
+ var morphTargetManager = babylonMesh.morphTargetManager;
915
+ for (var index = 0; index < morphTargetManager.numTargets; index++) {
916
+ var babylonMorphTarget = morphTargetManager.getTarget(index);
917
+ promises.push(this._loadMorphTargetVertexDataAsync("".concat(context, "/targets/").concat(index), babylonGeometry, primitive.targets[index], babylonMorphTarget));
918
+ }
919
+ return Promise.all(promises).then(function () {
920
+ morphTargetManager.areUpdatesFrozen = false;
921
+ });
922
+ };
923
+ GLTFLoader.prototype._loadMorphTargetVertexDataAsync = function (context, babylonGeometry, attributes, babylonMorphTarget) {
924
+ var _this = this;
925
+ var promises = new Array();
926
+ var loadAttribute = function (attribute, kind, setData) {
927
+ if (attributes[attribute] == undefined) {
928
+ return;
929
+ }
930
+ var babylonVertexBuffer = babylonGeometry.getVertexBuffer(kind);
931
+ if (!babylonVertexBuffer) {
932
+ return;
933
+ }
934
+ var accessor = ArrayItem.Get("".concat(context, "/").concat(attribute), _this._gltf.accessors, attributes[attribute]);
935
+ promises.push(_this._loadFloatAccessorAsync("/accessors/".concat(accessor.index), accessor).then(function (data) {
936
+ setData(babylonVertexBuffer, data);
937
+ }));
938
+ };
939
+ loadAttribute("POSITION", VertexBuffer.PositionKind, function (babylonVertexBuffer, data) {
940
+ var positions = new Float32Array(data.length);
941
+ babylonVertexBuffer.forEach(data.length, function (value, index) {
942
+ positions[index] = data[index] + value;
943
+ });
944
+ babylonMorphTarget.setPositions(positions);
945
+ });
946
+ loadAttribute("NORMAL", VertexBuffer.NormalKind, function (babylonVertexBuffer, data) {
947
+ var normals = new Float32Array(data.length);
948
+ babylonVertexBuffer.forEach(normals.length, function (value, index) {
949
+ normals[index] = data[index] + value;
950
+ });
951
+ babylonMorphTarget.setNormals(normals);
952
+ });
953
+ loadAttribute("TANGENT", VertexBuffer.TangentKind, function (babylonVertexBuffer, data) {
954
+ var tangents = new Float32Array((data.length / 3) * 4);
955
+ var dataIndex = 0;
956
+ babylonVertexBuffer.forEach((data.length / 3) * 4, function (value, index) {
957
+ // Tangent data for morph targets is stored as xyz delta.
958
+ // The vertexData.tangent is stored as xyzw.
959
+ // So we need to skip every fourth vertexData.tangent.
960
+ if ((index + 1) % 4 !== 0) {
961
+ tangents[dataIndex] = data[dataIndex] + value;
962
+ dataIndex++;
963
+ }
964
+ });
965
+ babylonMorphTarget.setTangents(tangents);
966
+ });
967
+ return Promise.all(promises).then(function () { });
968
+ };
969
+ GLTFLoader._LoadTransform = function (node, babylonNode) {
970
+ // Ignore the TRS of skinned nodes.
971
+ // See https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins (second implementation note)
972
+ if (node.skin != undefined) {
973
+ return;
974
+ }
975
+ var position = Vector3.Zero();
976
+ var rotation = Quaternion.Identity();
977
+ var scaling = Vector3.One();
978
+ if (node.matrix) {
979
+ var matrix = Matrix.FromArray(node.matrix);
980
+ matrix.decompose(scaling, rotation, position);
981
+ }
982
+ else {
983
+ if (node.translation) {
984
+ position = Vector3.FromArray(node.translation);
985
+ }
986
+ if (node.rotation) {
987
+ rotation = Quaternion.FromArray(node.rotation);
988
+ }
989
+ if (node.scale) {
990
+ scaling = Vector3.FromArray(node.scale);
991
+ }
992
+ }
993
+ babylonNode.position = position;
994
+ babylonNode.rotationQuaternion = rotation;
995
+ babylonNode.scaling = scaling;
996
+ };
997
+ GLTFLoader.prototype._loadSkinAsync = function (context, node, skin, assign) {
998
+ var _this = this;
999
+ var extensionPromise = this._extensionsLoadSkinAsync(context, node, skin);
1000
+ if (extensionPromise) {
1001
+ return extensionPromise;
1002
+ }
1003
+ if (skin._data) {
1004
+ assign(skin._data.babylonSkeleton);
1005
+ return skin._data.promise;
1006
+ }
1007
+ var skeletonId = "skeleton".concat(skin.index);
1008
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1009
+ var babylonSkeleton = new Skeleton(skin.name || skeletonId, skeletonId, this._babylonScene);
1010
+ babylonSkeleton._parentContainer = this._assetContainer;
1011
+ this._babylonScene._blockEntityCollection = false;
1012
+ this._loadBones(context, skin, babylonSkeleton);
1013
+ var promise = this._loadSkinInverseBindMatricesDataAsync(context, skin).then(function (inverseBindMatricesData) {
1014
+ _this._updateBoneMatrices(babylonSkeleton, inverseBindMatricesData);
1015
+ });
1016
+ skin._data = {
1017
+ babylonSkeleton: babylonSkeleton,
1018
+ promise: promise,
1019
+ };
1020
+ assign(babylonSkeleton);
1021
+ return promise;
1022
+ };
1023
+ GLTFLoader.prototype._loadBones = function (context, skin, babylonSkeleton) {
1024
+ if (skin.skeleton == undefined || this._parent.alwaysComputeSkeletonRootNode) {
1025
+ var rootNode = this._findSkeletonRootNode("".concat(context, "/joints"), skin.joints);
1026
+ if (rootNode) {
1027
+ if (skin.skeleton === undefined) {
1028
+ skin.skeleton = rootNode.index;
1029
+ }
1030
+ else {
1031
+ var isParent = function (a, b) {
1032
+ for (; b.parent; b = b.parent) {
1033
+ if (b.parent === a) {
1034
+ return true;
1035
+ }
1036
+ }
1037
+ return false;
1038
+ };
1039
+ var skeletonNode = ArrayItem.Get("".concat(context, "/skeleton"), this._gltf.nodes, skin.skeleton);
1040
+ if (skeletonNode !== rootNode && !isParent(skeletonNode, rootNode)) {
1041
+ Logger.Warn("".concat(context, "/skeleton: Overriding with nearest common ancestor as skeleton node is not a common root"));
1042
+ skin.skeleton = rootNode.index;
1043
+ }
1044
+ }
1045
+ }
1046
+ else {
1047
+ Logger.Warn("".concat(context, ": Failed to find common root"));
1048
+ }
1049
+ }
1050
+ var babylonBones = {};
1051
+ for (var _i = 0, _a = skin.joints; _i < _a.length; _i++) {
1052
+ var index = _a[_i];
1053
+ var node = ArrayItem.Get("".concat(context, "/joints/").concat(index), this._gltf.nodes, index);
1054
+ this._loadBone(node, skin, babylonSkeleton, babylonBones);
1055
+ }
1056
+ };
1057
+ GLTFLoader.prototype._findSkeletonRootNode = function (context, joints) {
1058
+ if (joints.length === 0) {
1059
+ return null;
1060
+ }
1061
+ var paths = {};
1062
+ for (var _i = 0, joints_1 = joints; _i < joints_1.length; _i++) {
1063
+ var index = joints_1[_i];
1064
+ var path = new Array();
1065
+ var node = ArrayItem.Get("".concat(context, "/").concat(index), this._gltf.nodes, index);
1066
+ while (node.index !== -1) {
1067
+ path.unshift(node);
1068
+ node = node.parent;
1069
+ }
1070
+ paths[index] = path;
1071
+ }
1072
+ var rootNode = null;
1073
+ for (var i = 0;; ++i) {
1074
+ var path = paths[joints[0]];
1075
+ if (i >= path.length) {
1076
+ return rootNode;
1077
+ }
1078
+ var node = path[i];
1079
+ for (var j = 1; j < joints.length; ++j) {
1080
+ path = paths[joints[j]];
1081
+ if (i >= path.length || node !== path[i]) {
1082
+ return rootNode;
1083
+ }
1084
+ }
1085
+ rootNode = node;
1086
+ }
1087
+ };
1088
+ GLTFLoader.prototype._loadBone = function (node, skin, babylonSkeleton, babylonBones) {
1089
+ var babylonBone = babylonBones[node.index];
1090
+ if (babylonBone) {
1091
+ return babylonBone;
1092
+ }
1093
+ var parentBabylonBone = null;
1094
+ if (node.index !== skin.skeleton) {
1095
+ if (node.parent && node.parent.index !== -1) {
1096
+ parentBabylonBone = this._loadBone(node.parent, skin, babylonSkeleton, babylonBones);
1097
+ }
1098
+ else if (skin.skeleton !== undefined) {
1099
+ Logger.Warn("/skins/".concat(skin.index, "/skeleton: Skeleton node is not a common root"));
1100
+ }
1101
+ }
1102
+ var boneIndex = skin.joints.indexOf(node.index);
1103
+ babylonBone = new Bone(node.name || "joint".concat(node.index), babylonSkeleton, parentBabylonBone, this._getNodeMatrix(node), null, null, boneIndex);
1104
+ babylonBones[node.index] = babylonBone;
1105
+ // Wait until the scene is loaded to ensure the transform nodes are loaded.
1106
+ this._postSceneLoadActions.push(function () {
1107
+ // Link the Babylon bone with the corresponding Babylon transform node.
1108
+ // A glTF joint is a pointer to a glTF node in the glTF node hierarchy similar to Unity3D.
1109
+ babylonBone.linkTransformNode(node._babylonTransformNode);
1110
+ });
1111
+ return babylonBone;
1112
+ };
1113
+ GLTFLoader.prototype._loadSkinInverseBindMatricesDataAsync = function (context, skin) {
1114
+ if (skin.inverseBindMatrices == undefined) {
1115
+ return Promise.resolve(null);
1116
+ }
1117
+ var accessor = ArrayItem.Get("".concat(context, "/inverseBindMatrices"), this._gltf.accessors, skin.inverseBindMatrices);
1118
+ return this._loadFloatAccessorAsync("/accessors/".concat(accessor.index), accessor);
1119
+ };
1120
+ GLTFLoader.prototype._updateBoneMatrices = function (babylonSkeleton, inverseBindMatricesData) {
1121
+ for (var _i = 0, _a = babylonSkeleton.bones; _i < _a.length; _i++) {
1122
+ var babylonBone = _a[_i];
1123
+ var baseMatrix = Matrix.Identity();
1124
+ var boneIndex = babylonBone._index;
1125
+ if (inverseBindMatricesData && boneIndex !== -1) {
1126
+ Matrix.FromArrayToRef(inverseBindMatricesData, boneIndex * 16, baseMatrix);
1127
+ baseMatrix.invertToRef(baseMatrix);
1128
+ }
1129
+ var babylonParentBone = babylonBone.getParent();
1130
+ if (babylonParentBone) {
1131
+ baseMatrix.multiplyToRef(babylonParentBone.getInvertedAbsoluteTransform(), baseMatrix);
1132
+ }
1133
+ babylonBone.updateMatrix(baseMatrix, false, false);
1134
+ babylonBone._updateDifferenceMatrix(undefined, false);
1135
+ }
1136
+ };
1137
+ GLTFLoader.prototype._getNodeMatrix = function (node) {
1138
+ return node.matrix
1139
+ ? Matrix.FromArray(node.matrix)
1140
+ : Matrix.Compose(node.scale ? Vector3.FromArray(node.scale) : Vector3.One(), node.rotation ? Quaternion.FromArray(node.rotation) : Quaternion.Identity(), node.translation ? Vector3.FromArray(node.translation) : Vector3.Zero());
1141
+ };
1142
+ /**
1143
+ * Loads a glTF camera.
1144
+ * @param context The context when loading the asset
1145
+ * @param camera The glTF camera property
1146
+ * @param assign A function called synchronously after parsing the glTF properties
1147
+ * @returns A promise that resolves with the loaded Babylon camera when the load is complete
1148
+ */
1149
+ GLTFLoader.prototype.loadCameraAsync = function (context, camera, assign) {
1150
+ if (assign === void 0) { assign = function () { }; }
1151
+ var extensionPromise = this._extensionsLoadCameraAsync(context, camera, assign);
1152
+ if (extensionPromise) {
1153
+ return extensionPromise;
1154
+ }
1155
+ var promises = new Array();
1156
+ this.logOpen("".concat(context, " ").concat(camera.name || ""));
1157
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1158
+ var babylonCamera = new FreeCamera(camera.name || "camera".concat(camera.index), Vector3.Zero(), this._babylonScene, false);
1159
+ babylonCamera._parentContainer = this._assetContainer;
1160
+ this._babylonScene._blockEntityCollection = false;
1161
+ babylonCamera.ignoreParentScaling = true;
1162
+ babylonCamera.rotation = new Vector3(0, Math.PI, 0);
1163
+ switch (camera.type) {
1164
+ case "perspective" /* PERSPECTIVE */: {
1165
+ var perspective = camera.perspective;
1166
+ if (!perspective) {
1167
+ throw new Error("".concat(context, ": Camera perspective properties are missing"));
1168
+ }
1169
+ babylonCamera.fov = perspective.yfov;
1170
+ babylonCamera.minZ = perspective.znear;
1171
+ babylonCamera.maxZ = perspective.zfar || 0;
1172
+ break;
1173
+ }
1174
+ case "orthographic" /* ORTHOGRAPHIC */: {
1175
+ if (!camera.orthographic) {
1176
+ throw new Error("".concat(context, ": Camera orthographic properties are missing"));
1177
+ }
1178
+ babylonCamera.mode = Camera.ORTHOGRAPHIC_CAMERA;
1179
+ babylonCamera.orthoLeft = -camera.orthographic.xmag;
1180
+ babylonCamera.orthoRight = camera.orthographic.xmag;
1181
+ babylonCamera.orthoBottom = -camera.orthographic.ymag;
1182
+ babylonCamera.orthoTop = camera.orthographic.ymag;
1183
+ babylonCamera.minZ = camera.orthographic.znear;
1184
+ babylonCamera.maxZ = camera.orthographic.zfar;
1185
+ break;
1186
+ }
1187
+ default: {
1188
+ throw new Error("".concat(context, ": Invalid camera type (").concat(camera.type, ")"));
1189
+ }
1190
+ }
1191
+ GLTFLoader.AddPointerMetadata(babylonCamera, context);
1192
+ this._parent.onCameraLoadedObservable.notifyObservers(babylonCamera);
1193
+ assign(babylonCamera);
1194
+ this.logClose();
1195
+ return Promise.all(promises).then(function () {
1196
+ return babylonCamera;
1197
+ });
1198
+ };
1199
+ GLTFLoader.prototype._loadAnimationsAsync = function () {
1200
+ var animations = this._gltf.animations;
1201
+ if (!animations) {
1202
+ return Promise.resolve();
1203
+ }
1204
+ var promises = new Array();
1205
+ for (var index = 0; index < animations.length; index++) {
1206
+ var animation = animations[index];
1207
+ promises.push(this.loadAnimationAsync("/animations/".concat(animation.index), animation).then(function (animationGroup) {
1208
+ // Delete the animation group if it ended up not having any animations in it.
1209
+ if (animationGroup.targetedAnimations.length === 0) {
1210
+ animationGroup.dispose();
1211
+ }
1212
+ }));
1213
+ }
1214
+ return Promise.all(promises).then(function () { });
1215
+ };
1216
+ /**
1217
+ * Loads a glTF animation.
1218
+ * @param context The context when loading the asset
1219
+ * @param animation The glTF animation property
1220
+ * @returns A promise that resolves with the loaded Babylon animation group when the load is complete
1221
+ */
1222
+ GLTFLoader.prototype.loadAnimationAsync = function (context, animation) {
1223
+ var promise = this._extensionsLoadAnimationAsync(context, animation);
1224
+ if (promise) {
1225
+ return promise;
1226
+ }
1227
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1228
+ var babylonAnimationGroup = new AnimationGroup(animation.name || "animation".concat(animation.index), this._babylonScene);
1229
+ babylonAnimationGroup._parentContainer = this._assetContainer;
1230
+ this._babylonScene._blockEntityCollection = false;
1231
+ animation._babylonAnimationGroup = babylonAnimationGroup;
1232
+ var promises = new Array();
1233
+ ArrayItem.Assign(animation.channels);
1234
+ ArrayItem.Assign(animation.samplers);
1235
+ for (var _i = 0, _a = animation.channels; _i < _a.length; _i++) {
1236
+ var channel = _a[_i];
1237
+ promises.push(this._loadAnimationChannelAsync("".concat(context, "/channels/").concat(channel.index), context, animation, channel, babylonAnimationGroup));
1238
+ }
1239
+ return Promise.all(promises).then(function () {
1240
+ babylonAnimationGroup.normalize(0);
1241
+ return babylonAnimationGroup;
1242
+ });
1243
+ };
1244
+ /**
1245
+ * @hidden Loads a glTF animation channel.
1246
+ * @param context The context when loading the asset
1247
+ * @param animationContext The context of the animation when loading the asset
1248
+ * @param animation The glTF animation property
1249
+ * @param channel The glTF animation channel property
1250
+ * @param babylonAnimationGroup The babylon animation group property
1251
+ * @param animationTargetOverride The babylon animation channel target override property. My be null.
1252
+ * @returns A void promise when the channel load is complete
1253
+ */
1254
+ GLTFLoader.prototype._loadAnimationChannelAsync = function (context, animationContext, animation, channel, babylonAnimationGroup, animationTargetOverride) {
1255
+ var _this = this;
1256
+ if (animationTargetOverride === void 0) { animationTargetOverride = null; }
1257
+ if (channel.target.node == undefined) {
1258
+ return Promise.resolve();
1259
+ }
1260
+ var targetNode = ArrayItem.Get("".concat(context, "/target/node"), this._gltf.nodes, channel.target.node);
1261
+ // Ignore animations that have no animation targets.
1262
+ if ((channel.target.path === "weights" /* WEIGHTS */ && !targetNode._numMorphTargets) ||
1263
+ (channel.target.path !== "weights" /* WEIGHTS */ && !targetNode._babylonTransformNode)) {
1264
+ return Promise.resolve();
1265
+ }
1266
+ var sampler = ArrayItem.Get("".concat(context, "/sampler"), animation.samplers, channel.sampler);
1267
+ return this._loadAnimationSamplerAsync("".concat(animationContext, "/samplers/").concat(channel.sampler), sampler).then(function (data) {
1268
+ var targetPath;
1269
+ var animationType;
1270
+ switch (channel.target.path) {
1271
+ case "translation" /* TRANSLATION */: {
1272
+ targetPath = "position";
1273
+ animationType = Animation.ANIMATIONTYPE_VECTOR3;
1274
+ break;
1275
+ }
1276
+ case "rotation" /* ROTATION */: {
1277
+ targetPath = "rotationQuaternion";
1278
+ animationType = Animation.ANIMATIONTYPE_QUATERNION;
1279
+ break;
1280
+ }
1281
+ case "scale" /* SCALE */: {
1282
+ targetPath = "scaling";
1283
+ animationType = Animation.ANIMATIONTYPE_VECTOR3;
1284
+ break;
1285
+ }
1286
+ case "weights" /* WEIGHTS */: {
1287
+ targetPath = "influence";
1288
+ animationType = Animation.ANIMATIONTYPE_FLOAT;
1289
+ break;
1290
+ }
1291
+ default: {
1292
+ throw new Error("".concat(context, "/target/path: Invalid value (").concat(channel.target.path, ")"));
1293
+ }
1294
+ }
1295
+ var outputBufferOffset = 0;
1296
+ var getNextOutputValue;
1297
+ switch (targetPath) {
1298
+ case "position": {
1299
+ getNextOutputValue = function (scale) {
1300
+ var value = Vector3.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1301
+ outputBufferOffset += 3;
1302
+ return value;
1303
+ };
1304
+ break;
1305
+ }
1306
+ case "rotationQuaternion": {
1307
+ getNextOutputValue = function (scale) {
1308
+ var value = Quaternion.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1309
+ outputBufferOffset += 4;
1310
+ return value;
1311
+ };
1312
+ break;
1313
+ }
1314
+ case "scaling": {
1315
+ getNextOutputValue = function (scale) {
1316
+ var value = Vector3.FromArray(data.output, outputBufferOffset).scaleInPlace(scale);
1317
+ outputBufferOffset += 3;
1318
+ return value;
1319
+ };
1320
+ break;
1321
+ }
1322
+ case "influence": {
1323
+ getNextOutputValue = function (scale) {
1324
+ var value = new Array(targetNode._numMorphTargets);
1325
+ for (var i = 0; i < targetNode._numMorphTargets; i++) {
1326
+ value[i] = data.output[outputBufferOffset++] * scale;
1327
+ }
1328
+ return value;
1329
+ };
1330
+ break;
1331
+ }
1332
+ }
1333
+ var getNextKey;
1334
+ switch (data.interpolation) {
1335
+ case "STEP" /* STEP */: {
1336
+ getNextKey = function (frameIndex) { return ({
1337
+ frame: data.input[frameIndex] * _this.parent.targetFps,
1338
+ value: getNextOutputValue(1),
1339
+ interpolation: AnimationKeyInterpolation.STEP,
1340
+ }); };
1341
+ break;
1342
+ }
1343
+ case "LINEAR" /* LINEAR */: {
1344
+ getNextKey = function (frameIndex) { return ({
1345
+ frame: data.input[frameIndex] * _this.parent.targetFps,
1346
+ value: getNextOutputValue(1),
1347
+ }); };
1348
+ break;
1349
+ }
1350
+ case "CUBICSPLINE" /* CUBICSPLINE */: {
1351
+ var invTargetFps_1 = 1 / _this.parent.targetFps;
1352
+ getNextKey = function (frameIndex) { return ({
1353
+ frame: data.input[frameIndex] * _this.parent.targetFps,
1354
+ inTangent: getNextOutputValue(invTargetFps_1),
1355
+ value: getNextOutputValue(1),
1356
+ outTangent: getNextOutputValue(invTargetFps_1),
1357
+ }); };
1358
+ break;
1359
+ }
1360
+ }
1361
+ var keys = new Array(data.input.length);
1362
+ for (var frameIndex = 0; frameIndex < data.input.length; frameIndex++) {
1363
+ keys[frameIndex] = getNextKey(frameIndex);
1364
+ }
1365
+ if (targetPath === "influence") {
1366
+ var _loop_2 = function (targetIndex) {
1367
+ var animationName = "".concat(babylonAnimationGroup.name, "_channel").concat(babylonAnimationGroup.targetedAnimations.length);
1368
+ var babylonAnimation = new Animation(animationName, targetPath, _this.parent.targetFps, animationType);
1369
+ babylonAnimation.setKeys(keys.map(function (key) { return ({
1370
+ frame: key.frame,
1371
+ inTangent: key.inTangent ? key.inTangent[targetIndex] : undefined,
1372
+ value: key.value[targetIndex],
1373
+ outTangent: key.outTangent ? key.outTangent[targetIndex] : undefined,
1374
+ }); }));
1375
+ _this._forEachPrimitive(targetNode, function (babylonAbstractMesh) {
1376
+ var babylonMesh = babylonAbstractMesh;
1377
+ var morphTarget = babylonMesh.morphTargetManager.getTarget(targetIndex);
1378
+ var babylonAnimationClone = babylonAnimation.clone();
1379
+ morphTarget.animations.push(babylonAnimationClone);
1380
+ babylonAnimationGroup.addTargetedAnimation(babylonAnimationClone, morphTarget);
1381
+ });
1382
+ };
1383
+ for (var targetIndex = 0; targetIndex < targetNode._numMorphTargets; targetIndex++) {
1384
+ _loop_2(targetIndex);
1385
+ }
1386
+ }
1387
+ else {
1388
+ var animationName = "".concat(babylonAnimationGroup.name, "_channel").concat(babylonAnimationGroup.targetedAnimations.length);
1389
+ var babylonAnimation = new Animation(animationName, targetPath, _this.parent.targetFps, animationType);
1390
+ babylonAnimation.setKeys(keys);
1391
+ if (animationTargetOverride != null && animationTargetOverride.animations != null) {
1392
+ animationTargetOverride.animations.push(babylonAnimation);
1393
+ babylonAnimationGroup.addTargetedAnimation(babylonAnimation, animationTargetOverride);
1394
+ }
1395
+ else {
1396
+ targetNode._babylonTransformNode.animations.push(babylonAnimation);
1397
+ babylonAnimationGroup.addTargetedAnimation(babylonAnimation, targetNode._babylonTransformNode);
1398
+ }
1399
+ }
1400
+ });
1401
+ };
1402
+ GLTFLoader.prototype._loadAnimationSamplerAsync = function (context, sampler) {
1403
+ if (sampler._data) {
1404
+ return sampler._data;
1405
+ }
1406
+ var interpolation = sampler.interpolation || "LINEAR" /* LINEAR */;
1407
+ switch (interpolation) {
1408
+ case "STEP" /* STEP */:
1409
+ case "LINEAR" /* LINEAR */:
1410
+ case "CUBICSPLINE" /* CUBICSPLINE */: {
1411
+ break;
1412
+ }
1413
+ default: {
1414
+ throw new Error("".concat(context, "/interpolation: Invalid value (").concat(sampler.interpolation, ")"));
1415
+ }
1416
+ }
1417
+ var inputAccessor = ArrayItem.Get("".concat(context, "/input"), this._gltf.accessors, sampler.input);
1418
+ var outputAccessor = ArrayItem.Get("".concat(context, "/output"), this._gltf.accessors, sampler.output);
1419
+ sampler._data = Promise.all([
1420
+ this._loadFloatAccessorAsync("/accessors/".concat(inputAccessor.index), inputAccessor),
1421
+ this._loadFloatAccessorAsync("/accessors/".concat(outputAccessor.index), outputAccessor),
1422
+ ]).then(function (_a) {
1423
+ var inputData = _a[0], outputData = _a[1];
1424
+ return {
1425
+ input: inputData,
1426
+ interpolation: interpolation,
1427
+ output: outputData,
1428
+ };
1429
+ });
1430
+ return sampler._data;
1431
+ };
1432
+ /**
1433
+ * Loads a glTF buffer.
1434
+ * @param context The context when loading the asset
1435
+ * @param buffer The glTF buffer property
1436
+ * @param byteOffset The byte offset to use
1437
+ * @param byteLength The byte length to use
1438
+ * @returns A promise that resolves with the loaded data when the load is complete
1439
+ */
1440
+ GLTFLoader.prototype.loadBufferAsync = function (context, buffer, byteOffset, byteLength) {
1441
+ var extensionPromise = this._extensionsLoadBufferAsync(context, buffer, byteOffset, byteLength);
1442
+ if (extensionPromise) {
1443
+ return extensionPromise;
1444
+ }
1445
+ if (!buffer._data) {
1446
+ if (buffer.uri) {
1447
+ buffer._data = this.loadUriAsync("".concat(context, "/uri"), buffer, buffer.uri);
1448
+ }
1449
+ else {
1450
+ if (!this._bin) {
1451
+ throw new Error("".concat(context, ": Uri is missing or the binary glTF is missing its binary chunk"));
1452
+ }
1453
+ buffer._data = this._bin.readAsync(0, buffer.byteLength);
1454
+ }
1455
+ }
1456
+ return buffer._data.then(function (data) {
1457
+ try {
1458
+ return new Uint8Array(data.buffer, data.byteOffset + byteOffset, byteLength);
1459
+ }
1460
+ catch (e) {
1461
+ throw new Error("".concat(context, ": ").concat(e.message));
1462
+ }
1463
+ });
1464
+ };
1465
+ /**
1466
+ * Loads a glTF buffer view.
1467
+ * @param context The context when loading the asset
1468
+ * @param bufferView The glTF buffer view property
1469
+ * @returns A promise that resolves with the loaded data when the load is complete
1470
+ */
1471
+ GLTFLoader.prototype.loadBufferViewAsync = function (context, bufferView) {
1472
+ var extensionPromise = this._extensionsLoadBufferViewAsync(context, bufferView);
1473
+ if (extensionPromise) {
1474
+ return extensionPromise;
1475
+ }
1476
+ if (bufferView._data) {
1477
+ return bufferView._data;
1478
+ }
1479
+ var buffer = ArrayItem.Get("".concat(context, "/buffer"), this._gltf.buffers, bufferView.buffer);
1480
+ bufferView._data = this.loadBufferAsync("/buffers/".concat(buffer.index), buffer, bufferView.byteOffset || 0, bufferView.byteLength);
1481
+ return bufferView._data;
1482
+ };
1483
+ GLTFLoader.prototype._loadAccessorAsync = function (context, accessor, constructor) {
1484
+ var _this = this;
1485
+ if (accessor._data) {
1486
+ return accessor._data;
1487
+ }
1488
+ var numComponents = GLTFLoader._GetNumComponents(context, accessor.type);
1489
+ var byteStride = numComponents * VertexBuffer.GetTypeByteLength(accessor.componentType);
1490
+ var length = numComponents * accessor.count;
1491
+ if (accessor.bufferView == undefined) {
1492
+ accessor._data = Promise.resolve(new constructor(length));
1493
+ }
1494
+ else {
1495
+ var bufferView_1 = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1496
+ accessor._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView_1.index), bufferView_1).then(function (data) {
1497
+ if (accessor.componentType === 5126 /* FLOAT */ && !accessor.normalized && (!bufferView_1.byteStride || bufferView_1.byteStride === byteStride)) {
1498
+ return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, length);
1499
+ }
1500
+ else {
1501
+ var typedArray_1 = new constructor(length);
1502
+ VertexBuffer.ForEach(data, accessor.byteOffset || 0, bufferView_1.byteStride || byteStride, numComponents, accessor.componentType, typedArray_1.length, accessor.normalized || false, function (value, index) {
1503
+ typedArray_1[index] = value;
1504
+ });
1505
+ return typedArray_1;
1506
+ }
1507
+ });
1508
+ }
1509
+ if (accessor.sparse) {
1510
+ var sparse_1 = accessor.sparse;
1511
+ accessor._data = accessor._data.then(function (data) {
1512
+ var typedArray = data;
1513
+ var indicesBufferView = ArrayItem.Get("".concat(context, "/sparse/indices/bufferView"), _this._gltf.bufferViews, sparse_1.indices.bufferView);
1514
+ var valuesBufferView = ArrayItem.Get("".concat(context, "/sparse/values/bufferView"), _this._gltf.bufferViews, sparse_1.values.bufferView);
1515
+ return Promise.all([
1516
+ _this.loadBufferViewAsync("/bufferViews/".concat(indicesBufferView.index), indicesBufferView),
1517
+ _this.loadBufferViewAsync("/bufferViews/".concat(valuesBufferView.index), valuesBufferView),
1518
+ ]).then(function (_a) {
1519
+ var indicesData = _a[0], valuesData = _a[1];
1520
+ var indices = GLTFLoader._GetTypedArray("".concat(context, "/sparse/indices"), sparse_1.indices.componentType, indicesData, sparse_1.indices.byteOffset, sparse_1.count);
1521
+ var sparseLength = numComponents * sparse_1.count;
1522
+ var values;
1523
+ if (accessor.componentType === 5126 /* FLOAT */ && !accessor.normalized) {
1524
+ values = GLTFLoader._GetTypedArray("".concat(context, "/sparse/values"), accessor.componentType, valuesData, sparse_1.values.byteOffset, sparseLength);
1525
+ }
1526
+ else {
1527
+ var sparseData = GLTFLoader._GetTypedArray("".concat(context, "/sparse/values"), accessor.componentType, valuesData, sparse_1.values.byteOffset, sparseLength);
1528
+ values = new constructor(sparseLength);
1529
+ VertexBuffer.ForEach(sparseData, 0, byteStride, numComponents, accessor.componentType, values.length, accessor.normalized || false, function (value, index) {
1530
+ values[index] = value;
1531
+ });
1532
+ }
1533
+ var valuesIndex = 0;
1534
+ for (var indicesIndex = 0; indicesIndex < indices.length; indicesIndex++) {
1535
+ var dataIndex = indices[indicesIndex] * numComponents;
1536
+ for (var componentIndex = 0; componentIndex < numComponents; componentIndex++) {
1537
+ typedArray[dataIndex++] = values[valuesIndex++];
1538
+ }
1539
+ }
1540
+ return typedArray;
1541
+ });
1542
+ });
1543
+ }
1544
+ return accessor._data;
1545
+ };
1546
+ /**
1547
+ * @param context
1548
+ * @param accessor
1549
+ * @hidden
1550
+ */
1551
+ GLTFLoader.prototype._loadFloatAccessorAsync = function (context, accessor) {
1552
+ return this._loadAccessorAsync(context, accessor, Float32Array);
1553
+ };
1554
+ GLTFLoader.prototype._loadIndicesAccessorAsync = function (context, accessor) {
1555
+ if (accessor.type !== "SCALAR" /* SCALAR */) {
1556
+ throw new Error("".concat(context, "/type: Invalid value ").concat(accessor.type));
1557
+ }
1558
+ if (accessor.componentType !== 5121 /* UNSIGNED_BYTE */ &&
1559
+ accessor.componentType !== 5123 /* UNSIGNED_SHORT */ &&
1560
+ accessor.componentType !== 5125 /* UNSIGNED_INT */) {
1561
+ throw new Error("".concat(context, "/componentType: Invalid value ").concat(accessor.componentType));
1562
+ }
1563
+ if (accessor._data) {
1564
+ return accessor._data;
1565
+ }
1566
+ if (accessor.sparse) {
1567
+ var constructor = GLTFLoader._GetTypedArrayConstructor("".concat(context, "/componentType"), accessor.componentType);
1568
+ accessor._data = this._loadAccessorAsync(context, accessor, constructor);
1569
+ }
1570
+ else {
1571
+ var bufferView = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1572
+ accessor._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView).then(function (data) {
1573
+ return GLTFLoader._GetTypedArray(context, accessor.componentType, data, accessor.byteOffset, accessor.count);
1574
+ });
1575
+ }
1576
+ return accessor._data;
1577
+ };
1578
+ GLTFLoader.prototype._loadVertexBufferViewAsync = function (bufferView) {
1579
+ var _this = this;
1580
+ if (bufferView._babylonBuffer) {
1581
+ return bufferView._babylonBuffer;
1582
+ }
1583
+ bufferView._babylonBuffer = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView).then(function (data) {
1584
+ return new Buffer(_this._babylonScene.getEngine(), data, false);
1585
+ });
1586
+ return bufferView._babylonBuffer;
1587
+ };
1588
+ GLTFLoader.prototype._loadVertexAccessorAsync = function (context, accessor, kind) {
1589
+ var _this = this;
1590
+ var _a;
1591
+ if ((_a = accessor._babylonVertexBuffer) === null || _a === void 0 ? void 0 : _a[kind]) {
1592
+ return accessor._babylonVertexBuffer[kind];
1593
+ }
1594
+ if (!accessor._babylonVertexBuffer) {
1595
+ accessor._babylonVertexBuffer = {};
1596
+ }
1597
+ if (accessor.sparse) {
1598
+ accessor._babylonVertexBuffer[kind] = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
1599
+ return new VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
1600
+ });
1601
+ }
1602
+ // Load joint indices as a float array since the shaders expect float data but glTF uses unsigned byte/short.
1603
+ // This prevents certain platforms (e.g. D3D) from having to convert the data to float on the fly.
1604
+ else if (kind === VertexBuffer.MatricesIndicesKind || kind === VertexBuffer.MatricesIndicesExtraKind) {
1605
+ accessor._babylonVertexBuffer[kind] = this._loadFloatAccessorAsync(context, accessor).then(function (data) {
1606
+ return new VertexBuffer(_this._babylonScene.getEngine(), data, kind, false);
1607
+ });
1608
+ }
1609
+ else {
1610
+ var bufferView_2 = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, accessor.bufferView);
1611
+ accessor._babylonVertexBuffer[kind] = this._loadVertexBufferViewAsync(bufferView_2).then(function (babylonBuffer) {
1612
+ var size = GLTFLoader._GetNumComponents(context, accessor.type);
1613
+ return new VertexBuffer(_this._babylonScene.getEngine(), babylonBuffer, kind, false, false, bufferView_2.byteStride, false, accessor.byteOffset, size, accessor.componentType, accessor.normalized, true, 1, true);
1614
+ });
1615
+ }
1616
+ return accessor._babylonVertexBuffer[kind];
1617
+ };
1618
+ GLTFLoader.prototype._loadMaterialMetallicRoughnessPropertiesAsync = function (context, properties, babylonMaterial) {
1619
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1620
+ throw new Error("".concat(context, ": Material type not supported"));
1621
+ }
1622
+ var promises = new Array();
1623
+ if (properties) {
1624
+ if (properties.baseColorFactor) {
1625
+ babylonMaterial.albedoColor = Color3.FromArray(properties.baseColorFactor);
1626
+ babylonMaterial.alpha = properties.baseColorFactor[3];
1627
+ }
1628
+ else {
1629
+ babylonMaterial.albedoColor = Color3.White();
1630
+ }
1631
+ babylonMaterial.metallic = properties.metallicFactor == undefined ? 1 : properties.metallicFactor;
1632
+ babylonMaterial.roughness = properties.roughnessFactor == undefined ? 1 : properties.roughnessFactor;
1633
+ if (properties.baseColorTexture) {
1634
+ promises.push(this.loadTextureInfoAsync("".concat(context, "/baseColorTexture"), properties.baseColorTexture, function (texture) {
1635
+ texture.name = "".concat(babylonMaterial.name, " (Base Color)");
1636
+ babylonMaterial.albedoTexture = texture;
1637
+ }));
1638
+ }
1639
+ if (properties.metallicRoughnessTexture) {
1640
+ properties.metallicRoughnessTexture.nonColorData = true;
1641
+ promises.push(this.loadTextureInfoAsync("".concat(context, "/metallicRoughnessTexture"), properties.metallicRoughnessTexture, function (texture) {
1642
+ texture.name = "".concat(babylonMaterial.name, " (Metallic Roughness)");
1643
+ babylonMaterial.metallicTexture = texture;
1644
+ }));
1645
+ babylonMaterial.useMetallnessFromMetallicTextureBlue = true;
1646
+ babylonMaterial.useRoughnessFromMetallicTextureGreen = true;
1647
+ babylonMaterial.useRoughnessFromMetallicTextureAlpha = false;
1648
+ }
1649
+ }
1650
+ return Promise.all(promises).then(function () { });
1651
+ };
1652
+ /**
1653
+ * @param context
1654
+ * @param material
1655
+ * @param babylonMesh
1656
+ * @param babylonDrawMode
1657
+ * @param assign
1658
+ * @hidden
1659
+ */
1660
+ GLTFLoader.prototype._loadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
1661
+ if (assign === void 0) { assign = function () { }; }
1662
+ var extensionPromise = this._extensionsLoadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign);
1663
+ if (extensionPromise) {
1664
+ return extensionPromise;
1665
+ }
1666
+ material._data = material._data || {};
1667
+ var babylonData = material._data[babylonDrawMode];
1668
+ if (!babylonData) {
1669
+ this.logOpen("".concat(context, " ").concat(material.name || ""));
1670
+ var babylonMaterial = this.createMaterial(context, material, babylonDrawMode);
1671
+ babylonData = {
1672
+ babylonMaterial: babylonMaterial,
1673
+ babylonMeshes: [],
1674
+ promise: this.loadMaterialPropertiesAsync(context, material, babylonMaterial),
1675
+ };
1676
+ material._data[babylonDrawMode] = babylonData;
1677
+ GLTFLoader.AddPointerMetadata(babylonMaterial, context);
1678
+ this._parent.onMaterialLoadedObservable.notifyObservers(babylonMaterial);
1679
+ this.logClose();
1680
+ }
1681
+ if (babylonMesh) {
1682
+ babylonData.babylonMeshes.push(babylonMesh);
1683
+ babylonMesh.onDisposeObservable.addOnce(function () {
1684
+ var index = babylonData.babylonMeshes.indexOf(babylonMesh);
1685
+ if (index !== -1) {
1686
+ babylonData.babylonMeshes.splice(index, 1);
1687
+ }
1688
+ });
1689
+ }
1690
+ assign(babylonData.babylonMaterial);
1691
+ return babylonData.promise.then(function () {
1692
+ return babylonData.babylonMaterial;
1693
+ });
1694
+ };
1695
+ GLTFLoader.prototype._createDefaultMaterial = function (name, babylonDrawMode) {
1696
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1697
+ var babylonMaterial = new PBRMaterial(name, this._babylonScene);
1698
+ babylonMaterial._parentContainer = this._assetContainer;
1699
+ this._babylonScene._blockEntityCollection = false;
1700
+ // Moved to mesh so user can change materials on gltf meshes: babylonMaterial.sideOrientation = this._babylonScene.useRightHandedSystem ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation;
1701
+ babylonMaterial.fillMode = babylonDrawMode;
1702
+ babylonMaterial.enableSpecularAntiAliasing = true;
1703
+ babylonMaterial.useRadianceOverAlpha = !this._parent.transparencyAsCoverage;
1704
+ babylonMaterial.useSpecularOverAlpha = !this._parent.transparencyAsCoverage;
1705
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1706
+ babylonMaterial.metallic = 1;
1707
+ babylonMaterial.roughness = 1;
1708
+ return babylonMaterial;
1709
+ };
1710
+ /**
1711
+ * Creates a Babylon material from a glTF material.
1712
+ * @param context The context when loading the asset
1713
+ * @param material The glTF material property
1714
+ * @param babylonDrawMode The draw mode for the Babylon material
1715
+ * @returns The Babylon material
1716
+ */
1717
+ GLTFLoader.prototype.createMaterial = function (context, material, babylonDrawMode) {
1718
+ var extensionPromise = this._extensionsCreateMaterial(context, material, babylonDrawMode);
1719
+ if (extensionPromise) {
1720
+ return extensionPromise;
1721
+ }
1722
+ var name = material.name || "material".concat(material.index);
1723
+ var babylonMaterial = this._createDefaultMaterial(name, babylonDrawMode);
1724
+ return babylonMaterial;
1725
+ };
1726
+ /**
1727
+ * Loads properties from a glTF material into a Babylon material.
1728
+ * @param context The context when loading the asset
1729
+ * @param material The glTF material property
1730
+ * @param babylonMaterial The Babylon material
1731
+ * @returns A promise that resolves when the load is complete
1732
+ */
1733
+ GLTFLoader.prototype.loadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
1734
+ var extensionPromise = this._extensionsLoadMaterialPropertiesAsync(context, material, babylonMaterial);
1735
+ if (extensionPromise) {
1736
+ return extensionPromise;
1737
+ }
1738
+ var promises = new Array();
1739
+ promises.push(this.loadMaterialBasePropertiesAsync(context, material, babylonMaterial));
1740
+ if (material.pbrMetallicRoughness) {
1741
+ promises.push(this._loadMaterialMetallicRoughnessPropertiesAsync("".concat(context, "/pbrMetallicRoughness"), material.pbrMetallicRoughness, babylonMaterial));
1742
+ }
1743
+ this.loadMaterialAlphaProperties(context, material, babylonMaterial);
1744
+ return Promise.all(promises).then(function () { });
1745
+ };
1746
+ /**
1747
+ * Loads the normal, occlusion, and emissive properties from a glTF material into a Babylon material.
1748
+ * @param context The context when loading the asset
1749
+ * @param material The glTF material property
1750
+ * @param babylonMaterial The Babylon material
1751
+ * @returns A promise that resolves when the load is complete
1752
+ */
1753
+ GLTFLoader.prototype.loadMaterialBasePropertiesAsync = function (context, material, babylonMaterial) {
1754
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1755
+ throw new Error("".concat(context, ": Material type not supported"));
1756
+ }
1757
+ var promises = new Array();
1758
+ babylonMaterial.emissiveColor = material.emissiveFactor ? Color3.FromArray(material.emissiveFactor) : new Color3(0, 0, 0);
1759
+ if (material.doubleSided) {
1760
+ babylonMaterial.backFaceCulling = false;
1761
+ babylonMaterial.twoSidedLighting = true;
1762
+ }
1763
+ if (material.normalTexture) {
1764
+ material.normalTexture.nonColorData = true;
1765
+ promises.push(this.loadTextureInfoAsync("".concat(context, "/normalTexture"), material.normalTexture, function (texture) {
1766
+ texture.name = "".concat(babylonMaterial.name, " (Normal)");
1767
+ babylonMaterial.bumpTexture = texture;
1768
+ }));
1769
+ babylonMaterial.invertNormalMapX = !this._babylonScene.useRightHandedSystem;
1770
+ babylonMaterial.invertNormalMapY = this._babylonScene.useRightHandedSystem;
1771
+ if (material.normalTexture.scale != undefined) {
1772
+ babylonMaterial.bumpTexture.level = material.normalTexture.scale;
1773
+ }
1774
+ babylonMaterial.forceIrradianceInFragment = true;
1775
+ }
1776
+ if (material.occlusionTexture) {
1777
+ material.occlusionTexture.nonColorData = true;
1778
+ promises.push(this.loadTextureInfoAsync("".concat(context, "/occlusionTexture"), material.occlusionTexture, function (texture) {
1779
+ texture.name = "".concat(babylonMaterial.name, " (Occlusion)");
1780
+ babylonMaterial.ambientTexture = texture;
1781
+ }));
1782
+ babylonMaterial.useAmbientInGrayScale = true;
1783
+ if (material.occlusionTexture.strength != undefined) {
1784
+ babylonMaterial.ambientTextureStrength = material.occlusionTexture.strength;
1785
+ }
1786
+ }
1787
+ if (material.emissiveTexture) {
1788
+ promises.push(this.loadTextureInfoAsync("".concat(context, "/emissiveTexture"), material.emissiveTexture, function (texture) {
1789
+ texture.name = "".concat(babylonMaterial.name, " (Emissive)");
1790
+ babylonMaterial.emissiveTexture = texture;
1791
+ }));
1792
+ }
1793
+ return Promise.all(promises).then(function () { });
1794
+ };
1795
+ /**
1796
+ * Loads the alpha properties from a glTF material into a Babylon material.
1797
+ * Must be called after the setting the albedo texture of the Babylon material when the material has an albedo texture.
1798
+ * @param context The context when loading the asset
1799
+ * @param material The glTF material property
1800
+ * @param babylonMaterial The Babylon material
1801
+ */
1802
+ GLTFLoader.prototype.loadMaterialAlphaProperties = function (context, material, babylonMaterial) {
1803
+ if (!(babylonMaterial instanceof PBRMaterial)) {
1804
+ throw new Error("".concat(context, ": Material type not supported"));
1805
+ }
1806
+ var alphaMode = material.alphaMode || "OPAQUE" /* OPAQUE */;
1807
+ switch (alphaMode) {
1808
+ case "OPAQUE" /* OPAQUE */: {
1809
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_OPAQUE;
1810
+ break;
1811
+ }
1812
+ case "MASK" /* MASK */: {
1813
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHATEST;
1814
+ babylonMaterial.alphaCutOff = material.alphaCutoff == undefined ? 0.5 : material.alphaCutoff;
1815
+ if (babylonMaterial.albedoTexture) {
1816
+ babylonMaterial.albedoTexture.hasAlpha = true;
1817
+ }
1818
+ break;
1819
+ }
1820
+ case "BLEND" /* BLEND */: {
1821
+ babylonMaterial.transparencyMode = PBRMaterial.PBRMATERIAL_ALPHABLEND;
1822
+ if (babylonMaterial.albedoTexture) {
1823
+ babylonMaterial.albedoTexture.hasAlpha = true;
1824
+ babylonMaterial.useAlphaFromAlbedoTexture = true;
1825
+ }
1826
+ break;
1827
+ }
1828
+ default: {
1829
+ throw new Error("".concat(context, "/alphaMode: Invalid value (").concat(material.alphaMode, ")"));
1830
+ }
1831
+ }
1832
+ };
1833
+ /**
1834
+ * Loads a glTF texture info.
1835
+ * @param context The context when loading the asset
1836
+ * @param textureInfo The glTF texture info property
1837
+ * @param assign A function called synchronously after parsing the glTF properties
1838
+ * @returns A promise that resolves with the loaded Babylon texture when the load is complete
1839
+ */
1840
+ GLTFLoader.prototype.loadTextureInfoAsync = function (context, textureInfo, assign) {
1841
+ var _this = this;
1842
+ if (assign === void 0) { assign = function () { }; }
1843
+ var extensionPromise = this._extensionsLoadTextureInfoAsync(context, textureInfo, assign);
1844
+ if (extensionPromise) {
1845
+ return extensionPromise;
1846
+ }
1847
+ this.logOpen("".concat(context));
1848
+ if (textureInfo.texCoord >= 6) {
1849
+ throw new Error("".concat(context, "/texCoord: Invalid value (").concat(textureInfo.texCoord, ")"));
1850
+ }
1851
+ var texture = ArrayItem.Get("".concat(context, "/index"), this._gltf.textures, textureInfo.index);
1852
+ texture._textureInfo = textureInfo;
1853
+ var promise = this._loadTextureAsync("/textures/".concat(textureInfo.index), texture, function (babylonTexture) {
1854
+ babylonTexture.coordinatesIndex = textureInfo.texCoord || 0;
1855
+ GLTFLoader.AddPointerMetadata(babylonTexture, context);
1856
+ _this._parent.onTextureLoadedObservable.notifyObservers(babylonTexture);
1857
+ assign(babylonTexture);
1858
+ });
1859
+ this.logClose();
1860
+ return promise;
1861
+ };
1862
+ /**
1863
+ * @param context
1864
+ * @param texture
1865
+ * @param assign
1866
+ * @hidden
1867
+ */
1868
+ GLTFLoader.prototype._loadTextureAsync = function (context, texture, assign) {
1869
+ if (assign === void 0) { assign = function () { }; }
1870
+ var extensionPromise = this._extensionsLoadTextureAsync(context, texture, assign);
1871
+ if (extensionPromise) {
1872
+ return extensionPromise;
1873
+ }
1874
+ this.logOpen("".concat(context, " ").concat(texture.name || ""));
1875
+ var sampler = texture.sampler == undefined ? GLTFLoader.DefaultSampler : ArrayItem.Get("".concat(context, "/sampler"), this._gltf.samplers, texture.sampler);
1876
+ var image = ArrayItem.Get("".concat(context, "/source"), this._gltf.images, texture.source);
1877
+ var promise = this._createTextureAsync(context, sampler, image, assign, undefined, !texture._textureInfo.nonColorData);
1878
+ this.logClose();
1879
+ return promise;
1880
+ };
1881
+ /**
1882
+ * @param context
1883
+ * @param sampler
1884
+ * @param image
1885
+ * @param assign
1886
+ * @param textureLoaderOptions
1887
+ * @param useSRGBBuffer
1888
+ * @hidden
1889
+ */
1890
+ GLTFLoader.prototype._createTextureAsync = function (context, sampler, image, assign, textureLoaderOptions, useSRGBBuffer) {
1891
+ var _this = this;
1892
+ if (assign === void 0) { assign = function () { }; }
1893
+ var samplerData = this._loadSampler("/samplers/".concat(sampler.index), sampler);
1894
+ var promises = new Array();
1895
+ var deferred = new Deferred();
1896
+ this._babylonScene._blockEntityCollection = !!this._assetContainer;
1897
+ var textureCreationOptions = {
1898
+ noMipmap: samplerData.noMipMaps,
1899
+ invertY: false,
1900
+ samplingMode: samplerData.samplingMode,
1901
+ onLoad: function () {
1902
+ if (!_this._disposed) {
1903
+ deferred.resolve();
1904
+ }
1905
+ },
1906
+ onError: function (message, exception) {
1907
+ if (!_this._disposed) {
1908
+ deferred.reject(new Error("".concat(context, ": ").concat(exception && exception.message ? exception.message : message || "Failed to load texture")));
1909
+ }
1910
+ },
1911
+ mimeType: image.mimeType,
1912
+ loaderOptions: textureLoaderOptions,
1913
+ useSRGBBuffer: !!useSRGBBuffer && this._parent.useSRGBBuffers,
1914
+ };
1915
+ var babylonTexture = new Texture(null, this._babylonScene, textureCreationOptions);
1916
+ babylonTexture._parentContainer = this._assetContainer;
1917
+ this._babylonScene._blockEntityCollection = false;
1918
+ promises.push(deferred.promise);
1919
+ promises.push(this.loadImageAsync("/images/".concat(image.index), image).then(function (data) {
1920
+ var name = image.uri || "".concat(_this._fileName, "#image").concat(image.index);
1921
+ var dataUrl = "data:".concat(_this._uniqueRootUrl).concat(name);
1922
+ babylonTexture.updateURL(dataUrl, data);
1923
+ }));
1924
+ babylonTexture.wrapU = samplerData.wrapU;
1925
+ babylonTexture.wrapV = samplerData.wrapV;
1926
+ assign(babylonTexture);
1927
+ return Promise.all(promises).then(function () {
1928
+ return babylonTexture;
1929
+ });
1930
+ };
1931
+ GLTFLoader.prototype._loadSampler = function (context, sampler) {
1932
+ if (!sampler._data) {
1933
+ sampler._data = {
1934
+ noMipMaps: sampler.minFilter === 9728 /* NEAREST */ || sampler.minFilter === 9729 /* LINEAR */,
1935
+ samplingMode: GLTFLoader._GetTextureSamplingMode(context, sampler),
1936
+ wrapU: GLTFLoader._GetTextureWrapMode("".concat(context, "/wrapS"), sampler.wrapS),
1937
+ wrapV: GLTFLoader._GetTextureWrapMode("".concat(context, "/wrapT"), sampler.wrapT),
1938
+ };
1939
+ }
1940
+ return sampler._data;
1941
+ };
1942
+ /**
1943
+ * Loads a glTF image.
1944
+ * @param context The context when loading the asset
1945
+ * @param image The glTF image property
1946
+ * @returns A promise that resolves with the loaded data when the load is complete
1947
+ */
1948
+ GLTFLoader.prototype.loadImageAsync = function (context, image) {
1949
+ if (!image._data) {
1950
+ this.logOpen("".concat(context, " ").concat(image.name || ""));
1951
+ if (image.uri) {
1952
+ image._data = this.loadUriAsync("".concat(context, "/uri"), image, image.uri);
1953
+ }
1954
+ else {
1955
+ var bufferView = ArrayItem.Get("".concat(context, "/bufferView"), this._gltf.bufferViews, image.bufferView);
1956
+ image._data = this.loadBufferViewAsync("/bufferViews/".concat(bufferView.index), bufferView);
1957
+ }
1958
+ this.logClose();
1959
+ }
1960
+ return image._data;
1961
+ };
1962
+ /**
1963
+ * Loads a glTF uri.
1964
+ * @param context The context when loading the asset
1965
+ * @param property The glTF property associated with the uri
1966
+ * @param uri The base64 or relative uri
1967
+ * @returns A promise that resolves with the loaded data when the load is complete
1968
+ */
1969
+ GLTFLoader.prototype.loadUriAsync = function (context, property, uri) {
1970
+ var _this = this;
1971
+ var extensionPromise = this._extensionsLoadUriAsync(context, property, uri);
1972
+ if (extensionPromise) {
1973
+ return extensionPromise;
1974
+ }
1975
+ if (!GLTFLoader._ValidateUri(uri)) {
1976
+ throw new Error("".concat(context, ": '").concat(uri, "' is invalid"));
1977
+ }
1978
+ if (IsBase64DataUrl(uri)) {
1979
+ var data = new Uint8Array(DecodeBase64UrlToBinary(uri));
1980
+ this.log("".concat(context, ": Decoded ").concat(uri.substr(0, 64), "... (").concat(data.length, " bytes)"));
1981
+ return Promise.resolve(data);
1982
+ }
1983
+ this.log("".concat(context, ": Loading ").concat(uri));
1984
+ return this._parent.preprocessUrlAsync(this._rootUrl + uri).then(function (url) {
1985
+ return new Promise(function (resolve, reject) {
1986
+ _this._parent._loadFile(_this._babylonScene, url, function (data) {
1987
+ if (!_this._disposed) {
1988
+ _this.log("".concat(context, ": Loaded ").concat(uri, " (").concat(data.byteLength, " bytes)"));
1989
+ resolve(new Uint8Array(data));
1990
+ }
1991
+ }, true, function (request) {
1992
+ reject(new LoadFileError("".concat(context, ": Failed to load '").concat(uri, "'").concat(request ? ": " + request.status + " " + request.statusText : ""), request));
1993
+ });
1994
+ });
1995
+ });
1996
+ };
1997
+ /**
1998
+ * Adds a JSON pointer to the metadata of the Babylon object at `<object>.metadata.gltf.pointers`.
1999
+ * @param babylonObject the Babylon object with metadata
2000
+ * @param babylonObject.metadata
2001
+ * @param pointer the JSON pointer
2002
+ */
2003
+ GLTFLoader.AddPointerMetadata = function (babylonObject, pointer) {
2004
+ var metadata = (babylonObject.metadata = babylonObject.metadata || {});
2005
+ var gltf = (metadata.gltf = metadata.gltf || {});
2006
+ var pointers = (gltf.pointers = gltf.pointers || []);
2007
+ pointers.push(pointer);
2008
+ };
2009
+ GLTFLoader._GetTextureWrapMode = function (context, mode) {
2010
+ // Set defaults if undefined
2011
+ mode = mode == undefined ? 10497 /* REPEAT */ : mode;
2012
+ switch (mode) {
2013
+ case 33071 /* CLAMP_TO_EDGE */:
2014
+ return Texture.CLAMP_ADDRESSMODE;
2015
+ case 33648 /* MIRRORED_REPEAT */:
2016
+ return Texture.MIRROR_ADDRESSMODE;
2017
+ case 10497 /* REPEAT */:
2018
+ return Texture.WRAP_ADDRESSMODE;
2019
+ default:
2020
+ Logger.Warn("".concat(context, ": Invalid value (").concat(mode, ")"));
2021
+ return Texture.WRAP_ADDRESSMODE;
2022
+ }
2023
+ };
2024
+ GLTFLoader._GetTextureSamplingMode = function (context, sampler) {
2025
+ // Set defaults if undefined
2026
+ var magFilter = sampler.magFilter == undefined ? 9729 /* LINEAR */ : sampler.magFilter;
2027
+ var minFilter = sampler.minFilter == undefined ? 9987 /* LINEAR_MIPMAP_LINEAR */ : sampler.minFilter;
2028
+ if (magFilter === 9729 /* LINEAR */) {
2029
+ switch (minFilter) {
2030
+ case 9728 /* NEAREST */:
2031
+ return Texture.LINEAR_NEAREST;
2032
+ case 9729 /* LINEAR */:
2033
+ return Texture.LINEAR_LINEAR;
2034
+ case 9984 /* NEAREST_MIPMAP_NEAREST */:
2035
+ return Texture.LINEAR_NEAREST_MIPNEAREST;
2036
+ case 9985 /* LINEAR_MIPMAP_NEAREST */:
2037
+ return Texture.LINEAR_LINEAR_MIPNEAREST;
2038
+ case 9986 /* NEAREST_MIPMAP_LINEAR */:
2039
+ return Texture.LINEAR_NEAREST_MIPLINEAR;
2040
+ case 9987 /* LINEAR_MIPMAP_LINEAR */:
2041
+ return Texture.LINEAR_LINEAR_MIPLINEAR;
2042
+ default:
2043
+ Logger.Warn("".concat(context, "/minFilter: Invalid value (").concat(minFilter, ")"));
2044
+ return Texture.LINEAR_LINEAR_MIPLINEAR;
2045
+ }
2046
+ }
2047
+ else {
2048
+ if (magFilter !== 9728 /* NEAREST */) {
2049
+ Logger.Warn("".concat(context, "/magFilter: Invalid value (").concat(magFilter, ")"));
2050
+ }
2051
+ switch (minFilter) {
2052
+ case 9728 /* NEAREST */:
2053
+ return Texture.NEAREST_NEAREST;
2054
+ case 9729 /* LINEAR */:
2055
+ return Texture.NEAREST_LINEAR;
2056
+ case 9984 /* NEAREST_MIPMAP_NEAREST */:
2057
+ return Texture.NEAREST_NEAREST_MIPNEAREST;
2058
+ case 9985 /* LINEAR_MIPMAP_NEAREST */:
2059
+ return Texture.NEAREST_LINEAR_MIPNEAREST;
2060
+ case 9986 /* NEAREST_MIPMAP_LINEAR */:
2061
+ return Texture.NEAREST_NEAREST_MIPLINEAR;
2062
+ case 9987 /* LINEAR_MIPMAP_LINEAR */:
2063
+ return Texture.NEAREST_LINEAR_MIPLINEAR;
2064
+ default:
2065
+ Logger.Warn("".concat(context, "/minFilter: Invalid value (").concat(minFilter, ")"));
2066
+ return Texture.NEAREST_NEAREST_MIPNEAREST;
2067
+ }
2068
+ }
2069
+ };
2070
+ GLTFLoader._GetTypedArrayConstructor = function (context, componentType) {
2071
+ switch (componentType) {
2072
+ case 5120 /* BYTE */:
2073
+ return Int8Array;
2074
+ case 5121 /* UNSIGNED_BYTE */:
2075
+ return Uint8Array;
2076
+ case 5122 /* SHORT */:
2077
+ return Int16Array;
2078
+ case 5123 /* UNSIGNED_SHORT */:
2079
+ return Uint16Array;
2080
+ case 5125 /* UNSIGNED_INT */:
2081
+ return Uint32Array;
2082
+ case 5126 /* FLOAT */:
2083
+ return Float32Array;
2084
+ default:
2085
+ throw new Error("".concat(context, ": Invalid component type ").concat(componentType));
2086
+ }
2087
+ };
2088
+ GLTFLoader._GetTypedArray = function (context, componentType, bufferView, byteOffset, length) {
2089
+ var buffer = bufferView.buffer;
2090
+ byteOffset = bufferView.byteOffset + (byteOffset || 0);
2091
+ var constructor = GLTFLoader._GetTypedArrayConstructor("".concat(context, "/componentType"), componentType);
2092
+ var componentTypeLength = VertexBuffer.GetTypeByteLength(componentType);
2093
+ if (byteOffset % componentTypeLength !== 0) {
2094
+ // HACK: Copy the buffer if byte offset is not a multiple of component type byte length.
2095
+ Logger.Warn("".concat(context, ": Copying buffer as byte offset (").concat(byteOffset, ") is not a multiple of component type byte length (").concat(componentTypeLength, ")"));
2096
+ return new constructor(buffer.slice(byteOffset, byteOffset + length * componentTypeLength), 0);
2097
+ }
2098
+ return new constructor(buffer, byteOffset, length);
2099
+ };
2100
+ GLTFLoader._GetNumComponents = function (context, type) {
2101
+ switch (type) {
2102
+ case "SCALAR":
2103
+ return 1;
2104
+ case "VEC2":
2105
+ return 2;
2106
+ case "VEC3":
2107
+ return 3;
2108
+ case "VEC4":
2109
+ return 4;
2110
+ case "MAT2":
2111
+ return 4;
2112
+ case "MAT3":
2113
+ return 9;
2114
+ case "MAT4":
2115
+ return 16;
2116
+ }
2117
+ throw new Error("".concat(context, ": Invalid type (").concat(type, ")"));
2118
+ };
2119
+ GLTFLoader._ValidateUri = function (uri) {
2120
+ return Tools.IsBase64(uri) || uri.indexOf("..") === -1;
2121
+ };
2122
+ /**
2123
+ * @param context
2124
+ * @param mode
2125
+ * @hidden
2126
+ */
2127
+ GLTFLoader._GetDrawMode = function (context, mode) {
2128
+ if (mode == undefined) {
2129
+ mode = 4 /* TRIANGLES */;
2130
+ }
2131
+ switch (mode) {
2132
+ case 0 /* POINTS */:
2133
+ return Material.PointListDrawMode;
2134
+ case 1 /* LINES */:
2135
+ return Material.LineListDrawMode;
2136
+ case 2 /* LINE_LOOP */:
2137
+ return Material.LineLoopDrawMode;
2138
+ case 3 /* LINE_STRIP */:
2139
+ return Material.LineStripDrawMode;
2140
+ case 4 /* TRIANGLES */:
2141
+ return Material.TriangleFillMode;
2142
+ case 5 /* TRIANGLE_STRIP */:
2143
+ return Material.TriangleStripDrawMode;
2144
+ case 6 /* TRIANGLE_FAN */:
2145
+ return Material.TriangleFanDrawMode;
2146
+ }
2147
+ throw new Error("".concat(context, ": Invalid mesh primitive mode (").concat(mode, ")"));
2148
+ };
2149
+ GLTFLoader.prototype._compileMaterialsAsync = function () {
2150
+ var _this = this;
2151
+ this._parent._startPerformanceCounter("Compile materials");
2152
+ var promises = new Array();
2153
+ if (this._gltf.materials) {
2154
+ for (var _i = 0, _a = this._gltf.materials; _i < _a.length; _i++) {
2155
+ var material = _a[_i];
2156
+ if (material._data) {
2157
+ for (var babylonDrawMode in material._data) {
2158
+ var babylonData = material._data[babylonDrawMode];
2159
+ for (var _b = 0, _c = babylonData.babylonMeshes; _b < _c.length; _b++) {
2160
+ var babylonMesh = _c[_b];
2161
+ // Ensure nonUniformScaling is set if necessary.
2162
+ babylonMesh.computeWorldMatrix(true);
2163
+ var babylonMaterial = babylonData.babylonMaterial;
2164
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh));
2165
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { useInstances: true }));
2166
+ if (this._parent.useClipPlane) {
2167
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true }));
2168
+ promises.push(babylonMaterial.forceCompilationAsync(babylonMesh, { clipPlane: true, useInstances: true }));
2169
+ }
2170
+ }
2171
+ }
2172
+ }
2173
+ }
2174
+ }
2175
+ return Promise.all(promises).then(function () {
2176
+ _this._parent._endPerformanceCounter("Compile materials");
2177
+ });
2178
+ };
2179
+ GLTFLoader.prototype._compileShadowGeneratorsAsync = function () {
2180
+ var _this = this;
2181
+ this._parent._startPerformanceCounter("Compile shadow generators");
2182
+ var promises = new Array();
2183
+ var lights = this._babylonScene.lights;
2184
+ for (var _i = 0, lights_1 = lights; _i < lights_1.length; _i++) {
2185
+ var light = lights_1[_i];
2186
+ var generator = light.getShadowGenerator();
2187
+ if (generator) {
2188
+ promises.push(generator.forceCompilationAsync());
2189
+ }
2190
+ }
2191
+ return Promise.all(promises).then(function () {
2192
+ _this._parent._endPerformanceCounter("Compile shadow generators");
2193
+ });
2194
+ };
2195
+ GLTFLoader.prototype._forEachExtensions = function (action) {
2196
+ for (var _i = 0, _a = this._extensions; _i < _a.length; _i++) {
2197
+ var extension = _a[_i];
2198
+ if (extension.enabled) {
2199
+ action(extension);
2200
+ }
2201
+ }
2202
+ };
2203
+ GLTFLoader.prototype._applyExtensions = function (property, functionName, actionAsync) {
2204
+ for (var _i = 0, _a = this._extensions; _i < _a.length; _i++) {
2205
+ var extension = _a[_i];
2206
+ if (extension.enabled) {
2207
+ var id = "".concat(extension.name, ".").concat(functionName);
2208
+ var loaderProperty = property;
2209
+ loaderProperty._activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions || {};
2210
+ var activeLoaderExtensionFunctions = loaderProperty._activeLoaderExtensionFunctions;
2211
+ if (!activeLoaderExtensionFunctions[id]) {
2212
+ activeLoaderExtensionFunctions[id] = true;
2213
+ try {
2214
+ var result = actionAsync(extension);
2215
+ if (result) {
2216
+ return result;
2217
+ }
2218
+ }
2219
+ finally {
2220
+ delete activeLoaderExtensionFunctions[id];
2221
+ }
2222
+ }
2223
+ }
2224
+ }
2225
+ return null;
2226
+ };
2227
+ GLTFLoader.prototype._extensionsOnLoading = function () {
2228
+ this._forEachExtensions(function (extension) { return extension.onLoading && extension.onLoading(); });
2229
+ };
2230
+ GLTFLoader.prototype._extensionsOnReady = function () {
2231
+ this._forEachExtensions(function (extension) { return extension.onReady && extension.onReady(); });
2232
+ };
2233
+ GLTFLoader.prototype._extensionsLoadSceneAsync = function (context, scene) {
2234
+ return this._applyExtensions(scene, "loadScene", function (extension) { return extension.loadSceneAsync && extension.loadSceneAsync(context, scene); });
2235
+ };
2236
+ GLTFLoader.prototype._extensionsLoadNodeAsync = function (context, node, assign) {
2237
+ return this._applyExtensions(node, "loadNode", function (extension) { return extension.loadNodeAsync && extension.loadNodeAsync(context, node, assign); });
2238
+ };
2239
+ GLTFLoader.prototype._extensionsLoadCameraAsync = function (context, camera, assign) {
2240
+ return this._applyExtensions(camera, "loadCamera", function (extension) { return extension.loadCameraAsync && extension.loadCameraAsync(context, camera, assign); });
2241
+ };
2242
+ GLTFLoader.prototype._extensionsLoadVertexDataAsync = function (context, primitive, babylonMesh) {
2243
+ return this._applyExtensions(primitive, "loadVertexData", function (extension) { return extension._loadVertexDataAsync && extension._loadVertexDataAsync(context, primitive, babylonMesh); });
2244
+ };
2245
+ GLTFLoader.prototype._extensionsLoadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
2246
+ return this._applyExtensions(primitive, "loadMeshPrimitive", function (extension) { return extension._loadMeshPrimitiveAsync && extension._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, assign); });
2247
+ };
2248
+ GLTFLoader.prototype._extensionsLoadMaterialAsync = function (context, material, babylonMesh, babylonDrawMode, assign) {
2249
+ return this._applyExtensions(material, "loadMaterial", function (extension) { return extension._loadMaterialAsync && extension._loadMaterialAsync(context, material, babylonMesh, babylonDrawMode, assign); });
2250
+ };
2251
+ GLTFLoader.prototype._extensionsCreateMaterial = function (context, material, babylonDrawMode) {
2252
+ return this._applyExtensions(material, "createMaterial", function (extension) { return extension.createMaterial && extension.createMaterial(context, material, babylonDrawMode); });
2253
+ };
2254
+ GLTFLoader.prototype._extensionsLoadMaterialPropertiesAsync = function (context, material, babylonMaterial) {
2255
+ return this._applyExtensions(material, "loadMaterialProperties", function (extension) { return extension.loadMaterialPropertiesAsync && extension.loadMaterialPropertiesAsync(context, material, babylonMaterial); });
2256
+ };
2257
+ GLTFLoader.prototype._extensionsLoadTextureInfoAsync = function (context, textureInfo, assign) {
2258
+ return this._applyExtensions(textureInfo, "loadTextureInfo", function (extension) { return extension.loadTextureInfoAsync && extension.loadTextureInfoAsync(context, textureInfo, assign); });
2259
+ };
2260
+ GLTFLoader.prototype._extensionsLoadTextureAsync = function (context, texture, assign) {
2261
+ return this._applyExtensions(texture, "loadTexture", function (extension) { return extension._loadTextureAsync && extension._loadTextureAsync(context, texture, assign); });
2262
+ };
2263
+ GLTFLoader.prototype._extensionsLoadAnimationAsync = function (context, animation) {
2264
+ return this._applyExtensions(animation, "loadAnimation", function (extension) { return extension.loadAnimationAsync && extension.loadAnimationAsync(context, animation); });
2265
+ };
2266
+ GLTFLoader.prototype._extensionsLoadSkinAsync = function (context, node, skin) {
2267
+ return this._applyExtensions(skin, "loadSkin", function (extension) { return extension._loadSkinAsync && extension._loadSkinAsync(context, node, skin); });
2268
+ };
2269
+ GLTFLoader.prototype._extensionsLoadUriAsync = function (context, property, uri) {
2270
+ return this._applyExtensions(property, "loadUri", function (extension) { return extension._loadUriAsync && extension._loadUriAsync(context, property, uri); });
2271
+ };
2272
+ GLTFLoader.prototype._extensionsLoadBufferViewAsync = function (context, bufferView) {
2273
+ return this._applyExtensions(bufferView, "loadBufferView", function (extension) { return extension.loadBufferViewAsync && extension.loadBufferViewAsync(context, bufferView); });
2274
+ };
2275
+ GLTFLoader.prototype._extensionsLoadBufferAsync = function (context, buffer, byteOffset, byteLength) {
2276
+ return this._applyExtensions(buffer, "loadBuffer", function (extension) { return extension.loadBufferAsync && extension.loadBufferAsync(context, buffer, byteOffset, byteLength); });
2277
+ };
2278
+ /**
2279
+ * Helper method called by a loader extension to load an glTF extension.
2280
+ * @param context The context when loading the asset
2281
+ * @param property The glTF property to load the extension from
2282
+ * @param extensionName The name of the extension to load
2283
+ * @param actionAsync The action to run
2284
+ * @returns The promise returned by actionAsync or null if the extension does not exist
2285
+ */
2286
+ GLTFLoader.LoadExtensionAsync = function (context, property, extensionName, actionAsync) {
2287
+ if (!property.extensions) {
2288
+ return null;
2289
+ }
2290
+ var extensions = property.extensions;
2291
+ var extension = extensions[extensionName];
2292
+ if (!extension) {
2293
+ return null;
2294
+ }
2295
+ return actionAsync("".concat(context, "/extensions/").concat(extensionName), extension);
2296
+ };
2297
+ /**
2298
+ * Helper method called by a loader extension to load a glTF extra.
2299
+ * @param context The context when loading the asset
2300
+ * @param property The glTF property to load the extra from
2301
+ * @param extensionName The name of the extension to load
2302
+ * @param actionAsync The action to run
2303
+ * @returns The promise returned by actionAsync or null if the extra does not exist
2304
+ */
2305
+ GLTFLoader.LoadExtraAsync = function (context, property, extensionName, actionAsync) {
2306
+ if (!property.extras) {
2307
+ return null;
2308
+ }
2309
+ var extras = property.extras;
2310
+ var extra = extras[extensionName];
2311
+ if (!extra) {
2312
+ return null;
2313
+ }
2314
+ return actionAsync("".concat(context, "/extras/").concat(extensionName), extra);
2315
+ };
2316
+ /**
2317
+ * Checks for presence of an extension.
2318
+ * @param name The name of the extension to check
2319
+ * @returns A boolean indicating the presence of the given extension name in `extensionsUsed`
2320
+ */
2321
+ GLTFLoader.prototype.isExtensionUsed = function (name) {
2322
+ return !!this._gltf.extensionsUsed && this._gltf.extensionsUsed.indexOf(name) !== -1;
2323
+ };
2324
+ /**
2325
+ * Increments the indentation level and logs a message.
2326
+ * @param message The message to log
2327
+ */
2328
+ GLTFLoader.prototype.logOpen = function (message) {
2329
+ this._parent._logOpen(message);
2330
+ };
2331
+ /**
2332
+ * Decrements the indentation level.
2333
+ */
2334
+ GLTFLoader.prototype.logClose = function () {
2335
+ this._parent._logClose();
2336
+ };
2337
+ /**
2338
+ * Logs a message
2339
+ * @param message The message to log
2340
+ */
2341
+ GLTFLoader.prototype.log = function (message) {
2342
+ this._parent._log(message);
2343
+ };
2344
+ /**
2345
+ * Starts a performance counter.
2346
+ * @param counterName The name of the performance counter
2347
+ */
2348
+ GLTFLoader.prototype.startPerformanceCounter = function (counterName) {
2349
+ this._parent._startPerformanceCounter(counterName);
2350
+ };
2351
+ /**
2352
+ * Ends a performance counter.
2353
+ * @param counterName The name of the performance counter
2354
+ */
2355
+ GLTFLoader.prototype.endPerformanceCounter = function (counterName) {
2356
+ this._parent._endPerformanceCounter(counterName);
2357
+ };
2358
+ GLTFLoader._RegisteredExtensions = {};
2359
+ /**
2360
+ * The default glTF sampler.
2361
+ */
2362
+ GLTFLoader.DefaultSampler = { index: -1 };
2363
+ return GLTFLoader;
2364
+ }());
2365
+ export { GLTFLoader };
2366
+ GLTFFileLoader._CreateGLTF2Loader = function (parent) { return new GLTFLoader(parent); };
2267
2367
  //# sourceMappingURL=glTFLoader.js.map