javascriptthree.jsstl-format

How to read triangles and vertices of the model from the STL format?


I need to get information about the vertices and triangles of the STL format model, please tell me how to extract this data in ThreeJS


Solution

  • You can use the STLLoader class for this task. So you define an instance of the loader and use it to load a STL asset from a specific path. In the onLoad() callback, you get an instance of BufferGeometry representing the geometry data from the STL file.

    const loader = new STLLoader();
    loader.load( './models/stl/ascii/slotted_disk.stl', function ( geometry ) {
    
        const material = new THREE.MeshBasicMaterial();
        const mesh = new THREE.Mesh( geometry, material );
    
        scene.add( mesh );
    
    } );
    

    Keep in mind that the geometry is non-indexed. So the faces/triangles are defined by three consecutive vertices in the position buffer attribute.

    Full example: https://threejs.org/examples/webgl_loader_stl