diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-11-19 23:13:23 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-11-19 23:13:23 +0100 |
commit | 1a785420ec47c78ac3f95bd0ece08c819653f6e8 (patch) | |
tree | 92ca77abfe9f775f333edc47bf85a0652884840f /chunk.gd | |
parent | 257127aa7f0fbb66ddd2600dd3d6a6aca60e56b8 (diff) |
Diffstat (limited to 'chunk.gd')
-rw-r--r-- | chunk.gd | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/chunk.gd b/chunk.gd new file mode 100644 index 0000000..8dcd416 --- /dev/null +++ b/chunk.gd @@ -0,0 +1,90 @@ +class_name Chunk +extends Node + + +var chunk_size := Vector3(16, 16, 16) + +@export var noise: FastNoiseLite + +var blocks: Array[Block] = [] + + +func generate_chunk(row, column, depth): + #var material := StandardMaterial3D.new() + #material.albedo_texture = preload("res://new_atlas_texture.tres") + #material.vertex_color_use_as_albedo = true + var material := preload("res://new_shader_material.tres") + var st = SurfaceTool.new() + st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.set_material(material) + + var blocks = [] + + for x in chunk_size.x: + #blocks[x] = [] + blocks.append([]) + for y in chunk_size.y: + #blocks[x][y] = [] + blocks[x].append([]) + for z in chunk_size.z: + var block_position = Vector3( + row*chunk_size.x + x, + column*chunk_size.y + y, + depth*chunk_size.z + z + #x, + #y, + #z + ) * 2 + var value = noise.get_noise_3d(block_position.x, block_position.y, block_position.z) * 100 + + var type = Block.Type.AIR + if value > -15: + type = Block.Type.GRASS + elif value > -20: + type = Block.Type.STONE + + var block = Block.new() + block.position = block_position + block.type = type + + blocks[x][y].append(block) + + for x in blocks.size(): + for y in blocks[x].size(): + for z in blocks[x][y].size(): + var block = blocks[x][y][z] + var block_position = block.position + + if not block.type == Block.Type.AIR: + if block.type == Block.Type.GRASS and blocks[x].size() > y + 1 and not blocks[x][y + 1][z].type == Block.Type.AIR: + block.type = Block.Type.DIRT + + var rng = RandomNumberGenerator.new() + rng.seed = randi() + st.set_color(Color(rng.randf(), rng.randf(), rng.randf())) + + st.set_uv(Vector2(0, 0)) + + if (blocks[x][y].size() > z+1 and blocks[x][y][z + 1].type == Block.Type.AIR) or blocks[x][y].size() == z+1: + block.add_face(st, Block.Face.FRONT, block) + + if (blocks.size() > x+1 and blocks[x + 1][y][z].type == Block.Type.AIR) or blocks.size() == x+1: + block.add_face(st, Block.Face.RIGHT) + + if blocks[x][y].size() > z-1 and (blocks[x][y][z - 1].type == Block.Type.AIR or z == 0): + block.add_face(st, Block.Face.BACK) + + if blocks.size() > x-1 and (blocks[x - 1][y][z].type == Block.Type.AIR or x == 0): + block.add_face(st, Block.Face.LEFT) + + if (blocks[x].size() > y+1 and blocks[x][y + 1][z].type == Block.Type.AIR) or blocks[x].size() == y+1: + block.add_face(st, Block.Face.TOP) + + if blocks[x].size() > y-1 and (blocks[x][y - 1][z].type == Block.Type.AIR or y == 0): + block.add_face(st, Block.Face.BOTTOM) + + + #st.generate_normals() + var mesh = MeshInstance3D.new() + mesh.mesh = st.commit() + mesh.create_trimesh_collision() |