Merge pull request #62227 from TokageItLab/fix-rotation-mode-node3d

This commit is contained in:
Rémi Verschelde 2022-06-24 10:07:21 +02:00 committed by GitHub
commit 49d8748848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 21 deletions

View File

@ -85,10 +85,13 @@ void Node3D::_notify_dirty() {
}
void Node3D::_update_local_transform() const {
if (this->get_rotation_edit_mode() != ROTATION_EDIT_MODE_BASIS) {
data.local_transform = data.local_transform.orthogonalized();
if (this->get_rotation_edit_mode() == ROTATION_EDIT_MODE_EULER) {
data.local_transform.basis.set_euler(data.rotation, data.rotation_order);
data.local_transform.basis.scale_local(data.scale);
} else if (this->get_rotation_edit_mode() == ROTATION_EDIT_MODE_QUATERNION) {
data.local_transform.basis = Basis(data.quaternion);
data.local_transform.basis.scale_local(data.scale);
}
data.local_transform.basis.set_euler_scale(data.rotation, data.scale);
data.dirty &= ~DIRTY_LOCAL;
}
@ -212,7 +215,18 @@ void Node3D::set_basis(const Basis &p_basis) {
set_transform(Transform3D(p_basis, data.local_transform.origin));
}
void Node3D::set_quaternion(const Quaternion &p_quaternion) {
set_transform(Transform3D(Basis(p_quaternion), data.local_transform.origin));
if (data.dirty & DIRTY_VECTORS) {
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
}
data.quaternion = p_quaternion;
data.dirty |= DIRTY_LOCAL;
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
}
}
void Node3D::set_transform(const Transform3D &p_transform) {
@ -228,7 +242,14 @@ Basis Node3D::get_basis() const {
return get_transform().basis;
}
Quaternion Node3D::get_quaternion() const {
return Quaternion(get_transform().basis);
if (data.dirty & DIRTY_VECTORS) {
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
}
return data.quaternion;
}
void Node3D::set_global_transform(const Transform3D &p_transform) {
@ -255,9 +276,9 @@ Transform3D Node3D::get_global_transform() const {
}
if (data.parent && !data.top_level_active) {
data.global_transform = data.parent->get_global_transform() * data.local_transform;
data.global_transform = data.parent->get_global_transform() * get_transform();
} else {
data.global_transform = data.local_transform;
data.global_transform = get_transform();
}
if (data.disable_scale) {
@ -303,7 +324,7 @@ Transform3D Node3D::get_relative_transform(const Node *p_parent) const {
}
void Node3D::set_position(const Vector3 &p_position) {
data.local_transform.origin = p_position;
get_transform().origin = p_position;
_propagate_transform_changed(this);
if (data.notify_local_transform) {
notification(NOTIFICATION_LOCAL_TRANSFORM_CHANGED);
@ -314,9 +335,13 @@ void Node3D::set_rotation_edit_mode(RotationEditMode p_mode) {
if (data.rotation_edit_mode == p_mode) {
return;
}
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.scale = get_transform().basis.get_scale();
data.rotation_edit_mode = p_mode;
// Shearing is not allowed except in ROTATION_EDIT_MODE_BASIS.
// Shearing is not allowed except in ROTATION_EDIT_MODE_BASIS, so validate local_transform.
data.dirty |= DIRTY_LOCAL;
_propagate_transform_changed(this);
if (data.notify_local_transform) {
@ -340,8 +365,9 @@ void Node3D::set_rotation_order(RotationOrder p_order) {
ERR_FAIL_INDEX(int32_t(order), 6);
if (data.dirty & DIRTY_VECTORS) {
data.rotation = data.local_transform.basis.get_euler_normalized(order);
data.scale = data.local_transform.basis.get_scale();
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(order);
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
} else {
data.rotation = Basis::from_euler(data.rotation, data.rotation_order).get_euler_normalized(order);
@ -359,7 +385,8 @@ Node3D::RotationOrder Node3D::get_rotation_order() const {
void Node3D::set_rotation(const Vector3 &p_euler_rad) {
if (data.dirty & DIRTY_VECTORS) {
data.scale = data.local_transform.basis.get_scale();
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
}
@ -373,7 +400,8 @@ void Node3D::set_rotation(const Vector3 &p_euler_rad) {
void Node3D::set_scale(const Vector3 &p_scale) {
if (data.dirty & DIRTY_VECTORS) {
data.rotation = data.local_transform.basis.get_euler_normalized(data.rotation_order);
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.dirty &= ~DIRTY_VECTORS;
}
@ -386,14 +414,14 @@ void Node3D::set_scale(const Vector3 &p_scale) {
}
Vector3 Node3D::get_position() const {
return data.local_transform.origin;
return get_transform().origin;
}
Vector3 Node3D::get_rotation() const {
if (data.dirty & DIRTY_VECTORS) {
data.scale = data.local_transform.basis.get_scale();
data.rotation = data.local_transform.basis.get_euler_normalized(data.rotation_order);
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
}
@ -402,9 +430,9 @@ Vector3 Node3D::get_rotation() const {
Vector3 Node3D::get_scale() const {
if (data.dirty & DIRTY_VECTORS) {
data.scale = data.local_transform.basis.get_scale();
data.rotation = data.local_transform.basis.get_euler_normalized(data.rotation_order);
data.quaternion = get_transform().basis.get_rotation_quaternion();
data.rotation = get_transform().basis.get_euler_normalized(data.rotation_order);
data.scale = get_transform().basis.get_scale();
data.dirty &= ~DIRTY_VECTORS;
}
@ -865,7 +893,7 @@ Variant Node3D::property_get_revert(const String &p_name) {
} else if (p_name == "quaternion") {
Variant variant = PropertyUtils::get_property_default_value(this, "transform", &valid);
if (valid && variant.get_type() == Variant::Type::TRANSFORM3D) {
r_ret = Quaternion(Transform3D(variant).get_basis());
r_ret = Transform3D(variant).get_basis().get_rotation_quaternion();
} else {
return Quaternion();
}

View File

@ -81,6 +81,7 @@ private:
mutable Transform3D global_transform;
mutable Transform3D local_transform;
mutable Basis::EulerOrder rotation_order = Basis::EULER_ORDER_YXZ;
mutable Quaternion quaternion;
mutable Vector3 rotation;
mutable Vector3 scale = Vector3(1, 1, 1);
mutable RotationEditMode rotation_edit_mode = ROTATION_EDIT_MODE_EULER;