Animated Sprites

1 minute read

In the Texture2D folder, some images contain multiple sprites used in animations, for example this one :

explosion sprites

This is difficult to use in Blender, so I wrote a small Python script to separate the individual sprites from the full image :

import numpy as np
from PIL import Image
import os

folder = r"Assets\Texture2D"
file = r"Explosion_Flash.png"

nh = 4    # number of images in the Height direction
nw = 4    # number of images in the Width direction

imgarray = Image.open(rf"{folder}/{file}")
imgarray = np.array(imgarray)

h = int(imgarray.shape[0]/nh)
w = int(imgarray.shape[1]/nw)

# Create export folder if it does not exist
folder_export = rf"{folder}\{file[:-4]}"

if not os.path.exists(folder_export):
    os.makedirs(folder_export)

# Export
l = 0 # image counter

for i in range(nh):
    for j in range(nw):
        img = imgarray[i*h:(i+1)*h,j*w:(j+1)*w,:]

        imgname = rf"{folder_export}/{file[:-4]}_{l}.png"
        img = Image.fromarray(img)
        img.save(imgname)
        
        l += 1

I used this technique for blood, muzzle flashes, explosions, etc. by importing them as mesh planes, and using an image sequence as a texture with the following material setup :

animated sprite setup

These sprites do not always have an alpha channel, they are often on a black background. In that case I used the color channel as a mix factor to change between a transparent BSDF and the colored sprite itself.

The sprites can be animated in two ways depending on your needs :

To hide the sprites, the easiest way is to send them 1000 m below the map.