无法在查看器中更改元素颜色

时间:2019-05-23 17:34:54

标签: javascript typescript three.js autodesk-forge

要测试PoC,我正在尝试将选定的元素涂成红色。为此,我创建了一个类,如下所示,但是当我选择元素时,元素没有任何反应。我已经在网络上尝试了许多示例(包括一些为渲染代理创建网格代理并添加覆盖图的示例),但是没有任何效果。

如何更改给定元素(dbIdfragId)的颜色?我在Forge API中找不到大多数关于此的API文档,所以我有点盲目。

/* 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 个答案:

答案 0 :(得分:0)

尝试viewer.setThemingColor-在[此处](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

要删除它们,请尝试viewer.clearThemingColors

编辑:

查看实时示例here