Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. 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 Shader Graph equivalent to the "Offset" property in shader code?

Discussion in 'Shader Graph' started by Nerdin8or, Apr 18, 2019.

  1. Nerdin8or

    Nerdin8or

    Joined:
    Feb 19, 2018
    Posts:
    7
    So we're working on a game that combines 2D pixel sprites and 3D geometry. We have been working in 2018 and I want to move to 2019 so we can use shader graph.

    The problem is we were rotating the sprites to exactly face the camera to minimize pixel distortion, and I added "Offset -1, -1000" to the sprite shader code so that their heads wouldn't clip into geometry they were supposed to be standing in front of. Is there any equivalent to that in shader graph? Some way to force the sprite to render on top if part of it is intersecting with 3D geometry? I unfortunately didn't even fully understand what that Offset line was doing, but it fixed our problem for the time being, so I was content...
     
  2. Nerdin8or

    Nerdin8or

    Joined:
    Feb 19, 2018
    Posts:
    7
    Anyone have any ideas?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,096
    The "Offset" option from Unity shaders is a weird beast, the exact implementation of which differs between GPUs. Roughly speaking it pushes each fragment towards or away from the camera by some amount based on an arbitrary per implementation multiplier that may take into account the surface's "slope" * the first number + another arbitrary per implementation multiplier * the second number.

    Generally speaking, it would be best replicated modifying the depth output value, either from the fragment shader or vertex shader clip space, but neither are things you're given access to with the Shader Graph. So the next best option is to calculate the per-vertex view dir and offsetting the vertex in that direction.
     
    bbqbologna and NeatWolf like this.
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,096
    Depending on your use case, this might be good enough:
    upload_2019-4-23_1-45-7.png
    You'll want the OffsetFactor to be quite small, less than 0.1 most of the time. This doesn't accurately replicate the full functionality of the Offset setting, but gets a big part of it.
     
  5. Nerdin8or

    Nerdin8or

    Joined:
    Feb 19, 2018
    Posts:
    7
    Thanks for the reply! I'll try that out.
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,899
    Nice tip, thanks!
     
  7. Blinkq

    Blinkq

    Joined:
    Apr 19, 2015
    Posts:
    21
    Thank you, in my case I was using this offset for road on terrain, and when I was using offsetFactor, road was moving to the side. So I changed Vector1 OffsetFactor to Vector3 and using only y and z.
     
  8. Simpowitch

    Simpowitch

    Joined:
    Nov 13, 2018
    Posts:
    6
    Amazing! Thank you so much!
     
  9. dimzki

    dimzki

    Joined:
    Apr 17, 2018
    Posts:
    2
    I was following Catlike's tutorial on how to make Hex Grid, on part 7 where it explains how to make road system,
    their tutorial is using the standard shader, but my project is using URP
    i almost gave up trying to find a way to make it work,
    but your explanation saved me, thank you very very very much !
     
  10. yogioh1234567

    yogioh1234567

    Joined:
    Aug 15, 2017
    Posts:
    3
    i use it ,but the shadow not correct
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    10,096
    Ideally you'd only ever use offset like this for something that's not shadow casting, because any offset will cause shadow casting and receiving problems, especially if you push the offset too much.
     
  12. yogioh1234567

    yogioh1234567

    Joined:
    Aug 15, 2017
    Posts:
    3
    maybe i should create 2 model, 1 for shadow,1 for show, is a good idea ?
     
unityunity