Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Toon Shader problem

Discussion in 'Shaders' started by maximusGG, May 23, 2014.

  1. maximusGG

    maximusGG

    Joined:
    May 14, 2014
    Posts:
    2
    I have 2 problems with the standard toon-shader:

    1. The object on the left has outlines in the middle of the mesh. The mesh has no errors and consists of only 1 Element. The outline still gets displayed wrong and i don't know why

    $texture_bug.png

    2. I want some details on my character. Is there any toon shader who supports normal or displacement maps, or even both?
    I know that a toon shader with a displacement map is kinda weird, but i have no idea how i can display small details on my meshes, without a retopologization of all my sculpted assets.

    Thanky in advance...

    Edit: I have no clue of shader coding and i don't have the time to get into it at the moment, so i am searching for an alternative :(
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,414
    Here is a toon shader variant with normal-mapping.

    It's based on the lit toon shaders found in the Unity standard assets package (it contains a "Toon Shading" directory). The Toon Shading package is required to compile the following shaders.




    Toony-LightedBump.shader
    Code (csharp):
    1.  
    2. Shader "Toon/Lighted Bumped" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    7.         _BumpMap ("Normalmap", 2D) = "bump" {}
    8.     }
    9.  
    10.     SubShader {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 300
    13.    
    14. CGPROGRAM
    15. #pragma surface surf ToonRamp
    16.  
    17. sampler2D _Ramp;
    18. sampler2D _BumpMap;
    19.  
    20. // custom lighting function that uses a texture ramp based
    21. // on angle between light direction and normal
    22. #pragma lighting ToonRamp exclude_path:prepass
    23. inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
    24. {
    25.     #ifndef USING_DIRECTIONAL_LIGHT
    26.     lightDir = normalize(lightDir);
    27.     #endif
    28.  
    29.     half d = dot (s.Normal, lightDir)*0.5 + 0.5;
    30.     half3 ramp = tex2D (_Ramp, float2(d,d)).rgb;
    31.  
    32.     half4 c;
    33.     c.rgb = s.Albedo * _LightColor0.rgb * ramp * (atten * 2);
    34.     c.a = 0;
    35.     return c;
    36. }
    37.  
    38.  
    39. sampler2D _MainTex;
    40. float4 _Color;
    41.  
    42. struct Input {
    43.     float2 uv_MainTex : TEXCOORD0;
    44.     float2 uv_BumpMap : TEXCOORD1;
    45. };
    46.  
    47. void surf (Input IN, inout SurfaceOutput o) {
    48.     half4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    49.     o.Albedo = c.rgb;
    50.     o.Alpha = c.a;
    51.     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    52. }
    53. ENDCG
    54.  
    55.     }
    56.  
    57.     Fallback "Toon/Lighted"
    58. }
    59.  

    Toony-LightedOutlineBump.shader
    Code (csharp):
    1.  
    2. Shader "Toon/Lighted Bumped Outline" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
    5.         _OutlineColor ("Outline Color", Color) = (0,0,0,1)
    6.         _Outline ("Outline width", Range (.002, 0.03)) = .005
    7.         _MainTex ("Base (RGB)", 2D) = "white" {}
    8.         _Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
    9.         _BumpMap ("Normalmap", 2D) = "bump" {}
    10.     }
    11.  
    12.     SubShader {
    13.         Tags { "RenderType"="Opaque" }
    14.         UsePass "Toon/Lighted Bumped/FORWARD"
    15.         UsePass "Toon/Basic Outline/OUTLINE"
    16.     }
    17.  
    18.     Fallback "Toon/Lighted Bumped"
    19. }
    20.  
    Hope it helps!
     
    Last edited: Sep 11, 2015
    Inubouya likes this.
  3. maximusGG

    maximusGG

    Joined:
    May 14, 2014
    Posts:
    2
    thx so much <3
    any idea how to fix the broken outlines?
    thx in advance the unity community is awesome :D
     
  4. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The toon shader renders outlines by rendering your model a second time, inside out and with its vertices moved outwards along their normal. There is probably some hidden complexity in your mesh that is exposed when it is pushed outwards.