← Pixel Dreamer Studio

2D particles from a sprite's shape

One emission point per opaque pixel, measured from the middle of the image.

Set the process material's emission_shape to Points and give it an emission_point_texture: a float texture where each pixel holds one starting position. The editor's Load Emission Mask builds one from an image. The script below does it in code, one point for every pixel that is not see-through. Measure the points from the middle of the image: in our test, points measured from the top-left corner put the shape 78 to 80 pixels right of and below the node.

Two faint grey rings with small blue particles. On the left the blue ring sits down and to the right of the grey one. On the right it lies on the grey ring
The script below in Godot 4.7 with the Compatibility renderer. The faint grey ring is the mask, drawn by a Sprite2D centered on the same node. Left: points measured from the top-left corner of the image. Right: the script as written.

The code

Attach this to a GPUParticles2D and set mask to your sprite: a logo, a letter, a character. Without a mask it draws a ring, so it runs as it is. The line that subtracts half is the fix.

extends GPUParticles2D
## 2D sparkles that come out of a sprite's shape: the editor's Load Emission Mask, in code.
## Set mask to your sprite. Without one, a ring is drawn in code so the script runs as it is.

@export var mask: Texture2D


func _ready() -> void:
	if mask == null:
		mask = make_ring()
	amount = 400
	lifetime = 1.5
	texture = make_dot()
	var points := mask_points(mask.get_image())
	var m := ParticleProcessMaterial.new()
	m.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_POINTS
	m.emission_point_texture = points
	m.emission_point_count = points.get_width()
	m.direction = Vector3(0, -1, 0)
	m.spread = 20.0
	m.initial_velocity_min = 5.0
	m.initial_velocity_max = 15.0
	m.gravity = Vector3.ZERO
	process_material = m


func mask_points(img: Image) -> ImageTexture:
	# one point for every pixel that is not see-through, in a 1-pixel-tall float texture
	var half := Vector2(img.get_width(), img.get_height()) / 2.0
	var list: Array[Vector2] = []
	for y in img.get_height():
		for x in img.get_width():
			if img.get_pixel(x, y).a > 0.5:
				list.append(Vector2(x, y) - half)  # the fix: from the middle, like a centered Sprite2D
	var out := Image.create(list.size(), 1, false, Image.FORMAT_RGF)
	for i in list.size():
		out.set_pixel(i, 0, Color(list[i].x, list[i].y, 0.0))
	return ImageTexture.create_from_image(out)


func make_ring() -> Texture2D:
	var img := Image.create(160, 160, false, Image.FORMAT_RGBA8)
	for y in 160:
		for x in 160:
			var d := Vector2(x - 79.5, y - 79.5).length()
			if d >= 60.0 and d <= 78.0:
				img.set_pixel(x, y, Color.WHITE)
	return ImageTexture.create_from_image(img)


func make_dot() -> Texture2D:
	var img := Image.create(4, 4, false, Image.FORMAT_RGBA8)
	img.fill(Color(0.6, 0.9, 1.0))
	return ImageTexture.create_from_image(img)

What we measured

We stopped the particles from moving and counted the blue points every second pixel by their distance from the node, averaged over four frames. Two runs of each.

Why from the middle

Emission points are positions from the node, and a Sprite2D is centered on its node by default. Pixel positions start at the top-left corner of the image, so subtract half the image size and the particles line up with the sprite, as on the right of the picture.

What to change

A ring of particles on the ground in 3D: particle ring emission. Particles from the surface of a 3D model: particles from a mesh surface.

Tested in Godot 4.7 with the Compatibility renderer.

More how-tos: Auras, rings, beams and weather