babylon.stlFileLoader.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 extensions = ".stl";
  9. public importMesh(meshesNames: any, scene: Scene, data: any, rootUrl: string, meshes: AbstractMesh[], particleSystems: ParticleSystem[], skeletons: Skeleton[]): boolean {
  10. var matches;
  11. while (matches = this.solidPattern.exec(data)) {
  12. var meshName = matches[1];
  13. var meshNameFromEnd = matches[3];
  14. if (meshName != meshNameFromEnd) {
  15. console.log("error in stl, solid name != endsolid name");
  16. }
  17. //check meshesNames
  18. if (meshesNames && meshName) {
  19. if (meshesNames instanceof Array) {
  20. if (!meshesNames.indexOf(meshName)) {
  21. continue;
  22. }
  23. } else {
  24. if (meshName !== meshesNames) {
  25. continue;
  26. }
  27. }
  28. }
  29. //stl mesh name can be empty as well
  30. meshName = meshName || "stlmesh";
  31. var babylonMesh = new Mesh(meshName, scene);
  32. this.parseSolid(babylonMesh, matches[2]);
  33. }
  34. return true;
  35. }
  36. public load(scene: Scene, data: string, rootUrl: string): boolean {
  37. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  38. if (result) {
  39. scene.createDefaultCameraOrLight();
  40. }
  41. return result;
  42. }
  43. private parseSolid(mesh: Mesh, solidData: string) {
  44. var normals = [];
  45. var positions = [];
  46. var indices = [];
  47. var indicesCount = 0;
  48. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  49. var matches;
  50. while (matches = this.facetsPattern.exec(solidData)) {
  51. var facet = matches[1];
  52. //one normal per face
  53. var normalMatches = this.normalPattern.exec(facet);
  54. this.normalPattern.lastIndex = 0;
  55. if (!normalMatches) {
  56. continue;
  57. }
  58. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  59. var vertexMatch;
  60. while (vertexMatch = this.vertexPattern.exec(facet)) {
  61. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  62. normals.push(normal[0], normal[1], normal[2]);
  63. }
  64. indices.push(indicesCount++, indicesCount++, indicesCount++);
  65. this.vertexPattern.lastIndex = 0;
  66. }
  67. this.facetsPattern.lastIndex = 0;
  68. mesh.setVerticesData(VertexBuffer.PositionKind, positions);
  69. mesh.setVerticesData(VertexBuffer.NormalKind, normals);
  70. mesh.setIndices(indices);
  71. mesh.computeWorldMatrix(true);
  72. }
  73. }
  74. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  75. }