Search Unity

Rim Light effect at any angle

Discussion in 'Shaders' started by Acenth, May 23, 2015.

  1. Acenth

    Acenth

    Joined:
    Aug 14, 2012
    Posts:
    42
    I have a sphere I want to have a glow / radial gradient look and it seems like Rim Lighting is the way to go, but almost all examples take viewDir into account.

    I'm trying to get the same lighting at any angle:



    So what's the best way to achieve this?

    Thanks! :)
     
  2. ThomasCreate

    ThomasCreate

    Joined:
    Feb 23, 2015
    Posts:
    81
    I needed to achieve a similar effect and I did a lot of scouring on the internet for examples since I'm still very new to shaders, but I couldn't find any solution. I ended up using the depth map to find the edges of the models and interpolated the colour using that, but that only worked because of the special use case this was for (the camera never moved and the models were the only thing in the scene).

    I doubt that your use case is similar, so I hope someone more knowledgeable can help you out - I'm very interested to hear a proper solution for this myself.
     
  3. antislash

    antislash

    Joined:
    Apr 23, 2015
    Posts:
    646
    did you try using the camera vector ?
    something like
    o.viewDir = UNITY_MATRIX_IT_MV[2].xyz;

    if you want a rim effect, that should work, but if you want an inner glow , not sure that will do what you want.
    i'm NOT a shader specialist but maybe you should explain more clearly what you want to achieve.
     
  4. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    Are you looking for a "baked in Fresnel" look? If so you can used a basic Fresnel calc but with any direction passed in(not view direction). Or are you just going for the Fresnel/rim lighting look so it always looks like your above example? If that's the case then you need the view direction.

    Taken from Unity docs:
    Code (CSharp):
    1.  half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
    2.           o.Emission = _RimColor.rgb * pow (rim, _RimPower);
    And for constant direction:
    Code (CSharp):
    1.  half rim = 1.0 - saturate(dot (normalize(_MyDirVector.xyz), o.Normal));
    2.           o.Emission = _RimColor.rgb * pow (rim, _RimPower);
     
    Last edited: May 28, 2015
  5. poorDolly

    poorDolly

    Joined:
    Jan 23, 2014
    Posts:
    10
    echo4papa,
    i get a question:
    what is frensel/rim?
    o.Emission= _RimColor.rgb* pow (rim, _RimPower);
    why use pow (rim, _RimPower);??
     
  6. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    Ah. More info here: http://en.wikipedia.org/wiki/Fresnel_equations

    It's what makes glancing angles look more reflective than straight on angles for some materials. It can can also be used for rim lighting.

    The reason you use a pow is because it let's you use a shorter range for _RimPower to control the strength of the effect. You don't need it if you don't want it.
     
  7. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    Can you please tell how and where can I set the _MyDirVector.xyz?
     
    Last edited: May 29, 2015
  8. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    That's a custom variable you would need to make. Declare it like this in your shader:

    Code (CSharp):
    1. uniform float4 _MyDirVector;
    and then in C# set it like this:
    Code (CSharp):
    1. SomeMaterial.SetVector("_MyDirVector",SomeVector4);
    SomeMaterial is the reference to the material of your mesh and SomeVector4 is a vector4 containing the direction you want.
     
    RenanRL likes this.
  9. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    @echo4papa thanks for the code. What I'm trying to do is something like this:
    rim-light-angled.jpg
    So, I think if the IN.viewDir vector can be changed to be looking a little bit downward, it will create this effect, but can't figure out how to do it. So please tell me how can I do this.
     
  10. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    If you want it to always look like that, you can either modify the ViewDir vector in the shader or the camera vector in C#. Either way you'll probably want to make a rotation matrix, probably in C#. Either pass it into the shader and multiply it against ViewDir or you can take the camera vector and multiply it against the rotation matrix in C# and pass in the result as _MyDirVector.

    Hope that helps.
     
  11. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    @echo4papa thanks for the info, can you please tell me how can I modify the ViewDir in the shader? I don't know much about shader coding. How can I define a vector which will be a little downward looking and multiply it with the ViewDir?
     
    Last edited: May 30, 2015