0

I am trying to code an STL model viewer in three.js

This is the code:

<!-- prerenderer -->
<!doctype html>
<html>
    
    <head>
        <title>Enjoy your model</title>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">

<script src="../libs/three.js"></script>
    <script src="../libs/OrbitControls.js"></script>

        
        <script src="../libs/STLLoader.js"></script >
        <style type="text/css">
            body        { padding-top:10px; word-wrap:break-word; width: 100vw; height: 100vh; margin: 0; overflow: hidden;}
            form        { text-align: center; }
        </style>
    </head>

    <body>

        
                <div >
                    <form action="/">
                        <input type="submit" value="Back to the homepage!" />
                    </form>
                </div>  
            

        <!-- Div which will hold the Output -->
        <div id="WebGL-output">
        </div>



        <script>

        

                var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera( 50, window.innerWidth/window.innerHeight, 1, 1000 );

        var renderer = new THREE.WebGLRenderer();
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.getElementById("WebGL-output").appendChild(renderer.domElement); 

        var controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.center =  new THREE.Vector3(
            );


        var loader = new THREE.STLLoader();
            loader.load( 'myModel.stl', function ( geometry ) {

                Var mesh = new THREE.Mesh (geometry);
                Mesh.scale.set (0.1, 0.1, 0.1);
                // mesh.rotation.set (- Math.PI / 2, Math.PI / 2, 0);
                // mesh.scale.set (0.3, 0.3, 0.3);
                // mesh.receiveShadow = true;
                Scene.add (mesh);
            } );


        camera.position.z = 5;

        var animate = function () {
            requestAnimationFrame( animate );


            renderer.render(scene, camera);
        };

        animate();


        </script>


        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script >
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="../libs/bootstrap.min.js"></script>
        
    </body>

</html>

What i get is Uncaught SyntaxError: Cannot use import statement outside a module for Orbit Controls and STLLoader.

What's even weirder is that sometimes it works, other times it does not

Then i tried this:

<!DOCTYPE html>

<html>

<head>
    <title>Example 01.02 - First Scene</title>
    <script type="text/javascript" src="../libs/three.js"></script>
    <script src="../libs/OrbitControls.js"></script>
    <script src="../libs/STLLoader.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.6/dat.gui.min.js"></script>
    <style>
        body {
            /* set margin to 0 and overflow to hidden, to go fullscreen */
            margin: 0;
            overflow: hidden;
        }
    </style>
</head>
<body>

<!-- Div which will hold the Output -->
<div id="WebGL-output">
</div>

<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">


            // Necessary for camera/plane rotation
            var degree = Math.PI/180;

            // Setup
            var scene = new THREE.Scene();
            var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
            var renderer = new THREE.WebGLRenderer();

            renderer.setSize(window.innerWidth, window.innerHeight);
            
            //bind rendered to the dom element
            document.getElementById("WebGL-output").appendChild(renderer.domElement);  

            // Resize after viewport-size-change
            window.addEventListener("resize", function () {
                var height = window.innerHeight;
                var width = window.innerWidth;
                renderer.setSize(width, height);
                camera.aspect = width / height;
                camera.updateProjectionMatrix();
            });

            // Adding controls
            controls = new THREE.OrbitControls(camera, renderer.domElement);

            // Ground (comment out line: "scene.add( plane );" if Ground is not needed...)
            var plane = new THREE.Mesh(
                new THREE.PlaneBufferGeometry(500, 500 ),
                new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
            );
            plane.rotation.x = -90 * degree;
            plane.position.y = 0;
            scene.add( plane );
            plane.receiveShadow = true;

            // ASCII file - STL Import
            var loader = new THREE.STLLoader();
            loader.load( '../esp32.stl', function ( geometry ) {
                var material = new THREE.MeshLambertMaterial( { color: 0xFFFFFF, specular: 0x111111, shininess: 200 } );
                var mesh = new THREE.Mesh( geometry, material );
                mesh.position.set( 0, 0, 0);
                scene.add( mesh );
            } );

            // Binary files - STL Import
            loader.load( '../esp32.stl', function ( geometry ) {
                var material = new THREE.MeshLambertMaterial( { color: 0xFFFFFF, specular: 0x111111, shininess: 200 } );
                var mesh = new THREE.Mesh( geometry, material );
                mesh.position.set( 0, 20, 0);
                scene.add( mesh );
            } );

            // Camera positioning
            camera.position.z = 100;
            camera.position.y = 100;
            camera.rotation.x = -45 * degree;

            // Ambient light (necessary for Phong/Lambert-materials, not for Basic)
            var ambientLight = new THREE.AmbientLight(0xffffff, 1);
            scene.add(ambientLight);

            // Draw scene
            var render = function () {
                renderer.render(scene, camera);
            };

            // Run game loop (render,repeat)
            var GameLoop = function () {
                requestAnimationFrame(GameLoop);

                render();
            };

            GameLoop();

</script>
</body>
</html>

And i get a black screen and

Uncaught TypeError: THREE.STLLoader is not a constructor
    at test2.html:65

and

Uncaught ReferenceError: module is not defined
    at STLLoader.js:32

All i want is to load an stl file and have some orbit controls to rotate around it.

EDIT: Look at all these errors that pop up for god knows why:

Uncaught SyntaxError: Cannot use import statement outside a module
ot OrbitControls
Uncaught ReferenceError: module is not defined
    at STLLoader.js:32
test2.html:51 Uncaught TypeError: THREE.OrbitControls is not a constructor
    at test2.html:51

How hard can it be to just even load a model?

EDIT 2: I am doing everything BY THE BOOK, and new errors pop up like :

Uncaught SyntaxError: The requested module '../libs/STLLoader.js' does not provide an export named 'STLLoader'
user1584421
  • 3,499
  • 11
  • 46
  • 86
  • It seems you are importing ES6 modules with `` which is not allowed. I suggest you make yourself more familiar with ES6 modules. – Mugen87 Sep 17 '20 at 14:33
  • @Mugen87 Once again, thank you Mugen. I am trying to figure out the problem right now. – user1584421 Sep 17 '20 at 14:44

1 Answers1

2

Sounds similar to Uncaught SyntaxError: Cannot use import statement outside a module

Depending on how you include OrbitControls and STLLoader, via <script> or import, you will need to copy from 'examples/js' or 'examples/jsm' respectively.

Terry G Lorber
  • 2,932
  • 2
  • 23
  • 33