- Home /
Shader Edit Help: 2D Outline Coloring
Hi everyone. Below is an Shadow outline shader for 2D Textures and Sprites. Could someone add in a variable to change the color of the texture outline? Tried adding in _Color but placement give me an error. Your help is appreciated.
Shader "Custom/Outline_2DSprite" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_OutLineSpread ("Outline Spread", Range(0,0.012)) = 0.007
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite On Blend One OneMinusSrcAlpha Cull Off
LOD 110
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float _OutLineSpread;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 mainColor = (tex2D(_MainTex, IN.uv_MainTex+float2(_OutLineSpread,_OutLineSpread)) + tex2D(_MainTex, IN.uv_MainTex-float2(_OutLineSpread,_OutLineSpread))) * fixed4(0,0,0,1);
fixed4 addcolor = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend One OneMinusSrcAlpha Cull Off Fog { Mode Off }
LOD 100
Pass {
Tags {"LightMode" = "Vertex"}
ColorMaterial AmbientAndDiffuse
Lighting On
SetTexture [_MainTex]
{
Combine texture * primary double, texture * primary
}
}
}
Fallback "Diffuse", 1
}
Answer by Bieere · Nov 27, 2014 at 03:27 PM
Shader "Custom/Outline_2DSprite"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_OutLineSpreadX ("Outline Spread", Range(0,0.012)) = 0.007
_OutLineSpreadY ("Outline Spread", Range(0,0.012)) = 0.007
_Color("Outline Color", Color) = (1.0,1.0,1.0,1.0)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite On Blend One OneMinusSrcAlpha Cull Off
LOD 110
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float _OutLineSpreadX;
float _OutLineSpreadY;
float4 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 mainColor = (tex2D(_MainTex, IN.uv_MainTex+float2(_OutLineSpreadX,_OutLineSpreadY)) + tex2D(_MainTex, IN.uv_MainTex-float2(_OutLineSpreadX,_OutLineSpreadY))) * _Color.rgba;
fixed4 addcolor = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend One OneMinusSrcAlpha Cull Off Fog { Mode Off }
LOD 100
Pass {
Tags {"LightMode" = "Vertex"}
ColorMaterial AmbientAndDiffuse
Lighting off
SetTexture [_MainTex]
{
Combine texture * primary double, texture * primary
}
}
}
Fallback "Diffuse", 1
}
Better late than never, but here ya go!
Expanding on your shader, I made some tweaks and fixes, especially with the overlapping textures only an issue with transparent outlines the color error and the issue where having x and y spread looked wierd, since other people (like me) will also find it useful. Im also gonna link people to this ass some people in the forums need this.
Shader "Custom/Outline_2DSprite"
{
Properties
{
_$$anonymous$$ainTex ("Base (RGB)", 2D) = "white" {}
_OutLineSpreadX ("Outline Spread", Range(0,0.03)) = 0.007
_OutLineSpreadY ("Outline Spread", Range(0,0.03)) = 0.007
_Color("Outline Color", Color) = (1.0,1.0,1.0,1.0)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend SrcAlpha One$$anonymous$$inusSrcAlpha Cull Off
Lighting Off
LOD 110
CGPROGRA$$anonymous$$
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_$$anonymous$$ainTex;
fixed4 color : COLOR;
};
sampler2D _$$anonymous$$ainTex;
float _OutLineSpreadX;
float _OutLineSpreadY;
float4 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
fixed4 TempColor = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex+float2(_OutLineSpreadX,0.0)) + tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex-float2(_OutLineSpreadX,0.0));
TempColor = TempColor + tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex+float2(0.0,_OutLineSpreadY)) + tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex-float2(0.0,_OutLineSpreadY));
if(TempColor.a > 0.1){
TempColor.a = 1;
}
fixed4 AlphaColor = (0,0,0,TempColor.a);
fixed4 mainColor = AlphaColor * _Color.rgba;
fixed4 addcolor = tex2D(_$$anonymous$$ainTex, IN.uv_$$anonymous$$ainTex) * IN.color;
if(addcolor.a > 0.95){
mainColor = addcolor;
}
o.Albedo = mainColor.rgb;
o.Alpha = mainColor.a;
}
ENDCG
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
ZWrite Off Blend One One$$anonymous$$inusSrcAlpha Cull Off Fog { $$anonymous$$ode Off }
LOD 100
Pass {
Tags {"Light$$anonymous$$ode" = "Vertex"}
Color$$anonymous$$aterial AmbientAndDiffuse
Lighting off
SetTexture [_$$anonymous$$ainTex]
{
Combine texture * primary double, texture * primary
}
}
}
Fallback "Diffuse", 1
}
Thank you! just note: fixed4 AlphaColor = (0,0,0,TempColor.a); this line fails on mobile devices. Simply fixed4 AlphaColor = TempColor.a; worked for me.
Hey @TVishahan! This shader is super good, I've been searching for something like this for quite a while. But I was wondering. Would it be possible for it to detect if multiple sprites that use this shader are overlapping, so it only renders the outline on the outer borders of the composed sprite? (as if the sprites involved were merged into one)
Answer by Dan-MacDonald · Mar 17, 2015 at 08:35 AM
This doesn't compile anymore in Unity 5.0.
I was able to get it somewhat working by changing the line
fixed4 AlphaColor = (0,0,0,TempColor`.a);
to
fixed4 AlphaColor = fixed4(TempColor.a,TempColor.a,TempColor.a,TempColor.a);
I'm not totally sure what changed between unity 4.6 and 5.0 and if my change is even a good one.
I'm assu$$anonymous$$g that Unity5 didn't like 0 being put in place since it's suppose to be floating point. So I'm going to check this out when I get home, but I'm assu$$anonymous$$g that Unity wants the 0 to be 0.0f
fixed4 AlphaColor = fixed4(0.0f,0.0f,0.0f,TempColor.a);
Would probably work as well.
Your answer
Follow this Question
Related Questions
Sprite obstruction transparent area 1 Answer
How can i get mixed texture from one shader and set it for another object with another shader? 0 Answers
QuickOutline: how can I change the shader to work with objects with two materials? 3 Answers
Soft edge shader - Issues when objects using same material overlap 0 Answers
Materials are colored differently on rotated objects 1 Answer