diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 9bddeb3bff..1053a82551 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -28,11 +28,22 @@ impl GlobalTransform { GlobalTransform::new(Mat4::from_quat(rotation)) } - // TODO: make sure scale is positive - pub fn from_scale(scale: Vec3) -> Self { + pub fn from_scale(scale: f32) -> Self { + GlobalTransform::new(Mat4::from_scale(Vec3::splat(scale))) + } + + pub fn from_non_uniform_scale(scale: Vec3) -> Self { GlobalTransform::new(Mat4::from_scale(scale)) } + pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self { + GlobalTransform::new(Mat4::from_scale_rotation_translation( + Vec3::splat(scale), + rotation, + translation, + )) + } + pub fn with_translation(mut self, translation: Vec3) -> Self { self.translate(translation); self @@ -43,11 +54,16 @@ impl GlobalTransform { self } - pub fn with_scale(mut self, scale: Vec3) -> Self { + pub fn with_scale(mut self, scale: f32) -> Self { self.apply_scale(scale); self } + pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self { + self.apply_non_uniform_scale(scale); + self + } + pub fn value(&self) -> &Mat4 { &self.value } @@ -83,14 +99,21 @@ impl GlobalTransform { } pub fn set_rotation(&mut self, rotation: Quat) { - let rotation = rotation * self.rotation().conjugate(); - rotation.normalize(); - self.value = Mat4::from_quat(rotation) * self.value; + self.value = + Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation()); } - pub fn set_scale(&mut self, scale: Vec3) { - let scale = scale / self.scale(); - self.value = Mat4::from_scale(scale) * self.value; + pub fn set_scale(&mut self, scale: f32) { + self.value = Mat4::from_scale_rotation_translation( + Vec3::splat(scale), + self.rotation(), + self.translation(), + ); + } + + pub fn set_non_uniform_scale(&mut self, scale: Vec3) { + self.value = + Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation()); } pub fn translate(&mut self, translation: Vec3) { @@ -101,7 +124,11 @@ impl GlobalTransform { self.value = Mat4::from_quat(rotation) * self.value; } - pub fn apply_scale(&mut self, scale: Vec3) { + pub fn apply_scale(&mut self, scale: f32) { + self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; + } + + pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { self.value = Mat4::from_scale(scale) * self.value; } } diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 692fc7df0b..e7fba66519 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -28,8 +28,19 @@ impl Transform { Transform::new(Mat4::from_quat(rotation)) } - // TODO: make sure scale is positive - pub fn from_scale(scale: Vec3) -> Self { + pub fn from_scale(scale: f32) -> Self { + Transform::new(Mat4::from_scale(Vec3::splat(scale))) + } + + pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self { + Transform::new(Mat4::from_scale_rotation_translation( + Vec3::splat(scale), + rotation, + translation, + )) + } + + pub fn from_non_uniform_scale(scale: Vec3) -> Self { Transform::new(Mat4::from_scale(scale)) } @@ -43,11 +54,16 @@ impl Transform { self } - pub fn with_scale(mut self, scale: Vec3) -> Self { + pub fn with_scale(mut self, scale: f32) -> Self { self.apply_scale(scale); self } + pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self { + self.apply_non_uniform_scale(scale); + self + } + pub fn value(&self) -> &Mat4 { &self.value } @@ -87,14 +103,21 @@ impl Transform { } pub fn set_rotation(&mut self, rotation: Quat) { - let rotation = rotation * self.rotation().conjugate(); - rotation.normalize(); - self.value = Mat4::from_quat(rotation) * self.value; + self.value = + Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation()); } - pub fn set_scale(&mut self, scale: Vec3) { - let scale = scale / self.scale(); - self.value = Mat4::from_scale(scale) * self.value; + pub fn set_scale(&mut self, scale: f32) { + self.value = Mat4::from_scale_rotation_translation( + Vec3::splat(scale), + self.rotation(), + self.translation(), + ); + } + + pub fn set_non_uniform_scale(&mut self, scale: Vec3) { + self.value = + Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation()); } pub fn translate(&mut self, translation: Vec3) { @@ -105,7 +128,11 @@ impl Transform { self.value = Mat4::from_quat(rotation) * self.value; } - pub fn apply_scale(&mut self, scale: Vec3) { + pub fn apply_scale(&mut self, scale: f32) { + self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; + } + + pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { self.value = Mat4::from_scale(scale) * self.value; } } diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index 32a8f473d6..bc1c0b3a7a 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -39,7 +39,7 @@ fn setup( .spawn(Camera2dComponents::default()) .spawn(SpriteSheetComponents { texture_atlas: texture_atlas_handle, - transform: Transform::from_scale(Vec3::one() * 6.0), + transform: Transform::from_scale(6.0), ..Default::default() }) .with(Timer::from_seconds(0.1, true)); diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index 377602ce71..f7681e5f50 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -61,8 +61,7 @@ fn load_atlas( .spawn(Camera2dComponents::default()) // draw a sprite from the atlas .spawn(SpriteSheetComponents { - transform: Transform::from_translation(Vec3::new(150.0, 0.0, 0.0)) - .with_scale(Vec3::one() * 4.0), + transform: Transform::from_scale(4.0).with_translation(Vec3::new(150.0, 0.0, 0.0)), sprite: TextureAtlasSprite::new(vendor_index as u32), texture_atlas: atlas_handle, ..Default::default() diff --git a/examples/ecs/parallel_query.rs b/examples/ecs/parallel_query.rs index 05e55faade..65981a18c8 100644 --- a/examples/ecs/parallel_query.rs +++ b/examples/ecs/parallel_query.rs @@ -15,7 +15,7 @@ fn spawn_system( commands .spawn(SpriteComponents { material, - transform: Transform::from_scale(Vec3::one() * 0.1), + transform: Transform::from_scale(0.1), ..Default::default() }) .with(Velocity(