• 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
0
Question by emilio1984 · Sep 17, 2015 at 02:21 PM · unity 5shader

Unity 5.2.0 Outlined/Silhouetted Diffuse Shader problem

Hi all, I have a problem with Outlined/Silhouetted Diffuse shader in unity 5.2.0. The material outline color is OK(White) and I can change the color. But I can't change the black color in material. What is problem? I tried add a texture but no result. Please see the attached shader code and picture.

 Shader "Outlined/Silhouetted Diffuse" {
     Properties {
         _Color ("Main Color", Color) = (.5,.5,.5,1)
         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
         _Outline ("Outline width", Range (0.0, 0.03)) = .005
         _MainTex ("Base (RGB)", 2D) = "white" { }
     }
  
 CGINCLUDE
 #include "UnityCG.cginc"
  
 struct appdata {
     float4 vertex : POSITION;
     float3 normal : NORMAL;
 };
  
 struct v2f {
     float4 pos : POSITION;
     float4 color : COLOR;
 };
  
 uniform float _Outline;
 uniform float4 _OutlineColor;
  
 v2f vert(appdata v) {
     // just make a copy of incoming vertex data but scaled according to normal direction
     v2f o;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  
     float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
     float2 offset = TransformViewToProjection(norm.xy);
  
     o.pos.xy += offset * o.pos.z * _Outline;
     o.color = _OutlineColor;
     return o;
 }
 ENDCG
  
     SubShader {
         Tags { "Queue" = "Transparent" }
  
         // note that a vertex shader is specified here but its using the one above
         Pass {
             Name "OUTLINE"
             Tags { "LightMode" = "Always" }
             Cull Off
             ZWrite Off
             ZTest Always
             ColorMask RGB // alpha not used
  
             // you can choose what kind of blending mode you want for the outline
             Blend SrcAlpha OneMinusSrcAlpha // Normal
             //Blend One One // Additive
             //Blend One OneMinusDstColor // Soft Additive
             //Blend DstColor Zero // Multiplicative
             //Blend DstColor SrcColor // 2x Multiplicative
  
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
  
 half4 frag(v2f i) :COLOR {
     return i.color;
 }
 ENDCG
         }
  
         Pass {
             Name "BASE"
             ZWrite On
             ZTest LEqual
             Blend SrcAlpha OneMinusSrcAlpha
             Material {
                 Diffuse [_Color]
                 Ambient [_Color]
             }
             Lighting On
             SetTexture [_MainTex] {
                 ConstantColor [_Color]
                 Combine texture * constant
             }
             SetTexture [_MainTex] {
                 Combine previous * primary DOUBLE
             }
         }
     }
  
     SubShader {
         Tags { "Queue" = "Transparent" }
  
         Pass {
             Name "OUTLINE"
             Tags { "LightMode" = "Always" }
             Cull Front
             ZWrite Off
             ZTest Always
             ColorMask RGB
  
             // you can choose what kind of blending mode you want for the outline
             Blend SrcAlpha OneMinusSrcAlpha // Normal
             //Blend One One // Additive
             //Blend One OneMinusDstColor // Soft Additive
             //Blend DstColor Zero // Multiplicative
             //Blend DstColor SrcColor // 2x Multiplicative
  
             CGPROGRAM
             #pragma vertex vert
             #pragma exclude_renderers gles xbox360 ps3
             ENDCG
             SetTexture [_MainTex] { combine primary }
         }
  
         Pass {
             Name "BASE"
             ZWrite On
             ZTest LEqual
             Blend SrcAlpha OneMinusSrcAlpha
             Material {
                 Diffuse [_Color]
                 Ambient [_Color]
             }
             Lighting On
             SetTexture [_MainTex] {
                 ConstantColor [_Color]
                 Combine texture * constant
             }
             SetTexture [_MainTex] {
                 Combine previous * primary DOUBLE
             }
         }
     }
  
     Fallback "Diffuse"
 }

alt text

obvodka.jpg (76.0 kB)
Comment
Add comment · Show 2
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 emilio1984 · Sep 25, 2015 at 08:04 AM 0
Share

Can somebody help me? support?

avatar image meat5000 ♦ · Oct 13, 2015 at 04:37 PM 0
Share

http://answers.unity3d.com/questions/916204/shader-is-black-after-upgrading-to-50.html

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by wilco3d · Jan 13, 2017 at 01:43 PM

Couldn't get the outline working in the above answer. So I took just the surface bits into the original and seems to do the job.

 Shader "Outlined/Silhouetted Diffuse Texture" {
     Properties {
         _Color ("Main Color", Color) = (.5,.5,.5,1)
         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
         _Outline ("Outline width", Range (0.0, 0.03)) = .005
         _MainTex ("Base (RGB)", 2D) = "white" { }
     }
  
 CGINCLUDE
 #include "UnityCG.cginc"
  
 struct appdata {
     float4 vertex : POSITION;
     float3 normal : NORMAL;
 };
  
 struct v2f {
     float4 pos : POSITION;
     float4 color : COLOR;
 };
  
 uniform float _Outline;
 uniform float4 _OutlineColor;
  
 v2f vert(appdata v) {
     // just make a copy of incoming vertex data but scaled according to normal direction
     v2f o;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  
     float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
     float2 offset = TransformViewToProjection(norm.xy);
  
     o.pos.xy += offset * o.pos.z * _Outline;
     o.color = _OutlineColor;
     return o;
 }
 ENDCG
  
     SubShader {
         Tags { "Queue" = "Transparent" }
  
         // note that a vertex shader is specified here but its using the one above
         Pass {
             Name "OUTLINE"
             Tags { "LightMode" = "Always" }
             Cull Off
             ZWrite Off
             ZTest Always
             ColorMask RGB // alpha not used
  
             // you can choose what kind of blending mode you want for the outline
             Blend SrcAlpha OneMinusSrcAlpha // Normal
             //Blend One One // Additive
             //Blend One OneMinusDstColor // Soft Additive
             //Blend DstColor Zero // Multiplicative
             //Blend DstColor SrcColor // 2x Multiplicative
  
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             
             half4 frag(v2f i) :COLOR {
                 return i.color;
             }
             ENDCG
         }
  
         CGPROGRAM
           #pragma surface surf Lambert
   
           sampler2D _MainTex;
           fixed4 _Color;
   
           struct Input {
               float2 uv_MainTex;
           };
   
           void surf (Input IN, inout SurfaceOutput o) {
               fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
               o.Albedo = c.rgb;
               o.Alpha = c.a;
           }
         ENDCG
     }
  
     SubShader {
         Tags { "Queue" = "Transparent" }
  
         Pass {
             Name "OUTLINE"
             Tags { "LightMode" = "Always" }
             Cull Front
             ZWrite Off
             ZTest Always
             ColorMask RGB
  
             // you can choose what kind of blending mode you want for the outline
             Blend SrcAlpha OneMinusSrcAlpha // Normal
             //Blend One One // Additive
             //Blend One OneMinusDstColor // Soft Additive
             //Blend DstColor Zero // Multiplicative
             //Blend DstColor SrcColor // 2x Multiplicative
  
             CGPROGRAM
             #pragma vertex vert
             #pragma exclude_renderers gles xbox360 ps3
             ENDCG
             SetTexture [_MainTex] { combine primary }
         }
  
         CGPROGRAM
           #pragma surface surf Lambert
   
           sampler2D _MainTex;
           fixed4 _Color;
   
           struct Input {
               float2 uv_MainTex;
           };
   
           void surf (Input IN, inout SurfaceOutput o) {
               fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
               o.Albedo = c.rgb;
               o.Alpha = c.a;
           }
           ENDCG
     }
  
     Fallback "Diffuse"
 }
Comment
Add comment · Show 5 · 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 taybrown90 · Jul 30, 2017 at 07:02 PM 0
Share

I was struggling getting the version on the wiki working with Unity 2017.1. This version works perfectly. Thanks!

avatar image Greyven · Sep 07, 2017 at 07:44 AM 0
Share

Hello, is there a way to make this code not "see through" but just outlined ? I try to understand what is written but i understand nothing, and don't know what to modify or delete to stop seeing shaded objects through other objects.

avatar image Greyven Greyven · Sep 07, 2017 at 01:49 PM 1
Share

Ok, I don't know what are the implications but I found a way to do it by putting in commentary the line #48: //ZTest Always. I don't know what is not done, but it gives me the result I wanted: just an outline, no black shape through other objects.

avatar image rxmarccall Greyven · Jan 10, 2018 at 07:11 PM 0
Share

Thank you for posting this! I was having the same issue and commenting out ZTest did the trick!

avatar image Skiiid · Feb 27, 2018 at 07:40 PM 0
Share

Thank you!

avatar image
1

Answer by RajGamer · Oct 31, 2015 at 12:43 PM

Use this shader program :

 Shader "Custom/ImageEffectShader"
 {
      Properties {
          _Color ("Main Color", Color) = (.5,.5,.5,1)
          _OutlineColor ("Outline Color", Color) = (0,0,0,1)
          _Outline ("Outline width", Range (.002, 0.03)) = .005
          _MainTex ("Base (RGB)", 2D) = "white" { }
      }
  
      CGINCLUDE
      #include "UnityCG.cginc"
  
      struct appdata {
          float4 vertex : POSITION;
          float3 normal : NORMAL;
      };
  
      struct v2f {
          float4 pos : POSITION;
          float4 color : COLOR;
      };
  
      uniform float _Outline;
      uniform float4 _OutlineColor;
  
      v2f vert(appdata v) {
          // just make a copy of incoming vertex data but scaled according to normal direction
          v2f o;
          o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  
          float3 norm = normalize(mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal));
          float2 offset = TransformViewToProjection(norm.xy);
  
          o.pos.xy += offset * o.pos.z * _Outline;
          o.color = _OutlineColor;
          return o;
      }
      ENDCG
  
      SubShader {
          //Tags {"Queue" = "Geometry+100" }
          CGPROGRAM
          #pragma surface surf Lambert
  
          sampler2D _MainTex;
          fixed4 _Color;
  
          struct Input {
              float2 uv_MainTex;
          };
  
          void surf (Input IN, inout SurfaceOutput o) {
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
              o.Albedo = c.rgb;
              o.Alpha = c.a;
          }
          ENDCG
  
          // note that a vertex shader is specified here but its using the one above
          Pass {
              Name "OUTLINE"
              Tags { "LightMode" = "Always" }
              Cull Front
              ZWrite On
              ColorMask RGB
              Blend SrcAlpha OneMinusSrcAlpha
              //Offset 50,50
  
              CGPROGRAM
              #pragma vertex vert
              #pragma fragment frag
              half4 frag(v2f i) :COLOR { return i.color; }
              ENDCG
          }
      }
  
      SubShader {
          CGPROGRAM
          #pragma surface surf Lambert
  
          sampler2D _MainTex;
          fixed4 _Color;
  
          struct Input {
              float2 uv_MainTex;
          };
  
          void surf (Input IN, inout SurfaceOutput o) {
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
              o.Albedo = c.rgb;
              o.Alpha = c.a;
          }
          ENDCG
  
          Pass {
              Name "OUTLINE"
              Tags { "LightMode" = "Always" }
              Cull Front
              ZWrite On
              ColorMask RGB
              Blend SrcAlpha OneMinusSrcAlpha
  
              CGPROGRAM
              #pragma vertex vert
              #pragma exclude_renderers gles xbox360 ps3
              ENDCG
              SetTexture [_MainTex] { combine primary }
          }
      }
  
      Fallback "Diffuse"
  }
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 wilco3d · Jan 13, 2017 at 01:00 PM 0
Share

The shader doesn't appear to work (in Unity 5.4 at least). The texture displays, but now the outline doesn't work.

Any ideas?

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

9 People are following this question.

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

Related Questions

Reflections not working on IOS 2 Answers

How to use the Unity 5 Transparent Shader? 1 Answer

Objects Not Visible In Low End Computer. (Shader Error) 1 Answer

Read depth buffer on the cpu 1 Answer

Control cubemap reflection amount from metallic and smoothness properties 0 Answers


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