Skip to content

Commit ddcbb3c

Browse files
bugsweeperBD103
andauthored
flipping texture coords methods has been added to the StandardMaterial (#12917)
# Objective Fixes #11996 The deprecated shape Quad's flip field role migrated to StandardMaterial's flip/flipped methods ## Solution flip/flipping methods of StandardMaterial is applicable to any mesh --- ## Changelog - Added flip and flipped methods to the StandardMaterial implementation - Added FLIP_HORIZONTAL, FLIP_VERTICAL, FLIP_X, FLIP_Y, FLIP_Z constants ## Migration Guide Instead of using `Quad::flip` field, call `flipped(true, false)` method on the StandardMaterial instance when adding the mesh. --------- Co-authored-by: BD103 <[email protected]>
1 parent 597799a commit ddcbb3c

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy_asset::Asset;
22
use bevy_color::Alpha;
3-
use bevy_math::{Affine2, Mat3, Vec4};
3+
use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4};
44
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
55
use bevy_render::{
66
mesh::MeshVertexBufferLayoutRef, render_asset::RenderAssets, render_resource::*,
@@ -487,6 +487,66 @@ pub struct StandardMaterial {
487487
pub uv_transform: Affine2,
488488
}
489489

490+
impl StandardMaterial {
491+
/// Horizontal flipping transform
492+
///
493+
/// Multiplying this with another Affine2 returns transformation with horizontally flipped texture coords
494+
pub const FLIP_HORIZONTAL: Affine2 = Affine2 {
495+
matrix2: Mat2::from_cols(Vec2::new(-1.0, 0.0), Vec2::Y),
496+
translation: Vec2::X,
497+
};
498+
499+
/// Vertical flipping transform
500+
///
501+
/// Multiplying this with another Affine2 returns transformation with vertically flipped texture coords
502+
pub const FLIP_VERTICAL: Affine2 = Affine2 {
503+
matrix2: Mat2::from_cols(Vec2::X, Vec2::new(0.0, -1.0)),
504+
translation: Vec2::Y,
505+
};
506+
507+
/// Flipping X 3D transform
508+
///
509+
/// Multiplying this with another Affine3 returns transformation with flipped X coords
510+
pub const FLIP_X: Affine3 = Affine3 {
511+
matrix3: Mat3::from_cols(Vec3::new(-1.0, 0.0, 0.0), Vec3::Y, Vec3::Z),
512+
translation: Vec3::X,
513+
};
514+
515+
/// Flipping Y 3D transform
516+
///
517+
/// Multiplying this with another Affine3 returns transformation with flipped Y coords
518+
pub const FLIP_Y: Affine3 = Affine3 {
519+
matrix3: Mat3::from_cols(Vec3::X, Vec3::new(0.0, -1.0, 0.0), Vec3::Z),
520+
translation: Vec3::Y,
521+
};
522+
523+
/// Flipping Z 3D transform
524+
///
525+
/// Multiplying this with another Affine3 returns transformation with flipped Z coords
526+
pub const FLIP_Z: Affine3 = Affine3 {
527+
matrix3: Mat3::from_cols(Vec3::X, Vec3::Y, Vec3::new(0.0, 0.0, -1.0)),
528+
translation: Vec3::Z,
529+
};
530+
531+
/// Flip the texture coordinates of the material.
532+
pub fn flip(&mut self, horizontal: bool, vertical: bool) {
533+
if horizontal {
534+
// Multiplication of `Affine2` is order dependent, which is why
535+
// we do not use the `*=` operator.
536+
self.uv_transform = Self::FLIP_HORIZONTAL * self.uv_transform;
537+
}
538+
if vertical {
539+
self.uv_transform = Self::FLIP_VERTICAL * self.uv_transform;
540+
}
541+
}
542+
543+
/// Consumes the material and returns a material with flipped texture coordinates
544+
pub fn flipped(mut self, horizontal: bool, vertical: bool) -> Self {
545+
self.flip(horizontal, vertical);
546+
self
547+
}
548+
}
549+
490550
impl Default for StandardMaterial {
491551
fn default() -> Self {
492552
StandardMaterial {

0 commit comments

Comments
 (0)