- Home /
How to show only the intersection between two meshes at runtime?
Hi, I'm basically trying to do the same thing as described here: https://stackoverflow.com/questions/42278279/unity-intersections-mask
Desired effect:
With the caveat that the plane isn't exactly a plane but a (very large) 3D cone, and the camera I'm using has to be an orthographic camera(so no deferred rendering). I also need to do this basically every frame.
I've tried looking up various intersection depth shaders but they all seem to be done with the perspective camera.
Even then they don't render the non-intersecting parts of the 3D objects as transparent, instead coloring parts of them differently.
The linked stackoverflow question mentions rendering the plane normally as an opaque object, and then using a fragment shader to render only the part of objects that intersect the plane.
However based on my (admittedly)limited understanding of shaders, I'm uncertain of how to get around to doing this - as far as I know each fragment only has 1 value as it's depth, which is the distance from the near-clipping plane of the camera to the point on the object closest to the camera that is shown by that fragment/pixel.
Since the rest of the object is transparent in this case, and I need to show parts of the object that would normally be covered(and thus, from what I understand, depth not known), I can't see how I could only draw the parts that intersect my cone.
I've tried the following approaches other than using shaders:
Use a CSG algorithm to actually do a boolean intersect operation between the cone and objects and render that.
Couldn't do it because the CSG algorithms were to expensive to do every frame.
Try using the
contactPoints
from theCollision
generated by Unity to extract all points(vertices) where the two meshes intersect and construct a new mesh from those pointsThis led me down the path of 3D Delaunay triangulation, which was too much for me to understand, probably too expensive like the CSG attempt, and I'm pretty sure there is a much simpler solution to this problem given that I'm just missing here.
Ultimately it just seems to me like it comes back to shaders. How would I get around to achieving this effect? It doesn't necessarily have to be with shaders, anything that works is fine for me tbh. An example code would be great.
Your answer
Follow this Question
Related Questions
Seing through triangles in runtime edited mesh 0 Answers
Add Detail Mesh at runtime 1 Answer
Conforming a mesh path to arbitrary surface - runtime 1 Answer
Collision with mesh updated at runtime 0 Answers
Intersection of two game object 0 Answers