Extracted bounding box debug draw logic to component

This commit is contained in:
Rob Kelly 2025-10-04 17:03:13 -06:00
parent 64a9292f9f
commit 460d105afd
5 changed files with 62 additions and 52 deletions

View File

@ -0,0 +1,49 @@
@tool
class_name BoundedGridDebugView extends Node3D
## Helper component that draws the AABB of a bounded grid in the editor
@export var debug_color := Color("#a486006b"):
set(value):
debug_color = value
if box:
_set_debug_box_color(debug_color)
var mesh_instance: MeshInstance3D
var box: BoxMesh
var material: StandardMaterial3D
@onready var target: BoundedGridLayer = get_parent()
func _ready() -> void:
# Don't want these during runtime
if not Engine.is_editor_hint():
queue_free()
# Initialize box drawing components
if not mesh_instance:
mesh_instance = MeshInstance3D.new()
add_child(mesh_instance)
if not box:
box = BoxMesh.new()
if not material:
material = StandardMaterial3D.new()
mesh_instance.mesh = box
box.material = material
material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
material.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_set_debug_box_color(debug_color)
_set_debug_box_shape(target.bounding_box)
func _set_debug_box_color(color: Color) -> void:
material.albedo_color = color
func _set_debug_box_shape(aabb: AABB) -> void:
mesh_instance.position = aabb.position + aabb.size / 2
box.size = aabb.size
func _process(_delta: float) -> void:
_set_debug_box_shape(target.bounding_box)

View File

@ -0,0 +1 @@
uid://r74iif2we4bu

View File

@ -1,4 +1,3 @@
@tool
class_name BoundedGridLayer extends GridLayer
## A layer that generates tiles in a locally-constrained grid.
@ -6,33 +5,15 @@ class_name BoundedGridLayer extends GridLayer
##
## Note that only feature handles are checked to be within the bounding box.
## The Y component of this AABB is not used.
@export var bounding_box: AABB:
set(value):
bounding_box = value
if _debug_box:
_set_debug_box_shape(bounding_box)
@export_group("Debug Draw")
@export var draw_debug := true
@export var debug_color := Color("#a486006b"):
set(value):
debug_color = value
if _debug_box:
_set_debug_box_color(debug_color)
var _debug_meshinstance: MeshInstance3D
var _debug_box: BoxMesh
@export var bounding_box: AABB
func _ready() -> void:
if Engine.is_editor_hint():
_init_debug_draw()
else:
var grid := _plane_size()
for lod in WorldGen.LOD_LIST:
for i in grid.x:
for j in grid.y:
_generation_grid[lod][Vector2(i, j)] = false
var grid := _plane_size()
for lod in WorldGen.LOD_LIST:
for i in grid.x:
for j in grid.y:
_generation_grid[lod][Vector2(i, j)] = false
func _generate(lod: WorldGen.LOD) -> bool:
@ -77,28 +58,3 @@ func local_to_world(local_pos: Vector2) -> Vector3:
func world_to_local(world_pos: Vector3) -> Vector2:
var rel_pos := world_pos * global_transform - bounding_box.position
return Vector2(rel_pos.x / grid_size.x, rel_pos.z / grid_size.y)
#region debug draw
func _init_debug_draw() -> void:
_debug_box = BoxMesh.new()
var mat := StandardMaterial3D.new()
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
_debug_box.material = mat
_debug_meshinstance = MeshInstance3D.new()
_debug_meshinstance.mesh = _debug_box
add_child(_debug_meshinstance)
_set_debug_box_color(debug_color)
_set_debug_box_shape(bounding_box)
func _set_debug_box_color(color: Color) -> void:
var mat := _debug_box.material as StandardMaterial3D
mat.albedo_color = color
func _set_debug_box_shape(aabb: AABB) -> void:
_debug_meshinstance.position = aabb.position + aabb.size / 2
_debug_box.size = aabb.size
#endregion

View File

@ -1,4 +1,3 @@
@tool
extends BoundedGridLayer
## Procedural cityscape generation

View File

@ -1,8 +1,9 @@
[gd_scene load_steps=4 format=3 uid="uid://cwygxhknl2h8r"]
[gd_scene load_steps=5 format=3 uid="uid://cwygxhknl2h8r"]
[ext_resource type="Script" uid="uid://d25t2340sdrox" path="res://src/world/generation/layer/metro_grid_layer/metro_grid_layer.gd" id="1_ng6r6"]
[ext_resource type="PackedScene" uid="uid://cgvg7n525g18l" path="res://src/world/generation/feature/metro/metro_empty.tscn" id="2_47xjc"]
[ext_resource type="PackedScene" uid="uid://beno0v5dxtwqs" path="res://src/world/generation/feature/metro/metro_construct_simple/metro_construct_simple.tscn" id="3_woad2"]
[ext_resource type="Script" uid="uid://r74iif2we4bu" path="res://src/world/generation/layer/grid_layer/bounded_grid_debug_view.gd" id="4_woad2"]
[node name="MetroGridLayer" type="Node3D"]
script = ExtResource("1_ng6r6")
@ -10,3 +11,7 @@ empty_scene = ExtResource("2_47xjc")
simple_construct_scene = ExtResource("3_woad2")
grid_size = Vector2(64, 64)
generated_lods = 6
[node name="BoundedGridDebugView" type="Node3D" parent="."]
script = ExtResource("4_woad2")
metadata/_custom_type_script = "uid://r74iif2we4bu"