我通过 STL 加载器添加模型元素,并且可以设置顶点颜色。我还需要稍后更改此颜色,我正在尝试通过以下代码来完成此操作。不幸的是颜色没有改变。有人知道怎么改吗?
let geometry = this.scene.getObjectByName("myFile.stl").geometry
const color = new THREE.Color(0xFF0000);
// @ts-ignore
for(let idx = 0; idx < geometry.attributes.color.count; idx++)
{
// @ts-ignore
geometry.getAttribute("color").setXYZ( idx, color.r, color.g, color.b )
}
最佳答案
更改值后,您必须将缓冲区属性的 needsUpdate
标志设置为 true
。尝试一下:
let geometry = this.scene.getObjectByName("myFile.stl").geometry;
const color = new THREE.Color(0xFF0000);
const colorAttribute = geometry.getAttribute("color");
for(let idx = 0; idx < colorAttribute.count; idx++)
{
// @ts-ignore
colorAttribute.setXYZ( idx, color.r, color.g, color.b )
}
colorAttribute.needsUpdate = true;
关于typescript - 三个JS中如何动态改变顶点颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67488404/