N
N
node_js2021-05-31 15:43:12
three.js
node_js, 2021-05-31 15:43:12

How to filter by type with threejs output?

How to set input .file to filter by file format and display ?
I use these formats for the project (.stl, .obj, .stp, .step, .igs, .iges, .zip) Do I
use this to upload a file?

var input = document.getElementById( 'input' );
input.addEventListener( 'change', function( event ) {
var file = this.files[ 0 ];
var reader = new FileReader();
reader.addEventListener( 'load', function ( event ) {
var contents = event.target.result;
var geometry = new STLLoader().parse( contents );
var material = new THREE.MeshStandardMaterial();
var mesh = new THREE.Mesh( geometry, material);
mesh.receiveShadow = true;
mesh.castShadow = true;
camera.position.set(0, 480, 0);
scene.add( mesh );
}, false );
if ( reader.readAsBinaryString !== undefined ) {
reader.readAsBinaryString( file );
} else {
reader.readAsArrayBuffer( file );
}
});

How to set conditions by type?
oadArbitraryModel("path/to/file.json");

loadArbitraryModel = function(path) {
    var strings = path.split('.'); //now the path is ["path/to/file", "json"]
    var types = ['json', 'obj', 'stl', 'babylon', 'collada'];
    var result = -1;
    types.forEach(function (item,index) {
        if (strings.length > 1) {
            if (strings[1] == item){ //strings[1] would be anything after the '.'
                result = index;
            }
        }
    });
    switch (result){
        case -1:
            alert("file format not supported/ improperly named");
        case 0:
            loadJSON(path);
            break;
        case 1:
            loadOBJ(path);
            break;
        case 3:
            loadSTL(path);
            break;
            //ETC etc etc
    }
}

Through this script, outputs via json. but I don’t understand how to implement it not through json,

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question