2026-08-16 22:38:23 +02:00
|
|
|
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:
|
2026-08-17 00:13:14 +02:00
|
|
|
if is_inside_tree():
|
|
|
|
|
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()
|
2026-08-16 22:38:23 +02:00
|
|
|
|
2026-08-17 00:13:14 +02:00
|
|
|
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)
|
2026-08-16 22:38:23 +02:00
|
|
|
|
2026-08-17 00:13:14 +02:00
|
|
|
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)
|