babylon.stlFileLoader.ts 7.8 KB

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