Skip to content

Globals

Thomas Iché edited this page Oct 27, 2022 · 2 revisions

Globals

The globals feature enable users to define global data structures that contain global data from your application, for example, the player position. These data become available in VFX Graphs without requiring to bind the values individually at the component level.

In order to define values, you need to create a VFX Globals definition asset, where you will reference an HLSL include (that you can generate/update as well using the inspector menu), and declare global values.

Then, using the Get Globals node and the Include Globals Block (required for reading these values in a context), you can access the values set by the code. In order to set the values in monobehaviours, simply use the Shader.SetGlobal...() API. For example :

[ExecuteAlways]
[RequireComponent(typeof(SphereCollider))]
public class SetSphereGlobal : MonoBehaviour
{
    SphereCollider m_Collider;
    private void OnEnable()
    {
        m_Collider = GetComponent<SphereCollider>();
    }

    private void Update()
    {
        Shader.SetGlobalVector("spherePosition", transform.position);
        Shader.SetGlobalFloat("sphereRadius", m_Collider.radius * transform.localScale.x);
    }
}
Clone this wiki locally