|
@@ -19,7 +19,7 @@ var _thread
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
- transform.origin = chunk_position * CHUNK_SIZE
|
|
|
+ transform.origin = Vector3(chunk_position * CHUNK_SIZE)
|
|
|
name = str(chunk_position)
|
|
|
if Settings.world_type == 0:
|
|
|
data = TerrainGenerator.random_blocks()
|
|
@@ -30,7 +30,7 @@ func _ready():
|
|
|
_generate_chunk_collider()
|
|
|
# However, we can use a thread for mesh generation.
|
|
|
_thread = Thread.new()
|
|
|
- _thread.start(self, "_generate_chunk_mesh")
|
|
|
+ _thread.start(self._generate_chunk_mesh)
|
|
|
|
|
|
|
|
|
func regenerate():
|
|
@@ -113,7 +113,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
bottom_uvs = top_uvs
|
|
|
|
|
|
# Main rendering code for normal blocks.
|
|
|
- var other_block_position = block_sub_position + Vector3.LEFT
|
|
|
+ var other_block_position = block_sub_position + Vector3i.LEFT
|
|
|
var other_block_id = 0
|
|
|
if other_block_position.x == -1:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -122,7 +122,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
if block_id != other_block_id and is_block_transparent(other_block_id):
|
|
|
_draw_block_face(surface_tool, [verts[2], verts[0], verts[3], verts[1]], uvs)
|
|
|
|
|
|
- other_block_position = block_sub_position + Vector3.RIGHT
|
|
|
+ other_block_position = block_sub_position + Vector3i.RIGHT
|
|
|
other_block_id = 0
|
|
|
if other_block_position.x == CHUNK_SIZE:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -131,7 +131,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
if block_id != other_block_id and is_block_transparent(other_block_id):
|
|
|
_draw_block_face(surface_tool, [verts[7], verts[5], verts[6], verts[4]], uvs)
|
|
|
|
|
|
- other_block_position = block_sub_position + Vector3.FORWARD
|
|
|
+ other_block_position = block_sub_position + Vector3i.FORWARD
|
|
|
other_block_id = 0
|
|
|
if other_block_position.z == -1:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -140,7 +140,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
if block_id != other_block_id and is_block_transparent(other_block_id):
|
|
|
_draw_block_face(surface_tool, [verts[6], verts[4], verts[2], verts[0]], uvs)
|
|
|
|
|
|
- other_block_position = block_sub_position + Vector3.BACK
|
|
|
+ other_block_position = block_sub_position + Vector3i.BACK
|
|
|
other_block_id = 0
|
|
|
if other_block_position.z == CHUNK_SIZE:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -149,7 +149,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
if block_id != other_block_id and is_block_transparent(other_block_id):
|
|
|
_draw_block_face(surface_tool, [verts[3], verts[1], verts[7], verts[5]], uvs)
|
|
|
|
|
|
- other_block_position = block_sub_position + Vector3.DOWN
|
|
|
+ other_block_position = block_sub_position + Vector3i.DOWN
|
|
|
other_block_id = 0
|
|
|
if other_block_position.y == -1:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -158,7 +158,7 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
if block_id != other_block_id and is_block_transparent(other_block_id):
|
|
|
_draw_block_face(surface_tool, [verts[4], verts[5], verts[0], verts[1]], bottom_uvs)
|
|
|
|
|
|
- other_block_position = block_sub_position + Vector3.UP
|
|
|
+ other_block_position = block_sub_position + Vector3i.UP
|
|
|
other_block_id = 0
|
|
|
if other_block_position.y == CHUNK_SIZE:
|
|
|
other_block_id = voxel_world.get_block_global_position(other_block_position + chunk_position * CHUNK_SIZE)
|
|
@@ -168,21 +168,21 @@ func _draw_block_mesh(surface_tool, block_sub_position, block_id):
|
|
|
_draw_block_face(surface_tool, [verts[2], verts[3], verts[6], verts[7]], top_uvs)
|
|
|
|
|
|
|
|
|
-func _draw_block_face(surface_tool, verts, uvs):
|
|
|
- surface_tool.add_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
|
|
- surface_tool.add_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
|
|
- surface_tool.add_uv(uvs[3]); surface_tool.add_vertex(verts[3])
|
|
|
+func _draw_block_face(surface_tool: SurfaceTool, verts, uvs):
|
|
|
+ surface_tool.set_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
|
|
+ surface_tool.set_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
|
|
+ surface_tool.set_uv(uvs[3]); surface_tool.add_vertex(verts[3])
|
|
|
|
|
|
- surface_tool.add_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
|
|
- surface_tool.add_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
|
|
- surface_tool.add_uv(uvs[0]); surface_tool.add_vertex(verts[0])
|
|
|
+ surface_tool.set_uv(uvs[2]); surface_tool.add_vertex(verts[2])
|
|
|
+ surface_tool.set_uv(uvs[1]); surface_tool.add_vertex(verts[1])
|
|
|
+ surface_tool.set_uv(uvs[0]); surface_tool.add_vertex(verts[0])
|
|
|
|
|
|
|
|
|
func _create_block_collider(block_sub_position):
|
|
|
var collider = CollisionShape3D.new()
|
|
|
collider.shape = BoxShape3D.new()
|
|
|
collider.shape.extents = Vector3.ONE / 2
|
|
|
- collider.transform.origin = block_sub_position + Vector3.ONE / 2
|
|
|
+ collider.transform.origin = Vector3(block_sub_position) + Vector3.ONE / 2
|
|
|
add_child(collider)
|
|
|
|
|
|
|
|
@@ -192,10 +192,12 @@ static func calculate_block_uvs(block_id):
|
|
|
var col = block_id % TEXTURE_SHEET_WIDTH
|
|
|
|
|
|
return [
|
|
|
- TEXTURE_TILE_SIZE * Vector2(col, row),
|
|
|
- TEXTURE_TILE_SIZE * Vector2(col, row + 1),
|
|
|
- TEXTURE_TILE_SIZE * Vector2(col + 1, row),
|
|
|
- TEXTURE_TILE_SIZE * Vector2(col + 1, row + 1),
|
|
|
+ # Godot 4 has a weird bug where there are seams at the edge
|
|
|
+ # of the textures. Adding a margin of 0.01 "fixes" it.
|
|
|
+ TEXTURE_TILE_SIZE * Vector2(col + 0.01, row + 0.01),
|
|
|
+ TEXTURE_TILE_SIZE * Vector2(col + 0.01, row + 0.99),
|
|
|
+ TEXTURE_TILE_SIZE * Vector2(col + 0.99, row + 0.01),
|
|
|
+ TEXTURE_TILE_SIZE * Vector2(col + 0.99, row + 0.99),
|
|
|
]
|
|
|
|
|
|
|