If I have a mesh (drawn in red below)


How can I calculate the inverse of the mesh (drawn in red below), given that the mesh is always constrained by the same four vertices of the encompassing bounding box?


My mesh is drawn using a series of clockwise wound triangles.

Is there possibly any easy way to do this in a shader?

I wish to draw the triangles, and therefore each pixel, of the inverse mesh rather than draw the original mesh with color of the mesh and the background swapped to give an inverse effect.


1 Answer 1


If all you need is drawing, simply swapping the colors of the background and the mesh will give you exactly the result you described, of course. If that's not an option for some reason, a simple color replacement shader should suffice.

If you do need to make an inverse mesh for other purposes, I found a simple way to achieve it. It may not apply to the way you're drawing it your mesh, but is a useful way to think about the problem, and took me surprisingly long to realize.

I needed to do it and found this old unanswered question, so after I found my solution I thought I would come here and explain. Here's what I did:

Make a square and set your mesh grid on it. In my case, I made it using a 3x3 grid.

Turning a square with a 3x3 mesh grid into a fish

Tada! A fish.

Then, hollow out a square to make a donut like shape. Make a second mesh, with 2 more grid points in each dimension (in this case, 5x5). The empty part of your donut should line up with internal points of your 5x5 grid.

These internal points are exactly the same as a 3x3 grid, so simply set them to the same positions as your fish equivalent, and you will have your inverse mesh!

Turning a hollowed out square with a 5x5 mesh grid into a square with a fish-shaped hole inside

Tada! A not fish.

  • \$\begingroup\$ Welcome to GDSE & thanks for contributing. It's not clear to me that this post solves the same problem presented by the question. The initial mesh isn't necessarily mesh grid & the result isn't tightly bounded (i.e. the inverse mesh shown in the example problem isn't a 'donut'). \$\endgroup\$
    – Pikalek
    Commented May 19, 2021 at 16:26
  • \$\begingroup\$ Oh, sorry. I don't know how to solve exactly the question asked, but I found it when looking for how to solve my own problem and though it was similar enough that it was worth posting my solution so the next person could find it. Do you think it would be best if I deleted it? Perhaps I could post another question describing my own needs and place this answer there? Thanks! \$\endgroup\$ Commented May 19, 2021 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .