K2xL
K2xL

Reputation: 10320

threejs STL Exporter instancedmeshes

When I export an InstancedMesh to STL, the STL file doesn't seem to show the whole mesh.

            renderer.render(scene, camera);
            var exporter = new STLExporter();
            var str = exporter.parse(scene); // Export the scene
            var blob = new Blob([str], { type: 'text/plain' }); // Generate Blob from the string
            var link = document.createElement('a');
            link.style.display = 'none';
            document.body.appendChild(link);
            link.href = URL.createObjectURL(blob);
            link.download = 'Scene.stl';
            link.click();

How can I get STLExporter to work with InstanceMeshes?

Upvotes: 0

Views: 295

Answers (1)

K2xL
K2xL

Reputation: 10320

Just figured it out. You have to export to a new Mesh.

@Mugen87 from this thread https://github.com/mrdoob/three.js/issues/18572#issuecomment-583745323 shared a nice function that worked for me.

THREE.SceneUtils = {

    createMeshesFromInstancedMesh: function ( instancedMesh ) {

        var group = new THREE.Group();

        var count = instancedMesh.count;
        var geometry = instancedMesh.geometry;
        var material = instancedMesh.material;

        for ( var i = 0; i < count; i ++ ) {

            var mesh = new THREE.Mesh( geometry, material );

            instancedMesh.getMatrixAt( i, mesh.matrix );
            mesh.matrix.decompose( mesh.position, mesh.quaternion, mesh.scale );

            group.add( mesh );

        }

        group.copy( instancedMesh );
        group.updateMatrixWorld(); // ensure correct world matrices of meshes

        return group;

    },

        //

};```

Upvotes: 1

Related Questions