Now About Social Code
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--addons/third-person-camera/plugin.cfg7
-rw-r--r--addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd176
-rw-r--r--addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn49
-rw-r--r--addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg6
-rw-r--r--addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import37
-rw-r--r--addons/third-person-camera/tpc.gd12
-rw-r--r--prefab/player.tscn22
-rw-r--r--scenes/main.tscn11
-rw-r--r--scripts/fly_camera.gd4
-rw-r--r--scripts/level_gen.gd65
-rw-r--r--scripts/marching_cubes.gd14
-rw-r--r--scripts/player.gd39
12 files changed, 384 insertions, 58 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
diff --git a/prefab/player.tscn b/prefab/player.tscn
new file mode 100644
index 0000000..b4f8630
--- /dev/null
+++ b/prefab/player.tscn
@@ -0,0 +1,22 @@
+[gd_scene load_steps=5 format=3 uid="uid://dsq68sqy2ldjm"]
+
+[ext_resource type="Script" path="res://scripts/player.gd" id="1_l6xtg"]
+[ext_resource type="PackedScene" uid="uid://wmf2eu0uuhrg" path="res://addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn" id="1_stkca"]
+
+[sub_resource type="BoxShape3D" id="BoxShape3D_ibgtc"]
+size = Vector3(1, 2, 1)
+
+[sub_resource type="BoxMesh" id="BoxMesh_wkmld"]
+size = Vector3(1, 2, 1)
+
+[node name="Player" type="CharacterBody3D"]
+script = ExtResource("1_l6xtg")
+
+[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
+transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
+shape = SubResource("BoxShape3D_ibgtc")
+
+[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"]
+mesh = SubResource("BoxMesh_wkmld")
+
+[node name="ThirdPersonCamera" parent="." instance=ExtResource("1_stkca")]
diff --git a/scenes/main.tscn b/scenes/main.tscn
index fb249bc..cf072fa 100644
--- a/scenes/main.tscn
+++ b/scenes/main.tscn
@@ -1,14 +1,14 @@
[gd_scene load_steps=8 format=3 uid="uid://bb55ocpenupao"]
[ext_resource type="Script" path="res://scripts/level_gen.gd" id="1_sfx65"]
-[ext_resource type="Script" path="res://scripts/fly_camera.gd" id="2_ba28s"]
+[ext_resource type="PackedScene" uid="uid://dsq68sqy2ldjm" path="res://prefab/player.tscn" id="3_8dect"]
[ext_resource type="Material" uid="uid://uiwp0gpofjrx" path="res://materials/terrain.tres" id="3_nsy4g"]
[sub_resource type="BoxMesh" id="BoxMesh_2l1xl"]
material = ExtResource("3_nsy4g")
[sub_resource type="Environment" id="Environment_w27kj"]
-background_color = Color(0.658824, 0.658824, 0.658824, 1)
+background_color = Color(0.792157, 0.792157, 0.792157, 1)
ambient_light_source = 2
ambient_light_color = Color(1, 1, 1, 1)
@@ -21,10 +21,6 @@ size = Vector2(200, 200)
[node name="Node3D" type="Node3D"]
script = ExtResource("1_sfx65")
-[node name="Camera3D" type="Camera3D" parent="."]
-transform = Transform3D(1, 0, 0, 0, 0.805763, 0.592238, 0, -0.592238, 0.805763, 1.365, 30, 6.859)
-script = ExtResource("2_ba28s")
-
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -0.878192, 0.478309, 0, -0.478309, -0.878192, 0, 50, 0)
light_energy = 2.0
@@ -45,3 +41,6 @@ shape = SubResource("BoxShape3D_gtg8d")
[node name="MeshInstance3D2" type="MeshInstance3D" parent="Water"]
mesh = SubResource("PlaneMesh_ypd0i")
skeleton = NodePath("../..")
+
+[node name="Player" parent="." instance=ExtResource("3_8dect")]
+transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.7109, 0, 0)
diff --git a/scripts/fly_camera.gd b/scripts/fly_camera.gd
index 0f4dc53..b342ab6 100644
--- a/scripts/fly_camera.gd
+++ b/scripts/fly_camera.gd
@@ -1,4 +1,4 @@
-extends Camera3D
+extends Node3D
# Modifier keys' speed multiplier
const SHIFT_MULTIPLIER = 2.5
@@ -113,4 +113,4 @@ func _update_mouselook():
_total_pitch += pitch
rotate_y(deg_to_rad(-yaw))
- rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch))
+ #rotate_object_local(Vector3(1,0,0), deg_to_rad(-pitch))
diff --git a/scripts/level_gen.gd b/scripts/level_gen.gd
index 184ed26..66f465a 100644
--- a/scripts/level_gen.gd
+++ b/scripts/level_gen.gd
@@ -24,7 +24,8 @@ func gen_map_grid():
var noise = FastNoiseLite.new()
noise.noise_type = FastNoiseLite.TYPE_SIMPLEX
noise.frequency = 0.06
- noise.set_seed(0xDEADBEEF)
+ #noise.set_seed(0xDEADBEEF)
+ noise.set_seed(random.randi())
for z in depth:
for x in width:
var mid_x = x - width/2.0
@@ -39,31 +40,19 @@ func gen_map_grid():
for y in gen_h:
map_grid[z*height*width + y*width + x] = -100.0
-func add_normal(tri_index: int, triangles: PackedInt32Array, vertices: PackedVector3Array, normal_list: Array[Vector4]):
- var new_tri_index: int = 0
- while new_tri_index < len(triangles):
- @warning_ignore("integer_division")
- var tri_index_1:int = int(new_tri_index/3)
- @warning_ignore("integer_division")
- var tri_index_2:int = int(tri_index/3)
- if tri_index_1 == tri_index_2:
- new_tri_index += 3
+func add_normal(tri_index: int, vertices: PackedVector3Array, normal_list: Array[Vector4]):
+ var vertex_index: int = 0
+ while vertex_index < len(vertices):
+ if vertex_index == tri_index:
+ vertex_index += 1
continue
- var orig_vert = vertices[triangles[tri_index]]
- var verts = [
- new_tri_index+0,
- new_tri_index+1,
- new_tri_index+2]
-
- for i in verts:
- var vert = vertices[triangles[i]]
- if i == tri_index:
- continue
- if vert == orig_vert:
- normal_list[triangles[tri_index]] += normal_list[triangles[i]]
+ var orig_vert = vertices[tri_index]
+ var vert = vertices[vertex_index]
+ if orig_vert.is_equal_approx(vert):
+ normal_list[tri_index] += normal_list[vertex_index]
- new_tri_index += 3
+ vertex_index += 1
func place_objects(num_objects: int, object: PackedScene):
var space = get_world_3d().direct_space_state
@@ -95,14 +84,12 @@ func _ready():
gen_map_grid()
var vertices = PackedVector3Array()
var normals = PackedVector3Array()
- var triangles = PackedInt32Array()
# Initialize the ArrayMesh.
var arr_mesh = ArrayMesh.new()
var arrays = []
arrays.resize(Mesh.ARRAY_MAX)
arrays[Mesh.ARRAY_VERTEX] = vertices
arrays[Mesh.ARRAY_NORMAL] = normals
- arrays[Mesh.ARRAY_INDEX] = triangles
var cell = MarchingCubes.Cell.new()
@@ -127,31 +114,29 @@ func _ready():
cell.value[i] = map_grid_get(grid[i][0], grid[i][1], grid[i][2])
cell.position[i] = Vector3(grid[i][0]*scale_x, grid[i][1]*scale_y, grid[i][2]*scale_z)
- MarchingCubes.march_cube(cell, triangles, vertices)
+ MarchingCubes.march_cube(cell, vertices)
var tri_index = 0
var normal_list: Array[Vector4] = []
normal_list.resize(len(vertices))
normal_list.fill(Vector4(0,0,0,0))
- while tri_index < len(triangles):
- var p1 = vertices[triangles[tri_index + 0]]
- var p2 = vertices[triangles[tri_index + 1]]
- var p3 = vertices[triangles[tri_index + 2]]
+ while tri_index < len(vertices):
+ var p1 = vertices[tri_index + 0]
+ var p2 = vertices[tri_index + 1]
+ var p3 = vertices[tri_index + 2]
var normal = (p2 - p3).cross(p1 - p3).normalized();
var normal_4 = Vector4(normal.x, normal.y, normal.z, 1)
- normal_list[triangles[tri_index + 0]] += normal_4
- normal_list[triangles[tri_index + 1]] += normal_4
- normal_list[triangles[tri_index + 2]] += normal_4
+ normal_list[tri_index + 0] += normal_4
+ normal_list[tri_index + 1] += normal_4
+ normal_list[tri_index + 2] += normal_4
tri_index += 3
- if smooth_normal:
- tri_index = 0
- while tri_index < len(triangles):
- add_normal(tri_index+0, triangles, vertices, normal_list)
- add_normal(tri_index+1, triangles, vertices, normal_list)
- add_normal(tri_index+2, triangles, vertices, normal_list)
- tri_index += 3
+ if smooth_normal:
+ var vert_index = 0
+ while vert_index < len(vertices):
+ add_normal(vert_index, vertices, normal_list)
+ vert_index += 1
for normal in normal_list:
var real_normal = Vector3(normal.x, normal.y, normal.z) / normal.w
diff --git a/scripts/marching_cubes.gd b/scripts/marching_cubes.gd
index 6c5296c..cbf278b 100644
--- a/scripts/marching_cubes.gd
+++ b/scripts/marching_cubes.gd
@@ -12,14 +12,12 @@ class Cell:
position.resize(8)
value.resize(8)
-func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector3Array):
+func march_cube(cell: Cell, verticies: PackedVector3Array):
var cube_index : int = 0
var vertex_list: Array[Vector3] = []
vertex_list.resize(12)
- var starting_vertex_count = verticies.size()
-
for i in 8:
if cell.value[i] < 0: cube_index |= (1 << i)
@@ -66,16 +64,12 @@ func march_cube(cell: Cell, triangles: PackedInt32Array, verticies: PackedVector
local_remap[mc._tri_table[cube_index][tri_index]] = new_vertex_count
new_vertex_count += 1
tri_index += 1
-
- for i in new_vertex_count:
- var vert = new_vertex_list[i]
- verticies.push_back(vert)
tri_index = 0
while mc._tri_table[cube_index][tri_index] != -1:
- triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 0]] + starting_vertex_count)
- triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 1]] + starting_vertex_count)
- triangles.push_back(local_remap[mc._tri_table[cube_index][tri_index + 2]] + starting_vertex_count)
+ verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 0]]])
+ verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 1]]])
+ verticies.push_back(new_vertex_list[local_remap[mc._tri_table[cube_index][tri_index + 2]]])
tri_index += 3
diff --git a/scripts/player.gd b/scripts/player.gd
new file mode 100644
index 0000000..49bf1b7
--- /dev/null
+++ b/scripts/player.gd
@@ -0,0 +1,39 @@
+extends CharacterBody3D
+
+
+const SPEED = 5.0
+const JUMP_VELOCITY = 4.5
+
+# Get the gravity from the project settings to be synced with RigidBody nodes.
+var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
+
+func _ready():
+ Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
+ $ThirdPersonCamera.mouse_follow = true
+
+func _physics_process(delta):
+ # Add the gravity.
+ if not is_on_floor():
+ velocity.y -= gravity * delta
+
+ # Handle Jump.
+ if Input.is_action_just_pressed("ui_accept") and is_on_floor():
+ velocity.y = JUMP_VELOCITY
+
+ var fwd = $ThirdPersonCamera.get_front_direction()
+ var right = $ThirdPersonCamera.get_right_direction()
+
+ look_at(position + fwd, Vector3.UP)
+
+ # Get the input direction and handle the movement/deceleration.
+ # As good practice, you should replace UI actions with custom gameplay actions.
+ var input_dir = Input.get_vector("ui_left", "ui_right", "ui_down", "ui_up")
+ var direction = (input_dir.y * fwd + input_dir.x * right).normalized()
+ if direction:
+ velocity.x = direction.x * SPEED
+ velocity.z = direction.z * SPEED
+ else:
+ velocity.x = move_toward(velocity.x, 0, SPEED)
+ velocity.z = move_toward(velocity.z, 0, SPEED)
+
+ move_and_slide()