#Shader custom function with per material array

1 messages · Page 1 of 1 (latest)

fluid fiber
#

Hello,

I want to have an array of colors in a custom function for shader graph


float4 _MyColorArray[4];
int _GlobalSpectralIndex;

void GetColorFromArray_float(out float3 result)
{
    result = _MyColorArray[_GlobalSpectralIndex].rgb;
}```

But so far, if I have 2 materials from a shader graph simply using the custom function... I can see the material updating inside the editor but nothing in the game view.
Let me share the c# code.
#
using System.Linq;
using UnityEngine;

public class ShaderTest : MonoBehaviour
{
    public MeshRenderer m_renderer;
    public MeshRenderer m_renderer2;

    private float delay = 3;
    public int index = 0;
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        Color[] colors = new Color[]
        {
            Color.red,
            Color.green,
            Color.blue,
            Color.yellow
        };

        // Convert to Vector4[]
        Vector4[] colorVectors = colors.Select(c => (Vector4)c).ToArray();

        m_renderer.material.SetVectorArray("_MyColorArray", colorVectors);
        m_renderer.material.SetInteger("_GlobalSpectralIndex", 0);

        Color[] colors2 = new Color[]
        {
            Color.black,
            Color.black,
            Color.black,
            Color.blue
        };

        // Convert to Vector4[]
        Vector4[] colorVectors2 = colors2.Select(c => (Vector4)c).ToArray();

        m_renderer2.material.SetVectorArray("_MyColorArray", colorVectors2);
        m_renderer2.material.SetInteger("_GlobalSpectralIndex", 0);

    }

    // Update is called once per frame
    void Update()
    {
        delay -= Time.deltaTime;
        if (delay < 0)
        {
            m_renderer.material.SetInteger("_GlobalSpectralIndex", index);
            m_renderer2.material.SetInteger("_GlobalSpectralIndex", index);

            index++;
            if (index > 3)
                index = 0;
            delay = 3;
        }
    }
}
#

To be fair, _GlobalSpectralIndex should be global, but nothing worked so I tried this.

Thanks for your help !