# Calculating the inverse mesh of a 2D mesh inside a bounding box

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.

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.