summaryrefslogtreecommitdiff
path: root/main.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-11-19 23:13:23 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-11-19 23:13:23 +0100
commit1a785420ec47c78ac3f95bd0ece08c819653f6e8 (patch)
tree92ca77abfe9f775f333edc47bf85a0652884840f /main.gd
parent257127aa7f0fbb66ddd2600dd3d6a6aca60e56b8 (diff)
next commitHEADmain
Diffstat (limited to 'main.gd')
-rw-r--r--main.gd202
1 files changed, 127 insertions, 75 deletions
diff --git a/main.gd b/main.gd
index 38ab4a1..ae29028 100644
--- a/main.gd
+++ b/main.gd
@@ -1,3 +1,4 @@
+class_name Main
extends Node3D
@@ -44,41 +45,93 @@ var block_faces = [
[Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0)],
],
]
+var block_face_uvs = [
+ [ # FRONT
+ [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
+ [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
+ ],
+ [ # BACK
+ [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
+ [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
+ ],
+ [ # LEFT
+ [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
+ [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
+ ],
+ [ # RIGHT
+ [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
+ [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
+ ],
+ [ # TOP
+ [Vector2(0.0, 1.0), Vector2(1.0, 2.0), Vector2(0.0, 2.0)],
+ [Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 2.0)],
+ ],
+ [ # BOTTOM
+ [Vector2(1.0, 0.0), Vector2(2.0, 1.0), Vector2(1.0, 1.0)],
+ [Vector2(1.0, 0.0), Vector2(2.0, 0.0), Vector2(2.0, 1.0)],
+ ],
+]
+var block_face_normals = [
+ Vector3(0, 0, 1), # FRONT
+ Vector3(0, 0, -1), # BACK
+ Vector3(-1, 0, 0), # LEFT
+ Vector3(1, 0, 0), # RIGHT
+ Vector3(0, 1, 0), # TOP
+ Vector3(0, -1, 0), # BOTTOM
+]
enum BlockType {
+ AIR,
GRASS,
+ DIRT,
+ LEAVES,
+ STONE,
}
var block_types = {
BlockType.GRASS: {
- "uvs": [
- [ # FRONT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # BACK
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # LEFT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # RIGHT
- [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)],
- [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)],
- ],
- [ # TOP
- [Vector2(0.0, 1.0), Vector2(1.0, 2.0), Vector2(0.0, 2.0)],
- [Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 2.0)],
- ],
- [ # BOTTOM
- [Vector2(1.0, 0.0), Vector2(2.0, 1.0), Vector2(1.0, 1.0)],
- [Vector2(1.0, 0.0), Vector2(2.0, 0.0), Vector2(2.0, 1.0)],
- ],
+ "uv2s": [
+ Vector2(0, 0), # FRONT
+ Vector2(0, 0), # BACK
+ Vector2(0, 0), # LEFT
+ Vector2(0, 0), # RIGHT
+ Vector2(0, 1), # TOP
+ Vector2(1, 0), # BOTTOM
+ ],
+ },
+ BlockType.DIRT: {
+ "uv2s": [
+ Vector2(1, 0), # FRONT
+ Vector2(1, 0), # BACK
+ Vector2(1, 0), # LEFT
+ Vector2(1, 0), # RIGHT
+ Vector2(1, 0), # TOP
+ Vector2(1, 0), # BOTTOM
+ ],
+ },
+ BlockType.LEAVES: {
+ "uv2s": [
+ Vector2(2, 2), # FRONT
+ Vector2(2, 2), # BACK
+ Vector2(2, 2), # LEFT
+ Vector2(2, 2), # RIGHT
+ Vector2(2, 2), # TOP
+ Vector2(2, 2), # BOTTOM
+ ],
+ },
+ BlockType.STONE: {
+ "uv2s": [
+ Vector2(2, 0), # FRONT
+ Vector2(2, 0), # BACK
+ Vector2(2, 0), # LEFT
+ Vector2(2, 0), # RIGHT
+ Vector2(2, 0), # TOP
+ Vector2(2, 0), # BOTTOM
],
},
}
+var chunks := []
+
func _ready() -> void:
brrr()
@@ -94,9 +147,12 @@ func brrr():
var thread = OtherThread.new()
var rrrb = func():
- for row in range(0, 2):
- for column in range(0, 2):
- for depth in range(0, 2):
+ for row in range(0, 8):
+ chunks.append([])
+ for column in range(0, 4):
+ chunks[row].append([])
+ for depth in range(0, 8):
+ chunks[row][column].append([])
generate_chunk(row, column, depth)
thread.start(rrrb)
@@ -123,19 +179,26 @@ func generate_chunk(row, column, depth):
for z in chunk_size.z:
var block_position = Vector3(
row*chunk_size.x + x,
- depth*chunk_size.y + y,
- column*chunk_size.z + z
+ 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)
+ var value = noise.get_noise_3d(block_position.x, block_position.y, block_position.z) * 100
- #blocks[x][y][z] = block_position
- blocks[x][y].append({
+ var type = BlockType.AIR
+ if value > -15:
+ type = BlockType.GRASS
+ elif value > -20:
+ type = BlockType.STONE
+
+ var block = {
"position": block_position,
- "set": abs(value) > 0.2,
- })
+ "type": type,
+ }
+ #blocks[x][y][z] = block_position
+ blocks[x][y].append(block)
for x in blocks.size():
for y in blocks[x].size():
@@ -143,36 +206,40 @@ func generate_chunk(row, column, depth):
var block = blocks[x][y][z]
var block_position = block.position
- if block.set:
+ if not block.type == BlockType.AIR:
+ if block.type == BlockType.GRASS and blocks[x].size() > y + 1 and not blocks[x][y + 1][z].type == BlockType.AIR:
+ block.type = BlockType.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 not blocks[x][y][z + 1].set) or blocks[x][y].size() == z+1:
- add_face(st, BlockFace.FRONT, block_position)
+ if (blocks[x][y].size() > z+1 and blocks[x][y][z + 1].type == BlockType.AIR) or blocks[x][y].size() == z+1:
+ add_face(st, BlockFace.FRONT, block)
- if (blocks.size() > x+1 and not blocks[x + 1][y][z].set) or blocks.size() == x+1:
- add_face(st, BlockFace.RIGHT, block_position)
+ if (blocks.size() > x+1 and blocks[x + 1][y][z].type == BlockType.AIR) or blocks.size() == x+1:
+ add_face(st, BlockFace.RIGHT, block)
- if blocks[x][y].size() > z-1 and (not blocks[x][y][z - 1].set or z == 0):
- add_face(st, BlockFace.BACK, block_position)
+ if blocks[x][y].size() > z-1 and (blocks[x][y][z - 1].type == BlockType.AIR or z == 0):
+ add_face(st, BlockFace.BACK, block)
- if blocks.size() > x-1 and (not blocks[x - 1][y][z].set or x == 0):
- add_face(st, BlockFace.LEFT, block_position)
+ if blocks.size() > x-1 and (blocks[x - 1][y][z].type == BlockType.AIR or x == 0):
+ add_face(st, BlockFace.LEFT, block)
- if (blocks[x].size() > y+1 and not blocks[x][y + 1][z].set) or blocks[x].size() == y+1:
- add_face(st, BlockFace.TOP, block_position)
+ if (blocks[x].size() > y+1 and blocks[x][y + 1][z].type == BlockType.AIR) or blocks[x].size() == y+1:
+ add_face(st, BlockFace.TOP, block)
- if blocks[x].size() > y-1 and (not blocks[x][y - 1][z].set or y == 0):
- add_face(st, BlockFace.BOTTOM, block_position)
+ if blocks[x].size() > y-1 and (blocks[x][y - 1][z].type == BlockType.AIR or y == 0):
+ add_face(st, BlockFace.BOTTOM, block)
+ chunks[row][column][depth] = blocks
#st.generate_normals()
var mesh = MeshInstance3D.new()
mesh.mesh = st.commit()
- #mesh.create_trimesh_collision()
+ mesh.create_trimesh_collision()
add_child.call_deferred(mesh)
@@ -181,29 +248,17 @@ func _process(_delta: float) -> void:
pass
-func add_face(surface_tool: SurfaceTool, face: BlockFace, block_position: Vector3):
- var uv2 = []
- uv2.resize(3)
+func add_face(surface_tool: SurfaceTool, face: BlockFace, block: Dictionary):
+ var block_position: Vector3 = block.position
+ var type: BlockType = block.type
+
+ var uv2s = []
+ uv2s.resize(3)
+ uv2s.fill(block_types[type].uv2s[face])
+
var normals = []
normals.resize(3)
- if face == BlockFace.FRONT:
- uv2.fill(Vector2(0, 0))
- normals.fill(Vector3(0, 0, 1))
- elif face == BlockFace.BACK:
- uv2.fill(Vector2(0, 0))
- normals.fill(Vector3(0, 0, -1))
- elif face == BlockFace.LEFT:
- uv2.fill(Vector2(0, 0))
- normals.fill(Vector3(-1, 0, 0))
- elif face == BlockFace.RIGHT:
- uv2.fill(Vector2(0, 0))
- normals.fill(Vector3(1, 0, 0))
- elif face == BlockFace.TOP:
- uv2.fill(Vector2(6, 0))
- normals.fill(Vector3(0, 1, 0))
- elif face == BlockFace.BOTTOM:
- uv2.fill(Vector2(1, 0))
- normals.fill(Vector3(0, -1, 0))
+ normals.fill(block_face_normals[face])
for idx in block_faces[face].size():
var triangle = block_faces[face][idx]
@@ -211,15 +266,12 @@ func add_face(surface_tool: SurfaceTool, face: BlockFace, block_position: Vector
return item + block_position
)
- var uvs = block_types[BlockType.GRASS].uvs[face][idx].map(func(item):
- return item
- #return Vector2(item.x / 6.0, item.y / 3.0)
- )
+ var uvs = block_face_uvs[face][idx]
surface_tool.add_triangle_fan(
PackedVector3Array(triangle_positions),
PackedVector2Array(uvs),
PackedColorArray(),
- PackedVector2Array(uv2),
+ PackedVector2Array(uv2s),
PackedVector3Array(normals)
)