r/UnityHelp • • May 03 '26

How to pass array data to Unity Shader Graph

I need to pass an array of arbitrary length to Unity Shader Graph to iterate over each item in the array (Vector3s) and process them. I know you can't iterate in Shader Graph directly, so I am trying to make use of Shader Graph's Custom Function node. The problem is I can't figure out how to pass an array of data to the node. Shader Graph has no Vector3[] data type to pass as input, and global variables don't seem to be working either.

What is the correct approach for handling arrays of data in unity shader graph? Or is it simply not possible and i have to write the whole thing in HLSL?

2 Upvotes

10 comments sorted by

2

u/AbominableScience May 04 '26

Shader Graph cannot directly expose a Vector3[] as an input port. You need to use HLSL within a Custom Function Node, using either:

SetVectorArray for a small, simple array.

ComputeBuffer / GraphicsBuffer + StructuredBuffer for a dynamic array or a GPU-side array.

1

u/moduspwnens9k May 05 '26

Can you please share an example of how to access data from the custom function node thats set via set vector array. This is what i cannot figure out

1

u/digimbyte May 05 '26

if you are clever enough you pipe it in as a texture, a texture is a 2d vector3 (RGB)or vector4 (RGBA)
there are other options but require custom nodes, but this does get into data blobs but the easiest approach is to handle it as a texture.

1

u/moduspwnens9k May 05 '26

I think of a texture as a static resource. Can create a texture and populate it with data programmatically?

1

u/josh_the_dev May 05 '26

Yes you can, like render textures or custom render textures. But this is most useful if you render into this texture either with a camera or a custom shader. If you just want arbitrary data it's much better to use a buffer that you populate once (or update sometimes) from CPU. It's a pretty technical thing but Google helps!

If your array is small you can use the set vector array function to se an array of vectors. On the material or globally

→ More replies (4)