Skip to content

Instantly share code, notes, and snippets.

@edwardrowe
Last active June 17, 2018 00:47
Show Gist options
  • Save edwardrowe/bbb73a6c5cc632e3e75667f997da9e90 to your computer and use it in GitHub Desktop.
Save edwardrowe/bbb73a6c5cc632e3e75667f997da9e90 to your computer and use it in GitHub Desktop.
Unity ShaderLab Retro Cutout Lighting Shaders
Shader "RedBlueGames/Lighting/LightMapCutout"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DarknessColor ("Darkness Color", Color) = (0,0,0.18,.24)
}
SubShader
{
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
ZWrite Off
Cull Off
Blend One OneMinusSrcAlpha
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 v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _DarknessColor;
fixed4 frag (v2f i) : SV_Target
{
float4 color = _DarknessColor;
float4 textureColor = tex2D(_MainTex, i.uv);
color.a = 1 - textureColor.r;
color.rgb *= color.a;
return color;
}
ENDCG
}
}
}
Shader "RedBlueGames/Lighting/Max" {
Properties
{
[HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
[HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
[PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
[PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
_Color ("Tint", Color) = (1,1,1,1)
_pixelsPerUnit("Pixels Per Unit", Float) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
BlendOp Max
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment SpriteFrag
#pragma target 2.0
#pragma multi_compile_instancing
#pragma multi_compile _ PIXELSNAP_ON
#pragma multi_compile _ ETC1_EXTERNAL_ALPHA
#include "UnitySprites.cginc"
float _pixelsPerUnit;
float4 AlignToPixelGrid(float4 vertex)
{
// Note in Unity 5.4 _Object2World should be unity_ObjectToWorld
float4 worldPos = mul(unity_ObjectToWorld, vertex);
worldPos.x = floor(worldPos.x * _pixelsPerUnit + 0.5) / _pixelsPerUnit;
worldPos.y = floor(worldPos.y * _pixelsPerUnit + 0.5) / _pixelsPerUnit;
return mul(unity_WorldToObject, worldPos);
}
v2f vert(appdata_t IN)
{
#ifdef PIXELSNAP_ON
IN.vertex = AlignToPixelGrid(IN.vertex);
#endif
return SpriteVert(IN);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment