![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Ilnaz |
Good day, dear colleagues.
Please help me with solving one trivial problem.
There is a two-dimensional array of a certain size, filled randomly with zeros and ones. Assume that the size of the array is 1280 by 720 cells. And the screen/window size is the same.
It is required to draw each cell (number) from this array in a certain color (0 - white, 1 - black).
I know how to do this using a loop and drawRect; I know that it is possible to set each pixel of an Image in a loop, put an Image in a texture and draw - but these solutions are slow.
Therefore, I would like to find out how this can be done using a shader.
I understand the basic principles of shaders. However, I could not figure out how to get the coordinates of the current pixel of the screen in the shader program and get the value of the corresponding cell from the array from the position of this pixel (index the array) - this is the only moment that is incomprehensible to me.
I will be very grateful for your help.
What format is the array in? From my testing, it seems converting a 2D Array of integers to a 1D PackedByteArray, then calling Image.create_from_data(WIDTH, HEIGHT, false, Image.FORMAT_L8, byte_array)
is reasonably fast. Getting the current pixel coordinate in the shader depends on whether it’s a CanvasItem or a Spatial shader.
With a full screen ColorRect node with a CanvasItem shader, the code was simply
shader_type canvas_item;
uniform sampler2D tex;
void fragment() {
COLOR = texture(tex, UV)*255.f;
}
a_world_of_madness | 2023-06-12 09:31