Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

@anselanza anselanza commented Mar 21, 2018

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

@edwinwebb edwinwebb commented Mar 21, 2018

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

@anselanza anselanza commented Mar 21, 2018

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

@edwinwebb edwinwebb commented Mar 21, 2018

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

@anselanza anselanza commented Mar 22, 2018

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

@edwinwebb edwinwebb commented Mar 27, 2018

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

@anselanza
Copy link
Author

@anselanza anselanza commented Mar 28, 2018

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.)

@anselanza anselanza closed this Mar 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.