Three.JS r97 Issue GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2

Three.JS r97 Issue GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2

我发布这个问题作为最后的手段。 我已经阅读了网站上所有类似的主题。 问题是:

我使用的是 Three.JS (r97) 的旧版本,无法更新到最新版本。 我正在研究一个体素生成器,它接收一个坐标列表 (x,y,z) 作为输入,它代表体素的中心。

对于每个中心坐标,我将 8 个新顶点和 12*3 个索引(12 个三角形)附加到顶点和索引缓冲区。每个体素的“蓝图”是 THREE.BoxGeometry,但代码应该适用于其他 THREE.Geometry。

我遇到以下错误:GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 2。我设置缓冲区的方式肯定有问题,但我无法深入了解。任何帮助表示赞赏。谢谢!

代码如下:

//vert_x, vert_y, vert_z have the same size
let vert_x = this.model.get("x")[0];
let vert_y = this.model.get("y")[0];
let vert_z = this.model.get("z")[0];
let voxel_geometry = new THREE.BoxGeometry(1, 1, 1);
vertices = new Float32Array(voxel_geometry.vertices.length * vert_x.length * 3);
indices = new Uint32Array(voxel_geometry.faces.length * vert_x.length * 3);
let faceOffset = 0;
let vIndex = 0;
let fIndex = 0;

for(let v=0; v<vert_x.length; v++) {
    for (let x=0; x<voxel_geometry.vertices.length; x++) {
        vertices[vIndex++] = voxel_geometry.vertices[x].x + vert_x[v];
        vertices[vIndex++] = voxel_geometry.vertices[x].y + vert_y[v];
        vertices[vIndex++] = voxel_geometry.vertices[x].z + vert_z[v];
    }
    for (let b=0; b<voxel_geometry.faces.length; b++) {
        indices[fIndex++] = voxel_geometry.faces[b].a + faceOffset;
        indices[fIndex++] = voxel_geometry.faces[b].b + faceOffset;
        indices[fIndex++] = voxel_geometry.faces[b].c + faceOffset;
    }
    faceOffset += voxel_geometry.vertices.length;
}

const geometry = new THREE.BufferGeometry();
geometry.addAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.addAttribute("color", new THREE.BufferAttribute(colors, 4));
geometry.setIndex(new THREE.BufferAttribute(indices, 1));
geometry.setDrawRange(0, indices.length-1);
}

我犯了大错!我没有检查颜色数组的大小是否正确。谢谢你。 这个问题可以关闭了。