Hello all. I’m trying to write a shader that renders a plane white wherever it intersects with other geometry, otherwise it’s a solid black. Refer to the image below:
I’m completely new to Unity, and started using it for my college research project couple of months ago. The point of this shader is to fake the black and white image effect of a ultrasound (a very barebones look is enough). I’ll parent a camera to the plane with the shader, that only renders the plane and nothing else, and have that displayed on a secondary display area on the screen. I figured a intersection shader would be the simplest approach, but I’ve actually not found a single example online.
I’ve seen cross section ones, where the geometry is actually cut away and anything below the plane is filled with a solid color. But that’s beyond my needs. I’ve also found a Winston Barrier example from the youtuber “Making Stuff Look Good” that sort of does what I want, but not really. I’ve also found this example:
Shader "Unlit/Intersection Glow"
{
Properties
{
_Color ("Color", Color) = (1,0,0,1)
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
Blend One One // additive blending for a simple "glow" effect
Cull Off // render backfaces as well
ZWrite Off // don't write into the Z-buffer, this effect shouldn't block objects
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 screenPos : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _CameraDepthTexture; // automatically set up by Unity. Contains the scene's depth buffer
fixed4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//Get the distance to the camera from the depth buffer for this point
float sceneZ = LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)).r);
//Actual distance to the camera
float fragZ = i.screenPos.z;
//If the two are similar, then there is an object intersecting with our object
float factor = 1-step( 0.1, abs(fragZ-sceneZ) );
return factor * _Color;
}
ENDCG
}
}
}
But it doesn’t seem to do anything other than make my geometry invisible. I read that I was supposed to not have “Zwrite Off”, so I commented it out but it still doesn’t to anything other than not render the Unity Wireframe lines inside the object with the shader. I’ve got a script that sends out the Depth buffer (from the WInston Example) attached to my camera, but it doesn’t make the shader work any better.
Any help as to how to best approach the shader would be most appreciated. Thanks.