gkjohnson / urdf-loaders

URDF Loaders for Unity and THREE.js with example ATHLETE URDF Files open sourced from NASA JPL
https://gkjohnson.github.io/urdf-loaders/javascript/example/bundle/index.html
Apache License 2.0
471 stars 122 forks source link

Add defaultMaterialLoader function to URDFLoader.js #298

Closed sunjiang0018 closed 3 weeks ago

sunjiang0018 commented 3 weeks ago

Currently URDFLoader can only load MeshPhongMaterial, and cannot load other materials (MeshStandardMaterial, etc.). Add defaultMeshLoader method to load more materials

gkjohnson commented 3 weeks ago

Hello! Thanks for the contribution. Is there a reason you need a replacement function like this rather than postprocessing the model once it has been loaded? Something like so:

const loader = new URDFLoader();
loader.loadAsync( url ).then( robot => {

  robot.traverse( c => {

    if ( c.material ) {

      const mat = c.material;
      c.material = new MeshStandardMaterial( {
        color: mat.color, 
        opacity: mat.opacity, 
        depthWrite: mat.depthWrite, 
        transparent: mat.transparent, 
      } );

    }

  } );

} );

edit: heh - closed right as a commented 😅

sunjiang0018 commented 3 weeks ago

#

Hello! Thanks for the contribution. Is there a reason you need a replacement function like this rather than postprocessing the model once it has been loaded? Something like so:

const loader = new URDFLoader();
loader.loadAsync( url ).then( robot => {

  robot.traverse( c => {

    if ( c.material ) {

      const mat = c.material;
      c.material = new MeshStandardMaterial( {
        color: mat.color, 
        opacity: mat.opacity, 
        depthWrite: mat.depthWrite, 
        transparent: mat.transparent, 
      } );

    }

  } );

} );

edit: heh - closed right as a commented 😅

oh, just saw😅. it seems to solve my problem, although it feels a bit inelegant. Thanks anyway

gkjohnson commented 3 weeks ago

it feels a bit inelegant. Thanks anyway

If there are simple solutions that don't require internal modifications to that class I think it's best. It's easy to wrap this into a custom loader wrapper, as well.

If there's more demand for this or some functionality that's not possible then I'm happy to discuss and reconsider. Thanks again for the contribution!