← Pixel Dreamer Studio

Sprite sheet frames without the empty cells

Cut the sheet into cells in reading order and leave out every cell where nothing is drawn.

A sprite sheet is a grid, and the last row is often not full: 45 frames on an 8 by 6 sheet leaves 3 empty cells. Cut every cell into the animation and those empty cells become frames where the effect disappears. In our Godot 4.7 test at 24 fps that was 0.125 s of nothing at the end of every loop, and a missing frame in the middle of a sheet made it blink halfway through. The script below builds the SpriteFrames from the grid and leaves out any cell where every pixel is transparent: 45 frames and 44 frames, no empty one.

The code

Save it as sheet_frames.gd and load it with const SheetFrames := preload("res://sheet_frames.gd"). Give it the sheet, the number of columns and rows, the speed and whether it loops, and set the result on your AnimatedSprite2D: $Effect.sprite_frames = SheetFrames.make(sheet, 8, 6, 24.0, false), then $Effect.play().

extends RefCounted
## Build SpriteFrames from a sprite sheet grid, in reading order, and skip the empty cells
## (a sheet of 8 x 6 cells with 45 frames has 3 empty ones at the end).
##   const SheetFrames := preload("res://sheet_frames.gd")
##   $Effect.sprite_frames = SheetFrames.make(preload("res://fx/explosion_sheet.png"), 8, 6, 24.0, false)
##   $Effect.play()


static func make(sheet: Texture2D, columns: int, rows: int, fps := 24.0, loop := false, anim := &"default") -> SpriteFrames:
	var frames := SpriteFrames.new()
	if not frames.has_animation(anim):
		frames.add_animation(anim)
	frames.set_animation_speed(anim, fps)
	frames.set_animation_loop(anim, loop)
	var img := sheet.get_image()
	if img.is_compressed():
		img.decompress()
	var cw := sheet.get_width() / columns
	var ch := sheet.get_height() / rows
	for r in rows:
		for c in columns:
			var rect := Rect2i(c * cw, r * ch, cw, ch)
			if img.get_region(rect).is_invisible():
				continue  # nothing drawn in this cell
			var cell := AtlasTexture.new()
			cell.atlas = sheet
			cell.region = Rect2(rect)
			frames.add_frame(anim, cell)
	return frames

What we measured

A sheet of 8 by 6 cells, 64 px each, with a square drawn in 45 of them, made in code so we knew which cells were empty. We counted the frames each way and checked each one for anything drawn, then played the result on a real AnimatedSprite2D. Two runs gave the same numbers.

Why it happens

A grid has columns times rows cells, but an effect has as many frames as it has, and the last row is filled from the left. Cutting by the grid adds the leftover cells as real frames, so the animation plays them too, fully transparent. Image.is_invisible() tells you when every pixel of a cell has zero alpha, so those cells can be skipped, wherever they are.

What to change

Ready-made 2D effects: HD VFX Sprite Sheets Vol 1 has 106 animated effects as transparent PNG sprite sheets for 2D engines, Godot 2D included.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: HD VFX Sprite Sheets Vol 1 has 106 animated effects as transparent PNG sprite sheets for 2D engines, Godot 2D included.

More how-tos for HD VFX Sprite Sheets Vol 1: GPUParticles2D not showing in Godot 4 · Transparent PNGs without a dark outline

More how-tos: Play, stop and remove effects