# Boolean Subtraction Operations on Mesh

I’m working on removing the intersecting part of a mesh from another mesh. Like this

The Concept I am trying is checking if a mesh triangle is within the volume of an object, using a ray that points outward away from the triangle, along it’s normal. That starts at its center and the direction of its normal.

My issue is this in my test I’m using two basic cubes offset of each other with a little overlap in there meshes. I found and calculated there triangles and center of each triangle and can cast the ray out from the center in the direction of the triangles normal. My concern here is that one ray from the center of each triangle will not give me an accurate representation of the intersection of the rest of the triangle. Any in sight on Mesh Intersection detection would be appreciated.

Is it Possible to create two planes using the vertices of a triangle from each cube that is touching and check them for collision?

Or is there some other obvious way of doing the mesh subtraction.

1 Like

I doubt the raycasting approach will work.

You may already know this, but the general term for this is constructive solid geometry, and a search for that term will likely yield some useful references.

CSG is often (or at least sometimes) implemented in terms of BSP trees, and often involves mesh/polygon clipping in one form or another. In general, the process is non-trivial and fairly susceptible to numerical error.

I haven’t implemented the specific operation you’re asking about myself, so I’ll leave it at that, but I’m sure there’s useful info available online that would be helpful.

1 Like

Did you get any further with this? Or has anyone else done or working on a Boolean system for Unity, got some ideas for a game but it really needs a bool system, or is it just not really feasible. I understand that anything but very basic meshes would not be realtime in the slightest.

There are two general ways, that I know:

1. To program pure boolean mesh operations “from zero” you need to solve several “Big problems” which usually stands in front of real 3D Editor like 3D Max, Blender, LightWave and so on. As I can remember, such big corpus as Autodesk, was unable to solve 3D Max Boolean operations problems till v5.

2. On other hand, If you need objects only looking like result of boolean operations, you have to try Stencil buffer for OpenGL. In OpenGL games with Stencil mask produces such effects like Volume light and shadow (which actually boolean operations), reflections.Try start from this example Tutorial - Stenciled Shadow Volumes in OpenGL - Josh Beam's Website

The first way is severe hard to program by indie, the second are closed since Unity 2 ( http://forum.unity3d.com/threads/24252-Rendering-to-the-stencil-buffer ), so you need to select first way or find some third way which I not see

It is a tough thing to get working, I had a go at it recently and managed to get it working with a lot of help. Did think about putting it in the Asset store but not sure if enough people would need it to justify the time to tidy it all up. The pics below show bool working with intersection and difference. For simple meshes it works in real time so simple holes in a wall etc would be no problem, the slightly more complex sphere to cube example here is about 400ms but I can optimize that a fair bit if it went onto the store.
Chris

2 Likes

Very nice work, SpookyCat! Will your approach work with concave (non convex) models?

Yes works with concave meshes, Ill put a grab up later, trying to figure out a nice demo to show it all off.

i would love to try this out!

Wow, this looks awesome!!

I’d love to try it out too!!

Cheers

I would love to have this, please let me be able to have this

Or point me in the direction you took to get this to work!

Hi Jan, I will be getting this out soon, I am just super busy with the MegaFiers system and new demo and new features but this is something we need working for our own game project so it will get done soon.
Chris

Wow, definitely interested in this! Keep us updated!

yes that is fantasic
My question is will it work on an animated mesh?

steve

can you provide the code, o a “how to” to do this? I need it. I’m very noob programing meshes. Thanks! ^^

well i dug this up last night and am trying to expand it to our needs
http://en.wikibooks.org/wiki/GLSL_Programming/Unity/Cutaways

this is as far as i got i dont seem to be getting the cutMatrix in to the shader

Pass {
Cull Off // turn off triangle culling, alternatives are:
// Cull Back (or nothing): cull only back faces
// Cull Front : cull only front faces

GLSLPROGRAM
// send in cutMatix

uniform vec4 cutMatrix;
uniform mat4 _Object2World; // model matrix
uniform mat4 _World2Object; // inverse model matrix
varying vec4 cutObjectInWorldSpace;
varying vec4 position_in_object_coordinates;
varying vec4 position_in_world_space;
varying vec4 cutPlaneNormal;

#ifdef VERTEX

void main()
{

position_in_object_coordinates = gl_Vertex;
)
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

cutPlaneNormal = vec4(0.32, 0.32, 0.32, 1.0); //works
// cutPlaneNormal = cutMatrix; //dosent work
position_in_world_space = _Object2World * gl_Vertex;

}

#endif

#ifdef FRAGMENT

void main()

{

float whichSide = dot(cutPlaneNormal,(cutMatrix - position_in_world_space));
if (whichSide > 0.01 )

{
discard; // stop processing the fragment if y coordinate is positive
}
if (gl_FrontFacing) // are we looking at a front face?
{
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0); // yes: blue
}
else
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); // no: red
}
}

#endif

ENDGLSL
}
}
}

Wow, thats great work SpookyCat! Please for the love of anything… put this up on the Asset store!

bump spookycat

I have written a Boolean operation plugin a while ago while creating some kind of destruction…Problem I had is that continuous boolean operations started adding a lot of artifact polygons that didn’t give the mesh a better look…So to fix that I had written a edge collapsing algorithm inplementation to reduce the number of polygons to normal.
What kind of algorithm you using Spooky?

Oh! its awesome!
i go to investigate the code, its unknown for me xDDD