babylon.stlFileLoader.js 7.8 KB

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