Search Unity

Toon shader with Transparency

Discussion in 'Shaders' started by hellraizerhhh, Sep 2, 2011.

  1. hellraizerhhh

    hellraizerhhh

    Joined:
    May 26, 2010
    Posts:
    619
    I have a Character i'm using the Toon Basic on, how do I get alpha channels or maps to work on it? Sorry if this has been asked before, I'm no shader expert.
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    Hi, this is pretty easy to do.

    What you need to do is set a blending mode (in this case srcAlpha oneMinusSrcAlpha) change the render type to transparent, and change he queue that it is rendering in. You then need to output the alpha to use also when writing the fragment.

    Here is the modified shader, have a look at how it differs from the original and you should be able to figure out what you want to do from here.

    Code (csharp):
    1.  
    2.     SubShader {
    3.         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    4.         Pass {
    5.             Name "BASE"
    6.             Cull Off
    7.             Blend SrcAlpha OneMinusSrcAlpha
    8.            
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #pragma fragmentoption ARB_precision_hint_fastest
    13.  
    14.             #include "UnityCG.cginc"
    15.  
    16.             sampler2D _MainTex;
    17.             samplerCUBE _ToonShade;
    18.             float4 _MainTex_ST;
    19.             float4 _Color;
    20.  
    21.             struct appdata {
    22.                 float4 vertex : POSITION;
    23.                 float2 texcoord : TEXCOORD0;
    24.                 float3 normal : NORMAL;
    25.             };
    26.            
    27.             struct v2f {
    28.                 float4 pos : POSITION;
    29.                 float2 texcoord : TEXCOORD0;
    30.                 float3 cubenormal : TEXCOORD1;
    31.             };
    32.  
    33.             v2f vert (appdata v)
    34.             {
    35.                 v2f o;
    36.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    37.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    38.                 o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    39.                 return o;
    40.             }
    41.  
    42.             float4 frag (v2f i) : COLOR
    43.             {
    44.                 float4 col = _Color * tex2D(_MainTex, i.texcoord);
    45.                 float4 cube = texCUBE(_ToonShade, i.cubenormal);
    46.                 return float4(2.0f * cube.rgb * col.rgb, col.a);
    47.             }
    48.             ENDCG          
    49.         }
    50.     }
    51.  
     
  3. hellraizerhhh

    hellraizerhhh

    Joined:
    May 26, 2010
    Posts:
    619
    Im not a shader writer so this is completely foreign to me. No clue what to do with this at all
     
  4. jehovah0121

    jehovah0121

    Joined:
    Sep 28, 2011
    Posts:
    17
    The thing is that this modification only works when there are two objects that don't share the same mesh.
    If you use the same mesh, there still will be obscuring no matter how transparent your shader is.