x


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?)
more ▼

asked Jul 24 '12 at 10:44 PM

JChilton gravatar image

JChilton
33 1 1 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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:

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.

more ▼

answered Jul 24 '12 at 11:04 PM

Julien.Lynge gravatar image

Julien.Lynge
7.7k 20 25 52

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.

Jul 25 '12 at 01:44 AM JChilton

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?

Jul 25 '12 at 01:47 AM JChilton

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.

Jul 25 '12 at 04:24 AM Julien.Lynge
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1744
x150
x11
x7

asked: Jul 24 '12 at 10:44 PM

Seen: 842 times

Last Updated: Jul 25 '12 at 04:24 AM