0

I just started learning THREE.js. While I'm able to make cubes and spheres just fine, whenever I try to load an .obj, it keeps throwing random errors. I'm completely confused.

// instantiate a loader
const loader = new OBJLoader();

// load a resource
loader.load(
    // resource URL
    'models/boat_large.obj',
    // called when resource is loaded
    function ( object ) {

        scene.add( object );

    },
    // called when loading is in progresses
    function ( xhr ) {

        console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

    },
    // called when loading has errors
    function ( error ) {

        console.log( 'An error happened' );

    }
);

I get the error: OBJLoader is not defined

I add <script type="module" src="loaders/OBJLoader.js"></script> to my html, I get the error:

Access to script at 'file:///C:/Users/Syzygy/Desktop/hello/www/loaders/OBJLoader.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.

I've followed all the tutorials I can find, I'm not making any progress

2
  • 2
    Does this answer your question? javascript modules and CORS Commented Jan 29, 2021 at 14:35
  • @UnholySheep Yes and no. I uploaded the file to my webhost, and now gives me the error Uncaught SyntaxError: Cannot use import statement outside a module deadbydawn.io Commented Jan 29, 2021 at 15:11

3 Answers 3

1

Here's an example (based on some three.js code) with all the pieces in place and some animation.


    <html>
      <body>
        <script type="module">
          import * as THREE from "https://unpkg.com/[email protected]/build/three.module.js";
          import { OBJLoader } from "https://unpkg.com/[email protected]/examples/jsm/loaders/OBJLoader.js";
    
          const scene = new THREE.Scene();
          const camera = new THREE.PerspectiveCamera(
            75,
            window.innerWidth / window.innerHeight,
            0.1,
            1000
          );
    
          const renderer = new THREE.WebGLRenderer();
          renderer.setSize(window.innerWidth, window.innerHeight);
          renderer.setClearColor("#ffffff");
          document.body.appendChild(renderer.domElement);
    
          var model;
    
          const loader = new OBJLoader();
          loader.load("obj_model_name.obj", obj => {
            model = obj;
            scene.add(model);
          });
    
          camera.position.set(10, 10, 10);
          camera.lookAt(0, 0, 0);
    
          const animate = function() {
            requestAnimationFrame(animate);
    
            if (model) {
              model.rotation.x += 0.01;
              model.rotation.y += 0.01;
            }
    
            renderer.render(scene, camera);
          };
    
          animate();
        </script>
      </body>
    </html>

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

Comments

0

change your < script > tag to < script type="module" >, so your code could be: < script type="module" > import {OBJLoader} from "./loaders/OBJLoader.js";

const loader = new OBJLoader(); ......

1 Comment

It's already type=module, I also added <script type="module" > import {OBJLoader} from "./loaders/OBJLoader.js"; </script> to my HTML file, still gives me OBJLoader is not defined error
0

Try to add:

import {OBJLoader} from 'three/examples/jsm/loaders/OBJLoader';

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.