Currently, I am conducting a PoC and experimenting with changing the color of a selected element to red. Despite creating a class as shown below, I am facing issues where the elements do not change color when selected. I have attempted various examples from different sources on the internet, but unfortunately, none of them seem to work.
Is there a way to modify the color of a specific element (either using dbId
or fragId
)? I have been unable to find comprehensive API documentation for this in the Forge API, so I am somewhat navigating in the dark.
/* 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
}
}