Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Camera position, world direction shading artifact

Discussion in 'Shaders' started by catmxt, Jul 27, 2021.

  1. catmxt

    catmxt

    Joined:
    Aug 2, 2019
    Posts:
    8
    Unity 2021.1.15f1
    URP 11.0.0

    i'm working on porting this road from a BRP surface shader to a URP vertex/fragment paradigm. ive run into this artifacting where everything in the -Z direction relative to the camera fades to black. i am using the
    UniversalFragmentPBR
    lighting function provided in
    Lighting.hlsl
    , which expects an
    InputData
    struct along with some surface parameters. one of the struct's fields is the world space view direction (towards the camera), whose z component would be negative on the left side of this frame and positive on the right. upload_2021-7-27_17-49-56.png


    this in mind, i tried modifying the value of
    inputData.viewDirWS.z
    to make it always negative:
    viewDirWS.z = -abs(viewDirWS.z)
    and... it worked?
    upload_2021-7-27_18-0-21.png


    so my question is.. what could i have done wrong to get this weird behaviour in the first place? and why did my dumb force-negative hack fix it?

    the shader is pretty bog-standard beyond the road markings (which are beyond the scope of this thread) based on posts such as https://cyangamedev.wordpress.com/2020/06/05/urp-shader-code/9/ and https://catlikecoding.com/unity/tutorials/rendering/part-6/ to write vertex and fragment shaders in URP.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    This makes me wonder if you’re trying to use this as a view space view dir somewhere in your code.

    The
    viewDirWS
    is effectively the world space position relative to the camera.
    “surface world space position - camera world space position”
    If you’re trying to use it as a view space view direction, or with another vector that is in view space, I can see the z value causing weirdness, as in view space -z is forward.
     
  3. catmxt

    catmxt

    Joined:
    Aug 2, 2019
    Posts:
    8
    hey, no i don't think i'm doing that. as per the first link in my post, i'm retrieving
    viewDirWS
    with
    GetWorldSpaceViewDir(positionInputs.positionWS)
    from
    ShaderVariablesFunctions.hlsl
    in the vertex shader. then it's assigned directly to an interpolator in my
    Varyings
    struct and not used again until being assigned to the
    InputData
    struct for use in a unity-supplied lighting function in the fragment shader.

    i thought it was a view space issue too, but if it were i would have thought that rotating the camera would have had an effect on the artifact's appearance, which isn't the case. regardless of the camera's rotation, the fade-to-black artifact is aligned along the world z-axis with the start of the fade coinciding with the camera's z-position upload_2021-7-28_10-43-43.png
     
  4. catmxt

    catmxt

    Joined:
    Aug 2, 2019
    Posts:
    8
    i think i've figured it out.
    viewDirWS
    was a red herring, actually what was happening was that i was using treating tangent space normals as world space normals, which of course were all aligned along the positive world z-axis.
     
    bgolus likes this.