@babylonjs/loaders 5.0.0-alpha.5 → 5.0.0-alpha.53

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. package/OBJ/index.d.ts +2 -0
  2. package/OBJ/index.js +4 -2
  3. package/OBJ/index.js.map +1 -1
  4. package/OBJ/mtlFileLoader.d.ts +4 -2
  5. package/OBJ/mtlFileLoader.js +7 -6
  6. package/OBJ/mtlFileLoader.js.map +1 -1
  7. package/OBJ/objFileLoader.d.ts +17 -78
  8. package/OBJ/objFileLoader.js +55 -619
  9. package/OBJ/objFileLoader.js.map +1 -1
  10. package/OBJ/objLoadingOptions.d.ts +43 -0
  11. package/OBJ/objLoadingOptions.js +2 -0
  12. package/OBJ/objLoadingOptions.js.map +1 -0
  13. package/OBJ/solidParser.d.ts +153 -0
  14. package/OBJ/solidParser.js +700 -0
  15. package/OBJ/solidParser.js.map +1 -0
  16. package/STL/index.js +1 -1
  17. package/STL/stlFileLoader.d.ts +6 -0
  18. package/STL/stlFileLoader.js +38 -11
  19. package/STL/stlFileLoader.js.map +1 -1
  20. package/glTF/1.0/glTFBinaryExtension.js +4 -4
  21. package/glTF/1.0/glTFLoader.d.ts +4 -4
  22. package/glTF/1.0/glTFLoader.js +60 -55
  23. package/glTF/1.0/glTFLoader.js.map +1 -1
  24. package/glTF/1.0/glTFLoaderInterfaces.d.ts +3 -1
  25. package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -1
  26. package/glTF/1.0/glTFLoaderUtils.js +6 -6
  27. package/glTF/1.0/glTFMaterialsCommonExtension.js +12 -12
  28. package/glTF/1.0/index.js +5 -5
  29. package/glTF/2.0/Extensions/EXT_lights_image_based.js +8 -5
  30. package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -1
  31. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +3 -3
  32. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -1
  33. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +1 -5
  34. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +5 -21
  35. package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -1
  36. package/glTF/2.0/Extensions/EXT_texture_webp.js +2 -2
  37. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -1
  38. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +1 -1
  39. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +8 -4
  40. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -1
  41. package/glTF/2.0/Extensions/KHR_lights_punctual.js +9 -8
  42. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -1
  43. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +2 -2
  44. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +1 -2
  45. package/glTF/2.0/Extensions/KHR_materials_ior.js +3 -4
  46. package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -1
  47. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +3 -4
  48. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -1
  49. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +0 -1
  50. package/glTF/2.0/Extensions/KHR_materials_sheen.js +3 -4
  51. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -1
  52. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +1 -2
  53. package/glTF/2.0/Extensions/KHR_materials_specular.js +12 -6
  54. package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -1
  55. package/glTF/2.0/Extensions/KHR_materials_translucency.js +6 -6
  56. package/glTF/2.0/Extensions/KHR_materials_translucency.js.map +1 -1
  57. package/glTF/2.0/Extensions/KHR_materials_transmission.js +92 -84
  58. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
  59. package/glTF/2.0/Extensions/KHR_materials_unlit.js +3 -3
  60. package/glTF/2.0/Extensions/KHR_materials_variants.js +86 -22
  61. package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -1
  62. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +31 -0
  63. package/glTF/2.0/Extensions/KHR_materials_volume.js +79 -0
  64. package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -0
  65. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +1 -1
  66. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +1 -2
  67. package/glTF/2.0/Extensions/KHR_texture_basisu.js +3 -4
  68. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -1
  69. package/glTF/2.0/Extensions/KHR_texture_transform.js +2 -2
  70. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +2 -2
  71. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +3 -3
  72. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -1
  73. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +6 -7
  74. package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -1
  75. package/glTF/2.0/Extensions/MSFT_lod.d.ts +2 -1
  76. package/glTF/2.0/Extensions/MSFT_lod.js +48 -8
  77. package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -1
  78. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +2 -2
  79. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +2 -2
  80. package/glTF/2.0/Extensions/index.d.ts +1 -0
  81. package/glTF/2.0/Extensions/index.js +25 -24
  82. package/glTF/2.0/Extensions/index.js.map +1 -1
  83. package/glTF/2.0/glTFLoader.d.ts +5 -10
  84. package/glTF/2.0/glTFLoader.js +125 -92
  85. package/glTF/2.0/glTFLoader.js.map +1 -1
  86. package/glTF/2.0/glTFLoaderInterfaces.d.ts +4 -2
  87. package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -1
  88. package/glTF/2.0/index.d.ts +1 -1
  89. package/glTF/2.0/index.js +4 -4
  90. package/glTF/2.0/index.js.map +1 -1
  91. package/glTF/glTFFileLoader.d.ts +15 -9
  92. package/glTF/glTFFileLoader.js +62 -76
  93. package/glTF/glTFFileLoader.js.map +1 -1
  94. package/glTF/glTFValidation.js +1 -1
  95. package/glTF/glTFValidation.js.map +1 -1
  96. package/glTF/index.d.ts +2 -2
  97. package/glTF/index.js +4 -4
  98. package/glTF/index.js.map +1 -1
  99. package/index.d.ts +3 -3
  100. package/index.js +3 -3
  101. package/index.js.map +1 -1
  102. package/legacy/legacy-glTF.js +4 -4
  103. package/legacy/legacy-glTF1.d.ts +1 -1
  104. package/legacy/legacy-glTF1.js +1 -1
  105. package/legacy/legacy-glTF1.js.map +1 -1
  106. package/legacy/legacy-glTF1FileLoader.js +2 -2
  107. package/legacy/legacy-glTF2.d.ts +1 -1
  108. package/legacy/legacy-glTF2.js +3 -3
  109. package/legacy/legacy-glTF2.js.map +1 -1
  110. package/legacy/legacy-glTF2FileLoader.js +2 -2
  111. package/legacy/legacy-glTFFileLoader.js +3 -3
  112. package/legacy/legacy-objFileLoader.d.ts +1 -1
  113. package/legacy/legacy-objFileLoader.js +2 -2
  114. package/legacy/legacy-objFileLoader.js.map +1 -1
  115. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  116. package/legacy/legacy-stlFileLoader.js +2 -2
  117. package/legacy/legacy-stlFileLoader.js.map +1 -1
  118. package/legacy/legacy.js +6 -6
  119. package/package.json +13 -4
@@ -1,12 +1,9 @@
1
- import { Vector3, Vector2 } from "@babylonjs/core/Maths/math.vector";
2
- import { Color4 } from '@babylonjs/core/Maths/math.color';
3
- import { Tools } from "@babylonjs/core/Misc/tools";
4
- import { VertexData } from "@babylonjs/core/Meshes/mesh.vertexData";
5
- import { Geometry } from "@babylonjs/core/Meshes/geometry";
6
- import { Mesh } from "@babylonjs/core/Meshes/mesh";
7
- import { SceneLoader } from "@babylonjs/core/Loading/sceneLoader";
8
- import { AssetContainer } from "@babylonjs/core/assetContainer";
9
- import { MTLFileLoader } from './mtlFileLoader';
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";
10
7
  /**
11
8
  * OBJ file type loader.
12
9
  * This is a babylon scene loader plugin.
@@ -15,9 +12,9 @@ var OBJFileLoader = /** @class */ (function () {
15
12
  /**
16
13
  * Creates loader for .OBJ files
17
14
  *
18
- * @param meshLoadOptions options for loading and parsing OBJ/MTL files.
15
+ * @param loadingOptions options for loading and parsing OBJ/MTL files.
19
16
  */
20
- function OBJFileLoader(meshLoadOptions) {
17
+ function OBJFileLoader(loadingOptions) {
21
18
  /**
22
19
  * Defines the name of the plugin.
23
20
  */
@@ -26,41 +23,8 @@ var OBJFileLoader = /** @class */ (function () {
26
23
  * Defines the extension the plugin is able to load.
27
24
  */
28
25
  this.extensions = ".obj";
29
- /** @hidden */
30
- this.obj = /^o/;
31
- /** @hidden */
32
- this.group = /^g/;
33
- /** @hidden */
34
- this.mtllib = /^mtllib /;
35
- /** @hidden */
36
- this.usemtl = /^usemtl /;
37
- /** @hidden */
38
- this.smooth = /^s /;
39
- /** @hidden */
40
- this.vertexPattern = /v(\s+[\d|\.|\+|\-|e|E]+){3,7}/;
41
- // vn float float float
42
- /** @hidden */
43
- this.normalPattern = /vn(\s+[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
44
- // vt float float
45
- /** @hidden */
46
- this.uvPattern = /vt(\s+[\d|\.|\+|\-|e|E]+)( +[\d|\.|\+|\-|e|E]+)/;
47
- // f vertex vertex vertex ...
48
- /** @hidden */
49
- this.facePattern1 = /f\s+(([\d]{1,}[\s]?){3,})+/;
50
- // f vertex/uvs vertex/uvs vertex/uvs ...
51
- /** @hidden */
52
- this.facePattern2 = /f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
53
- // f vertex/uvs/normal vertex/uvs/normal vertex/uvs/normal ...
54
- /** @hidden */
55
- this.facePattern3 = /f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
56
- // f vertex//normal vertex//normal vertex//normal ...
57
- /** @hidden */
58
- this.facePattern4 = /f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/;
59
- // f -vertex/-uvs/-normal -vertex/-uvs/-normal -vertex/-uvs/-normal ...
60
- /** @hidden */
61
- this.facePattern5 = /f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/;
62
- this._forAssetContainer = false;
63
- this._meshLoadOptions = meshLoadOptions || OBJFileLoader.currentMeshLoadOptions;
26
+ this._assetContainer = null;
27
+ this._loadingOptions = loadingOptions || OBJFileLoader.DefaultLoadingOptions;
64
28
  }
65
29
  Object.defineProperty(OBJFileLoader, "INVERT_TEXTURE_Y", {
66
30
  /**
@@ -75,17 +39,18 @@ var OBJFileLoader = /** @class */ (function () {
75
39
  enumerable: false,
76
40
  configurable: true
77
41
  });
78
- Object.defineProperty(OBJFileLoader, "currentMeshLoadOptions", {
42
+ Object.defineProperty(OBJFileLoader, "DefaultLoadingOptions", {
79
43
  get: function () {
80
44
  return {
81
- ComputeNormals: OBJFileLoader.COMPUTE_NORMALS,
82
- ImportVertexColors: OBJFileLoader.IMPORT_VERTEX_COLORS,
83
- InvertY: OBJFileLoader.INVERT_Y,
84
- InvertTextureY: OBJFileLoader.INVERT_TEXTURE_Y,
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,
85
50
  UVScaling: OBJFileLoader.UV_SCALING,
86
- MaterialLoadingFailsSilently: OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY,
87
- OptimizeWithUV: OBJFileLoader.OPTIMIZE_WITH_UV,
88
- SkipMaterials: OBJFileLoader.SKIP_MATERIALS
51
+ materialLoadingFailsSilently: OBJFileLoader.MATERIAL_LOADING_FAILS_SILENTLY,
52
+ optimizeWithUV: OBJFileLoader.OPTIMIZE_WITH_UV,
53
+ skipMaterials: OBJFileLoader.SKIP_MATERIALS
89
54
  };
90
55
  },
91
56
  enumerable: false,
@@ -98,13 +63,12 @@ var OBJFileLoader = /** @class */ (function () {
98
63
  * In consequence it is impossible to get material information in your HTML file
99
64
  *
100
65
  * @param url The URL of the MTL file
101
- * @param rootUrl
66
+ * @param rootUrl defines where to load data from
102
67
  * @param onSuccess Callback function to be called when the MTL file is loaded
103
- * @private
104
68
  */
105
69
  OBJFileLoader.prototype._loadMTL = function (url, rootUrl, onSuccess, onFailure) {
106
70
  //The complete path to the mtl file
107
- var pathOfFile = Tools.BaseUrl + rootUrl + url;
71
+ var pathOfFile = rootUrl + url;
108
72
  // Loads through the babylon tools to allow fileInput search.
109
73
  Tools.LoadFile(pathOfFile, onSuccess, undefined, undefined, false, function (request, exception) {
110
74
  onFailure(pathOfFile, exception);
@@ -115,7 +79,7 @@ var OBJFileLoader = /** @class */ (function () {
115
79
  * @returns the created plugin
116
80
  */
117
81
  OBJFileLoader.prototype.createPlugin = function () {
118
- return new OBJFileLoader(OBJFileLoader.currentMeshLoadOptions);
82
+ return new OBJFileLoader(OBJFileLoader.DefaultLoadingOptions);
119
83
  };
120
84
  /**
121
85
  * If the data string can be loaded directly.
@@ -176,9 +140,9 @@ var OBJFileLoader = /** @class */ (function () {
176
140
  */
177
141
  OBJFileLoader.prototype.loadAssetContainerAsync = function (scene, data, rootUrl, onProgress, fileName) {
178
142
  var _this = this;
179
- this._forAssetContainer = true;
143
+ var container = new AssetContainer(scene);
144
+ this._assetContainer = container;
180
145
  return this.importMeshAsync(null, scene, data, rootUrl).then(function (result) {
181
- var container = new AssetContainer(scene);
182
146
  result.meshes.forEach(function (mesh) { return container.meshes.push(mesh); });
183
147
  result.meshes.forEach(function (mesh) {
184
148
  var material = mesh.material;
@@ -196,10 +160,10 @@ var OBJFileLoader = /** @class */ (function () {
196
160
  }
197
161
  }
198
162
  });
199
- _this._forAssetContainer = false;
163
+ _this._assetContainer = null;
200
164
  return container;
201
165
  }).catch(function (ex) {
202
- _this._forAssetContainer = false;
166
+ _this._assetContainer = null;
203
167
  throw ex;
204
168
  });
205
169
  };
@@ -207,572 +171,33 @@ var OBJFileLoader = /** @class */ (function () {
207
171
  * Read the OBJ file and create an Array of meshes.
208
172
  * Each mesh contains all information given by the OBJ and the MTL file.
209
173
  * i.e. vertices positions and indices, optional normals values, optional UV values, optional material
210
- *
211
- * @param meshesNames
212
- * @param scene Scene The scene where are displayed the data
213
- * @param data String The content of the obj file
214
- * @param rootUrl String The path to the folder
215
- * @returns Array<AbstractMesh>
216
- * @private
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
217
179
  */
218
180
  OBJFileLoader.prototype._parseSolid = function (meshesNames, scene, data, rootUrl) {
219
181
  var _this = this;
220
- var positions = []; //values for the positions of vertices
221
- var normals = []; //Values for the normals
222
- var uvs = []; //Values for the textures
223
- var colors = [];
224
- var meshesFromObj = []; //[mesh] Contains all the obj meshes
225
- var handledMesh; //The current mesh of meshes array
226
- var indicesForBabylon = []; //The list of indices for VertexData
227
- var wrappedPositionForBabylon = []; //The list of position in vectors
228
- var wrappedUvsForBabylon = []; //Array with all value of uvs to match with the indices
229
- var wrappedColorsForBabylon = []; // Array with all color values to match with the indices
230
- var wrappedNormalsForBabylon = []; //Array with all value of normals to match with the indices
231
- var tuplePosNorm = []; //Create a tuple with indice of Position, Normal, UV [pos, norm, uvs]
232
- var curPositionInIndices = 0;
233
- var hasMeshes = false; //Meshes are defined in the file
234
- var unwrappedPositionsForBabylon = []; //Value of positionForBabylon w/o Vector3() [x,y,z]
235
- var unwrappedColorsForBabylon = []; // Value of colorForBabylon w/o Color4() [r,g,b,a]
236
- var unwrappedNormalsForBabylon = []; //Value of normalsForBabylon w/o Vector3() [x,y,z]
237
- var unwrappedUVForBabylon = []; //Value of uvsForBabylon w/o Vector3() [x,y,z]
238
- var triangles = []; //Indices from new triangles coming from polygons
239
- var materialNameFromObj = ""; //The name of the current material
240
182
  var fileToLoad = ""; //The name of the mtlFile to load
241
183
  var materialsFromMTLFile = new MTLFileLoader();
242
- var objMeshName = ""; //The name of the current obj mesh
243
- var increment = 1; //Id for meshes created by the multimaterial
244
- var isFirstMaterial = true;
245
- var grayColor = new Color4(0.5, 0.5, 0.5, 1);
246
- /**
247
- * Search for obj in the given array.
248
- * This function is called to check if a couple of data already exists in an array.
249
- *
250
- * If found, returns the index of the founded tuple index. Returns -1 if not found
251
- * @param arr Array<{ normals: Array<number>, idx: Array<number> }>
252
- * @param obj Array<number>
253
- * @returns {boolean}
254
- */
255
- var isInArray = function (arr, obj) {
256
- if (!arr[obj[0]]) {
257
- arr[obj[0]] = { normals: [], idx: [] };
258
- }
259
- var idx = arr[obj[0]].normals.indexOf(obj[1]);
260
- return idx === -1 ? -1 : arr[obj[0]].idx[idx];
261
- };
262
- var isInArrayUV = function (arr, obj) {
263
- if (!arr[obj[0]]) {
264
- arr[obj[0]] = { normals: [], idx: [], uv: [] };
265
- }
266
- var idx = arr[obj[0]].normals.indexOf(obj[1]);
267
- if (idx != 1 && (obj[2] === arr[obj[0]].uv[idx])) {
268
- return arr[obj[0]].idx[idx];
269
- }
270
- return -1;
271
- };
272
- /**
273
- * This function set the data for each triangle.
274
- * Data are position, normals and uvs
275
- * If a tuple of (position, normal) is not set, add the data into the corresponding array
276
- * If the tuple already exist, add only their indice
277
- *
278
- * @param indicePositionFromObj Integer The index in positions array
279
- * @param indiceUvsFromObj Integer The index in uvs array
280
- * @param indiceNormalFromObj Integer The index in normals array
281
- * @param positionVectorFromOBJ Vector3 The value of position at index objIndice
282
- * @param textureVectorFromOBJ Vector3 The value of uvs
283
- * @param normalsVectorFromOBJ Vector3 The value of normals at index objNormale
284
- */
285
- var setData = function (indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positionVectorFromOBJ, textureVectorFromOBJ, normalsVectorFromOBJ, positionColorsFromOBJ) {
286
- //Check if this tuple already exists in the list of tuples
287
- var _index;
288
- if (_this._meshLoadOptions.OptimizeWithUV) {
289
- _index = isInArrayUV(tuplePosNorm, [
290
- indicePositionFromObj,
291
- indiceNormalFromObj,
292
- indiceUvsFromObj
293
- ]);
294
- }
295
- else {
296
- _index = isInArray(tuplePosNorm, [
297
- indicePositionFromObj,
298
- indiceNormalFromObj
299
- ]);
300
- }
301
- //If it not exists
302
- if (_index === -1) {
303
- //Add an new indice.
304
- //The array of indices is only an array with his length equal to the number of triangles - 1.
305
- //We add vertices data in this order
306
- indicesForBabylon.push(wrappedPositionForBabylon.length);
307
- //Push the position of vertice for Babylon
308
- //Each element is a Vector3(x,y,z)
309
- wrappedPositionForBabylon.push(positionVectorFromOBJ);
310
- //Push the uvs for Babylon
311
- //Each element is a Vector3(u,v)
312
- wrappedUvsForBabylon.push(textureVectorFromOBJ);
313
- //Push the normals for Babylon
314
- //Each element is a Vector3(x,y,z)
315
- wrappedNormalsForBabylon.push(normalsVectorFromOBJ);
316
- if (positionColorsFromOBJ !== undefined) {
317
- //Push the colors for Babylon
318
- //Each element is a BABYLON.Color4(r,g,b,a)
319
- wrappedColorsForBabylon.push(positionColorsFromOBJ);
320
- }
321
- //Add the tuple in the comparison list
322
- tuplePosNorm[indicePositionFromObj].normals.push(indiceNormalFromObj);
323
- tuplePosNorm[indicePositionFromObj].idx.push(curPositionInIndices++);
324
- if (_this._meshLoadOptions.OptimizeWithUV) {
325
- tuplePosNorm[indicePositionFromObj].uv.push(indiceUvsFromObj);
326
- }
327
- }
328
- else {
329
- //The tuple already exists
330
- //Add the index of the already existing tuple
331
- //At this index we can get the value of position, normal, color and uvs of vertex
332
- indicesForBabylon.push(_index);
333
- }
334
- };
335
- /**
336
- * Transform Vector() and BABYLON.Color() objects into numbers in an array
337
- */
338
- var unwrapData = function () {
339
- //Every array has the same length
340
- for (var l = 0; l < wrappedPositionForBabylon.length; l++) {
341
- //Push the x, y, z values of each element in the unwrapped array
342
- unwrappedPositionsForBabylon.push(wrappedPositionForBabylon[l].x, wrappedPositionForBabylon[l].y, wrappedPositionForBabylon[l].z);
343
- unwrappedNormalsForBabylon.push(wrappedNormalsForBabylon[l].x, wrappedNormalsForBabylon[l].y, wrappedNormalsForBabylon[l].z);
344
- unwrappedUVForBabylon.push(wrappedUvsForBabylon[l].x, wrappedUvsForBabylon[l].y); //z is an optional value not supported by BABYLON
345
- if (_this._meshLoadOptions.ImportVertexColors === true) {
346
- //Push the r, g, b, a values of each element in the unwrapped array
347
- unwrappedColorsForBabylon.push(wrappedColorsForBabylon[l].r, wrappedColorsForBabylon[l].g, wrappedColorsForBabylon[l].b, wrappedColorsForBabylon[l].a);
348
- }
349
- }
350
- // Reset arrays for the next new meshes
351
- wrappedPositionForBabylon = [];
352
- wrappedNormalsForBabylon = [];
353
- wrappedUvsForBabylon = [];
354
- wrappedColorsForBabylon = [];
355
- tuplePosNorm = [];
356
- curPositionInIndices = 0;
357
- };
358
- /**
359
- * Create triangles from polygons
360
- * It is important to notice that a triangle is a polygon
361
- * We get 5 patterns of face defined in OBJ File :
362
- * facePattern1 = ["1","2","3","4","5","6"]
363
- * facePattern2 = ["1/1","2/2","3/3","4/4","5/5","6/6"]
364
- * facePattern3 = ["1/1/1","2/2/2","3/3/3","4/4/4","5/5/5","6/6/6"]
365
- * facePattern4 = ["1//1","2//2","3//3","4//4","5//5","6//6"]
366
- * facePattern5 = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-4/-4/-4","-5/-5/-5","-6/-6/-6"]
367
- * Each pattern is divided by the same method
368
- * @param face Array[String] The indices of elements
369
- * @param v Integer The variable to increment
370
- */
371
- var getTriangles = function (faces, v) {
372
- //Work for each element of the array
373
- for (var faceIndex = v; faceIndex < faces.length - 1; faceIndex++) {
374
- //Add on the triangle variable the indexes to obtain triangles
375
- triangles.push(faces[0], faces[faceIndex], faces[faceIndex + 1]);
376
- }
377
- //Result obtained after 2 iterations:
378
- //Pattern1 => triangle = ["1","2","3","1","3","4"];
379
- //Pattern2 => triangle = ["1/1","2/2","3/3","1/1","3/3","4/4"];
380
- //Pattern3 => triangle = ["1/1/1","2/2/2","3/3/3","1/1/1","3/3/3","4/4/4"];
381
- //Pattern4 => triangle = ["1//1","2//2","3//3","1//1","3//3","4//4"];
382
- //Pattern5 => triangle = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-1/-1/-1","-3/-3/-3","-4/-4/-4"];
383
- };
384
- /**
385
- * Create triangles and push the data for each polygon for the pattern 1
386
- * In this pattern we get vertice positions
387
- * @param face
388
- * @param v
389
- */
390
- var setDataForCurrentFaceWithPattern1 = function (face, v) {
391
- //Get the indices of triangles for each polygon
392
- getTriangles(face, v);
393
- //For each element in the triangles array.
394
- //This var could contains 1 to an infinity of triangles
395
- for (var k = 0; k < triangles.length; k++) {
396
- // Set position indice
397
- var indicePositionFromObj = parseInt(triangles[k]) - 1;
398
- setData(indicePositionFromObj, 0, 0, //In the pattern 1, normals and uvs are not defined
399
- positions[indicePositionFromObj], //Get the vectors data
400
- Vector2.Zero(), Vector3.Up(), //Create default vectors
401
- _this._meshLoadOptions.ImportVertexColors === true ? colors[indicePositionFromObj] : undefined);
402
- }
403
- //Reset variable for the next line
404
- triangles = [];
405
- };
406
- /**
407
- * Create triangles and push the data for each polygon for the pattern 2
408
- * In this pattern we get vertice positions and uvsu
409
- * @param face
410
- * @param v
411
- */
412
- var setDataForCurrentFaceWithPattern2 = function (face, v) {
413
- //Get the indices of triangles for each polygon
414
- getTriangles(face, v);
415
- for (var k = 0; k < triangles.length; k++) {
416
- //triangle[k] = "1/1"
417
- //Split the data for getting position and uv
418
- var point = triangles[k].split("/"); // ["1", "1"]
419
- //Set position indice
420
- var indicePositionFromObj = parseInt(point[0]) - 1;
421
- //Set uv indice
422
- var indiceUvsFromObj = parseInt(point[1]) - 1;
423
- setData(indicePositionFromObj, indiceUvsFromObj, 0, //Default value for normals
424
- positions[indicePositionFromObj], //Get the values for each element
425
- uvs[indiceUvsFromObj], Vector3.Up(), //Default value for normals
426
- _this._meshLoadOptions.ImportVertexColors === true ? colors[indicePositionFromObj] : undefined);
427
- }
428
- //Reset variable for the next line
429
- triangles = [];
430
- };
431
- /**
432
- * Create triangles and push the data for each polygon for the pattern 3
433
- * In this pattern we get vertice positions, uvs and normals
434
- * @param face
435
- * @param v
436
- */
437
- var setDataForCurrentFaceWithPattern3 = function (face, v) {
438
- //Get the indices of triangles for each polygon
439
- getTriangles(face, v);
440
- for (var k = 0; k < triangles.length; k++) {
441
- //triangle[k] = "1/1/1"
442
- //Split the data for getting position, uv, and normals
443
- var point = triangles[k].split("/"); // ["1", "1", "1"]
444
- // Set position indice
445
- var indicePositionFromObj = parseInt(point[0]) - 1;
446
- // Set uv indice
447
- var indiceUvsFromObj = parseInt(point[1]) - 1;
448
- // Set normal indice
449
- var indiceNormalFromObj = parseInt(point[2]) - 1;
450
- setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positions[indicePositionFromObj], uvs[indiceUvsFromObj], normals[indiceNormalFromObj] //Set the vector for each component
451
- );
452
- }
453
- //Reset variable for the next line
454
- triangles = [];
455
- };
456
- /**
457
- * Create triangles and push the data for each polygon for the pattern 4
458
- * In this pattern we get vertice positions and normals
459
- * @param face
460
- * @param v
461
- */
462
- var setDataForCurrentFaceWithPattern4 = function (face, v) {
463
- getTriangles(face, v);
464
- for (var k = 0; k < triangles.length; k++) {
465
- //triangle[k] = "1//1"
466
- //Split the data for getting position and normals
467
- var point = triangles[k].split("//"); // ["1", "1"]
468
- // We check indices, and normals
469
- var indicePositionFromObj = parseInt(point[0]) - 1;
470
- var indiceNormalFromObj = parseInt(point[1]) - 1;
471
- setData(indicePositionFromObj, 1, //Default value for uv
472
- indiceNormalFromObj, positions[indicePositionFromObj], //Get each vector of data
473
- Vector2.Zero(), normals[indiceNormalFromObj], _this._meshLoadOptions.ImportVertexColors === true ? colors[indicePositionFromObj] : undefined);
474
- }
475
- //Reset variable for the next line
476
- triangles = [];
477
- };
478
- /**
479
- * Create triangles and push the data for each polygon for the pattern 3
480
- * In this pattern we get vertice positions, uvs and normals
481
- * @param face
482
- * @param v
483
- */
484
- var setDataForCurrentFaceWithPattern5 = function (face, v) {
485
- //Get the indices of triangles for each polygon
486
- getTriangles(face, v);
487
- for (var k = 0; k < triangles.length; k++) {
488
- //triangle[k] = "-1/-1/-1"
489
- //Split the data for getting position, uv, and normals
490
- var point = triangles[k].split("/"); // ["-1", "-1", "-1"]
491
- // Set position indice
492
- var indicePositionFromObj = positions.length + parseInt(point[0]);
493
- // Set uv indice
494
- var indiceUvsFromObj = uvs.length + parseInt(point[1]);
495
- // Set normal indice
496
- var indiceNormalFromObj = normals.length + parseInt(point[2]);
497
- setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positions[indicePositionFromObj], uvs[indiceUvsFromObj], normals[indiceNormalFromObj], //Set the vector for each component
498
- _this._meshLoadOptions.ImportVertexColors === true ? colors[indicePositionFromObj] : undefined);
499
- }
500
- //Reset variable for the next line
501
- triangles = [];
502
- };
503
- var addPreviousObjMesh = function () {
504
- //Check if it is not the first mesh. Otherwise we don't have data.
505
- if (meshesFromObj.length > 0) {
506
- //Get the previous mesh for applying the data about the faces
507
- //=> in obj file, faces definition append after the name of the mesh
508
- handledMesh = meshesFromObj[meshesFromObj.length - 1];
509
- //Set the data into Array for the mesh
510
- unwrapData();
511
- // Reverse tab. Otherwise face are displayed in the wrong sens
512
- indicesForBabylon.reverse();
513
- //Set the information for the mesh
514
- //Slice the array to avoid rewriting because of the fact this is the same var which be rewrited
515
- handledMesh.indices = indicesForBabylon.slice();
516
- handledMesh.positions = unwrappedPositionsForBabylon.slice();
517
- handledMesh.normals = unwrappedNormalsForBabylon.slice();
518
- handledMesh.uvs = unwrappedUVForBabylon.slice();
519
- if (_this._meshLoadOptions.ImportVertexColors === true) {
520
- handledMesh.colors = unwrappedColorsForBabylon.slice();
521
- }
522
- //Reset the array for the next mesh
523
- indicesForBabylon = [];
524
- unwrappedPositionsForBabylon = [];
525
- unwrappedColorsForBabylon = [];
526
- unwrappedNormalsForBabylon = [];
527
- unwrappedUVForBabylon = [];
528
- }
529
- };
530
- //Main function
531
- //Split the file into lines
532
- var lines = data.split('\n');
533
- //Look at each line
534
- for (var i = 0; i < lines.length; i++) {
535
- var line = lines[i].trim().replace(/\s\s/g, " ");
536
- var result;
537
- //Comment or newLine
538
- if (line.length === 0 || line.charAt(0) === '#') {
539
- continue;
540
- //Get information about one position possible for the vertices
541
- }
542
- else if (this.vertexPattern.test(line)) {
543
- result = line.match(/[^ ]+/g); // match will return non-null due to passing regex pattern
544
- // Value of result with line: "v 1.0 2.0 3.0"
545
- // ["v", "1.0", "2.0", "3.0"]
546
- // Create a Vector3 with the position x, y, z
547
- positions.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
548
- if (this._meshLoadOptions.ImportVertexColors === true) {
549
- if (result.length >= 7) {
550
- // TODO: if these numbers are > 1 we can use Color4.FromInts(r,g,b,a)
551
- colors.push(new Color4(parseFloat(result[4]), parseFloat(result[5]), parseFloat(result[6]), (result.length === 7 || result[7] === undefined) ? 1 : parseFloat(result[7])));
552
- }
553
- else {
554
- // TODO: maybe push NULL and if all are NULL to skip (and remove grayColor var).
555
- colors.push(grayColor);
556
- }
557
- }
558
- }
559
- else if ((result = this.normalPattern.exec(line)) !== null) {
560
- //Create a Vector3 with the normals x, y, z
561
- //Value of result
562
- // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
563
- //Add the Vector in the list of normals
564
- normals.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
565
- }
566
- else if ((result = this.uvPattern.exec(line)) !== null) {
567
- //Create a Vector2 with the normals u, v
568
- //Value of result
569
- // ["vt 0.1 0.2 0.3", "0.1", "0.2"]
570
- //Add the Vector in the list of uvs
571
- uvs.push(new Vector2(parseFloat(result[1]) * OBJFileLoader.UV_SCALING.x, parseFloat(result[2]) * OBJFileLoader.UV_SCALING.y));
572
- //Identify patterns of faces
573
- //Face could be defined in different type of pattern
574
- }
575
- else if ((result = this.facePattern3.exec(line)) !== null) {
576
- //Value of result:
577
- //["f 1/1/1 2/2/2 3/3/3", "1/1/1 2/2/2 3/3/3"...]
578
- //Set the data for this face
579
- setDataForCurrentFaceWithPattern3(result[1].trim().split(" "), // ["1/1/1", "2/2/2", "3/3/3"]
580
- 1);
581
- }
582
- else if ((result = this.facePattern4.exec(line)) !== null) {
583
- //Value of result:
584
- //["f 1//1 2//2 3//3", "1//1 2//2 3//3"...]
585
- //Set the data for this face
586
- setDataForCurrentFaceWithPattern4(result[1].trim().split(" "), // ["1//1", "2//2", "3//3"]
587
- 1);
588
- }
589
- else if ((result = this.facePattern5.exec(line)) !== null) {
590
- //Value of result:
591
- //["f -1/-1/-1 -2/-2/-2 -3/-3/-3", "-1/-1/-1 -2/-2/-2 -3/-3/-3"...]
592
- //Set the data for this face
593
- setDataForCurrentFaceWithPattern5(result[1].trim().split(" "), // ["-1/-1/-1", "-2/-2/-2", "-3/-3/-3"]
594
- 1);
595
- }
596
- else if ((result = this.facePattern2.exec(line)) !== null) {
597
- //Value of result:
598
- //["f 1/1 2/2 3/3", "1/1 2/2 3/3"...]
599
- //Set the data for this face
600
- setDataForCurrentFaceWithPattern2(result[1].trim().split(" "), // ["1/1", "2/2", "3/3"]
601
- 1);
602
- }
603
- else if ((result = this.facePattern1.exec(line)) !== null) {
604
- //Value of result
605
- //["f 1 2 3", "1 2 3"...]
606
- //Set the data for this face
607
- setDataForCurrentFaceWithPattern1(result[1].trim().split(" "), // ["1", "2", "3"]
608
- 1);
609
- //Define a mesh or an object
610
- //Each time this keyword is analysed, create a new Object with all data for creating a babylonMesh
611
- }
612
- else if (this.group.test(line) || this.obj.test(line)) {
613
- //Create a new mesh corresponding to the name of the group.
614
- //Definition of the mesh
615
- var objMesh = {
616
- name: line.substring(2).trim(),
617
- indices: undefined,
618
- positions: undefined,
619
- normals: undefined,
620
- uvs: undefined,
621
- colors: undefined,
622
- materialName: ""
623
- };
624
- addPreviousObjMesh();
625
- //Push the last mesh created with only the name
626
- meshesFromObj.push(objMesh);
627
- //Set this variable to indicate that now meshesFromObj has objects defined inside
628
- hasMeshes = true;
629
- isFirstMaterial = true;
630
- increment = 1;
631
- //Keyword for applying a material
632
- }
633
- else if (this.usemtl.test(line)) {
634
- //Get the name of the material
635
- materialNameFromObj = line.substring(7).trim();
636
- //If this new material is in the same mesh
637
- if (!isFirstMaterial || !hasMeshes) {
638
- //Set the data for the previous mesh
639
- addPreviousObjMesh();
640
- //Create a new mesh
641
- var objMesh =
642
- //Set the name of the current obj mesh
643
- {
644
- name: (objMeshName || "mesh") + "_mm" + increment.toString(),
645
- indices: undefined,
646
- positions: undefined,
647
- normals: undefined,
648
- uvs: undefined,
649
- colors: undefined,
650
- materialName: materialNameFromObj
651
- };
652
- increment++;
653
- //If meshes are already defined
654
- meshesFromObj.push(objMesh);
655
- hasMeshes = true;
656
- }
657
- //Set the material name if the previous line define a mesh
658
- if (hasMeshes && isFirstMaterial) {
659
- //Set the material name to the previous mesh (1 material per mesh)
660
- meshesFromObj[meshesFromObj.length - 1].materialName = materialNameFromObj;
661
- isFirstMaterial = false;
662
- }
663
- //Keyword for loading the mtl file
664
- }
665
- else if (this.mtllib.test(line)) {
666
- //Get the name of mtl file
667
- fileToLoad = line.substring(7).trim();
668
- //Apply smoothing
669
- }
670
- else if (this.smooth.test(line)) {
671
- // smooth shading => apply smoothing
672
- //Today I don't know it work with babylon and with obj.
673
- //With the obj file an integer is set
674
- }
675
- else {
676
- //If there is another possibility
677
- console.log("Unhandled expression at line : " + line);
678
- }
679
- }
680
- //At the end of the file, add the last mesh into the meshesFromObj array
681
- if (hasMeshes) {
682
- //Set the data for the last mesh
683
- handledMesh = meshesFromObj[meshesFromObj.length - 1];
684
- //Reverse indices for displaying faces in the good sense
685
- indicesForBabylon.reverse();
686
- //Get the good array
687
- unwrapData();
688
- //Set array
689
- handledMesh.indices = indicesForBabylon;
690
- handledMesh.positions = unwrappedPositionsForBabylon;
691
- handledMesh.normals = unwrappedNormalsForBabylon;
692
- handledMesh.uvs = unwrappedUVForBabylon;
693
- if (this._meshLoadOptions.ImportVertexColors === true) {
694
- handledMesh.colors = unwrappedColorsForBabylon;
695
- }
696
- }
697
- //If any o or g keyword found, create a mesh with a random id
698
- if (!hasMeshes) {
699
- // reverse tab of indices
700
- indicesForBabylon.reverse();
701
- //Get positions normals uvs
702
- unwrapData();
703
- //Set data for one mesh
704
- meshesFromObj.push({
705
- name: Geometry.RandomId(),
706
- indices: indicesForBabylon,
707
- positions: unwrappedPositionsForBabylon,
708
- colors: unwrappedColorsForBabylon,
709
- normals: unwrappedNormalsForBabylon,
710
- uvs: unwrappedUVForBabylon,
711
- materialName: materialNameFromObj
712
- });
713
- }
714
- //Create a Mesh list
715
- var babylonMeshesArray = []; //The mesh for babylon
716
184
  var materialToUse = new Array();
717
- //Set data for each mesh
718
- for (var j = 0; j < meshesFromObj.length; j++) {
719
- //check meshesNames (stlFileLoader)
720
- if (meshesNames && meshesFromObj[j].name) {
721
- if (meshesNames instanceof Array) {
722
- if (meshesNames.indexOf(meshesFromObj[j].name) === -1) {
723
- continue;
724
- }
725
- }
726
- else {
727
- if (meshesFromObj[j].name !== meshesNames) {
728
- continue;
729
- }
730
- }
731
- }
732
- //Get the current mesh
733
- //Set the data with VertexBuffer for each mesh
734
- handledMesh = meshesFromObj[j];
735
- //Create a Mesh with the name of the obj mesh
736
- scene._blockEntityCollection = this._forAssetContainer;
737
- var babylonMesh = new Mesh(meshesFromObj[j].name, scene);
738
- scene._blockEntityCollection = false;
739
- //Push the name of the material to an array
740
- //This is indispensable for the importMesh function
741
- materialToUse.push(meshesFromObj[j].materialName);
742
- var vertexData = new VertexData(); //The container for the values
743
- //Set the data for the babylonMesh
744
- vertexData.uvs = handledMesh.uvs;
745
- vertexData.indices = handledMesh.indices;
746
- vertexData.positions = handledMesh.positions;
747
- if (this._meshLoadOptions.ComputeNormals === true) {
748
- var normals_1 = new Array();
749
- VertexData.ComputeNormals(handledMesh.positions, handledMesh.indices, normals_1);
750
- vertexData.normals = normals_1;
751
- }
752
- else {
753
- vertexData.normals = handledMesh.normals;
754
- }
755
- if (this._meshLoadOptions.ImportVertexColors === true) {
756
- vertexData.colors = handledMesh.colors;
757
- }
758
- //Set the data from the VertexBuffer to the current Mesh
759
- vertexData.applyToMesh(babylonMesh);
760
- if (this._meshLoadOptions.InvertY) {
761
- babylonMesh.scaling.y *= -1;
762
- }
763
- //Push the mesh into an array
764
- babylonMeshesArray.push(babylonMesh);
765
- }
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
766
192
  var mtlPromises = [];
767
- //load the materials
768
- //Check if we have a file to load
769
- if (fileToLoad !== "" && this._meshLoadOptions.SkipMaterials === false) {
193
+ // Check if we have a file to load
194
+ if (fileToLoad !== "" && !this._loadingOptions.skipMaterials) {
770
195
  //Load the file synchronously
771
196
  mtlPromises.push(new Promise(function (resolve, reject) {
772
197
  _this._loadMTL(fileToLoad, rootUrl, function (dataLoaded) {
773
198
  try {
774
199
  //Create materials thanks MTLLoader function
775
- materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl, _this._forAssetContainer);
200
+ materialsFromMTLFile.parseMTL(scene, dataLoaded, rootUrl, _this._assetContainer);
776
201
  //Look at each material loaded in the mtl file
777
202
  for (var n = 0; n < materialsFromMTLFile.materials.length; n++) {
778
203
  //Three variables to get all meshes with the same material
@@ -794,7 +219,13 @@ var OBJFileLoader = /** @class */ (function () {
794
219
  else {
795
220
  for (var o = 0; o < _indices.length; o++) {
796
221
  //Apply the material to the Mesh for each mesh with the material
797
- babylonMeshesArray[_indices[o]].material = materialsFromMTLFile.materials[n];
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
+ }
798
229
  }
799
230
  }
800
231
  }
@@ -802,7 +233,7 @@ var OBJFileLoader = /** @class */ (function () {
802
233
  }
803
234
  catch (e) {
804
235
  Tools.Warn("Error processing MTL file: '" + fileToLoad + "'");
805
- if (_this._meshLoadOptions.MaterialLoadingFailsSilently) {
236
+ if (_this._loadingOptions.materialLoadingFailsSilently) {
806
237
  resolve();
807
238
  }
808
239
  else {
@@ -811,7 +242,7 @@ var OBJFileLoader = /** @class */ (function () {
811
242
  }
812
243
  }, function (pathOfFile, exception) {
813
244
  Tools.Warn("Error downloading MTL file: '" + fileToLoad + "'");
814
- if (_this._meshLoadOptions.MaterialLoadingFailsSilently) {
245
+ if (_this._loadingOptions.materialLoadingFailsSilently) {
815
246
  resolve();
816
247
  }
817
248
  else {
@@ -841,6 +272,11 @@ var OBJFileLoader = /** @class */ (function () {
841
272
  * Compute the normals for the model, even if normals are present in the file.
842
273
  */
843
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;
844
280
  /**
845
281
  * Defines custom scaling of UV coordinates of loaded meshes.
846
282
  */