- Home /
I have read up on having arrays in shaders but I just can't get it to work, the shader code is here:
Shader "Custom/CardAssembler" {
Properties
{
_MainTex ("", 2D) = "white" {}
_BaseTex ("", 2D) = "white" {}
_Portrait ("", 2D) = "white" {}
_StartHealth ("", 2D) = "white" {}
_Otherhealth ("", 2D) = "white" {}
_Red ("Red", 2D) = "white" {}
_Green ("Green", 2D) = "white" {}
_Black ("Black", 2D) = "white" {}
_Blue ("Blue", 2D) = "white" {}
_Gray ("Gray", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_healthLevels ("levels", float) = 0.0
}
SubShader
{
ZTest Always Cull Off ZWrite Off Fog { Mode Off } //Rendering settings
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma target 3.0
#pragma fragment frag
#include "UnityCG.cginc"
//we include "UnityCG.cginc" to use the appdata_img struct
struct v2f {
float4 pos : POSITION;
half2 uv : TEXCOORD0;
};
//Our Vertex Shader
v2f vert (appdata_img v){
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = MultiplyUV (UNITY_MATRIX_TEXTURE0, v.texcoord.xy);
return o;
}
sampler2D _MainTex; //Reference in Pass is necessary to let us use this variable in shaders
sampler2D _BaseTex;
sampler2D _Portrait;
sampler2D _StartHealth;
sampler2D _Otherhealth;
sampler2D _Red;
sampler2D _Green;
sampler2D _Blue;
sampler2D _Black;
sampler2D _Gray;
float healtha[20] = {2,2,2,2,2,2,2,2,2,2,22,2,2,2,2,2,2,2,2,2};
fixed4 _Color; //Reference in Pass is necessary to let us use this variable in shaders
float _healthLevels;
fixed4 BlendColor(fixed4 oldCol ,fixed4 newCol)
{
fixed4 Col;
if (oldCol.a > 0)
Col = fixed4(newCol.x * newCol.a + oldCol.x * (1.0f - newCol.a), newCol.y * newCol.a + oldCol.y * (1.0f - newCol.a), newCol.z * newCol.a + oldCol.z * (1.0f - newCol.a), oldCol.a / newCol.a);
else
Col = newCol;
return(Col);
}
//Our Fragment Shader
fixed4 frag (v2f i) : COLOR
{
fixed4 orgCol = tex2D(_Portrait, fixed2(i.uv.x, - 0.33333 + i.uv.y / 0.75)); //Get the orginal rendered color
orgCol = BlendColor(orgCol, tex2D(_BaseTex, i.uv)); //Get the orginal rendered color
// float alp = avgNoise(i);l
//alp = min(1.0f, max(0.0f, alp));
//fixed4 col = fixed4(orgCol.r * alp + _Color.r * (1.0f - alp), orgCol.g * alp + _Color.g * (1.0f - alp), orgCol.b * alp + _Color.b * (1.0f - alp), 1);
//col = fixed4(alp, alp, alp, 1);
float gap = 0.08f;
if (i.uv.y >= 0.9166666666666667)
[unroll(20)]
for (float j = 0.0f; j < _healthLevels; j++)
{
if (i.uv.x > j * gap && i.uv.x < (j) * gap + 0.09375)
{
fixed4 colo;
if (j == 0.0f)
colo = tex2D(_StartHealth, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
else
colo = tex2D(_Otherhealth, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
if (colo.a == 1.0f)
orgCol = BlendColor(orgCol, colo);
if (healtha[j] == 0)
colo = tex2D(_Red, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
else
if (healtha[j] == 1)
colo = tex2D(_Blue, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
else
if (healtha[j] == 2)
colo = tex2D(_Green, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
else
if (healtha[j] == 3)
colo = tex2D(_Black, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
else
colo = tex2D(_Gray, (i.uv - fixed2(j * gap, 0.9166666666666667f)) * fixed2(10.666f, 12));
//if (colo.a == 1.0f)
orgCol = BlendColor(orgCol, colo);
}
}
return orgCol;
}
ENDCG
}
}
FallBack "Diffuse"
}
Now, the major problem is that when I try to access it, I can't... As you can see in the shader I test if the value of certain array elements is 0, 1, 2, 3 or greater. They always turn out as 0, even though I fill up the array with the value of 2.
Furthermore I cannot see them either through GetFloat or HasProperty.
Comment
Your answer

Follow this Question
Related Questions
Possible shader interference when updating matrix 1 Answer
Using Array of Structs in Shader 1 Answer
How to use array in cg shader 1 Answer
Add shaders into Array based on Material Array 0 Answers
GLSL shader - array of uniform const 0 Answers