How to define an array of floats in Shader properties?

Viewed 26384

I'd like to define an array of floats in my shader, something like this:

Properties
{
    _TilesX ("Tiles X", Int) = 10
    _TilesY ("Tiles Y", Int) = 10

    _TileData1 ("Tile data", Float[]) = {} // THIS!!!

    _Texture1 ("Texture odd", 2D) = "white" {}
    _Texture2 ("Texture even", 2D) = "white" {}
}

I'm trying to create a single plane that I'll use as a grid and I want to modify the _TileData1 at run-time to change the Y offset of a tile. I'm using _TilesX and _TilesY to get a 2d position of the tile from a 1d array.

Just to be clear, I just want to find out how to define a property type of float[] since I couldn't find how to do so on Unity's manual pages or forums.

2 Answers

You can't use properties for float[]. But you can use them as variables in your shader and set them from script:

In your shader:

CGPROGRAM
int _SegmentsCount = 0;
float _Segments[1000];

void surf (Input IN, inout SurfaceOutput o) {
    for (int i = 0; i < _SegmentsCount; i++) {
        // This is obsviously just an example,
        // avoid loops in shaders if you can help it.
    }
}

ENDCG

Then in your script:

float [] array = new float[] { 0.25f, 0.75f };
material.SetFloatArray("_Segments", array);
material.SetInt("_SegmentsCount", 2);

renderer.material = material;
Related