babylon.stlFileLoader.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var STLFileLoader = /** @class */ (function () {
  5. function STLFileLoader() {
  6. this.solidPattern = /solid (\S*)([\S\s]*)endsolid[ ]*(\S*)/g;
  7. this.facetsPattern = /facet([\s\S]*?)endfacet/g;
  8. this.normalPattern = /normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  9. this.vertexPattern = /vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g;
  10. this.name = "stl";
  11. // force data to come in as an ArrayBuffer
  12. // we'll convert to string if it looks like it's an ASCII .stl
  13. this.extensions = {
  14. ".stl": { isBinary: true },
  15. };
  16. }
  17. STLFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  18. var matches;
  19. if (typeof data !== "string") {
  20. if (this.isBinary(data)) {
  21. // binary .stl
  22. var babylonMesh = new BABYLON.Mesh("stlmesh", scene);
  23. this.parseBinary(babylonMesh, data);
  24. if (meshes) {
  25. meshes.push(babylonMesh);
  26. }
  27. return true;
  28. }
  29. // ASCII .stl
  30. // convert to string
  31. var array_buffer = new Uint8Array(data);
  32. var str = '';
  33. for (var i = 0; i < data.byteLength; i++) {
  34. str += String.fromCharCode(array_buffer[i]); // implicitly assumes little-endian
  35. }
  36. data = str;
  37. }
  38. //if arrived here, data is a string, containing the STLA data.
  39. while (matches = this.solidPattern.exec(data)) {
  40. var meshName = matches[1];
  41. var meshNameFromEnd = matches[3];
  42. if (meshName != meshNameFromEnd) {
  43. BABYLON.Tools.Error("Error in STL, solid name != endsolid name");
  44. return false;
  45. }
  46. // check meshesNames
  47. if (meshesNames && meshName) {
  48. if (meshesNames instanceof Array) {
  49. if (!meshesNames.indexOf(meshName)) {
  50. continue;
  51. }
  52. }
  53. else {
  54. if (meshName !== meshesNames) {
  55. continue;
  56. }
  57. }
  58. }
  59. // stl mesh name can be empty as well
  60. meshName = meshName || "stlmesh";
  61. var babylonMesh = new BABYLON.Mesh(meshName, scene);
  62. this.parseASCII(babylonMesh, matches[2]);
  63. if (meshes) {
  64. meshes.push(babylonMesh);
  65. }
  66. }
  67. return true;
  68. };
  69. STLFileLoader.prototype.load = function (scene, data, rootUrl) {
  70. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  71. if (result) {
  72. scene.createDefaultCameraOrLight();
  73. }
  74. return result;
  75. };
  76. STLFileLoader.prototype.loadAssetContainer = function (scene, data, rootUrl, onError) {
  77. var container = new BABYLON.AssetContainer(scene);
  78. this.importMesh(null, scene, data, rootUrl, container.meshes, null, null);
  79. container.removeAllFromScene();
  80. return container;
  81. };
  82. STLFileLoader.prototype.isBinary = function (data) {
  83. // check if file size is correct for binary stl
  84. var faceSize, nFaces, reader;
  85. reader = new DataView(data);
  86. faceSize = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
  87. nFaces = reader.getUint32(80, true);
  88. if (80 + (32 / 8) + (nFaces * faceSize) === reader.byteLength) {
  89. return true;
  90. }
  91. // check characters higher than ASCII to confirm binary
  92. var fileLength = reader.byteLength;
  93. for (var index = 0; index < fileLength; index++) {
  94. if (reader.getUint8(index) > 127) {
  95. return true;
  96. }
  97. }
  98. return false;
  99. };
  100. STLFileLoader.prototype.parseBinary = function (mesh, data) {
  101. var reader = new DataView(data);
  102. var faces = reader.getUint32(80, true);
  103. var dataOffset = 84;
  104. var faceLength = 12 * 4 + 2;
  105. var offset = 0;
  106. var positions = new Float32Array(faces * 3 * 3);
  107. var normals = new Float32Array(faces * 3 * 3);
  108. var indices = new Uint32Array(faces * 3);
  109. var indicesCount = 0;
  110. for (var face = 0; face < faces; face++) {
  111. var start = dataOffset + face * faceLength;
  112. var normalX = reader.getFloat32(start, true);
  113. var normalY = reader.getFloat32(start + 4, true);
  114. var normalZ = reader.getFloat32(start + 8, true);
  115. for (var i = 1; i <= 3; i++) {
  116. var vertexstart = start + i * 12;
  117. // ordering is intentional to match ascii import
  118. positions[offset] = reader.getFloat32(vertexstart, true);
  119. positions[offset + 2] = reader.getFloat32(vertexstart + 4, true);
  120. positions[offset + 1] = reader.getFloat32(vertexstart + 8, true);
  121. normals[offset] = normalX;
  122. normals[offset + 2] = normalY;
  123. normals[offset + 1] = normalZ;
  124. offset += 3;
  125. }
  126. indices[indicesCount] = indicesCount++;
  127. indices[indicesCount] = indicesCount++;
  128. indices[indicesCount] = indicesCount++;
  129. }
  130. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  131. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  132. mesh.setIndices(indices);
  133. mesh.computeWorldMatrix(true);
  134. };
  135. STLFileLoader.prototype.parseASCII = function (mesh, solidData) {
  136. var positions = [];
  137. var normals = [];
  138. var indices = [];
  139. var indicesCount = 0;
  140. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  141. var matches;
  142. while (matches = this.facetsPattern.exec(solidData)) {
  143. var facet = matches[1];
  144. //one normal per face
  145. var normalMatches = this.normalPattern.exec(facet);
  146. this.normalPattern.lastIndex = 0;
  147. if (!normalMatches) {
  148. continue;
  149. }
  150. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  151. var vertexMatch;
  152. while (vertexMatch = this.vertexPattern.exec(facet)) {
  153. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  154. normals.push(normal[0], normal[1], normal[2]);
  155. }
  156. indices.push(indicesCount++, indicesCount++, indicesCount++);
  157. this.vertexPattern.lastIndex = 0;
  158. }
  159. this.facetsPattern.lastIndex = 0;
  160. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  161. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  162. mesh.setIndices(indices);
  163. mesh.computeWorldMatrix(true);
  164. };
  165. return STLFileLoader;
  166. }());
  167. BABYLON.STLFileLoader = STLFileLoader;
  168. if (BABYLON.SceneLoader) {
  169. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  170. }
  171. })(BABYLON || (BABYLON = {}));
  172. //# sourceMappingURL=babylon.stlFileLoader.js.map