Tomer Weller
Tomer Weller

Reputation: 2812

THREE.BufferGeometry - accessing face indices and face normals

In BufferGeometry, is there a way to access face indices and normals without converting to Geometry?

The geometry at hand is a SphereBufferGeometry created by the threejs editor.

I only need to read face indices and normals, not modify them.

Upvotes: 2

Views: 3006

Answers (1)

WestLangley
WestLangley

Reputation: 104783

BufferGeometry is either "indexed" or "non-indexed". SphereGeometry, derived from BufferGeometry, is of the indexed type.

You can access the face normals in the geometry data structure like so:

var normal = new THREE.Vector3(); // create once and reuse

...

// specify the desired face index
var faceIndex = 15; // [ 0 ... num_faces-1 ]

// specify which face vertex you want
var vertexNumber = 2; // [ 0, 1, or 2 ]

// compute the index where the data is stored
var index = geometry.index.array[ 3 * faceIndex + vertexNumber ];

// get the vertex normal from the attribute data
normal.fromBufferAttribute( geometry.attributes.normal, index );
console.log( normal );

three.js r.143

Upvotes: 5

Related Questions