1+ // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
2+
3+ Shader "Custom/GlassBackfaceCull"
4+ {
5+
6+ Properties
7+ {
8+ _MainTex( "Texture" , 2D ) = "white" {}
9+ _Color( "Color" , Color ) = (1 , 1 , 1 , 1 )
10+ _EdgeColor( "Edge Color" , Color ) = (1 , 1 , 1 , 1 )
11+ _EdgeThickness( "Silouette Dropoff Rate" , float ) = 1.0
12+ }
13+
14+ SubShader
15+ {
16+ Tags
17+ {
18+ "Queue" = "Transparent"
19+ }
20+
21+ Pass
22+ {
23+ ZWrite Off
24+ Blend SrcAlpha OneMinusSrcAlpha // standard alpha blending
25+
26+ CGPROGRAM
27+
28+ #pragma vertex vert
29+ #pragma fragment frag
30+
31+ // Properties
32+ sampler2D _MainTex;
33+ uniform float4 _Color;
34+ uniform float4 _EdgeColor;
35+ uniform float _EdgeThickness;
36+
37+ struct vertexInput
38+ {
39+ float4 vertex : POSITION ;
40+ float3 normal : NORMAL ;
41+ float3 texCoord : TEXCOORD0 ;
42+ };
43+
44+ struct vertexOutput
45+ {
46+ float4 pos : SV_POSITION ;
47+ float3 normal : NORMAL ;
48+ float3 texCoord : TEXCOORD0 ;
49+ float3 viewDir : TEXCOORD1 ;
50+ };
51+
52+ vertexOutput vert (vertexInput input)
53+ {
54+ vertexOutput output;
55+
56+ // convert input to world space
57+ output.pos = UnityObjectToClipPos (input.vertex);
58+ float4 normal4 = float4 (input.normal, 0.0 );
59+ output.normal = normalize (mul (normal4, unity_WorldToObject ).xyz);
60+ output.viewDir = normalize (_WorldSpaceCameraPos - mul (unity_ObjectToWorld , input.vertex).xyz);
61+
62+ output.texCoord = input.texCoord;
63+
64+ return output;
65+ }
66+
67+ float4 frag (vertexOutput input) : COLOR
68+ {
69+ // sample texture for color
70+ float4 texColor = tex2D (_MainTex, input.texCoord.xy);
71+
72+ // apply silouette equation
73+ // based on how close normal is to being orthogonal to view vector
74+ // dot product is smaller the smaller the angle bw the vectors is
75+ // close to edge = closer to 0
76+ // far from edge = closer to 1
77+ float edgeFactor = abs (dot (input.viewDir, input.normal));
78+
79+ // apply edgeFactor to Albedo color & EdgeColor
80+ float oneMinusEdge = 1.0 - edgeFactor;
81+ float3 rgb = (_Color.rgb * edgeFactor) + (_EdgeColor * oneMinusEdge);
82+ rgb = min (float3 (1 , 1 , 1 ), rgb); // clamp to real color vals
83+ rgb = rgb * texColor.rgb;
84+
85+ // apply edgeFactor to Albedo transparency & EdgeColor transparency
86+ // close to edge = more opaque EdgeColor & more transparent Albedo
87+ float opacity = min (1.0 , _Color.a / edgeFactor);
88+
89+ // opacity^thickness means the edge color will be near 0 away from the edges
90+ // and escalate quickly in opacity towards the edges
91+ opacity = pow (opacity, _EdgeThickness);
92+ opacity = opacity * texColor.a;
93+
94+ float4 output = float4 (rgb, opacity);
95+ return output;
96+ }
97+
98+ ENDCG
99+ }
100+ }
101+
102+ }
0 commit comments