我想从几何体制作索引缓冲区几何体以将其加载到 OpenGL ES2 中,我使用以下代码并成功创建了与原始几何体相似的几何体,但是在应用纹理时,某些面无法正确渲染
var g = new THREE.BufferGeometry();
var fposition = new Float32Array(geometry.vertices.length * 3);
var fnormals = new Float32Array(geometry.vertices.length * 3);
var fcolors = new Float32Array(geometry.vertices.length * 3);
var fuvs = new Float32Array(geometry.vertices.length * 2);
var findexes = new Uint16Array(geometry.faces.length * 3);
var vi = [];
var k = 0;
var ni = 0;
for (var i = 0; i < geometry.vertices.length; i++) {
fposition[i * 3] = (geometry.vertices[i].x);
fposition[i * 3 + 1] = (geometry.vertices[i].y);
fposition[i * 3 + 2] = (geometry.vertices[i].z);
fcolors[i * 3] = 1;
fcolors[i * 3 + 1] = 1;
fcolors[i * 3 + 2] = 1;
}
for (var i = 0; i < geometry.faces.length; i++) {
findexes[i * 3] = (geometry.faces[i].a);
findexes[(i * 3) + 1] = (geometry.faces[i].b);
findexes[(i * 3) + 2] = (geometry.faces[i].c);
if (vi.indexOf(geometry.faces[i].a) === -1) {
fuvs[k] = (geometry.faceVertexUvs[0][i][0].x);
fuvs[k + 1] = (geometry.faceVertexUvs[0][i][0].y);
fnormals[ni] = (geometry.faces[i].vertexNormals[0].x);
fnormals[ni + 1] = (geometry.faces[i].vertexNormals[0].y);
fnormals[ni + 2] = (geometry.faces[i].vertexNormals[0].z);
vi.push(geometry.faces[i].a);
k += 2;
ni += 3
}
if (vi.indexOf(geometry.faces[i].b) === -1) {
fuvs[k] = (geometry.faceVertexUvs[0][i][1].x);
fuvs[k + 1] = (geometry.faceVertexUvs[0][i][1].y);
fnormals[ni] = (geometry.faces[i].vertexNormals[1].x);
fnormals[ni + 1] = (geometry.faces[i].vertexNormals[1].y);
fnormals[ni + 2] = (geometry.faces[i].vertexNormals[1].z);
vi.push(geometry.faces[i].b);
k += 2;
ni += 3;
}
if (vi.indexOf(geometry.faces[i].c) === -1) {
fuvs[k] = (geometry.faceVertexUvs[0][i][2].x);
fuvs[k + 1] = (geometry.faceVertexUvs[0][i][2].y);
fnormals[ni] = (geometry.faces[i].vertexNormals[2].x);
fnormals[ni + 1] = (geometry.faces[i].vertexNormals[2].y);
fnormals[ni + 2] = (geometry.faces[i].vertexNormals[2].z);
vi.push(geometry.faces[i].c);
k += 2;
ni += 3;
}
}
var position = new THREE.BufferAttribute(fposition, 3);
var index = new THREE.BufferAttribute(findexes, 1);
var color = new THREE.BufferAttribute(fcolors, 3);
var normal = new THREE.BufferAttribute(fnormals, 3);
var uv = new THREE.BufferAttribute(fuvs, 2);
g.addAttribute("position", position);
g.addAttribute("color", color);
g.addAttribute("uv", uv);
g.addAttribute("normal", normal);
g.setIndex(index);
g.getAttribute("uv").needsUpdate = true;