Float Array Shader Problem

Hello,

I’m having trouble with passing in some floats to my shader. Here’s a snippet of what and where my float declarations are:

...
Pass{
  CGPROGRAM
  #pragma vertex vert
  #pragma fragment frag
  #include "UnityCG.cginc"
  //we include "UnityCG.cginc" to use the appdata_img struct

float sampleStrength;
float sampleDist;
float samples[10];
...

My understanding is that I can use the

  mat.SetFloat("sampleDist", 1.0f);
  mat.SetFloat("sampleStrength", 2.2f);

It’s also my understanding that the array unrolls into samples0-9. For example:

  mat.SetFloat("samples0", -0.08f);
  mat.SetFloat("samples1", -0.05f);
...

It appears that I’m not getting the values I’m setting and what’s even more strange is that if I re-order my declaration, like:

float samples[10]; // moved from bottom to top
float sampleStrength;
float sampleDist;

I get different outcomes.

I’m now wondering if there’s alignment issues I don’t understand, like 16 byte boundaries or something. Anyone see this issue? If I do not pass in the values, but declare and set them in my fragment shader, everything works. So, I know the values are correct. I’m just trying to figure out what I could be doing wrong trying to pass them in.

Thanks
Jeff.

You can only use SetFloat ( and all the others Set and Get ) to properties declared outside the CG program, in the Properties section, for example

Shader "TEST" {

Properties {
    _Speed ("Speed", Range (0.0,5.0)) = 0.0
}

SubShader {
    Tags { tags.. }

    Pass {
        CGPROGRAM
            #pragma vertex vert

            #include "UnityCG.cginc"

             float _Speed; // Unity will map this one to the one you declared in properties
   }
}

Here, you could use mat.SetFloat(“_Speed”, 2.0f)
have a look at this, it might be helpful:

EDIT: I just re-read your post and it seems that you ARE getting to set the values into the array, is that correct? In that case I apologise for the misunderstanding. ( and as far as I knew you could only use set and get with the properties declared in the Properties section )