From 4c8452c84124f692f55d547744b2ad65ff8e293f Mon Sep 17 00:00:00 2001 From: Urho Laukkarinen Date: Sun, 6 Oct 2024 13:53:29 +0300 Subject: [PATCH] Implemented scaling with global orientation (#7) --- crates/transform-gizmo/src/config.rs | 14 +------------- crates/transform-gizmo/src/gizmo.rs | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/crates/transform-gizmo/src/config.rs b/crates/transform-gizmo/src/config.rs index 6823795..16f1578 100644 --- a/crates/transform-gizmo/src/config.rs +++ b/crates/transform-gizmo/src/config.rs @@ -93,19 +93,7 @@ impl GizmoConfig { /// Transform orientation of the gizmo pub(crate) fn orientation(&self) -> GizmoOrientation { - if self.is_scaling() { - // Scaling currently only works in local orientation, - // so the configured orientation is ignored. - GizmoOrientation::Local - } else { - self.orientation - } - } - - /// Whether the config includes any scaling modes - fn is_scaling(&self) -> bool { - (self.mode_override.is_none() && !self.modes.is_disjoint(GizmoMode::all_scale())) - || self.mode_override.filter(GizmoMode::is_scale).is_some() + self.orientation } /// Whether the modes have changed, compared to given other config diff --git a/crates/transform-gizmo/src/gizmo.rs b/crates/transform-gizmo/src/gizmo.rs index 92f2f2b..c794d5c 100644 --- a/crates/transform-gizmo/src/gizmo.rs +++ b/crates/transform-gizmo/src/gizmo.rs @@ -9,7 +9,7 @@ use crate::config::{ use crate::math::{screen_to_world, Transform}; use crate::GizmoOrientation; use epaint::Mesh; -use glam::{DQuat, DVec3}; +use glam::{DMat4, DQuat, DVec3}; use crate::subgizmo::rotation::RotationParams; use crate::subgizmo::scale::ScaleParams; @@ -223,7 +223,7 @@ impl Gizmo { self.update_translation(delta, transform, start_transform) } GizmoResult::Scale { total } => { - Self::update_scale(transform, start_transform, total) + self.update_scale(transform, start_transform, total) } GizmoResult::Arcball { delta, total: _ } => { self.update_rotation_quat(transform, delta.into()) @@ -287,14 +287,28 @@ impl Gizmo { } fn update_scale( + &self, transform: &Transform, start_transform: &Transform, scale: mint::Vector3, ) -> Transform { + let new_scale = match self.config.orientation() { + GizmoOrientation::Global => { + let scaled_transform_mat = DMat4::from_scale(scale.into()) + * DMat4::from_scale_rotation_translation( + DVec3::from(start_transform.scale), + DQuat::from(start_transform.rotation), + DVec3::from(start_transform.translation), + ); + let (scale, _, _) = scaled_transform_mat.to_scale_rotation_translation(); + scale + } + GizmoOrientation::Local => DVec3::from(start_transform.scale) * DVec3::from(scale), + }; + Transform { - scale: (DVec3::from(start_transform.scale) * DVec3::from(scale)).into(), - rotation: transform.rotation, - translation: transform.translation, + scale: new_scale.into(), + ..*transform } }