- Home /
Partial object culling/cutoff
Essentially, are there any tried and true techniques for partial culling of objects in Unity that are of the same mesh? Such as an object that is on one side of a plane is no longer rendered, yet it is on the frontside.
Maybe even a shader that 'skips' a certain z distance? Like viewing through a portal that skips to the other side. So i could have a cube or a sphere with this 'portal shader' and it would hide anything inside of it.
examples:
Pulling a sword from an invisible portal, the sword is only drawn as it gets pulled out of the air.
Cheshire cat smile. (not really a planar cutoff)
Loss of limb. (fancy planar cutoff?)
Answer by Julien-Lynge · Jul 24, 2012 at 11:04 PM
This is an opinion question, so I'm giving you an opinion answer of how I would do it. There are many, many other ways both on the shader side and the mesh side.
The idea: use a shader that reads mesh vertex color (http://docs.unity3d.com/Documentation/ScriptReference/Mesh-colors.html). There are different ways to do this depending on whether your shader is written with ShaderLab, vertex/fragment (Cg/HLSL), or as a surface shader. How to do this is a separate question, but here's a couple links to get you started:
ShaderLab binding of vertex color for use in the shader: http://docs.unity3d.com/Documentation/Components/SL-BindChannels.html
Cg shaders: http://answers.unity3d.com/questions/189584/how-to-get-vertex-color-in-a-cg-shader.html
Whenever you need to calculate which vertices of the mesh are visible and which are not (every frame while your sword is getting pulled), you loop through the vertices and assign the vertex color like this pseudocode:
foreach (vertex in the mesh)
{
if (vertex is in the bounding space you want to be invisible)
vertex color = white
else
vertex color = clear
}
Now all you have to do in your shader is multiply the color by the vertex color. Multiplying by white won't change the color, while multiplying by clear will result in clear. You could even do fancy effects like energy fade-ins: if inside the bounding area, make clear, if just outside the bounding area, fade from clear to bright green (or whatever color), if further out fade from bright green to white.
For your last option (loss of limb), you'd have to do some procedural mesh generation that's worth a separate question and a bit of math. Rather than changing the vertex color of vertices in the bounding area, you'd remove them from the mesh entirely, cut their triangles with the plane of the bounding area, build a ring of the cut triangles, and create a mesh around the ring.
That's awesome. Thanks for both solutions, gave me a clearer idea of what I'm looking for. I found an alternative solution that's an acceptable workaround in the meantime though (my objects are static, and only will be viewed from one hemisphere). I'll definitely look into this later on if I come across a similar predicament.
What kind of effect would I see of multiplying a vertex by clear, if the adjacent vertex outside of the bounding area is fairly far away? Would it fade from one vertex to the next, or would that all depend entirely on how the shader itself renders it?
Yeah, it would interpolate between the vertices. If you wanted it to be a sharp cutoff, other solutions (procedural mesh deformation or code at the vertex shader level) would probably be better.
Answer by cowlinator · Feb 10, 2015 at 02:10 AM
You could use Clip(), as seen in "Slices via World Space Position" at http://docs.unity3d.com/Manual/SL-SurfaceShaderExamples.html
I've heard mixed opinions about the performance of Clip() ( http://forum.unity3d.com/threads/clip.257310/#post-1701475 )
Your answer
![](https://koobas.hobune.stream/wayback/20220613074644im_/https://answers.unity.com/themes/thub/images/avi.jpg)