← Pixel Dreamer Studio

Parallax2D background with no gaps

repeat_size = the texture width. repeat_times = twice the screen width divided by the texture width.

Set repeat_size to exactly the texture's width and raise repeat_times when the texture is narrower than the screen. In our Godot 4.7 test with a 320 px layer on a 960 px screen, the default repeat_times of 1 still left up to 510 black pixels on the middle row, 4 left 190, and the script below, which sets 6, left 0 at every camera position we tried.

Left: a black gap between hill tiles. Right: the hills repeat with no gap
Godot 4.7 with the Compatibility renderer, camera at x 1500, a 320 px wide hills texture. Left: repeat_size 320 with repeat_times left at 1. Right: the script (repeat_times 6).

The code

Make one node per layer (sky, far hills, near trees), attach this script and set texture and speed: 0 is a sky that never moves, 1 moves with the camera, above 1 is for things in front of the player. Turn on pixel_art for pixel art layers. If your Camera2D zooms out during play, call fit() after the zoom changes.

extends Parallax2D
## One parallax layer that tiles forever, sized from its own texture.
## Make one node per layer (sky, far hills, near trees) under your level, each with its own speed.

@export var texture: Texture2D
@export_range(0.0, 2.0, 0.05) var speed := 0.5  ## 0 = static sky, 1 = moves with the camera, above 1 = foreground
@export var pixel_art := false


func _ready() -> void:
	var s := Sprite2D.new()
	s.texture = texture
	s.centered = false  # the repeat starts at the texture's left edge
	if pixel_art:
		s.texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST
	add_child(s)
	scroll_scale = Vector2(speed, 1.0)
	repeat_size = Vector2(texture.get_width(), 0.0)  # exactly one texture width, no gap and no overlap
	fit()


## Call again after your Camera2D zooms out.
func fit() -> void:
	var view := get_viewport_rect().size.x
	var cam := get_viewport().get_camera_2d()
	if cam:
		view /= cam.zoom.x  # zoomed out = more of the layer on screen
	repeat_times = 2 * int(ceil(view / texture.get_width()))  # half of this left gaps in our test

What we measured

A 320 x 180 px layer on a 960 x 540 screen at speed 0.5. We counted the black pixels on the middle row, where the layer did not cover the screen, with the camera at x 0, 777, 1500, 5000 and -2000, and kept the worst. Two runs gave the same numbers.

How many copies

The obvious guess, screen width divided by texture width plus one, was 4 here and still left a 190 px gap. The script uses twice the ratio and divides the screen width by the camera zoom, which covered the screen in every case we measured.

What to change

Layers to try it with: Asian Fantasy Parallax Free has 2 scenes (Torii and Sakura Hill, Thai Temple Fields) with 7 tiling layers each, in day, dusk and night, as 320 x 180 pixel art plus HD.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Asian Fantasy Parallax has 12 scenes with 7 tiling layers each, in day, dusk and night, and ready Godot 4 Parallax2D scenes.

More how-tos: Skies and backgrounds