From ffc92ed12c6e680776d355ff9fcef70e5e28d7c8 Mon Sep 17 00:00:00 2001 From: dusoleil Date: Sun, 18 Sep 2022 08:32:29 -0400 Subject: Prototype levels of detail per chunk --- Chunk.gd | 16 ++++++++++++++++ Chunk.tscn | 2 ++ Rock.gd | 18 ++++++++++++++++++ Rock.tscn | 14 +++++++++++++- World.gd | 29 ++++++++++++++++++++++++++++- World.tscn | 27 ++++++++++++++++++++++++++- 6 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 Chunk.gd create mode 100644 Rock.gd diff --git a/Chunk.gd b/Chunk.gd new file mode 100644 index 0000000..7c5c1c3 --- /dev/null +++ b/Chunk.gd @@ -0,0 +1,16 @@ +extends Area + +enum LOD {DISTANCE,MID,CLOSE} +var lod = -1 + +func lod_update(): + for obj in $"gen_tree".get_children(): + match lod: + LOD.CLOSE: + obj.lod_close() + LOD.MID: + obj.lod_mid() + LOD.DISTANCE: + obj.lod_distance() + _: + obj.lod_distance() diff --git a/Chunk.tscn b/Chunk.tscn index 0437a7e..8f5ea84 100644 --- a/Chunk.tscn +++ b/Chunk.tscn @@ -1,5 +1,6 @@ [gd_scene load_steps=7 format=2] +[ext_resource path="res://Chunk.gd" type="Script" id=1] [ext_resource path="res://ChunkedCSGMesh.gd" type="Script" id=3] [sub_resource type="SphereShape" id=1] @@ -19,6 +20,7 @@ size = Vector2( 98, 98 ) [node name="Chunk" type="Area"] collision_layer = 32 collision_mask = 0 +script = ExtResource( 1 ) [node name="Size" type="CollisionShape" parent="."] shape = SubResource( 1 ) diff --git a/Rock.gd b/Rock.gd new file mode 100644 index 0000000..577e626 --- /dev/null +++ b/Rock.gd @@ -0,0 +1,18 @@ +extends StaticBody + +onready var collision_enabled = self.collision_layer + +func lod_distance(): + $"MeshInstance".visible = false + $"MeshInstance2".visible = true + self.collision_layer = 0 + +func lod_mid(): + $"MeshInstance".visible = true + $"MeshInstance2".visible = false + self.collision_layer = 0 + +func lod_close(): + $"MeshInstance".visible = true + $"MeshInstance2".visible = false + self.collision_layer = collision_enabled diff --git a/Rock.tscn b/Rock.tscn index 0a80df3..f3c7316 100644 --- a/Rock.tscn +++ b/Rock.tscn @@ -1,5 +1,6 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=10 format=2] +[ext_resource path="res://Rock.gd" type="Script" id=1] [ext_resource path="res://ChunkedMeshInstance.gd" type="Script" id=2] [sub_resource type="ConvexPolygonShape" id=2] @@ -28,14 +29,25 @@ normal_texture = SubResource( 5 ) [sub_resource type="PrismMesh" id=1] size = Vector3( 5, 10, 5 ) +[sub_resource type="CubeMesh" id=9] +size = Vector3( 5, 10, 5 ) + [node name="Rock" type="StaticBody"] collision_layer = 8 collision_mask = 0 +script = ExtResource( 1 ) [node name="CollisionShape" type="CollisionShape" parent="."] shape = SubResource( 2 ) [node name="MeshInstance" type="MeshInstance" parent="."] +visible = false material_override = SubResource( 6 ) script = ExtResource( 2 ) _mesh = SubResource( 1 ) + +[node name="MeshInstance2" type="MeshInstance" parent="."] +visible = false +material_override = SubResource( 6 ) +script = ExtResource( 2 ) +_mesh = SubResource( 9 ) diff --git a/World.gd b/World.gd index 97f0a09..af2ba6a 100644 --- a/World.gd +++ b/World.gd @@ -4,7 +4,11 @@ export(NodePath) var traveler = null onready var _traveler = get_node(traveler) export var acceleration = 10.0 -onready var chunk_render_distance = $"ChunkRenderDistance/Radius".shape.radius +onready var lod_distance = $"ChunkRenderDistance" +onready var lod_mid = $"ChunkRenderMid" +onready var lod_close = $"ChunkRenderClose" + +onready var chunk_render_distance = lod_distance.get_node("Radius").shape.radius onready var chunk_half_size = $"Chunks/Chunk/Size".shape.radius onready var chunk_size = chunk_half_size * 2.0 @@ -13,9 +17,32 @@ func _ready(): ChunkLoader.world = self ChunkGen.chunk_half_size = self.chunk_half_size +func _on_ChunkRenderDistance_area_entered(area:Area): + if area.lod < area.LOD.DISTANCE: + area.lod = area.LOD.DISTANCE + area.lod_update() + +func _on_ChunkRenderMid_area_entered(area:Area): + if area.lod < area.LOD.MID: + area.lod = area.LOD.MID + area.lod_update() + +func _on_ChunkRenderClose_area_entered(area:Area): + if area.lod < area.LOD.CLOSE: + area.lod = area.LOD.CLOSE + area.lod_update() + func _on_ChunkRenderDistance_area_exited(area:Area): area.queue_free() +func _on_ChunkRenderMid_area_exited(area:Area): + area.lod = area.LOD.DISTANCE + area.lod_update() + +func _on_ChunkRenderClose_area_exited(area:Area): + area.lod = area.LOD.MID + area.lod_update() + func travel(direction:Vector3): var heading = _traveler.global_transform.basis heading = Basis(Vector3.UP*heading.get_euler().y) diff --git a/World.tscn b/World.tscn index 3bdf371..37056c5 100644 --- a/World.tscn +++ b/World.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=9 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://World.gd" type="Script" id=1] [ext_resource path="res://Chunk.tscn" type="PackedScene" id=3] @@ -6,6 +6,12 @@ [sub_resource type="SphereShape" id=24] radius = 500.0 +[sub_resource type="SphereShape" id=25] +radius = 300.0 + +[sub_resource type="SphereShape" id=26] +radius = 150.0 + [sub_resource type="BoxShape" id=23] [node name="World" type="Spatial"] @@ -19,6 +25,20 @@ collision_mask = 32 [node name="Radius" type="CollisionShape" parent="ChunkRenderDistance"] shape = SubResource( 24 ) +[node name="ChunkRenderMid" type="Area" parent="."] +collision_layer = 0 +collision_mask = 32 + +[node name="Radius" type="CollisionShape" parent="ChunkRenderMid"] +shape = SubResource( 25 ) + +[node name="ChunkRenderClose" type="Area" parent="."] +collision_layer = 0 +collision_mask = 32 + +[node name="Radius" type="CollisionShape" parent="ChunkRenderClose"] +shape = SubResource( 26 ) + [node name="Chunks" type="RigidBody" parent="."] collision_layer = 0 collision_mask = 0 @@ -36,4 +56,9 @@ shape = SubResource( 23 ) visible = false collision_layer = 0 +[connection signal="area_entered" from="ChunkRenderDistance" to="." method="_on_ChunkRenderDistance_area_entered"] [connection signal="area_exited" from="ChunkRenderDistance" to="." method="_on_ChunkRenderDistance_area_exited"] +[connection signal="area_entered" from="ChunkRenderMid" to="." method="_on_ChunkRenderMid_area_entered"] +[connection signal="area_exited" from="ChunkRenderMid" to="." method="_on_ChunkRenderMid_area_exited"] +[connection signal="area_entered" from="ChunkRenderClose" to="." method="_on_ChunkRenderClose_area_entered"] +[connection signal="area_exited" from="ChunkRenderClose" to="." method="_on_ChunkRenderClose_area_exited"] -- cgit v1.2.3