最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does converting a Geometry to a BufferGeometry in Three.js increase the number of vertices? - Stack Overflow

programmeradmin6浏览0评论

I've been using the fromGeometry method to make BufferGeometry objects from regular Geometry objects, and I've found that it seems the number of vertices increases during the conversion. For example if I do something like:

var geometry = new THREE.BoxGeometry(80,80,80,16,16,16);
var bGeometry = new THREE.BufferGeometry();
bGeometry.fromGeometry(geometry);
console.log(bGeometry.getAttribute('position'));
console.log(geometry.vertices.length);

I get a Float32Array[27648], which divided by 3 yields 9216, while the Geometry has 1538 vertices. I understand THREE.BoxGeometry merges some vertices, so I suspect that might have to do with it, but am I correct that BoxGeometry objects have more vertices? Or are Geometry objects "unpressed" in the GPU anyway so they actually have the same number of vertices, just not in my Geometry object?

The reason I'm asking is that I was trying to create a custom attribute, but my array was too short when I only iterated over the number of vertices in my Geometry object, thinking that there would be the same amount of vertices in both objects.

I've been using the fromGeometry method to make BufferGeometry objects from regular Geometry objects, and I've found that it seems the number of vertices increases during the conversion. For example if I do something like:

var geometry = new THREE.BoxGeometry(80,80,80,16,16,16);
var bGeometry = new THREE.BufferGeometry();
bGeometry.fromGeometry(geometry);
console.log(bGeometry.getAttribute('position'));
console.log(geometry.vertices.length);

I get a Float32Array[27648], which divided by 3 yields 9216, while the Geometry has 1538 vertices. I understand THREE.BoxGeometry merges some vertices, so I suspect that might have to do with it, but am I correct that BoxGeometry objects have more vertices? Or are Geometry objects "unpressed" in the GPU anyway so they actually have the same number of vertices, just not in my Geometry object?

The reason I'm asking is that I was trying to create a custom attribute, but my array was too short when I only iterated over the number of vertices in my Geometry object, thinking that there would be the same amount of vertices in both objects.

Share Improve this question edited Oct 23, 2015 at 1:20 WestLangley 105k11 gold badges287 silver badges283 bronze badges asked Oct 22, 2015 at 20:52 JoshJosh 3382 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

The best way to find out the answer to these questions is just to look at the three.js code. It is very accessible and generally easy to follow.

First we can look up the BufferGeometry.fromGeometry method:

fromGeometry: function ( geometry ) {

    geometry.__directGeometry = new THREE.DirectGeometry().fromGeometry( geometry );

    return this.fromDirectGeometry( geometry.__directGeometry );

}

Which you can see is calling DirectGeometry.fromGeometry

fromGeometry: function ( geometry ) {
    // ...
    var vertices = geometry.vertices;
    // ...
    for ( var i = 0; i < faces.length; i ++ ) {
        // ...
        this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] );
        // ...
    }
    // ...
}

And here you can clearly see that it is adding additional vertices that e from the geometry faces property. The answer by gaitat gives a great example as to why that is!

Lets take the simple case of a BoxGeometry(80,80,80,1,1,1), For the box BufferGeometry we get 36 vertices and for the box Geometry we get 8 vertices. How is this possible? Lets do the math:

To define a box we only need 8 vertices to define 12 triangles that make up that shape; but for each vertex of the box you have 6 triangles sharing it. Keep in mind that each face of the cube has 2 triangles.

If you unroll all the triangles of the box Geometry into a box BufferGeometry you have: 6 faces x 2 triangles per face x 3 vertices per triangle x 3 floats per vertex = 108 floats

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论