Skip to content

Instantly share code, notes, and snippets.

@andrewgotow
Created September 11, 2016 16:52
Show Gist options
  • Save andrewgotow/cebb2e9eb772ca9e79fec1c7699bfe88 to your computer and use it in GitHub Desktop.
Save andrewgotow/cebb2e9eb772ca9e79fec1c7699bfe88 to your computer and use it in GitHub Desktop.
Unity - Depth Intersection Shader
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 = mul(UNITY_MATRIX_MVP, 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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment