I need to pass an array of integers to a shader as a uniform. As uniform arrays are not yet supported, I'm using a FORMAT_R8
texture/isampler2D to achieve this.
If I'm correctly reading the bytes from the texture, they should be in the range [0..7] and then by normalizing that value, I should get different shades of red, but that's not happening.
In the screenshot you can see that when the value is read as 0, it's correctly displaying it black, but when it's other than 0, it's full red, no shades. I tried comparing the value read and it's always greater than any value I can try, up to the maximum value of a 32 bit signed integer, so no idea what value I'm reading back from the texture.
Any ideas?
The full project is here: https://github.com/Drean64/clash-of-the-colors/tree/simple
GDScript:
extends ViewportContainer
func _ready():
var attr_ink = PoolByteArray([])
attr_ink.resize(32*24)
for i in range(0, 32*24):
attr_ink[i] = i % 8 # fill array with [0..7]
var image = Image.new()
image.create_from_data(32, 24, false, Image.FORMAT_R8, attr_ink)
var ink = ImageTexture.new()
ink.create_from_image(image, 0)
(self.material as ShaderMaterial).set_shader_param("ink", ink)
Shader:
shader_type canvas_item;
uniform isampler2D ink;
void fragment() {
COLOR = vec4(0., 0., 0., 1.); // Black
ivec2 cell = ivec2(UV / TEXTURE_PIXEL_SIZE) / 8; // 8x8 pixel cell coordinate
int ink_color = texelFetch(ink, cell, 0).r; // Should be [0..7]
COLOR.r = float(ink_color) / 7.; // Should normalize the red value to [0..1]
// Line above is a kind of debug to get a hint at what the ink_color value is
}