babylon.stlFileLoader.js 7.4 KB

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