• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Brandondpbell · Apr 04, 2014 at 06:14 PM · shadertexturespritematerialoutline

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
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TVishahan · Feb 06, 2015 at 05:47 PM 2
Share

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
   }



alt text

screenshot-2015-02-06-195010.png (7.7 kB)
avatar image blueknee TVishahan · Apr 08, 2016 at 07:08 AM 0
Share

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.

avatar image ViicEsquivel TVishahan · May 26, 2017 at 02:50 PM 0
Share

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)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pickleninja · Oct 14, 2015 at 05:49 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sprite obstruction transparent area 1 Answer

Pixel snap for material made with Unlit Shader Graph? 1 Answer

Multiple materials on same object 0 Answers

How to apply different textures to one model? 1 Answer

2D Sprite Always Visible - Shader / Material not working (ZTest, ZWrite, Culling) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges