Search code examples
autodesk-forgeautodesk-viewerautodesk-model-derivative

Default material for model in Forge Viewer


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?


Solution

  • 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);
            });
        }
    }