bimspot / xeokit-react

Integratation of the xeokit viewer into a React application.
32 stars 22 forks source link

How to load GLTF files with texture? #1

Closed chloesun closed 5 years ago

chloesun commented 5 years ago

Thanks for creating the react component and sharing with the community! From the example, this is how you reference the src

export const sampleModel1 = {
  id: 'duplex',
  src: './models/duplex/scene.gltf',
  metaModelSrc: './metaModels/duplex/metaModel.json',
  edges: true
};

For the GLTF files with texture, (gltf, bin, png), how to load them? spec

xeolabs commented 5 years ago

@chloesun the GLTFLoaderPlugin has a performance configuration - this is normally set true to load large models into the viewer for high-performance non-realistic engineering-style rendering without textures.

Set that false to load models into xeokit's lower-performance flavor of scene representation, which supports glTF 2 materials with physically-based rendering, textures etc.

Note that's going to freak your browser out if you try to load a hugely detailed model with that configuration. Best for a few thousand objects, not good for hundreds of thousands.

const gltfLoader = new GLTFLoaderPlugin(viewer);

const model = gltfLoader.load({
        id: "myModel",
        src: "./models/gltf/schependomlaan/scene.gltf",
        metaModelSrc: "./metaModels/schependomlaan/metaModel.json",

        performance: false // <<--------- Set false to enable full materials
    });
chloesun commented 5 years ago

Here is where my GLTF stored, right under public directory: folder

I added this in Models.js

export const sampleModel3 = {
  id: 'cbr',
  src: './cbr_room/cbr.gltf',
  performance: false
};

I got a blank canvas with the error messages like this, note I have no problem rendering models with gltf and json (like your sample models) 3

If I removed the performance: false, I can see the spinner, but with the same error, like this: 2

What could be wrong?

eriadam commented 5 years ago

Usually "unexpected token < in JSON at position 0" means that the server gives you back a 404 error and an html response — instead of JSON. Maybe the path to the gltf file is not correct or you don't have read permissions?

chloesun commented 5 years ago

@eriadam I think the path is correct, and I can render this model at https://gltf-viewer.donmccurdy.com/, so I think there is no permission issue either...

eriadam commented 5 years ago

@chloesun In your web inspector look into the resources being loaded and the network logs, you should see the request for the gltf file and the response as well. I am pretty sure the response is not a proper gltf. Note that the JSON loader throws the error, not the renderer.

chloesun commented 5 years ago

@eriadam thanks for the speedy response, the status code is 304 network

eriadam commented 5 years ago

@chloesun Cool, what is in the Preview/Response tabs?

chloesun commented 5 years ago

@eriadam response: response preview preview

eriadam commented 5 years ago

Yap, it's html alright. When you request http://localhost:1234/cbr_room/cbr.gltf even simply from the browser, your local server needs to return the gltf file. I am not sure about your local setup, maybe some reverse proxy or web server config issue.

barnabasmolnar commented 5 years ago

@chloesun

Are you by any chance using Parcel to serve your app? I see your css and js files have these hashes that I know Parcel generates when bundling your app, although other bundlers may do that too, of course. In any case, @eriadam is almost certainly right in that the gltf file is not being served properly. If you're using a bundler like Parcel that serves static files with these hashes, then your gltf file should have one too.

I've mostly relied on create react app to serve react applications so I'm not sure I can be of much help on this one. If you are, however, willing to share a bit more on how you're bundling your app, we might at the very least be able to help point you in the right direction.

chloesun commented 5 years ago

I'm using Parcel to bundle the app

barnabasmolnar commented 5 years ago

Might be worth asking the parcel guys about this.

This is what a quick search yielded for me, not sure if this is relevant to your use-case: https://github.com/parcel-bundler/parcel/issues/1080

Maybe have a look at the last comment and the plugin mentioned there: https://www.npmjs.com/package/parcel-plugin-static-files-copy

Honestly though, I'm only shooting in the dark here, I've never actually used Parcel to serve a React app. :blush: Please do let us know if you manage to figure it out.

chloesun commented 5 years ago

@barnabasmolnar Thanks for the guidance. I've tried this library https://github.com/tiaanduplessis/parcel-plugin-asset-copier, to solve the servicing static files like gltf with Parcel, based on this discussion https://github.com/parcel-bundler/parcel/issues/1411

The JSON error is gone, and I saw this error from the console: index

Since I want to reference gltf model with gltf bin and png, and I think I only need to add one gltf src here :

export const sampleModel3 = {
  id: 'cbr',
  src: 'cbr_room/cbr.gltf',
  performance: false
};

If I remove the performance:false tag, there is no error, and the gltf and bin file loaded properly, but there is nothing showed in the viewer view

Somehow, this approach can reference the geometry, but still not the texture. I also tested with your newly updated code, it is the same behaviour.

chloesun commented 5 years ago

Also, thanks for updating the components with more features!

chloesun commented 5 years ago

This PR will solve the issue https://github.com/xeokit/xeokit-sdk/pull/117