- Home /
2D Outline shader issue: Apply outline on separate sprites as a whole?
Hi all,
so I have this 2d outline shader which is a modified version of the shader in the comment to this answer. The idea is to represent health in an outline, you start with full health = full outline = green. As hp goes down, the outline fill decreases and it becomes red.
The issue however is that I was testing it on a single sprite, when I came to the actual model that we're using, it turned out that it was a complex sprite made out of many renderers. Every piece acts on its own now :(
Please see this video demonstrating the problem.
Here's a simplified version of the shader without the "Fade to black" and "Fill inside" portions seen in the video, just the outline and thickness:
Shader "Custom/2DOutline"
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_Width ("Thickness", Range(0, 1)) = 0.5
_Fill ("Filling", Range(0 , 1)) = 1
}
SubShader
{
Lighting Off
LOD 110
CGPROGRAM
#pragma surface surf Lambert alpha
struct Input
{
float2 uv_MainTex;
fixed4 color : COLOR;
};
sampler2D _MainTex;
float _Width;
float _Fill;
void surf(Input IN, inout SurfaceOutput o)
{
if (IN.uv_MainTex.y > _Fill)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
else
{
_Width /= 100;
fixed4 TempColor = tex2D(_MainTex, IN.uv_MainTex + float2(_Width, _Width))
+ tex2D(_MainTex, IN.uv_MainTex - float2(_Width, _Width));
fixed4 OutlineColor = 0;
OutlineColor.r = (1 - _Fill) / (_Fill * _Fill);
OutlineColor.g = _Fill;
OutlineColor.b = 0;
OutlineColor.a = 1;
fixed4 AlphaColor = (0, 0, 0, TempColor.a);
fixed4 MainColor = AlphaColor * OutlineColor.rgba;
fixed4 TexColor = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
if(TexColor.a > 0.95)
MainColor = TexColor;
o.Albedo = MainColor.rgb;
o.Alpha = MainColor.a;
}
}
ENDCG
}
Fallback "Diffuse"
}
The goal is to make the shader work with multiple renderers the same way did with a single one i.e. have the outline applied on the whole model not on each individual piece. I'm not sure how to. I tried comparing IN.worldPos.y
to _Fill
but didn't work, I figured if I go with world space and not the texture coord things should improve, no dice.
Any ideas?
Thanks!
Edit: Forums crosslink.
Hi, I need something like this too! Did you find a solution since you asked this question?
Your answer
Follow this Question
Related Questions
Edge outline & 2D shadow Shader for planar mesh 0 Answers
Outline Overlapping Game Objects 1 Answer
2d lighting effect 1 Answer
Combine Shaders 0 Answers
one shader for multiple sprites 0 Answers