babylon.stlFileLoader.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var STLFileLoader = (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.isBinary = function (data) {
  74. // check if file size is correct for binary stl
  75. var faceSize, nFaces, reader;
  76. reader = new DataView(data);
  77. faceSize = (32 / 8 * 3) + ((32 / 8 * 3) * 3) + (16 / 8);
  78. nFaces = reader.getUint32(80, true);
  79. if (80 + (32 / 8) + (nFaces * faceSize) === reader.byteLength) {
  80. return true;
  81. }
  82. // check characters higher than ASCII to confirm binary
  83. var fileLength = reader.byteLength;
  84. for (var index = 0; index < fileLength; index++) {
  85. if (reader.getUint8(index) > 127) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. };
  91. STLFileLoader.prototype.parseBinary = function (mesh, data) {
  92. var reader = new DataView(data);
  93. var faces = reader.getUint32(80, true);
  94. var dataOffset = 84;
  95. var faceLength = 12 * 4 + 2;
  96. var offset = 0;
  97. var positions = new Float32Array(faces * 3 * 3);
  98. var normals = new Float32Array(faces * 3 * 3);
  99. var indices = new Uint32Array(faces * 3);
  100. var indicesCount = 0;
  101. for (var face = 0; face < faces; face++) {
  102. var start = dataOffset + face * faceLength;
  103. var normalX = reader.getFloat32(start, true);
  104. var normalY = reader.getFloat32(start + 4, true);
  105. var normalZ = reader.getFloat32(start + 8, true);
  106. for (var i = 1; i <= 3; i++) {
  107. var vertexstart = start + i * 12;
  108. // ordering is intentional to match ascii import
  109. positions[offset] = reader.getFloat32(vertexstart, true);
  110. positions[offset + 2] = reader.getFloat32(vertexstart + 4, true);
  111. positions[offset + 1] = reader.getFloat32(vertexstart + 8, true);
  112. normals[offset] = normalX;
  113. normals[offset + 2] = normalY;
  114. normals[offset + 1] = normalZ;
  115. offset += 3;
  116. }
  117. indices[indicesCount] = indicesCount++;
  118. indices[indicesCount] = indicesCount++;
  119. indices[indicesCount] = indicesCount++;
  120. }
  121. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  122. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  123. mesh.setIndices(indices);
  124. mesh.computeWorldMatrix(true);
  125. };
  126. STLFileLoader.prototype.parseASCII = function (mesh, solidData) {
  127. var positions = [];
  128. var normals = [];
  129. var indices = [];
  130. var indicesCount = 0;
  131. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  132. var matches;
  133. while (matches = this.facetsPattern.exec(solidData)) {
  134. var facet = matches[1];
  135. //one normal per face
  136. var normalMatches = this.normalPattern.exec(facet);
  137. this.normalPattern.lastIndex = 0;
  138. if (!normalMatches) {
  139. continue;
  140. }
  141. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  142. var vertexMatch;
  143. while (vertexMatch = this.vertexPattern.exec(facet)) {
  144. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  145. normals.push(normal[0], normal[1], normal[2]);
  146. }
  147. indices.push(indicesCount++, indicesCount++, indicesCount++);
  148. this.vertexPattern.lastIndex = 0;
  149. }
  150. this.facetsPattern.lastIndex = 0;
  151. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  152. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  153. mesh.setIndices(indices);
  154. mesh.computeWorldMatrix(true);
  155. };
  156. return STLFileLoader;
  157. }());
  158. BABYLON.STLFileLoader = STLFileLoader;
  159. if (BABYLON.SceneLoader) {
  160. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  161. }
  162. })(BABYLON || (BABYLON = {}));
  163. //# sourceMappingURL=babylon.stlFileLoader.js.map