Capítulo 201 de 859

Chapter 201: STLLoader

Core Idea

A loader for the STL format, as created by Solidworks and other CAD programs.

Supports both binary and ASCII encoded files. The loader returns a non-indexed buffer geometry.

Limitations:

const loader = new STLLoader();
const geometry = await loader.loadAsync( './models/stl/slotted_disk.stl' )
scene.add( new THREE.Mesh( geometry ) );

For binary STLs geometry might contain colors for vertices. To use it:

// use the same code to load STL as above
if ( geometry.hasColors ) {
	material = new THREE.MeshPhongMaterial( { opacity: geometry.alpha, vertexColors: true } );
}
const mesh = new THREE.Mesh( geometry, material );

For ASCII STLs containing multiple solids, each solid is assigned to a different group. Groups can be used to assign a different color by defining an array of materials with the same length of geometry.groups and passing it to the Mesh constructor:

const materials = [];
const nGeometryGroups = geometry.groups.length;
for ( let i = 0; i < nGeometryGroups; i ++ ) {
	const material = new THREE.MeshPhongMaterial( { color: colorMap[ i ], wireframe: false } );
	materials.push( material );
}
const mesh = new THREE.Mesh(geometry, materials);

Code Examples

import { STLLoader } from 'three/addons/loaders/STLLoader.js';
  • What it demonstrates: Typical usage of STLLoader.

Reference Tables

Constructor

Signature
new STLLoader( manager : LoadingManager )

Methods

MethodDescription
.load( url : string, onLoad : function, onProgress : onProgressCallback, onError : onErrorCallback )Starts loading from the given URL and passes the loaded STL asset to the onLoad() callback.
.parse( data : ArrayBuffer ) : BufferGeometryParses the given STL data and returns the resulting geometry.

Key Takeaways

  1. STLLoader extends: Loader
  2. Key methods: load, parse.

Connects To