@babylonjs/loaders 5.0.0-alpha.9 → 5.0.0-beta.4

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