@babylonjs/loaders 5.0.0-rc.0 → 5.0.0-rc.11

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,301 +1,297 @@
1
- import { Vector2 } from "@babylonjs/core/Maths/math.vector.js";
2
- import { Tools } from "@babylonjs/core/Misc/tools.js";
3
- import { SceneLoader } from "@babylonjs/core/Loading/sceneLoader.js";
4
- import { AssetContainer } from "@babylonjs/core/assetContainer.js";
5
- import { MTLFileLoader } from './mtlFileLoader.js';
6
- import { SolidParser } from "./solidParser.js";
7
- /**
8
- * OBJ file type loader.
9
- * This is a babylon scene loader plugin.
10
- */
11
- var OBJFileLoader = /** @class */ (function () {
12
- /**
13
- * Creates loader for .OBJ files
14
- *
15
- * @param loadingOptions options for loading and parsing OBJ/MTL files.
16
- */
17
- function OBJFileLoader(loadingOptions) {
18
- /**
19
- * Defines the name of the plugin.
20
- */
21
- this.name = "obj";
22
- /**
23
- * Defines the extension the plugin is able to load.
24
- */
25
- this.extensions = ".obj";
26
- this._assetContainer = null;
27
- this._loadingOptions = loadingOptions || OBJFileLoader.DefaultLoadingOptions;
28
- }
29
- Object.defineProperty(OBJFileLoader, "INVERT_TEXTURE_Y", {
30
- /**
31
- * Invert Y-Axis of referenced textures on load
32
- */
33
- get: function () {
34
- return MTLFileLoader.INVERT_TEXTURE_Y;
35
- },
36
- set: function (value) {
37
- MTLFileLoader.INVERT_TEXTURE_Y = value;
38
- },
39
- enumerable: false,
40
- configurable: true
41
- });
42
- Object.defineProperty(OBJFileLoader, "DefaultLoadingOptions", {
43
- get: function () {
44
- return {
45
- computeNormals: OBJFileLoader.COMPUTE_NORMALS,
46
- optimizeNormals: OBJFileLoader.OPTIMIZE_NORMALS,
47
- importVertexColors: OBJFileLoader.IMPORT_VERTEX_COLORS,
48
- invertY: OBJFileLoader.INVERT_Y,
49
- invertTextureY: OBJFileLoader.INVERT_TEXTURE_Y,
50
- UVScaling: OBJFileLoader.UV_SCALING,
51
- materialLoadingFailsSilently: OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY,
52
- optimizeWithUV: OBJFileLoader.OPTIMIZE_WITH_UV,
53
- skipMaterials: OBJFileLoader.SKIP_MATERIALS
54
- };
55
- },
56
- enumerable: false,
57
- configurable: true
58
- });
59
- /**
60
- * Calls synchronously the MTL file attached to this obj.
61
- * Load function or importMesh function don't enable to load 2 files in the same time asynchronously.
62
- * Without this function materials are not displayed in the first frame (but displayed after).
63
- * In consequence it is impossible to get material information in your HTML file
64
- *
65
- * @param url The URL of the MTL file
66
- * @param rootUrl defines where to load data from
67
- * @param onSuccess Callback function to be called when the MTL file is loaded
68
- */
69
- OBJFileLoader.prototype._loadMTL = function (url, rootUrl, onSuccess, onFailure) {
70
- //The complete path to the mtl file
71
- var pathOfFile = rootUrl + url;
72
- // Loads through the babylon tools to allow fileInput search.
73
- Tools.LoadFile(pathOfFile, onSuccess, undefined, undefined, false, function (request, exception) {
74
- onFailure(pathOfFile, exception);
75
- });
76
- };
77
- /**
78
- * Instantiates a OBJ file loader plugin.
79
- * @returns the created plugin
80
- */
81
- OBJFileLoader.prototype.createPlugin = function () {
82
- return new OBJFileLoader(OBJFileLoader.DefaultLoadingOptions);
83
- };
84
- /**
85
- * If the data string can be loaded directly.
86
- *
87
- * @param data string containing the file data
88
- * @returns if the data can be loaded directly
89
- */
90
- OBJFileLoader.prototype.canDirectLoad = function (data) {
91
- return false;
92
- };
93
- /**
94
- * Imports one or more meshes from the loaded OBJ data and adds them to the scene
95
- * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
96
- * @param scene the scene the meshes should be added to
97
- * @param data the OBJ data to load
98
- * @param rootUrl root url to load from
99
- * @param onProgress event that fires when loading progress has occured
100
- * @param fileName Defines the name of the file to load
101
- * @returns a promise containg the loaded meshes, particles, skeletons and animations
102
- */
103
- OBJFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl, onProgress, fileName) {
104
- //get the meshes from OBJ file
105
- return this._parseSolid(meshesNames, scene, data, rootUrl).then(function (meshes) {
106
- return {
107
- meshes: meshes,
108
- particleSystems: [],
109
- skeletons: [],
110
- animationGroups: [],
111
- transformNodes: [],
112
- geometries: [],
113
- lights: []
114
- };
115
- });
116
- };
117
- /**
118
- * Imports all objects from the loaded OBJ data and adds them to the scene
119
- * @param scene the scene the objects should be added to
120
- * @param data the OBJ data to load
121
- * @param rootUrl root url to load from
122
- * @param onProgress event that fires when loading progress has occured
123
- * @param fileName Defines the name of the file to load
124
- * @returns a promise which completes when objects have been loaded to the scene
125
- */
126
- OBJFileLoader.prototype.loadAsync = function (scene, data, rootUrl, onProgress, fileName) {
127
- //Get the 3D model
128
- return this.importMeshAsync(null, scene, data, rootUrl, onProgress).then(function () {
129
- // return void
130
- });
131
- };
132
- /**
133
- * Load into an asset container.
134
- * @param scene The scene to load into
135
- * @param data The data to import
136
- * @param rootUrl The root url for scene and resources
137
- * @param onProgress The callback when the load progresses
138
- * @param fileName Defines the name of the file to load
139
- * @returns The loaded asset container
140
- */
141
- OBJFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
142
- var _this = this;
143
- var container = new AssetContainer(scene);
144
- this._assetContainer = container;
145
- return this.importMeshAsync(null, scene, data, rootUrl).then(function (result) {
146
- result.meshes.forEach(function (mesh) { return container.meshes.push(mesh); });
147
- result.meshes.forEach(function (mesh) {
148
- var material = mesh.material;
149
- if (material) {
150
- // Materials
151
- if (container.materials.indexOf(material) == -1) {
152
- container.materials.push(material);
153
- // Textures
154
- var textures = material.getActiveTextures();
155
- textures.forEach(function (t) {
156
- if (container.textures.indexOf(t) == -1) {
157
- container.textures.push(t);
158
- }
159
- });
160
- }
161
- }
162
- });
163
- _this._assetContainer = null;
164
- return container;
165
- }).catch(function (ex) {
166
- _this._assetContainer = null;
167
- throw ex;
168
- });
169
- };
170
- /**
171
- * Read the OBJ file and create an Array of meshes.
172
- * Each mesh contains all information given by the OBJ and the MTL file.
173
- * i.e. vertices positions and indices, optional normals values, optional UV values, optional material
174
- * @param meshesNames defines a string or array of strings of the mesh names that should be loaded from the file
175
- * @param scene defines the scene where are displayed the data
176
- * @param data defines the content of the obj file
177
- * @param rootUrl defines the path to the folder
178
- * @returns the list of loaded meshes
179
- */
180
- OBJFileLoader.prototype._parseSolid = function (meshesNames, scene, data, rootUrl) {
181
- var _this = this;
182
- var fileToLoad = ""; //The name of the mtlFile to load
183
- var materialsFromMTLFile = new MTLFileLoader();
184
- var materialToUse = new Array();
185
- var babylonMeshesArray = []; //The mesh for babylon
186
- // Main function
187
- var solidParser = new SolidParser(materialToUse, babylonMeshesArray, this._loadingOptions);
188
- solidParser.parse(meshesNames, data, scene, this._assetContainer, function (fileName) {
189
- fileToLoad = fileName;
190
- });
191
- // load the materials
192
- var mtlPromises = [];
193
- // Check if we have a file to load
194
- if (fileToLoad !== "" && !this._loadingOptions.skipMaterials) {
195
- //Load the file synchronously
196
- mtlPromises.push(new Promise(function (resolve, reject) {
197
- _this._loadMTL(fileToLoad, rootUrl, function (dataLoaded) {
198
- try {
199
- //Create materials thanks MTLLoader function
200
- materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl, _this._assetContainer);
201
- //Look at each material loaded in the mtl file
202
- for (var n = 0; n < materialsFromMTLFile.materials.length; n++) {
203
- //Three variables to get all meshes with the same material
204
- var startIndex = 0;
205
- var _indices = [];
206
- var _index;
207
- //The material from MTL file is used in the meshes loaded
208
- //Push the indice in an array
209
- //Check if the material is not used for another mesh
210
- while ((_index = materialToUse.indexOf(materialsFromMTLFile.materials[n].name, startIndex)) > -1) {
211
- _indices.push(_index);
212
- startIndex = _index + 1;
213
- }
214
- //If the material is not used dispose it
215
- if (_index === -1 && _indices.length === 0) {
216
- //If the material is not needed, remove it
217
- materialsFromMTLFile.materials[n].dispose();
218
- }
219
- else {
220
- for (var o = 0; o < _indices.length; o++) {
221
- //Apply the material to the Mesh for each mesh with the material
222
- var mesh = babylonMeshesArray[_indices[o]];
223
- var material = materialsFromMTLFile.materials[n];
224
- mesh.material = material;
225
- if (!mesh.getTotalIndices()) {
226
- // No indices, we need to turn on point cloud
227
- material.pointsCloud = true;
228
- }
229
- }
230
- }
231
- }
232
- resolve();
233
- }
234
- catch (e) {
235
- Tools.Warn("Error processing MTL file: '".concat(fileToLoad, "'"));
236
- if (_this._loadingOptions.materialLoadingFailsSilently) {
237
- resolve();
238
- }
239
- else {
240
- reject(e);
241
- }
242
- }
243
- }, function (pathOfFile, exception) {
244
- Tools.Warn("Error downloading MTL file: '".concat(fileToLoad, "'"));
245
- if (_this._loadingOptions.materialLoadingFailsSilently) {
246
- resolve();
247
- }
248
- else {
249
- reject(exception);
250
- }
251
- });
252
- }));
253
- }
254
- //Return an array with all Mesh
255
- return Promise.all(mtlPromises).then(function () {
256
- return babylonMeshesArray;
257
- });
258
- };
259
- /**
260
- * Defines if UVs are optimized by default during load.
261
- */
262
- OBJFileLoader.OPTIMIZE_WITH_UV = true;
263
- /**
264
- * Invert model on y-axis (does a model scaling inversion)
265
- */
266
- OBJFileLoader.INVERT_Y = false;
267
- /**
268
- * Include in meshes the vertex colors available in some OBJ files. This is not part of OBJ standard.
269
- */
270
- OBJFileLoader.IMPORT_VERTEX_COLORS = false;
271
- /**
272
- * Compute the normals for the model, even if normals are present in the file.
273
- */
274
- OBJFileLoader.COMPUTE_NORMALS = false;
275
- /**
276
- * Optimize the normals for the model. Lighting can be uneven if you use OptimizeWithUV = true because new vertices can be created for the same location if they pertain to different faces.
277
- * Using OptimizehNormals = true will help smoothing the lighting by averaging the normals of those vertices.
278
- */
279
- OBJFileLoader.OPTIMIZE_NORMALS = false;
280
- /**
281
- * Defines custom scaling of UV coordinates of loaded meshes.
282
- */
283
- OBJFileLoader.UV_SCALING = new Vector2(1, 1);
284
- /**
285
- * Skip loading the materials even if defined in the OBJ file (materials are ignored).
286
- */
287
- OBJFileLoader.SKIP_MATERIALS = false;
288
- /**
289
- * When a material fails to load OBJ loader will silently fail and onSuccess() callback will be triggered.
290
- *
291
- * Defaults to true for backwards compatibility.
292
- */
293
- OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY = true;
294
- return OBJFileLoader;
295
- }());
296
- export { OBJFileLoader };
297
- if (SceneLoader) {
298
- //Add this loader into the register plugin
299
- SceneLoader.RegisterPlugin(new OBJFileLoader());
300
- }
1
+ import { Vector2 } from "@babylonjs/core/Maths/math.vector.js";
2
+ import { Tools } from "@babylonjs/core/Misc/tools.js";
3
+ import { SceneLoader } from "@babylonjs/core/Loading/sceneLoader.js";
4
+ import { AssetContainer } from "@babylonjs/core/assetContainer.js";
5
+ import { MTLFileLoader } from "./mtlFileLoader";
6
+ import { SolidParser } from "./solidParser";
7
+ /**
8
+ * OBJ file type loader.
9
+ * This is a babylon scene loader plugin.
10
+ */
11
+ var OBJFileLoader = /** @class */ (function () {
12
+ /**
13
+ * Creates loader for .OBJ files
14
+ *
15
+ * @param loadingOptions options for loading and parsing OBJ/MTL files.
16
+ */
17
+ function OBJFileLoader(loadingOptions) {
18
+ /**
19
+ * Defines the name of the plugin.
20
+ */
21
+ this.name = "obj";
22
+ /**
23
+ * Defines the extension the plugin is able to load.
24
+ */
25
+ this.extensions = ".obj";
26
+ this._assetContainer = null;
27
+ this._loadingOptions = loadingOptions || OBJFileLoader._DefaultLoadingOptions;
28
+ }
29
+ Object.defineProperty(OBJFileLoader, "INVERT_TEXTURE_Y", {
30
+ /**
31
+ * Invert Y-Axis of referenced textures on load
32
+ */
33
+ get: function () {
34
+ return MTLFileLoader.INVERT_TEXTURE_Y;
35
+ },
36
+ set: function (value) {
37
+ MTLFileLoader.INVERT_TEXTURE_Y = value;
38
+ },
39
+ enumerable: false,
40
+ configurable: true
41
+ });
42
+ Object.defineProperty(OBJFileLoader, "_DefaultLoadingOptions", {
43
+ get: function () {
44
+ return {
45
+ computeNormals: OBJFileLoader.COMPUTE_NORMALS,
46
+ optimizeNormals: OBJFileLoader.OPTIMIZE_NORMALS,
47
+ importVertexColors: OBJFileLoader.IMPORT_VERTEX_COLORS,
48
+ invertY: OBJFileLoader.INVERT_Y,
49
+ invertTextureY: OBJFileLoader.INVERT_TEXTURE_Y,
50
+ // eslint-disable-next-line @typescript-eslint/naming-convention
51
+ UVScaling: OBJFileLoader.UV_SCALING,
52
+ materialLoadingFailsSilently: OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY,
53
+ optimizeWithUV: OBJFileLoader.OPTIMIZE_WITH_UV,
54
+ skipMaterials: OBJFileLoader.SKIP_MATERIALS,
55
+ };
56
+ },
57
+ enumerable: false,
58
+ configurable: true
59
+ });
60
+ /**
61
+ * Calls synchronously the MTL file attached to this obj.
62
+ * Load function or importMesh function don't enable to load 2 files in the same time asynchronously.
63
+ * Without this function materials are not displayed in the first frame (but displayed after).
64
+ * In consequence it is impossible to get material information in your HTML file
65
+ *
66
+ * @param url The URL of the MTL file
67
+ * @param rootUrl defines where to load data from
68
+ * @param onSuccess Callback function to be called when the MTL file is loaded
69
+ * @param onFailure
70
+ */
71
+ OBJFileLoader.prototype._loadMTL = function (url, rootUrl, onSuccess, onFailure) {
72
+ //The complete path to the mtl file
73
+ var pathOfFile = rootUrl + url;
74
+ // Loads through the babylon tools to allow fileInput search.
75
+ Tools.LoadFile(pathOfFile, onSuccess, undefined, undefined, false, function (request, exception) {
76
+ onFailure(pathOfFile, exception);
77
+ });
78
+ };
79
+ /**
80
+ * Instantiates a OBJ file loader plugin.
81
+ * @returns the created plugin
82
+ */
83
+ OBJFileLoader.prototype.createPlugin = function () {
84
+ return new OBJFileLoader(OBJFileLoader._DefaultLoadingOptions);
85
+ };
86
+ /**
87
+ * If the data string can be loaded directly.
88
+ * @returns if the data can be loaded directly
89
+ */
90
+ OBJFileLoader.prototype.canDirectLoad = function () {
91
+ return false;
92
+ };
93
+ /**
94
+ * Imports one or more meshes from the loaded OBJ data and adds them to the scene
95
+ * @param meshesNames a string or array of strings of the mesh names that should be loaded from the file
96
+ * @param scene the scene the meshes should be added to
97
+ * @param data the OBJ data to load
98
+ * @param rootUrl root url to load from
99
+ * @returns a promise containing the loaded meshes, particles, skeletons and animations
100
+ */
101
+ OBJFileLoader.prototype.importMeshAsync = function (meshesNames, scene, data, rootUrl) {
102
+ //get the meshes from OBJ file
103
+ return this._parseSolid(meshesNames, scene, data, rootUrl).then(function (meshes) {
104
+ return {
105
+ meshes: meshes,
106
+ particleSystems: [],
107
+ skeletons: [],
108
+ animationGroups: [],
109
+ transformNodes: [],
110
+ geometries: [],
111
+ lights: [],
112
+ };
113
+ });
114
+ };
115
+ /**
116
+ * Imports all objects from the loaded OBJ data and adds them to the scene
117
+ * @param scene the scene the objects should be added to
118
+ * @param data the OBJ data to load
119
+ * @param rootUrl root url to load from
120
+ * @returns a promise which completes when objects have been loaded to the scene
121
+ */
122
+ OBJFileLoader.prototype.loadAsync = function (scene, data, rootUrl) {
123
+ //Get the 3D model
124
+ return this.importMeshAsync(null, scene, data, rootUrl).then(function () {
125
+ // return void
126
+ });
127
+ };
128
+ /**
129
+ * Load into an asset container.
130
+ * @param scene The scene to load into
131
+ * @param data The data to import
132
+ * @param rootUrl The root url for scene and resources
133
+ * @returns The loaded asset container
134
+ */
135
+ OBJFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl) {
136
+ var _this = this;
137
+ var container = new AssetContainer(scene);
138
+ this._assetContainer = container;
139
+ return this.importMeshAsync(null, scene, data, rootUrl)
140
+ .then(function (result) {
141
+ result.meshes.forEach(function (mesh) { return container.meshes.push(mesh); });
142
+ result.meshes.forEach(function (mesh) {
143
+ var material = mesh.material;
144
+ if (material) {
145
+ // Materials
146
+ if (container.materials.indexOf(material) == -1) {
147
+ container.materials.push(material);
148
+ // Textures
149
+ var textures = material.getActiveTextures();
150
+ textures.forEach(function (t) {
151
+ if (container.textures.indexOf(t) == -1) {
152
+ container.textures.push(t);
153
+ }
154
+ });
155
+ }
156
+ }
157
+ });
158
+ _this._assetContainer = null;
159
+ return container;
160
+ })
161
+ .catch(function (ex) {
162
+ _this._assetContainer = null;
163
+ throw ex;
164
+ });
165
+ };
166
+ /**
167
+ * Read the OBJ file and create an Array of meshes.
168
+ * Each mesh contains all information given by the OBJ and the MTL file.
169
+ * i.e. vertices positions and indices, optional normals values, optional UV values, optional material
170
+ * @param meshesNames defines a string or array of strings of the mesh names that should be loaded from the file
171
+ * @param scene defines the scene where are displayed the data
172
+ * @param data defines the content of the obj file
173
+ * @param rootUrl defines the path to the folder
174
+ * @returns the list of loaded meshes
175
+ */
176
+ OBJFileLoader.prototype._parseSolid = function (meshesNames, scene, data, rootUrl) {
177
+ var _this = this;
178
+ var fileToLoad = ""; //The name of the mtlFile to load
179
+ var materialsFromMTLFile = new MTLFileLoader();
180
+ var materialToUse = new Array();
181
+ var babylonMeshesArray = []; //The mesh for babylon
182
+ // Main function
183
+ var solidParser = new SolidParser(materialToUse, babylonMeshesArray, this._loadingOptions);
184
+ solidParser.parse(meshesNames, data, scene, this._assetContainer, function (fileName) {
185
+ fileToLoad = fileName;
186
+ });
187
+ // load the materials
188
+ var mtlPromises = [];
189
+ // Check if we have a file to load
190
+ if (fileToLoad !== "" && !this._loadingOptions.skipMaterials) {
191
+ //Load the file synchronously
192
+ mtlPromises.push(new Promise(function (resolve, reject) {
193
+ _this._loadMTL(fileToLoad, rootUrl, function (dataLoaded) {
194
+ try {
195
+ //Create materials thanks MTLLoader function
196
+ materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl, _this._assetContainer);
197
+ //Look at each material loaded in the mtl file
198
+ for (var n = 0; n < materialsFromMTLFile.materials.length; n++) {
199
+ //Three variables to get all meshes with the same material
200
+ var startIndex = 0;
201
+ var _indices = [];
202
+ var _index = void 0;
203
+ //The material from MTL file is used in the meshes loaded
204
+ //Push the indice in an array
205
+ //Check if the material is not used for another mesh
206
+ while ((_index = materialToUse.indexOf(materialsFromMTLFile.materials[n].name, startIndex)) > -1) {
207
+ _indices.push(_index);
208
+ startIndex = _index + 1;
209
+ }
210
+ //If the material is not used dispose it
211
+ if (_index === -1 && _indices.length === 0) {
212
+ //If the material is not needed, remove it
213
+ materialsFromMTLFile.materials[n].dispose();
214
+ }
215
+ else {
216
+ for (var o = 0; o < _indices.length; o++) {
217
+ //Apply the material to the Mesh for each mesh with the material
218
+ var mesh = babylonMeshesArray[_indices[o]];
219
+ var material = materialsFromMTLFile.materials[n];
220
+ mesh.material = material;
221
+ if (!mesh.getTotalIndices()) {
222
+ // No indices, we need to turn on point cloud
223
+ material.pointsCloud = true;
224
+ }
225
+ }
226
+ }
227
+ }
228
+ resolve();
229
+ }
230
+ catch (e) {
231
+ Tools.Warn("Error processing MTL file: '".concat(fileToLoad, "'"));
232
+ if (_this._loadingOptions.materialLoadingFailsSilently) {
233
+ resolve();
234
+ }
235
+ else {
236
+ reject(e);
237
+ }
238
+ }
239
+ }, function (pathOfFile, exception) {
240
+ Tools.Warn("Error downloading MTL file: '".concat(fileToLoad, "'"));
241
+ if (_this._loadingOptions.materialLoadingFailsSilently) {
242
+ resolve();
243
+ }
244
+ else {
245
+ reject(exception);
246
+ }
247
+ });
248
+ }));
249
+ }
250
+ //Return an array with all Mesh
251
+ return Promise.all(mtlPromises).then(function () {
252
+ return babylonMeshesArray;
253
+ });
254
+ };
255
+ /**
256
+ * Defines if UVs are optimized by default during load.
257
+ */
258
+ OBJFileLoader.OPTIMIZE_WITH_UV = true;
259
+ /**
260
+ * Invert model on y-axis (does a model scaling inversion)
261
+ */
262
+ OBJFileLoader.INVERT_Y = false;
263
+ /**
264
+ * Include in meshes the vertex colors available in some OBJ files. This is not part of OBJ standard.
265
+ */
266
+ OBJFileLoader.IMPORT_VERTEX_COLORS = false;
267
+ /**
268
+ * Compute the normals for the model, even if normals are present in the file.
269
+ */
270
+ OBJFileLoader.COMPUTE_NORMALS = false;
271
+ /**
272
+ * Optimize the normals for the model. Lighting can be uneven if you use OptimizeWithUV = true because new vertices can be created for the same location if they pertain to different faces.
273
+ * Using OptimizehNormals = true will help smoothing the lighting by averaging the normals of those vertices.
274
+ */
275
+ OBJFileLoader.OPTIMIZE_NORMALS = false;
276
+ /**
277
+ * Defines custom scaling of UV coordinates of loaded meshes.
278
+ */
279
+ OBJFileLoader.UV_SCALING = new Vector2(1, 1);
280
+ /**
281
+ * Skip loading the materials even if defined in the OBJ file (materials are ignored).
282
+ */
283
+ OBJFileLoader.SKIP_MATERIALS = false;
284
+ /**
285
+ * When a material fails to load OBJ loader will silently fail and onSuccess() callback will be triggered.
286
+ *
287
+ * Defaults to true for backwards compatibility.
288
+ */
289
+ OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY = true;
290
+ return OBJFileLoader;
291
+ }());
292
+ export { OBJFileLoader };
293
+ if (SceneLoader) {
294
+ //Add this loader into the register plugin
295
+ SceneLoader.RegisterPlugin(new OBJFileLoader());
296
+ }
301
297
  //# sourceMappingURL=objFileLoader.js.map