如何为特定的dbId设置独立的材料
问题描述:
我正在使用以下代码为具有特定dbId的元素设置材料.
I am setting up material for an element with a particular dbId with the following code.
getFragIdListFromGuid
是我实现的从某些dbId检索fragIdList的功能.但是我发现这实际上改变了场景中所有片段的材质.他们是否共享材料?
getFragIdListFromGuid
is a function I implemented to retrieve fragIdList from certain dbId. But I found this one actually changing all of the fragment material in the scene. Do they share the material together?
const fragIdList = await getFragIdListFromDBId(this.dataComponents, this.instanceTree, dbId)
fragIdList.forEach((fragId) => {
let material = fragList.getMaterial(fragId)
if (material) {
material.opacity = opacity
material.transparent = true
material.needsUpdate = true
}
})
答
该素材有可能在不同的片段之间共享.要在处理之前解决原始材料的克隆问题并应用克隆,请执行以下操作:
There's a chance that the material is shared amongst different fragments. To work around clone the original material before processing and apply the clone:
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);