← Pixel Dreamer Studio

Scroll or tile a 2D texture without stretched edge lines

2D nodes do not repeat their texture by default. Set Texture > Repeat to Enabled on the node that draws it.

Set the node's texture_repeat to Enabled, in the Inspector under CanvasItem > Texture > Repeat. A new 2D node is set to Inherit and the project default is Disabled, so every UV past 1.0 reads the texture's last column. In our Godot 4.7 test a Sprite2D whose shader added 0.5 to UV.x showed that one color over 136 of its 256 pixels. With repeat enabled the second half showed the start of the texture again. A Sprite2D region 4 times as wide as its texture broke the same way, 194 of 256 pixels, while a TextureRect in Tile mode tiled correctly either way.

The code

Put it on a Sprite2D with a seamless texture and set speed in texture widths per second. It turns repeat on, adds a small scrolling shader and keeps the offset between 0 and 1, so the number never grows: 5 seconds at 0.25 left it at about 0.25, not 1.25. The shader draws the texture as it is, so it ignores the node's Modulate.

extends Sprite2D
## A 2D sprite whose texture scrolls forever. texture_repeat has to be Enabled:
## the 2D default clamps every UV past 1.0 to the edge pixel, so half the sprite turns into one smeared color.

@export var speed := Vector2(0.25, 0.0)  # texture widths per second

var offset_uv := Vector2.ZERO


func _ready() -> void:
	texture_repeat = CanvasItem.TEXTURE_REPEAT_ENABLED
	var shader := Shader.new()
	shader.code = """shader_type canvas_item;
uniform vec2 offset_uv;
void fragment() {
	COLOR = texture(TEXTURE, UV + offset_uv);
}
"""
	var mat := ShaderMaterial.new()
	mat.shader = shader
	material = mat


func _process(delta: float) -> void:
	offset_uv = (offset_uv + speed * delta).posmod(1.0)  # stays small, so it never loses precision
	(material as ShaderMaterial).set_shader_parameter("offset_uv", offset_uv)

What we measured

A 64 by 8 texture with red rising from 0 to 255 from left to right, drawn 256 pixels wide with nearest filtering, in the Compatibility renderer. We counted the pixels across the middle that showed red 255, the last column, and read the red at 8 points from left to right. Two runs gave the same numbers.

Why it happens

Whether a texture repeats is decided by the node that draws it, not by the texture file. A 2D node set to Inherit takes the setting from above it, and at the top that is the project default, Disabled. With repeat off, a UV of 1.3 reads the same column as 1.0, so everything past the edge becomes the last column stretched out. A TextureRect in Tile mode tiled fine either way in our test, which is why the problem tends to show up with shaders and sprite regions.

What to change

Textures made to scroll: 60 VFX Textures has 20 noise textures, 2048 px, that tile seamlessly in both directions, for flowing energy, dissolves and distortion, plus 26 shape masks and 14 8x8 flipbooks.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: 60 VFX Textures has 20 seamless noise textures to scroll for energy, dissolves and distortion, plus 26 shape masks and 14 flipbooks.

More how-tos: Shaders, materials and lights