Skip to content

Instantly share code, notes, and snippets.

@obscuren
Created October 25, 2018 11:55
Show Gist options
  • Save obscuren/19f7a6b5b8c931f7ed146b85bacaf385 to your computer and use it in GitHub Desktop.
Save obscuren/19f7a6b5b8c931f7ed146b85bacaf385 to your computer and use it in GitHub Desktop.
A Unity3D Glass Shader
Shader "GridGames/GlassShader"
{
Properties {
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Colour ("Colour", Color) = (1,1,1,1)
_Bump ("Distortion", 2D) = "bump" {}
_Magnitude ("Magnitude", Range(0,1)) = 0.05
}
SubShader {
Tags { "Queue" = "Transparent" "IgnoreProjection" = "True" "RenderType" = "Opaque" }
ZWrite On Lighting Off Cull Off Fog { Mode Off } Blend One Zero
GrabPass { "_GrabTexture" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _GrabTexture;
sampler2D _MainTex;
fixed4 _Colour;
sampler2D _Bump;
float _Magnitude;
struct vin_vct
{
float4 vertex : POSITION;
float4 color : COLOR;
float4 texcoord : TEXCOORD0;
};
struct v2f_vct
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
float4 uvgrab : TEXCOORD1;
};
v2f_vct vert(vin_vct v)
{
v2f_vct o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.texcoord = v.texcoord;
o.uvgrab = ComputeGrabScreenPos(o.vertex);
return o;
}
half4 frag(v2f_vct i) : COLOR
{
half4 mainColour = tex2D(_MainTex, i.texcoord);
half4 bump = tex2D(_Bump, i.texcoord);
half2 distort = UnpackNormal(bump).rg;
i.uvgrab.xy += distort * _Magnitude;
fixed4 col = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
return col * mainColour * _Colour;
}
ENDCG
}
}
Fallback "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment