Search Unity

Question Add gradient on rim Lighting

Discussion in 'Shaders' started by Quasar47, Jul 9, 2020.

  1. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    Hello there,

    I have a rim light in a custom fragment shader and I would want to change it's color based on his dot product with the light direction to create a gradient effect, like so :

    example.png

    The problem is that the dot product assigns 1 to everything up until the "equator" of the sphere, meaning tht eveything on the lit side will be white, and the lerp between colors for the gradient will always output the end value. I would like to know if it was possible to map the dot product to keep it inside the bounds of the rim lighting, and use it to lerp it between the red color at the ends of the rim, and white where the normal is the most parallel to the light direction.

    What I have so far is :

    Rim lighting :

    Code (CSharp):
    1.  
    2.  
    3.                 float NdotL = dot(_WorldSpaceLightPos0, i.normal);
    4.  
    5.                 // Calculate rim lighting.
    6.                 float rimDot = 1 - dot(viewDir, i.normal);
    7.  
    8.                 rimDot /= _RimStepWidth;
    9.                 rimDot += _RimAmount;
    10.  
    11.                 // We only want rim to appear on the lit side of the surface,
    12.                 // so multiply it by NdotL, raised to a power to smoothly blend it.
    13.                 float rimIntensity = floor(rimDot * NdotL * _RimThreshold);
    14.  
    15.                 // bring the light intensity back into a range where we can use it for color
    16.                 // and clamp it so it doesn't do weird stuff below 0 / above one
    17.                 rimIntensity /= _RimNbSteps;
    18.                 rimIntensity = saturate(rimIntensity);
    19.  
    20.  
    21.  
    and color :

    Code (CSharp):
    1. float4 rimCol= smoothstep(float4(1,0,0,1), float4(0, 0, 1, 1), floor(NdotL * .22))    /*replace this by the gradient*/ * _RimColor;
    2.            
    3. float4 color;
    4. color.rgb = lerp(color.rgb, rimCol, saturate(rimIntensity) );
    5. return color;
    6.  

    Thank you for your help, any advice will be appreciated.