120 lines
3.9 KiB
GDScript
120 lines
3.9 KiB
GDScript
extends Camera3D
|
|
|
|
@export var height: float = 28.0
|
|
@export var horizontal_distance: float = 28.0
|
|
@export var horizontal_angle_deg: float = 45.0
|
|
|
|
# --- PERSPECTIVE ZOOM SETTINGS ---
|
|
@export var fov_min: float = 15.0 # Maximum zoom in (narrow FOV)
|
|
@export var fov_max: float = 60.0 # Maximum zoom out (wide FOV)
|
|
@export var default_fov: float = 35.0
|
|
|
|
# --- TILT-SHIFT DOF SETTINGS ---
|
|
@export var focus_slice_thickness: float = 2.0 # Total thickness (in meters) of what stays sharp
|
|
@export var blur_transition: float = 2.0 # How fast it transitions into full blur
|
|
|
|
const _FLASH_DURATION: float = 0.30
|
|
|
|
var _shake_amount: float = 0.0
|
|
var _shake_decay: float = 0.0
|
|
var _flash_timer: float = 0.0
|
|
var _flash_rect: ColorRect = null
|
|
var _target_fov: float = 0.0
|
|
|
|
|
|
func _ready() -> void:
|
|
set_as_top_level(true)
|
|
projection = PROJECTION_PERSPECTIVE # Changed to perspective
|
|
_target_fov = default_fov
|
|
fov = default_fov
|
|
|
|
near = 0.1
|
|
far = 150.0 # Tight far plane to ensure depth buffer precision for DoF
|
|
|
|
_setup_flash()
|
|
|
|
|
|
func _setup_flash() -> void:
|
|
var canvas := CanvasLayer.new()
|
|
canvas.layer = 20
|
|
add_child(canvas)
|
|
_flash_rect = ColorRect.new()
|
|
_flash_rect.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
|
_flash_rect.color = Color(1.0, 0.25, 0.2, 0.0)
|
|
_flash_rect.mouse_filter = Control.MOUSE_FILTER_IGNORE
|
|
canvas.add_child(_flash_rect)
|
|
|
|
|
|
func _unhandled_input(event: InputEvent) -> void:
|
|
# Zoom inputs now scale the Field of View (FOV)
|
|
if event.is_action_pressed("zoom_in"):
|
|
_target_fov = clamp(_target_fov / 1.2, fov_min, fov_max)
|
|
if event.is_action_pressed("zoom_out"):
|
|
_target_fov = clamp(_target_fov * 1.2, fov_min, fov_max)
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
fov = lerpf(fov, _target_fov, delta * 10.0)
|
|
|
|
var angle_rad := deg_to_rad(horizontal_angle_deg)
|
|
var offset := Vector3(
|
|
sin(angle_rad) * horizontal_distance,
|
|
height,
|
|
cos(angle_rad) * horizontal_distance,
|
|
)
|
|
var target := get_parent() as Node3D
|
|
global_position = target.global_position + offset
|
|
look_at(target.global_position, Vector3.UP)
|
|
|
|
# --- DYNAMIC TILT SHIFT CALCULATION ---
|
|
_update_tilt_shift_dof(target.global_position)
|
|
|
|
if _shake_amount > 0.005:
|
|
global_position += Vector3(
|
|
randf_range(-_shake_amount, _shake_amount),
|
|
randf_range(-_shake_amount * 0.25, _shake_amount * 0.25),
|
|
randf_range(-_shake_amount, _shake_amount),
|
|
)
|
|
_shake_amount = move_toward(_shake_amount, 0.0, _shake_decay * delta)
|
|
|
|
if _flash_timer > 0.0:
|
|
_flash_timer = maxf(0.0, _flash_timer - delta)
|
|
_flash_rect.color.a = (_flash_timer / _FLASH_DURATION) * 0.6
|
|
else:
|
|
_flash_rect.color.a = 0.0
|
|
|
|
|
|
func trigger_hit(strength: float) -> void:
|
|
_shake_amount = strength * 0.5
|
|
_shake_decay = _shake_amount / 0.4
|
|
_flash_timer = _FLASH_DURATION
|
|
|
|
|
|
func _update_tilt_shift_dof(target_pos: Vector3) -> void:
|
|
# Make sure the camera has a practical attributes resource assigned
|
|
var cam_attr: CameraAttributesPractical = null
|
|
|
|
if "attributes" in self and self.attributes is CameraAttributesPractical:
|
|
cam_attr = self.attributes
|
|
elif "camera_attributes" in self and self.camera_attributes is CameraAttributesPractical:
|
|
cam_attr = self.camera_attributes
|
|
|
|
if cam_attr != null:
|
|
# 1. Find exact distance from camera lens to your target
|
|
var target_distance := global_position.distance_to(target_pos)
|
|
|
|
# 2. Scale the focus depth based on FOV zoom level so the blur stays consistent
|
|
var zoom_factor := default_fov / fov
|
|
var dynamic_slice := focus_slice_thickness * zoom_factor
|
|
var dynamic_transition := blur_transition * zoom_factor
|
|
|
|
# 3. Apply the sharp "slice" directly around the target distance
|
|
cam_attr.dof_blur_far_distance = target_distance + (dynamic_slice * 0.5)
|
|
cam_attr.dof_blur_near_distance = target_distance - (dynamic_slice * 0.5)
|
|
|
|
# 4. Apply transitions and force execution
|
|
cam_attr.dof_blur_far_transition = dynamic_transition
|
|
cam_attr.dof_blur_near_transition = dynamic_transition
|
|
cam_attr.dof_blur_far_enabled = true
|
|
cam_attr.dof_blur_near_enabled = true
|