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
0
Question by Pauls · Apr 25, 2013 at 07:54 PM · shadermaterialoutlinetoon

Outline diffuse shader (toon) : no results?

Hi,

I would like to use a shader to get the edges with a specific color (http://wiki.unity3d.com/index.php/OutlinedDiffuse). (The code only works with "vert", there is no "frag" function, but since it is on the wiki, I guess it is also working like that).


EDIT #2:

Thanks whydoitdoit for the link. Would you know how to avoid the outline effect inside an intersection between 2 rectangles? The 2 rectangles are perfectly perpendicular in Blender...

Also how could I change the color of the front "frag", I tried with return float4(70, 70, 30, 1); but it triggers a warning : "Shader warning in 'Toon2': Program 'vert', implicit truncation of vector type (compiling for d3d11_9x)"

I have got this result without the lightcolor with :

 //c.rgb = lightColor * c.rgb * 2;
 c.rgb = c.rgb * 2;

The image :

alt text

Thanks again


[3]: /storage/temp/10419-settings.png

rect.png (7.6 kB)
cross.png (8.8 kB)
Comment
Add comment · Show 4
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 whydoidoit · Apr 25, 2013 at 09:46 PM 0
Share

Something like this?

avatar image Pauls · Apr 26, 2013 at 01:31 AM 0
Share

@whydoitdoit : thanks whydoitdoit, I really appreciate your help, and your website! Having the community that share, and a website like yours to share your knowledge is a great motivation for me. I edited my post with an image of my result : can I ask you how it is possible to -not- have the outline inside an intersection between 2 rectangles? And how to change the outline color (with a float4?) ? Thanks a lot

avatar image whydoidoit · Apr 26, 2013 at 05:51 AM 1
Share

So you can do that by not writing to the depth buffer on the first pass, or by offsetting the depth buffer backwards during that pass (or forwards in the other pass) - however, it ends up looking odd when you put two characters next to each other, because their outlines will merge and they won't be outlined against each other very well.

avatar image whydoidoit · Apr 26, 2013 at 05:57 AM 0
Share

Converted the toon shader to an answer (because it's too unwieldy as a comment :)

1 Reply

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

Answer by whydoidoit · Apr 26, 2013 at 05:53 AM

 Shader "Custom/OutlineToonShader" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _Bump ("Bump", 2D) = "bump" {}
         _ColorMerge ("Color Merge", Range(0.1,20000)) = 8
         _Ramp ("Ramp Texture", 2D) = "white" {}
         _Outline ("Outline", Range(0, 0.15)) = 0.08
         _OutllineColor ("OutlineColor", Color) = (0,0,0,1)
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
  
         Pass {
  
             Cull Front
             Lighting Off
             ZWrite On
             Tags { "LightMode"="ForwardBase" }
  
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
  
             #include "UnityCG.cginc"
             struct a2v
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float3 tangent : TANGENT;
             }; 
  
             struct v2f
             {
                 float4 pos : POSITION;
             };
  
             float _Outline;
             float4 _OutlineColor;
 
  
             v2f vert (a2v v)
             {
                 v2f o;
                 float4 pos = mul( UNITY_MATRIX_MV, v.vertex); 
                 float3 normal = mul( (float3x3)UNITY_MATRIX_IT_MV, v.normal);  
                 normal.z = -0.4;
                 pos = pos + float4(normalize(normal),0) * _Outline;
                 o.pos = mul(UNITY_MATRIX_P, pos);
  
                 return o;
             }
  
             float4 frag (v2f IN) : COLOR
             {
                 return _OutlineColor;
             }
  
             ENDCG
  
         }
  
         Pass {
  
             Cull Back 
             Lighting On
             Tags { "LightMode"="ForwardBase" }
  
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
  
             #include "UnityCG.cginc"
             uniform float4 _LightColor0;
  
             sampler2D _MainTex;
             sampler2D _Bump;
             sampler2D _Ramp;
  
             float4 _MainTex_ST;
             float4 _Bump_ST;
  
             float _Tooniness;
             float _ColorMerge;
  
             struct a2v
             {
                 float4 vertex : POSITION;
                 float3 normal : NORMAL;
                 float4 texcoord : TEXCOORD0;
                 float4 tangent : TANGENT;
  
             }; 
  
             struct v2f
             {
                 float4 pos : POSITION;
                 float2 uv : TEXCOORD0;
                 float2 uv2 : TEXCOORD1;
                 float3 lightDirection;
  
             };
  
             v2f vert (a2v v)
             {
                 v2f o;
                 //Create a rotation matrix for tangent space
                 TANGENT_SPACE_ROTATION; 
                 //Store the light's direction in tangent space
                 o.lightDirection = mul(rotation, ObjSpaceLightDir(v.vertex));
                 //Transform the vertex to projection space
                 o.pos = mul( UNITY_MATRIX_MVP, v.vertex); 
                 //Get the UV coordinates
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);  
                 o.uv2 = TRANSFORM_TEX (v.texcoord, _Bump);
                 return o;
             }
  
             float4 frag(v2f i) : COLOR  
             { 
                 //Get the color of the pixel from the texture
                 float4 c = tex2D (_MainTex, i.uv);  
                 //Merge the colours
                 c.rgb = (floor(c.rgb*_ColorMerge)/_ColorMerge);
  
                 //Get the normal from the bump map
                 float3 n =  UnpackNormal(tex2D (_Bump, i.uv2)); 
  
                 //Based on the ambient light
                 float3 lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz;
  
                 //Work out this distance of the light
                 float lengthSq = dot(i.lightDirection, i.lightDirection);
                 //Fix the attenuation based on the distance
                 float atten = 1.0 / (1.0 + lengthSq);
                 //Angle to the light
                 float diff = saturate (dot (n, normalize(i.lightDirection)));  
                 //Perform our toon light mapping 
                 diff = tex2D(_Ramp, float2(diff, 0.5));
                 //Update the colour
                 lightColor += _LightColor0.rgb * (diff * atten); 
                 //Product the final color
                 c.rgb = lightColor * c.rgb * 2;
                 return c; 
  
             } 
  
             ENDCG
         }
  
     }
     FallBack "Diffuse"
 }
Comment
Add comment · Show 2 · 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 whydoidoit · Apr 26, 2013 at 05:54 AM 0
Share

The one above has an _OutlineColor that can be changed.

avatar image Pauls · Apr 26, 2013 at 05:36 PM 0
Share

Thanks a lot, ZWrite Off on the first pass and the Outline Color work perfectly! Thank you very much!

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

12 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

Related Questions

Ztest shader issues 0 Answers

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

Shader Edit Help: 2D Outline Coloring 2 Answers

Better Outlines 0 Answers

Interior Outlines? 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