Buffery Geometry with array containing vertex positions and normals

I have vertex buffer data that i want to bring into Three.js. It is a flat array that contains vertex positions and normals, like this: PX PY PZ NX NY NZ

The corresponding geometry is indexed, so i’ve tried this:

let geometry = new THREE.BufferGeometry();
geometry.setAttribute( "position", new THREE.BufferAttribute(myVertices, 6)); // 6 because there are 6 values for each vertex, but this 
geometry.setIndex(new THREE.BufferAttribute(myTriangles, 1));

no luck.

am i going about this the wrong way? thanks

Hi!
As an option, you can use InterleavedBuffer and InterleavedBufferAttribute, here is a short example: Edit fiddle - JSFiddle - Code Playground

In your case, it will be something like this (just from the scratch, thus you’ll need to fix it, if it doesn’t work :sweat_smile: ):

let geometry = new THREE.BufferGeometry();
let buffer = new Float32Array(  _your_array_of_data_for_vertices_and_normals_ );
let interleavedBuffer = new THREE.InterleavedBuffer( buffer, 6 );
geometry.setAttribute( "position", new THREE.InterleavedBufferAttribute(interleavedBuffer, 3, 0));
geometry.setAttribute( "normal", new THREE.InterleavedBufferAttribute(interleavedBuffer, 3, 3)); 
geometry.setIndex( myTriangles );
2 Likes

that works. thank you so much!

You’re welcome :beers: :handshake: