Three.js r141中PlaneGeometry的vertices属性未定义问题咨询
问题解答
是的,Three.js r141版本确实调整了顶点更新的处理方式——从r125版本开始,官方就移除了旧的Geometry类,统一使用BufferGeometry作为几何体的标准实现,你代码里的PlaneGeometry在r141中已经是PlaneBufferGeometry的别名,两者本质都是缓冲几何体,不再拥有.vertices属性。
缓冲几何体的顶点数据存储在attributes.position中,这是一个Float32Array类型的缓冲数组,每个顶点由连续的3个元素(x、y、z)组成。要修改顶点,需要直接操作这个数组,代码调整如下:
const geometry = new THREE.PlaneBufferGeometry(1.7, 1, 15, 9); const material = new THREE.MeshBasicMaterial({color: color}); const mesh = new THREE.Mesh(geometry, material); // 获取顶点位置属性 const positionAttr = mesh.geometry.attributes.position; // 遍历所有顶点(每个顶点占3个数组元素) for (let i = 0; i < positionAttr.count; i++) { // 获取当前顶点的x值 const x = positionAttr.getX(i); // 修改z值 positionAttr.setZ(i, 0.6 * Math.sin(x * 2 + this.elapsedTime)); } // 必须标记属性需要更新,Three.js才会重新上传数据到GPU positionAttr.needsUpdate = true;
如果是在动画循环中实时更新顶点,每次修改后都要设置positionAttr.needsUpdate = true,确保GPU能获取到最新的顶点数据。
内容的提问来源于stack exchange,提问作者yvl
相关产品推荐
相关产品推荐

