Fix: auto-scale tile 3D models to fit 0.98-unit tile footprint
Adds _fit_model_to_tile() + _model_local_aabb() helpers. After a tile model loads, its AABB is computed from all descendant VisualInstance3D nodes in the model's local space. The model is then uniformly scaled so its largest XZ dimension = 0.98 units, and repositioned so it is centred on XZ with its bottom at y = 0 of the tile body. Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -450,9 +450,48 @@ func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void:
|
||||
child.queue_free()
|
||||
|
||||
b.add_child(model_node)
|
||||
# Scale so the XZ footprint fits the 0.98-unit tile square.
|
||||
_fit_model_to_tile(model_node, 0.98)
|
||||
)
|
||||
|
||||
|
||||
## Scales model so its largest XZ dimension equals `tile_size`, then centres
|
||||
## it on XZ and seats the bottom at y = 0 in the parent body's local space.
|
||||
func _fit_model_to_tile(model: Node3D, tile_size: float) -> void:
|
||||
var aabb := _model_local_aabb(model)
|
||||
if aabb.size == Vector3.ZERO:
|
||||
return
|
||||
var footprint := max(aabb.size.x, aabb.size.z)
|
||||
if footprint <= 0.0:
|
||||
return
|
||||
var s := tile_size / footprint
|
||||
model.scale = Vector3.ONE * s
|
||||
var c := aabb.get_center()
|
||||
model.position = Vector3(-c.x * s, -aabb.position.y * s, -c.z * s)
|
||||
|
||||
|
||||
## Returns the combined AABB of all VisualInstance3D descendants expressed
|
||||
## in the root node's own local coordinate space (before root.scale/position).
|
||||
func _model_local_aabb(root: Node3D) -> AABB:
|
||||
var result := AABB()
|
||||
var first := true
|
||||
# Each entry: { n: Node3D, x: Transform3D } where x transforms from
|
||||
# that node's local space to root's local space.
|
||||
var stack : Array = [{ "n": root, "x": Transform3D.IDENTITY }]
|
||||
while not stack.is_empty():
|
||||
var e : Dictionary = stack.pop_back()
|
||||
var node : Node3D = e["n"]
|
||||
var xform : Transform3D = e["x"]
|
||||
if node is VisualInstance3D:
|
||||
var local_aabb := xform * (node as VisualInstance3D).get_aabb()
|
||||
result = local_aabb if first else result.merge(local_aabb)
|
||||
first = false
|
||||
for child in node.get_children():
|
||||
if child is Node3D:
|
||||
stack.push_back({ "n": child, "x": xform * (child as Node3D).transform })
|
||||
return result
|
||||
|
||||
|
||||
func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
||||
var body := _tile_bodies.get("%d,%d" % [x, y]) as StaticBody3D
|
||||
if body == null: return
|
||||
|
||||
Reference in New Issue
Block a user