Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 · Mar 15, 2014 at 03:04 PM · shadershader writingoutline

Shader edit help/request: outline only and color feature

Hi everyone, I'm asking for help on shaders I found online which I can't figure out.

A) The first adds an Outline to 2D Sprites. B) A Shiny Toon Shader. C) Non-Toon Oultine Shader

For [A] I'd like to be able to change Outline Color via Inspector. And a variant which draws only the outline of the sprite.

For [B] Same: Changing Outline Color and a variant which draws only the outline of the object.

For [C] Same: Drawing only the outline, not the actual Object.

I've tried hard but any help would be greatly appreciated. Thanks in advance!


A: Outline_2DSprite

 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
 }

B) Shiny Toon Shader

 Shader "Custom/Toon_Shiny" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Bump ("Bump", 2D) = "bump" {}
         _Tooniness ("Tooniness", Range(0.1,20)) = 4
         _ColorMerge ("Color Merge", Range(0.1,20000)) = 8
         _Ramp ("Ramp Texture", 2D) = "white" {}
         _Outline ("Outline", Range(0,1)) = 0.4
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
  
         CGPROGRAM
 // Upgrade NOTE: excluded shader from Xbox360 because it uses wrong array syntax (type[size] name)
 #pragma exclude_renderers xbox360
         #pragma surface surf Toon
  
         sampler2D _MainTex;
         sampler2D _Bump;
         sampler2D _Ramp;
         float _Tooniness;
         float _Outline;
         float _ColorMerge;
  
         struct Input {
             float2 uv_MainTex;
             float2 uv_Bump;
             float3 viewDir;
         };
  
         void surf (Input IN, inout SurfaceOutput o) {
             half4 c = tex2D (_MainTex, IN.uv_MainTex);
             o.Normal = UnpackNormal( tex2D(_Bump, IN.uv_Bump));
             half edge = saturate(dot (o.Normal, normalize(IN.viewDir))); 
             edge = edge < _Outline ? edge/4 : 1;
             o.Albedo = (floor(c.rgb*_ColorMerge)/_ColorMerge) * edge;
             o.Alpha = c.a;
         }
  
         half4 LightingToon(SurfaceOutput s, half3 lightDir, half atten )
         {
             half4 c;
             half NdotL = dot(s.Normal, lightDir); 
             NdotL = tex2D(_Ramp, float2(NdotL, 0.5));
  
             c.rgb = s.Albedo * _LightColor0.rgb * NdotL * atten * 2;
             c.a = s.Alpha;
             return c;
         }
  
         ENDCG
     } 
     FallBack "Diffuse"
 }
 

C) 3D Outline Basic Shader

 Shader "Custom/Outline_ObjectBasic" {
     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)) = .01
         _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
             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
             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 flash
             //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"
 }

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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Toon Outline Shader + ortographic camera? 0 Answers

How to do an outline shader?,How to do an outline? 0 Answers

How to determine shader center? 0 Answers

QuickOutline: how can I change the shader to work with objects with two materials? 3 Answers

Edge outline & 2D shadow Shader for planar mesh 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