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
469 stars 121 forks source link

How to display urdf model ? #263

Closed DDman1984 closed 1 year ago

DDman1984 commented 1 year ago

hi, I have a urdf and it describe the mtl and obj link relative, I can load the urdf and obj and mtl by below, but how to add this on scene?

Loading the robot object directly on the scene doesn't show anything.

Should I wait for all obj downloads to complete?

How to use onComplete callback?

I try to cache each obj and display them, but their positions are displayed abnormally. How do I make them display based on each link position parameter of urdf?

` const loadArm = async () => { const manager = new LoadingManager(); const loader = new URDFLoader(manager);

loader.packages =
  "xxxxx path";

loader.loadMeshCb = function (path, manager, onComplete) {
  const mtlLoader = new MTLLoader(manager);
  const objLoader = new OBJLoader(manager);
  const mtlpath = path.replace(".obj", ".mtl");
  mtlLoader.load(mtlpath, (materialCreator) => {
    materialCreator.preload();
    objLoader.setMaterials(materialCreator);
    objLoader.load(
      path,
      (result) => {
        onComplete(result);
      },
      undefined,
      (err) => {
        // try to load again, notify user, etc
        onComplete(null, err);
      }
    );
  });
};

const robot = await loader.loadAsync("xxxxx path");

`

gkjohnson commented 1 year ago

Have you read the documentation? It shows very specifically how to use the onComplete callback and add it to the scene:

import { LoadingManager } from 'three';
import URDFLoader from 'urdf-loader';

// ...init three.js scene...

const manager = new LoadingManager();
const loader = new URDFLoader( manager );
loader.packages = {
    packageName : './package/dir/'            // The equivalent of a (list of) ROS package(s):// directory
};
loader.load(
  'T12/urdf/T12.URDF',                    // The path to the URDF within the package OR absolute
  robot => {

    // The robot is loaded!
    scene.add( robot );

  }
);