- Home /
Geometry Intersection Shader
I have seen innumerable examples on how to perform this, yet none of them work anymore, or they only allow one instance of this intersection to appear.
I would like to recreate the intersection shader from The Divison, as seen here
Characters and world geometry alike are highlighted inside a sphere. As well, the intensity of the highlight is a gradient, where in the center it is less intense, and on the edges, it is most intense.
How can I start this? Will I need to render a sphere several times and use the stencil buffer?
Answer by Glurth · Mar 31, 2018 at 09:20 PM
This shader "colors" an area inside a "rendered" mesh: for you case, simply create a material with it, and apply it to a sphere (or whatever) placed appropriately in worldspace. It DOES lack the gradient you mentioned, but it's a start. (the sample picture uses 2 concentric spheres with different materials, both using this shader)
Shader "Unlit/highlightVolume"
{
Properties
{
_Color("Color", Color) = (1,1,1,.2)
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
Pass
{
Stencil
{
Ref 172
Comp Always
Pass Replace
ZFail Zero
}
Blend Zero One
Cull Front
ZTest GEqual
ZWrite Off
}// end stencil pass
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Stencil
{
Ref 172
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return _Color;
}
ENDCG
}//end color pass
}
}
This is useful, however if the camera is inside the object it is applied to, the highlight disappears.
This works great in builtin, but doesn't in URP. Any chance you would know why?
This shader uses the stencil flags, which, as far as I've been able to tell, are not available in neither URP nor HDRP.
From the docs it seems that it should be supported for Unity 2020.3 https://docs.unity3d.com/Manual/SL-Stencil.html
A user suggested that multiple passes might be a problem https://forum.unity.com/threads/intersection-shader-cull-front-$$anonymous$$us-cull-back.536812/
Not sure why that would be as the docs also mention multiple passes
Answer by Bunny83 · Mar 31, 2018 at 01:32 AM
Well, since you want the sphere highlighting to not be visible behind the car in the front you have to use the stencil buffer. A common implementation could be done by useng a two pass stencil shader. The first pass would render just the front faces of the sphere (cull back) and set the stencil value on pass. The second pass would only render the backfaces of the sphere (cull front) and do a stencil compare to only pass if the stencil value is "1". The important part of the second pass is that you now only draw when the ztest fails (ztest greater). That means when the backfaces are actually behind something else. The second pass would use alpha blending to render those backfaces. Note that you would need to do the fading based on the distance of each fragment from the center of the sphere.
Note that this technique may give you problems when your camera would "enter" the sphere area. If you want / need to support this case you basically have to implement the usual depth fail stencil shadows. It works similar to this one but instead of actual rendering the sphere we only increment / decrement the stencil value. You would need a thrid pass to actually rendering the sphere visually.
Ok, I've done that by having three individual spheres with the stencils.
I'm somewhat stuck on how to get the depth of the pixel which occluded the comparison, in order to fade out the center
Well I just realized even what I have is still broken. In the editor, it shows just fine, but my main camera shows parts of the sphere which shouldn't be drawn?
Uhm we don't know what your shader does since you haven't shown your code. However i suspect you have the same problem as in this question?
Your answer

Follow this Question
Related Questions
"Sand/grain" URP Shader Artifact On Borders After % Operation 1 Answer
how do you make a texture only show near edges at certain angles 0 Answers
Shader Graph transparency issue 0 Answers
Shader white on Quest 2 when reflection intensity > 0 1 Answer
How do I keep an object lit when a light moves past it? 0 Answers