How can I clear a texture of all set pixels?
I'm repeatedly creating a texture3D and passing it to the shader. It seems to remember the old pixels after each pass, despite me creating a new texture3D each time. I even tried passing null to the shader in between passes, but somehow it still remembers old pixels.
Here are the details:
I've got a 3D voxel game like minecraft and I'm using a texture3D to communicate to the shader which voxels should be hidden (so as not to obscure the player) I'm creating a texture3D like this:
hide_voxels():
var width = 128
var height = 128
var depth = 128
var texture3D = Texture3D.new()
texture3D.create(width, height, depth, Image.FORMAT_RGB8)
var image = Image.new()
# Setting one layer creates and initializes every layer in the stack of images.
image.create(width, depth, false, Image.FORMAT_RGB8)
texture3D.set_layer_data(image, 0)
x = 1
y = 54
z = 2
image = texture3D.get_layer_data(y)
image.lock()
image.set_pixel(x, z, Color(1.0, 1.0, 1.0, 1.0))
image.unlock()
texture3D.set_layer_data(image, y)
x = 2
y = 54
z = 2
image = texture3D.get_layer_data(y)
image.lock()
image.set_pixel(x, z, Color(1.0, 1.0, 1.0, 1.0))
image.unlock()
texture3D.set_layer_data(image, y)
x = player_position.x
y = 54
z = player_position.z
image = texture3D.get_layer_data(y)
image.lock()
image.set_pixel(x, z, Color(1.0, 1.0, 1.0, 1.0))
image.unlock()
texture3D.set_layer_data(image, y)
material.set_shader_param("blocks_to_hide", texture3D)
Every pixel in the texture with an alpha of 1.0 marks a voxel that should be hidden by the shader.
I have it set up so that when I click a button it calls hide_voxels and hides the first two voxels at (1,2) and (2,2) and also hides whatever voxel the player is standing on. I move the player between clicks:
click 1: all three voxels hidden fine
click 2: all three voxels hidden fine. The previous voxel the player had hidden is unhidden. All good.
click 3: all three voxels hidden fine. The previous voxel the player had hidden is unhidden. All good, except the first voxel that was hidden in click 1 becomes hidden again! So now we have four hidden voxels!
Somehow the texture is remembering the pixel that I set in click 1.
I can "clear" the texture by standing still and clicking 7 times. Each click toggles the voxel that should not have been hidden until finally its unhidden permanently.
I can't wait for godot 4 so I can have real arrays!