Skip to content

Instantly share code, notes, and snippets.

@keijiro
Last active July 5, 2018 11:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keijiro/01e009421a6aa4ac0bc06ab2431b82d3 to your computer and use it in GitHub Desktop.
Save keijiro/01e009421a6aa4ac0bc06ab2431b82d3 to your computer and use it in GitHub Desktop.
A Unity shader that detects flipped polygons.
Shader "FlipCheck"
{
SubShader
{
Cull Off
Pass
{
CGPROGRAM
#pragma vertex Vertex
#pragma fragment Fragment
#include "UnityCG.cginc"
struct Attributes
{
float4 position : POSITION;
half3 normal : NORMAL;
};
struct Varyings
{
float4 position : SV_POSITION;
half3 normal : NORMAL;
};
Varyings Vertex(Attributes input)
{
Varyings o;
o.position = UnityObjectToClipPos(input.position);
o.normal = UnityObjectToWorldNormal(input.normal);
return o;
}
fixed4 Fragment(Varyings input, fixed vface : VFACE) : SV_Target
{
fixed3 c = input.normal / 2 + 0.5;
c = lerp(c, half3(1, 0, 0), vface < 0);
c = GammaToLinearSpace(c);
return fixed4(c, 1);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment