Skip to content

Instantly share code, notes, and snippets.

@garzaa
Created July 1, 2019 18:32
Show Gist options
  • Save garzaa/226016958263fd57632d9e7c2ac589a5 to your computer and use it in GitHub Desktop.
Save garzaa/226016958263fd57632d9e7c2ac589a5 to your computer and use it in GitHub Desktop.
A Fresnel-shaded mirror for Unity.
// based on this asset: https://wiki.unity3d.com/index.php/MirrorReflection4
Shader "FX/Mirror"
{
Properties
{
_MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
[HideInInspector] _ReflectionTex ("", 2D) = "white" {}
}
SubShader
{
Tags {
"Queue"="Transparent+1"
"RenderType"="Transparent"
}
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float2 uv : TEXCOORD0;
float4 refl : TEXCOORD1;
float4 pos : POSITION;
float4 col: COLOR;
};
struct v2f
{
half2 uv : TEXCOORD0;
float4 refl : TEXCOORD1;
float4 pos : SV_POSITION;
fixed4 col: COLOR;
};
float4 _MainTex_ST;
fixed4 _Color;
v2f vert(appdata_t i)
{
v2f o;
o.pos = UnityObjectToClipPos (i.pos);
o.uv = TRANSFORM_TEX(i.uv, _MainTex);
o.refl = ComputeScreenPos (o.pos);
o.col = i.col * _Color;
return o;
}
sampler2D _MainTex;
sampler2D _ReflectionTex;
float4 _MainTex_TexelSize;
fixed4 SineDisplace(sampler2D _reflTex, float2 uv, fixed4 maskTex)
{
// poor man's Fresnel effect
float normY = -(uv.y - _MainTex_TexelSize);
// distort more towards the bottom of screen
uv.x += pow(normY/30, 2) * sin(500*((normY)));
fixed4 color = tex2D (_reflTex, uv);
color.rgb = lerp(color.rgb, _Color, sqrt(pow(normY / _MainTex_TexelSize, 2)));
color.a = maskTex.a;
return color;
}
fixed4 frag(v2f i) : SV_Target
{
//get the base reflector texture details
fixed4 maskTex = tex2D(_MainTex, i.uv);
//sample the 2d texture from the projected coordinates of the reflection
fixed4 refl = SineDisplace(_ReflectionTex, UNITY_PROJ_COORD(i.refl), maskTex);
return refl;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment