Search Unity

Question Global variables in the Shader Graph (URP/LWRP)

Discussion in 'Shader Graph' started by RedMattis, Feb 1, 2020.

  1. RedMattis

    RedMattis

    Joined:
    Apr 18, 2015
    Posts:
    33
    I'm trying to figure out how to get global variables to work within URP.

    I've tried setting a variable in the Shader Graph and then setting the reference name doing something like this:
    Code (CSharp):
    1. Shader.SetGlobalFloat("Vector1_71B9E5F1", currentFloor);
    Nothing seems to happen though, so I'm guessing that would have to be done on the actual material instance?

    I've also tried something like
    Code (CSharp):
    1. Shader.SetGlobalFloat("_GLOBAL_FLOOR", currentFloor);
    , and then setting it in .hlsl code (something I'm not very familiar with). But if that is the way, then I don't think I know the correct syntax. I've tried a bunch of variants of the below without success:

    upload_2020-2-1_22-12-13.png

    I've also tried changing the reference string of a property to _GLOBAL_FLOOR, but that just made unity instantly crash to desktop when I pressed 'enter' in the field.

    I'm running out of ideas, and I haven't been able to find any solution that seems to be Shader Graph compatible.
     
    Last edited: Feb 1, 2020
  2. alexandral_unity

    alexandral_unity

    Unity Technologies

    Joined:
    Jun 18, 2018
    Posts:
    163
    When you were trying to set the property using the reference name from the blackboard, did you have the "exposed" toggle enabled? You need that disabled to properly set variables from script.
     
    grezol, Michele23452 and Kokowolo like this.
  3. RedMattis

    RedMattis

    Joined:
    Apr 18, 2015
    Posts:
    33
    I tried both. I'll give it another try though.

    Should I ever change the reference reference ids? (Vector1_71B9E5F1, f.ex.). I did try changing them before (to EnvironmentAlbedo f.ex.), but it resulted in my editor-chain-crashing until I manually edited the graph file to remove the input and all references to it within that file.

    When I set the property, do I have to set it on a specific GameObject, or can I set it globally for all of Unity? I'd prefer to do the later, because my gut feeling is that running Update loops on every single game object with a material (to set the variable on it) would be terrible for performance.

    With all that said, while I'll probably need global variables later regardless, this particular problem might be better solved with some sort of occlusion. What I was really trying to to was:
    A) Hide roofs/floors/above of buildings when the player enters them (3rd top-down person game) / hide ground, etc. when going into a basement.
    B) Black-out or perhaps cull the world based on whether or not it was visible to the player object (not the main camera, the main camera is a top-down one)
     
  4. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    I got a similar issue, i have to use the reference ID and can't use the custom name i set for my vector1 otherwise the setglobal doesn't work, it's not really nice because i got 2 differents shader so instead to use one global name i have to got to different reference in my script
     
  5. MissEmbyr

    MissEmbyr

    Joined:
    Nov 8, 2016
    Posts:
    13
    You can change the reference ID, you should make them match so you're only using one keyword slot.
     
  6. Black_Raptor

    Black_Raptor

    Joined:
    Nov 3, 2014
    Posts:
    181
    Thanks you i didn't know we can change the reference ID without any issue with the shader graph !
     
  7. RMGK

    RMGK

    Joined:
    Sep 30, 2011
    Posts:
    75
    Turning "Exposed" off worked for me. 2020.2
     
    Kokowolo likes this.
  8. dblaauw

    dblaauw

    Joined:
    Oct 7, 2013
    Posts:
    9

    How do you turn "exposed" off if it's not there? I'm using the URP Lit Shader Graph, and whenever I add a property in the blackboard, I just see the name and type, but there's no "exposed" option or anything else (no option to add defaults either). When using the VFX graph I do see the "exposed" option.

    I'm trying to make a snow system that shades my simple low poly world white when snowing, and I want to control this effect from C#. I had it working in 2019.4, but now I have to re-do everything since I moved to 2020.2 and had to use URP for our project.

    Screenshot 2021-01-10 171853.png
     
  9. dblaauw

    dblaauw

    Joined:
    Oct 7, 2013
    Posts:
    9
    I'm so stupid! Just click the "node settings" button to see the node properties!

    Screenshot 2021-01-10 183832.png
     
    Kokowolo likes this.
  10. Michele23452

    Michele23452

    Joined:
    Jul 8, 2021
    Posts:
    1
    Thank,dude