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

Add defaultMaterialLoader function to URDFLoader.js #298

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion javascript/src/URDFLoader.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LoadingManager, Object3D } from 'three';
import { LoadingManager, Object3D, Material, MaterialParameters } from 'three';
import { URDFRobot } from './URDFClasses';

interface MeshLoadDoneFunc {
Expand All @@ -9,10 +9,15 @@ interface MeshLoadFunc{
(url: string, manager: LoadingManager, onLoad: MeshLoadDoneFunc): void;
}

interface MaterialLoadFunc {
<T extends MaterialParameters>(parameters?: T): Material;
}

export default class URDFLoader {

manager: LoadingManager;
defaultMeshLoader: MeshLoadFunc;
defaultMaterialLoader: MaterialLoadFunc;

// options
fetchOptions: Object;
Expand All @@ -21,6 +26,7 @@ export default class URDFLoader {
parseCollision: boolean;
packages: string | { [key: string]: string } | ((targetPkg: string) => string);
loadMeshCb: MeshLoadFunc;
loadMaterialCb: MaterialLoadFunc;

constructor(manager?: LoadingManager);
loadAsync(urdf: string): Promise<URDFRobot>;
Expand Down
28 changes: 17 additions & 11 deletions javascript/src/URDFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class URDFLoader {

this.manager = manager || THREE.DefaultLoadingManager;
this.loadMeshCb = this.defaultMeshLoader.bind(this);
this.loadMaterialCb = this.defaultMaterialLoader.bind(this);
this.parseVisual = true;
this.parseCollision = false;
this.packages = '';
Expand Down Expand Up @@ -459,9 +460,9 @@ class URDFLoader {
function processMaterial(node) {

const matNodes = [ ...node.children ];
const material = new THREE.MeshPhongMaterial();
const parameters = {};

material.name = node.getAttribute('name') || '';
parameters.name = node.getAttribute('name') || '';
matNodes.forEach(n => {

const type = n.nodeName.toLowerCase();
Expand All @@ -473,10 +474,10 @@ class URDFLoader {
.split(/\s/g)
.map(v => parseFloat(v));

material.color.setRGB(rgba[0], rgba[1], rgba[2]);
material.opacity = rgba[3];
material.transparent = rgba[3] < 1;
material.depthWrite = !material.transparent;
parameters.color = new THREE.Color(rgba[0], rgba[1], rgba[2]);
parameters.opacity = rgba[3];
parameters.transparent = rgba[3] < 1;
parameters.depthWrite = !parameters.transparent;

} else if (type === 'texture') {

Expand All @@ -487,15 +488,15 @@ class URDFLoader {

const loader = new THREE.TextureLoader(manager);
const filePath = resolvePath(filename);
material.map = loader.load(filePath);
material.map.colorSpace = THREE.SRGBColorSpace;
parameters.map = loader.load(filePath);
parameters.map.colorSpace = THREE.SRGBColorSpace;

}

}
});

return material;
return this.loadMaterialCb(parameters);

}

Expand Down Expand Up @@ -523,7 +524,7 @@ class URDFLoader {

} else {

material = new THREE.MeshPhongMaterial();
material = this.loadMaterialCb();

}

Expand Down Expand Up @@ -644,7 +645,8 @@ class URDFLoader {

const loader = new STLLoader(manager);
loader.load(path, geom => {
const mesh = new THREE.Mesh(geom, new THREE.MeshPhongMaterial());
const material = this.loadMaterialCb();
const mesh = new THREE.Mesh(geom, material);
done(mesh);
});

Expand All @@ -661,4 +663,8 @@ class URDFLoader {

}

defaultMaterialLoader(parameters) {
return new THREE.MeshPhongMaterial(parameters);
}

};