Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way for declaring a Unity shader array

Hope a simple question. This is a simple camera-effect shader. This works. However, having the _ColorSet01 array INSIDE the frag function means the array gets initialized for every pixel ¿Am I wrong?. Believing that I've moved it OUTSIDE the function, right below the _MainTex definition. By doing this, every item in the array lost their value and became float4(0,0,0,0) when queried inside the function.

I'm obviously handling this array the wrong way. Any help would be appreciated.

Btw this is Unity 2018.3.10 if it helps.

sampler2D _MainTex;

float4 frag(v2f_img input) : COLOR {
	float4 base = tex2D(_MainTex, input.uv);
	float4 _ColorSet01[6] =
	{
		float4(1,0,0,1),
		float4(1,1,0,1),
		float4(1,0,0,1),
		float4(1,0,1,1),
		float4(1,1,0,1),
		float4(1,0,0,1)
	};
	if (base.a > 0)
	{
		base.rgb = _ColorSet01[1].rgb;
	}
	return base;
}
like image 478
NestorArturo Avatar asked Oct 27 '25 08:10

NestorArturo


1 Answers

Really sorry… found the answer just 5 minutes after posting the question. This is the way of declaring the array outside the function and preserving the values:

static const float4 _ColorSet01[6] = {
float4(1,0,0,1),
float4(1,1,0,1),
float4(1,0,0,1),
float4(1,0,1,1),
float4(1,1,0,1),
float4(1,0,0,1)
};

"static" and "const" did the trick for me.

like image 114
NestorArturo Avatar answered Oct 29 '25 05:10

NestorArturo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!