diff options
author | dusoleil <howcansocksbereal@gmail.com> | 2022-09-19 06:28:21 -0400 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2022-09-19 06:28:21 -0400 |
commit | 9bc2a0767e15d4dde7fd6231b07882d6456af71e (patch) | |
tree | 1cc9b88eadf86c7df16ed01418cf64d0c8b92d3b /world | |
parent | 354287265bda1448935c05e76455cff18d57f5da (diff) | |
download | game_jam49-9bc2a0767e15d4dde7fd6231b07882d6456af71e.tar.gz game_jam49-9bc2a0767e15d4dde7fd6231b07882d6456af71e.zip |
Use MultiMesh to dramatically increase performance of generated world
Diffstat (limited to 'world')
-rw-r--r-- | world/World.gd | 2 | ||||
-rw-r--r-- | world/chunk/Chunk.gd | 1 | ||||
-rw-r--r-- | world/chunk/ChunkGen.gd | 36 | ||||
-rw-r--r-- | world/chunk/ChunkLoader.gd | 13 | ||||
-rw-r--r-- | world/obstacles/Rock.tscn | 33 | ||||
-rw-r--r-- | world/obstacles/Rock_mat.tres | 21 | ||||
-rw-r--r-- | world/obstacles/Rock_mesh.tres | 4 |
7 files changed, 66 insertions, 44 deletions
diff --git a/world/World.gd b/world/World.gd index af2ba6a..2c8d822 100644 --- a/world/World.gd +++ b/world/World.gd @@ -33,7 +33,7 @@ func _on_ChunkRenderClose_area_entered(area:Area): area.lod_update() func _on_ChunkRenderDistance_area_exited(area:Area): - area.queue_free() + ChunkLoader.free_chunk(chunk_coords(v2_coords(area.transform.origin))) func _on_ChunkRenderMid_area_exited(area:Area): area.lod = area.LOD.DISTANCE diff --git a/world/chunk/Chunk.gd b/world/chunk/Chunk.gd index 7c5c1c3..1860c84 100644 --- a/world/chunk/Chunk.gd +++ b/world/chunk/Chunk.gd @@ -4,6 +4,7 @@ enum LOD {DISTANCE,MID,CLOSE} var lod = -1 func lod_update(): + return for obj in $"gen_tree".get_children(): match lod: LOD.CLOSE: diff --git a/world/chunk/ChunkGen.gd b/world/chunk/ChunkGen.gd index d60b2e5..75d3736 100644 --- a/world/chunk/ChunkGen.gd +++ b/world/chunk/ChunkGen.gd @@ -12,8 +12,9 @@ onready var rng = RandomNumberGenerator.new() var _seed func _ready(): - randomize() - self._seed = randi() + #randomize() + #self._seed = randi() + self._seed = 0 self.noise.seed = self._seed self.noise.seed = 0 self.noise.period = 16 @@ -27,7 +28,7 @@ func gen_chunk(chunk): var gen_tree = Spatial.new() gen_tree.name = "gen_tree" chunk.add_child(gen_tree) - iterate_chunk(chunk, Vector2(10.0,10.0), funcref(self,"gen_rocks")) + gen_rocks(chunk) func v2_coords(coords:Vector3): return Vector2(coords.x,coords.z) @@ -41,14 +42,9 @@ func iterate_chunk(chunk,step:Vector2,cb:FuncRef): for y in range(-chunk_size_rounded.y,chunk_size_rounded.y,step.y): cb.call_func(chunk,Vector2(x,y)) -onready var Rock = preload("res://world/obstacles/Rock.tscn") -const rock_size = 5.0 -func make_rock(chunk,xform:Transform): - var rock = Rock.instance() - rock.transform = xform - chunk.get_node("gen_tree").add_child(rock) - -func gen_rocks(chunk,chunk_coords:Vector2): +var rock_mesh = preload("res://world/obstacles/Rock_mesh.tres") +var rock_mat = preload("res://world/obstacles/Rock_mat.tres") +func make_rock(chunk,chunk_coords:Vector2): var coords = v3_coords(chunk_coords) var basis = Basis() var world_coords = chunk_coords + v2_coords(chunk.transform.origin) @@ -64,4 +60,20 @@ func gen_rocks(chunk,chunk_coords:Vector2): noise_val = lerp(1.0,4.0,noise_val) basis = basis.scaled(Vector3.ONE*noise_val) coords.y += (noise_val*5.0)-5.0 - make_rock(chunk,Transform(basis,coords)) + var gen_tree = chunk.get_node("gen_tree") + var rocks = gen_tree.get_node("rocks").multimesh + rocks.visible_instance_count += 1 + rocks.set_instance_transform(rocks.visible_instance_count-1, Transform(basis,coords)) + +func gen_rocks(chunk): + var multi_inst = MultiMeshInstance.new() + multi_inst.name = "rocks" + var multi = MultiMesh.new() + multi_inst.multimesh = multi + chunk.get_node("gen_tree").add_child(multi_inst) + multi.transform_format = MultiMesh.TRANSFORM_3D + multi.instance_count = 1000 + multi.visible_instance_count = 0 + multi.mesh = rock_mesh + rock_mesh.material = rock_mat + iterate_chunk(chunk, Vector2(10.0,10.0), funcref(self,"make_rock")) diff --git a/world/chunk/ChunkLoader.gd b/world/chunk/ChunkLoader.gd index 9f1d9b3..2e01bca 100644 --- a/world/chunk/ChunkLoader.gd +++ b/world/chunk/ChunkLoader.gd @@ -24,6 +24,14 @@ func _exit_tree(): func add_chunk(coords:Vector2): chunks[coords] = coords.length() +func free_chunk(coords:Vector2): + if !loaded_chunks.has(coords): + return + var c = loaded_chunks[coords].get_ref() + if c != null: + c.queue_free() + loaded_chunks.erase(coords) + func chunk_update(): var closest_unloaded_chunk = null var closest_len = 0.0 @@ -40,10 +48,7 @@ func chunk_update(): mtx.unlock() for chunk in loaded_chunks.keys(): if !chunks.has(chunk): - var c = loaded_chunks[chunk].get_ref() - if c != null: - c.queue_free() - loaded_chunks.erase(chunk) + free_chunk(chunk) chunks = {} func chunk_loader(): diff --git a/world/obstacles/Rock.tscn b/world/obstacles/Rock.tscn index 3b50ff3..819e9a8 100644 --- a/world/obstacles/Rock.tscn +++ b/world/obstacles/Rock.tscn @@ -1,34 +1,13 @@ -[gd_scene load_steps=10 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://world/obstacles/Rock.gd" type="Script" id=1] [ext_resource path="res://world/chunk/helper/ChunkedMeshInstance.gd" type="Script" id=2] +[ext_resource path="res://world/obstacles/Rock_mesh.tres" type="PrismMesh" id=3] +[ext_resource path="res://world/obstacles/Rock_mat.tres" type="Material" id=4] [sub_resource type="ConvexPolygonShape" id=2] points = PoolVector3Array( 2.5, -5, 2.5, 2.5, -5, -2.5, -2.5, -5, -2.5, -2.5, -5, 2.5, 0, 5, 2.5, 0, 5, -2.5 ) -[sub_resource type="OpenSimplexNoise" id=3] -octaves = 4 -period = 32.0 - -[sub_resource type="NoiseTexture" id=4] -noise = SubResource( 3 ) - -[sub_resource type="NoiseTexture" id=5] -seamless = true -as_normalmap = true -bump_strength = 32.0 -noise = SubResource( 3 ) - -[sub_resource type="SpatialMaterial" id=6] -albedo_color = Color( 0.231373, 0.231373, 0.231373, 1 ) -albedo_texture = SubResource( 4 ) -normal_enabled = true -normal_scale = 1.0 -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 ) @@ -42,12 +21,12 @@ shape = SubResource( 2 ) [node name="MeshInstance" type="MeshInstance" parent="."] visible = false -material_override = SubResource( 6 ) +material_override = ExtResource( 4 ) script = ExtResource( 2 ) -_mesh = SubResource( 1 ) +_mesh = ExtResource( 3 ) [node name="MeshInstance2" type="MeshInstance" parent="."] visible = false -material_override = SubResource( 6 ) +material_override = ExtResource( 4 ) script = ExtResource( 2 ) _mesh = SubResource( 9 ) diff --git a/world/obstacles/Rock_mat.tres b/world/obstacles/Rock_mat.tres new file mode 100644 index 0000000..8547487 --- /dev/null +++ b/world/obstacles/Rock_mat.tres @@ -0,0 +1,21 @@ +[gd_resource type="SpatialMaterial" load_steps=4 format=2] + +[sub_resource type="OpenSimplexNoise" id=3] +octaves = 4 +period = 32.0 + +[sub_resource type="NoiseTexture" id=4] +noise = SubResource( 3 ) + +[sub_resource type="NoiseTexture" id=5] +seamless = true +as_normalmap = true +bump_strength = 32.0 +noise = SubResource( 3 ) + +[resource] +albedo_color = Color( 0.231373, 0.231373, 0.231373, 1 ) +albedo_texture = SubResource( 4 ) +normal_enabled = true +normal_scale = 1.0 +normal_texture = SubResource( 5 ) diff --git a/world/obstacles/Rock_mesh.tres b/world/obstacles/Rock_mesh.tres new file mode 100644 index 0000000..bced5b6 --- /dev/null +++ b/world/obstacles/Rock_mesh.tres @@ -0,0 +1,4 @@ +[gd_resource type="PrismMesh" format=2] + +[resource] +size = Vector3( 5, 10, 5 ) |