Skip to content

Commit

Permalink
Implemented scaling with global orientation (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
urholaukkarinen committed Oct 6, 2024
1 parent 8dbdd1e commit edce73e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
14 changes: 1 addition & 13 deletions crates/transform-gizmo/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 19 additions & 5 deletions crates/transform-gizmo/src/gizmo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -287,14 +287,28 @@ impl Gizmo {
}

fn update_scale(
&self,
transform: &Transform,
start_transform: &Transform,
scale: mint::Vector3<f64>,
) -> 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
}
}

Expand Down

0 comments on commit edce73e

Please sign in to comment.