Search Unity

Adding transparency to shadow

Discussion in 'Shaders' started by barbe63, Dec 4, 2014.

  1. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Hello,

    My current project involve a toon shader that have transparency for my ragdolls to vanish. Unfortunatly the shadow it casts doesn't vanish as the body does. i reall know nothing about shaders, here are the toon shaders i found on internet.

    Code (CSharp):
    1.    
    2.   Shader "Toon/Basic Alpha" { Properties { _Color ("Main Color", Color) = (.5,.5,.5,1) _MainTex ("Base (RGB)", 2D) = "white" {} _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal } }
    3.     SubShader {
    4.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    5.     Pass {
    6.     Name "BASE"
    7.     Blend SrcAlpha OneMinusSrcAlpha
    8.     CGPROGRAM
    9.     #pragma vertex vert
    10.     #pragma fragment frag
    11.     #pragma fragmentoption ARB_precision_hint_fastest
    12.     #include "UnityCG.cginc"
    13.     sampler2D _MainTex;
    14.     samplerCUBE _ToonShade;
    15.     float4 _MainTex_ST;
    16.     float4 _Color;
    17.     struct appdata {
    18.     float4 vertex : POSITION;
    19.     float2 texcoord : TEXCOORD0;
    20.     float3 normal : NORMAL;
    21.     };
    22.     struct v2f {
    23.     float4 pos : POSITION;
    24.     float2 texcoord : TEXCOORD0;
    25.     float3 cubenormal : TEXCOORD1;
    26.     };
    27.     v2f vert (appdata v) {
    28.     v2f o;
    29.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    30.     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    31.     o.cubenormal = mul (UNITY_MATRIX_MV, float4(v.normal,0));
    32.     return o;
    33.     }
    34.     float4 frag (v2f i) : COLOR {
    35.     float4 col = _Color * tex2D(_MainTex, i.texcoord);
    36.     float4 cube = texCUBE(_ToonShade, i.cubenormal);
    37.     return float4(2.0f * cube.rgb * col.rgb, col.a);
    38.     }
    39.     ENDCG
    40.     }
    41.     }
    42.     SubShader {
    43.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    44.     Pass {
    45.     Name "BASE"
    46.     Blend SrcAlpha OneMinusSrcAlpha
    47.     SetTexture [_MainTex] {
    48.     constantColor [_Color]
    49.     Combine texture * constant
    50.     }
    51.     SetTexture [_ToonShade] {
    52.     combine texture * previous DOUBLE, previous
    53.     }
    54.     }
    55.     }
    56.     Fallback "VertexLit"
    57.     }
    58.    
    59.  

    and

    Code (CSharp):
    1.  
    2.     Shader "Toon/Basic Outline Alpha" { 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" { } _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "" { Texgen CubeNormal } }
    3.     CGINCLUDE
    4.     #include "UnityCG.cginc"
    5.     struct appdata {
    6.     float4 vertex : POSITION;
    7.     float3 normal : NORMAL;
    8.     };
    9.     struct v2f {
    10.     float4 pos : POSITION;
    11.     float4 color : COLOR;
    12.     };
    13.     uniform float4 _Color;
    14.     uniform float _Outline;
    15.     uniform float4 _OutlineColor;
    16.     v2f vert(appdata v) {
    17.     v2f o;
    18.     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    19.     float3 norm = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
    20.     norm.x *= UNITY_MATRIX_P[0][0];
    21.     norm.y *= UNITY_MATRIX_P[1][1];
    22.     o.pos.xy += norm.xy * o.pos.z * _Outline;
    23.     o.color = float4(_OutlineColor.rgb, _Color.a);
    24.     return o;
    25.     }
    26.     ENDCG
    27.     SubShader {
    28.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    29.     UsePass "Toon/Basic Alpha/BASE"
    30.     Pass {
    31.     Name "OUTLINE"
    32.     Tags { "LightMode" = "Always" }
    33.     Cull Front
    34.     ZWrite On
    35.     ColorMask RGB
    36.     Blend SrcAlpha OneMinusSrcAlpha
    37.     CGPROGRAM
    38.     #pragma vertex vert
    39.     #pragma fragment frag
    40.     half4 frag(v2f i) :COLOR { return i.color; }
    41.     ENDCG
    42.     }
    43.     }
    44.     SubShader {
    45.     Tags {"Queue"="Transparent" "RenderType"="Transparent"}
    46.     UsePass "Toon/Basic Alpha/BASE"
    47.     Pass {
    48.     Name "OUTLINE"
    49.     Tags { "LightMode" = "Always" }
    50.     Cull Front
    51.     ZWrite On
    52.     ColorMask RGB
    53.     Blend SrcAlpha OneMinusSrcAlpha
    54.     CGPROGRAM
    55.     #pragma vertex vert
    56.     #pragma exclude_renderers gles xbox360 ps3
    57.     ENDCG
    58.     SetTexture [_MainTex] { combine primary }
    59.     }
    60.     }
    61.     Fallback "Toon/Basic Alpha"
    62.     }
    I've been told here
    http://answers.unity3d.com/answers/846945/view.html
    that asking here is a good idea since it's a bit difficult and that i may have to change the recceiver shader instead. My walls/floor shaders are the toon shading lighted from unity.

    What should i add on my shaders to make it possible? Is it a good idea anyway in term of costs? (knowing it's going to be a IOS/Android game)
     
  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    You would need to hack or replace the built in Unity shadows (which is possible) but requires a bit of knowledge.
    I think the easiest solution might be using the projector shadows or look around the asset store or you can do some dissolve effect using the Cutout shaders.