Drone as CharacterBody3D

This commit is contained in:
2025-02-02 14:01:03 -05:00
parent 5f6012696b
commit 1550f57f5d
207 changed files with 4397 additions and 471 deletions

112
core/drone/drone.gd Normal file
View File

@@ -0,0 +1,112 @@
extends CharacterBody3D
# Local coords to axis
# Yaw = Y
# Roll = Z
# Pitch = X
@export_category("Parameters")
@export var flight_modes: Array = ["normal", "acro"]
@export var armed: bool = false
@export var throttle_speed: float = 0.0
@export var rotation_speed: float = 1.0
@export var acceleration: float = 10.0
@export var max_speed: float = 10.0
@export var max_rotation_speed: float = 10.0
@export var camera_tilt_angle: float = 0.0
@export var gravity: float = 9.8
@export_category("Node References")
@export var camera: Camera3D
@export var anim_player: AnimationPlayer
var animation_initalized: bool = false
var using_joy_controller: bool = true
func _ready():
pass
func handle_throttle(throttle_input: float, delta):
if using_joy_controller:
if throttle_input > 0:
throttle_speed = throttle_input * 100
var desired_vel = velocity.y + throttle_speed * 0.5
velocity.y = lerp(velocity.y, desired_vel, delta)
else:
pass
# Speed scale adjusted for animation
anim_player.speed_scale = throttle_input * 4
anim_player.speed_scale = clampf(anim_player.speed_scale, 1.0, 4.0)
velocity.x = clampf(velocity.x, -max_speed, max_speed)
velocity.y = clampf(velocity.y, -100.0, max_speed)
velocity.z = clampf(velocity.z, -max_speed, max_speed)
#print("anim speed: ", anim_player.speed_scale) # DEBUG
#print("throttle speed: ", throttle_speed) # DEBUG
func handle_yaw(yaw_input: float, delta):
#rotate_y(rotation_speed * yaw_input * delta)
#global_rotate(Vector3.UP, yaw_input * rotation_speed * delta)
transform.basis = transform.basis.rotated(Vector3.UP, yaw_input * delta)
func handle_roll(roll_input: float, delta):
if !is_on_floor():
transform.basis = transform.basis.rotated(Vector3.BACK, roll_input * delta)
func handle_pitch(pitch_input: float, delta):
if !is_on_floor():
transform.basis = transform.basis.rotated(Vector3.RIGHT, pitch_input * delta)
func handle_arming():
if !armed:
if !animation_initalized:
anim_player.play("spin_props")
animation_initalized = true
elif animation_initalized:
anim_player.play()
armed = !armed
print("Drone armed")
elif armed:
anim_player.pause()
armed = !armed
print("Drone disarmed")
func _physics_process(delta):
# Gravity
if !is_on_floor():
velocity.y -= gravity * delta
else:
global_rotation.x = lerpf(global_rotation.x, 0.0, delta * 20)
global_rotation.z = lerpf(global_rotation.z, 0.0, delta * 20)
if Input.is_action_just_pressed("arm"):
handle_arming()
if armed:
if Input.is_action_pressed("throttle_down") or Input.is_action_pressed("throttle_up"):
var throttle_input = Input.get_axis("throttle_down", "throttle_up")
handle_throttle(throttle_input, delta)
if Input.is_action_pressed("yaw_left") or Input.is_action_pressed("yaw_right"):
var yaw_input = Input.get_axis("yaw_right", "yaw_left")
handle_yaw(yaw_input, delta)
if Input.is_action_pressed("roll_left") or Input.is_action_pressed("roll_right"):
var roll_input = Input.get_axis("roll_left", "roll_right")
handle_roll(roll_input, delta)
if Input.is_action_pressed("pitch_backward") or Input.is_action_pressed("pitch_forward"):
var pitch_input = Input.get_axis("pitch_backward", "pitch_forward")
handle_pitch(pitch_input, delta)
print("velocity: ", velocity)
move_and_slide()

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,23 @@
[gd_scene load_steps=5 format=3 uid="uid://bfhw7682gnh22"]
[ext_resource type="Texture2D" uid="uid://qbn2lsddorgb" path="res://assets/textures/Kenny's Prototype Textures PNG/Dark/texture_black (7).png" id="1_dly02"]
[sub_resource type="WorldBoundaryShape3D" id="WorldBoundaryShape3D_bpckr"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_qslih"]
albedo_texture = ExtResource("1_dly02")
uv1_triplanar = true
[sub_resource type="PlaneMesh" id="PlaneMesh_hj83g"]
material = SubResource("StandardMaterial3D_qslih")
size = Vector2(100, 100)
[node name="TestLevel" type="Node3D"]
[node name="StaticBody3D" type="StaticBody3D" parent="."]
[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"]
shape = SubResource("WorldBoundaryShape3D_bpckr")
[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D"]
mesh = SubResource("PlaneMesh_hj83g")

View File

@@ -1,3 +1,31 @@
[gd_scene format=3 uid="uid://deah8x3tnm045"]
[gd_scene load_steps=6 format=3 uid="uid://deah8x3tnm045"]
[ext_resource type="PackedScene" uid="uid://bfhw7682gnh22" path="res://core/levels/test_level.tscn" id="1_ylfl7"]
[ext_resource type="PackedScene" uid="uid://dwvmna8qc0vb4" path="res://core/drone/drone.tscn" id="2_orkyq"]
[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_rtxf7"]
sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1)
ground_horizon_color = Color(0.64625, 0.65575, 0.67075, 1)
[sub_resource type="Sky" id="Sky_dsv6k"]
sky_material = SubResource("ProceduralSkyMaterial_rtxf7")
[sub_resource type="Environment" id="Environment_ruppb"]
background_mode = 2
sky = SubResource("Sky_dsv6k")
tonemap_mode = 2
glow_enabled = true
[node name="Main" type="Node3D"]
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(-0.866023, -0.433016, 0.250001, 0, 0.499998, 0.866027, -0.500003, 0.749999, -0.43301, 0, 0, 0)
shadow_enabled = true
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
environment = SubResource("Environment_ruppb")
[node name="TestLevel" parent="." instance=ExtResource("1_ylfl7")]
[node name="Drone" parent="." instance=ExtResource("2_orkyq")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.017014, 0)

View File

@@ -0,0 +1,9 @@
[gd_resource type="StandardMaterial3D" load_steps=2 format=3 uid="uid://d0gkxc5fuh354"]
[ext_resource type="Texture2D" uid="uid://c2qrve7v33f01" path="res://assets/textures/Kenny's Prototype Textures PNG/Dark/texture_black (1).png" id="1_6mcyk"]
[resource]
albedo_texture = ExtResource("1_6mcyk")
roughness = 0.5
uv1_scale = Vector3(0.15, 0.15, 0.15)
uv1_triplanar = true

View File

@@ -0,0 +1,6 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://cyyhyowgl74ic"]
[resource]
albedo_color = Color(0.28485, 0.28485, 0.28485, 1)
metallic = 1.0
roughness = 0.8

View File

@@ -0,0 +1,6 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://bs525pbcpqua6"]
[resource]
albedo_color = Color(0.298895, 0.980368, 0.360663, 1)
metallic_specular = 0.8
roughness = 0.6

View File

@@ -0,0 +1,23 @@
[gd_resource type="StandardMaterial3D" load_steps=4 format=3 uid="uid://uy2txkh3slat"]
[sub_resource type="Gradient" id="Gradient_nwimj"]
offsets = PackedFloat32Array(0.285714, 0.857143)
colors = PackedColorArray(0.46918, 0.233464, 0.330482, 1, 1, 1, 1, 1)
[sub_resource type="FastNoiseLite" id="FastNoiseLite_symyh"]
noise_type = 0
frequency = 0.0028
fractal_type = 3
fractal_octaves = 10
[sub_resource type="NoiseTexture2D" id="NoiseTexture2D_6qupf"]
seamless = true
color_ramp = SubResource("Gradient_nwimj")
noise = SubResource("FastNoiseLite_symyh")
[resource]
albedo_color = Color(0.57906, 0.301518, 0.414739, 1)
albedo_texture = SubResource("NoiseTexture2D_6qupf")
metallic = 1.0
roughness = 0.29
uv1_triplanar = true

View File

@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://b2okmc5xyxhkm"]
[resource]
transparency = 1
diffuse_mode = 2
albedo_color = Color(0.141176, 0.239216, 0.0862745, 0.784314)
metallic_specular = 0.6
roughness = 0.4

View File

@@ -0,0 +1,6 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://dpaqrdyltu0hj"]
[resource]
transparency = 1
albedo_color = Color(0.898039, 0.392157, 0.258824, 0.784314)
roughness = 0.4

View File

@@ -0,0 +1,6 @@
[gd_resource type="StandardMaterial3D" format=3 uid="uid://is2nkfruok8v"]
[resource]
albedo_color = Color(0.807964, 0.6722, 0.473427, 1)
metallic_specular = 0.8
roughness = 0.6

View File

@@ -0,0 +1,90 @@
/* License CC BY-NC-SA 4.0 Deed */
/* https://creativecommons.org/licenses/by-nc-sa/4.0/ */
/* Fork of Ryk's VCR distortion shader */
/* https://www.shadertoy.com/view/ldjGzV */
shader_type canvas_item;
uniform sampler2D screen_texture: hint_screen_texture, filter_linear_mipmap, repeat_disable;
group_uniforms Image;
uniform float curvature: hint_range(0., 10., .01) = 2.;
uniform float skip: hint_range(0., 1., .01) = 1.;
uniform float image_flicker: hint_range(0., 1., .01) = 1.;
group_uniforms Vignette;
uniform float vignette_flicker_speed: hint_range(0., 2., .01) = 1.;
uniform float vignette_strength: hint_range(0., 2., 0.01) = 1.;
group_uniforms Scanlines;
uniform float small_scanlines_speed: hint_range(0., 10., .01) = 1.;
uniform float small_scanlines_proximity: hint_range(.01, 2., .01) = 1.;
uniform float small_scanlines_opacity: hint_range(0.01, 5., .01) = 1.;
uniform float scanlines_opacity: hint_range(0., 2., .01) = 1.;
uniform float scanlines_speed: hint_range(0., 5., .01) = 1.;
uniform float scanline_thickness: hint_range(0., .6, .01) = 0.5;
uniform float scanlines_spacing: hint_range(0.3, 3., .01) = 1.;
group_uniforms Noise;
uniform sampler2D noise_texture: filter_linear_mipmap, repeat_enable;
float noise(vec2 p, vec2 uv)
{
float s = texture(noise_texture,vec2(1.*TIME,2.*TIME)*8. + p*1.).x;
s *= s;
return s;
}
float onOff(float a, float b, float c)
{
return step(c, sin(TIME + a*cos(TIME*b)));
}
float ramp(float y, float start, float end)
{
float inside = step(start,y) - step(end,y);
float fact = (y-start)/(end-start)*inside;
return (1.-fact) * inside;
}
float stripes(vec2 uv)
{
float noi = noise(uv*vec2(0.5,1.) + vec2(1.,3.), uv)*scanlines_opacity;
return ramp(mod(uv.y*4.*scanlines_spacing + TIME*scanlines_speed/(2.*scanlines_spacing)+sin(TIME*scanlines_speed + sin(TIME*scanlines_speed*0.63*scanlines_spacing)),1.),scanline_thickness,.6)*noi;
}
vec3 getVideo(vec2 uv)
{
vec2 look = uv;
float window = 1./(1.+20.*(look.y-mod(TIME/4.,1.))*(look.y-mod(TIME/4.,1.)))*image_flicker;
look.x = look.x + sin(look.y*10. + TIME)/50.*onOff(4.,4.,.3)*(1.+cos(TIME*80.))*window;
float vShift = 0.4*onOff(2.,3.,.9)*(sin(TIME)*sin(TIME*20.)+(0.5 + 0.1*sin(TIME*200.)*cos(TIME)))*skip;
look.y = mod(look.y + vShift, 1.);
vec3 video = texture(screen_texture,look).xyz;
return video;
}
vec2 screenDistort(vec2 uv)
{
uv -= vec2(.5,.5);
uv = uv*1.2*(1./1.2+curvature*uv.x*uv.x*uv.y*uv.y);
uv += vec2(.5,.5);
return uv;
}
void fragment()
{
vec2 uv = FRAGCOORD.xy / (1.0 / SCREEN_PIXEL_SIZE).xy;
uv = screenDistort(uv);
vec3 video = getVideo(uv);
float vigAmt = 3.+.3*sin(TIME*vignette_flicker_speed+1. + 5.*cos(TIME*5.*vignette_flicker_speed+1.));
vigAmt *= vignette_strength;
float vignette = (1.-vigAmt*(uv.y-.5)*(uv.y-.5))*(1.-vigAmt*(uv.x-.5)*(uv.x-.5));
video += stripes(uv);
video += noise(uv*2., uv)/2.;
video *= vignette;
video *= (12./small_scanlines_opacity+mod(uv.y*30.*small_scanlines_proximity+TIME*small_scanlines_speed,1.))/13.*small_scanlines_opacity;
COLOR = vec4(video,1.0);
}