Search Unity

How to get the correct viewDir on image effect shader?

Discussion in 'Image Effects' started by AhSai, Mar 21, 2019.

  1. AhSai

    AhSai

    Joined:
    Jun 25, 2017
    Posts:
    129
    I am current trying to achieve something like this: upload_2019-3-21_6-10-3.png
    , where when the camera facing the sun, it will be have different color. If it is on a object other than the camera, viewDir is calculated by subtracting _WorldSpaceCameraPos by object's worldPosition. However, for image effect shader, the object itself is the camera, so how do I get the correct viewDir on an image effect shader?
     
  2. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    The camera has a view direction, it is it's forward. which you can either pass on as a texcoord after calculating the world space camera forward from it's object space (0.,0.,1.0) or you can set it as a property in CPU on the material. To get the per pixel effect which yo there you can do the dot(cameraForward, SunPos - CamPos) to get the factor with which you can use as a transparency for this over blow effect (can also be a lens flare texture). Btw you can also get the effect in the image by using bloom.
     
  3. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    Adding to that answer. that is the case if one view direction is enough for you (most cases it is enough for me) however if you want a per pixel view direction, then you can calculate that in different ways. simplest is to calculate the view direction (camera to pixel pos on the near plane) in vertex shader for all four corners, saving it in a texture coordinate and let it be interpolated from vertex to the fragment shader for each fragment.
     
  4. georgetowersassets

    georgetowersassets

    Joined:
    Nov 21, 2020
    Posts:
    2
    were you able to figure this out? it works if done in the vertex shader but for devices where custom blitting isn't available we need to convert that to frag shader code,
    the response given by Sharhabi is horribly wrong in many ways: number one: is beyond naive to think the camdir is 0,0,1 but regardless dot((0,0,1), sunpos-campos) makes a gradient from 1,0 towards the whole screen from _Col2 to _Col1
    number 2 calculating viewpos in the vert (for a quad) will give you 4 values to interpolate between, again useless
    number 3: the effect is athmospheric scattering, not a lens flare nor bloom so it can't be replicated by those...