Javascript Can';t在树内使用全局变量。enumNodeFragments()委托

Javascript Can';t在树内使用全局变量。enumNodeFragments()委托,javascript,autodesk-forge,autodesk-viewer,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

我试图通过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);
}

document
window
都应该在回调函数的范围内可用,但是这不是一个好的做法。尝试在
突出显示功能的范围内定义旧材质的贴图:

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

我以前也试过。调用树之后。enumNodeFragments oldMaterials为空。viewer.model.getFragmentList().getMaterial(fragId)不是空的。我在tree.enumNodeFragments之后调用console.log(document.oldMaterials)或console.log(window.oldMaterials),在控制台中看到{}(空字典)。正如我提到的,引用全局变量(尤其是
document
),这不是一个好的做法,可能会导致不同的问题。尝试改用本地
oldMaterials
常量,并确保
enumNodeFragments
中的回调函数实际执行。如何确保enumNodeFragments中的回调函数实际执行?我的意思是:确认回调函数实际执行,例如,将
console.log
debugger
语句放入其中。