- Home /
This post has been wikified, any user with enough reputation can edit it.
Fake directional light on toon shader
I'm using the basic toon shader. Probably is a silly question, I'm very new to writing shaders.
What it currently does: The UNITY_MATRIX_MV causes the model's faces to be shaded relative to the camera's view on it. This essentially gives a flashlight effect.
Desired result: A Vector material property 'Sun Direction' on the should control the direction of shading.
To illustrate, imagine a sphere. If _SunDirection = (0,-1,0), top faces should be bright and bottom dark. The intensity is controlled by the toon ramp cubemap.
Thanks in advance.
struct appdata {
half4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
fixed3 normal : NORMAL;
};
struct v2f {
half4 pos : SV_POSITION;
half2 texcoord : TEXCOORD0;
half3 cubenormal : TEXCOORD1;
UNITY_FOG_COORDS(2)
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
half3 norm = v.normal;
o.cubenormal = mul (UNITY_MATRIX_MV, half4(v.normal,0));
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = _Color * tex2D(_MainTex, i.texcoord);
fixed4 cube = texCUBE(_ToonShade, i.cubenormal);
fixed4 c = fixed4(2.0f * cube.rgb * col.rgb, col.a);
UNITY_APPLY_FOG(i.fogCoord, c);
return c;
}
Comment