Camera mouse look and trees

This commit is contained in:
2024-03-06 20:59:34 -05:00
parent ec869732ba
commit 2037197774
31 changed files with 450768 additions and 27 deletions

View File

@ -4,7 +4,7 @@ extends Resource
class_name PlanetData
@export var radius : float = 50:
@export var radius : float = 2000:
get:
return radius
set(value):

View File

@ -27,7 +27,7 @@ var material = load("res://PlanetColoring.tres")
var center_of_mass : Vector3
const THRESHOLDS = [512*512, 128*128, 64*64, 32*32]
const THRESHOLDS = [INF, 4096*4096, 2048*2048, 1024*1024, 512*512, 256*256, 128*128]
const RESOLUTION = 17
func split(planet_data : PlanetData):
@ -80,7 +80,7 @@ func update_from_camera_pos(planet_data : PlanetData, camera : Camera3D = null):
if (self.level < (THRESHOLDS.size() - 1) and dist < THRESHOLDS[self.level] and self.is_leaf):
split(planet_data)
elif (dist > THRESHOLDS[self.level] + 4 and not self.is_leaf):
elif (dist > THRESHOLDS[self.level] + THRESHOLDS[self.level]/2 and not self.is_leaf):
join()
regenerate_mesh(planet_data)
@ -110,6 +110,7 @@ func regenerate_mesh(planet_data):
var b_axis : Vector3 = normal.cross(a_axis)
for y in range(RESOLUTION):
for x in range(RESOLUTION):
var i : int = x + y*RESOLUTION
# Relative position of the current point inside the tile
@ -130,7 +131,16 @@ func regenerate_mesh(planet_data):
# Calculate center of mass of current tile.
if (percent.x == 0.5 and percent.y == 0.5):
center_of_mass = point_on_planet
# Generate surface objects
if (level == THRESHOLDS.size()-1 and elevation > planet_data.radius and randf() > 0.98 and elevation < planet_data.radius*1.02):
# Create a new instance of a tree
var test_tree = load("res://EvergreenTree.tscn").instantiate()
var tree_axis_a = Vector3(point_on_sphere.y, point_on_sphere.z, point_on_sphere.x).normalized()
var tree_axis_b = point_on_sphere.cross(tree_axis_a).normalized()
test_tree.transform = Transform3D(tree_axis_b/2, point_on_sphere/2, tree_axis_a/2, point_on_planet)
call_deferred("add_child", test_tree)
vertex_array[i] = point_on_planet
if x != RESOLUTION-1 and y != RESOLUTION-1:
index_array[tri_index + 2] = i
@ -175,6 +185,14 @@ func _update_mesh(arrays : Array, planet_data):
return
mesh_instance.mesh = _mesh
if (level == THRESHOLDS.size()-1):
mesh_instance.create_trimesh_collision()
# DEBUG SHADING
#mesh_instance.material_override = StandardMaterial3D.new()
#mesh_instance.material_override.albedo_color = Color.from_hsv(float(level)/(THRESHOLDS.size()-1), 1, 1)
mesh_instance.material_override = material
mesh_instance.material_override.set_shader_parameter("min_elevation", planet_data.min_elevation - 0.2)
mesh_instance.material_override.set_shader_parameter("max_elevation", planet_data.max_elevation)

View File

@ -1,14 +1,14 @@
extends Camera3D
extends CharacterBody3D
@export var velocity: Vector3
@export var acceleration: Vector3
var g : float = 0.01
@export var mouse_sensitivity : float = 0.01
var g : float = 0.006
# Called when the node enters the scene tree for the first time.
func _ready():
global_position = Vector3(0,0,-150)
global_position = Vector3(0,0,-4000)
acceleration = -global_position.normalized()
velocity = Vector3(0.5, 0.5, 0)
velocity = Vector3(1.3, 1.8, 0)
# Called every frame. 'delta' is the elapsed time since the previous frame.
@ -17,4 +17,11 @@ func _process(delta):
velocity += acceleration
global_translate(velocity)
Global.camera_position = global_position
look_at(Vector3(0,0,0))
print(get_slide_collision_count())
func _unhandled_input(event):
if event is InputEventMouseMotion:
rotate_y(-event.relative.x * mouse_sensitivity)
$Pivot.rotate_x(-event.relative.y * mouse_sensitivity)
$Pivot.rotation.x = clamp($Pivot.rotation.x, -1.2, 1.2)