- Home /
Toon Shader with Shadows AND Alpha? (Unity 2.6)
Well, to start off with, I'm very bad at working with shaders, I'm barely even sure how they work, honestly...
But for my game, we kinda need Toon Shading which are affected by Shadows (Diffuse, I believe its called?) AND Alpha, as some of our characters have see-through textures of sorts, and I REALLY want to make the enemies fade out on death...
Can anyone help me?
Thanks!
Answer by skovacs1 · Nov 04, 2010 at 06:41 PM
This is a post where I had added alpha to the standard toon shader for Unity 3. Adding alpha is not too hard.
Adding shadows (and other lighting) involves changing the shader with a bit more knowledge of what's actually going on. Since the Unity 3 shaders implement lighting very differently than 2.6, I can't really write and test something and be sure it will work for you. Here is a shader from the wiki which does Toon Shadows in Unity 2.x. I would recommend starting there and adding alpha by changing the blend mode and the render queue. I'm not sure, but I believe that objects in the Transparent render queue will not receive shadows, but if you don't put transparent objects in this queue, objects will be drawn in the wrong order.
Since I don't have 2.6, I can't test this, so it is offered with no guarantee that it will work, but I would try something like:
Shader "Toon/AlphaShadowed" { Properties { _Color ("Main Color", Color) = (.5,.5,.5,1) _MainTex ("Base (RGB)", 2D) = "white" {} _ToonShade ("ToonShader Cubemap(RGB)", CUBE) = "white" { Texgen CubeNormal } }
Category {
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
Pass {
// Fallback case for non-pixel-lit situations.
Blend SrcAlpha OneMinusSrcAlpha
Name "BASE"
Tags {"LightMode" = "VertexOrNone"}
SetTexture [_MainTex] { constantColor [_Color] Combine texture * constant }
SetTexture [_ToonShade] { combine texture * previous DOUBLE, previous }
}
Pass {
Blend SrcAlpha OneMinusSrcAlpha
Name "PPL"
Tags { "LightMode" = "Pixel" }
ColorMask RGBA
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_builtin
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"
struct v2f {
V2F_POS_FOG;
LIGHTING_COORDS
float2 uvA;
float3 uvB;
};
uniform float4 _MainTex_ST;
v2f vert (appdata_tan v) {
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uvA = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uvB = mul((float3x3)glstate.matrix.invtrans.modelview[0], v.normal );
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
uniform sampler2D _MainTex;
uniform samplerCUBE _ToonShade;
uniform float4 _Color;
float4 frag (v2f i) : COLOR {
half4 texcol = tex2D( _MainTex, i.uvA ) * _Color;
half4 lighting = LIGHT_ATTENUATION(i) * texCUBE(_ToonShade, i.uvB);
return half4((texcol * lighting * 2).rgb, texcol.a);
}
ENDCG
}
}
}
Fallback "VertexLit"
}
Thanks for the contribution!
And for a test, I would say its pretty impressive, because it worked... The only thing is that I apparently forgot I wanted it to have outlines as well... Completely forgot that it ain't naturally under Toon Shading, ugh. So I'm trying to experiment here and there to see whether I can combine the old Toon Shader that Omega created a while back in http://forum.unity3d.com/threads/24668-Toon-Outline-but-with-Diffuse-surface with the one you just created... Trying to see whether I can get your alpha with his shading...
Thanks again!