Three.js中

时间:2016-02-12 19:14:27

标签: javascript three.js buffer-geometry

我以为我想要一个安静的简单的东西,但事实证明它比我想象的更复杂。

我想要的只是THREE.BufferGeometry(球体)的每个顶点上的网格!

CODEPEN:codepen.io/cheesyeyes/pen/YwBZGz

this is what I want to have

这是我的缓冲几何:

var bufferGeometry = new THREE.SphereBufferGeometry(1,1,1);

我正在尝试获取顶点的位置:

bufferGeometry.getAttribute('position',0);

所以如何正确读出它们,以便得到bufferGeometry所有(这里是5个)顶点的位置?

我试过这样的话。它确实有效,但我认为它非常难看:

vertexMesh.position.x=position.array[0];
vertexMesh.position.y=position.array[1];
vertexMesh.position.z=position.array[2];

for(i=12;i<position.array.length-9;i=i+3)
            { 

            //vertexGeometry
            var vertexGeometry = new THREE.SphereGeometry(0.1,32,32);

            var vertexMaterial = new THREE.MeshBasicMaterial({color:0xff0000});   

            var vertexMesh = new THREE.Mesh(vertexGeometry,vertexMaterial);    

            vertexMesh.position.x=position.array[i];
            vertexMesh.position.y=position.array[i+1];
            vertexMesh.position.z=position.array[i+2]; 

             scene.add(vertexMesh);

            }

我根本没有得到这个阵列的排列方式..请帮帮我! 谢谢!

1 个答案:

答案 0 :(得分:1)

BufferGeometry stores vertices in a 1-dimensional array, where every set of three indices represent a single Vector3 (a vertex position). So if you had this array:

        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/event');
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.onload = function() {
            if (xhr.status === 200) {
                var userInfo = JSON.parse(xhr.responseText);
                console.log(userInfo);
            }
        };
        xhr.send(JSON.stringify({
            id: 1,
            type: 'BOOK_VIEWED'
        }));

you should be thinking about it in (x, y, z) triplets:

var verts = [0, 0, 0, 0, 1, 0, 1, 1, 1];

In fact you can see an example of this in the three.js documentation; go read up on the BufferGeometry page and your question would probably answer itself:

vert # 1: (0, 0, 0)
vert # 2: (0, 1, 0)
vert # 3: (1, 1, 1)

That being said, is there a particular reason you're using BufferGeometry? You should only use that class if you really need to for optimization purposes. BufferGeometry stores its data as raw arrays of numbers and is much harder to access and manipulate than the regular Geometry class, which conveniently stores its vertices as an array of Vector3s. Stick to Geometry instead of BufferGeometry unless you have a compelling reason not to.