can't get viewport pixel color

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By findaeden

Hello. I’ve been trying to create a stereo audio effect based on how many red pixels there are on the screen, with a bias towards the left pixels for the left speaker, and the right pixels for the right speaker. For that, I need to get the red values of each pixel on screen, but I can’t figure out how to do so. apparently I need to use lock() after get_data(), but adding it crashes the game and gives the error “Invalid call. Nonexistent function ‘get_pixel’ in base ‘Nil’.” this is the faulty code in question.

leftamount = (get_viewport().get_texture().get_data().get_pixel(pixelcur.x, pixelcur.y).r) * effectleft
rightamount = (get_viewport().get_texture().get_data().get_pixel(pixelcur.x, pixelcur.y).r) * effectright
:bust_in_silhouette: Reply From: GrayScienceCEO

doing get_image on the viewport texture returns the image with the function “get_pixel(x,y)”
Replacing get_data with get_image should work but it will lag if it is overused
Note on the viewport texture
“Note: This will fetch the texture data from the GPU, which might cause performance problems when overused.”

and another thing you should do to prevent black textures or past textures is wait until
the texture fully loads
“Note: When trying to store the current texture (e.g. in a file), it might be completely black or outdated if used too early, especially when used in e.g. Node._ready(). To make sure the texture you get is correct, you can await RenderingServer.frame_post_draw signal.”

put await RenderingServer.frame_post_draw
before you get the viewport texture to let it load fully, then you can grab the full version of it

this question is over 1 year old. the game came out in april

findaeden | 2022-12-25 21:59

I just found your answer 1st on google search, so I answered it incase anyone else was trying to do the same thing

GrayScienceCEO | 2022-12-25 22:03