Creating new BufferGeometry from an array of intersected faces ( `intersects[ 0 ].face` )

I’m wondering if someone could give me a little help creating a new buffer geometry from a selection of positions from an already existing buffer geometry, i am working in this live example and have been going nuts on it for days now

if you click the button top left once it creates a new CSG mesh from the original geometry,
if you click it again it casts rays from the CSG mesh to the original geometry,
if a face from the original geometry is intersected i push that face into an array like so cont.push(intersects[ 0 ].face)
i remove any duplicates from that array incase a face was intersected more than once and push non duplicates to result = []
i then push the resulting intersects[ 0 ].face properties a, b and c ( result[f].a result[f].b result[f].c ) to a newFaces array,
i then push the position attribute from the original geometry to a final array based on the faces that were intersected like so… (newVertices.push(objsFinal.geometry.attributes.position.array[newFaces[v]])) which are correct in terms of amount and positions from what i can see on comparison from the original relative face indexes number and position

i am now trying to turn this final array into a new BufferGeometry like so

var newGeo = new THREE.BufferGeometry();
newGeo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(newVertices), 3));
newGeo.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(newNormal), 3));
var newMesh = new THREE.Mesh(newGeo, mat)

but my resulting geometry is not as expected, faces are all over the place…
image

is there any chance someone could have a look at the code and maybe point me in the right direction as to what’s going wrong, if there’s a step i’m missing or how i am not getting the correct positions somehow?

really appreciate any help honestly, it’s been days i’ve been trying to get this to work

@prisoner849 you’re expertise on this issue would be amazing if you could point me in the right direction at all!

2 Likes

I did it :slight_smile:

original Buffergeo position.count: 924, index.count: 1068

final processed Buffergeo ( all interior / unseen faces removed ) position.count: 105, index.count: 516

boring stuff but here’s the pen for anyone that needs something similar :slight_smile:

1 Like