Is there a way to pass an array of floats to a shader?
Starting with Unity 5.4, there is native “arrays in shaders” support. Material.SetFloatArray, Shader.SetGlobalFloatArray, MaterialPropertyBlock.SetFloatArray
Currently that is possible, but cumbersome. E.g. in the shader, you can do float myarray[100], and then that becomes individual float material properties (myarray0, myarray1, …, myarray99) that can be set from scripting. So cumbersome + somewhat high overhead of applying them one by one internally.
For example, take this shader code
Shader "Custom/ArrayTest" {
Properties {
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
float myarray[3];
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutputStandard o) {
o.Albedo = float3(myarray[0], myarray[1], myarray[2]);
}
ENDCG
}
FallBack "Diffuse"
}
And a script to test it. Must be placed on the same object.
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
void Awake() {
var material = GetComponent<Renderer>().sharedMaterial;
material.SetFloat("myarray0", 1);
material.SetFloat("myarray1", 1);
material.SetFloat("myarray2", 0);
}
}
Hope in the future Unity will add proper arrays support.