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

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 "uncompressed" 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.

Answers:

Answer

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 come from the geometry faces property. The answer by gaitat gives a great example as to why that is!

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.