OBJLoader doesn't work

I have included the three.js file in my HTML page, but I cannot seem to use OBJLoader?

I downloaded the master zip, and looked in the src folder but couldn’t find it in there either?

All loaders are located here:

And need to be included separately to the main three.js file.

1 Like

Thanks.

Might be worthwhile updating the documentation to explain this.

@paulcanning the documentation is a community project. If you feel that something is missing then please add it :slightly_smiling_face:

BTW each docs page has an “Edit” button in the top right.

It’s strange that they are located in a different folder. Plus, I took a look at the code. I saw THREE.OBJLoader = (function() { ... }), which is very unfriendly to npm user who import code with CommonJS or ES6 import.

@chenyong have a read through this issue thread:

1 Like

Seems we still need to wait for next release to have ES6 module support.

If you’re using CommonJS, this should work fine:

var THREE = require ('three');
require('three/examples/js/loaders/OBJLoader');

var loader = new THREE.OBJLoader();

For ES6, yeah, there is no easy answer right now. Probably easiest to copy the files you need, add export lines, and make sure THREE is in the global namespace.

2 Likes

Trying. Acutally in ClojureScript, but with CommonJS/ES6 modules support. I’m copying the files and modifying it.

Normally we dont use global variables like window.THREE, but turned our three.js prefers writing in this way?

Well, files in examples/js/* depend on window.THREE anyway. I think it’s an eventual goal to change that…

1 Like

macOS 10.15.7, Safari 13.1.3

Me too, I can’t make OBJLoader work. I downloaded mrdoob/three.js and put the whole folder on my localhost folder. On my index.html file I write

I can quite build and animate a sphere with lights and textures (really cool!) but if I add the line

const loader = new OBJLoader();

I get this error message on the console

ReferenceError: Can’t find variable: OBJLoader

I even tried to delete the line

and add (as I have seen this suggestion)

var THREE = require (‘three’);
require(‘three/examples/js/loaders/OBJLoader’);
var loader = new THREE.OBJLoader();

but now I get the error message

ReferenceError: Can’t find variable: require

If I use
import * as THREE from ‘three/build/three.module.js’;
I get the error
SyntaxError: Unexpected token ‘*’. import call expects exactly one argument.

If I use
import { OBJLoader } from ‘three/examples/js/loaders/OBJLoader.js’;
I get the error
SyntaxError: Unexpected token ‘{’. import call expects exactly one argument.

I’m puzzled.

I guess that here I miss something. May you please help? Thank you.