three.js 0.124版本调用InstancedBufferGeometry.copy报clone相关TypeError如何解决
问题原因
- three.js r124版本中,
InstancedBufferGeometry的copy方法仅支持接收同类型的InstancedBufferGeometry实例作为入参,你传入的BoxGeometry是普通BufferGeometry的子类,不存在InstancedBufferGeometry特有的instancedAttributes属性,方法内部尝试对这个不存在的属性下的元素调用clone方法时就会触发该报错。
解决方法
- 方案1:手动拷贝基础几何体的标准属性,不要调用
copy方法:
createObj() { const geometry = new THREE.InstancedBufferGeometry(); const baseGeometry = new THREE.BoxGeometry(2, 2, 20, 2, 2, 6); // 手动拷贝所有基础属性 geometry.index = baseGeometry.index; geometry.attributes = baseGeometry.attributes; // 如有需要可以手动拷贝groups等其他普通BufferGeometry属性 geometry.groups = baseGeometry.groups; }
- 方案2:如果需要做实例化渲染,在手动拷贝基础属性后,再补充所需的实例化属性即可,示例如下:
// 示例:补充实例化位置属性,假设有100个实例 const instanceCount = 100; const positions = new Float32Array(instanceCount * 3); // 此处省略给positions赋值每个实例位置坐标的逻辑 geometry.setAttribute('instancePosition', new THREE.InstancedBufferAttribute(positions, 3)); geometry.instanceCount = instanceCount;
内容的提问来源于stack exchange,提问作者user824624
相关产品推荐
相关产品推荐

