Hi everyone! I've been developing some code to dynamically change the color of a unit's uniform in my game, however, I'm running into an issue. My code works but is really slow. Is there anything that I can do to speed up this process? I was hoping to use some caching to help alleviate it, but this issue still slows down the loading of the battlefield by a good ten seconds, temporarily freezing the game as it does so. Thank you in advance.
func apply_color_mask(main : Image, mask : Image, color : Color):
main.lock();
mask.lock();
color.a = 0.5;
# Check if the pixels used in this mask have been cached. If not,
# create an entry to do so.
var sheetWidth = mask.get_width();
var sheetHeight = mask.get_height();
var currentDict;
# If this mask is not currently cached.
if _pixelLocations.get(hash(mask)) == null:
print("Creating cache!");
# Create the cache.
_pixelLocations[hash(mask)] = [];
currentDict = _pixelLocations.get(hash(mask));
# Locate all used pixels in this mask.
for y in sheetHeight:
for x in sheetWidth:
var currentPixel = mask.get_pixel(x, y);
if currentPixel == Color(1,1,1,1):
# We have a match.
currentDict.append(Vector2(x,y));
else:
currentDict = _pixelLocations.get(hash(mask));
for currentEntry in currentDict:
var grayscalePixel = main.get_pixelv(currentEntry).gray();
grayscalePixel = Color(grayscalePixel, grayscalePixel, grayscalePixel, 1);
main.set_pixelv(currentEntry, grayscalePixel);
mask.set_pixelv(currentEntry, color);
var blitRect = Rect2(0, 0, main.get_width(), main.get_height())
main.blend_rect(mask, blitRect, Vector2(0,0));
mask.unlock();
mask.unlock();
return main;