For a project of mine, I wanted to have custom clipping planes for objects, so that if an object is intersection with another, it would hide any part after the intersection.
It looks like this:
I decided to extend the Standard shader provided by Unity3D to achieve this effect.
If you do not care about the technical details, skip to the bottom!
Warning, this post will get quite technical.
You can find the Unity3D shader sources from the Unity3D Download Archive.
A clipping plane can be defined by 2 vectors, a position and a normal.
These two vectors can be used to check whether parts of shapes are in front or behind the plane. We keep the parts in front, and hide the parts behind.
Wolfram Mathworld describes the algorithm to get the distance of a point to a plane. Here it is in code:
In order to find which parts of the objects are to be clipped, we need to extract the world coordinate of all points to be rendered. This is already done for most of the standard shader’s vertex programs:
In the fragment programs, we can then use the clip function with the distance to the plane as the parameter. If the clip function is called with any number less than zero, it will discard the current pixel. This is perfect, because if the distance to the plane is less than zero, a point is behind the plane.
If you have more planes, you can call clip with float2, float3, float4 parameters, or call clip multiple times. For example:
All we need to do now is change all passes of the Standard shader and modify the vertex and fragment programs to call this function.
We will use Unity3D’s wonderful shader program variants feature for this, so that if we do not want any clipping planes it will not cause any performance hits as the code will just be eliminated in that case. The CLIP_TWO and CLIP_THREE definitions are produced by the shader variant system, because in each pass we will have this directive :
It basically tells Unity3D’s shader compiler to generate four variants of the shader, a variant with no clipping planes, a variant with one clipping plane, another variant with two clipping planes, and the last one with three. We can choose how many clipping planes we want to use, by for example enabling the CLIP_ONE keyword, or the CLIP_TWO keyword. The method to enable keywords is: Material.EnableKeyword.
Create a new shader called StandardClippable.shader, and place it in your project’s Assets/Shaders directory. Copy the contents of the Standard.shader file in the builtin_shaders zip, which can be found inside the DefaultResourcesExtra directory. Paste into the StandardClippable.shader. Change the first line to be:
Add the properties for the plane positions and normals, so that the properties block will look like this:
We just added the lines after 41.
We will create a helper .cginc file named “plane_clipping.cginc”. Here is its contents:
The comments in the above file should explain what it is doing.
The next step is to use the PLANE_CLIP macro in the fragment programs of all passes.
The First Pass
Let’s look at the FORWARD pass for example:
The lines which are important are 28, 29 and 31. This pass uses the vertForwardBase vertex program, and the fragForwardBase fragment program. These programs are defined in the UnityStandardCore.cginc file.
So, find the UnityStandardCore.cginc file in the default shaders zip. Make a copy of it, and save it as standard_clipped.cginc next to our StandardClippable.shader file.
Change all references of “UnityStandardCore.cginc” to be “standard_clipped.cginc” instead.
Also, add the line
#pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE
just above the include lines. The forward pass should now look like this:
And your Shaders folder should now have three files:
- plane_clipping.cginc and
Let’s open the standard_clipped.cginc file. Add this line to the top of the file:
Place it just below the include for
AutoLight.cginc. This now allows us to use the functions and macros defined in that file.
We will be editing the vertex program first. Here it is as copied from the file:
The vertex program will need to pass the world position to the fragment program. It currently does so only if UNITY_SPECCUBE_BOX_PROJECTION is defined (relevant lines in above snippet: 12 and 23).
Change the lines
#if UNITY_SPECCUBE_BOX_PROJECTION || PLANE_CLIPPING_ENABLED
There should be one inside the struct definition just above the function, and one within the function. This way, the posWorld vector will be passed onto the fragment shader to be used by plane clipping.
The next step is the fragment program:
This uses the
Which uses the IN_WORLDPOS macro in order to get the world position if necessary.
The world position is acquired only if UNITY_SPECCUBE_BOX_PROJECTION (similar to above) is defined, so change the line
#if UNITY_SPECCUBE_BOX_PROJECTION || PLANE_CLIPPING_ENABLED
The lines up to FRAGMENT_SETUP should now look like:
And finally, let’s add the plane clipping to the fragment shader. Place this line
Your fragment shader code should now look like this:
This fixes the FORWARD pass!
The next pass is the FORWARD_ADD.
Let’s make the vertex shader pass the world position to the fragment shader:
Add these lines before the closing brace of the VertexOutputForwardAdd struct:
We use TEXCOORD9 because 8 was used just above it.
And in the vertForwardAdd function, add the lines
Just after this line:
float4 posWorld = mul(_Object2World, v.vertex);
The fragment shader uses the FRAGMENT_SETUP_FWDADD macro, which is defined just below FRAGMENT_SETUP.
Above the FRAGMENT_SETUP_FWDADD macro, add these lines:
#define IN_WORLDPOS_FWDADD(i) i.posWorld
#define IN_WORLDPOS_FWDADD(i) half3(0,0,0)
And use the newly created
IN_WORLDPOS_FWDADD macro instead of the
half3(0,0,0) as the last parameter for FragmentSetup. Here’s how the relevant lines should look:
And finally, you can now call the
PLANE_CLIP(s.posWorld) macro right after
FRAGMENT_SETUP_FWDADD(s) inside fragForwardAdd.
Here’s the code for the VertexOutputForwardAdd struct, vertForwardAdd function and fragForwardAdd function altogether:
And here’s what the pass definition in StandardClippable.shader should look like:
The shadow pass uses another file, UnityStandardShadow.cginc. Make a copy of this file and save it as standard_shadow_clipped.cginc. In the shadow pass definition, include the standard_shadow_clipped.cginc instead of UnityStandardShadow.cginc, and don’t forget the #pragma declarations!
Inside standard_shadow_clipped.cginc, include plane_clipping.cginc as usual.
In some conditions, the shadow vertex shader does not use an output struct. We want to ensure that we have the output struct so that we can pass the world position. around line 27, change the code so that it looks like this:
Inside the VertexOutputShadowCaster struct (around line 50), add the posWorld parameter:
And finally, just after it, here’s the modified vertShadowCaster and fragShadowCaster functions:
Guess what we added 😉
The deferred pass is very similar to the forward pass. Try to do it yourself 🙂 If you have any trouble, write a comment below, and I will help you!
Lod 150 passes
You just need to use the correct include files and the #pragma declarations in these passes.
An example script
Here’s an example that shows how to use the EnableKeyword method correctly and define the clipping planes for the shader.
You can find all the code on Github, in Unity3D-Plane-Clipping project I created.