Pascal
Pascal

Reputation: 1305

Error while loading an STL file using three.js

I want to show a preview of an STL file using three.js. I followed the following tutorial, because this is exactly what I need:

Tutorial

I get an error called undefined is not a function (near '...loader.addEventListener...') in the following line:

    var loader=new THREE.STLLoader();
    loader.addEventListener('load', function (event){
    var geometry=event.content;
    var material=new THREE.MeshLambertMaterial({ ambient: 0xFBB917,color: 0xfdd017 });
    var mesh=new THREE.Mesh(geometry, material);
    scene.add(mesh);});

I also included all files correctly, what is wrong with my code or is there any alternative for a simple preview of a STL file using javascript?

Upvotes: 0

Views: 759

Answers (1)

Don McCurdy
Don McCurdy

Reputation: 11960

It looks like that tutorial was written with an older version of three.js. For newer versions, when loading STL (and other model formats) you must use a .load function:

var material = new THREE.MeshLambertMaterial({ ambient: 0xFBB917,color: 0xfdd017 });

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

STLLoader.js:L18-30

Upvotes: 3

Related Questions