【发布时间】:2021-08-01 09:32:27
【问题描述】:
我需要将整数数组作为统一传递给着色器。由于尚不支持统一数组,因此我使用FORMAT_R8 texture/isampler2D 来实现此目的。
如果我从纹理中正确读取字节,它们应该在 [0..7] 范围内,然后通过规范化该值,我应该得到不同的红色阴影,但是这不会发生。
在屏幕截图中,您可以看到当值被读取为 0 时,它正确显示为黑色,但当它不是 0 时,它是全红色,没有阴影。我尝试比较读取的值,它总是大于我可以尝试的任何值,最大为 32 位有符号整数的最大值,所以不知道我从纹理中读回的值是什么。
有什么想法吗?
完整的项目在这里: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_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
}
【问题讨论】:
标签: shader fragment-shader godot gdscript godot-shader-language