Skip to content

Instantly share code, notes, and snippets.

@hiepnd
Created January 11, 2016 01:39
Show Gist options
  • Save hiepnd/3e1dc2b41f9b4eec473a to your computer and use it in GitHub Desktop.
Save hiepnd/3e1dc2b41f9b4eec473a to your computer and use it in GitHub Desktop.
Shader "Screw/Silhouetted Diffuse" {
Properties {
_Color ("Main Color", Color) = (.5,.5,.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (0.0, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" { }
_Alpha ("Alpha", Range(0, 1)) = 1
}
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
half2 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : POSITION;
float4 color : COLOR;
half2 uv : TEXCOORD0;
};
uniform float _Outline;
uniform float4 _OutlineColor;
v2f vert(appdata v) {
// just make a copy of incoming vertex data but scaled according to normal direction
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
float2 offset = TransformViewToProjection(norm.xy);
o.pos.xy += offset * o.pos.z * _Outline;
o.color = _OutlineColor;
return o;
}
ENDCG
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
Name "OUTLINE"
Cull Off
ZWrite Off
ZTest Always
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half _Alpha;
half4 frag(v2f i) :COLOR {
return half4(i.color.rgb, _Alpha);
}
ENDCG
}
Pass {
Name "BASE"
ZWrite On
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert2
#pragma fragment frag
v2f vert2 (appdata v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
return o;
}
uniform sampler2D _MainTex;
half4 _Color;
half _Alpha;
half4 frag (v2f i) : COLOR
{
half4 c = tex2D(_MainTex, i.uv) * _Color;
c.a = _Alpha;
return c;
}
ENDCG
}
}
Fallback "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment