简体   繁体   English

自定义three.js几何图形不会显示其外观

[英]Custom three.js geometry won't show it's face

I'm making a custom three.js geometry for non-orthogonal cubes. 我正在为非正交多维数据集创建自定义的three.js几何。 It is loosely based on the existing Box-geometry in three.js, but greatly simplified insofar that it only supports one segment per side and also has the absolute position of its vertices fed directly to it. 它大致基于three.js中现有的Box-geometry,但是在极大程度上简化了它,使其仅支持每侧一个线段,并且其顶点的绝对位置直接馈送到该线段。

I have problems both in wire frame rendering and texture rendering. 线框渲染和纹理渲染都遇到问题。 In wire frame rendering I only get to see one of the six sides, as can be seen here: 在线框渲染中,我只能看到六个侧面之一,如下所示:

线框渲染

This is the snippet that I use for setting the material: 这是我用来设置材料的片段:

if (woodTexture) {
    texture = THREE.ImageUtils.loadTexture( 'crate.gif' );
    texture.anisotropy = makeRenderer.renderer.getMaxAnisotropy();
    material = new THREE.MeshBasicMaterial( { map: texture } );
} else {
    material = new THREE.MeshBasicMaterial( { color: color, wireframe: true, side: THREE.DoubleSide } );
}

I know for sure the path for crate.gif is valid, as it works for Box geometries. 我肯定知道crate.gif的路径是有效的,因为它适用于Box几何体。

Here follows my faulty geometry. 我的几何形状有问题。 The 'quadruplets' array contains six arrays with each four Vector3 instances. 'quadruplets'数组包含六个数组,每个数组有四个Vector3实例。 Each inner array delineates a side of the cube. 每个内部数组都描述了立方体的一侧。

THREE.Box3Geometry = function (quadruplets, debug) {

THREE.Geometry.call(this);

var constructee = this;  // constructee = the instance currently being constructed by the Box3Geometry constructor

buildPlane(quadruplets[0], 0, debug); // px
buildPlane(quadruplets[1], 1); // nx
buildPlane(quadruplets[2], 2); // py
buildPlane(quadruplets[3], 3); // ny
buildPlane(quadruplets[4], 4); // pz
buildPlane(quadruplets[5], 5); // nz

function buildPlane(quadruplet, materialIndex, debug) {

    // populate the vertex array:
    constructee.vertices.push(quadruplet[0]);
    constructee.vertices.push(quadruplet[1]);
    constructee.vertices.push(quadruplet[2]);
    constructee.vertices.push(quadruplet[3]);

    // construct faceVertexUvs:
    var uva = new THREE.Vector2(0, 1);
    var uvb = new THREE.Vector2(0, 0);
    var uvc = new THREE.Vector2(1, 0);
    var uvd = new THREE.Vector2(1, 1);

    // construct faces:
    var a = 0;  // vertex: u:50, v:50
    var b = 2;  // vertex: u:50, v:-50
    var c = 3;  // vertex: u:-50, v:-50
    var d = 1;  // vertex: u:-50, v:50

    // construct normal:
    var pv0 = quadruplet[1].clone().sub(quadruplet[0]);  // pv = plane vector
    var pv1 = quadruplet[2].clone().sub(quadruplet[0]);
    normal = new THREE.Vector3(0,0,0).crossVectors(pv0, pv1).normalize();;

    var face1 = new THREE.Face3(a, b, d);
    face1.normal.copy(normal);
    face1.vertexNormals.push(normal.clone(), normal.clone(), normal.clone());
    face1.materialIndex = materialIndex;

    constructee.faces.push(face1);
    constructee.faceVertexUvs[ 0 ].push([ uva, uvb, uvd ]);

    var face2 = new THREE.Face3(b, c, d);
    face2.normal.copy(normal);
    face2.vertexNormals.push(normal.clone(), normal.clone(), normal.clone());
    face2.materialIndex = materialIndex;

    constructee.faces.push(face2);
    constructee.faceVertexUvs[ 0 ].push([ uvb.clone(), uvc, uvd.clone() ]);

}
this.mergeVertices();

};

THREE.Box3Geometry.prototype = Object.create(THREE.Geometry.prototype);

And this is the Box geometry from which I was "inspired" . 这就是我从中受到启发的Box几何形状

You build a nice array of vertices, but give every face1/face2 combo the same set of indexes into that array: 0, 1, 2, 3. You essentially define the same quad 6 times. 您构建了一个漂亮的顶点数组,但给每个face1 / face2组合赋予了该数组相同的索引集:0、1、2、3。您实际上定义了相同的四边形6次。

What you need to do is keep a running base offset and add that to the vertex indices used to define each face. 您需要做的是保持运行中的基本偏移并将其添加到用于定义每个面的顶点索引中。 If you look at your BoxGeometry example, you'll that they do exactly that. 如果您看一下BoxGeometry示例,您会发现他们确实做到了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM