问题
To test out a PoC, I'm trying to colour a selected element red. To do this I have created a class, as below, but nothing happens to the elements when I select them. I've tried a number of the examples around the web (including some which create a mesh proxy for the render proxy and add an overlay) but nothing works.
How do I change the colour for a given element (either dbId
or fragId
)? I can't find API documentation for most of this in the Forge API, so I'm flying a little blind.
/* global Autodesk */
import * as three from "three";
import * as uuid from "uuid";
type SelectionChangedEvent = {
fragIdsArray: number[];
dbIdArray: number[];
nodeArray: number[];
model: object;
};
export default class ViewerInteractionHandler {
viewer: Autodesk.Viewing.Private.GuiViewer3D;
material: THREE.Material;
constructor(viewer: Autodesk.Viewing.Private.GuiViewer3D) {
this.viewer = viewer;
viewer.addEventListener(
Autodesk.Viewing.SELECTION_CHANGED_EVENT,
(e) => this.handleSelectionChange(e)
);
this.material = new three.MeshStandardMaterial({
name: "CustomMaterial",
color: 0xFF0000,
});
this.viewer.impl.matman().addMaterial(uuid(), this.material, true);
}
async handleSelectionChange(event: SelectionChangedEvent): Promise<void> {
this.changeMaterialsForFragments(event.fragIdsArray);
}
changeMaterialsForFragments(fragIdsArray: number[]) {
fragIdsArray.map((fragId) => {
this.viewer.model.getFragmentList().setMaterial(fragId, this.material);
});
this.viewer.impl.invalidate(true);
this.viewer.impl.sceneUpdated(true); // not sure which it is, trying both
}
}
回答1:
Try viewer.setThemingColor
- see doc [here] (https://autodeskviewer.com/viewers/latest/docs/Autodesk.Viewing.Viewer3D.html):
viewer.setThemingColor( dbId, color:THREE.Vector4, [, model [, recursive:boolean ] ] ) //starting from Viewer v6.3 you may recursively apply color to child nodes
And to remove them try viewer.clearThemingColors
EDIT:
See live sample here
来源:https://stackoverflow.com/questions/56280344/unable-to-change-element-colour-in-viewer