breadwinner/scenes/main_menu/main_menu_mouse_handler.gd
Balogh Krisztián 18d9a149a3 added main menu
when you quit the game, it basically crashes and the audio lags for now.
changable audio is still not fixed, the child spatial_audio3d still plays too loud.
2026-08-16 22:38:23 +02:00

41 lines
1.4 KiB
GDScript

extends Area3D
@onready var viewport: SubViewport = %SubViewport
@onready var screen_mesh: MeshInstance3D = %MeshInstance3D
func _ready() -> void:
# 1. Wait exactly one frame to guarantee the SubViewport is fully loaded and rendering
await get_tree().process_frame
# 2. Get the material you put on your ScreenMesh
var mat = screen_mesh.get_active_material(0)
# 3. Manually pipe the live video feed from the viewport directly into the material!
if mat:
mat.albedo_texture = viewport.get_texture()
func _exit_tree() -> void:
pass
func _input_event(_camera: Camera3D, event: InputEvent, event_position: Vector3, _normal: Vector3, _shape_idx: int) -> void:
if not (event is InputEventMouse or event is InputEventScreenTouch):
return
# 1. CRITICAL FIX: Duplicate the event so we don't corrupt Godot's internal input state
var custom_event = event.duplicate()
var local_pos = screen_mesh.to_local(event_position)
var quad_size = screen_mesh.mesh.size
var percent_x = (local_pos.x / quad_size.x) + 0.5
var percent_y = 0.5 - (local_pos.y / quad_size.y)
var viewport_size = viewport.size
var final_2d_pos = Vector2(percent_x * viewport_size.x, percent_y * viewport_size.y)
custom_event.position = final_2d_pos
if custom_event is InputEventMouse:
custom_event.global_position = final_2d_pos
# 2. Push the duplicated, modified event to the viewport
viewport.push_input(custom_event)