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

obj and mtl importing #363

Open
1 of 4 tasks
jyy1082 opened this issue Dec 24, 2017 · 19 comments
Open
1 of 4 tasks

obj and mtl importing #363

jyy1082 opened this issue Dec 24, 2017 · 19 comments

Comments

@jyy1082
Copy link

jyy1082 commented Dec 24, 2017

Does whs.js support to import obj and mtl files with image resource? If yes, is there anyone can guide me how to use WHS.Importer to add those into WHS.App?

Thank you!

Version:
  • v2.1.9
Issue type:
  • Bug
  • Proposal/Enhancement
  • [X ] Question
  • Discussion

Desktop
  • [X ] Chrome
@sasha240100
Copy link
Member

Hey, @jyy1082 , sorry for delayed answer.
TL;DR: It's possible, and here's how:

Importing obj

const object = new WHS.Importer({
  loader: new THREE.OBJLoader(), // you need to provide obj loader there
  // ... 
});

Importing mtl

You may write your own module, which can be reusable then, but the simplest way is to use three-mtl-loader and then:

mtlLoader.load('model.mtl', function(matl) {
  const object = new WHS.Importer({
    material: matl
    // ... 
  });

  // ...
});

And the final stage, add this to app:

object.addTo(app);

@jyy1082
Copy link
Author

jyy1082 commented Dec 27, 2017

Hi,
Thanks for your replay!
My obj and mtl files are for one model. Can I import them together as a object using one WHS.Importer?

@sasha240100
Copy link
Member

sasha240100 commented Dec 28, 2017 via email

@jyy1082
Copy link
Author

jyy1082 commented Dec 28, 2017

I mean one obj and one mtl files together for one model.

https://www.learnthreejs.com/load-3d-model-using-three-js-obj-loader/

This is the way to use THREEJS to implement. How should I use WHS to do that? Thanks.

@sasha240100
Copy link
Member

@jyy1082 See the code above, it's pretty similar with what you need

@jyy1082
Copy link
Author

jyy1082 commented Dec 28, 2017

I will try it. Thanks again!

@hirako2000
Copy link
Collaborator

does it work @jyy1082 ?

@antoniocapelo
Copy link

antoniocapelo commented Jan 26, 2018

I'm also struggling a bit with this.
This snippet works when using with vanilla ThreeJS loader:

        var loader = newTHREE.OBJLoader();
        loader.load( 'o.obj', ( object ) => {
            this.scene.add( object );
        });

However, when doing the 'same' thing with WHS Importer:

        new WHS.Importer({
            url: 'o.obj',
            loader: new OBJLoader(),

            parser(geometry, material) { // material comes as undefined since i'm not loading any .mtl file
                return new THREE.Mesh(geometry, new THREE.MeshNormalMaterial({
                    color: 0xff0000,
                }));
            },

            position: [0, 100, 0]
        }).addTo(this.app);

I got an error in a three.module.js codebase TypeError: Cannot read property 'center' of undefined at Sphere.copy (three.module.js?8bc5:8345)

image

Currently I'm using the vanilla approach but would like how to integrate better with WhiteStorm.
Cheers

@sasha240100
Copy link
Member

@antoniocapelo, since you’ve moved to OBJLoader, your parser() accept other arguments.

In three.js you have: loader.load(‘o.js’, parser); and in your code instead of parser you have an arrow function with object as input data. Same in whs.

parser arguments:
JSONLoader: (geometry, material)
OBJLoader: (object)

Should look like this:

parser(object) {
  return parser;
}

@sasha240100
Copy link
Member

parser(object) {
  return object;
}

Correct one. (made a typo in my prev msg)

@antoniocapelo
Copy link

antoniocapelo commented Feb 5, 2018

Sorry for the delay @sasha240100 ! I tried what you described and it does work (not when loading the .mtl file at the same time though — But the 'vanilla' way I described is looking good until now, so that's covered).

Thanks!

@noahcoetsee
Copy link
Contributor

@sasha240100 I am once again volunteering myself to create a more permanent module for loading these models in (unless someone's already done it?).

@sasha240100
Copy link
Member

sasha240100 commented Mar 2, 2018 via email

@noahcoetsee
Copy link
Contributor

Yeah, that sounds great! Where's the source?

@sasha240100
Copy link
Member

@thecodecrafter Sorry for late response. I wasn't able to find the source code (probably lost it because of not using git for that project). I could help you in development, just point me

@noahcoetsee
Copy link
Contributor

Ok, I will try to remember to do this in the near future. My programming world is a hellish nightmare of projects (abandoned and current) everywhere right now. Will return when ready, sir! ;)

@zardilior
Copy link

This issue should be closed as it looks solved

@hirako2000
Copy link
Collaborator

Agreed @zardilior but it would be nice to add an example to illustrate how to do it, anyone volunteers to add a simple example?

@noahcoetsee
Copy link
Contributor

I'm unable to at the moment, as I'm super busy with school. I'll get back to you during late December!

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

No branches or pull requests

6 participants