summaryrefslogtreecommitdiff
path: root/main.gd
diff options
context:
space:
mode:
Diffstat (limited to 'main.gd')
-rw-r--r--main.gd214
1 files changed, 13 insertions, 201 deletions
diff --git a/main.gd b/main.gd
index 38ab4a1..f1806e4 100644
--- a/main.gd
+++ b/main.gd
@@ -1,83 +1,10 @@
+class_name Main
extends Node3D
-var chunk_size := Vector3(16, 16, 16)
-
@onready var noise := FastNoiseLite.new()
-var face_corners = [
- Vector2(0, 0), Vector2(1, 0),
- Vector2(0, 1), Vector2(1, 1),
-]
-
-enum BlockFace {
- FRONT,
- BACK,
- LEFT,
- RIGHT,
- TOP,
- BOTTOM,
-}
-var block_faces = [
- [ # FRONT
- [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
- [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0)],
- ],
- [ # BACK
- [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
- [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
- ],
- [ # LEFT
- [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, 1.0), Vector3(-1.0, -1.0, -1.0)],
- [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, 1.0, 1.0), Vector3(-1.0, -1.0, 1.0)],
- ],
- [ # RIGHT
- [Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(1.0, -1.0, 1.0)],
- [Vector3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, -1.0, -1.0)],
- ],
- [ # TOP
- [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0), Vector3(-1.0, 1.0, 1.0)],
- [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0)],
- ],
- [ # BOTTOM
- [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(-1.0, -1.0, -1.0)],
- [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0)],
- ],
-]
-
-enum BlockType {
- GRASS,
-}
-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)],
- ],
- ],
- },
-}
+var chunks := []
func _ready() -> void:
@@ -94,132 +21,17 @@ 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):
- generate_chunk(row, column, depth)
+ 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([])
+ var chunk = Chunk.new()
+ chunk.noise = noise
+ var mesh = chunk.generate_chunk(row, column, depth)
+ add_child.call_deferred(mesh)
+ chunks[row][column][depth] = chunk
thread.start(rrrb)
#thread.wait_to_finish()
-
-
-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,
- depth*chunk_size.y + y,
- column*chunk_size.z + z
- #x,
- #y,
- #z
- ) * 2
- var value = noise.get_noise_3d(block_position.x, block_position.y, block_position.z)
-
- #blocks[x][y][z] = block_position
- blocks[x][y].append({
- "position": block_position,
- "set": abs(value) > 0.2,
- })
-
- 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 block.set:
- 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.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[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.size() > x-1 and (not blocks[x - 1][y][z].set or x == 0):
- add_face(st, BlockFace.LEFT, block_position)
-
- 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 (not blocks[x][y - 1][z].set or y == 0):
- add_face(st, BlockFace.BOTTOM, block_position)
-
-
- #st.generate_normals()
- var mesh = MeshInstance3D.new()
- mesh.mesh = st.commit()
- #mesh.create_trimesh_collision()
- add_child.call_deferred(mesh)
-
-
-# Called every frame. 'delta' is the elapsed time since the previous frame.
-func _process(_delta: float) -> void:
- pass
-
-
-func add_face(surface_tool: SurfaceTool, face: BlockFace, block_position: Vector3):
- var uv2 = []
- uv2.resize(3)
- 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))
-
- for idx in block_faces[face].size():
- var triangle = block_faces[face][idx]
- var triangle_positions = triangle.map(func(item):
- 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)
- )
-
- surface_tool.add_triangle_fan(
- PackedVector3Array(triangle_positions),
- PackedVector2Array(uvs),
- PackedColorArray(),
- PackedVector2Array(uv2),
- PackedVector3Array(normals)
- )