- Home /
How to pass an array to a shader?
Is there a way to pass an array of floats to a shader?
Answer by valyard · Jun 02, 2015 at 03:27 PM
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.
Hi, Why after I SetFloat separately , like material.SetFloat("myarray0", 1);
the value in the shader still zero? However, GetFloat("myarray0") = 1.
Your answer
Follow this Question
Related Questions
GLSL shader - array of uniform const 0 Answers
Setting and storing vertex locations in a shader 0 Answers
Texture2D to Texture3D 2 Answers
I have read up on having arrays in shaders but I just can't get it to work, the shader code is here: 0 Answers
Can I get the a list of the properties in a shader at runtime? 1 Answer