Search Unity

Hel with issues based on object offset from camera

Discussion in 'Shaders' started by Beloudest, Mar 28, 2017.

  1. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    247
    Hi I have tried to make a shader that fades out the texture when viewed from a 90 degree angle. It works fine when I keep my test plane at vector 0,0,0. If I begin to move my plane around the map the shader stops behaving as expected. It appears I need to apply an offset of sorts but I don't know what that would be?

    Maybe one of you guys might have an idea. Feel free to try the shader on a flat plane, as you view it from side angles it should fade out (only when at position vector 0,0,0). Thanks, I've been on this for days.
     
    Last edited: Mar 28, 2017
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You should look into your basic fresnel shader, and stop using acos.

    A dot product returns a cosine of two angles, so "1.0" when viewed straight on, transitioning to "0.0" when viewed at 90 degrees. So really all you need is:

    saturate(dot(worldNormal, worldViewDir))

    If you want to change it so the transition happens only closer to 90, or over a specific range, like from 45 to 90, you just need to multiply the dot product before the saturate.

    For example:
    saturate(dot(normal, view) * 1.414)

    The value 1.414 is roughly 1.0 / 0.7071, or 1 divided by cos(45 degrees). That'd produce a fade out that goes from 45 degrees to 90 degrees. For simplicity you could just divide by a 0.0 to 1.0 range you set via a material property, working out the cosine value for the angle you want to fade from.

    The reason for avoiding acos is because most trigonometry functions are extremely expensive. Using acos(dot(normal, view)) increases the performance cost of that line by nearly 10x over dot(normal, view)! (Possibly more depending on the GPU)
     
    Last edited: Mar 29, 2017
    Beloudest likes this.
  3. Beloudest

    Beloudest

    Joined:
    Mar 13, 2015
    Posts:
    247

    Thanks, I'll try this right away.