babylon.stlFileLoader.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. var result = this.importMesh(null, scene, data, rootUrl, null, null, null);
  41. if (result) {
  42. scene.createDefaultCameraOrLight();
  43. }
  44. return result;
  45. };
  46. STLFileLoader.prototype.parseSolid = function (mesh, solidData) {
  47. var normals = [];
  48. var positions = [];
  49. var indices = [];
  50. var indicesCount = 0;
  51. //load facets, ignoring loop as the standard doesn't define it can contain more than vertices
  52. var matches;
  53. while (matches = this.facetsPattern.exec(solidData)) {
  54. var facet = matches[1];
  55. //one normal per face
  56. var normalMatches = this.normalPattern.exec(facet);
  57. this.normalPattern.lastIndex = 0;
  58. if (!normalMatches) {
  59. continue;
  60. }
  61. var normal = [Number(normalMatches[1]), Number(normalMatches[5]), Number(normalMatches[3])];
  62. var vertexMatch;
  63. while (vertexMatch = this.vertexPattern.exec(facet)) {
  64. positions.push(Number(vertexMatch[1]), Number(vertexMatch[5]), Number(vertexMatch[3]));
  65. normals.push(normal[0], normal[1], normal[2]);
  66. }
  67. indices.push(indicesCount++, indicesCount++, indicesCount++);
  68. this.vertexPattern.lastIndex = 0;
  69. }
  70. this.facetsPattern.lastIndex = 0;
  71. mesh.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions);
  72. mesh.setVerticesData(BABYLON.VertexBuffer.NormalKind, normals);
  73. mesh.setIndices(indices);
  74. mesh.computeWorldMatrix(true);
  75. };
  76. return STLFileLoader;
  77. }());
  78. BABYLON.STLFileLoader = STLFileLoader;
  79. BABYLON.SceneLoader.RegisterPlugin(new STLFileLoader());
  80. })(BABYLON || (BABYLON = {}));