Skip to content

Instantly share code, notes, and snippets.

@samiede
Created September 30, 2021 08:06
Show Gist options
  • Save samiede/71eea2bbcab4db2474410b01371bbe5b to your computer and use it in GitHub Desktop.
Save samiede/71eea2bbcab4db2474410b01371bbe5b to your computer and use it in GitHub Desktop.
Shader "Unlit/DepthEstimationShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass {
Cull Off
Blend One One
BlendOp Min
ZTest Off
ZWrite On
CGPROGRAM
// Upgrade NOTE: excluded shader from DX11; has structs without semantics (struct v2f members depth)
#pragma exclude_renderers d3d11
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f {
float4 vertex: POSITION;
float depth: TEXCOORD1;
};
v2f vert (appdata_full v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.depth = o.vertex.z / o.vertex.w;
#if !defined(UNITY_REVERSED_Z) // basically only OpenGL
o.depth = 0.depth * 0.5 + 0.5; // remap -1 to 1 range to 0.0 to 1.0
#endif
return o;
}
half4 frag( v2f i, float facing : VFACE ) : COLOR
{
return facing > 0 ? float4(i.depth, 1, 0, 1) : float4(1, i.depth, 0, 1);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment