I'm trying to set per-face UV indices in a BufferGeometry.
I'm starting with a Geometry. Each face of my geometry has a face.materialIndex
corresponding to a UV index. I'm trying to convert this to a BufferGeometry, and then map over the face.materialIndex
to the BufferGeometry
.
Here's what I have so far:
// Convert geometry > buffergeometry
const bufGeo = new BufferGeometry().fromGeometry( geometry );
// Get an array of all the original geometry's indices...
const faceIndices = geometry.faces.map( face => face.materialIndex );
// Build a new array with the indices...
const indices = new Uint16Array( faceIndices );
// Apply to the BufferGeometry
bufGeo.setIndex( new BufferAttribute( indices, 1 ) );
Right now this appears to clobber my mesh and make it not draw at all. What am I doing wrong?
By the way, under the hood, when a Geometry is converted to a BufferGeometry, Three.js puts it in an intermediary format first called a DirectGeometry
. This used to copy over indices, but it was removed for reasons unknown in this commit by Mr Doob. Right now Three appears to discard indices entirely in a Geo > BufGeo conversion.
I have also tried using the code from that commit (modified to use setIndex):
const indices = new Uint16Array( faceIndices.length * 3 );
bufGeo.addAttribute( 'index', new BufferAttribute( indices, 1 ).copyIndicesArray( faceIndices ) );
But I have the same problem. The resulting mesh is clobbered.
webgl
tag to this question. Nothing about this question has anything to do with WebGL. It is entirely a three.js question – gman