无法在tree.enumNodeFragments()委托内使用全局变量

时间:2019-03-07 14:02:53

标签: javascript autodesk-forge autodesk-viewer

我试图通过fragId保存元素的旧材料,但无法执行此操作,因为在tree.enumNodeFragments()文档内部,也无法访问窗口。

    highlight(externalId, color, dict) {
    let viewer = this.viewer;
    var dbId = dict[externalId];


    let myMaterial = this.createMaterial(color);

    // used to rescale and remove the z-fighting
    let scaleRatio = 1.005; // this was determined as optimal through visual inspection

    var tree = NOP_VIEWER.model.getData().instanceTree;

    document.oldMaterials = {};


    tree.enumNodeFragments(dbId,
        function(fragId) {
         document.oldMaterials[fragId] = viewer.model.getFragmentList().getMaterial(fragId);

            viewer.model.getFragmentList().setMaterial(fragId, myMaterial);


            /* important technique if you want to remove z-fighting */
            let fragProxy = viewer.impl.getFragmentProxy(viewer.model, fragId);
            fragProxy.scale = new THREE.Vector3(scaleRatio, scaleRatio, scaleRatio);
            fragProxy.updateAnimTransform();
        },
        true);

    viewer.impl.invalidate(true);
}

1 个答案:

答案 0 :(得分:1)

在回调函数的范围内,documentwindow都应该可用,但这不是一个好习惯。尝试在highlight函数范围内定义旧材料图:

function highlight(externalId, color, dict) {
    const viewer = this.viewer;
    const dbId = dict[externalId];
    const myMaterial = this.createMaterial(color);
    // used to rescale and remove the z-fighting
    const scaleRatio = 1.005; // this was determined as optimal through visual inspection
    const tree = NOP_VIEWER.model.getData().instanceTree;
    const oldMaterials = {};

    tree.enumNodeFragments(dbId, function(fragId) {
        oldMaterials[fragId] = viewer.model.getFragmentList().getMaterial(fragId);
        viewer.model.getFragmentList().setMaterial(fragId, myMaterial);
        /* important technique if you want to remove z-fighting */
        const fragProxy = viewer.impl.getFragmentProxy(viewer.model, fragId);
        fragProxy.scale = new THREE.Vector3(scaleRatio, scaleRatio, scaleRatio);
        fragProxy.updateAnimTransform();
    },
    true);

    viewer.impl.invalidate(true);
}