stl-exporter.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Sukolsak Sakshuwong <sukolsak@stanford.edu>
  5. */
  6. import { asciiWrite } from '../../mol-io/common/ascii';
  7. import { Box3D } from '../../mol-math/geometry';
  8. import { Vec3, Mat4 } from '../../mol-math/linear-algebra';
  9. import { PLUGIN_VERSION } from '../../mol-plugin/version';
  10. import { RuntimeContext } from '../../mol-task';
  11. import { MeshExporter, AddMeshInput } from './mesh-exporter';
  12. // avoiding namespace lookup improved performance in Chrome (Aug 2020)
  13. const v3fromArray = Vec3.fromArray;
  14. const v3transformMat4 = Vec3.transformMat4;
  15. const v3triangleNormal = Vec3.triangleNormal;
  16. const v3toArray = Vec3.toArray;
  17. // https://www.fabbers.com/tech/STL_Format
  18. export type StlData = {
  19. stl: Uint8Array
  20. }
  21. export class StlExporter extends MeshExporter<StlData> {
  22. readonly fileExtension = 'stl';
  23. private triangleBuffers: ArrayBuffer[] = [];
  24. private triangleCount = 0;
  25. private centerTransform: Mat4;
  26. protected async addMeshWithColors(input: AddMeshInput) {
  27. const { values, isGeoTexture, ctx } = input;
  28. const t = Mat4();
  29. const tmpV = Vec3();
  30. const v1 = Vec3();
  31. const v2 = Vec3();
  32. const v3 = Vec3();
  33. const stride = isGeoTexture ? 4 : 3;
  34. const aTransform = values.aTransform.ref.value;
  35. const instanceCount = values.uInstanceCount.ref.value;
  36. for (let instanceIndex = 0; instanceIndex < instanceCount; ++instanceIndex) {
  37. if (ctx.shouldUpdate) await ctx.update({ current: instanceIndex + 1 });
  38. const { vertices, indices, vertexCount, drawCount } = StlExporter.getInstance(input, instanceIndex);
  39. Mat4.fromArray(t, aTransform, instanceIndex * 16);
  40. Mat4.mul(t, this.centerTransform, t);
  41. // position
  42. const vertexArray = new Float32Array(vertexCount * 3);
  43. for (let i = 0; i < vertexCount; ++i) {
  44. v3transformMat4(tmpV, v3fromArray(tmpV, vertices, i * stride), t);
  45. v3toArray(tmpV, vertexArray, i * 3);
  46. }
  47. // face
  48. const triangleBuffer = new ArrayBuffer(50 * drawCount);
  49. const dataView = new DataView(triangleBuffer);
  50. for (let i = 0; i < drawCount; i += 3) {
  51. v3fromArray(v1, vertexArray, (isGeoTexture ? i : indices![i]) * 3);
  52. v3fromArray(v2, vertexArray, (isGeoTexture ? i + 1 : indices![i + 1]) * 3);
  53. v3fromArray(v3, vertexArray, (isGeoTexture ? i + 2 : indices![i + 2]) * 3);
  54. v3triangleNormal(tmpV, v1, v2, v3);
  55. const byteOffset = 50 * i;
  56. dataView.setFloat32(byteOffset, tmpV[0], true);
  57. dataView.setFloat32(byteOffset + 4, tmpV[1], true);
  58. dataView.setFloat32(byteOffset + 8, tmpV[2], true);
  59. dataView.setFloat32(byteOffset + 12, v1[0], true);
  60. dataView.setFloat32(byteOffset + 16, v1[1], true);
  61. dataView.setFloat32(byteOffset + 20, v1[2], true);
  62. dataView.setFloat32(byteOffset + 24, v2[0], true);
  63. dataView.setFloat32(byteOffset + 28, v2[1], true);
  64. dataView.setFloat32(byteOffset + 32, v2[2], true);
  65. dataView.setFloat32(byteOffset + 36, v3[0], true);
  66. dataView.setFloat32(byteOffset + 40, v3[1], true);
  67. dataView.setFloat32(byteOffset + 44, v3[2], true);
  68. }
  69. this.triangleBuffers.push(triangleBuffer);
  70. this.triangleCount += drawCount;
  71. }
  72. }
  73. async getData() {
  74. const stl = new Uint8Array(84 + 50 * this.triangleCount);
  75. asciiWrite(stl, `Exported from Mol* ${PLUGIN_VERSION}`);
  76. const dataView = new DataView(stl.buffer);
  77. dataView.setUint32(80, this.triangleCount, true);
  78. let byteOffset = 84;
  79. for (const buffer of this.triangleBuffers) {
  80. stl.set(new Uint8Array(buffer), byteOffset);
  81. byteOffset += buffer.byteLength;
  82. }
  83. return { stl };
  84. }
  85. async getBlob(ctx: RuntimeContext) {
  86. return new Blob([(await this.getData()).stl], { type: 'model/stl' });
  87. }
  88. constructor(boundingBox: Box3D) {
  89. super();
  90. const tmpV = Vec3();
  91. Vec3.add(tmpV, boundingBox.min, boundingBox.max);
  92. Vec3.scale(tmpV, tmpV, -0.5);
  93. this.centerTransform = Mat4.fromTranslation(Mat4(), tmpV);
  94. }
  95. }