How to invert BufferGeometry, and let's add it to BufferGeometryUtils?

I tried to invert a BufferGeometry based on this StackOverflow post from a few years back,

but things seem to have changed and it doesn’t work.

Here’s a working example before negative scaling of geometry:

Here’s the example with -1 scale for X:

Finally here is the attempt to use that function but it totally changes the vertex ordering and the cube is no longer a cube:

So it seems like the answer on SO is outdated perhaps? Or maybe it works in some cases but not others. If so, what would be a better implementation?

Also the BufferGeometry docs don’t do any explaining of the data layouts of different standard attributes:

Looks like trying to fix this is going to be a dive into source code. :face_holding_back_tears: :smiling_face_with_tear:

I’m hoping someone here might already be familiar with this.

(I’m migrating from Three.js with old Geometry classes to newer Three.js with BufferGeometry).

Maybe this should go into BufferGeometryUtils?

@rockclimber Oh hey, that’s you who answered that SO question ^. :slight_smile::wave:

Oh no I was discovered! I tried your example in CodePen and it seems like the solution I posted works but only for non-indexed geometries
In your last example under

geometry.scale(-1, 1, 1);


geometry = geometry.toNonIndexed();

To make it work for indexed geometries index attribute should have its elements swapped from ABC to CAB and other attributes don’t have to be modified

Edit: I added a version for indexed geometries. It’s much simpler but I haven’t tested too much if i.e. UVs work as expected

    function handleInvertedGeometry(geometry) {
	  const index = geometry.index.array
	  for (let i = 0, il = index.length / 3; i < il; i++) {
	    let x = index[i * 3]
	    index[i * 3] = index[i * 3 + 2]
        index[i * 3 + 2] = x
	  geometry.index.needsUpdate = true

the normals are flipped nicely but
the reflections are flipped , any fix for that ?

@trusktr @rockclimber

@orion_prime that looks correct. A glass ball make the world look upside down. See some images on Google:

And this thread:

1 Like

ohh, that’s right !! … floor gets reflected on the top surface of the sphere now ! … my bad

1 Like