Merge pull request #32550 from JFonS/fix_editor_spatial_bounds

Fix calculate_spatial_bounds for selection display
This commit is contained in:
Rémi Verschelde 2019-10-04 19:32:05 +02:00 committed by GitHub
commit 5a23ab61fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 11 deletions

View file

@ -2172,7 +2172,7 @@ void SpatialEditorViewport::_notification(int p_what) {
VisualInstance *vi = Object::cast_to<VisualInstance>(sp); VisualInstance *vi = Object::cast_to<VisualInstance>(sp);
se->aabb = vi ? vi->get_aabb() : _calculate_spatial_bounds(sp, AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4))); se->aabb = vi ? vi->get_aabb() : _calculate_spatial_bounds(sp);
Transform t = sp->get_global_gizmo_transform(); Transform t = sp->get_global_gizmo_transform();
t.translate(se->aabb.position); t.translate(se->aabb.position);
@ -3209,20 +3209,35 @@ Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const
return point + offset; return point + offset;
} }
AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, const AABB &p_bounds) { AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, bool p_exclude_toplevel_transform) {
AABB bounds = p_bounds; AABB bounds;
const MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(p_parent);
if (mesh_instance) {
bounds = mesh_instance->get_aabb();
}
for (int i = 0; i < p_parent->get_child_count(); i++) { for (int i = 0; i < p_parent->get_child_count(); i++) {
Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i)); Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i));
if (child) { if (child) {
MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(child); AABB child_bounds = _calculate_spatial_bounds(child, false);
if (mesh_instance) {
AABB mesh_instance_bounds = mesh_instance->get_aabb(); if (bounds.size == Vector3() && p_parent->get_class_name() == StringName("Spatial")) {
mesh_instance_bounds.position += mesh_instance->get_global_gizmo_transform().origin - p_parent->get_global_gizmo_transform().origin; bounds = child_bounds;
bounds.merge_with(mesh_instance_bounds); } else {
bounds.merge_with(child_bounds);
} }
bounds = _calculate_spatial_bounds(child, bounds);
} }
} }
if (bounds.size == Vector3() && p_parent->get_class_name() != StringName("Spatial")) {
bounds = AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
}
if (!p_exclude_toplevel_transform) {
bounds = p_parent->get_transform().xform(bounds);
}
return bounds; return bounds;
} }
@ -3249,7 +3264,7 @@ void SpatialEditorViewport::_create_preview(const Vector<String> &files) const {
editor->get_scene_root()->add_child(preview_node); editor->get_scene_root()->add_child(preview_node);
} }
} }
*preview_bounds = _calculate_spatial_bounds(preview_node, AABB()); *preview_bounds = _calculate_spatial_bounds(preview_node);
} }
void SpatialEditorViewport::_remove_preview() { void SpatialEditorViewport::_remove_preview() {

View file

@ -375,7 +375,7 @@ private:
Point2i _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const; Point2i _get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const;
Vector3 _get_instance_position(const Point2 &p_pos) const; Vector3 _get_instance_position(const Point2 &p_pos) const;
static AABB _calculate_spatial_bounds(const Spatial *p_parent, const AABB &p_bounds); static AABB _calculate_spatial_bounds(const Spatial *p_parent, bool p_exclude_toplevel_transform = true);
void _create_preview(const Vector<String> &files) const; void _create_preview(const Vector<String> &files) const;
void _remove_preview(); void _remove_preview();
bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node); bool _cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node);