Godot Version
4.3
Question
Hey all, I am storing some data in a PackedColorArray. I convert this data to a texture, and I set the global shader parameter using that texture. I then use that in a shader.
Currently, I have simplified things so my PackedColorArray only holds one value (and thus my texture is only one pixel). The value in my PackedColorArray is [{0, 0, 0, 2}]. I have verified this using the debugger.
After I set the value of the PackedColorArray, I then set/update the global shader parameter like so:
func update_global_texture () -> void:
var temp_byte_array: PackedByteArray = _cell_texture_data.to_byte_array()
_cell_texture_source_image.set_data(x, z, false, Image.FORMAT_RGBAF, temp_byte_array)
_cell_texture.update(_cell_texture_source_image)
RenderingServer.global_shader_parameter_set("_HexCellData", _cell_texture)
After setting the “_HexCellData” shader parameter, I then use it in a shader. Right now I am just setting the albedo color in the shader based on what value is passed in:
shader_type spatial;
uniform sampler2D _HexCellData: source_color;
void vertex()
{
//empty
}
void fragment()
{
vec4 data = texture(_HexCellData, vec2(0, 0));
//vec4 data = textureLod(_HexCellData, vec2(0, 0), 0.0); //This gets the same result
float t = data[3];
vec4 c = vec4(0.0, 0.0, 0.0, 1.0);
if (t >= 4.0)
{
c = vec4(1, 1, 1, 1);
}
else if (t >= 3.0)
{
c = vec4(0.59, 0.42, 0.22, 1);
}
else if (t >= 2.0)
{
c = vec4(0.71, 0.65, 0.53, 1);
}
else if (t > 1.0)
{
c = vec4(0, 1, 0, 1);
}
else if (t == 1.0)
{
c = vec4(0, 0.5, 0, 1);
}
else if (t >= 0.0)
{
c = vec4(1, 1, 0, 1);
}
else
{
c = vec4(1, 0, 1, 0);
}
ALBEDO = c.rgb;
ALPHA = c.a;
}
The value of “c” in the shader is not what I am expecting.
Since the value of the PackedColorArray is [{0, 0, 0, 2}], this means that the value of the byte array (when I call “to_byte_array”) is [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64] (this is the 32-bit floating point representation of the number 2.0).
Then, I pass in that byte array to the texture, and I use the format RGBAF. I would expect the value to still be 2.0 in the shader.
When we get to the shader code, the value is always 1.0 - no matter what I set the value of the PackedColorArray to be. I’ve tried setting the value in the PackedColorArray to be other values, but the resulting value in the shader is always 1.0.
This is extremely frustrating. Any suggestions?
Thanks!