← Pixel Dreamer Studio

Underwater bubbles that stop at the water surface

One collision box on the water, one line in the particle material.

Set the bubbles' ParticleProcessMaterial collision mode to Hide On Contact, then put a GPUParticlesCollisionBox3D above the water with its bottom face on the water surface. A bubble that rises into the box is hidden there. Particles with collision off, like fire or smoke above the water, pass through the box as if it was not there. This needs GPUParticles3D: CPUParticles3D ignore collision boxes.

Side view of dark blue water with two orange capsule divers. Bubbles from the left diver stop at the surface line, bubbles from the right diver keep rising into the air above it
The two scripts below in Godot 4.7. Left: the bubbles as written. Right: the same bubbles with collision mode set back to Disabled.

The bubbles

Add a GPUParticles3D where the bubbles come out, a diver's mouth or a vent on the sea floor, and attach this script. The line that matters is collision_mode. The rest is how the bubbles look and move.

extends GPUParticles3D
## Air bubbles that rise and vanish at the water surface.
## They need a GPUParticlesCollisionBox3D above the water: water_surface.gd below.

@export var max_depth := 6.0  # the deepest point you use this at, in meters
@export var rise_speed := Vector2(1.0, 1.8)  # slowest and fastest bubble, m/s
@export var size := 0.16
@export var color := Color(0.75, 0.9, 1.0)


func _ready() -> void:
	amount = 24
	local_coords = false
	# Long enough for the slowest bubble to reach the surface from max_depth.
	lifetime = max_depth / rise_speed.x + 0.5
	var m := ParticleProcessMaterial.new()
	m.emission_shape = ParticleProcessMaterial.EMISSION_SHAPE_SPHERE
	m.emission_sphere_radius = 0.1
	m.direction = Vector3.UP
	m.spread = 10.0
	m.initial_velocity_min = rise_speed.x
	m.initial_velocity_max = rise_speed.y
	m.gravity = Vector3.ZERO
	m.scale_min = 0.5
	m.scale_max = 1.0
	# The line that does the job: a particle that touches a collision shape is hidden.
	m.collision_mode = ParticleProcessMaterial.COLLISION_HIDE_ON_CONTACT
	process_material = m
	collision_base_size = size
	visibility_aabb = AABB(Vector3(-2, -1, -2), Vector3(4, max_depth + 3, 4))
	draw_pass_1 = _bubble()


func _bubble() -> QuadMesh:
	var g := Gradient.new()  # a thin bright ring with a faint middle
	g.offsets = PackedFloat32Array([0.0, 0.7, 0.85, 1.0])
	g.colors = PackedColorArray([Color(1, 1, 1, 0.15), Color(1, 1, 1, 0.3), Color(1, 1, 1, 1), Color(1, 1, 1, 0)])
	var t := GradientTexture2D.new()
	t.gradient = g
	t.fill = GradientTexture2D.FILL_RADIAL
	t.fill_from = Vector2(0.5, 0.5)
	t.fill_to = Vector2(1.0, 0.5)
	var mat := StandardMaterial3D.new()
	mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
	mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
	mat.billboard_mode = BaseMaterial3D.BILLBOARD_PARTICLES
	mat.albedo_texture = t
	mat.albedo_color = color
	var q := QuadMesh.new()
	q.size = Vector2(size, size)
	q.material = mat
	return q

Seen from the side, 10 meters away, with bubbles let out 2.5 meters under the water: 0 bubble pixels above the surface with the box below in the scene. With collision mode set back to Disabled, 27 pixels above the surface on average, and bubbles as high as 1.42 meters over the water.

The water surface

Add a GPUParticlesCollisionBox3D to your water scene and attach this script. You need one for each body of water, not one per bubble emitter. Set surface_y to the height of the water and water_size to how much of x and z the water covers.

extends GPUParticlesCollisionBox3D
## One per body of water. The bottom face of this box sits on the water surface,
## so particles with collision on (the bubbles above) vanish there.

@export var surface_y := 0.0  # world height of the water surface
@export var water_size := Vector2(40, 40)  # how much of x and z the water covers
@export var height := 20.0  # how far above the water the box reaches


func _ready() -> void:
	set_surface(surface_y)


func set_surface(y: float) -> void:  # call it again when the water level changes
	surface_y = y
	size = Vector3(water_size.x, height, water_size.y)
	global_position.y = y + height * 0.5

The box goes from the surface up to height meters above it. In the test its bottom face read 0.0, the height of the water.

Why collision_base_size

Godot's default collision_base_size is 0.01 meters, so a bubble was hidden when its middle reached the water. With that, in two runs of 30 frames, a few bubble pixels still showed above the surface, as high as 0.11 and 0.14 meters. With collision_base_size set to the bubble size, 0.16, there were 0 pixels above the surface in both runs.

Four things that look like bugs

What to change

Where something falls into the water: a water splash on the surface. Particles that fall through the floor instead: particles fall through the floor. Bubbles seen through a see-through water surface: particles in front of and behind water or glass.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Underwater Action VFX for Godot 4 has Diver Bubbles that wobble up, Dive Splash, Air Bubble Pickup, Treasure Chest Bubbles and Hydrothermal Vent Bubbles. Loops are turned on and off with emitting.

More how-tos: Water