diff options
Diffstat (limited to 'world/chunk/ChunkGen.gd')
-rw-r--r-- | world/chunk/ChunkGen.gd | 87 |
1 files changed, 59 insertions, 28 deletions
diff --git a/world/chunk/ChunkGen.gd b/world/chunk/ChunkGen.gd index 75d3736..e71cc57 100644 --- a/world/chunk/ChunkGen.gd +++ b/world/chunk/ChunkGen.gd @@ -25,10 +25,18 @@ func rng_2dv(coords:Vector2): self.rng.seed = hash(coords) func gen_chunk(chunk): - var gen_tree = Spatial.new() - gen_tree.name = "gen_tree" - chunk.add_child(gen_tree) - gen_rocks(chunk) + gen_distance(chunk,chunk.get_node("gen_tree/lod_distance")) + gen_mid(chunk,chunk.get_node("gen_tree/lod_mid")) + gen_close(chunk,chunk.get_node("gen_tree/lod_close")) + +func gen_distance(chunk,tree): + gen_lowres_rocks(chunk,tree) + +func gen_mid(chunk,tree): + gen_rocks(chunk,tree) + +func gen_close(chunk,tree): + gen_rock_hitboxes(chunk,tree) func v2_coords(coords:Vector3): return Vector2(coords.x,coords.z) @@ -36,15 +44,43 @@ func v2_coords(coords:Vector3): func v3_coords(coords:Vector2): return Vector3(coords.x,0.0,coords.y) -func iterate_chunk(chunk,step:Vector2,cb:FuncRef): +func iterate_chunk(chunk,tree,step:Vector2,cb:FuncRef,data): var chunk_size_rounded = Vector2(chunk_half_size,chunk_half_size).snapped(step) for x in range(-chunk_size_rounded.x,chunk_size_rounded.x,step.x): for y in range(-chunk_size_rounded.y,chunk_size_rounded.y,step.y): - cb.call_func(chunk,Vector2(x,y)) + cb.call_func(chunk,tree,Vector2(x,y),data) + +func make_multimesh(name,cnt,mesh): + var multi_inst = MultiMeshInstance.new() + multi_inst.name = name + var multi = MultiMesh.new() + multi_inst.multimesh = multi + multi.transform_format = MultiMesh.TRANSFORM_3D + multi.instance_count = cnt + multi.visible_instance_count = 0 + multi.mesh = mesh + return multi_inst 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): +func gen_rocks(chunk,tree): + rock_mesh.material = rock_mat + var multi = make_multimesh("rocks",200,rock_mesh) + tree.add_child(multi) + iterate_chunk(chunk, multi.multimesh, Vector2(10.0,10.0), funcref(self,"rock_rng"), [0.4,0.6,1.0,4.0,1.0,funcref(self,"make_rock")]) + +var rock_lowres_mesh = preload("res://world/obstacles/Rock_lowres_mesh.tres") +func gen_lowres_rocks(chunk,tree): + rock_lowres_mesh.material = rock_mat + var multi = make_multimesh("rocks",50,rock_lowres_mesh) + tree.add_child(multi) + iterate_chunk(chunk, multi.multimesh, Vector2(20.0,20.0), funcref(self,"rock_rng"), [0.4,0.6,2.0,8.0,0.5,funcref(self,"make_rock")]) + +var rock_coll = preload("res://world/obstacles/Rock_coll.tres") +func gen_rock_hitboxes(chunk,tree): + iterate_chunk(chunk, tree, Vector2(20.0,20.0), funcref(self,"rock_rng"), [0.4,0.6,1.0,4.0,1.0,funcref(self,"make_rock_hitbox")]) + +func rock_rng(chunk,rocks,chunk_coords:Vector2,data): var coords = v3_coords(chunk_coords) var basis = Basis() var world_coords = chunk_coords + v2_coords(chunk.transform.origin) @@ -52,28 +88,23 @@ func make_rock(chunk,chunk_coords:Vector2): var rng_val = self.rng.randf_range(0.0,TAU) basis = Basis(Vector3.UP,rng_val) * basis var noise_val = self.noise.get_noise_2dv(world_coords * 0.125) - if noise_val < 0.4: + if noise_val < data[0]: return - noise_val = min(noise_val,0.6) - noise_val -= 0.4 - noise_val /= 0.2 - noise_val = lerp(1.0,4.0,noise_val) + noise_val = min(noise_val,data[1]) + noise_val -= data[0] + noise_val /= data[1] - data[0] + noise_val = lerp(data[2],data[3],noise_val) basis = basis.scaled(Vector3.ONE*noise_val) - coords.y += (noise_val*5.0)-5.0 - var gen_tree = chunk.get_node("gen_tree") - var rocks = gen_tree.get_node("rocks").multimesh + basis = basis.scaled(Vector3(1.0,data[4],1.0)) + coords.y += (noise_val*data[4]*5.0)-5.0 + data[5].call_func(rocks,Transform(basis,coords)) + +func make_rock(rocks,xform:Transform): rocks.visible_instance_count += 1 - rocks.set_instance_transform(rocks.visible_instance_count-1, Transform(basis,coords)) + rocks.set_instance_transform(rocks.visible_instance_count-1, xform) -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")) +func make_rock_hitbox(rocks,xform:Transform): + var shape = CollisionShape.new() + shape.shape = rock_coll + shape.transform = xform + rocks.add_child(shape) |