← Pixel Dreamer Studio

Space lights, posts or coins evenly along a Path3D

Sample by distance along the baked curve, not by point index, and on a closed loop divide by the count, not the count minus one.

Use curve.sample_baked(length * i / (count - 1)) with length = curve.get_baked_length(): that walks the curve by distance. Spreading the steps over the point index with curve.sample(index, t) gives every segment the same number of objects however long it is. In our Godot 4.7 test 12 lights on a 14.47 m path with segments of 2, 8 and 4.47 m ended up 0.36 to 3.19 m apart that way, and 1.31 to 1.32 m apart with sample_baked(). On a closed 40 m loop, dividing by count - 1 put the last light exactly on the first one. Dividing by count gave 12 gaps of 3.33 m.

The code

Save it as path_spacing.gd. even_points() returns the positions, and place() adds a copy of a scene at each one as a child of the Path3D, so they sit on the curve wherever the path is. Pass closed = true for a loop whose last point is back on its first.

extends RefCounted
## Place things at even distances along a Path3D: string lights, fence posts, coins.


static func even_points(curve: Curve3D, count: int, closed := false) -> PackedVector3Array:
	var out := PackedVector3Array()
	var length := curve.get_baked_length()
	var gaps := count if closed else count - 1  # a loop ends where it starts, so it has one gap more
	for i in count:
		out.append(curve.sample_baked(length * i / maxi(gaps, 1), true))
	return out


static func place(path: Path3D, scene: PackedScene, count: int, closed := false) -> Array[Node3D]:
	var nodes: Array[Node3D] = []
	for p in even_points(path.curve, count, closed):
		var n := scene.instantiate() as Node3D
		path.add_child(n)
		n.position = p  # curve points are in the Path3D's own space
		nodes.append(n)
	return nodes

What we measured

Distances are measured along the curve unless we say straight. Two runs gave the same numbers.

Why it happens

A curve's point index counts points, not meters. A step from t = 0 to 1 covers a whole segment whatever its length, and on a curved segment equal steps in t are not equal distances either. The baked curve is the curve cut into short pieces with their lengths added up, so an offset into it is a distance. A line has one gap fewer than the objects on it. A loop that ends where it starts has as many gaps as objects, so dividing by count - 1 stretches the gaps until the last object reaches the start.

What to change

Lights along a path: Winter & Christmas VFX for Godot 4 has String_Lights presets whose script builds the bulbs, the wire and a few real lights. Set its path to a Path3D and the bulbs follow the curve, spaced by distance along it, for roof edges or a spiral around a tree, with Twinkle, Chase, Alternate or Steady patterns.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Winter & Christmas VFX for Godot 4 has string lights that follow a Path3D, with the bulbs, the wire and a few real lights built for you.

More how-tos for Winter & Christmas VFX for Godot 4: Snow that stays on the roofs

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