Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

DeferredShading with ramp lighting...?

Discussion in 'Shaders' started by Zurra, Jul 21, 2015.

  1. Zurra


    Oct 24, 2013
    I've tried to modify the the internal-deferredshading so it uses a ramp texture for the lighting. I wonder why my code is not working, no errors or anything..

    Also, I'm not entirely sure how I should tell what this _Ramp texture is. Currently I just click my new internal-deferredShading file in unity and assign a texture to the Ramp slot. Is that correct way? Any way, here is my code...

    Code (CSharp):
    1. Shader "Hidden/Glory/Internal-DeferredShading" {
    2. Properties {
    3. ...
    4.     _Ramp( "RampTex",2D) = "" {}
    5. ...
    6. SubShader {
    7. ...
    8. half4 CalculateLight (unity_v2f_deferred i)
    9. {
    10. ...    half3 Lambert = LambertTerm (normalWorld, light.dir);
    11.     light.ndotl = tex2D(_Ramp,Lambert.xy);
    12. }
    Edit: This method works in "normal" shaders but I really want to do it in the internal-deferredshading file.
  2. Plutoman


    May 24, 2013
    You probably need to do a SetTexture from either the material or the Shader.SetGlobalTexture. I don't believe assigning it to that will actually work.
    Zurra likes this.
  3. MSplitz-PsychoK


    May 16, 2015
    Ehh... I don't really understand what you're trying to do, so I'm not really sure how to fix what you're doing, but I can give you some idea of what's wrong.

    First off, nDotL (you have in line 11) stands for NormalDotLight, so that value should be a value between -1 and 1, where -1 is a surface that faces directly away from the light and 1 is a surface that faces directly towards it. As you take a surface and rotate it so it's facing the light more and more, nDotL goes up, and so does the brightness of the surface to simulate real lighting.

    In a normal shader, you have the surface get gradually brighter as it faces the light. In a toon shader, it's mostly the same, but instead of a smooth transition to a brighter surface, you use a step value (also called a ramp value) to make the brightness happen in chunks.

    Instead of trying to set your ndotl to a value from your ramp texture, usually you would want to use modulus to round it to one of your step values, then shade each step differently or have different colors for each step. Sorry if this is confusing.
  4. Zurra


    Oct 24, 2013
    Thanks Plutoman, Shader.SetGlobalTexture solved one part of my problem :)
    Now I still need to figure out how to add color to the lighting, currently it only shades the light from black to white which is kind of obvious because the it's lambert lighting from range -1 to 0. I thought if I just multiply it with
    Code (CSharp):
    1. half3(1.0,0,0)
    It would change color to red but it does not. Need to dig a bit more.
    But thanks for the help :)