首页 > 解决方案 > 我想让对象以某种程度的透明度在查看器中出现和消失

问题描述

我正在制作一个 revi 模型,以放入锻造厂,您可以在其中阅读建筑物的历史。从 nascia - 倒塌 - 拆除 - 重建。在一段和下一段之间我想为模型或模型零件的变化添加一定程度的透明度(淡入淡出)。

这是我的功能:

 function T00()
  {
  NOP_VIEWER.model.search('00', function onSuccess(dbids) {NOP_VIEWER.showAll(); 
   NOP_VIEWER.isolate(dbids); }, function onError(err) { console.error(err); }, 
  'Timeline_Storico');}.fadeToggle(400)

我隔离了我想看到的模型部分。如何添加透明度?

标签: javascriptautodesk-forgerevit

解决方案


首先,我建议查看其他帖子以了解有关渲染机制的一些背景: 如何设置每个节点的不透明度

默认材料定义在

viewer.impl.matman()._materials;

不同的片段可能共享相同的材料。

在下面的博客中,我演示了如何通过更改材料参数使模型变灰。 https://forge.autodesk.com/blog/showhide-textures-object-forge-viewer 同样,您可以将它们的不透明度设置为不同的值。例如

 //iterate materials 
for (index in mats) {

    //index is the material name (unique string in the list)
    m = mats[index];

    //store texture info
    oldTextures[index] = m.map;
    oldColors[index] = m.color;

    //set the material without texture and the grey color
    //var grey = new THREE.Color(0.5, 0.5, 0.5); 
    //m.map = null;
    //m.color = grey;

    //change transparency
    m.opacity = 0.1*index;
    m.transparent = true;
    //force update
    material.needsUpdate = true

    //mark the material dirty. The viewer will refresh
    m.needsUpdate = true; 
} 
//refresh the scene
viewer.impl.invalidate(true, true, false);

然而问题是:由于片段与材质共享,所有其他片段(具有相同材质)也将被更新,而不是通过透明度对象。

同时,可以通过克隆特定对象的材质,更改其不透明度,最后将此材质仅应用于对象来解决此问题。当然,因为是克隆素材,所以需要添加到viewer.impl.matman()的素材列表中。其他带有代码的帖子演示 如何为特定的 dbId 复制设置独立的材料,为您提供方便:

let material = fragList.getMaterial(fragId).clone();
 if (material) {
material.opacity = opacity
material.transparent = true
material.needsUpdate = true
}
    viewer.impl.matman().addMaterial ('myCustomMaterial', material, 
 true);
    viewer.model.getFragmentList().setMaterial(fragId, material);
    viewer.impl.invalidate(true);

推荐阅读