Search Unity

Question How do you bias a shader effect towards the camera?

Discussion in 'Shader Graph' started by LightArtPro, May 25, 2023.

  1. LightArtPro

    LightArtPro

    Joined:
    Aug 23, 2012
    Posts:
    5
    Tld: Want to create a retroflective shader effect but use an offset camera and still see the effect.

    RetroReflectiveSigns2.jpg

    In my game I have a car driving at night and want to evoke the effect of retroflextive signs and road markings lighting up in the cars headlights while using a 3rd person camera.

    Any suggestions how fudge it so when you are looking from the 3rd person/ almost top down camera it still picks up the retro-reflection of the car's headlights? ViewDir.jpg

    I created a Fresnel effect in shader graph for the emissive channel but only works if you use a 1st person camera situated behind the light source (Due to the cameras vir dir, signs surface normals and light source being oblique to each other).

    I was thinking in the shader to use maths to bias the reflection vector from the sign's surface normals towards the camera but not a coder so I'm stuck.


    Any examples will help, Cheers!
     
  2. OhiraKyou

    OhiraKyou

    Joined:
    Mar 27, 2012
    Posts:
    259
    When I want to bias one direction toward another, I typically use a lerp function or shader graph lerp node to linearly interpolate from the default direction to the target direction. Then, the lerp's alpha is just a 0-1 bias slider. The result is normalized to ensure a unit-length vector appropriate for use as a direction.

    Code (csharp):
    1. float3 biasedVector = lerp(directionToLight, directionToCamera, biasTowardCamera);
    2. float3 biasedDirection = normalize(biasedVector);
    You just have to make sure the two directions are in the same space.

    You could also use the dot product between the two vectors to analyze how they're currently aligned (the result being -1 to 1) and apply any range remapping, clamping, smoothing, or other math operations you need. Then, the result could replace or modify the bias slider to produce a more dynamic effect. But, I find a simple 0-1 slider good enough for my usage.

    I use this to flatten foliage normals against a reference up vector so that the foliage blends well with flat terrain. Also, to flatten the normals of unimportant background elements against the view direction, reducing the perceptual 3D pop of specific objects (like tree leaves). In those cases, I simply plug the modified normals (in object space) into Shader Graph's vertex normal socket—so that the fragment stage automatically uses the flattened normals.
     
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,741
    Maybe don't? You want the reflections to be visible from angle where the fresnel effect would have removed most of the reflections, so just don't do fresnel and add the reflections without view angle mattering.