typescript - 三个JS中如何动态改变顶点颜色

标签 typescript three.js

我通过 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/

相关文章:

javascript - Typescript,箭头函数和 rxjs 内的 "this"问题

debugging - Chrome Typescript 调试引用错误 'this'

Angular 4获取dom元素

three.js - OrbitControl 或 TrackballControl

javascript - Q.Promise 和 KO.mapping 未将对象数组转换为正确的类型

javascript - Three.JS 错误 - 纹理不是二的幂/无法更改纹理

webgl - Three.js 中的水模拟

camera - 在 Threejs 中使用鼠标滚轮更改相机位置.z

javascript - Javascript 预加载内容 three.js

javascript - 无法在 Javascript 对象中动态添加键