0

I'd need to have the complete model use a default material (e.g. grey color) and then use externally defined materials for each node.

So I'm looking for some advice on two points: 1) Setting a default material on all nodes. 2) Setting the material / color for given nodes after they're fetched from an external source.

Could this be done at some point before the model is loaded into the viewer? (i.e. server-side)? If not, can it be done in the viewer?

ohra
  • 496
  • 3
  • 12

1 Answers1

1

All geometry coming from Forge will always have some material defined for it, but you can iterate over dbIDs of all objects on the model and set a custom THREE.js material for them using something along these lines:

function setCustomMaterial(viewer, dbids) {
    const material = new THREE.MeshPhongMaterial({
        color: 0xAB00EE,
        specular: 0xEEABEE
    });
    viewer.impl.matman().addMaterial('CustomMaterial', material, true);
    const fragList = viewer.model.getFragmentList();
    const instanceTree = viewer.model.getData().instanceTree;
    for (let dbid of dbids) {
        instanceTree.enumNodeFragments(dbid, function(frag) {
            fragList.setMaterial(frag, material);
        });
    }
}
Petr Broz
  • 8,891
  • 2
  • 15
  • 24
  • Since I only needed to change the material colors I ended up just getting the fragment's current material with fragList.getMaterial, setting its color and setting material.needsUpdate = true. That way I avoided adding a new material altogether. – ohra Feb 12 '19 at 07:48