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.
- The 0.04 m outline: 8.0 pixels at 2 m, 3.0 at 5 m, 1.2 at 12 m, and 3.0 at 12 m with the field of view at 30.
- The script with
width_pxat 3: 3.0 pixels in all four, the same to the side and above. - A turned box at 2 m and at 12 m: with
from_centeron, the outline covered all of the thin ring of background around it. Pushed along the normals instead, it covered 75% at 2 m and left gaps at the corners.
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
width_px: pixels of the image you render. A window twice as tall needs twice the number to look as thick against the scene.from_center: on for boxes and hard edges with the origin in the middle, off for meshes whose origin is far from their middle, the same as on the first outline page.outline_color: the outline pass isunshaded, so lights do not shade it.
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
- Find and fix the seam in a repeating texture
- PS1 vertex wobble that is the same near and far
- Snow that stays on the roofs
- Packed ORM maps on a StandardMaterial3D
- Neon signs that share a material but not a colour
- A MatCap that stays the right way up from every side
- Scroll or tile a 2D texture without stretched edge lines
- Keep floors, roads and roofs sharp when the camera looks along them
- Make WorldEnvironment color grading apply to a 2D game
- Get a hard cel-shaded light edge from StandardMaterial3D's Toon mode
- Make metallic materials stop looking black or flat
- Make a glowing sign or screen actually light the wall around it
- Use a LUT strip for color grading in Godot 4
- Tiling textures that keep their size on any mesh
- A sharp low-resolution PS1 look
- Only 8 lights show on a long wall in Compatibility
- Hit flash shader for 2D sprites
- Flash a 3D enemy white when it takes a hit
- Dissolve shader for 3D models
- 2D outline shader that is not cut off
- 3D outline shader without gaps at the corners
- A force field shield with a hit ripple
- Normal map inverted: DirectX vs OpenGL
- Full-screen post-process shader in 3D
- Scroll a shader texture for hours without the once-an-hour jump