Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted universal pipeline shader graph parallax

Discussion in 'General Graphics' started by natmaxex, Oct 16, 2020.

  1. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    33
    Universal pipeline shader graph parallax has been something i have been playing with and can't quite get working correctly.
    test 1.PNG

    Idk it looks alright when moving left and right or while rotating, but when i zoom in and out it looks strange.

    Note: Ignore the lerp texture on the end, that's just so i can see where the surface of the shader material is.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,388
    Divide the tangent space view direction vector by it’s own z before you multiply it.
     
  3. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    33
    like this?
    test 2.PNG

    How do i solve for z, is it the blue channel?
    pardon my ignorance.
     
    Last edited: Oct 16, 2020
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,388
    Divide the entire view direction vector by z. Not the view space view direction's z, the same tangent space view direction's own z. But yes "z" == "B" in the shader graph context.

    The Divide node's "A" and Split node's "In" should be taking the tangent space View Direction node's "Out(3)" directly. The Divide's "B" should be taking that Split node's "B" output. Then the Divide node's "Out(3)" should go straight into your Multiply nodes.
     
  5. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    33
    How about now?
    test 3.PNG
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,388
    Looks good to me, what does it look like to you in the scene view? I might suggest testing it with out the noise though, just a constant offset, and see if it looks correct. Know this kind of number offset should be perfect on any flat surface when using a constant offset. But it won't be "correct" for anything else, so it may still look funny if you're using a noise or texture displacement.
     
  7. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    33
    It doesn't look that much different from my first try. Is there a way i can be more accurate in deformations to the height on flat and mesh surfaces?
    I wish there what more information on how to make advanced shaders.
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,388
    Depends on what you mean by "advanced" shaders. There's a ton of information out there on how to make advanced shaders, just not a lot that use Shader Graph because it's relatively new, and to be honest Shader Graph isn't where you write "advanced" shaders.

    I did notice one thing that's not quite right with your above graph. The noise is using the view direction as the UV. That's always going to look really weird. Just break that connection and use the mesh's default UVs, but that's still going to look funny since, again, bump offset is a very basic approximation. For a flat plane being offset by a constant amount, it's basically ground truth. The problem is it's offsetting by the height texture (in your case the noise) sampled at the mesh surface, which has nothing to do with the height you want, which is the position some distance below the surface. For that you need "POM" (Parallax Occlusion Mapping) or one of its relatives (Relief Mapping for example).

    Both of those are ray marching style effects. Basically you are stepping through the height field at some fixed distance until you find you're "under" the height map surface at that depth & uv offset and render the main texture there. To those kinds of effects usually requires a loop, or a lot of copy & pasting of the same code over and over. Loops aren't possible in Shader Graph (using nodes), and you need a lot of copies of the same chunk of code to get anything that looks good. For example here's part of a simple POM shader written in Unreal's material system.


    So the usual solution to this is to write a custom node that does the advanced work for you. Lots of people wrote these for Shader Graph early on, but Unity broken and removed support for custom nodes a while ago. So now the solution is to find some example shader code written in regular hlsl, and use a Custom Function node to call the function direction, passing the necessary info into it.

    Normally I immediately push any Shader Graph related posters to the Shader Graph subforum, but I kind of didn't here because I figured the conversation would end up here, and the solution isn't really "in" shader graph. At least not if you're using the URP. The HDRP already has a Parallax Occlusion Mapping node. There's no reason it couldn't also be used for the URP, there's nothing HDRP specific about POM, apart from Unity decided not to expose it to the URP. :mad:

    There's also lots of POM examples out there for Unity, but none of them will work with the URP & Shader Graph since those are all written for Unity's built in rendering paths, and you need to write shaders a little differently for the SRPs. Here's the code Unity uses:
    https://github.com/Unity-Technologi....core/ShaderLibrary/PerPixelDisplacement.hlsl
     
    natmaxex likes this.
  9. natmaxex

    natmaxex

    Joined:
    Jul 12, 2012
    Posts:
    33
    thanks, I figured as much. I guess I thought I could spit hairs and create a crude parallax effect using a silly method.
     
unityunity