Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Three.js中BufferGeometry顶点上的几何图形_Javascript_Three.js_Buffer Geometry - Fatal编程技术网

Javascript Three.js中BufferGeometry顶点上的几何图形

Javascript Three.js中BufferGeometry顶点上的几何图形,javascript,three.js,buffer-geometry,Javascript,Three.js,Buffer Geometry,我原以为我想要一件简单的事情,但结果比我想象的要复杂 我想要的只是三个缓冲几何体(球体)每个顶点上的网格 CODEPEN:CODEPEN.io/cheeseyes/pen/YwBZGz 这是我的缓冲区几何体: var bufferGeometry = new THREE.SphereBufferGeometry(1,1,1); 我正在尝试获取顶点的位置: bufferGeometry.getAttribute('position',0); 那么,我如何正确地读出它们,从而得到几何体的所有(

我原以为我想要一件简单的事情,但结果比我想象的要复杂

我想要的只是三个缓冲几何体(球体)每个顶点上的网格

CODEPEN:CODEPEN.io/cheeseyes/pen/YwBZGz

这是我的缓冲区几何体:

var bufferGeometry = new THREE.SphereBufferGeometry(1,1,1);
我正在尝试获取顶点的位置:

bufferGeometry.getAttribute('position',0);
那么,我如何正确地读出它们,从而得到几何体的所有(这里是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);

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

对于(i=12;iBufferGeometry),几何体将顶点存储在一维数组中,其中每组三个索引表示一个向量3(顶点位置)。因此,如果使用此数组:

var verts = [0, 0, 0, 0, 1, 0, 1, 1, 1];
你应该在(x,y,z)三胞胎中考虑它:

vert # 1: (0, 0, 0)
vert # 2: (0, 1, 0)
vert # 3: (1, 1, 1)
事实上,您可以在three.js文档中看到这方面的一个示例;请阅读BufferGeometry页面,您的问题可能会自动回答:

// components of the position vector for each vertex are stored
// contiguously in the buffer.
for ( var i = 0; i < vertexPositions.length; i++ )
{
    vertices[ i*3 + 0 ] = vertexPositions[i][0];
    vertices[ i*3 + 1 ] = vertexPositions[i][1];
    vertices[ i*3 + 2 ] = vertexPositions[i][2];
}
//存储每个顶点的位置向量分量
//连续地在缓冲区中。
对于(var i=0;i

也就是说,使用BufferGeometry有什么特别的原因吗?只有在出于优化目的确实需要时,才应该使用该类。BufferGeometry将其数据存储为原始数字数组,并且比常规Geometry类更难访问和操作,后者方便地将其顶点存储为向量数组OR3.坚持几何体而不是缓冲几何体,除非你有令人信服的理由不这样做。

因为它们都是三角形,一些顶点在其中多次出现。因为每个顶点都被所有变形三角形使用,对吗?(只是为了理解)我如何“过滤掉它们?”“过滤掉它们”是什么意思?这听起来像是另一个超出原始帖子范围的问题。至于顶点,如果在缓冲几何体上设置了“索引”属性,那么它将使用索引三角形方法来存储面。换句话说,“位置”属性数组存储几何体中的一组无重复的所有顶点,“索引”数组存储指示哪些顶点用于哪个面的三元组。如果未设置“索引”属性,则“位置”索引是(x,y,z)坐标的三元组的简单数组,其中每个三元组是顶点,每组三元组是面。因此[0,0,0,0,1,0,1,1,1]表示一个面,顶点位于(0,0,0)、(0,1,0)、(1,1,1)处。再次,我强烈建议使用几何体类而不是BufferGeometry,这将大大简化处理原始几何体的工作。