In order to detect sprite overlap between my player and enemies, I am trying to create a mask of all non-transparent pixels of my sprite.
I have been doing it with the following method, which returns an array of coordinates of non-transparent pixels.
# mask is initially an empty array
var mask = 
# get image data
var image = sprite.texture.get_data()
# lock the image so get_pixel() can be called on it
# add non-transparent pixel coordinates to mask
for x in image.get_width():
for y in image.get_height():
if image.get_pixel(x,y) != 0:
mask.append(global_position + Vector2(x,y))
This works and allows me to do pixel-perfect collision detection between my player and enemies. But it is not efficient. Even though I only call it when my player intersects the hit boxes of my enemies, it is still very inefficient and causes lag.
So, my question: How can I optimize my get_mask() function?
I am open to radically changing the function, so long as the result is a mask of non-transparent pixels that I can use to check for overlap between two sprites.
I would love to be able to generate the mask on the gpu using Godot's shader language, but there's no way to get a shader to output data in a way that can be used in non-shader scripts.