Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Shader Graph' started by CassieNova, Dec 8, 2020.

  1. CassieNova

    CassieNova

    Joined:
    Oct 25, 2020
    Posts:
    8
    I'm trying to send arrays of colors and floats to my PBR Shader from a script and access them in Shader Graph:

    Code (CSharp):
    1. // From Script....
    2.  
    3. // no an array but using this to access the arrays below...
    4. material.setInt("baseColorsCount", baseColors.Length);
    5.  
    6. // Array
    7. material.SetColorArray ("baseColors", baseColors);
    8. 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:

    Code (CSharp):
    1. for (int i = 0; i < baseColorsCount; i += 1) {
    2.    // Do something
    3. }
    …but in Shader Graph.

    Thanks!
     
    lacas8282 likes this.
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    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).
    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.
     
  3. CassieNova

    CassieNova

    Joined:
    Oct 25, 2020
    Posts:
    8
    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.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    Yep.
    But here is a thing
    https://docs.unity3d.com/ScriptReference/Texture2DArray.html

    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.
     
    marcosbiagi likes this.
  5. CassieNova

    CassieNova

    Joined:
    Oct 25, 2020
    Posts:
    8

    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:

    Code (CSharp):
    1.  
    2.         material.SetInt ("baseColorCount", baseColours.Length);
    3.         material.SetColorArray ("baseColors", baseColours);
    4.         material.SetFloatArray ("baseStartHeights", baseStartHeights);
    5.         material.SetFloat ("minHeight", minHeight);
    6.         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.
     
    marcosbiagi likes this.
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Could you not use a texture which contains values stored to different coordinates, then use some indexing system to access those values.
     
  7. wengyhong

    wengyhong

    Joined:
    May 19, 2021
    Posts:
    6
    Hi there, do you mind sharing how you solved it? I am also looking into iterating an array of colors

    Thanks
     
  8. Umbrason

    Umbrason

    Joined:
    Apr 1, 2018
    Posts:
    1
    mitaywalle likes this.
  9. Residual_Motion

    Residual_Motion

    Joined:
    Sep 20, 2020
    Posts:
    1
    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.
     

    Attached Files: