import_stl.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. let import_stl = function(path, done) {
  2. iron.Data.getBlob(path, function(b) {
  3. let input = new arm.BytesInput(b.bytes);
  4. let header = input.read(80);
  5. if (header.getString(0, 5) == "solid") {
  6. return; // ascii not supported
  7. }
  8. let faces = input.readInt32();
  9. let posTemp = new Float32Array(faces * 9);
  10. let norTemp = new Float32Array(faces * 3);
  11. for (let i = 0; i < faces; ++i) {
  12. let i3 = i * 3;
  13. norTemp[i3 ] = input.readFloat();
  14. norTemp[i3 + 1] = input.readFloat();
  15. norTemp[i3 + 2] = input.readFloat();
  16. let i9 = i * 9;
  17. posTemp[i9 ] = input.readFloat();
  18. posTemp[i9 + 1] = input.readFloat();
  19. posTemp[i9 + 2] = input.readFloat();
  20. posTemp[i9 + 3] = input.readFloat();
  21. posTemp[i9 + 4] = input.readFloat();
  22. posTemp[i9 + 5] = input.readFloat();
  23. posTemp[i9 + 6] = input.readFloat();
  24. posTemp[i9 + 7] = input.readFloat();
  25. posTemp[i9 + 8] = input.readFloat();
  26. input.readInt16(); // attribute
  27. }
  28. let scalePos = 0.0;
  29. for (let i = 0; i < posTemp.length; ++i) {
  30. let f = Math.abs(posTemp[i]);
  31. if (scalePos < f) scalePos = f;
  32. }
  33. let inv = 32767 * (1 / scalePos);
  34. let verts = posTemp.length / 3;
  35. let posa = new Int16Array(verts * 4);
  36. let nora = new Int16Array(verts * 2);
  37. let inda = new Uint32Array(verts);
  38. for (let i = 0; i < verts; ++i) {
  39. posa[i * 4 ] = posTemp[i * 3 ] * inv;
  40. posa[i * 4 + 1] = -posTemp[i * 3 + 2] * inv;
  41. posa[i * 4 + 2] = posTemp[i * 3 + 1] * inv;
  42. nora[i * 2 ] = norTemp[i ] * 32767;
  43. nora[i * 2 + 1] = -norTemp[i + 2] * 32767;
  44. posa[i * 4 + 3] = norTemp[i + 1] * 32767;
  45. inda[i] = i;
  46. }
  47. let name = path.split("\\").pop().split("/").pop().split(".").shift();
  48. done({
  49. name: name,
  50. posa: posa,
  51. nora: nora,
  52. inda: inda,
  53. scale_pos: scalePos,
  54. scale_tex: 1.0
  55. });
  56. iron.Data.deleteBlob(path);
  57. });
  58. }
  59. let plugin = new arm.Plugin();
  60. let formats = arm.Path.meshFormats;
  61. let importers = arm.Path.meshImporters;
  62. formats.push("stl");
  63. importers.h["stl"] = import_stl;
  64. plugin.delete = function() {
  65. formats.splice(formats.indexOf("stl"), 1);
  66. importers.h["stl"] = null;
  67. };