Created
August 12, 2019 17:47
-
-
Save planaria/872a015c35cb836116e3aafdf2c1fcfb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Effect/Glass" | |
{ | |
Properties | |
{ | |
_Color("Color", Color) = (1,1,1,1) | |
_Shift("Shift", float) = 0.3 | |
_Chroma("Chroma", float) = 0.01 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"RenderType"="Transparent" | |
"Queue"="Transparent" | |
} | |
GrabPass { } | |
Cull Front | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
sampler2D _GrabTexture; | |
float _Shift; | |
float _Chroma; | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float4 localPos : POSITION1; | |
float3 normal : NORMAL; | |
float3 localNormal : TEXCOORD1; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.localPos = v.vertex; | |
o.normal = UnityObjectToWorldNormal(v.normal); | |
o.localNormal = v.normal; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float3 p = i.localPos.xyz; | |
float3 v = -normalize(ObjSpaceViewDir(i.localPos)); | |
float3 n = i.normal * dot(v, i.normal); | |
float3 p1 = p + n * _Shift; | |
float3 p2 = p + n * (_Shift + _Chroma); | |
float3 p3 = p + n * (_Shift + _Chroma * 2.0); | |
float4 wp1 = UnityObjectToClipPos(float4(p1, 1.0)); | |
float4 wp2 = UnityObjectToClipPos(float4(p2, 1.0)); | |
float4 wp3 = UnityObjectToClipPos(float4(p3, 1.0)); | |
float4 sp1 = ComputeGrabScreenPos(wp1); | |
float4 sp2 = ComputeGrabScreenPos(wp2); | |
float4 sp3 = ComputeGrabScreenPos(wp3); | |
fixed4 col1 = tex2Dproj(_GrabTexture, sp1); | |
fixed4 col2 = tex2Dproj(_GrabTexture, sp2); | |
fixed4 col3 = tex2Dproj(_GrabTexture, sp3); | |
fixed4 col; | |
col.r = col1.r; | |
col.g = col2.g; | |
col.b = col3.b; | |
// col.r = col1.r * 0.6 + col2.r * 0.3 + col3.r * 0.1; | |
// col.g = col1.g * 0.2 + col2.g * 0.6 + col3.g * 0.2; | |
// col.b = col1.b * 0.1 + col2.b * 0.3 + col3.b * 0.6; | |
col.a = 1.0; | |
return col; | |
} | |
ENDCG | |
} | |
GrabPass { } | |
Cull Back | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
sampler2D _GrabTexture; | |
float4 _Color; | |
float _Shift; | |
float _Chroma; | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float4 localPos : POSITION1; | |
float3 normal : NORMAL; | |
float3 localNormal : TEXCOORD1; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.localPos = v.vertex; | |
o.normal = UnityObjectToWorldNormal(v.normal); | |
o.localNormal = v.normal; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float3 p = i.localPos.xyz; | |
float3 v = -normalize(ObjSpaceViewDir(i.localPos)); | |
float3 n = i.normal * dot(v, i.normal); | |
float3 p1 = p + n * _Shift; | |
float3 p2 = p + n * (_Shift + _Chroma); | |
float3 p3 = p + n * (_Shift + _Chroma * 2.0); | |
float4 wp1 = UnityObjectToClipPos(float4(p1, 1.0)); | |
float4 wp2 = UnityObjectToClipPos(float4(p2, 1.0)); | |
float4 wp3 = UnityObjectToClipPos(float4(p3, 1.0)); | |
float4 sp1 = ComputeGrabScreenPos(wp1); | |
float4 sp2 = ComputeGrabScreenPos(wp2); | |
float4 sp3 = ComputeGrabScreenPos(wp3); | |
fixed4 col1 = tex2Dproj(_GrabTexture, sp1); | |
fixed4 col2 = tex2Dproj(_GrabTexture, sp2); | |
fixed4 col3 = tex2Dproj(_GrabTexture, sp3); | |
fixed4 col; | |
col.r = col1.r; | |
col.g = col2.g; | |
col.b = col3.b; | |
col.a = 1.0; | |
float d = abs(dot(v, i.localNormal)); | |
col.rgb *= 1.0 + (0.3 - min(d, 0.3)) * 10.0; | |
return col * _Color; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment