Global properties for VFX graph?

Would be nice to be able to set some global properties like we can do it in shaders.

Morning, and thanks for your feedback and being active recently on the VFX Graph section of the forum.
Global property exists in VFX Graph and can be easily read thanks to Custom HLSL.

Custom HLSL code can even be used seamlessly between shader graph and VFX Graph.

You can take a look at this Unite Session where Global properties are used to either control a Rain Intensity and Wind Direction. Those Global are read and modulate all the shaders and VFX in the Scene.

Now, I guess that you might suggest being able to easily get the reference of a global directly in the Blackboard, like in Shader Graph ?

You can’t really Set the Global value in VFX Graph, as you can’t set it inside a Shader Graph (you can set a preview value). But you can Get a Global Value thanks to Custom HLSL, and here are the steps:

  • Create a C# Script that initializes and set the Global.
    Here is a simple MonoBehavior that allows you to set a Global Color named “myColor”:
using UnityEngine;

namespace SetGlobal
{
    public class DeclareGlobal : MonoBehaviour
    {

        [SerializeField] private Color myColor = Color.black;
        private static readonly int MyGlobalColor = Shader.PropertyToID("myColor");

        private void OnValidate()
        {
            Shader.SetGlobalColor(MyGlobalColor, myColor);
        }
    }
}
  • Add this script to a Game Object and use it to set the Global Color value.
    Unity_EoIP9LIryO

  • Create an HLSL file and declare a float4 with the name of the previously set Global.

float4 myColor ;
  • In your VFX Graph, create a custom HLSL block and include your previously created hlsl file.
#include "myGlobals.hlsl";

*You can now get the value to drive your particle’s behavior or visual aspect.

#include "myGlobals.hlsl";

void SetColor(inout VFXAttributes attributes)
{
	attributes.color = myColor.xyz;
}

You can now use the Global Value to drive your shaders and VFX at once.
Unity_y4THiagrJi

I’m joining a small package with this scene to help others get started with Global in VFX Graph.
VFXG_GLobals.unitypackage (15.0 KB)

P.S: you can, also take a look at this package that helps with the use of Global inside Unity.

I wish you a beautiful weekend.

I assume it quite new because simple google search didn’t gave me anything.
Thanks for this detailed explanation!

Yes, it’s new thanks to VFX Graph Custom HLSL in Unity6. Don’t hesitate to take a look at the provided video of the Unite session to see Global in Actions.


Ayo!
Is there any reason of why the same node wont work in another VFX system?
Seems like the solution only worked oncem, thanks a lot.
This is the error I’m getting

Also, the package un recommended is not working on Unity6+!

I’ve tested the package that I provided as a simple exemple and it’s working in Unity6.
Now, I should update this small package so that it works on Both URP and HDRP.

Are you talking to some HSLS file included in this Repo ?

If so, this repo isn’t officialy supported by Unity but it supposed to help with Globals.
Let me summon @FredMoreau here :slight_smile:

I just tested the Shader Globals package. in Unity 6.0.43f1 and it works just fine.

@SrtoRubfish are you getting errors or warnings? Or is there something that doesn’t work as expected? Can you provide more details? Thanks

Is there a way to get and output a Texture2D type set from Shader.SetGlobalTexture? This below works great in ShaderGraph custom function but I can’t figure out how to do this in vfx graph. I see in documentation that there is a VFXSampler2D type? Is it possible to just pass Texture2D for sampling in a node in vfx graph? Documentation

Texture2D<float4> _SHADOWS_MASK;
SAMPLER(sampler_SHADOWS_MASK);
float4 _SHADOWS_MASK_TexelSize;
float4 _SHADOWS_MASK_ST;

void GetGlobalShadows_float(out UnityTexture2D shadowsMask)
{
    shadowsMask = UnityBuildTexture2DStruct(_SHADOWS_MASK);
}

How to read global texture in custom hlsl operator? I’ve read the docs but they contains nothing about it. How to use SampleTexture method to read global texture?

Edit: Looks like this works

Texture2D _MyGlobalTex;
SamplerState sampler_MyGlobalTex;

float4 GetMyGlobalTex(float2 uv, float mipLevel)
{
    return SampleTexture(VFX_SAMPLER(_MyGlobalTex), uv, mipLevel);
}