@babylonjs/loaders 5.0.0-rc.7 → 5.0.0-rc.9
Sign up to get free protection for your applications and to get access to all the features.
- package/OBJ/index.d.ts +4 -4
- package/OBJ/index.js +4 -4
- package/OBJ/mtlFileLoader.d.ts +41 -41
- package/OBJ/mtlFileLoader.js +232 -232
- package/OBJ/objFileLoader.d.ts +126 -126
- package/OBJ/objFileLoader.js +296 -296
- package/OBJ/objLoadingOptions.d.ts +43 -43
- package/OBJ/objLoadingOptions.js +1 -1
- package/OBJ/solidParser.d.ts +154 -154
- package/OBJ/solidParser.js +693 -693
- package/STL/index.d.ts +1 -1
- package/STL/index.js +1 -1
- package/STL/stlFileLoader.d.ts +64 -64
- package/STL/stlFileLoader.js +233 -233
- package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
- package/glTF/1.0/glTFBinaryExtension.js +60 -60
- package/glTF/1.0/glTFLoader.d.ts +136 -136
- package/glTF/1.0/glTFLoader.js +1815 -1815
- package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
- package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
- package/glTF/1.0/glTFLoaderUtils.d.ts +62 -62
- package/glTF/1.0/glTFLoaderUtils.js +250 -250
- package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
- package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -129
- package/glTF/1.0/index.d.ts +5 -5
- package/glTF/1.0/index.js +5 -5
- package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -43
- package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -117
- package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -36
- package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -88
- package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -34
- package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -49
- package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -29
- package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -40
- package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -50
- package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -73
- package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -112
- package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -36
- package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -89
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -94
- package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -53
- package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -42
- package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -62
- package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -80
- package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -84
- package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -74
- package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -83
- package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -288
- package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -73
- package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -87
- package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -254
- package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -85
- package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -22
- package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -23
- package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -29
- package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -40
- package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -33
- package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -61
- package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -32
- package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -49
- package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -54
- package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -240
- package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -109
- package/glTF/2.0/Extensions/MSFT_lod.js +369 -369
- package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
- package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
- package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
- package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
- package/glTF/2.0/Extensions/index.d.ts +26 -26
- package/glTF/2.0/Extensions/index.js +26 -26
- package/glTF/2.0/glTFLoader.d.ts +405 -405
- package/glTF/2.0/glTFLoader.js +2366 -2366
- package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
- package/glTF/2.0/glTFLoaderExtension.js +1 -1
- package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -211
- package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
- package/glTF/2.0/index.d.ts +4 -4
- package/glTF/2.0/index.js +4 -4
- package/glTF/glTFFileLoader.d.ts +433 -433
- package/glTF/glTFFileLoader.js +927 -927
- package/glTF/glTFValidation.d.ts +29 -29
- package/glTF/glTFValidation.js +123 -123
- package/glTF/index.d.ts +5 -5
- package/glTF/index.js +5 -5
- package/index.d.ts +3 -3
- package/index.js +3 -3
- package/legacy/legacy-glTF.d.ts +2 -2
- package/legacy/legacy-glTF.js +18 -18
- package/legacy/legacy-glTF1.d.ts +2 -2
- package/legacy/legacy-glTF1.js +14 -14
- package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
- package/legacy/legacy-glTF1FileLoader.js +3 -3
- package/legacy/legacy-glTF2.d.ts +2 -2
- package/legacy/legacy-glTF2.js +32 -32
- package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
- package/legacy/legacy-glTF2FileLoader.js +3 -3
- package/legacy/legacy-glTFFileLoader.d.ts +3 -3
- package/legacy/legacy-glTFFileLoader.js +4 -4
- package/legacy/legacy-objFileLoader.d.ts +1 -1
- package/legacy/legacy-objFileLoader.js +12 -12
- package/legacy/legacy-stlFileLoader.d.ts +1 -1
- package/legacy/legacy-stlFileLoader.js +12 -12
- package/legacy/legacy.d.ts +6 -6
- package/legacy/legacy.js +7 -7
- package/package.json +3 -3
package/OBJ/objFileLoader.js
CHANGED
@@ -1,297 +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
|
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
|
-
}
|
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
|
+
}
|
297
297
|
//# sourceMappingURL=objFileLoader.js.map
|