Values in arrays is always 0

I was porting some shaders from RenderMonkey over to Unity and ran into several problems relating to arrays. Here is a test case for one. The values returned from the array are always 0. I tested this in RenderMonkey and changing the values of the colors array changes the rendered output as I would expect. In Unity I only get black.

Shader "Filter/Depth Of Field" {
	Properties {
	}
	
	SubShader {
		Pass {		
			CGPROGRAM
	    	#pragma vertex vert
		    #pragma fragment frag
						
				const float colors[4] = {
					0, 1, 0, 1
				};

				struct vertStruct {
					float4 vertex : POSITION;
					float2 texCoord : TEXCOORD0;
				};
				
				vertStruct vert(vertStruct vertData) {
					vertStruct output;

					output.vertex = mul(glstate.matrix.mvp, vertData.vertex);
					output.texCoord = vertData.texCoord;
				
					return output;
				}
			
				float4 frag(float2 texCoord : TEXCOORD0) : COLOR {
					return float4(colors[0], colors[1], colors[2], colors[3]);
				}

	    ENDCG
		}
	} 
	FallBack "Diffuse", 1
}

Initial variable values will not actually work. They have to be assigned through material’s properties, or set from the script.

Also, array variables won’t really work either.

Are these Cg limitations, or Unity implementation limitations?

Unity’s implementation.

I was doing the exact same thing as dkoontz, can you suggest a way to define and apply kernels to use them in pixel shaders? Any built in shader that does this that I can refer to? Are there plans to upgrade Unity’s implementation to make arrays work? any other limitations on Unity’s implementation that we should be aware of?

I’m finally getting serious about learning to write my own shaders and it’ll save me a lot of time to know these things beforehand.

I just use float4 and float4x4 and then set them using Material.SetVector() and Material.SetMatrix().

Thanks Daniel I’m soo new at this didn’t think of using those.

Yikes, really? The thing fails silently, no warnings, no errors? (I only found this thread after bonking my head on the desk for several hours…) And you can’t use consts, and arrays won’t work, really? you can’t sample a texture in a vertex shader? Immensely frustrating. Is there any way to get arbitrary data into a vertex shader?

I believe Aras added texture sampling in vertex shaders to Unity 3.

That’s a good idea, meh11, but it’s still not big enough when you consider that in a vertex program you’ll only have access to the structure of the single, current vertex… so storing extra info in the vertex’s uv2, color, or whatever, can’t really encode more data than a shader parameter.

Anyway this would be a great idea for vertex-shader-based keyframe interpolation!