Custom toon shader lighting model thing

Here's a custom lighting model that uses a texture (ideally it will be a horizontal greyscale gradient) to create toonshading style shading bands.. You know the kind...

Diffuse is calculated as in Lamber - dot product of surface normal and light direction

the h value here is not the halfway vector - it is a float that takes the diffuse value, halves it and adds 0.5 to get a value between 0 and 1. Remember if the vector is facing away from the other in a dot product, it will return a value of minus one. -1 x 0.5 +0.5 will be 0.
This h value is then assigned to a float2  "rh", which acts as a UV value for the variable "ramp". We now have a greyscale value from the texture. The final colour is calculated by multiplying the albedo by light colour and the ramp value. I guess you could also multiply it by the attenuation if you were after a non-flat shader too

 

 Shader "Dave/ToonRamp"{
    Properties{
        _Color ("Color",color)=(1,1,1,1)
        _RampTex("Ramp Texture",2D) = "white"{}
    }


        SubShader{
            CGPROGRAM
            #pragma surface surf ToonRamp

            float4 _Color;
            sampler2D _RampTex;    
            
            float4 LightingToonRamp(SurfaceOutput s, fixed3 lightDir, fixed atten) {
                float diff = dot(s.Normal, lightDir);
                float h = diff * 0.5 + 0.5;
                float2 rh = h;
                float3 ramp = tex2D(_RampTex, rh).rgb;
                float4 c;
                c.rgb = s.Albedo*_LightColor0*(ramp);
                c.a = s.Alpha;
                return c;
                                               
            }

            struct Input {
                float2 uv_MainTex;
            };
            void surf(Input IN, inout SurfaceOutput o) {

                o.Albedo = _Color.rgb;
            }



        ENDCG
    }
             
    Fallback "Diffuse"
}

Comments

Popular posts from this blog

setting VFX graph properties using C#

scripting custom render texture creation, assignment, shaders