babylon.stlFileLoader.js 7.8 KB

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