Search Unity

Object / World Space problem... [ShaderGraph Shader]

Discussion in 'Shaders' started by Casse, Dec 25, 2018.

  1. Casse

    Casse

    Joined:
    Jan 26, 2014
    Posts:
    20
    I'm trying to recreate MinionArts fake liquid Shader but by using ShaderGraph instead...
    https://twitter.com/minionsart/status/986374665399685121

    I'm currently stuck as I'm trying to get the liquid to stay "flat against gravity" while rotating the object... If I use World space on the Position node it works except the liquid volume is stuck on y=0, so it only works if it rotates around if it's on y=0. If I change the space to Object, the liquid stays with the model - but now the rotations no longer work...

    So I need some way to give me the benefits of World Space but keep the liquid with the object regardless of where it is (somewhere that isn't y=0)

    Shader:
    Screenshot_61.png

    Shader in use (with World Space on the Position node - flat with gravity but only works at y=0):
    Screenshot_62.png

    Shader in use (with Object Space on the Position node - now stays with the model but the rotations no longer work):
    Screenshot_63.png
     
    Kasta11 likes this.
  2. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    Did you solve this problem? Facing similar issue.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Subtract the object position from the world space position.
     
  4. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Knipsch likes this.
  6. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    Changed that graph as you said (hopefully)


    but still same problem, object vertices are still moving around objects Y axis and not worlds Y axis:
    https://imgur.com/LEMdRWD
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Oh, I missed that you're directly manipulating the vertices. That's not going to work for a number of reasons. Not the least of which is you're passing in the object relative world position. Look at that gif you posted, notice you actually rotated the mesh 180 degrees!

    To get what you have working a little more like what you want, try adding a Transform node at the end to transform from world to object using the Direction type.

    Also you're using the XY value from the world position to drive your noise. You probably want to use XZ?
     
  8. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    Thank you for help, I'll try to absorb what are you trying to tell me. But I don't think I'll get the wanted result anyway with particular graph.

    All I want is to move vertices up and down on world Y axis independently on object rotation. Then I want to affect this movement based on Y world axis, so the lowest vertex on Y world (not object Y) axis doesn't move at all and the highest move the most.

    Trying to figure out by myself a few days, but shader graph is too poorly documented (or I didn't found anything except almost empty unity documentation and a few Brackeys videos :D).
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
  10. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    Did exactly same graph as on the screenshot and it doesn't work at all, in scene mesh disappears and in shader graph is nothing on the main preview too.

    I'am using HD pipeline renderer, could it be the problem?
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Ahh ... yep. That's a key bit of information. The world position node is broken in the HDRP. There is no "world position" in the HDRP, but rather a camera relative position. So far the nodes are inconsistent about how to deal with that and some nodes assume actual world positions, and others are camera relative positions.

    There's a thread somewhere on the Shader Graph forum on that topic which I think the official Unity response was something along the lines of "oh yeah, that doesn't work, not sure how to fix that, sorry".
     
  12. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    Thank you for everything, you helped me a lot anyway.
     
  13. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Here's a SRP agnostic solution:
    upload_2019-5-22_17-22-11.png
     
    ajdonn01 likes this.
  14. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    You are a genius!!!

    I hope I finally get what I was trying to find out the whole time.

    Few questions:

    1. Why do we need to use remap and why are we using values (-0.25f, 0.25f)?
    2. Output of that saturate node is 0 for the lowest vertex on axis (in this case Y) and 1 for the highest vertex on axis?

    3. What saturate do in general as a node?
     
  15. Kasta11

    Kasta11

    Joined:
    Jan 19, 2016
    Posts:
    8
    4. Trying to figure out what is going on before and after remap (using cube with pivot in center of bottom face not in object center and (0f, 1f) values as remap InMinMax):
     
  16. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Arbitrary decision, but the default sphere and box meshes have pivots at their center and are 1 unit tall, so unrotated & unscaled the min max is from -0.5 to 0.5 off of the pivot. Using -0.25 to 0.25 meant that the top vertices were being fully displaced, the bottom vertices aren't being displaced at all, even with some minor rotation, and there was still a small window of transition.

    Yep.

    Clamp between 0.0 and 1.0.
    https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-saturate

    If it's at the bottom, then the first example would be 0.0 to 1.0 prior to the remap already, no? When you rotate that, the bottom vertex will be at -0.35, the "middle" two vertices will be at 0.35, and the top vertex will be at 1.05. That's not quite the perfectly balanced setup your examples show, but might be good enough. The bigger issue is really going to be scale. Since the above shader applies the object's scale then any kind of scaling of the mesh will mess all of this up, especially if your pivot is at the base.

    You can work around that by doing the lerp in rotated, but unscaled world space. Like this:
    upload_2019-5-23_10-10-50.png
     
  17. Arkreo

    Arkreo

    Joined:
    Jun 25, 2018
    Posts:
    1
    Hello, is there any way you could send me what you did? Maybe your shader or even a part of your project using it? I'm facing issues having things to work properly (kind of a beginner with shaders...). In facts, when I apply the same shader as you did to my material, it instantly disappears. Nothing is rendered.
     
  18. Subliminum

    Subliminum

    Joined:
    Nov 9, 2017
    Posts:
    97
    https://www.patreon.com/minionsart

    Here's Joyce's Patreon, she is the original artist who inspired this thread. She has also now made a lot of her incredible shaders available in their ShaderGraph form (some are free, some are patreon bonus')...if you are still interested.

    Her Twitter handle;
    https://twitter.com/minionsart
     
  19. TastyJammy

    TastyJammy

    Joined:
    Aug 26, 2019
    Posts:
    3
    Can you please explain why substracting this two position gives correct result in this case
     
  20. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    Because you want a world space position relative to the object's position, not relative to the world origin. Subtracing the object position means "0,0,0" is now at the center of the object.
     
    TastyJammy likes this.
  21. TastyJammy

    TastyJammy

    Joined:
    Aug 26, 2019
    Posts:
    3
    Thank you for your reply, but isn't moving the origin to the center of the object means I am getting object space position?
     
  22. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    No. This is still world aligned and world scaled space, just object centered. An object space position would turn and scale with the object. For example the original post had the last image with the liquid surface going diagonal. That was using object space where the “up” was rotated with the object.
     
    TastyJammy likes this.
  23. TastyJammy

    TastyJammy

    Joined:
    Aug 26, 2019
    Posts:
    3
    Oh now I got it. Thank you very much