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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/OBJ/index.d.ts +4 -4
  2. package/OBJ/index.js +4 -4
  3. package/OBJ/mtlFileLoader.d.ts +41 -41
  4. package/OBJ/mtlFileLoader.js +232 -232
  5. package/OBJ/objFileLoader.d.ts +126 -126
  6. package/OBJ/objFileLoader.js +296 -296
  7. package/OBJ/objLoadingOptions.d.ts +43 -43
  8. package/OBJ/objLoadingOptions.js +1 -1
  9. package/OBJ/solidParser.d.ts +154 -154
  10. package/OBJ/solidParser.js +693 -693
  11. package/STL/index.d.ts +1 -1
  12. package/STL/index.js +1 -1
  13. package/STL/stlFileLoader.d.ts +64 -64
  14. package/STL/stlFileLoader.js +233 -233
  15. package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
  16. package/glTF/1.0/glTFBinaryExtension.js +60 -60
  17. package/glTF/1.0/glTFLoader.d.ts +136 -136
  18. package/glTF/1.0/glTFLoader.js +1815 -1815
  19. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
  20. package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
  21. package/glTF/1.0/glTFLoaderUtils.d.ts +62 -62
  22. package/glTF/1.0/glTFLoaderUtils.js +250 -250
  23. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
  24. package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -129
  25. package/glTF/1.0/index.d.ts +5 -5
  26. package/glTF/1.0/index.js +5 -5
  27. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -43
  28. package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -117
  29. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -36
  30. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -88
  31. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -34
  32. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -49
  33. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -29
  34. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -40
  35. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -50
  36. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -73
  37. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -39
  38. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -112
  39. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -36
  40. package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -89
  41. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -39
  42. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -94
  43. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -38
  44. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -53
  45. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -42
  46. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -62
  47. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -38
  48. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -80
  49. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -39
  50. package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -84
  51. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -38
  52. package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -74
  53. package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -39
  54. package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -83
  55. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -38
  56. package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -288
  57. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -38
  58. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -73
  59. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -87
  60. package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -254
  61. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -39
  62. package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -85
  63. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -22
  64. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -23
  65. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -29
  66. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -40
  67. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -33
  68. package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -61
  69. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -32
  70. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -49
  71. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -54
  72. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -240
  73. package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -109
  74. package/glTF/2.0/Extensions/MSFT_lod.js +369 -369
  75. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
  76. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
  77. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
  78. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
  79. package/glTF/2.0/Extensions/index.d.ts +26 -26
  80. package/glTF/2.0/Extensions/index.js +26 -26
  81. package/glTF/2.0/glTFLoader.d.ts +405 -405
  82. package/glTF/2.0/glTFLoader.js +2366 -2366
  83. package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
  84. package/glTF/2.0/glTFLoaderExtension.js +1 -1
  85. package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -211
  86. package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
  87. package/glTF/2.0/index.d.ts +4 -4
  88. package/glTF/2.0/index.js +4 -4
  89. package/glTF/glTFFileLoader.d.ts +433 -433
  90. package/glTF/glTFFileLoader.js +927 -927
  91. package/glTF/glTFValidation.d.ts +29 -29
  92. package/glTF/glTFValidation.js +123 -123
  93. package/glTF/index.d.ts +5 -5
  94. package/glTF/index.js +5 -5
  95. package/index.d.ts +3 -3
  96. package/index.js +3 -3
  97. package/legacy/legacy-glTF.d.ts +2 -2
  98. package/legacy/legacy-glTF.js +18 -18
  99. package/legacy/legacy-glTF1.d.ts +2 -2
  100. package/legacy/legacy-glTF1.js +14 -14
  101. package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
  102. package/legacy/legacy-glTF1FileLoader.js +3 -3
  103. package/legacy/legacy-glTF2.d.ts +2 -2
  104. package/legacy/legacy-glTF2.js +32 -32
  105. package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
  106. package/legacy/legacy-glTF2FileLoader.js +3 -3
  107. package/legacy/legacy-glTFFileLoader.d.ts +3 -3
  108. package/legacy/legacy-glTFFileLoader.js +4 -4
  109. package/legacy/legacy-objFileLoader.d.ts +1 -1
  110. package/legacy/legacy-objFileLoader.js +12 -12
  111. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  112. package/legacy/legacy-stlFileLoader.js +12 -12
  113. package/legacy/legacy.d.ts +6 -6
  114. package/legacy/legacy.js +7 -7
  115. package/package.json +3 -3
@@ -1,694 +1,694 @@
1
- import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
2
- import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial.js";
3
- import { Color3, Color4 } from "@babylonjs/core/Maths/math.color.js";
4
- import { Vector2, Vector3 } from "@babylonjs/core/Maths/math.vector.js";
5
- import { Geometry } from "@babylonjs/core/Meshes/geometry.js";
6
- import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
7
- import { VertexData } from "@babylonjs/core/Meshes/mesh.vertexData.js";
8
- /**
9
- * Class used to load mesh data from OBJ content
10
- */
11
- var SolidParser = /** @class */ (function () {
12
- /**
13
- * Creates a new SolidParser
14
- * @param materialToUse defines the array to fill with the list of materials to use (it will be filled by the parse function)
15
- * @param babylonMeshesArray defines the array to fill with the list of loaded meshes (it will be filled by the parse function)
16
- * @param loadingOptions defines the loading options to use
17
- */
18
- function SolidParser(materialToUse, babylonMeshesArray, loadingOptions) {
19
- this._positions = []; //values for the positions of vertices
20
- this._normals = []; //Values for the normals
21
- this._uvs = []; //Values for the textures
22
- this._colors = [];
23
- this._meshesFromObj = []; //[mesh] Contains all the obj meshes
24
- this._indicesForBabylon = []; //The list of indices for VertexData
25
- this._wrappedPositionForBabylon = []; //The list of position in vectors
26
- this._wrappedUvsForBabylon = []; //Array with all value of uvs to match with the indices
27
- this._wrappedColorsForBabylon = []; // Array with all color values to match with the indices
28
- this._wrappedNormalsForBabylon = []; //Array with all value of normals to match with the indices
29
- this._tuplePosNorm = []; //Create a tuple with indice of Position, Normal, UV [pos, norm, uvs]
30
- this._curPositionInIndices = 0;
31
- this._hasMeshes = false; //Meshes are defined in the file
32
- this._unwrappedPositionsForBabylon = []; //Value of positionForBabylon w/o Vector3() [x,y,z]
33
- this._unwrappedColorsForBabylon = []; // Value of colorForBabylon w/o Color4() [r,g,b,a]
34
- this._unwrappedNormalsForBabylon = []; //Value of normalsForBabylon w/o Vector3() [x,y,z]
35
- this._unwrappedUVForBabylon = []; //Value of uvsForBabylon w/o Vector3() [x,y,z]
36
- this._triangles = []; //Indices from new triangles coming from polygons
37
- this._materialNameFromObj = ""; //The name of the current material
38
- this._objMeshName = ""; //The name of the current obj mesh
39
- this._increment = 1; //Id for meshes created by the multimaterial
40
- this._isFirstMaterial = true;
41
- this._grayColor = new Color4(0.5, 0.5, 0.5, 1);
42
- this._materialToUse = materialToUse;
43
- this._babylonMeshesArray = babylonMeshesArray;
44
- this._loadingOptions = loadingOptions;
45
- }
46
- /**
47
- * Search for obj in the given array.
48
- * This function is called to check if a couple of data already exists in an array.
49
- *
50
- * If found, returns the index of the founded tuple index. Returns -1 if not found
51
- * @param arr Array<{ normals: Array<number>, idx: Array<number> }>
52
- * @param obj Array<number>
53
- * @returns {boolean}
54
- */
55
- SolidParser.prototype._isInArray = function (arr, obj) {
56
- if (!arr[obj[0]]) {
57
- arr[obj[0]] = { normals: [], idx: [] };
58
- }
59
- var idx = arr[obj[0]].normals.indexOf(obj[1]);
60
- return idx === -1 ? -1 : arr[obj[0]].idx[idx];
61
- };
62
- SolidParser.prototype._isInArrayUV = function (arr, obj) {
63
- if (!arr[obj[0]]) {
64
- arr[obj[0]] = { normals: [], idx: [], uv: [] };
65
- }
66
- var idx = arr[obj[0]].normals.indexOf(obj[1]);
67
- if (idx != 1 && obj[2] === arr[obj[0]].uv[idx]) {
68
- return arr[obj[0]].idx[idx];
69
- }
70
- return -1;
71
- };
72
- /**
73
- * This function set the data for each triangle.
74
- * Data are position, normals and uvs
75
- * If a tuple of (position, normal) is not set, add the data into the corresponding array
76
- * If the tuple already exist, add only their indice
77
- *
78
- * @param indicePositionFromObj Integer The index in positions array
79
- * @param indiceUvsFromObj Integer The index in uvs array
80
- * @param indiceNormalFromObj Integer The index in normals array
81
- * @param positionVectorFromOBJ Vector3 The value of position at index objIndice
82
- * @param textureVectorFromOBJ Vector3 The value of uvs
83
- * @param normalsVectorFromOBJ Vector3 The value of normals at index objNormale
84
- * @param positionColorsFromOBJ
85
- */
86
- SolidParser.prototype._setData = function (indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positionVectorFromOBJ, textureVectorFromOBJ, normalsVectorFromOBJ, positionColorsFromOBJ) {
87
- //Check if this tuple already exists in the list of tuples
88
- var _index;
89
- if (this._loadingOptions.optimizeWithUV) {
90
- _index = this._isInArrayUV(this._tuplePosNorm, [indicePositionFromObj, indiceNormalFromObj, indiceUvsFromObj]);
91
- }
92
- else {
93
- _index = this._isInArray(this._tuplePosNorm, [indicePositionFromObj, indiceNormalFromObj]);
94
- }
95
- //If it not exists
96
- if (_index === -1) {
97
- //Add an new indice.
98
- //The array of indices is only an array with his length equal to the number of triangles - 1.
99
- //We add vertices data in this order
100
- this._indicesForBabylon.push(this._wrappedPositionForBabylon.length);
101
- //Push the position of vertice for Babylon
102
- //Each element is a Vector3(x,y,z)
103
- this._wrappedPositionForBabylon.push(positionVectorFromOBJ);
104
- //Push the uvs for Babylon
105
- //Each element is a Vector3(u,v)
106
- this._wrappedUvsForBabylon.push(textureVectorFromOBJ);
107
- //Push the normals for Babylon
108
- //Each element is a Vector3(x,y,z)
109
- this._wrappedNormalsForBabylon.push(normalsVectorFromOBJ);
110
- if (positionColorsFromOBJ !== undefined) {
111
- //Push the colors for Babylon
112
- //Each element is a BABYLON.Color4(r,g,b,a)
113
- this._wrappedColorsForBabylon.push(positionColorsFromOBJ);
114
- }
115
- //Add the tuple in the comparison list
116
- this._tuplePosNorm[indicePositionFromObj].normals.push(indiceNormalFromObj);
117
- this._tuplePosNorm[indicePositionFromObj].idx.push(this._curPositionInIndices++);
118
- if (this._loadingOptions.optimizeWithUV) {
119
- this._tuplePosNorm[indicePositionFromObj].uv.push(indiceUvsFromObj);
120
- }
121
- }
122
- else {
123
- //The tuple already exists
124
- //Add the index of the already existing tuple
125
- //At this index we can get the value of position, normal, color and uvs of vertex
126
- this._indicesForBabylon.push(_index);
127
- }
128
- };
129
- /**
130
- * Transform Vector() and BABYLON.Color() objects into numbers in an array
131
- */
132
- SolidParser.prototype._unwrapData = function () {
133
- //Every array has the same length
134
- for (var l = 0; l < this._wrappedPositionForBabylon.length; l++) {
135
- //Push the x, y, z values of each element in the unwrapped array
136
- this._unwrappedPositionsForBabylon.push(this._wrappedPositionForBabylon[l].x, this._wrappedPositionForBabylon[l].y, this._wrappedPositionForBabylon[l].z);
137
- this._unwrappedNormalsForBabylon.push(this._wrappedNormalsForBabylon[l].x, this._wrappedNormalsForBabylon[l].y, this._wrappedNormalsForBabylon[l].z);
138
- this._unwrappedUVForBabylon.push(this._wrappedUvsForBabylon[l].x, this._wrappedUvsForBabylon[l].y); //z is an optional value not supported by BABYLON
139
- if (this._loadingOptions.importVertexColors) {
140
- //Push the r, g, b, a values of each element in the unwrapped array
141
- this._unwrappedColorsForBabylon.push(this._wrappedColorsForBabylon[l].r, this._wrappedColorsForBabylon[l].g, this._wrappedColorsForBabylon[l].b, this._wrappedColorsForBabylon[l].a);
142
- }
143
- }
144
- // Reset arrays for the next new meshes
145
- this._wrappedPositionForBabylon = [];
146
- this._wrappedNormalsForBabylon = [];
147
- this._wrappedUvsForBabylon = [];
148
- this._wrappedColorsForBabylon = [];
149
- this._tuplePosNorm = [];
150
- this._curPositionInIndices = 0;
151
- };
152
- /**
153
- * Create triangles from polygons
154
- * It is important to notice that a triangle is a polygon
155
- * We get 5 patterns of face defined in OBJ File :
156
- * facePattern1 = ["1","2","3","4","5","6"]
157
- * facePattern2 = ["1/1","2/2","3/3","4/4","5/5","6/6"]
158
- * facePattern3 = ["1/1/1","2/2/2","3/3/3","4/4/4","5/5/5","6/6/6"]
159
- * facePattern4 = ["1//1","2//2","3//3","4//4","5//5","6//6"]
160
- * facePattern5 = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-4/-4/-4","-5/-5/-5","-6/-6/-6"]
161
- * Each pattern is divided by the same method
162
- * @param faces Array[String] The indices of elements
163
- * @param v Integer The variable to increment
164
- */
165
- SolidParser.prototype._getTriangles = function (faces, v) {
166
- //Work for each element of the array
167
- for (var faceIndex = v; faceIndex < faces.length - 1; faceIndex++) {
168
- //Add on the triangle variable the indexes to obtain triangles
169
- this._triangles.push(faces[0], faces[faceIndex], faces[faceIndex + 1]);
170
- }
171
- //Result obtained after 2 iterations:
172
- //Pattern1 => triangle = ["1","2","3","1","3","4"];
173
- //Pattern2 => triangle = ["1/1","2/2","3/3","1/1","3/3","4/4"];
174
- //Pattern3 => triangle = ["1/1/1","2/2/2","3/3/3","1/1/1","3/3/3","4/4/4"];
175
- //Pattern4 => triangle = ["1//1","2//2","3//3","1//1","3//3","4//4"];
176
- //Pattern5 => triangle = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-1/-1/-1","-3/-3/-3","-4/-4/-4"];
177
- };
178
- /**
179
- * Create triangles and push the data for each polygon for the pattern 1
180
- * In this pattern we get vertice positions
181
- * @param face
182
- * @param v
183
- */
184
- SolidParser.prototype._setDataForCurrentFaceWithPattern1 = function (face, v) {
185
- //Get the indices of triangles for each polygon
186
- this._getTriangles(face, v);
187
- //For each element in the triangles array.
188
- //This var could contains 1 to an infinity of triangles
189
- for (var k = 0; k < this._triangles.length; k++) {
190
- // Set position indice
191
- var indicePositionFromObj = parseInt(this._triangles[k]) - 1;
192
- this._setData(indicePositionFromObj, 0, 0, // In the pattern 1, normals and uvs are not defined
193
- this._positions[indicePositionFromObj], // Get the vectors data
194
- Vector2.Zero(), Vector3.Up(), // Create default vectors
195
- this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
196
- }
197
- //Reset variable for the next line
198
- this._triangles = [];
199
- };
200
- /**
201
- * Create triangles and push the data for each polygon for the pattern 2
202
- * In this pattern we get vertice positions and uvsu
203
- * @param face
204
- * @param v
205
- */
206
- SolidParser.prototype._setDataForCurrentFaceWithPattern2 = function (face, v) {
207
- //Get the indices of triangles for each polygon
208
- this._getTriangles(face, v);
209
- for (var k = 0; k < this._triangles.length; k++) {
210
- //triangle[k] = "1/1"
211
- //Split the data for getting position and uv
212
- var point = this._triangles[k].split("/"); // ["1", "1"]
213
- //Set position indice
214
- var indicePositionFromObj = parseInt(point[0]) - 1;
215
- //Set uv indice
216
- var indiceUvsFromObj = parseInt(point[1]) - 1;
217
- this._setData(indicePositionFromObj, indiceUvsFromObj, 0, //Default value for normals
218
- this._positions[indicePositionFromObj], //Get the values for each element
219
- this._uvs[indiceUvsFromObj], Vector3.Up(), //Default value for normals
220
- this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
221
- }
222
- //Reset variable for the next line
223
- this._triangles = [];
224
- };
225
- /**
226
- * Create triangles and push the data for each polygon for the pattern 3
227
- * In this pattern we get vertice positions, uvs and normals
228
- * @param face
229
- * @param v
230
- */
231
- SolidParser.prototype._setDataForCurrentFaceWithPattern3 = function (face, v) {
232
- //Get the indices of triangles for each polygon
233
- this._getTriangles(face, v);
234
- for (var k = 0; k < this._triangles.length; k++) {
235
- //triangle[k] = "1/1/1"
236
- //Split the data for getting position, uv, and normals
237
- var point = this._triangles[k].split("/"); // ["1", "1", "1"]
238
- // Set position indice
239
- var indicePositionFromObj = parseInt(point[0]) - 1;
240
- // Set uv indice
241
- var indiceUvsFromObj = parseInt(point[1]) - 1;
242
- // Set normal indice
243
- var indiceNormalFromObj = parseInt(point[2]) - 1;
244
- this._setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, this._positions[indicePositionFromObj], this._uvs[indiceUvsFromObj], this._normals[indiceNormalFromObj] //Set the vector for each component
245
- );
246
- }
247
- //Reset variable for the next line
248
- this._triangles = [];
249
- };
250
- /**
251
- * Create triangles and push the data for each polygon for the pattern 4
252
- * In this pattern we get vertice positions and normals
253
- * @param face
254
- * @param v
255
- */
256
- SolidParser.prototype._setDataForCurrentFaceWithPattern4 = function (face, v) {
257
- this._getTriangles(face, v);
258
- for (var k = 0; k < this._triangles.length; k++) {
259
- //triangle[k] = "1//1"
260
- //Split the data for getting position and normals
261
- var point = this._triangles[k].split("//"); // ["1", "1"]
262
- // We check indices, and normals
263
- var indicePositionFromObj = parseInt(point[0]) - 1;
264
- var indiceNormalFromObj = parseInt(point[1]) - 1;
265
- this._setData(indicePositionFromObj, 1, //Default value for uv
266
- indiceNormalFromObj, this._positions[indicePositionFromObj], //Get each vector of data
267
- Vector2.Zero(), this._normals[indiceNormalFromObj], this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
268
- }
269
- //Reset variable for the next line
270
- this._triangles = [];
271
- };
272
- /*
273
- * Create triangles and push the data for each polygon for the pattern 3
274
- * In this pattern we get vertice positions, uvs and normals
275
- * @param face
276
- * @param v
277
- */
278
- SolidParser.prototype._setDataForCurrentFaceWithPattern5 = function (face, v) {
279
- //Get the indices of triangles for each polygon
280
- this._getTriangles(face, v);
281
- for (var k = 0; k < this._triangles.length; k++) {
282
- //triangle[k] = "-1/-1/-1"
283
- //Split the data for getting position, uv, and normals
284
- var point = this._triangles[k].split("/"); // ["-1", "-1", "-1"]
285
- // Set position indice
286
- var indicePositionFromObj = this._positions.length + parseInt(point[0]);
287
- // Set uv indice
288
- var indiceUvsFromObj = this._uvs.length + parseInt(point[1]);
289
- // Set normal indice
290
- var indiceNormalFromObj = this._normals.length + parseInt(point[2]);
291
- this._setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, this._positions[indicePositionFromObj], this._uvs[indiceUvsFromObj], this._normals[indiceNormalFromObj], //Set the vector for each component
292
- this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
293
- }
294
- //Reset variable for the next line
295
- this._triangles = [];
296
- };
297
- SolidParser.prototype._addPreviousObjMesh = function () {
298
- //Check if it is not the first mesh. Otherwise we don't have data.
299
- if (this._meshesFromObj.length > 0) {
300
- //Get the previous mesh for applying the data about the faces
301
- //=> in obj file, faces definition append after the name of the mesh
302
- this._handledMesh = this._meshesFromObj[this._meshesFromObj.length - 1];
303
- //Set the data into Array for the mesh
304
- this._unwrapData();
305
- // Reverse tab. Otherwise face are displayed in the wrong sens
306
- this._indicesForBabylon.reverse();
307
- //Set the information for the mesh
308
- //Slice the array to avoid rewriting because of the fact this is the same var which be rewrited
309
- this._handledMesh.indices = this._indicesForBabylon.slice();
310
- this._handledMesh.positions = this._unwrappedPositionsForBabylon.slice();
311
- this._handledMesh.normals = this._unwrappedNormalsForBabylon.slice();
312
- this._handledMesh.uvs = this._unwrappedUVForBabylon.slice();
313
- if (this._loadingOptions.importVertexColors) {
314
- this._handledMesh.colors = this._unwrappedColorsForBabylon.slice();
315
- }
316
- //Reset the array for the next mesh
317
- this._indicesForBabylon = [];
318
- this._unwrappedPositionsForBabylon = [];
319
- this._unwrappedColorsForBabylon = [];
320
- this._unwrappedNormalsForBabylon = [];
321
- this._unwrappedUVForBabylon = [];
322
- }
323
- };
324
- SolidParser.prototype._optimizeNormals = function (mesh) {
325
- var positions = mesh.getVerticesData(VertexBuffer.PositionKind);
326
- var normals = mesh.getVerticesData(VertexBuffer.NormalKind);
327
- var mapVertices = {};
328
- if (!positions || !normals) {
329
- return;
330
- }
331
- for (var i = 0; i < positions.length / 3; i++) {
332
- var x = positions[i * 3 + 0];
333
- var y = positions[i * 3 + 1];
334
- var z = positions[i * 3 + 2];
335
- var key = x + "_" + y + "_" + z;
336
- var lst = mapVertices[key];
337
- if (!lst) {
338
- lst = [];
339
- mapVertices[key] = lst;
340
- }
341
- lst.push(i);
342
- }
343
- var normal = new Vector3();
344
- for (var key in mapVertices) {
345
- var lst = mapVertices[key];
346
- if (lst.length < 2) {
347
- continue;
348
- }
349
- var v0Idx = lst[0];
350
- for (var i = 1; i < lst.length; ++i) {
351
- var vIdx = lst[i];
352
- normals[v0Idx * 3 + 0] += normals[vIdx * 3 + 0];
353
- normals[v0Idx * 3 + 1] += normals[vIdx * 3 + 1];
354
- normals[v0Idx * 3 + 2] += normals[vIdx * 3 + 2];
355
- }
356
- normal.copyFromFloats(normals[v0Idx * 3 + 0], normals[v0Idx * 3 + 1], normals[v0Idx * 3 + 2]);
357
- normal.normalize();
358
- for (var i = 0; i < lst.length; ++i) {
359
- var vIdx = lst[i];
360
- normals[vIdx * 3 + 0] = normal.x;
361
- normals[vIdx * 3 + 1] = normal.y;
362
- normals[vIdx * 3 + 2] = normal.z;
363
- }
364
- }
365
- mesh.setVerticesData(VertexBuffer.NormalKind, normals);
366
- };
367
- /**
368
- * Function used to parse an OBJ string
369
- * @param meshesNames defines the list of meshes to load (all if not defined)
370
- * @param data defines the OBJ string
371
- * @param scene defines the hosting scene
372
- * @param assetContainer defines the asset container to load data in
373
- * @param onFileToLoadFound defines a callback that will be called if a MTL file is found
374
- */
375
- SolidParser.prototype.parse = function (meshesNames, data, scene, assetContainer, onFileToLoadFound) {
376
- var _a;
377
- // Split the file into lines
378
- var lines = data.split("\n");
379
- // Look at each line
380
- for (var i = 0; i < lines.length; i++) {
381
- var line = lines[i].trim().replace(/\s\s/g, " ");
382
- var result = void 0;
383
- // Comment or newLine
384
- if (line.length === 0 || line.charAt(0) === "#") {
385
- continue;
386
- //Get information about one position possible for the vertices
387
- }
388
- else if (SolidParser.VertexPattern.test(line)) {
389
- result = line.match(/[^ ]+/g); // match will return non-null due to passing regex pattern
390
- // Value of result with line: "v 1.0 2.0 3.0"
391
- // ["v", "1.0", "2.0", "3.0"]
392
- // Create a Vector3 with the position x, y, z
393
- this._positions.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
394
- if (this._loadingOptions.importVertexColors) {
395
- if (result.length >= 7) {
396
- var r = parseFloat(result[4]);
397
- var g = parseFloat(result[5]);
398
- var b = parseFloat(result[6]);
399
- this._colors.push(new Color4(r > 1 ? r / 255 : r, g > 1 ? g / 255 : g, b > 1 ? b / 255 : b, result.length === 7 || result[7] === undefined ? 1 : parseFloat(result[7])));
400
- }
401
- else {
402
- // TODO: maybe push NULL and if all are NULL to skip (and remove grayColor var).
403
- this._colors.push(this._grayColor);
404
- }
405
- }
406
- }
407
- else if ((result = SolidParser.NormalPattern.exec(line)) !== null) {
408
- //Create a Vector3 with the normals x, y, z
409
- //Value of result
410
- // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
411
- //Add the Vector in the list of normals
412
- this._normals.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
413
- }
414
- else if ((result = SolidParser.UVPattern.exec(line)) !== null) {
415
- //Create a Vector2 with the normals u, v
416
- //Value of result
417
- // ["vt 0.1 0.2 0.3", "0.1", "0.2"]
418
- //Add the Vector in the list of uvs
419
- this._uvs.push(new Vector2(parseFloat(result[1]) * this._loadingOptions.UVScaling.x, parseFloat(result[2]) * this._loadingOptions.UVScaling.y));
420
- //Identify patterns of faces
421
- //Face could be defined in different type of pattern
422
- }
423
- else if ((result = SolidParser.FacePattern3.exec(line)) !== null) {
424
- //Value of result:
425
- //["f 1/1/1 2/2/2 3/3/3", "1/1/1 2/2/2 3/3/3"...]
426
- //Set the data for this face
427
- this._setDataForCurrentFaceWithPattern3(result[1].trim().split(" "), // ["1/1/1", "2/2/2", "3/3/3"]
428
- 1);
429
- }
430
- else if ((result = SolidParser.FacePattern4.exec(line)) !== null) {
431
- //Value of result:
432
- //["f 1//1 2//2 3//3", "1//1 2//2 3//3"...]
433
- //Set the data for this face
434
- this._setDataForCurrentFaceWithPattern4(result[1].trim().split(" "), // ["1//1", "2//2", "3//3"]
435
- 1);
436
- }
437
- else if ((result = SolidParser.FacePattern5.exec(line)) !== null) {
438
- //Value of result:
439
- //["f -1/-1/-1 -2/-2/-2 -3/-3/-3", "-1/-1/-1 -2/-2/-2 -3/-3/-3"...]
440
- //Set the data for this face
441
- this._setDataForCurrentFaceWithPattern5(result[1].trim().split(" "), // ["-1/-1/-1", "-2/-2/-2", "-3/-3/-3"]
442
- 1);
443
- }
444
- else if ((result = SolidParser.FacePattern2.exec(line)) !== null) {
445
- //Value of result:
446
- //["f 1/1 2/2 3/3", "1/1 2/2 3/3"...]
447
- //Set the data for this face
448
- this._setDataForCurrentFaceWithPattern2(result[1].trim().split(" "), // ["1/1", "2/2", "3/3"]
449
- 1);
450
- }
451
- else if ((result = SolidParser.FacePattern1.exec(line)) !== null) {
452
- //Value of result
453
- //["f 1 2 3", "1 2 3"...]
454
- //Set the data for this face
455
- this._setDataForCurrentFaceWithPattern1(result[1].trim().split(" "), // ["1", "2", "3"]
456
- 1);
457
- // Define a mesh or an object
458
- // Each time this keyword is analysed, create a new Object with all data for creating a babylonMesh
459
- }
460
- else if (SolidParser.GroupDescriptor.test(line) || SolidParser.ObjectDescriptor.test(line)) {
461
- // Create a new mesh corresponding to the name of the group.
462
- // Definition of the mesh
463
- var objMesh = {
464
- name: line.substring(2).trim(),
465
- indices: undefined,
466
- positions: undefined,
467
- normals: undefined,
468
- uvs: undefined,
469
- colors: undefined,
470
- materialName: "",
471
- };
472
- this._addPreviousObjMesh();
473
- //Push the last mesh created with only the name
474
- this._meshesFromObj.push(objMesh);
475
- //Set this variable to indicate that now meshesFromObj has objects defined inside
476
- this._hasMeshes = true;
477
- this._isFirstMaterial = true;
478
- this._increment = 1;
479
- //Keyword for applying a material
480
- }
481
- else if (SolidParser.UseMtlDescriptor.test(line)) {
482
- //Get the name of the material
483
- this._materialNameFromObj = line.substring(7).trim();
484
- //If this new material is in the same mesh
485
- if (!this._isFirstMaterial || !this._hasMeshes) {
486
- //Set the data for the previous mesh
487
- this._addPreviousObjMesh();
488
- //Create a new mesh
489
- var objMesh =
490
- //Set the name of the current obj mesh
491
- {
492
- name: (this._objMeshName || "mesh") + "_mm" + this._increment.toString(),
493
- indices: undefined,
494
- positions: undefined,
495
- normals: undefined,
496
- uvs: undefined,
497
- colors: undefined,
498
- materialName: this._materialNameFromObj,
499
- };
500
- this._increment++;
501
- //If meshes are already defined
502
- this._meshesFromObj.push(objMesh);
503
- this._hasMeshes = true;
504
- }
505
- //Set the material name if the previous line define a mesh
506
- if (this._hasMeshes && this._isFirstMaterial) {
507
- //Set the material name to the previous mesh (1 material per mesh)
508
- this._meshesFromObj[this._meshesFromObj.length - 1].materialName = this._materialNameFromObj;
509
- this._isFirstMaterial = false;
510
- }
511
- // Keyword for loading the mtl file
512
- }
513
- else if (SolidParser.MtlLibGroupDescriptor.test(line)) {
514
- // Get the name of mtl file
515
- onFileToLoadFound(line.substring(7).trim());
516
- // Apply smoothing
517
- }
518
- else if (SolidParser.SmoothDescriptor.test(line)) {
519
- // smooth shading => apply smoothing
520
- // Today I don't know it work with babylon and with obj.
521
- // With the obj file an integer is set
522
- }
523
- else {
524
- //If there is another possibility
525
- console.log("Unhandled expression at line : " + line);
526
- }
527
- }
528
- // At the end of the file, add the last mesh into the meshesFromObj array
529
- if (this._hasMeshes) {
530
- // Set the data for the last mesh
531
- this._handledMesh = this._meshesFromObj[this._meshesFromObj.length - 1];
532
- //Reverse indices for displaying faces in the good sense
533
- this._indicesForBabylon.reverse();
534
- //Get the good array
535
- this._unwrapData();
536
- //Set array
537
- this._handledMesh.indices = this._indicesForBabylon;
538
- this._handledMesh.positions = this._unwrappedPositionsForBabylon;
539
- this._handledMesh.normals = this._unwrappedNormalsForBabylon;
540
- this._handledMesh.uvs = this._unwrappedUVForBabylon;
541
- if (this._loadingOptions.importVertexColors) {
542
- this._handledMesh.colors = this._unwrappedColorsForBabylon;
543
- }
544
- }
545
- // If any o or g keyword not found, create a mesh with a random id
546
- if (!this._hasMeshes) {
547
- var newMaterial = null;
548
- if (this._indicesForBabylon.length) {
549
- // reverse tab of indices
550
- this._indicesForBabylon.reverse();
551
- //Get positions normals uvs
552
- this._unwrapData();
553
- }
554
- else {
555
- // There is no indices in the file. We will have to switch to point cloud rendering
556
- for (var _i = 0, _b = this._positions; _i < _b.length; _i++) {
557
- var pos = _b[_i];
558
- this._unwrappedPositionsForBabylon.push(pos.x, pos.y, pos.z);
559
- }
560
- if (this._normals.length) {
561
- for (var _c = 0, _d = this._normals; _c < _d.length; _c++) {
562
- var normal = _d[_c];
563
- this._unwrappedNormalsForBabylon.push(normal.x, normal.y, normal.z);
564
- }
565
- }
566
- if (this._uvs.length) {
567
- for (var _e = 0, _f = this._uvs; _e < _f.length; _e++) {
568
- var uv = _f[_e];
569
- this._unwrappedUVForBabylon.push(uv.x, uv.y);
570
- }
571
- }
572
- if (this._colors.length) {
573
- for (var _g = 0, _h = this._colors; _g < _h.length; _g++) {
574
- var color = _h[_g];
575
- this._unwrappedColorsForBabylon.push(color.r, color.g, color.b, color.a);
576
- }
577
- }
578
- if (!this._materialNameFromObj) {
579
- // Create a material with point cloud on
580
- newMaterial = new StandardMaterial(Geometry.RandomId(), scene);
581
- newMaterial.pointsCloud = true;
582
- this._materialNameFromObj = newMaterial.name;
583
- if (!this._normals.length) {
584
- newMaterial.disableLighting = true;
585
- newMaterial.emissiveColor = Color3.White();
586
- }
587
- }
588
- }
589
- //Set data for one mesh
590
- this._meshesFromObj.push({
591
- name: Geometry.RandomId(),
592
- indices: this._indicesForBabylon,
593
- positions: this._unwrappedPositionsForBabylon,
594
- colors: this._unwrappedColorsForBabylon,
595
- normals: this._unwrappedNormalsForBabylon,
596
- uvs: this._unwrappedUVForBabylon,
597
- materialName: this._materialNameFromObj,
598
- directMaterial: newMaterial,
599
- });
600
- }
601
- //Set data for each mesh
602
- for (var j = 0; j < this._meshesFromObj.length; j++) {
603
- //check meshesNames (stlFileLoader)
604
- if (meshesNames && this._meshesFromObj[j].name) {
605
- if (meshesNames instanceof Array) {
606
- if (meshesNames.indexOf(this._meshesFromObj[j].name) === -1) {
607
- continue;
608
- }
609
- }
610
- else {
611
- if (this._meshesFromObj[j].name !== meshesNames) {
612
- continue;
613
- }
614
- }
615
- }
616
- //Get the current mesh
617
- //Set the data with VertexBuffer for each mesh
618
- this._handledMesh = this._meshesFromObj[j];
619
- //Create a Mesh with the name of the obj mesh
620
- scene._blockEntityCollection = !!assetContainer;
621
- var babylonMesh = new Mesh(this._meshesFromObj[j].name, scene);
622
- babylonMesh._parentContainer = assetContainer;
623
- scene._blockEntityCollection = false;
624
- //Push the name of the material to an array
625
- //This is indispensable for the importMesh function
626
- this._materialToUse.push(this._meshesFromObj[j].materialName);
627
- if (((_a = this._handledMesh.positions) === null || _a === void 0 ? void 0 : _a.length) === 0) {
628
- //Push the mesh into an array
629
- this._babylonMeshesArray.push(babylonMesh);
630
- continue;
631
- }
632
- var vertexData = new VertexData(); //The container for the values
633
- //Set the data for the babylonMesh
634
- vertexData.uvs = this._handledMesh.uvs;
635
- vertexData.indices = this._handledMesh.indices;
636
- vertexData.positions = this._handledMesh.positions;
637
- if (this._loadingOptions.computeNormals) {
638
- var normals = new Array();
639
- VertexData.ComputeNormals(this._handledMesh.positions, this._handledMesh.indices, normals);
640
- vertexData.normals = normals;
641
- }
642
- else {
643
- vertexData.normals = this._handledMesh.normals;
644
- }
645
- if (this._loadingOptions.importVertexColors) {
646
- vertexData.colors = this._handledMesh.colors;
647
- }
648
- //Set the data from the VertexBuffer to the current Mesh
649
- vertexData.applyToMesh(babylonMesh);
650
- if (this._loadingOptions.invertY) {
651
- babylonMesh.scaling.y *= -1;
652
- }
653
- if (this._loadingOptions.optimizeNormals) {
654
- this._optimizeNormals(babylonMesh);
655
- }
656
- //Push the mesh into an array
657
- this._babylonMeshesArray.push(babylonMesh);
658
- if (this._handledMesh.directMaterial) {
659
- babylonMesh.material = this._handledMesh.directMaterial;
660
- }
661
- }
662
- };
663
- // Descriptor
664
- /** Object descriptor */
665
- SolidParser.ObjectDescriptor = /^o/;
666
- /** Group descriptor */
667
- SolidParser.GroupDescriptor = /^g/;
668
- /** Material lib descriptor */
669
- SolidParser.MtlLibGroupDescriptor = /^mtllib /;
670
- /** Use a material descriptor */
671
- SolidParser.UseMtlDescriptor = /^usemtl /;
672
- /** Smooth descriptor */
673
- SolidParser.SmoothDescriptor = /^s /;
674
- // Patterns
675
- /** Pattern used to detect a vertex */
676
- SolidParser.VertexPattern = /v(\s+[\d|.|+|\-|e|E]+){3,7}/;
677
- /** Pattern used to detect a normal */
678
- SolidParser.NormalPattern = /vn(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/;
679
- /** Pattern used to detect a UV set */
680
- SolidParser.UVPattern = /vt(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/;
681
- /** Pattern used to detect a first kind of face (f vertex vertex vertex) */
682
- SolidParser.FacePattern1 = /f\s+(([\d]{1,}[\s]?){3,})+/;
683
- /** Pattern used to detect a second kind of face (f vertex/uvs vertex/uvs vertex/uvs) */
684
- SolidParser.FacePattern2 = /f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
685
- /** Pattern used to detect a third kind of face (f vertex/uvs/normal vertex/uvs/normal vertex/uvs/normal) */
686
- SolidParser.FacePattern3 = /f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
687
- /** Pattern used to detect a fourth kind of face (f vertex//normal vertex//normal vertex//normal)*/
688
- SolidParser.FacePattern4 = /f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/;
689
- /** Pattern used to detect a fifth kind of face (f -vertex/-uvs/-normal -vertex/-uvs/-normal -vertex/-uvs/-normal) */
690
- SolidParser.FacePattern5 = /f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/;
691
- return SolidParser;
692
- }());
693
- export { SolidParser };
1
+ import { VertexBuffer } from "@babylonjs/core/Buffers/buffer.js";
2
+ import { StandardMaterial } from "@babylonjs/core/Materials/standardMaterial.js";
3
+ import { Color3, Color4 } from "@babylonjs/core/Maths/math.color.js";
4
+ import { Vector2, Vector3 } from "@babylonjs/core/Maths/math.vector.js";
5
+ import { Geometry } from "@babylonjs/core/Meshes/geometry.js";
6
+ import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
7
+ import { VertexData } from "@babylonjs/core/Meshes/mesh.vertexData.js";
8
+ /**
9
+ * Class used to load mesh data from OBJ content
10
+ */
11
+ var SolidParser = /** @class */ (function () {
12
+ /**
13
+ * Creates a new SolidParser
14
+ * @param materialToUse defines the array to fill with the list of materials to use (it will be filled by the parse function)
15
+ * @param babylonMeshesArray defines the array to fill with the list of loaded meshes (it will be filled by the parse function)
16
+ * @param loadingOptions defines the loading options to use
17
+ */
18
+ function SolidParser(materialToUse, babylonMeshesArray, loadingOptions) {
19
+ this._positions = []; //values for the positions of vertices
20
+ this._normals = []; //Values for the normals
21
+ this._uvs = []; //Values for the textures
22
+ this._colors = [];
23
+ this._meshesFromObj = []; //[mesh] Contains all the obj meshes
24
+ this._indicesForBabylon = []; //The list of indices for VertexData
25
+ this._wrappedPositionForBabylon = []; //The list of position in vectors
26
+ this._wrappedUvsForBabylon = []; //Array with all value of uvs to match with the indices
27
+ this._wrappedColorsForBabylon = []; // Array with all color values to match with the indices
28
+ this._wrappedNormalsForBabylon = []; //Array with all value of normals to match with the indices
29
+ this._tuplePosNorm = []; //Create a tuple with indice of Position, Normal, UV [pos, norm, uvs]
30
+ this._curPositionInIndices = 0;
31
+ this._hasMeshes = false; //Meshes are defined in the file
32
+ this._unwrappedPositionsForBabylon = []; //Value of positionForBabylon w/o Vector3() [x,y,z]
33
+ this._unwrappedColorsForBabylon = []; // Value of colorForBabylon w/o Color4() [r,g,b,a]
34
+ this._unwrappedNormalsForBabylon = []; //Value of normalsForBabylon w/o Vector3() [x,y,z]
35
+ this._unwrappedUVForBabylon = []; //Value of uvsForBabylon w/o Vector3() [x,y,z]
36
+ this._triangles = []; //Indices from new triangles coming from polygons
37
+ this._materialNameFromObj = ""; //The name of the current material
38
+ this._objMeshName = ""; //The name of the current obj mesh
39
+ this._increment = 1; //Id for meshes created by the multimaterial
40
+ this._isFirstMaterial = true;
41
+ this._grayColor = new Color4(0.5, 0.5, 0.5, 1);
42
+ this._materialToUse = materialToUse;
43
+ this._babylonMeshesArray = babylonMeshesArray;
44
+ this._loadingOptions = loadingOptions;
45
+ }
46
+ /**
47
+ * Search for obj in the given array.
48
+ * This function is called to check if a couple of data already exists in an array.
49
+ *
50
+ * If found, returns the index of the founded tuple index. Returns -1 if not found
51
+ * @param arr Array<{ normals: Array<number>, idx: Array<number> }>
52
+ * @param obj Array<number>
53
+ * @returns {boolean}
54
+ */
55
+ SolidParser.prototype._isInArray = function (arr, obj) {
56
+ if (!arr[obj[0]]) {
57
+ arr[obj[0]] = { normals: [], idx: [] };
58
+ }
59
+ var idx = arr[obj[0]].normals.indexOf(obj[1]);
60
+ return idx === -1 ? -1 : arr[obj[0]].idx[idx];
61
+ };
62
+ SolidParser.prototype._isInArrayUV = function (arr, obj) {
63
+ if (!arr[obj[0]]) {
64
+ arr[obj[0]] = { normals: [], idx: [], uv: [] };
65
+ }
66
+ var idx = arr[obj[0]].normals.indexOf(obj[1]);
67
+ if (idx != 1 && obj[2] === arr[obj[0]].uv[idx]) {
68
+ return arr[obj[0]].idx[idx];
69
+ }
70
+ return -1;
71
+ };
72
+ /**
73
+ * This function set the data for each triangle.
74
+ * Data are position, normals and uvs
75
+ * If a tuple of (position, normal) is not set, add the data into the corresponding array
76
+ * If the tuple already exist, add only their indice
77
+ *
78
+ * @param indicePositionFromObj Integer The index in positions array
79
+ * @param indiceUvsFromObj Integer The index in uvs array
80
+ * @param indiceNormalFromObj Integer The index in normals array
81
+ * @param positionVectorFromOBJ Vector3 The value of position at index objIndice
82
+ * @param textureVectorFromOBJ Vector3 The value of uvs
83
+ * @param normalsVectorFromOBJ Vector3 The value of normals at index objNormale
84
+ * @param positionColorsFromOBJ
85
+ */
86
+ SolidParser.prototype._setData = function (indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, positionVectorFromOBJ, textureVectorFromOBJ, normalsVectorFromOBJ, positionColorsFromOBJ) {
87
+ //Check if this tuple already exists in the list of tuples
88
+ var _index;
89
+ if (this._loadingOptions.optimizeWithUV) {
90
+ _index = this._isInArrayUV(this._tuplePosNorm, [indicePositionFromObj, indiceNormalFromObj, indiceUvsFromObj]);
91
+ }
92
+ else {
93
+ _index = this._isInArray(this._tuplePosNorm, [indicePositionFromObj, indiceNormalFromObj]);
94
+ }
95
+ //If it not exists
96
+ if (_index === -1) {
97
+ //Add an new indice.
98
+ //The array of indices is only an array with his length equal to the number of triangles - 1.
99
+ //We add vertices data in this order
100
+ this._indicesForBabylon.push(this._wrappedPositionForBabylon.length);
101
+ //Push the position of vertice for Babylon
102
+ //Each element is a Vector3(x,y,z)
103
+ this._wrappedPositionForBabylon.push(positionVectorFromOBJ);
104
+ //Push the uvs for Babylon
105
+ //Each element is a Vector3(u,v)
106
+ this._wrappedUvsForBabylon.push(textureVectorFromOBJ);
107
+ //Push the normals for Babylon
108
+ //Each element is a Vector3(x,y,z)
109
+ this._wrappedNormalsForBabylon.push(normalsVectorFromOBJ);
110
+ if (positionColorsFromOBJ !== undefined) {
111
+ //Push the colors for Babylon
112
+ //Each element is a BABYLON.Color4(r,g,b,a)
113
+ this._wrappedColorsForBabylon.push(positionColorsFromOBJ);
114
+ }
115
+ //Add the tuple in the comparison list
116
+ this._tuplePosNorm[indicePositionFromObj].normals.push(indiceNormalFromObj);
117
+ this._tuplePosNorm[indicePositionFromObj].idx.push(this._curPositionInIndices++);
118
+ if (this._loadingOptions.optimizeWithUV) {
119
+ this._tuplePosNorm[indicePositionFromObj].uv.push(indiceUvsFromObj);
120
+ }
121
+ }
122
+ else {
123
+ //The tuple already exists
124
+ //Add the index of the already existing tuple
125
+ //At this index we can get the value of position, normal, color and uvs of vertex
126
+ this._indicesForBabylon.push(_index);
127
+ }
128
+ };
129
+ /**
130
+ * Transform Vector() and BABYLON.Color() objects into numbers in an array
131
+ */
132
+ SolidParser.prototype._unwrapData = function () {
133
+ //Every array has the same length
134
+ for (var l = 0; l < this._wrappedPositionForBabylon.length; l++) {
135
+ //Push the x, y, z values of each element in the unwrapped array
136
+ this._unwrappedPositionsForBabylon.push(this._wrappedPositionForBabylon[l].x, this._wrappedPositionForBabylon[l].y, this._wrappedPositionForBabylon[l].z);
137
+ this._unwrappedNormalsForBabylon.push(this._wrappedNormalsForBabylon[l].x, this._wrappedNormalsForBabylon[l].y, this._wrappedNormalsForBabylon[l].z);
138
+ this._unwrappedUVForBabylon.push(this._wrappedUvsForBabylon[l].x, this._wrappedUvsForBabylon[l].y); //z is an optional value not supported by BABYLON
139
+ if (this._loadingOptions.importVertexColors) {
140
+ //Push the r, g, b, a values of each element in the unwrapped array
141
+ this._unwrappedColorsForBabylon.push(this._wrappedColorsForBabylon[l].r, this._wrappedColorsForBabylon[l].g, this._wrappedColorsForBabylon[l].b, this._wrappedColorsForBabylon[l].a);
142
+ }
143
+ }
144
+ // Reset arrays for the next new meshes
145
+ this._wrappedPositionForBabylon = [];
146
+ this._wrappedNormalsForBabylon = [];
147
+ this._wrappedUvsForBabylon = [];
148
+ this._wrappedColorsForBabylon = [];
149
+ this._tuplePosNorm = [];
150
+ this._curPositionInIndices = 0;
151
+ };
152
+ /**
153
+ * Create triangles from polygons
154
+ * It is important to notice that a triangle is a polygon
155
+ * We get 5 patterns of face defined in OBJ File :
156
+ * facePattern1 = ["1","2","3","4","5","6"]
157
+ * facePattern2 = ["1/1","2/2","3/3","4/4","5/5","6/6"]
158
+ * facePattern3 = ["1/1/1","2/2/2","3/3/3","4/4/4","5/5/5","6/6/6"]
159
+ * facePattern4 = ["1//1","2//2","3//3","4//4","5//5","6//6"]
160
+ * facePattern5 = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-4/-4/-4","-5/-5/-5","-6/-6/-6"]
161
+ * Each pattern is divided by the same method
162
+ * @param faces Array[String] The indices of elements
163
+ * @param v Integer The variable to increment
164
+ */
165
+ SolidParser.prototype._getTriangles = function (faces, v) {
166
+ //Work for each element of the array
167
+ for (var faceIndex = v; faceIndex < faces.length - 1; faceIndex++) {
168
+ //Add on the triangle variable the indexes to obtain triangles
169
+ this._triangles.push(faces[0], faces[faceIndex], faces[faceIndex + 1]);
170
+ }
171
+ //Result obtained after 2 iterations:
172
+ //Pattern1 => triangle = ["1","2","3","1","3","4"];
173
+ //Pattern2 => triangle = ["1/1","2/2","3/3","1/1","3/3","4/4"];
174
+ //Pattern3 => triangle = ["1/1/1","2/2/2","3/3/3","1/1/1","3/3/3","4/4/4"];
175
+ //Pattern4 => triangle = ["1//1","2//2","3//3","1//1","3//3","4//4"];
176
+ //Pattern5 => triangle = ["-1/-1/-1","-2/-2/-2","-3/-3/-3","-1/-1/-1","-3/-3/-3","-4/-4/-4"];
177
+ };
178
+ /**
179
+ * Create triangles and push the data for each polygon for the pattern 1
180
+ * In this pattern we get vertice positions
181
+ * @param face
182
+ * @param v
183
+ */
184
+ SolidParser.prototype._setDataForCurrentFaceWithPattern1 = function (face, v) {
185
+ //Get the indices of triangles for each polygon
186
+ this._getTriangles(face, v);
187
+ //For each element in the triangles array.
188
+ //This var could contains 1 to an infinity of triangles
189
+ for (var k = 0; k < this._triangles.length; k++) {
190
+ // Set position indice
191
+ var indicePositionFromObj = parseInt(this._triangles[k]) - 1;
192
+ this._setData(indicePositionFromObj, 0, 0, // In the pattern 1, normals and uvs are not defined
193
+ this._positions[indicePositionFromObj], // Get the vectors data
194
+ Vector2.Zero(), Vector3.Up(), // Create default vectors
195
+ this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
196
+ }
197
+ //Reset variable for the next line
198
+ this._triangles = [];
199
+ };
200
+ /**
201
+ * Create triangles and push the data for each polygon for the pattern 2
202
+ * In this pattern we get vertice positions and uvsu
203
+ * @param face
204
+ * @param v
205
+ */
206
+ SolidParser.prototype._setDataForCurrentFaceWithPattern2 = function (face, v) {
207
+ //Get the indices of triangles for each polygon
208
+ this._getTriangles(face, v);
209
+ for (var k = 0; k < this._triangles.length; k++) {
210
+ //triangle[k] = "1/1"
211
+ //Split the data for getting position and uv
212
+ var point = this._triangles[k].split("/"); // ["1", "1"]
213
+ //Set position indice
214
+ var indicePositionFromObj = parseInt(point[0]) - 1;
215
+ //Set uv indice
216
+ var indiceUvsFromObj = parseInt(point[1]) - 1;
217
+ this._setData(indicePositionFromObj, indiceUvsFromObj, 0, //Default value for normals
218
+ this._positions[indicePositionFromObj], //Get the values for each element
219
+ this._uvs[indiceUvsFromObj], Vector3.Up(), //Default value for normals
220
+ this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
221
+ }
222
+ //Reset variable for the next line
223
+ this._triangles = [];
224
+ };
225
+ /**
226
+ * Create triangles and push the data for each polygon for the pattern 3
227
+ * In this pattern we get vertice positions, uvs and normals
228
+ * @param face
229
+ * @param v
230
+ */
231
+ SolidParser.prototype._setDataForCurrentFaceWithPattern3 = function (face, v) {
232
+ //Get the indices of triangles for each polygon
233
+ this._getTriangles(face, v);
234
+ for (var k = 0; k < this._triangles.length; k++) {
235
+ //triangle[k] = "1/1/1"
236
+ //Split the data for getting position, uv, and normals
237
+ var point = this._triangles[k].split("/"); // ["1", "1", "1"]
238
+ // Set position indice
239
+ var indicePositionFromObj = parseInt(point[0]) - 1;
240
+ // Set uv indice
241
+ var indiceUvsFromObj = parseInt(point[1]) - 1;
242
+ // Set normal indice
243
+ var indiceNormalFromObj = parseInt(point[2]) - 1;
244
+ this._setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, this._positions[indicePositionFromObj], this._uvs[indiceUvsFromObj], this._normals[indiceNormalFromObj] //Set the vector for each component
245
+ );
246
+ }
247
+ //Reset variable for the next line
248
+ this._triangles = [];
249
+ };
250
+ /**
251
+ * Create triangles and push the data for each polygon for the pattern 4
252
+ * In this pattern we get vertice positions and normals
253
+ * @param face
254
+ * @param v
255
+ */
256
+ SolidParser.prototype._setDataForCurrentFaceWithPattern4 = function (face, v) {
257
+ this._getTriangles(face, v);
258
+ for (var k = 0; k < this._triangles.length; k++) {
259
+ //triangle[k] = "1//1"
260
+ //Split the data for getting position and normals
261
+ var point = this._triangles[k].split("//"); // ["1", "1"]
262
+ // We check indices, and normals
263
+ var indicePositionFromObj = parseInt(point[0]) - 1;
264
+ var indiceNormalFromObj = parseInt(point[1]) - 1;
265
+ this._setData(indicePositionFromObj, 1, //Default value for uv
266
+ indiceNormalFromObj, this._positions[indicePositionFromObj], //Get each vector of data
267
+ Vector2.Zero(), this._normals[indiceNormalFromObj], this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
268
+ }
269
+ //Reset variable for the next line
270
+ this._triangles = [];
271
+ };
272
+ /*
273
+ * Create triangles and push the data for each polygon for the pattern 3
274
+ * In this pattern we get vertice positions, uvs and normals
275
+ * @param face
276
+ * @param v
277
+ */
278
+ SolidParser.prototype._setDataForCurrentFaceWithPattern5 = function (face, v) {
279
+ //Get the indices of triangles for each polygon
280
+ this._getTriangles(face, v);
281
+ for (var k = 0; k < this._triangles.length; k++) {
282
+ //triangle[k] = "-1/-1/-1"
283
+ //Split the data for getting position, uv, and normals
284
+ var point = this._triangles[k].split("/"); // ["-1", "-1", "-1"]
285
+ // Set position indice
286
+ var indicePositionFromObj = this._positions.length + parseInt(point[0]);
287
+ // Set uv indice
288
+ var indiceUvsFromObj = this._uvs.length + parseInt(point[1]);
289
+ // Set normal indice
290
+ var indiceNormalFromObj = this._normals.length + parseInt(point[2]);
291
+ this._setData(indicePositionFromObj, indiceUvsFromObj, indiceNormalFromObj, this._positions[indicePositionFromObj], this._uvs[indiceUvsFromObj], this._normals[indiceNormalFromObj], //Set the vector for each component
292
+ this._loadingOptions.importVertexColors ? this._colors[indicePositionFromObj] : undefined);
293
+ }
294
+ //Reset variable for the next line
295
+ this._triangles = [];
296
+ };
297
+ SolidParser.prototype._addPreviousObjMesh = function () {
298
+ //Check if it is not the first mesh. Otherwise we don't have data.
299
+ if (this._meshesFromObj.length > 0) {
300
+ //Get the previous mesh for applying the data about the faces
301
+ //=> in obj file, faces definition append after the name of the mesh
302
+ this._handledMesh = this._meshesFromObj[this._meshesFromObj.length - 1];
303
+ //Set the data into Array for the mesh
304
+ this._unwrapData();
305
+ // Reverse tab. Otherwise face are displayed in the wrong sens
306
+ this._indicesForBabylon.reverse();
307
+ //Set the information for the mesh
308
+ //Slice the array to avoid rewriting because of the fact this is the same var which be rewrited
309
+ this._handledMesh.indices = this._indicesForBabylon.slice();
310
+ this._handledMesh.positions = this._unwrappedPositionsForBabylon.slice();
311
+ this._handledMesh.normals = this._unwrappedNormalsForBabylon.slice();
312
+ this._handledMesh.uvs = this._unwrappedUVForBabylon.slice();
313
+ if (this._loadingOptions.importVertexColors) {
314
+ this._handledMesh.colors = this._unwrappedColorsForBabylon.slice();
315
+ }
316
+ //Reset the array for the next mesh
317
+ this._indicesForBabylon = [];
318
+ this._unwrappedPositionsForBabylon = [];
319
+ this._unwrappedColorsForBabylon = [];
320
+ this._unwrappedNormalsForBabylon = [];
321
+ this._unwrappedUVForBabylon = [];
322
+ }
323
+ };
324
+ SolidParser.prototype._optimizeNormals = function (mesh) {
325
+ var positions = mesh.getVerticesData(VertexBuffer.PositionKind);
326
+ var normals = mesh.getVerticesData(VertexBuffer.NormalKind);
327
+ var mapVertices = {};
328
+ if (!positions || !normals) {
329
+ return;
330
+ }
331
+ for (var i = 0; i < positions.length / 3; i++) {
332
+ var x = positions[i * 3 + 0];
333
+ var y = positions[i * 3 + 1];
334
+ var z = positions[i * 3 + 2];
335
+ var key = x + "_" + y + "_" + z;
336
+ var lst = mapVertices[key];
337
+ if (!lst) {
338
+ lst = [];
339
+ mapVertices[key] = lst;
340
+ }
341
+ lst.push(i);
342
+ }
343
+ var normal = new Vector3();
344
+ for (var key in mapVertices) {
345
+ var lst = mapVertices[key];
346
+ if (lst.length < 2) {
347
+ continue;
348
+ }
349
+ var v0Idx = lst[0];
350
+ for (var i = 1; i < lst.length; ++i) {
351
+ var vIdx = lst[i];
352
+ normals[v0Idx * 3 + 0] += normals[vIdx * 3 + 0];
353
+ normals[v0Idx * 3 + 1] += normals[vIdx * 3 + 1];
354
+ normals[v0Idx * 3 + 2] += normals[vIdx * 3 + 2];
355
+ }
356
+ normal.copyFromFloats(normals[v0Idx * 3 + 0], normals[v0Idx * 3 + 1], normals[v0Idx * 3 + 2]);
357
+ normal.normalize();
358
+ for (var i = 0; i < lst.length; ++i) {
359
+ var vIdx = lst[i];
360
+ normals[vIdx * 3 + 0] = normal.x;
361
+ normals[vIdx * 3 + 1] = normal.y;
362
+ normals[vIdx * 3 + 2] = normal.z;
363
+ }
364
+ }
365
+ mesh.setVerticesData(VertexBuffer.NormalKind, normals);
366
+ };
367
+ /**
368
+ * Function used to parse an OBJ string
369
+ * @param meshesNames defines the list of meshes to load (all if not defined)
370
+ * @param data defines the OBJ string
371
+ * @param scene defines the hosting scene
372
+ * @param assetContainer defines the asset container to load data in
373
+ * @param onFileToLoadFound defines a callback that will be called if a MTL file is found
374
+ */
375
+ SolidParser.prototype.parse = function (meshesNames, data, scene, assetContainer, onFileToLoadFound) {
376
+ var _a;
377
+ // Split the file into lines
378
+ var lines = data.split("\n");
379
+ // Look at each line
380
+ for (var i = 0; i < lines.length; i++) {
381
+ var line = lines[i].trim().replace(/\s\s/g, " ");
382
+ var result = void 0;
383
+ // Comment or newLine
384
+ if (line.length === 0 || line.charAt(0) === "#") {
385
+ continue;
386
+ //Get information about one position possible for the vertices
387
+ }
388
+ else if (SolidParser.VertexPattern.test(line)) {
389
+ result = line.match(/[^ ]+/g); // match will return non-null due to passing regex pattern
390
+ // Value of result with line: "v 1.0 2.0 3.0"
391
+ // ["v", "1.0", "2.0", "3.0"]
392
+ // Create a Vector3 with the position x, y, z
393
+ this._positions.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
394
+ if (this._loadingOptions.importVertexColors) {
395
+ if (result.length >= 7) {
396
+ var r = parseFloat(result[4]);
397
+ var g = parseFloat(result[5]);
398
+ var b = parseFloat(result[6]);
399
+ this._colors.push(new Color4(r > 1 ? r / 255 : r, g > 1 ? g / 255 : g, b > 1 ? b / 255 : b, result.length === 7 || result[7] === undefined ? 1 : parseFloat(result[7])));
400
+ }
401
+ else {
402
+ // TODO: maybe push NULL and if all are NULL to skip (and remove grayColor var).
403
+ this._colors.push(this._grayColor);
404
+ }
405
+ }
406
+ }
407
+ else if ((result = SolidParser.NormalPattern.exec(line)) !== null) {
408
+ //Create a Vector3 with the normals x, y, z
409
+ //Value of result
410
+ // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
411
+ //Add the Vector in the list of normals
412
+ this._normals.push(new Vector3(parseFloat(result[1]), parseFloat(result[2]), parseFloat(result[3])));
413
+ }
414
+ else if ((result = SolidParser.UVPattern.exec(line)) !== null) {
415
+ //Create a Vector2 with the normals u, v
416
+ //Value of result
417
+ // ["vt 0.1 0.2 0.3", "0.1", "0.2"]
418
+ //Add the Vector in the list of uvs
419
+ this._uvs.push(new Vector2(parseFloat(result[1]) * this._loadingOptions.UVScaling.x, parseFloat(result[2]) * this._loadingOptions.UVScaling.y));
420
+ //Identify patterns of faces
421
+ //Face could be defined in different type of pattern
422
+ }
423
+ else if ((result = SolidParser.FacePattern3.exec(line)) !== null) {
424
+ //Value of result:
425
+ //["f 1/1/1 2/2/2 3/3/3", "1/1/1 2/2/2 3/3/3"...]
426
+ //Set the data for this face
427
+ this._setDataForCurrentFaceWithPattern3(result[1].trim().split(" "), // ["1/1/1", "2/2/2", "3/3/3"]
428
+ 1);
429
+ }
430
+ else if ((result = SolidParser.FacePattern4.exec(line)) !== null) {
431
+ //Value of result:
432
+ //["f 1//1 2//2 3//3", "1//1 2//2 3//3"...]
433
+ //Set the data for this face
434
+ this._setDataForCurrentFaceWithPattern4(result[1].trim().split(" "), // ["1//1", "2//2", "3//3"]
435
+ 1);
436
+ }
437
+ else if ((result = SolidParser.FacePattern5.exec(line)) !== null) {
438
+ //Value of result:
439
+ //["f -1/-1/-1 -2/-2/-2 -3/-3/-3", "-1/-1/-1 -2/-2/-2 -3/-3/-3"...]
440
+ //Set the data for this face
441
+ this._setDataForCurrentFaceWithPattern5(result[1].trim().split(" "), // ["-1/-1/-1", "-2/-2/-2", "-3/-3/-3"]
442
+ 1);
443
+ }
444
+ else if ((result = SolidParser.FacePattern2.exec(line)) !== null) {
445
+ //Value of result:
446
+ //["f 1/1 2/2 3/3", "1/1 2/2 3/3"...]
447
+ //Set the data for this face
448
+ this._setDataForCurrentFaceWithPattern2(result[1].trim().split(" "), // ["1/1", "2/2", "3/3"]
449
+ 1);
450
+ }
451
+ else if ((result = SolidParser.FacePattern1.exec(line)) !== null) {
452
+ //Value of result
453
+ //["f 1 2 3", "1 2 3"...]
454
+ //Set the data for this face
455
+ this._setDataForCurrentFaceWithPattern1(result[1].trim().split(" "), // ["1", "2", "3"]
456
+ 1);
457
+ // Define a mesh or an object
458
+ // Each time this keyword is analysed, create a new Object with all data for creating a babylonMesh
459
+ }
460
+ else if (SolidParser.GroupDescriptor.test(line) || SolidParser.ObjectDescriptor.test(line)) {
461
+ // Create a new mesh corresponding to the name of the group.
462
+ // Definition of the mesh
463
+ var objMesh = {
464
+ name: line.substring(2).trim(),
465
+ indices: undefined,
466
+ positions: undefined,
467
+ normals: undefined,
468
+ uvs: undefined,
469
+ colors: undefined,
470
+ materialName: "",
471
+ };
472
+ this._addPreviousObjMesh();
473
+ //Push the last mesh created with only the name
474
+ this._meshesFromObj.push(objMesh);
475
+ //Set this variable to indicate that now meshesFromObj has objects defined inside
476
+ this._hasMeshes = true;
477
+ this._isFirstMaterial = true;
478
+ this._increment = 1;
479
+ //Keyword for applying a material
480
+ }
481
+ else if (SolidParser.UseMtlDescriptor.test(line)) {
482
+ //Get the name of the material
483
+ this._materialNameFromObj = line.substring(7).trim();
484
+ //If this new material is in the same mesh
485
+ if (!this._isFirstMaterial || !this._hasMeshes) {
486
+ //Set the data for the previous mesh
487
+ this._addPreviousObjMesh();
488
+ //Create a new mesh
489
+ var objMesh =
490
+ //Set the name of the current obj mesh
491
+ {
492
+ name: (this._objMeshName || "mesh") + "_mm" + this._increment.toString(),
493
+ indices: undefined,
494
+ positions: undefined,
495
+ normals: undefined,
496
+ uvs: undefined,
497
+ colors: undefined,
498
+ materialName: this._materialNameFromObj,
499
+ };
500
+ this._increment++;
501
+ //If meshes are already defined
502
+ this._meshesFromObj.push(objMesh);
503
+ this._hasMeshes = true;
504
+ }
505
+ //Set the material name if the previous line define a mesh
506
+ if (this._hasMeshes && this._isFirstMaterial) {
507
+ //Set the material name to the previous mesh (1 material per mesh)
508
+ this._meshesFromObj[this._meshesFromObj.length - 1].materialName = this._materialNameFromObj;
509
+ this._isFirstMaterial = false;
510
+ }
511
+ // Keyword for loading the mtl file
512
+ }
513
+ else if (SolidParser.MtlLibGroupDescriptor.test(line)) {
514
+ // Get the name of mtl file
515
+ onFileToLoadFound(line.substring(7).trim());
516
+ // Apply smoothing
517
+ }
518
+ else if (SolidParser.SmoothDescriptor.test(line)) {
519
+ // smooth shading => apply smoothing
520
+ // Today I don't know it work with babylon and with obj.
521
+ // With the obj file an integer is set
522
+ }
523
+ else {
524
+ //If there is another possibility
525
+ console.log("Unhandled expression at line : " + line);
526
+ }
527
+ }
528
+ // At the end of the file, add the last mesh into the meshesFromObj array
529
+ if (this._hasMeshes) {
530
+ // Set the data for the last mesh
531
+ this._handledMesh = this._meshesFromObj[this._meshesFromObj.length - 1];
532
+ //Reverse indices for displaying faces in the good sense
533
+ this._indicesForBabylon.reverse();
534
+ //Get the good array
535
+ this._unwrapData();
536
+ //Set array
537
+ this._handledMesh.indices = this._indicesForBabylon;
538
+ this._handledMesh.positions = this._unwrappedPositionsForBabylon;
539
+ this._handledMesh.normals = this._unwrappedNormalsForBabylon;
540
+ this._handledMesh.uvs = this._unwrappedUVForBabylon;
541
+ if (this._loadingOptions.importVertexColors) {
542
+ this._handledMesh.colors = this._unwrappedColorsForBabylon;
543
+ }
544
+ }
545
+ // If any o or g keyword not found, create a mesh with a random id
546
+ if (!this._hasMeshes) {
547
+ var newMaterial = null;
548
+ if (this._indicesForBabylon.length) {
549
+ // reverse tab of indices
550
+ this._indicesForBabylon.reverse();
551
+ //Get positions normals uvs
552
+ this._unwrapData();
553
+ }
554
+ else {
555
+ // There is no indices in the file. We will have to switch to point cloud rendering
556
+ for (var _i = 0, _b = this._positions; _i < _b.length; _i++) {
557
+ var pos = _b[_i];
558
+ this._unwrappedPositionsForBabylon.push(pos.x, pos.y, pos.z);
559
+ }
560
+ if (this._normals.length) {
561
+ for (var _c = 0, _d = this._normals; _c < _d.length; _c++) {
562
+ var normal = _d[_c];
563
+ this._unwrappedNormalsForBabylon.push(normal.x, normal.y, normal.z);
564
+ }
565
+ }
566
+ if (this._uvs.length) {
567
+ for (var _e = 0, _f = this._uvs; _e < _f.length; _e++) {
568
+ var uv = _f[_e];
569
+ this._unwrappedUVForBabylon.push(uv.x, uv.y);
570
+ }
571
+ }
572
+ if (this._colors.length) {
573
+ for (var _g = 0, _h = this._colors; _g < _h.length; _g++) {
574
+ var color = _h[_g];
575
+ this._unwrappedColorsForBabylon.push(color.r, color.g, color.b, color.a);
576
+ }
577
+ }
578
+ if (!this._materialNameFromObj) {
579
+ // Create a material with point cloud on
580
+ newMaterial = new StandardMaterial(Geometry.RandomId(), scene);
581
+ newMaterial.pointsCloud = true;
582
+ this._materialNameFromObj = newMaterial.name;
583
+ if (!this._normals.length) {
584
+ newMaterial.disableLighting = true;
585
+ newMaterial.emissiveColor = Color3.White();
586
+ }
587
+ }
588
+ }
589
+ //Set data for one mesh
590
+ this._meshesFromObj.push({
591
+ name: Geometry.RandomId(),
592
+ indices: this._indicesForBabylon,
593
+ positions: this._unwrappedPositionsForBabylon,
594
+ colors: this._unwrappedColorsForBabylon,
595
+ normals: this._unwrappedNormalsForBabylon,
596
+ uvs: this._unwrappedUVForBabylon,
597
+ materialName: this._materialNameFromObj,
598
+ directMaterial: newMaterial,
599
+ });
600
+ }
601
+ //Set data for each mesh
602
+ for (var j = 0; j < this._meshesFromObj.length; j++) {
603
+ //check meshesNames (stlFileLoader)
604
+ if (meshesNames && this._meshesFromObj[j].name) {
605
+ if (meshesNames instanceof Array) {
606
+ if (meshesNames.indexOf(this._meshesFromObj[j].name) === -1) {
607
+ continue;
608
+ }
609
+ }
610
+ else {
611
+ if (this._meshesFromObj[j].name !== meshesNames) {
612
+ continue;
613
+ }
614
+ }
615
+ }
616
+ //Get the current mesh
617
+ //Set the data with VertexBuffer for each mesh
618
+ this._handledMesh = this._meshesFromObj[j];
619
+ //Create a Mesh with the name of the obj mesh
620
+ scene._blockEntityCollection = !!assetContainer;
621
+ var babylonMesh = new Mesh(this._meshesFromObj[j].name, scene);
622
+ babylonMesh._parentContainer = assetContainer;
623
+ scene._blockEntityCollection = false;
624
+ //Push the name of the material to an array
625
+ //This is indispensable for the importMesh function
626
+ this._materialToUse.push(this._meshesFromObj[j].materialName);
627
+ if (((_a = this._handledMesh.positions) === null || _a === void 0 ? void 0 : _a.length) === 0) {
628
+ //Push the mesh into an array
629
+ this._babylonMeshesArray.push(babylonMesh);
630
+ continue;
631
+ }
632
+ var vertexData = new VertexData(); //The container for the values
633
+ //Set the data for the babylonMesh
634
+ vertexData.uvs = this._handledMesh.uvs;
635
+ vertexData.indices = this._handledMesh.indices;
636
+ vertexData.positions = this._handledMesh.positions;
637
+ if (this._loadingOptions.computeNormals) {
638
+ var normals = new Array();
639
+ VertexData.ComputeNormals(this._handledMesh.positions, this._handledMesh.indices, normals);
640
+ vertexData.normals = normals;
641
+ }
642
+ else {
643
+ vertexData.normals = this._handledMesh.normals;
644
+ }
645
+ if (this._loadingOptions.importVertexColors) {
646
+ vertexData.colors = this._handledMesh.colors;
647
+ }
648
+ //Set the data from the VertexBuffer to the current Mesh
649
+ vertexData.applyToMesh(babylonMesh);
650
+ if (this._loadingOptions.invertY) {
651
+ babylonMesh.scaling.y *= -1;
652
+ }
653
+ if (this._loadingOptions.optimizeNormals) {
654
+ this._optimizeNormals(babylonMesh);
655
+ }
656
+ //Push the mesh into an array
657
+ this._babylonMeshesArray.push(babylonMesh);
658
+ if (this._handledMesh.directMaterial) {
659
+ babylonMesh.material = this._handledMesh.directMaterial;
660
+ }
661
+ }
662
+ };
663
+ // Descriptor
664
+ /** Object descriptor */
665
+ SolidParser.ObjectDescriptor = /^o/;
666
+ /** Group descriptor */
667
+ SolidParser.GroupDescriptor = /^g/;
668
+ /** Material lib descriptor */
669
+ SolidParser.MtlLibGroupDescriptor = /^mtllib /;
670
+ /** Use a material descriptor */
671
+ SolidParser.UseMtlDescriptor = /^usemtl /;
672
+ /** Smooth descriptor */
673
+ SolidParser.SmoothDescriptor = /^s /;
674
+ // Patterns
675
+ /** Pattern used to detect a vertex */
676
+ SolidParser.VertexPattern = /v(\s+[\d|.|+|\-|e|E]+){3,7}/;
677
+ /** Pattern used to detect a normal */
678
+ SolidParser.NormalPattern = /vn(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/;
679
+ /** Pattern used to detect a UV set */
680
+ SolidParser.UVPattern = /vt(\s+[\d|.|+|\-|e|E]+)( +[\d|.|+|\-|e|E]+)/;
681
+ /** Pattern used to detect a first kind of face (f vertex vertex vertex) */
682
+ SolidParser.FacePattern1 = /f\s+(([\d]{1,}[\s]?){3,})+/;
683
+ /** Pattern used to detect a second kind of face (f vertex/uvs vertex/uvs vertex/uvs) */
684
+ SolidParser.FacePattern2 = /f\s+((([\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
685
+ /** Pattern used to detect a third kind of face (f vertex/uvs/normal vertex/uvs/normal vertex/uvs/normal) */
686
+ SolidParser.FacePattern3 = /f\s+((([\d]{1,}\/[\d]{1,}\/[\d]{1,}[\s]?){3,})+)/;
687
+ /** Pattern used to detect a fourth kind of face (f vertex//normal vertex//normal vertex//normal)*/
688
+ SolidParser.FacePattern4 = /f\s+((([\d]{1,}\/\/[\d]{1,}[\s]?){3,})+)/;
689
+ /** Pattern used to detect a fifth kind of face (f -vertex/-uvs/-normal -vertex/-uvs/-normal -vertex/-uvs/-normal) */
690
+ SolidParser.FacePattern5 = /f\s+(((-[\d]{1,}\/-[\d]{1,}\/-[\d]{1,}[\s]?){3,})+)/;
691
+ return SolidParser;
692
+ }());
693
+ export { SolidParser };
694
694
  //# sourceMappingURL=solidParser.js.map