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

Feature Request Access world space normal mapped normal in graph

Discussion in 'Shader Graph' started by khalvr, Nov 15, 2019.

  1. khalvr

    khalvr

    Joined:
    Sep 12, 2017
    Posts:
    53
    Is there any way to access the world space normal with the normal map applied? I remember this being a limitation of surface shaders too, which is why i had my own include files for splicing together PBR shaders. There are many effects which you might want to make depend on the pixel normal, such as shader-based dust layers. I know you can transform the normal map in the graph using the normal and the tangent, but this calculation is expensive enough as it is and there is no sense in doing it twice.
     
    Last edited: Nov 15, 2019
    fffMalzbier likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Use the Transform node, set to tangent to world.

    How else do you think this would be exposed to the user if it’s not being calculated using the normal and tangent? If by “twice” you’re referring to the fact the shader transforms the normal value plugged into the Master node into world space, shader compilers should recognize this is the same thing and optimize the duplicate work away.
     
  3. khalvr

    khalvr

    Joined:
    Sep 12, 2017
    Posts:
    53
    Yes, this is what i'm referring to. The PBR master node expects normal input to be in tangent space. The normal is then transformed to world space before lighting calculations. Do you know for sure that the compiler will catch this? Considering how explicit you need to be with some other things, i'm not sure if i trust that. What i envisioned was a drop-down option for the normal node to use the pixel normal instead of the vertex normal, which would place dependent calculations in the fragment function.

    I tried making a test graph for this case, with a normal sampler piped into the normal map, and also piping it through a tangent>world node into the albedo. When hitting "compile and show code", it gets stuck in a loop and has so far compiled "11,000 out of about 1156 variants", so i can't really test the validity of what you're saying (though it would probably be too much for me to sift through anyway). There is no such optimization in the HLSL code generated by shader graph though.