feat(textures): atlas blocs tileable + UV mapping Chunk.gd (supprime vertex colors)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,16 @@ var _blocks: PackedInt32Array = PackedInt32Array()
|
||||
var _mesh_instance: MeshInstance3D = null
|
||||
var _static_body: StaticBody3D = null
|
||||
|
||||
static var shared_material: StandardMaterial3D = null
|
||||
|
||||
static func _init_material() -> void:
|
||||
if shared_material == null:
|
||||
shared_material = StandardMaterial3D.new()
|
||||
shared_material.albedo_texture = load("res://assets/textures/block_atlas.png")
|
||||
shared_material.texture_filter = BaseMaterial3D.TEXTURE_FILTER_NEAREST_WITH_MIPMAPS
|
||||
shared_material.vertex_color_use_as_albedo = false
|
||||
shared_material.shading_mode = BaseMaterial3D.SHADING_MODE_PER_VERTEX
|
||||
|
||||
func _ready() -> void:
|
||||
if _blocks.size() == 0:
|
||||
_blocks.resize(4096)
|
||||
@@ -32,11 +42,16 @@ func generate_mesh() -> void:
|
||||
_static_body.queue_free()
|
||||
_static_body = null
|
||||
|
||||
_init_material()
|
||||
|
||||
var st: SurfaceTool = SurfaceTool.new()
|
||||
st.begin(Mesh.PRIMITIVE_TRIANGLES)
|
||||
|
||||
var has_faces: bool = false
|
||||
|
||||
var tile_w: float = BlockDatabase.ATLAS_TILE_SIZE.x
|
||||
var tile_h: float = BlockDatabase.ATLAS_TILE_SIZE.y
|
||||
|
||||
for x: int in range(CHUNK_SIZE):
|
||||
for y: int in range(CHUNK_SIZE):
|
||||
for z: int in range(CHUNK_SIZE):
|
||||
@@ -44,25 +59,25 @@ func generate_mesh() -> void:
|
||||
if not BlockDatabase.is_solid(block_id):
|
||||
continue
|
||||
|
||||
var color: Color = BlockDatabase.get_color(block_id)
|
||||
var uv_tl: Vector2 = BlockDatabase.get_atlas_uv(block_id)
|
||||
|
||||
if not _is_opaque_at(x, y + 1, z):
|
||||
_add_face_top(st, x, y, z, color)
|
||||
_add_face_top(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
if not _is_opaque_at(x, y - 1, z):
|
||||
_add_face_bottom(st, x, y, z, color)
|
||||
_add_face_bottom(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
if not _is_opaque_at(x + 1, y, z):
|
||||
_add_face_right(st, x, y, z, color)
|
||||
_add_face_right(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
if not _is_opaque_at(x - 1, y, z):
|
||||
_add_face_left(st, x, y, z, color)
|
||||
_add_face_left(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
if not _is_opaque_at(x, y, z + 1):
|
||||
_add_face_front(st, x, y, z, color)
|
||||
_add_face_front(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
if not _is_opaque_at(x, y, z - 1):
|
||||
_add_face_back(st, x, y, z, color)
|
||||
_add_face_back(st, x, y, z, uv_tl, tile_w, tile_h)
|
||||
has_faces = true
|
||||
|
||||
if not has_faces:
|
||||
@@ -70,15 +85,11 @@ func generate_mesh() -> void:
|
||||
|
||||
st.generate_normals()
|
||||
|
||||
var mat: StandardMaterial3D = StandardMaterial3D.new()
|
||||
mat.vertex_color_use_as_albedo = true
|
||||
mat.shading_mode = BaseMaterial3D.SHADING_MODE_PER_VERTEX
|
||||
st.set_material(mat)
|
||||
|
||||
var mesh: ArrayMesh = st.commit()
|
||||
|
||||
_mesh_instance = MeshInstance3D.new()
|
||||
_mesh_instance.mesh = mesh
|
||||
_mesh_instance.material_override = shared_material
|
||||
add_child(_mesh_instance)
|
||||
|
||||
var shape: ConcavePolygonShape3D = ConcavePolygonShape3D.new()
|
||||
@@ -96,80 +107,109 @@ func _is_opaque_at(x: int, y: int, z: int) -> bool:
|
||||
return false
|
||||
return BlockDatabase.is_solid(get_block(x, y, z))
|
||||
|
||||
func _add_face_top(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
# UV layout for a quad: TL, TR, BR, BL
|
||||
# Face vertices are added as two triangles: TL-TR-BR and TL-BR-BL
|
||||
|
||||
func _add_face_top(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
# tri 1
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz + 1.0))
|
||||
st.set_color(color)
|
||||
# tri 2
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz + 1.0))
|
||||
|
||||
func _add_face_bottom(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
func _add_face_bottom(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz))
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx, fy, fz))
|
||||
|
||||
func _add_face_right(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
func _add_face_right(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz + 1.0))
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz))
|
||||
|
||||
func _add_face_left(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
func _add_face_left(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz))
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz + 1.0))
|
||||
|
||||
func _add_face_front(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
func _add_face_front(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz + 1.0))
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz + 1.0))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz + 1.0))
|
||||
|
||||
func _add_face_back(st: SurfaceTool, x: int, y: int, z: int, color: Color) -> void:
|
||||
func _add_face_back(st: SurfaceTool, x: int, y: int, z: int, uv_tl: Vector2, tw: float, th: float) -> void:
|
||||
var fx: float = float(x)
|
||||
var fy: float = float(y)
|
||||
var fz: float = float(z)
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, 0.0))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz))
|
||||
st.set_color(color)
|
||||
st.set_uv(uv_tl)
|
||||
st.add_vertex(Vector3(fx, fy, fz))
|
||||
st.set_uv(uv_tl + Vector2(tw, th))
|
||||
st.add_vertex(Vector3(fx + 1.0, fy + 1.0, fz))
|
||||
st.set_uv(uv_tl + Vector2(0.0, th))
|
||||
st.add_vertex(Vector3(fx, fy + 1.0, fz))
|
||||
|
||||
Reference in New Issue
Block a user