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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question view direction in blit pass

Discussion in 'Shaders' started by Ozego, Aug 11, 2020.

  1. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    Hello. I am trying to calculate the view direction in a blit pass for a image effect; but I am having some trouble calculating the direction each pixel is looking/pointing. The result I need is a normalized float3.

    I tried quite a few different attempts but I guess my matrix math is too weak. The closest I've gotten is

    float3 viewdir = mul(_CameraToWorldMatrix ,float3((.5-o.uv.x)*_AspectRatio,.5-o.uv.y,1));
    Where I set _CameraToWorldMatrix and _AspectRatio in c#.
    All help is greatly appreciated!
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,265
  3. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    Thank you bgolus. I reverse engineered keijiro's project and extracted the tasty bit.

    For future reference:
    vertex program
    v2f vert (appdata v)
    {
    v2f o;
    o.uv = v.uv;
    float3 pos = float3(v.uv*2.-1.,1);
    o.vertex = pos.xyzz;
    o.ray = mul(unity_CameraInvProjection, pos.xyzz ).xyz;
    o.ray.x *= -1;
    o.ray = mul(unity_CameraToWorld, o.ray.xyz);
    return o;
    }

    and then just
    float3 viewDir = normalize(i.ray);
    in the fragment program and you will have the viewdir.