Migrating to BufferGeometry

Hello,

I’m working on a project where I need to slice a geometry(loaded from an .stl file). I’ve already found a library which claims to do so(GitHub - tdhooper/threejs-slice-geometry: Slice three.js geometry with a plane.). The problem is that it is a bit old and it uses, for example, the Geometry class. So, I’m trying to rewrite it.

The issue is related with the inability to access the vertices and the edges of the geometry:

given the following code, how should I edit it in order to achieve the same thing using BufferGeometry?

var geometry = //something of type THREE.Geometry
geometry.vertices.forEach(function(vertex) {
            //Do stuff on vertex
        });

Thanks in advance for any help that you can give me.

geometry.attributes.position

Hi, thanks for the reply but I don’t understand. I actually already tried such a solution but I can’t see how I can iterate through the vertices in that way.

for (let i = 0; i < geometry.attributes.position.array.length; i += 3) {

}
1 Like

Oh, it was that simple? Thanks a lot!

You are welcome :laughing:

Small suggestion: if you are loading 3D models from other software (e.g. as glTF) then it might have interleaved vertex data, and position.array.length won’t work. To support both kinds of data, use position.count instead:

const vector = new Vector3();
const position = geometry.attributes.position;

for ( let i = 0; i < position.count; i++ ) {
  vector.fromBufferAttribute( position, i );
  console.log( vector );
  position.setXYZ( i, vector.x, vector.y, vector.z );
}

2 Likes

Ok, thanks for the tip :slight_smile: