Improved camera

This commit is contained in:
Eduard Urbach 2024-02-12 16:58:17 +01:00
parent e7132a50f7
commit 43e0825aff
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 59 additions and 7 deletions

View File

@ -3,7 +3,7 @@ extends Node
@export var environment: Environment
@export var camera_attributes: CameraAttributesPractical
var camera: Camera3D
var camera: Camera
var player: Player
var players: PlayerManager
var account_id: String

View File

@ -1,5 +1,8 @@
class_name Camera
extends Camera3D
@export_flags_3d_physics var collision_mask := 1
@export_group("Zoom")
@export var zoom_speed := 0.5
@export var zoom_interpolation := 10.0
@ -7,9 +10,12 @@ extends Camera3D
@export var zoom_max := 8.0
var target_distance: float
var occlusion_distance: float = far
var pivot: Node3D
func _ready():
target_distance = position.z
pivot = get_parent()
Global.camera = self
func _input(event):
@ -28,7 +34,38 @@ func on_distance_changed():
Global.camera_attributes.dof_blur_far_distance = target_distance + 1.0
func _process(delta):
if abs(target_distance - position.z) < 0.01:
var distance := target_distance
if occlusion_distance < target_distance:
distance = occlusion_distance
if abs(distance - position.z) < 0.01:
return
position.z = lerpf(position.z, target_distance, zoom_interpolation * delta)
position.z = lerpf(position.z, distance, zoom_interpolation * delta)
func _physics_process(_delta):
occlusion_distance = 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 := 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

View File

@ -1,6 +1,8 @@
extends Node3D
@export var sensitivity: float
@export var max_rotation_x: float = 25.0
@export var min_rotation_x: float = -80.0
var enabled: bool
@ -24,3 +26,5 @@ func _unhandled_input(event):
rotation.x += deg_to_rad(-event.relative.y * sensitivity)
rotation.y += deg_to_rad(-event.relative.x * sensitivity)
rotation.x = clampf(rotation.x, deg_to_rad(min_rotation_x), deg_to_rad(max_rotation_x))

View File

@ -5,8 +5,9 @@
[ext_resource type="PackedScene" uid="uid://2bbycjulf00g" path="res://character/health/HealthComponent.tscn" id="2_fsqxc"]
[ext_resource type="PackedScene" uid="uid://x102pryt2s5a" path="res://character/movement/MovementComponent.tscn" id="3_2phqx"]
[sub_resource type="BoxShape3D" id="BoxShape3D_x1ppt"]
size = Vector3(0.7, 0.6, 0.7)
[sub_resource type="CylinderShape3D" id="CylinderShape3D_ub3kk"]
height = 0.5
radius = 0.35
[sub_resource type="Animation" id="Animation_qls31"]
resource_name = "slime_fall"
@ -37,13 +38,15 @@ _data = {
}
[node name="Slime" type="CharacterBody3D" groups=["enemy"]]
collision_layer = 256
collision_mask = 769
script = ExtResource("1_r5888")
[node name="Model" parent="." instance=ExtResource("1_1h1hj")]
[node name="Collision" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.0452515, 0.3, 0)
shape = SubResource("BoxShape3D_x1ppt")
shape = SubResource("CylinderShape3D_ub3kk")
[node name="Animation" type="AnimationPlayer" parent="."]
root_node = NodePath("../Model")

View File

@ -108,6 +108,8 @@ texture = ExtResource("1_hhudy")
[node name="Area" type="Area3D" parent="."]
unique_name_in_owner = true
collision_layer = 64
collision_mask = 512
monitorable = false
[node name="CollisionShape3D" type="CollisionShape3D" parent="Area"]

View File

@ -13,10 +13,12 @@
[ext_resource type="AnimationLibrary" uid="uid://d4n0puibh4hyt" path="res://assets/animations/human.blend" id="11_d0e6r"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_2f50n"]
radius = 0.25
radius = 0.3
height = 1.6
[node name="Player" type="CharacterBody3D" groups=["player"]]
collision_layer = 512
collision_mask = 769
script = ExtResource("1_8gebs")
[node name="Model" type="Node3D" parent="."]

View File

@ -140,6 +140,10 @@ zoom_out={
3d_render/layer_5="Water"
3d_render/layer_9="Enemies"
3d_render/layer_10="Players"
3d_physics/layer_1="World"
3d_physics/layer_7="Loot"
3d_physics/layer_9="Enemies"
3d_physics/layer_10="Players"
[physics]