← Pixel Dreamer Studio

Hover a 3D object inside a SubViewport

SubViewports start with physics_object_picking off, so mouse_entered and mouse_exited never fire. Turn it on.

A low-resolution or pixel-art 3D game often draws the world in a SubViewport and scales it up. Hovering 3D objects then stops working: mouse_entered never fires, even with a CollisionShape3D and input_ray_pickable on. In our Godot 4.7 test the main window had physics_object_picking on and a StaticBody3D under the mouse got 1 mouse_entered. Inside a SubViewport, drawn at a quarter size, the same body got 0, because the SubViewport had physics_object_picking off. After turning it on, it got 1. The script below turns it on for you and shows an outline while the mouse is over the object.

The one-line fix

On the SubViewport, tick Physics > Object Picking in the Inspector, or in code: $SubViewportContainer/SubViewport.physics_object_picking = true. Our SubViewport sat in a SubViewportContainer, which passes the mouse on to it.

The code

Put this script on a StaticBody3D or Area3D that has a CollisionShape3D. Set outline to a MeshInstance3D with a show_outline(on) function, for example the one from the same-width outline page. The script turns on physics_object_picking for the viewport it is in, then shows the outline on mouse_entered and hides it on mouse_exited.

extends CollisionObject3D
## Shows an outline while the mouse is over this body (StaticBody3D, Area3D, ...), also inside a SubViewport.
## Needs a CollisionShape3D child. outline = a MeshInstance3D with a show_outline(on) function.

@export var outline: Node


func _ready() -> void:
	input_ray_pickable = true
	var vp := get_viewport()
	if not vp.physics_object_picking:
		vp.physics_object_picking = true  # a SubViewport starts with this off: no mouse_entered at all
	mouse_entered.connect(_hover.bind(true))
	mouse_exited.connect(_hover.bind(false))
	_hover.call_deferred(false)  # after the outline's own _ready


func _hover(on: bool) -> void:
	if outline:
		outline.show_outline(on)

What we measured

A 1 m box with a box collision shape in front of the camera, and a mouse move sent into the window from the corner to the middle of the box. The SubViewport filled the 960 × 540 window in a SubViewportContainer with stretch on and stretch_shrink at 4, so it drew at 240 × 135. Two runs gave the same numbers.

Related pages

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: 3D outline shader without gaps at the corners · Get a hard cel-shaded light edge from StandardMaterial3D's Toon mode · A 3D outline that stays the same width on screen

More how-tos: Game systems