2024-02-12 19:35:37 +01:00

48 lines
1.2 KiB
GDScript

class_name Camera
extends Camera3D
@export_group("Zoom")
@export var zoom_speed := 0.5
@export var zoom_interpolation := 10.0
@export var zoom_min := 2.0
@export var zoom_max := 8.0
var target_distance: float
func _ready():
target_distance = position.z
Global.camera = self
func _input(event):
if event.is_action_pressed("zoom_in", true):
target_distance -= zoom_speed
on_distance_changed()
get_viewport().set_input_as_handled()
if event.is_action_pressed("zoom_out", true):
target_distance += zoom_speed
on_distance_changed()
get_viewport().set_input_as_handled()
func on_distance_changed():
target_distance = clampf(target_distance, zoom_min, zoom_max)
Global.camera_attributes.dof_blur_far_distance = target_distance + 1.0
func is_below_terrain():
return %CameraRays.occlusion_point.y > global_position.y
func _process(delta):
var distance := target_distance
if %CameraRays.occlusion_distance < target_distance:
distance = %CameraRays.occlusion_distance - 0.5
if is_below_terrain():
position.z = distance
return
if abs(distance - position.z) < 0.01:
return
position.z = lerpf(position.z, distance, zoom_interpolation * delta)