#issue with float4[] indexing in URP shadergraph

1 messages · Page 1 of 1 (latest)

drifting vessel
#

trying to make a shader that indexes from a table:

#ifndef GETLAYERCOLOR_INCLUDE
#define GETLAYERCOLOR_INCLUDE

uniform float4 layerColors[10] = {
    float4(1, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0),
    float4(0, 0, 0, 0)
};

void GetLayerColor_float(float2 UV, out float4 LayerColor)
{
    LayerColor = layerColors[0];
}

#endif```
`layerColors[0]` makes the output float4 black (all zeroes) but doing `LayerColor = float4(1, 0, 0, 0)` outputs correctly; how come layerColors[0] isn't returning the element at index 0?
wispy dust
#

This is not very well documented, but you can't initialize uniforms like that. They're supposed to be set from the CPU side.

#

What you can have is a static const array instead:

static const float4 layerColors[10] = { ... };

This is baked into the binary at compile time.

drifting vessel
#

ah ok

#

i was trying to give it an initializer for testing and then make it a global later