← Pixel Dreamer Studio

A 3D outline that stays the same width on screen

Push the outline shell out by a number of pixels, in clip space, instead of by a number of meters.

The usual 3D outline draws the mesh again a little bigger, by a set number of meters. On screen that width shrinks with distance like everything else. In our Godot 4.7 test a 0.04 m outline around a sphere was 8 pixels wide at 2 m, 3 at 5 m and 1.2 at 12 m. Zooming the camera in from a field of view of 75 to 30 at 12 m made it 3 pixels again. The script below moves the shell out in pixels instead, and measured 3 pixels at 2, 5 and 12 m and at both zoom levels.

The code

Put this script on a MeshInstance3D in place of its own script. It works like the one on the 3D outline page, but width_px is in pixels on screen, not meters. It copies the mesh's material, adds the outline as its next_pass, and show_outline(false) turns it off.

extends MeshInstance3D
## Outline around a 3D mesh that stays the same number of pixels wide on screen, near or far.

@export var outline_color := Color(1.0, 0.8, 0.1)
@export var width_px := 3.0  # in pixels on screen, not meters
@export var from_center := true  # true: push out from the mesh origin (boxes, hard edges) · false: along the normals

const OUTLINE_SHADER := """
shader_type spatial;
render_mode unshaded, cull_front;
uniform vec4 outline_color : source_color = vec4(1.0, 0.8, 0.1, 1.0);
uniform float width_px = 3.0;
uniform bool from_center = true;

void vertex() {
	vec3 dir = from_center ? normalize(VERTEX) : NORMAL;
	vec4 clip = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX, 1.0);
	vec4 clip_dir = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(dir, 0.0);
	// which way the vertex would move on screen, measured in pixels
	vec2 px = (clip_dir.xy * clip.w - clip.xy * clip_dir.w) * VIEWPORT_SIZE;
	float l = length(px);
	if (l > 0.0001) {
		// width_px pixels; * clip.w cancels the divide by depth, so far away is not thinner
		clip.xy += px / l * width_px * 2.0 / VIEWPORT_SIZE * clip.w;
	}
	POSITION = clip;
}

void fragment() {
	ALBEDO = outline_color.rgb;
}
"""

var _outline: ShaderMaterial


func _ready() -> void:
	_outline = ShaderMaterial.new()
	_outline.shader = Shader.new()
	_outline.shader.code = OUTLINE_SHADER
	var base := get_active_material(0)
	base = base.duplicate() if base else StandardMaterial3D.new()
	material_override = base  # a copy, so other meshes that share the material get no outline
	show_outline(true)


func show_outline(on: bool) -> void:
	_outline.set_shader_parameter("outline_color", outline_color)
	_outline.set_shader_parameter("width_px", width_px)
	_outline.set_shader_parameter("from_center", from_center)
	material_override.next_pass = _outline if on else null  # drawn after the mesh's own material

What we measured

A sphere 1 m across in the middle of a 960 × 540 window, with the camera 2, 5 and 12 m away at a field of view of 75, then 12 m away at 30. We drew it with the outline off and on and counted the outline pixels to the right of the sphere and above it, across 5 rows and 5 columns. Two runs gave the same numbers.

Why it happens

A width in meters is a size in the world, so the camera shrinks it with distance and blows it up when the field of view gets narrower. In clip space, the GPU divides x and y by w, the depth, to find the point on screen. The script works out which way the vertex would move on screen, moves it width_px pixels that way, and multiplies the move by w, so the divide by depth takes it straight back off. It works in pixels on both axes, so the wide window did not make the sides thicker than the top.

What to change

Gaps at box corners and outlining just one copy: 3D outline shader without gaps. To show it only while the mouse is over the object, also inside a SubViewport: hover outline. For 2D sprites, see the 2D outline page.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: PD Toon World Starter Kit for Godot 4 has a toon outline that keeps the same width on screen at any distance, with a mode for cubes and hard-edged props so the corners never split.

More how-tos for PD Toon World Starter Kit for Godot 4: Hover a 3D object inside a SubViewport

More how-tos: Shaders, materials and lights