我必须在循环函数中更新 bufferGeometry 的索引,但只能更新未分配的索引。如果索引数组的元素被赋值,下次就不能再赋值了。我搜索了一些问题,比如mesh.geometry.index.needsUpdate = true;
,但是没有效果。
我的代码通过 bufferGeometry 创建了一个新的 Mesh。
var geometry = new THREE.BufferGeometry();
var pBuff = new ArrayBuffer(MAX_POINTS*3*4);
var positions = new Float32Array(pBuff);
var indices = new ArrayBuffer(MAX_POINTS*2*3);
var iIndices = new Uint16Array(indices);
positions[0] = 1.0;
positions[1] = 1.0;
positions[2] = 1.0;
positions[3] = 1.0;
positions[4] = 1.0;
positions[5] = -1.0;
positions[6] = -1.0;
positions[7] = 1.0;
positions[8] = -1.0;
positions[9] = -1.0;
positions[10] = 1.0;
positions[11] = 1.0;
positions[12] = 1.0;
positions[13] = -1.0;
positions[14] = 1.0;
positions[15] = 1.0;
positions[16] = -1.0;
positions[17] = -1.0;
positions[18] = -1.0;
positions[19] = -1.0;
positions[20] = -1.0;
positions[21] = -1.0;
positions[22] = -1.0;
positions[23] = 1.0;
iIndices[0] = 0;
iIndices[1] = 1;
iIndices[2] = 2;
iIndices[3] = 2;
iIndices[4] = 3;
iIndices[5] = 0;
geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));
geometry.setIndex(new THREE.BufferAttribute(iIndices, 1));
cube = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({color: 'rgb(255, 255, 0)', side: THREE.DoubleSide}));
scene.add(cube);
这是我的代码更新循环函数中的索引。
var iIndices = cube.geometry.getIndex();
if(change_index){
iIndices[0] = 0;
iIndices[1] = 1;
iIndices[2] = 2;
iIndices[3] = 2;
iIndices[4] = 3;
iIndices[5] = 0;
change_index = false;
}else{
iIndices[0] = 4;
iIndices[1] = 5;
iIndices[2] = 6;
iIndices[3] = 6;
iIndices[4] = 7;
iIndices[5] = 4;
change_index = true;
}
cube.geometry.index.needsUpdate = true;
cube.geometry.setDrawRange( 0, 6 );
这是渲染结果。
只能保留第一个分配的索引。例如,indices([0,1,2,2,3,0]) 是有效的。下次我设置新值时,indices([4,5,6,6,7,4]) 无效。
我搜索了一些问题。例如,cube.geometry.index.needsUpdate = true;
无效。其他geometry.addAttribute('indices', new THREE.BufferAttribute(new Float32Array(iIndices), 3));
和更新cube.geometry.attributes.indices.array;
。这是无效的。
如何在循环函数中更新 bufferGeometry 的这些索引?
当我尝试一些方法来解决它时,我发现了一个有新问题的低方法。如何优化它。
这是我的方法。
var iIndices = cube.geometry.index.array.subarray(0, 6);
if(change_index){
iIndices[0] = 0;
iIndices[1] = 1;
iIndices[2] = 2;
iIndices[3] = 6;
iIndices[4] = 7;
iIndices[5] = 4;
change_index = false;
}else{
iIndices[0] = 4;
iIndices[1] = 5;
iIndices[2] = 6;
iIndices[3] = 2;
iIndices[4] = 3;
iIndices[5] = 0;
change_index = true;
}
cube.geometry.index.needsUpdate = true;
这是结果(之前)。
这是结果(之后)。
几何的顶点是不变的,但几何的索引(result01)是 [0,1,2,2,3,0] 并且几何的索引(result02)是 [4,5,6,6,7,4 ].当几何图形被渲染时,索引在 result01 和 result02 之间的每一帧都会更新。
但是,该方法仅支持new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({wireframe: false}));
. 当 时wireframe=true
,几何索引无法更新。
什么时候优化呢wireframe=true
?