0

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?

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.