Viewer会自动将材质更改为默认值

时间:2017-10-09 10:30:35

标签: autodesk-forge autodesk-viewer

我对某些元素的材质发生了变化的问题

加载几何体时:

_this.viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, () => {
    changeModelMaterial()
});

...

const changeModelMaterial = () => {
    const grey = new THREE.Color(0.5, 0.5, 0.5);
    let dbIds = getDbIds()
    changeAllElementsMaterial(grey)
    setMaterialOfDbIds(dbIds)
}

我用来更改资料的代码:

const changeAllElementsMaterial = (color) => {
    const fragmentList = _this.viewer.model.getFragmentList();
    for (let materialId of fragmentList.materialids) {
        if (fragmentList.materialmap[materialId]) {
            fragmentList.materialmap[materialId].map = null
            fragmentList.materialmap[materialId].color = color
            fragmentList.materialmap[materialId].needsUpdate = true;
        }
    }
    _this.viewer.impl.invalidate(true);
}

const setMaterialOfDbIds = (dbIds) => {
    var color_diffuse = 0xAB00EE;
    var color_specular = 0xEEABEE;
    var colorM = new THREE.MeshPhongMaterial({
        color: color_diffuse,
        specular: color_specular
    });
    _this.viewer.impl.matman().addMaterial(
        'ADN-Material-' +
        "common color material",  // or a GUID
        colorM,
        true);

   for (let dbId of dbIds) {
       _this.viewer.model.getData().instanceTree.enumNodeFragments(dbId, function (fragId) {
           _this.viewer.model.getFragmentList().setMaterial(fragId, colorM);

       });
   }
   _this.viewer.impl.invalidate(true);
}

它有效,因为我看到模型材料发生了变化,但问题是材料在1-2秒后恢复到默认状态。

在此之后,即使手动运行此代码,我也无法更改材料。

问题是Viewer在这2秒后锁定材料变化的原因,如何防止它

也许你可以告诉我,我可以通过材料变化做得更好,例如。也许更好的是在GEOMETRY_LOAD之后运行我的代码。最好在首次渲染模型之前更改材料

........

提示:

当从GEOMETRY_LOADED_EVENT更改事件到OBJECT_TREE_CREATED_EVENT“有时”但有时它只能运行良好(材料保持到使用模型结束),但主要是当我在OBJECT_TREE_CREATED之后运行我的方法它不起作用(甚至不工作它运行它手动,材料以某种方式锁定)。所以我怀疑问题是在GEOMETRY_LOAD和OBJECT_TREE_CREATED的时间之间

我将不胜感激任何帮助

==============================完整代码=============== ===============

的index.html

<div id="main">
    <div id="MyViewerDiv"></div>
    <button id="open-nav-button" onClick="showDocInfo()">test</button>
</div>
<script src="https://developer.api.autodesk.com/derivativeservice/v2/viewers/three.min.js"></script>
<script src="https://developer.api.autodesk.com/derivativeservice/v2/viewers/viewer3D.min.js"></script>

<script type="text/javascript" src="lib/jquery.min.js"></script>

<script src="js/autodesk-viewer.js"></script>
<script src="js/extension/test-extension.js"></script>

<script>
    const autodeskViewer = new AutodeskViewer()
    const showDocInfo = () => {
        autodeskViewer.showDocInfo()
    }
</script>

的Autodesk-viewer.js

var AutodeskViewer = (function () {
function AutodeskViewer() {
    var _this = this;
    this.urn = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Zm9yZ2UtamF2YS1zYW1wbGUtYXBwLTFzcGduazdqcWpxdjhmYXV0YmNzd2R0cGdvN3VtNWY1L1BPQy1Gb3JnZS1JVCUyMDIwMTclMjBSdWNoXzEwMDUxNy5ud2Q';

    this.initializeViewer = function (containerId, documentId) {
        _this.viewerApp = new Autodesk.Viewing.ViewingApplication(containerId);
        var config = {
            extensions: ['TestExtension']
        };
        _this.viewerApp.registerViewer(_this.viewerApp.k3D, Autodesk.Viewing.Private.GuiViewer3D, config);
        _this.viewerApp.loadDocument(documentId, _this.onDocumentLoadSuccess, _this.onDocumentLoadFailure);
    }

    this.onDocumentLoadSuccess = function (doc) {
        const viewables = _this.viewerApp.bubble.search(av.BubbleNode.MODEL_NODE);
        if (viewables.length === 0) {
            return;
        }
        _this.viewerApp.selectItem(viewables[0].data, _this.onItemLoadSuccess, _this.onItemLoadFail);
        _this.viewer3d = _this.viewerApp.getCurrentViewer();
    }

    this.onDocumentLoadFailure = (viewerErrorCode) => {}

    this.onItemLoadSuccess = (viewer) => {
        _this.viewer = viewer
    }

    this.onItemLoadFail = (errorCode) => {}

    this.initialize = () => {
        var options = {
            env: 'AutodeskProduction',
            getAccessToken: _this.getToken,
            refreshToken: _this.getToken
        };
        Autodesk.Viewing.Initializer(options, _this.initCallback);
    };

    this.initCallback = function () {
        _this.initializeViewer('MyViewerDiv', _this.urn, '3d');
    };
    this.getToken = function (onGetAccessToken) {
        $.get("forge/oauth/token")
            .done(function (data) {
                token = data
                onGetAccessToken(token, 60 * 60);
            })
            .fail(function (error) {
                console.log('ERROR', error);
            });
    };

    this.showDocInfo = function () {};
    this.initialize();
}

return AutodeskViewer;
}());

测试extension.js

var _self;
var _viewer;
var _tempValue = 0;

function TestExtension(viewer, options) {
    Autodesk.Viewing.Extension.call(this, viewer, options);
    _self = this;
    _viewer = viewer;
}

const changeModelMaterial = () => {
    // _tempValue++;
    // if (_tempValue == 2) {
    const elements = [4340, 4342, 4344, 4346, 4348, 4367, 4371, 4375, 4380, 4452, 4468, 4488, 4503, 4517, 4520, 4522, 4524, 4526, 4528, 4530]

    changeAllElementsMaterial(new THREE.Color(0.5, 0.5, 0.5))
    setMaterialOfDbIds(elements)
    _tempValue = 0
    // }
}

const changeAllElementsMaterial = (color) => {
    var fragmentList = _viewer.model.getFragmentList();
    for (let materialId of fragmentList.materialids) {
        if (fragmentList.materialmap[materialId]) {
            fragmentList.materialmap[materialId].map = null
            fragmentList.materialmap[materialId].color = color
            fragmentList.materialmap[materialId].needsUpdate = true;
        }
    }
    _viewer.impl.invalidate(true);
}

const setMaterialOfDbIds = (dbIds) => {
    var colorM = new THREE.MeshPhongMaterial({
        color: new THREE.Color(0xAB00EE)
    });

    for (let dbId of dbIds) {
        _viewer.model.getData().instanceTree.enumNodeFragments(dbId, function (fragId) {
            _viewer.model.getFragmentList().setMaterial(fragId, colorM);

        });
    }
    _viewer.impl.invalidate(true);
}

TestExtension.prototype = Object.create(Autodesk.Viewing.Extension.prototype);
TestExtension.prototype.constructor = TestExtension;

TestExtension.prototype.load = function () {
    _viewer.addEventListener(Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT, changeModelMaterial)
    // _viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, changeModelMaterial)
    return true
};

TestExtension.prototype.unload = function () {
    return true
};

Autodesk.Viewing.theExtensionManager.registerExtension('TestExtension', TestExtension);

2 个答案:

答案 0 :(得分:0)

到目前为止,我无法重现该问题,我正在使用从该扩展程序中提取的以下代码(ES7):Viewing.Extension.Material

createColorMaterial (color) {

  const material = new THREE.MeshPhongMaterial({
    specular: new THREE.Color(color),
    side: THREE.DoubleSide,
    reflectivity: 0.0,
    color
  })

  const materials = this.viewer.impl.getMaterials()

  materials.addMaterial(
    this.guid(),
    material,
    true)

  return material
}

async onModelCompletedLoad() {

  const material = this.createColorMaterial(0xFF0000)

  const model = this.viewer.model

  const fragIds = await Toolkit.getFragIds(model)

  fragIds.forEach((fragId) => {

    model.getFragmentList().setMaterial(
      fragId, material)
  })

  this.viewer.impl.sceneUpdated(true)
} 

onModelCompletedLoad是在GEOMETRY_LOADED_EVENTOBJECT_TREE_CREATED_EVENT被解雇时触发的自定义事件。

有关详情,请参阅此文章:Asynchronous viewer events notification

我怀疑你可以在首次渲染模型之前轻松更改材质,但是你可以使用隐藏模型的自定义覆盖,直到你的自定义逻辑执行了所有必需的步骤,这是我在我的演示中使用的方法:https://forge-rcdb.autodesk.io/configurator

加载模型后,所有自定义材料都保持良好状态:

enter image description here

可以从there实时测试材料扩展名。

希望有所帮助

答案 1 :(得分:0)

我发现了解决方案,非常偶然......来自我试图做的其他事情

之前:

    const setMaterialOfDbIds = (dbIds) => {
        var color_diffuse = 0xAB00EE;
        var color_specular = 0xEEABEE;
        var colorM = new THREE.MeshPhongMaterial({
            color: color_diffuse,
            specular: color_specular
        });
        _this.viewer.impl.matman().addMaterial("common color material", colorM, true);

        for (let dbId of dbIds) {
            _this.viewer.model.getData().instanceTree.enumNodeFragments(dbId, function (fragId) {
                _this.viewer.model.getFragmentList().setMaterial(fragId, colorM);
            });
        }
        _this.viewer.impl.invalidate(true);
    }

    const setMaterialOfDbIds = (dbIds) => {
        var color_diffuse = 0xAB00EE;
        var color_specular = 0xEEABEE;
        var colorM = new THREE.MeshPhongMaterial({
            color: color_diffuse,
            specular: color_specular
        });
        _this.viewer.impl.matman().addMaterial("common color material", colorM, true);

        for (let dbId of dbIds) {
            _this.viewer.model.getData().instanceTree.enumNodeFragments(dbId, function (fragId) {
                _this.viewer.model.getFragmentList().setMaterial(fragId, colorM);
                var fragProxy = _this.viewer.impl.getFragmentProxy(_this.viewer.model, fragId)
                fragProxy.updateAnimTransform()
            });
        }
        _this.viewer.impl.invalidate(true);
    }

我真的不知道为什么要添加

var fragProxy = _this.viewer.impl.getFragmentProxy(_this.viewer.model, fragId)
fragProxy.updateAnimTransform()

有所作为,在任何更新材料的例子中我都没有看到类似的东西。 有趣的是,此代码仅针对模型中的少数元素运行,但它甚至适用于材料之前更改的元素(在changeAllElementsMaterial方法中)。

@Philippe Leefsma如果你明白了,请告诉更多为什么它有效