babylon.stlFileLoader.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.extensions = ".stl";
  11. }
  12. STLFileLoader.prototype.importMesh = function (meshesNames, scene, data, rootUrl, meshes, particleSystems, skeletons) {
  13. var matches;
  14. if (typeof data !== "string") {
  15. BABYLON.Tools.Error("STL format not recognized. Ensure it's ASCII formatted");
  16. return false;
  17. }
  18. while (matches = this.solidPattern.exec(data)) {
  19. var meshName = matches[1];
  20. var meshNameFromEnd = matches[3];
  21. if (meshName != meshNameFromEnd) {
  22. BABYLON.Tools.Error("Error in STL, solid name != endsolid name");
  23. return false;
  24. }
  25. // check meshesNames
  26. if (meshesNames && meshName) {
  27. if (meshesNames instanceof Array) {
  28. if (!meshesNames.indexOf(meshName)) {
  29. continue;
  30. }
  31. }
  32. else {
  33. if (meshName !== meshesNames) {
  34. continue;
  35. }
  36. }
  37. }
  38. // stl mesh name can be empty as well
  39. meshName = meshName || "stlmesh";
  40. var babylonMesh = new BABYLON.Mesh(meshName, scene);
  41. this.parseSolid(babylonMesh, matches[2]);
  42. meshes.push(babylonMesh);
  43. }
  44. return true;
  45. };
  46. STLFileLoader.prototype.load = function (scene, data, rootUrl) {
  47. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  48. if (result) {
  49. scene.createDefaultCameraOrLight();
  50. }
  51. return result;
  52. };
  53. STLFileLoader.prototype.parseSolid = function (mesh, solidData) {
  54. var normals = [];
  55. var positions = [];
  56. var indices = [];
  57. var indicesCount = 0;
  58. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  59. var matches;
  60. while (matches = this.facetsPattern.exec(solidData)) {
  61. var facet = matches[1];
  62. //one normal per face
  63. var normalMatches = this.normalPattern.exec(facet);
  64. this.normalPattern.lastIndex = 0;
  65. if (!normalMatches) {
  66. continue;
  67. }
  68. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  69. var vertexMatch;
  70. while (vertexMatch = this.vertexPattern.exec(facet)) {
  71. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  72. normals.push(normal[0], normal[1], normal[2]);
  73. }
  74. indices.push(indicesCount++, indicesCount++, indicesCount++);
  75. this.vertexPattern.lastIndex = 0;
  76. }
  77. this.facetsPattern.lastIndex = 0;
  78. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  79. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  80. mesh.setIndices(indices);
  81. mesh.computeWorldMatrix(true);
  82. };
  83. return STLFileLoader;
  84. }());
  85. BABYLON.STLFileLoader = STLFileLoader;
  86. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  87. })(BABYLON || (BABYLON = {}));
  88. //# sourceMappingURL=babylon.stlFileLoader.js.map