@babylonjs/loaders 5.0.0-rc.1 → 5.0.0-rc.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. package/OBJ/index.d.ts +4 -4
  2. package/OBJ/index.js +4 -4
  3. package/OBJ/index.js.map +1 -1
  4. package/OBJ/mtlFileLoader.d.ts +41 -40
  5. package/OBJ/mtlFileLoader.js +232 -231
  6. package/OBJ/mtlFileLoader.js.map +1 -1
  7. package/OBJ/objFileLoader.d.ts +126 -133
  8. package/OBJ/objFileLoader.js +296 -300
  9. package/OBJ/objFileLoader.js.map +1 -1
  10. package/OBJ/objLoadingOptions.d.ts +43 -43
  11. package/OBJ/objLoadingOptions.js +1 -1
  12. package/OBJ/objLoadingOptions.js.map +1 -1
  13. package/OBJ/solidParser.d.ts +154 -153
  14. package/OBJ/solidParser.js +693 -699
  15. package/OBJ/solidParser.js.map +1 -1
  16. package/STL/index.d.ts +1 -1
  17. package/STL/index.js +1 -1
  18. package/STL/index.js.map +1 -1
  19. package/STL/stlFileLoader.d.ts +64 -71
  20. package/STL/stlFileLoader.js +233 -239
  21. package/STL/stlFileLoader.js.map +1 -1
  22. package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
  23. package/glTF/1.0/glTFBinaryExtension.js +60 -61
  24. package/glTF/1.0/glTFBinaryExtension.js.map +1 -1
  25. package/glTF/1.0/glTFLoader.d.ts +136 -106
  26. package/glTF/1.0/glTFLoader.js +1814 -1696
  27. package/glTF/1.0/glTFLoader.js.map +1 -1
  28. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
  29. package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
  30. package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -1
  31. package/glTF/1.0/glTFLoaderUtils.d.ts +63 -62
  32. package/glTF/1.0/glTFLoaderUtils.js +250 -232
  33. package/glTF/1.0/glTFLoaderUtils.js.map +1 -1
  34. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
  35. package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -127
  36. package/glTF/1.0/glTFMaterialsCommonExtension.js.map +1 -1
  37. package/glTF/1.0/index.d.ts +5 -5
  38. package/glTF/1.0/index.js +5 -5
  39. package/glTF/1.0/index.js.map +1 -1
  40. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -36
  41. package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -110
  42. package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -1
  43. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -28
  44. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -80
  45. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -1
  46. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -27
  47. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -42
  48. package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -1
  49. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -21
  50. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -32
  51. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -1
  52. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -32
  53. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -55
  54. package/glTF/2.0/Extensions/ExtrasAsMetadata.js.map +1 -1
  55. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -31
  56. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -101
  57. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -1
  58. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -28
  59. package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -81
  60. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -1
  61. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -31
  62. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -86
  63. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -1
  64. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -30
  65. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -45
  66. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js.map +1 -1
  67. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -34
  68. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -54
  69. package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -1
  70. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -30
  71. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -72
  72. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -1
  73. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -31
  74. package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -76
  75. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -1
  76. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -30
  77. package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -66
  78. package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -1
  79. package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -31
  80. package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -76
  81. package/glTF/2.0/Extensions/KHR_materials_translucency.js.map +1 -1
  82. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -30
  83. package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -277
  84. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
  85. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -30
  86. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -65
  87. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +1 -1
  88. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -76
  89. package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -243
  90. package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -1
  91. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -31
  92. package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -78
  93. package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -1
  94. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -19
  95. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -21
  96. package/glTF/2.0/Extensions/KHR_mesh_quantization.js.map +1 -1
  97. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -21
  98. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -32
  99. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -1
  100. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -25
  101. package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -53
  102. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +1 -1
  103. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -29
  104. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -46
  105. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -1
  106. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -38
  107. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -216
  108. package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -1
  109. package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -75
  110. package/glTF/2.0/Extensions/MSFT_lod.js +369 -333
  111. package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -1
  112. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
  113. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
  114. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js.map +1 -1
  115. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
  116. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
  117. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js.map +1 -1
  118. package/glTF/2.0/Extensions/index.d.ts +26 -26
  119. package/glTF/2.0/Extensions/index.js +26 -26
  120. package/glTF/2.0/Extensions/index.js.map +1 -1
  121. package/glTF/2.0/glTFLoader.d.ts +406 -357
  122. package/glTF/2.0/glTFLoader.js +2366 -2266
  123. package/glTF/2.0/glTFLoader.js.map +1 -1
  124. package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
  125. package/glTF/2.0/glTFLoaderExtension.js +1 -1
  126. package/glTF/2.0/glTFLoaderExtension.js.map +1 -1
  127. package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -209
  128. package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
  129. package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -1
  130. package/glTF/2.0/index.d.ts +4 -4
  131. package/glTF/2.0/index.js +5 -4
  132. package/glTF/2.0/index.js.map +1 -1
  133. package/glTF/glTFFileLoader.d.ts +433 -373
  134. package/glTF/glTFFileLoader.js +927 -870
  135. package/glTF/glTFFileLoader.js.map +1 -1
  136. package/glTF/glTFValidation.d.ts +29 -29
  137. package/glTF/glTFValidation.js +123 -121
  138. package/glTF/glTFValidation.js.map +1 -1
  139. package/glTF/index.d.ts +5 -5
  140. package/glTF/index.js +6 -5
  141. package/glTF/index.js.map +1 -1
  142. package/index.d.ts +3 -3
  143. package/index.js +4 -3
  144. package/index.js.map +1 -1
  145. package/legacy/legacy-glTF.d.ts +2 -2
  146. package/legacy/legacy-glTF.js +18 -18
  147. package/legacy/legacy-glTF.js.map +1 -1
  148. package/legacy/legacy-glTF1.d.ts +2 -2
  149. package/legacy/legacy-glTF1.js +15 -14
  150. package/legacy/legacy-glTF1.js.map +1 -1
  151. package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
  152. package/legacy/legacy-glTF1FileLoader.js +3 -2
  153. package/legacy/legacy-glTF1FileLoader.js.map +1 -1
  154. package/legacy/legacy-glTF2.d.ts +2 -2
  155. package/legacy/legacy-glTF2.js +33 -32
  156. package/legacy/legacy-glTF2.js.map +1 -1
  157. package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
  158. package/legacy/legacy-glTF2FileLoader.js +3 -2
  159. package/legacy/legacy-glTF2FileLoader.js.map +1 -1
  160. package/legacy/legacy-glTFFileLoader.d.ts +3 -3
  161. package/legacy/legacy-glTFFileLoader.js +4 -3
  162. package/legacy/legacy-glTFFileLoader.js.map +1 -1
  163. package/legacy/legacy-objFileLoader.d.ts +1 -1
  164. package/legacy/legacy-objFileLoader.js +13 -12
  165. package/legacy/legacy-objFileLoader.js.map +1 -1
  166. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  167. package/legacy/legacy-stlFileLoader.js +13 -12
  168. package/legacy/legacy-stlFileLoader.js.map +1 -1
  169. package/legacy/legacy.d.ts +6 -6
  170. package/legacy/legacy.js +8 -6
  171. package/legacy/legacy.js.map +1 -1
  172. package/license.md +71 -0
  173. package/package.json +37 -193
  174. package/readme.md +2 -2
@@ -1,700 +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
- */
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 };
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 };
700
694
  //# sourceMappingURL=solidParser.js.map