Search Unity

Shader Graph - Vertex position on top of other object

Discussion in 'Shaders' started by eukolo12, Jun 28, 2021.

  1. eukolo12

    eukolo12

    Joined:
    Mar 19, 2021
    Posts:
    4
    Hi,
    I just cannot find the proper solution on how to create a shader, that alters the vertex z position so that my texture is always above other elements.
    Here is an example, the yellow circle should render above the bridge:


    I'm new to shader graph, so m approach would be as following (just don't know where I can get the z position of the overlaying object to add it to the shaders z coordinate)


    I would appreciate any help.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The short answer is: You can't.*

    The long answer is: Shader Graph is missing a ton of features, especially in the URP, that would let you do what you want in a sane way. There's no way to adjust the "Z" of the vertices to be above another object, because there's not a good way to know the height value of another object from a shader of any kind.

    Depending on what you want the visual to be, some options could be to use a custom non-shader graph shader (or hand modify the generated shader code from a shader graph shader) that uses
    ZTest Always
    so that it your shader always renders "on top" (in screen space) of other objects in the scene, or to use a script component to raycast against the ground and move the entire gameobject up, or use a decal projection.

    For that last option, the decal projection, there are some examples of custom decal projector shaders that work with the URP, but I don't think any are built using Shader Graph.

    It would technically be possible to write your own decal projector using Shader Graph, but that requires some substantive knowledge of shaders to execute, and sort of pointless since Unity added official decal support to URP 12 which just recently came out. I'm not sure what version of Unity URP 12 uses, but I think it requires the 2021.2 alpha. It's new enough that Unity's URP 12 documentation still says URP 11 though...
    upload_2021-6-28_11-26-1.png
     
    Last edited: Jun 28, 2021
  3. eukolo12

    eukolo12

    Joined:
    Mar 19, 2021
    Posts:
    4
    Thank you very much. I guess then I will just learn shader scripting (programming?) and see if I can come up with a solution.
    Or maybe just iterate over each vertex and adjust the z position, depends on performance.
    Nevertheless, thank you :)