@tool
extends Node3D
# ==============================================================================
# PROPRIEDADES DO INSPECTOR - SELEÇÃO E GERAÇÃO DA METRÓPOLE SOBRE O NÓ BASE
# ==============================================================================
@export_category("Seleção de Nó Base")
@export var target_base_node_path: NodePath:
set(value):
target_base_node_path = value
if is_node_ready(): _generate_from_selected_node()
@export var executar_geracao: bool = false:
set(value):
if value:
_generate_from_selected_node()
executar_geracao = false
@export_category("Configuração de Ruas e Grade da Cidade")
@export var city_box_size: float = 90.0:
set(value):
city_box_size = value
if is_node_ready(): _generate_from_selected_node()
@export var highway_width: float = 10.0:
set(value):
highway_width = value
if is_node_ready(): _generate_from_selected_node()
@export var block_size: Vector2 = Vector2(16.0, 16.0):
set(value):
block_size = value
if is_node_ready(): _generate_from_selected_node()
@export var street_width: float = 4.5:
set(value):
street_width = value
if is_node_ready(): _generate_from_selected_node()
@export var sidewalk_width: float = 1.4:
set(value):
sidewalk_width = value
if is_node_ready(): _generate_from_selected_node()
@export var street_height_offset: float = 0.1:
set(value):
street_height_offset = value
if is_node_ready(): _generate_from_selected_node()
@export_category("Configuração de Estruturas Urbanas")
@export var building_max_height: float = 35.0:
set(value):
building_max_height = value
if is_node_ready(): _generate_from_selected_node()
@export var include_suburbs_houses: bool = true:
set(value):
include_suburbs_houses = value
if is_node_ready(): _generate_from_selected_node()
@export var include_favela: bool = true:
set(value):
include_favela = value
if is_node_ready(): _generate_from_selected_node()
@export var include_plazas_and_parks: bool = true:
set(value):
include_plazas_and_parks = value
if is_node_ready(): _generate_from_selected_node()
@export var noise_seed: int = 1337:
set(value):
noise_seed = value
if is_node_ready(): _generate_from_selected_node()
var generated_container: Node3D
# ==============================================================================
# CICLO DE VIDA
# ==============================================================================
func _ready() -> void:
if target_base_node_path.is_empty():
var p = get_parent()
if p:
var siblings = p.get_children()
var my_index = siblings.find(self)
if my_index > 0:
target_base_node_path = get_path_to(siblings[my_index - 1])
_generate_from_selected_node()
# ==============================================================================
# LÓGICA DE SELEÇÃO E GERAÇÃO DA METRÓPOLE
# ==============================================================================
func _generate_from_selected_node() -> void:
generated_container = get_node_or_null("GeneratedCityContainer") as Node3D
if not generated_container:
generated_container = Node3D.new()
generated_container.name = "GeneratedCityContainer"
add_child(generated_container)
if Engine.is_editor_hint() and get_tree():
generated_container.owner = get_tree().edited_scene_root
else:
for child in generated_container.get_children():
child.queue_free()
var base_node: Node3D = null
if not target_base_node_path.is_empty():
base_node = get_node_or_null(target_base_node_path) as Node3D
if not base_node:
var p = get_parent()
if p:
var siblings = p.get_children()
var my_index = siblings.find(self)
if my_index > 0 and siblings[my_index - 1] is Node3D:
base_node = siblings[my_index - 1] as Node3D
var base_position = Vector3.ZERO
var base_height_val = 0.0
if base_node:
base_position = base_node.global_position
# Tenta pegar propriedades comuns de platô ou altura se existirem no nó base
if base_node.has_method("get"):
if base_node.get("flat_height") != null:
base_height_val = base_node.get("flat_height")
elif base_node.get("height_scale") != null:
# Caso seja um terreno, usa a posição Y dele como referência base
base_height_val = base_position.y
var rng = RandomNumberGenerator.new()
rng.seed = noise_seed + 9999
# --- SHADER DE TELHADO PARA OS SUBÚRBIOS ---
var roof_shader = Shader.new()
roof_shader.code = """
shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back;
uniform vec3 tile_color = vec3(0.55, 0.22, 0.15);
void fragment() {
vec2 grid = fract(UV * vec2(16.0, 8.0));
float lines = step(0.12, grid.x) * step(0.15, grid.y);
vec3 col = mix(tile_color * 0.5, tile_color, lines);
ALBEDO = col;
ROUGHNESS = 0.6;
}
"""
var base_h = base_height_val + street_height_offset
var half_size = city_box_size * 0.5
var outer_min_x = base_position.x - half_size
var outer_max_x = base_position.x + half_size
var outer_min_z = base_position.z - half_size
var outer_max_z = base_position.z + half_size
# 1. GERAÇÃO DA RUA PERIMETRAL EXTERNA (SurfaceTool)
var st_roads = SurfaceTool.new()
st_roads.begin(Mesh.PRIMITIVE_TRIANGLES)
_draw_square_highway_ring(st_roads, outer_min_x, outer_max_x, outer_min_z, outer_max_z, highway_width, base_h)
# 2. GERAÇÃO DA GRADE INTERNA, CALÇADAS E ESTRUTURAS NAS QUADRAS
var inner_min_x = outer_min_x + highway_width
var inner_max_x = outer_max_x - highway_width
var inner_min_z = outer_min_z + highway_width
var inner_max_z = outer_max_z - highway_width
var cell_total = block_size.x + street_width + (sidewalk_width * 2.0)
var city_center_pos = Vector2(base_position.x, base_position.z)
var cx = inner_min_x
while cx < inner_max_x:
var cz = inner_min_z
while cz < inner_max_z:
var bx0 = cx + sidewalk_width
var bz0 = cz + sidewalk_width
var bx1 = bx0 + block_size.x
var bz1 = bz0 + block_size.y
if bx1 <= inner_max_x and bz1 <= inner_max_z:
var lot_center_x = (bx0 + bx1) * 0.5
var lot_center_z = (bz0 + bz1) * 0.5
var lot_center_vec2 = Vector2(lot_center_x, lot_center_z)
var dist_from_center = lot_center_vec2.distance_to(city_center_pos)
# Calçada elevada da quadra
var sidewalk_h = base_h + 0.05
_add_quad(st_roads,
Vector3(bx0, sidewalk_h, bz0), Vector3(bx1, sidewalk_h, bz0),
Vector3(bx1, sidewalk_h, bz1), Vector3(bx0, sidewalk_h, bz1),
Color(0.75, 0.75, 0.75)
)
# Asfalto da Rua Secundária ao redor da quadra
_add_quad(st_roads,
Vector3(cx, base_h, cz), Vector3(bx1 + sidewalk_width + street_width, base_h, cz),
Vector3(bx1 + sidewalk_width + street_width, base_h, bz1 + sidewalk_width + street_width), Vector3(cx, base_h, bz1 + sidewalk_width + street_width),
Color(0.18, 0.18, 0.18)
)
# ==============================================================
# CONTEÚDO DA QUADRA (Favela, Praça, Subúrbio ou Prédios)
# ==============================================================
var is_in_favela_corner = include_favela and (lot_center_x > base_position.x + (city_box_size * 0.2)) and (lot_center_z > base_position.z + (city_box_size * 0.2))
if is_in_favela_corner:
# Geração de Favela (casinhas aglomeradas)
var sub_steps = 2
var sub_size = block_size.x / float(sub_steps)
for fx in range(sub_steps):
for fz in range(sub_steps):
var house_x = bx0 - (block_size.x * 0.25) + (fx * sub_size) + (sub_size * 0.5)
var house_z = bz0 - (block_size.y * 0.25) + (fz * sub_size) + (sub_size * 0.5)
if rng.randf() > 0.85: continue
var f_w = sub_size * rng.randf_range(0.7, 0.9)
var f_d = sub_size * rng.randf_range(0.7, 0.9)
var f_h = rng.randf_range(2.5, 5.5)
var favela_body = MeshInstance3D.new()
var fb_box = BoxMesh.new()
fb_box.size = Vector3(f_w, f_h, f_d)
favela_body.mesh = fb_box
var f_wall_mat = StandardMaterial3D.new()
var favela_colors = [Color(0.65, 0.35, 0.22), Color(0.7, 0.7, 0.72), Color(0.55, 0.65, 0.75), Color(0.75, 0.68, 0.55)]
f_wall_mat.albedo_color = favela_colors[rng.randi() % favela_colors.size()]
favela_body.material_override = f_wall_mat
favela_body.position = Vector3(house_x, base_h + 0.1 + (f_h * 0.5), house_z)
generated_container.add_child(favela_body)
if Engine.is_editor_hint() and get_tree():
favela_body.owner = get_tree().edited_scene_root
else:
var is_plaza = include_plazas_and_parks and (dist_from_center < 10.0 or rng.randf() < 0.08)
var is_suburb = include_suburbs_houses and (dist_from_center > (city_box_size * 0.3))
if is_plaza:
var plaza_ground = MeshInstance3D.new()
var pg_box = BoxMesh.new()
pg_box.size = Vector3(block_size.x * 0.9, 0.08, block_size.y * 0.9)
plaza_ground.mesh = pg_box
var plaza_mat = StandardMaterial3D.new()
plaza_mat.albedo_color = Color(0.2, 0.45, 0.22)
plaza_ground.material_override = plaza_mat
plaza_ground.position = Vector3(lot_center_x, base_h + 0.1, lot_center_z)
generated_container.add_child(plaza_ground)
if Engine.is_editor_hint() and get_tree():
plaza_ground.owner = get_tree().edited_scene_root
var fountain = MeshInstance3D.new()
var f_cyl = CylinderMesh.new()
f_cyl.top_radius = 0.8; f_cyl.bottom_radius = 1.0; f_cyl.height = 0.5
fountain.mesh = f_cyl
var f_mat = StandardMaterial3D.new()
f_mat.albedo_color = Color(0.85, 0.85, 0.85)
fountain.material_override = f_mat
fountain.position = Vector3(lot_center_x, base_h + 0.35, lot_center_z)
generated_container.add_child(fountain)
if Engine.is_editor_hint() and get_tree():
fountain.owner = get_tree().edited_scene_root
elif is_suburb:
var h_w = block_size.x * 0.7
var h_d = block_size.y * 0.7
var h_h = 3.0
var house_body = MeshInstance3D.new()
var hb_box = BoxMesh.new()
hb_box.size = Vector3(h_w, h_h, h_d)
house_body.mesh = hb_box
var h_mat = StandardMaterial3D.new()
h_mat.albedo_color = Color(0.85, 0.82, 0.75)
house_body.material_override = h_mat
house_body.position = Vector3(lot_center_x, base_h + 0.1 + (h_h * 0.5), lot_center_z)
generated_container.add_child(house_body)
if Engine.is_editor_hint() and get_tree():
house_body.owner = get_tree().edited_scene_root
var roof_mesh = MeshInstance3D.new()
var r_prism = PrismMesh.new()
r_prism.size = Vector3(h_w * 1.1, 1.2, h_d * 1.1)
roof_mesh.mesh = r_prism
var roof_mat = ShaderMaterial.new()
roof_mat.shader = roof_shader
roof_mesh.material_override = roof_mat
roof_mesh.position = Vector3(lot_center_x, base_h + 0.1 + h_h + 0.6, lot_center_z)
generated_container.add_child(roof_mesh)
if Engine.is_editor_hint() and get_tree():
roof_mesh.owner = get_tree().edited_scene_root
else:
# Prédios Centrais (Mais altos perto do centro)
var b_w = block_size.x * 0.85
var b_d = block_size.y * 0.85
var max_possible_dist = city_box_size * 0.5
var height_factor = clamp(1.0 - (dist_from_center / max_possible_dist), 0.3, 1.0)
var b_h = rng.randf_range(8.0, building_max_height) * height_factor
var building = MeshInstance3D.new()
var b_box = BoxMesh.new()
b_box.size = Vector3(b_w, b_h, b_d)
building.mesh = b_box
var b_mat = StandardMaterial3D.new()
var building_colors = [Color(0.2, 0.22, 0.28), Color(0.35, 0.38, 0.42), Color(0.5, 0.5, 0.55), Color(0.25, 0.25, 0.25)]
b_mat.albedo_color = building_colors[rng.randi() % building_colors.size()]
b_mat.roughness = 0.4
b_mat.metallic = 0.3
building.material_override = b_mat
building.position = Vector3(lot_center_x, base_h + 0.1 + (b_h * 0.5), lot_center_z)
generated_container.add_child(building)
if Engine.is_editor_hint() and get_tree():
building.owner = get_tree().edited_scene_root
cz += cell_total
cx += cell_total
st_roads.generate_normals()
var streets_mesh_instance = MeshInstance3D.new()
streets_mesh_instance.mesh = st_roads.commit()
var road_mat = StandardMaterial3D.new()
road_mat.vertex_color_use_as_albedo = true
road_mat.roughness = 0.7
streets_mesh_instance.material_override = road_mat
generated_container.add_child(streets_mesh_instance)
if Engine.is_editor_hint() and get_tree():
streets_mesh_instance.owner = get_tree().edited_scene_root
# ==============================================================================
# FUNÇÕES GEOMÉTRICAS DE APOIO
# ==============================================================================
func _draw_square_highway_ring(st: SurfaceTool, min_x: float, max_x: float, min_z: float, max_z: float, hw: float, base_h: float) -> void:
var highway_color = Color(0.12, 0.12, 0.12)
_add_quad(st, Vector3(min_x, base_h, min_z), Vector3(max_x, base_h, min_z), Vector3(max_x, base_h, min_z + hw), Vector3(min_x, base_h, min_z + hw), highway_color)
_add_quad(st, Vector3(min_x, base_h, max_z - hw), Vector3(max_x, base_h, max_z - hw), Vector3(max_x, base_h, max_z), Vector3(min_x, base_h, max_z), highway_color)
_add_quad(st, Vector3(min_x, base_h, min_z + hw), Vector3(min_x + hw, base_h, min_z + hw), Vector3(min_x + hw, base_h, max_z - hw), Vector3(min_x, base_h, max_z - hw), highway_color)
_add_quad(st, Vector3(max_x - hw, base_h, min_z + hw), Vector3(max_x, base_h, min_z + hw), Vector3(max_x, base_h, max_z - hw), Vector3(max_x - hw, base_h, max_z - hw), highway_color)
func _add_quad(st: SurfaceTool, v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, col: Color) -> void:
st.set_color(col); st.set_uv(Vector2(0, 0)); st.add_vertex(v0)
st.set_color(col); st.set_uv(Vector2(1, 0)); st.add_vertex(v1)
st.set_color(col); st.set_uv(Vector2(1, 1)); st.add_vertex(v2)
st.set_color(col); st.set_uv(Vector2(0, 0)); st.add_vertex(v0)
st.set_color(col); st.set_uv(Vector2(1, 1)); st.add_vertex(v2)
st.set_color(col); st.set_uv(Vector2(0, 1)); st.add_vertex(v3)