I generate a dynamic sprite sheet consisting of images layered on top of each other (head, body, overlays, weapon, etc.). This allows the players to change their hats for instance at runtime, i.e.switch head layer.
Each image is 2048px² in dimension and is about 150kb in size. Since the order of layers is different for the front and back perspective of the sprite (e.g. weapon behind vs in front of body) I need two textures per sprite.
Given a test case of refreshing of 6 layers of images per sprite makes 12 images (2048px²) totalling 1800kb. In game, this could be a player switching to a secondary weapon. This happens in plain view.
The problem is this operation takes up to 1.5 seconds. Is this as more or less expected or is my use of the canvas texture flawed?
//non-production test code
_test() {
console.time(1);
//layer order
const TEMP_BACK = ["back", "weapon", "head"];//etc.
const TEMP_ELSE = ["back", "body", "vest"]; //etc.
const compoundTextureFront = this.__frontText;
const compoundTextureBack = this.__backText;
const f = this.$engine.anims.getFrameName();
compoundTextureFront.clear();
compoundTextureBack.clear();
//to be changed
const spriteIds = {
weapon:'1',
vest: '2',
head: '3',
back: '4',
front: '5',
body:'6'
};
TEMP_ELSE.forEach((slot)=>{
if(spriteIds[slot] !== undefined) {
compoundTextureFront.draw(0,0, $scene.textures.get(spriteIds[slot]).getSourceImage());
}
});
compoundTextureFront.refresh();
TEMP_BACK.forEach((slot)=>{
if(spriteIds[slot] !== undefined) {
compoundTextureBack.draw(0,0, $scene.textures.get(spriteIds[slot]).getSourceImage());
}
});
compoundTextureBack.refresh();
this.$engine.setTexture("new_texture", f);
console.timeEnd(1);
}