How do I access (and iterate) ColorArray and FloatArray data in Shader Graph?

I’m trying to send arrays of colors and floats to my PBR Shader from a script and access them in Shader Graph:

// From Script....

// no an array but using this to access the arrays below...
material.setInt("baseColorsCount", baseColors.Length);

// Array
material.SetColorArray ("baseColors", baseColors);
material.SetFloatArray ("baseStartHeights", baseStartHeights);

But what property do I select in order to access the values in the graph?

Oh one more thing, how does one iterate through any of these arrays within Shader Graph? How would I do something simple like this:

for (int i = 0; i < baseColorsCount; i += 1) {
   // Do something
}

…but in Shader Graph.

Thanks!

1 Like

I haven’t been using arrays with Shader Graph. I can only think about Texture2D Array.
Other than that, each property besides their name, have reference. This is what you want, to access properties from the script.
You need set it, if you like, to be more meaningful and use it in material.SetFloat, GetFloat etc.

Something like that ( you got different version of Shader Graph, so it may look a bit different).
6602530--750931--upload_2020-12-9_1-20-0.png

In this case, you use “_PrimaryColor”. I also suggest to cache string into int and use shader ID, rather string.

Thanks for replying!

So in my case where I need to “material.SetColorArray(“baseColors”, baseColors)”, something like this?

btw, I’ll definitely look into caching my shaderIDs.

Yep.
But here is a thing

So I don’t know, if that even is what you want.
What size of array do you need? Can you use vecotrs4, or matrix?

Alternatively, you would need write shader by hand. But that may be a bit beyond.

1 Like

I’d love for the array size to be dynamic, I have the following:

I’ll be sending this information to the shader from a script like so:

        material.SetInt ("baseColorCount", baseColours.Length);
        material.SetColorArray ("baseColors", baseColours);
        material.SetFloatArray ("baseStartHeights", baseStartHeights);
        material.SetFloat ("minHeight", minHeight);
        material.SetFloat ("maxHeight", maxHeight);

and I’m trying to create the following shader but in Shader Graph, but as you can see, the shader uses two arrays and I have no idea how to get around this and keep using Shader Graph:

So far I managed to get “heightPercent” working in my shader. I could even make a custom function and get it over with, but those arrays are confusing me.

1 Like

Could you not use a texture which contains values stored to different coordinates, then use some indexing system to access those values.

Hi there, do you mind sharing how you solved it? I am also looking into iterating an array of colors

Thanks

You can pass in the length using a separate property and iterate using a regular old for loop.
example is here:
https://github.com/Umbrason/DynamicInteractableShader/blob/main/Shader/GetNearestPosition.hlsl

1 Like

In case someone else comes across this I thought I’d add an option.

In some cases you can use a Gradient as a color array. Setting the mode to fixed makes it more obvious. Use the Sample Gradient node to look up the color you want.

The Sample Gradient node uses a 0 to 1 index though so there’s a little bit of math if you using integers.

9338522--1306574--GradientAsColorArray_useWithSampleGradient.png