How to access texture set by Shader.SetGlobalTexture() in URP shader graph?

As per the post title, how can I access texture set by Shader.SetGlobalTexture() in URP shader graph?

Shader.SetGlobalTexture("_TerrainData", terrainTexture)

In URP Shader Graph, how to get texture and sample it?

The terrain data doesn’t seem to get assigned.

Hi @jakesee ,

From your screenshot I guess you’re on a version earlier than 6.
You may just uncheck Override Property Declaration and Exposed altogether and it will make that property a global.

In Unity 6, we simplified the UI to better reflect the Scopes.
You would simply set it to Global.

Questions:

  • which version of Unity / URP are you on?
  • which sub target (material) do you use? Unlit/Lit?
  • is this a material to be applied on a Terrain?
  • what are you trying to achieve?

Thanks

Heya, thanks for helping out.

Unity 2022.3.48f1
URP 14.0.11
URP Lit

I have a grid based terrain, the global Texture2D holds information about cells like visibility and surface type (grass, sand, etc.). The shader I am trying to make uses cells information from the global Texture2D to blend colors.

What is the best way to check whether the global texture has been assigned during Play Mode?

If I uncheck Override Property Declaration, I cannot set “Global” Shader Declaration.

Checked

Unchecked

At the moment, I fallback to using a Custom Function node, with a .hlsl file, which looks like this below, and it works, i.e. the creation and assignment of global texture works. But I would like to know how to avoid using custom function if possible.

#ifndef MYHLSLINCLUDE_INCLUDED
#define MYHLSLINCLUDE_INCLUDED

sampler2D _TerrainData;
float4 _TerrainData_TexelSize;

void GetCellData_float(float4 indices, float nb, out float4 data)
{
    // ....
   data *= 255;
}

#endif

Hi,

Custom Function Nodes are probably better in that you can put them inside Subgraphs too.

Yes, in previous versions (before Unity 6), not exposing a property would implicitly make it a Global. You don’t need to override the property declaration for that. It’s why we revamped the UI.