Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created October 9, 2015 03:00
Show Gist options
  • Save mattatz/2b2a29c564a5028a9fd4 to your computer and use it in GitHub Desktop.
Save mattatz/2b2a29c564a5028a9fd4 to your computer and use it in GitHub Desktop.
Post effect shader for Unity : In multiple displays environment, this effect removes display's bezel area.
Shader "PostEffect/Bezel"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_HorizontalCount ("Horizontal Screen Count", Int) = 7
_VerticalCount ("Vertical Screen Count", Int) = 2
_Bezel ("Bezel Size", Vector) = (0.01, 0.01, -1, -1)
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata IN) {
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.uv = IN.uv;
return OUT;
}
sampler2D _MainTex;
int _HorizontalCount;
int _VerticalCount;
float2 _Bezel;
fixed4 frag (v2f IN) : SV_Target {
float2 uv = IN.uv;
float hp = 1.0 / _HorizontalCount;
float vp = 1.0 / _VerticalCount;
int htimes = floor(uv.x / hp);
int vtimes = floor(uv.y / vp);
float fromMinX = htimes * hp;
float fromMaxX = (htimes + 1) * hp;
float toMinX = htimes * hp + _Bezel.x * 0.5;
float toMaxX = (htimes + 1) * hp - _Bezel.x * 0.5;
float tx = (uv.x - fromMinX) / (fromMaxX - fromMinX);
uv.x = lerp(toMinX, toMaxX, tx);
float fromMinY = vtimes * vp;
float fromMaxY = (vtimes + 1) * vp;
float toMinY = vtimes * vp + _Bezel.y * 0.5;
float toMaxY = (vtimes + 1) * vp - _Bezel.y * 0.5;
float ty = (uv.y - fromMinY) / (fromMaxY - fromMinY);
uv.y = lerp(toMinY, toMaxY, ty);
return tex2D(_MainTex, uv);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment