DOF test
This commit is contained in:
+56
-11
@@ -1,10 +1,17 @@
|
||||
extends Camera3D
|
||||
|
||||
@export var height: float = 18.0
|
||||
@export var horizontal_distance: float = 18.0
|
||||
@export var height: float = 28.0
|
||||
@export var horizontal_distance: float = 28.0
|
||||
@export var horizontal_angle_deg: float = 45.0
|
||||
@export var ortho_size: float = 15.0
|
||||
@export var ortho_size_max: float = 40.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
|
||||
|
||||
@@ -12,14 +19,18 @@ 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_size: float = 0.0
|
||||
var _target_fov: float = 0.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_as_top_level(true)
|
||||
projection = PROJECTION_ORTHOGONAL
|
||||
_target_size = ortho_size
|
||||
size = ortho_size
|
||||
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()
|
||||
|
||||
|
||||
@@ -35,14 +46,16 @@ func _setup_flash() -> void:
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
# Zoom inputs now scale the Field of View (FOV)
|
||||
if event.is_action_pressed("zoom_in"):
|
||||
_target_size = clamp(_target_size / 1.2, ortho_size, ortho_size_max)
|
||||
_target_fov = clamp(_target_fov / 1.2, fov_min, fov_max)
|
||||
if event.is_action_pressed("zoom_out"):
|
||||
_target_size = clamp(_target_size * 1.2, ortho_size, ortho_size_max)
|
||||
_target_fov = clamp(_target_fov * 1.2, fov_min, fov_max)
|
||||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
size = lerpf(size, _target_size, delta * 10.0)
|
||||
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,
|
||||
@@ -53,6 +66,9 @@ func _process(delta: float) -> void:
|
||||
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),
|
||||
@@ -72,3 +88,32 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user