summaryrefslogtreecommitdiff
path: root/chunk.gd
blob: 8dcd416833f0506c1a288138258942f7b0e712c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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()