2D Outline Shader (SpriteRender vs Quad)
I've got a pretty simple outline shader for 2D sprites going. If alpha pixel, look for surrounding pixels for non alpha pixel (see code below).
It works fine on a quad (left) but not on SpriteRenderer (right). I need it to work with sprite renderers in Unity. It's like the SpriteRenderer creates a different mesh based on the sprite/texture used in it. Anyone know what I'm missing!?
It works fine on a quad (left) but not on SpriteRenderer (right). I need it to work with sprite renderers in Unity. It's like the SpriteRenderer creates a different mesh based on the sprite/texture used in it. Anyone know what I'm missing!?
fixed4 SampleSpriteTexture (float2 uv) { fixed4 color = tex2D (_MainTex, uv); return color; } fixed4 frag(v2f IN) : COLOR { fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color; c.rgb *= c.a; fixed4 outlineC = _OutlineColor; outlineC.rgb *= outlineC.a; //outlineC.a *= ceil(c.a); if (c.a == 0.0) { fixed upAlpha = SampleSpriteTexture ( IN.texcoord + fixed2(0, _OutlineThickness)).a; fixed downAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(0, _OutlineThickness)).a; fixed rightAlpha = SampleSpriteTexture ( IN.texcoord + fixed2(_OutlineThickness, 0)).a; fixed leftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, 0)).a; fixed upRightAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, _OutlineThickness)).a; fixed upLeftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(_OutlineThickness, -_OutlineThickness)).a; fixed downRightAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(-_OutlineThickness, _OutlineThickness)).a; fixed downLeftAlpha = SampleSpriteTexture ( IN.texcoord - fixed2(-_OutlineThickness, -_OutlineThickness)).a; if (upAlpha != 0.0|| downAlpha != 0.0 || rightAlpha != 0.0 || leftAlpha != 0.0 || upRightAlpha != 0.0 || upLeftAlpha != 0.0 || downLeftAlpha != 0.0 || downRightAlpha != 0.0) return outlineC; } return c; }
outline-shader.gif
924 x 480 - 304K
Comments