Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

How to use OBJLoader? #15

Closed
anselanza opened this issue Mar 21, 2018 · 6 comments
Closed

How to use OBJLoader? #15

anselanza opened this issue Mar 21, 2018 · 6 comments

Comments

@anselanza
Copy link

Forgive me if this is a trivial question, but I am trying to load an OBJ from within the three-seed structure, in a similar way to how the standard examples use ObjectLoader, but can't quite figure out how to get it done.

I import what I assume is the correct loader:

import { Group, OBJLoader } from 'three';

But if I try

const loader = new OBJLoader();

Then I get the error:

TypeError: _threeObjLoader.OBJLoader is not a constructor
@edwinwebb
Copy link
Owner

Not at all. Sadly, this is a common problem using the examples code in this type build system. The README should make a mention of this.

I've used this package in the past to resolve your issue : https://www.npmjs.com/package/three-obj-loader

@anselanza
Copy link
Author

Hmmm interesting. I tried to install that package, too, and seemed to have largely the same issues. I might need to get a bit more familiar with threejs itself I suppose.

@edwinwebb
Copy link
Owner

Try this :)

import { Group, ObjectLoader  } from 'three';
import MODEL from './flower.json';

import * as THREE from 'three';
import OBJLoader from 'three-obj-loader';

export default class Flower extends Group {
  constructor() {
    const loader = new ObjectLoader();
    
    super();

    this.name = 'flower';

    const x = OBJLoader(THREE);
    const y = new THREE.OBJLoader();

    console.log(y.load)

    loader.load(MODEL, (mesh)=>{
      this.add(mesh);
    });
  }
}

logs..

      load(url, onLoad, onProgress, onError) {

      var scope = this;
      this.onError = onError || defaultOnError;

      var loader = new THREE.FileLoader(scope.manager);
      loader.setPath(this.pa…```

@anselanza
Copy link
Author

I think I see what you're getting at, although the example above is still trying to import a JSON model, not an .obj? I really appreciate the time you're giving on this!

Using your example, I got a bit further (no compile or console errors any more, at least):

/**
 * @exports Office
 */

import { Group , ObjectLoader } from 'three';
// import MODEL from './studio_v.obj';

import * as THREE from 'three';
import OBJLoader from 'three-obj-loader';
// import { loadScene } from '../../Loaders/loader';

export default class Office extends Group {
  constructor() {
    
    super();

    OBJLoader(THREE); // wtf...? but has to be used before the line below or "THREE.OBJLoader is not a constructor"
    const loader = new THREE.OBJLoader();

    this.name='office';

    loader.load('./studio_v.obj', (mesh) => {
      this.add(mesh);
    });

  }
}

Now getting a 404 on my studio_v.obj file, so I guess that's a webpack thing. How would I make sure webpack puts the file somewhere accessible? (I can't exactly import it anywhere...)

@edwinwebb
Copy link
Owner

If you 'import' the obj that will give you a URL to load.

@anselanza
Copy link
Author

Wonderful, thank you!

(I had thought somehow that importing from a non-JS file would throw parsing errors, but it seems my understanding of Webpack might be a little deficient.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants