From 1a785420ec47c78ac3f95bd0ece08c819653f6e8 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Tue, 19 Nov 2024 23:13:23 +0100 Subject: next commit --- chunk.gd | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 chunk.gd (limited to 'chunk.gd') 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() -- cgit v1.2.3