babylon.stlFileLoader.js 3.7 KB

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