dof update

This commit is contained in:
2026-06-08 03:05:11 +03:00
parent 2bea615307
commit 9d0799083c
10 changed files with 499 additions and 44 deletions
+42 -31
View File
@@ -4,14 +4,14 @@ extends Camera3D
@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
# --- ORTHOGRAPHIC ZOOM SETTINGS ---
@export var size_min: float = 5.0 # Maximum zoom in (small frame size)
@export var size_max: float = 30.0 # Maximum zoom out (large frame size)
@export var default_size: float = 15.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
@export var focus_slice_thickness: float = 6.0
@export var blur_transition: float = 15.0
const _FLASH_DURATION: float = 0.30
@@ -19,17 +19,17 @@ 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
var _target_size: float = 0.0
func _ready() -> void:
set_as_top_level(true)
projection = PROJECTION_PERSPECTIVE # Changed to perspective
_target_fov = default_fov
fov = default_fov
projection = Camera3D.PROJECTION_ORTHOGONAL # Switched to orthographic
_target_size = default_size
size = default_size
near = 0.1
far = 150.0 # Tight far plane to ensure depth buffer precision for DoF
far = 150.0
_setup_flash()
@@ -46,15 +46,15 @@ func _setup_flash() -> void:
func _unhandled_input(event: InputEvent) -> void:
# Zoom inputs now scale the Field of View (FOV)
# Zoom inputs now scale the Orthographic Size property
if event.is_action_pressed("zoom_in"):
_target_fov = clamp(_target_fov / 1.2, fov_min, fov_max)
_target_size = clamp(_target_size / 1.2, size_min, size_max)
if event.is_action_pressed("zoom_out"):
_target_fov = clamp(_target_fov * 1.2, fov_min, fov_max)
_target_size = clamp(_target_size * 1.2, size_min, size_max)
func _process(delta: float) -> void:
fov = lerpf(fov, _target_fov, delta * 10.0)
size = lerpf(size, _target_size, delta * 10.0)
var angle_rad := deg_to_rad(horizontal_angle_deg)
var offset := Vector3(
@@ -67,7 +67,7 @@ func _process(delta: float) -> void:
look_at(target.global_position, Vector3.UP)
# --- DYNAMIC TILT SHIFT CALCULATION ---
_update_tilt_shift_dof(target.global_position)
_update_ortho_tilt_shift_dof(target.global_position)
if _shake_amount > 0.005:
global_position += Vector3(
@@ -90,8 +90,7 @@ func trigger_hit(strength: float) -> void:
_flash_timer = _FLASH_DURATION
func _update_tilt_shift_dof(target_pos: Vector3) -> void:
# Make sure the camera has a practical attributes resource assigned
func _update_ortho_tilt_shift_dof(target_pos: Vector3) -> void:
var cam_attr: CameraAttributesPractical = null
if "attributes" in self and self.attributes is CameraAttributesPractical:
@@ -100,20 +99,32 @@ func _update_tilt_shift_dof(target_pos: Vector3) -> void:
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)
# 1. Project distance onto the camera's true forward forward axis
var to_target := target_pos - global_position
var forward_vector := -global_transform.basis.z
var ortho_depth := to_target.dot(forward_vector)
# 2. Scale the focus depth based on FOV zoom level so the blur stays consistent
var zoom_factor := default_fov / fov
# 2. Extract the dynamic zoom multiplier
var zoom_factor := size / default_size
var dynamic_slice := focus_slice_thickness * zoom_factor
var dynamic_transition := blur_transition * zoom_factor
var half_slice := dynamic_slice * 0.5
# 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)
# Base transition modified by the zoom state
var base_transition := blur_transition * zoom_factor
# 3. Apply the focal zones
cam_attr.dof_blur_far_distance = ortho_depth + half_slice
cam_attr.dof_blur_near_distance = maxf(near + 0.1, ortho_depth - half_slice)
# 4. ASYMMETRICAL CORRECTION FOR GODOT 4.7 FORWARD+
# Far transition needs a massive punch to actually manifest on screen
cam_attr.dof_blur_far_transition = maxf(0.1, base_transition * 0.5)
# Near transition needs to be greatly expanded (larger number = gentler slope)
# to stop it from snapping into massive instant blur
cam_attr.dof_blur_near_transition = maxf(1.0, base_transition * 1.5)
# 5. Keep both enabled
cam_attr.dof_blur_far_enabled = false
cam_attr.dof_blur_near_enabled = false
# 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