38 lines
1.1 KiB
GDScript3
38 lines
1.1 KiB
GDScript3
|
class_name CameraRays
|
||
|
extends Node3D
|
||
|
|
||
|
@export_flags_3d_physics var collision_mask := 1
|
||
|
|
||
|
var occlusion_distance: float = 1000
|
||
|
var occlusion_point: Vector3
|
||
|
var pivot: Node3D
|
||
|
|
||
|
func _ready():
|
||
|
pivot = get_parent()
|
||
|
|
||
|
func _physics_process(_delta):
|
||
|
occlusion_distance = Global.camera.far
|
||
|
|
||
|
var corner: Vector2 = get_viewport().get_visible_rect().size
|
||
|
var space_state = get_world_3d().direct_space_state
|
||
|
|
||
|
send_ray(space_state, Vector2(0, 0))
|
||
|
send_ray(space_state, Vector2(corner.x, 0))
|
||
|
send_ray(space_state, Vector2(0, corner.y))
|
||
|
send_ray(space_state, Vector2(corner.x, corner.y))
|
||
|
send_ray(space_state, Vector2(corner.x / 2, corner.y / 2))
|
||
|
|
||
|
func send_ray(space_state: PhysicsDirectSpaceState3D, pos: Vector2):
|
||
|
var from := pivot.global_position
|
||
|
var to := Global.camera.project_position(pos, -1)
|
||
|
var query = PhysicsRayQueryParameters3D.create(from, to, collision_mask)
|
||
|
var result = space_state.intersect_ray(query)
|
||
|
|
||
|
if result:
|
||
|
var hit_point: Vector3 = result.position
|
||
|
var diff := hit_point - pivot.global_position
|
||
|
var distance := diff.length()
|
||
|
|
||
|
if distance < occlusion_distance:
|
||
|
occlusion_distance = distance
|
||
|
occlusion_point = hit_point
|