Skip to content

Commit

Permalink
added basic 3mf export (elalish#500)
Browse files Browse the repository at this point in the history
* added basic 3mf export

* updated worker test

* fixes

* formatting

* fix
  • Loading branch information
pca006132 authored Jul 27, 2023
1 parent 5f9b484 commit 59760b3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 21 deletions.
1 change: 1 addition & 0 deletions bindings/wasm/examples/3mf-export.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@jscadui/3mf-export';
31 changes: 23 additions & 8 deletions bindings/wasm/examples/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ function finishRun() {
}

const mv = document.querySelector('model-viewer');
let objectURL = null;
let glbURL = null;
let threeMFURL = null;
let manifoldWorker = null;

function createWorker() {
Expand All @@ -370,10 +371,16 @@ function createWorker() {
finishRun();
runButton.disabled = true;

URL.revokeObjectURL(objectURL);
objectURL = e.data.objectURL;
mv.src = objectURL;
if (objectURL == null) {
if (threeMFURL != undefined) {
URL.revokeObjectURL(threeMFURL);
threeMFURL = undefined;
}
URL.revokeObjectURL(glbURL);
glbURL = e.data.glbURL;
threeMFURL = e.data.threeMFURL;
threemfButton.disabled = threeMFURL == undefined;
mv.src = glbURL;
if (glbURL == null) {
mv.showPoster();
poster.textContent = 'Error';
createWorker();
Expand Down Expand Up @@ -411,10 +418,18 @@ runButton.onclick = function() {
}
};

const downloadButton = document.querySelector('#download');
downloadButton.onclick = function() {
const glbButton = document.querySelector('#glb');
glbButton.onclick = function() {
const link = document.createElement('a');
link.download = 'manifold.glb';
link.href = objectURL;
link.href = glbURL;
link.click();
};

const threemfButton = document.querySelector('#threemf');
threemfButton.onclick = function() {
const link = document.createElement('a');
link.download = 'manifold.3mf';
link.href = threeMFURL;
link.click();
};
5 changes: 3 additions & 2 deletions bindings/wasm/examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
</div>
</div>
<button type="button" id="compile" class="margin green" disabled><span>Run</span></button>
<button type="button" id="download" class="blue margin">Save GLB</button>
<button type="button" id="glb" class="blue margin">Save GLB</button>
<button type="button" id="threemf" class="blue margin">Save 3MF</button>
</div>
<div class="container" style="flex: 2;">
<div id="editor" class="col"></div>
Expand All @@ -74,4 +75,4 @@
<script type="module" src="editor.js"></script>
<script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.1.1/model-viewer.min.js"></script>

</html>
</html>
20 changes: 16 additions & 4 deletions bindings/wasm/examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions bindings/wasm/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"@gltf-transform/core": "^3.2.1",
"@gltf-transform/extensions": "^3.2.1",
"@gltf-transform/functions": "^3.2.1",
"@jscadui/3mf-export": "^0.3.0",
"fflate": "^0.8.0",
"gl-matrix": "^3.4.3",
"simple-dropzone": "0.8.3",
"three": "0.151.2"
Expand Down
12 changes: 6 additions & 6 deletions bindings/wasm/examples/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function initialized(worker) {
});
}

let objectURL = null;
let glbURL = null;

async function runExample(name) {
const worker = new ManifoldWorker();
Expand All @@ -55,12 +55,12 @@ async function runExample(name) {

worker.onmessage = async function(e) {
try {
URL.revokeObjectURL(objectURL);
objectURL = e.data.objectURL;
if (objectURL == null) {
reject('no objectURL');
URL.revokeObjectURL(glbURL);
glbURL = e.data.glbURL;
if (glbURL == null) {
reject('no glbURL)');
}
const docIn = await io.read(objectURL);
const docIn = await io.read(glbURL);
const nodes = docIn.getRoot().listNodes();
for (const node of nodes) {
const mesh = node.getMesh();
Expand Down
25 changes: 24 additions & 1 deletion bindings/wasm/examples/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

import {Document, Material, Node, WebIO} from '@gltf-transform/core';
import {KHRMaterialsUnlit, KHRONOS_EXTENSIONS} from '@gltf-transform/extensions';
import {fileForContentTypes, to3dmodel} from '@jscadui/3mf-export';
import {strToU8, Zippable, zipSync} from 'fflate'
import * as glMatrix from 'gl-matrix';

import Module from './built/manifold';
//@ts-ignore
import {setupIO, writeMesh} from './gltf-io';

import type {GLTFMaterial, Quat} from './public/editor';
import type {CrossSection, Manifold, ManifoldToplevel, Mesh, Vec3} from './public/manifold';

Expand Down Expand Up @@ -457,8 +460,28 @@ async function exportGLB(manifold?: Manifold) {
wrapper.addChild(node);
}

const results: {glbURL?: string, threeMFURL?: string} = {};
if (manifold != null) {
const mesh = manifold.getMesh();
let vertices = new Float32Array(mesh.numVert * 3);
for (let i = 0; i < mesh.numVert; ++i) {
for (let j = 0; j < 3; ++j)
vertices[i * 3 + j] = mesh.vertProperties[i * mesh.numProp + j];
}
const model =
to3dmodel({simple: [{vertices, indices: mesh.triVerts, id: '1'}]});
const files: Zippable = {};
files['3D/3dmodel.model'] = strToU8(model);
files[fileForContentTypes.name] = strToU8(fileForContentTypes.content);
const zipFile = zipSync(files);
results.threeMFURL = URL.createObjectURL(new Blob(
[zipFile],
{type: 'application/vnd.ms-package.3dmanufacturing-3dmodel+xml'}));
}

const glb = await io.writeBinary(doc);

const blob = new Blob([glb], {type: 'application/octet-stream'});
self.postMessage({objectURL: URL.createObjectURL(blob)});
results.glbURL = URL.createObjectURL(blob);
self.postMessage(results);
}

0 comments on commit 59760b3

Please sign in to comment.