- Home /
Double-sided cross section ("slice")
Hello, I've started a new project and can't force Unity to make a double-sided cross section of all meshes from z=-0.5 to z=0.5.
So far I've made a shader clipping all objects (and for "empty inside" ones works fine). However for "filled inside" objects I need to render planes from both sides, so it would look like a "slice" of any mesh.
I'm in a dead point atm, because in Unity we don't have boolean operations on meshes and shader can't render anything which is not present...
Maybe someone would have any great idea for any work-around? I'd appreciate any help ;)
Nothing I'm aware of, shy of coding your own boolean operations. Wouldn't envy that task. This is an interesting question though.
I've also thought about using that vertices on the edges we have after clipping (maybe z-coordinate comparison and adding to array?) and bulding face from them - but I can't imagine how they should be connected, because shapes may be very complex (and thus order of them).
Hope any veteran would clarify this a bit more ;)
I'm not familiar enough with shader lore to guarantee that this cannot be done on the GPU, but I've never seen the effect before AFAI$$anonymous$$.
Unless I'm badly mistaken, any solution is going to be non-trivial, involving a per-frame (if rot/scale/z-pos change), per-mesh boolean operation.
One option is to create a new mesh, dynamically, based upon your original mesh and the two clipping planes. As you mentioned there are no Boolean operators for solids, so you will need to do it manually. This is obviously not a trivial algorithm, as Sunny mentioned, and getting it fast enough for runtime use can be hard.
Two possible ways I would do this: For each vertex in the mesh, deter$$anonymous$$e if it lies between the two planes (in model space). If so, include it in the final mesh. This would be fine, if we didn't care what the edge of the mesh looked like.
The trickier part will be triangles of the original mesh that cross a plane boundary. I would do this part by checking each EDGE (any line segment between two vertices) of each triangle: if all triangle verticies lies between the two planes, keep the verticies, AND the triangle. but if one vertex lies between the planes, and the other does not, we need to compute WHERE the plane intersects the edge.
Once you have the intersection (see link below) you can create a new vertex at that point. Don't forget you will need to recreate the edge triangles: any triangles the planes intersect, with two vertices inside, will need to become TWO triangles (since a truncated triangle is a quad.) Also note that any triangle that intersects the plane will have TWO edges that intersect the plane (except in rare cases- where a triangle edge lies on the plane exactly).
So lets say you have triangle ABC, and the plane intersects AB and AC at D and E respectively. If A is between the planes: our new triangle will be ADE (B and C are replaced with the two new intersection) If B and C lie between the planes: our two new triangles will be DBC and EBC ( A is replaced with the two new intersections
Edit: Closing the remaining two open sides can be complex or simple depending on if your shapes are concave or not. If not concave, using one vertex as a base, create a triangle to every other vertex(peacock-style). (here I'm referring to vertices your created in the previous part, that lie exactly on a plane.)
This is one of my favorite wiki posts, I think it will have all the line/plane and other math functions you need to do the above. http://wiki.unity3d.com/index.php/3d_$$anonymous$$ath_functions
Your answer
Follow this Question
Related Questions
Trying to make a texture fill an entire Plane 0 Answers
Cross section of plane 0 Answers
Render wireframe on cut section of polygons 2 Answers
How do I fix these clouds? 0 Answers
Planes clipping through each other. 0 Answers