diff options
Diffstat (limited to 'addons')
6 files changed, 287 insertions, 0 deletions
diff --git a/addons/third-person-camera/plugin.cfg b/addons/third-person-camera/plugin.cfg new file mode 100644 index 0000000..60cb408 --- /dev/null +++ b/addons/third-person-camera/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="Third Person Camera" +description="A Third Person Camera node for Godot4." +author="Jean KOUSSAWO" +version="1.1.0" +script="tpc.gd" diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd new file mode 100644 index 0000000..a460835 --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd @@ -0,0 +1,176 @@ +@icon("./ThirdPersonCameraIcon.svg") +@tool +class_name ThirdPersonCamera extends Node3D + + +@onready var _camera := $Camera +@onready var _camera_rotation_pivot = $RotationPivot +@onready var _camera_offset_pivot = $RotationPivot/OffsetPivot +@onready var _camera_spring_arm := $RotationPivot/OffsetPivot/CameraSpringArm +@onready var _camera_marker := $RotationPivot/OffsetPivot/CameraSpringArm/CameraMarker + + + +## +@export var distance_from_pivot := 10.0 : + set(value) : + distance_from_pivot = value + $RotationPivot/OffsetPivot/CameraSpringArm.spring_length = distance_from_pivot + +## +@export var pivot_offset := Vector2.ZERO + +## +@export_range(-90.0, 90.0) var initial_dive_angle_deg := -20.0 : + set(value) : + initial_dive_angle_deg = clampf(value, tilt_lower_limit_deg, tilt_upper_limit_deg) + +## +@export_range(-90.0, 90.0) var tilt_upper_limit_deg := 60.0 + +## +@export_range(-90.0, 90.0) var tilt_lower_limit_deg := -60.0 + +## +@export_range(1.0, 1000.0) var tilt_sensitiveness := 10.0 + +## +@export_range(1.0, 1000.0) var horizontal_rotation_sensitiveness := 10.0 + +## +@export var current : bool = false : + set(value) : + $Camera.current = value + current = value + + +## +@export_group("mouse") +## +@export var mouse_follow : bool = false + +## +@export_range(0., 100.) var mouse_x_sensitiveness : float = 1 + +## +@export_range(0., 100.) var mouse_y_sensitiveness : float = 1 + + +# Camera3D properies replication +@export_category("Camera3D") +@export var keep_aspect : Camera3D.KeepAspect = Camera3D.KEEP_HEIGHT +@export_flags_3d_render var cull_mask : int = 1048575 +@export var environment : Environment +@export var attributes : CameraAttributes +@export var doppler_tracking : Camera3D.DopplerTracking = Camera3D.DOPPLER_TRACKING_DISABLED +@export var projection : Camera3D.ProjectionType = Camera3D.PROJECTION_PERSPECTIVE +@export_range(1.0, 179.0, 0.1, "suffix:°") var FOV = 75.0 +@export var near := 0.05 +@export var far := 4000.0 + + + +var camera_tilt_deg := 0. +var camera_horizontal_rotation_deg := 0. + + +func _ready(): + _camera.top_level = true + + +func _physics_process(_delta): + _update_camera_properties() + if Engine.is_editor_hint() : + _camera_marker.global_position = Vector3(0., 0., 1.).rotated(Vector3(1., 0., 0.), deg_to_rad(initial_dive_angle_deg)).rotated(Vector3(0., 1., 0.), deg_to_rad(-camera_horizontal_rotation_deg)) * _camera_spring_arm.spring_length + _camera_spring_arm.global_position + pass + #_camera.global_position = _camera_marker.global_position + tweenCameraToMarker() + _camera_offset_pivot.global_position = _camera_offset_pivot.get_parent().to_global(Vector3(pivot_offset.x, pivot_offset.y, 0.0)) + _camera_rotation_pivot.global_rotation_degrees.x = initial_dive_angle_deg + _camera_rotation_pivot.global_position = global_position + _process_tilt_input() + _process_horizontal_rotation_input() + _update_camera_tilt() + _update_camera_horizontal_rotation() + +func tweenCameraToMarker() : + var tween = create_tween() + tween.tween_property(_camera, "global_position", _camera_marker.global_position, 0.1) + +func _process_horizontal_rotation_input() : + if InputMap.has_action("tp_camera_right") and InputMap.has_action("tp_camera_left") : + var camera_horizontal_rotation_variation = Input.get_action_strength("tp_camera_right") - Input.get_action_strength("tp_camera_left") + camera_horizontal_rotation_variation = camera_horizontal_rotation_variation * get_process_delta_time() * 30 * horizontal_rotation_sensitiveness + camera_horizontal_rotation_deg += camera_horizontal_rotation_variation + + +func _process_tilt_input() : + if InputMap.has_action("tp_camera_up") and InputMap.has_action("tp_camera_down") : + var tilt_variation = Input.get_action_strength("tp_camera_up") - Input.get_action_strength("tp_camera_down") + tilt_variation = tilt_variation * get_process_delta_time() * 5 * tilt_sensitiveness + camera_tilt_deg = clamp(camera_tilt_deg + tilt_variation, tilt_lower_limit_deg - initial_dive_angle_deg, tilt_upper_limit_deg - initial_dive_angle_deg) + + + +func _update_camera_tilt() : + var tilt_final_val = clampf(initial_dive_angle_deg + camera_tilt_deg, tilt_lower_limit_deg, tilt_upper_limit_deg) + var tween = create_tween() + tween.tween_property(_camera, "global_rotation_degrees:x", tilt_final_val, 0.1) + + +func _update_camera_horizontal_rotation() : + # TODO : inverse + var tween = create_tween() + tween.tween_property(_camera_rotation_pivot, "global_rotation_degrees:y", camera_horizontal_rotation_deg * -1, 0.1).as_relative() + camera_horizontal_rotation_deg = 0.0 # reset the value + var vect_to_offset_pivot : Vector2 = ( + Vector2(_camera_offset_pivot.global_position.x, _camera_offset_pivot.global_position.z) + - + Vector2(_camera.global_position.x, _camera.global_position.z) + ).normalized() + _camera.global_rotation.y = -Vector2(0., -1.).angle_to(vect_to_offset_pivot.normalized()) + + + + +func _unhandled_input(event): + if mouse_follow and event is InputEventMouseMotion: + camera_horizontal_rotation_deg += event.relative.x * 0.1 * mouse_x_sensitiveness + camera_tilt_deg -= event.relative.y * 0.07 * mouse_y_sensitiveness + return + + pass + + +func _update_camera_properties() : + _camera.keep_aspect = keep_aspect + _camera.cull_mask = cull_mask + _camera.doppler_tracking = doppler_tracking + _camera.projection = projection + _camera.fov = FOV + _camera.near = near + _camera.far = far + if _camera.environment != environment : + _camera.environment = environment + if _camera.attributes != attributes : + _camera.attributes = attributes + + +func get_camera() : + return $Camera + + +func get_front_direction() : + var dir : Vector3 = _camera_offset_pivot.global_position - _camera.global_position + dir.y = 0. + dir = dir.normalized() + return dir + +func get_back_direction() : + return -get_front_direction() + +func get_left_direction() : + return get_front_direction().rotated(Vector3.UP, PI/2) + +func get_right_direction() : + return get_front_direction().rotated(Vector3.UP, -PI/2) diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn new file mode 100644 index 0000000..f1faf2e --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn @@ -0,0 +1,49 @@ +[gd_scene load_steps=6 format=3 uid="uid://wmf2eu0uuhrg"] + +[ext_resource type="Script" path="res://addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd" id="1_telmq"] + +[sub_resource type="SeparationRayShape3D" id="SeparationRayShape3D_84uqy"] +margin = 1.135 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2bjii"] +albedo_color = Color(0.8, 0.686275, 0.215686, 1) + +[sub_resource type="SphereMesh" id="SphereMesh_ag7lb"] +material = SubResource("StandardMaterial3D_2bjii") +radius = 0.05 +height = 0.1 + +[sub_resource type="CylinderMesh" id="CylinderMesh_ybkhn"] +top_radius = 0.0 +height = 0.938 + +[node name="ThirdPersonCamera" type="Node3D"] +script = ExtResource("1_telmq") + +[node name="RotationPivot" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.939693, 0.34202, 0, -0.34202, 0.939693, 0, 0, 0) +top_level = true + +[node name="OffsetPivot" type="Node3D" parent="RotationPivot"] +transform = Transform3D(1, -3.9187e-07, 6.47546e-10, 3.94476e-07, 1, 5.65946e-05, -2.27374e-11, -5.65946e-05, 1, 0, 0, 0) + +[node name="CameraSpringArm" type="SpringArm3D" parent="RotationPivot/OffsetPivot"] +process_priority = 11 +shape = SubResource("SeparationRayShape3D_84uqy") +spring_length = 10.0 + +[node name="CameraMarker" type="Marker3D" parent="RotationPivot/OffsetPivot/CameraSpringArm"] +transform = Transform3D(1, 7.93407e-08, 3.5101e-07, 1.48521e-08, 1, -7.89762e-06, 2.08538e-07, 1.2219e-06, 1, -6.69741e-09, -0.000566006, 9.99999) + +[node name="PivotDebug" type="MeshInstance3D" parent="RotationPivot/OffsetPivot"] +visible = false +mesh = SubResource("SphereMesh_ag7lb") + +[node name="Camera" type="Camera3D" parent="."] +transform = Transform3D(1, 5.38245e-15, -1.47882e-14, 0, 0.939693, 0.34202, 1.57372e-14, -0.34202, 0.939693, -1.47882e-13, 3.4202, 9.39693) +top_level = true + +[node name="CameraDebug" type="MeshInstance3D" parent="Camera"] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0.570638) +visible = false +mesh = SubResource("CylinderMesh_ybkhn") diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg new file mode 100644 index 0000000..b5944dc --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg @@ -0,0 +1,6 @@ +<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg"> +<path d="M10 1C9.24348 1.00087 8.51521 1.28746 7.96107 1.80249C7.40693 2.31752 7.06783 3.0229 7.0117 3.77734C6.46068 3.2778 5.74375 3.00081 5 3C4.20435 3 3.44129 3.31605 2.87868 3.87866C2.31607 4.44127 2 5.20435 2 6C2.00085 6.61968 2.19358 7.22389 2.5517 7.72961C2.90982 8.23533 3.5 8 4 8L4.82651 7.72961C6 7.5 6.45769 7.72961 7.0117 7.72961L8 8C8.22213 8 8.80616 8.08307 9 8C12.5 6.5 11 10 11 11L12 10L15 12V6L12 8V6.23047C12.3139 5.9497 12.5652 5.60598 12.7375 5.22168C12.9098 4.83738 12.9992 4.42115 13 4C13 3.20435 12.6839 2.44127 12.1213 1.87866C11.5587 1.31605 10.7956 1 10 1Z" fill="#FC7F7F"/> +<path d="M4.27197 8.17027L4.24228 10.1757L3.35748 10.2324L3.13183 14.7162L1.87886 14.8203L1.8848 10.3365L1 10.4122L1.03563 8.17973L4.27197 8.17027Z" fill="#FC7F7F"/> +<path d="M7.8943 10.3838C7.8943 10.6896 7.8656 10.9608 7.80819 11.1973C7.75079 11.4338 7.67162 11.6403 7.57067 11.8169C7.47169 11.9935 7.35491 12.1432 7.22031 12.2662C7.08571 12.3892 6.94121 12.4901 6.78682 12.5689C6.6344 12.6446 6.47605 12.7014 6.31176 12.7392C6.14747 12.7739 5.98614 12.7928 5.82779 12.7959V15H4.44418C4.44418 14.2779 4.44517 13.559 4.44715 12.8432C4.44913 12.1275 4.4521 11.4054 4.45606 10.677C4.46002 10.2923 4.46101 9.90766 4.45903 9.52297C4.45705 9.13829 4.462 8.75045 4.47387 8.35946C4.72724 8.23964 4.9806 8.14977 5.23397 8.08986C5.48733 8.02995 5.74861 8 6.01781 8C6.17023 8 6.32264 8.0205 6.47506 8.06149C6.62747 8.09932 6.77296 8.16081 6.91152 8.24595C7.05206 8.33108 7.18171 8.43829 7.30048 8.56757C7.42122 8.69369 7.52514 8.84505 7.61223 9.02162C7.69933 9.1982 7.76762 9.4 7.8171 9.62703C7.86857 9.8509 7.8943 10.1032 7.8943 10.3838ZM6.57601 10.5446C6.57601 10.3396 6.53642 10.1804 6.45724 10.0669C6.38005 9.95023 6.2791 9.89189 6.15439 9.89189C6.11283 9.89189 6.06928 9.8982 6.02375 9.91081C5.98021 9.92027 5.93864 9.93288 5.89905 9.94865L5.86342 11.3297C5.89113 11.336 5.91785 11.3392 5.94359 11.3392C5.96932 11.3392 5.99604 11.3392 6.02375 11.3392C6.09501 11.3392 6.1633 11.3203 6.22862 11.2824C6.29592 11.2446 6.3553 11.191 6.40677 11.1216C6.45823 11.0491 6.49881 10.9655 6.5285 10.8709C6.56017 10.7732 6.57601 10.6644 6.57601 10.5446Z" fill="#FC7F7F"/> +<path d="M11 8.28378L10.8337 10.2797C10.7763 10.2545 10.7189 10.2372 10.6615 10.2277C10.6041 10.2182 10.5467 10.2135 10.4893 10.2135C10.3428 10.2135 10.2033 10.245 10.0707 10.3081C9.93804 10.368 9.82027 10.461 9.71734 10.5872C9.61639 10.7133 9.53523 10.8709 9.47387 11.0601C9.41449 11.2493 9.3848 11.4716 9.3848 11.727C9.3848 11.891 9.40063 12.0329 9.4323 12.1527C9.46595 12.2725 9.51247 12.3718 9.57185 12.4507C9.63124 12.5295 9.7015 12.5878 9.78266 12.6257C9.86382 12.6635 9.95289 12.6824 10.0499 12.6824C10.1231 12.6824 10.1993 12.6698 10.2785 12.6446C10.3577 12.6194 10.4359 12.5863 10.5131 12.5453C10.5903 12.5043 10.6655 12.457 10.7387 12.4034C10.812 12.3498 10.8793 12.2946 10.9406 12.2378L10.8219 14.4608C10.7447 14.5239 10.6605 14.5806 10.5695 14.6311C10.4804 14.6784 10.3884 14.7209 10.2933 14.7588C10.1983 14.7935 10.1033 14.8203 10.0083 14.8392C9.9133 14.8581 9.82225 14.8676 9.73515 14.8676C9.48377 14.8676 9.24921 14.7935 9.03147 14.6453C8.81374 14.4971 8.6247 14.2937 8.46437 14.0351C8.30404 13.7766 8.17736 13.4723 8.08432 13.1223C7.99327 12.7691 7.94774 12.3892 7.94774 11.9824C7.94774 11.4432 7.99624 10.9356 8.09323 10.4595C8.19022 9.98018 8.33274 9.56081 8.52078 9.20135C8.70883 8.84189 8.94141 8.55811 9.21853 8.35C9.49762 8.14189 9.81829 8.03784 10.1805 8.03784C10.3171 8.03784 10.4567 8.05518 10.5992 8.08986C10.7437 8.12455 10.8773 8.18919 11 8.28378Z" fill="#FC7F7F"/> +</svg> diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import new file mode 100644 index 0000000..fa3b162 --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dgbqe4e8m5dlo" +path="res://.godot/imported/ThirdPersonCameraIcon.svg-895880fcccc5c0191c5c521669ff32ad.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg" +dest_files=["res://.godot/imported/ThirdPersonCameraIcon.svg-895880fcccc5c0191c5c521669ff32ad.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/addons/third-person-camera/tpc.gd b/addons/third-person-camera/tpc.gd new file mode 100644 index 0000000..45a417e --- /dev/null +++ b/addons/third-person-camera/tpc.gd @@ -0,0 +1,12 @@ +@tool +extends EditorPlugin + + +func _enter_tree(): + # Initialization of the plugin goes here. + pass + + +func _exit_tree(): + # Clean-up of the plugin goes here. + pass |