babylon.stlFileLoader.js 3.9 KB

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