Toon shader shadow

TSchemmer


I'm currently trying to change my asset style from realistic to low poly / cartoonic.
For example, I have a toon surface shader

half4 LightingRamp(SurfaceOutput s, half3 lightDir, half atten) {
    half NdotL = dot(s.Normal, lightDir);
    half diff = NdotL * 0.5 + 0.5;
    half3 ramp = tex2D(_LightingTex, float2(diff, 0)).rgb;
    half4 c;
    c.rgb = _LightColor0.rgb * atten * ramp *_Color;    
    c.a = s.Alpha;
    return c;
}

where _LightingTex is a 2D texture ramp. This works fine for lighting effects on the objects themselfs.
As I have multiple objects with this shader in my scene, some of them are casting a shadow onto my wall. As you can see, the shadow here is not a ramp but a continuous gradient, as it is (probably) done in some sort of ambient from unity. My question is now: is there an option to create this colorramp effect on the global shadows as well? Something like this: Can I do it material shader based, or is it a post processing effect?
Thanks

Kalle Halvarsson

With surface shaders: No, you can't do it in the shader. Actually, I think the best way to get a unified cartoon effect is to use a color grading LUT as a post effect. The great thing about LUTs is that you can create one easily in photoshop by first applying some cool effects to a regular image until it looks the way you want (such as "Posterize"), and then copy the effect stack to apply to a LUT texture, like this one. When you use this LUT in Unity, everything will look as they would with your Photoshop filters applied. One small caveat I've noticed though is that some standard LUT textures need to be flipped vertically to work with the Post Processing Stack. Here is a nice tutorial on how to create posterized LUTs.

If you want to get the toon-like shadows directly in the shader, it is not any harder than making a regular forward rendered vertex/fragment shader, though this by itself requires a bit of knowledge on how these work - i can recommend looking at the standard shader source code, this, or this (somewhat outdated) tutorial. You can find the details surrounding how to add shadow support from my post here. The only thing you need to change is to add a similar color ramp to the shadow mask:

half shadow = SHADOW_ATTENUATION(IN)
shadow = tex2D(_ShadowRamp, float2(shadow, 0));

For this, you can set the shadow ramp as a global shader variable from script, so you won't have to assign it for each material.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related