0

ジオメトリからインデックス付きバッファ ジオメトリを作成して 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;

右のモデルが正しいモデルです。モデルの中心にある線に注意してください モデルの中央にある線に注目してください

4

0 に答える 0