Skip to content

Commit e797807

Browse files
committed
flipping texture coords methods has been added to the StandardMaterial
1 parent 11817f4 commit e797807

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 59 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,64 @@ 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+
self.uv_transform = Self::FLIP_HORIZONTAL * self.uv_transform;
535+
}
536+
if vertical {
537+
self.uv_transform = Self::FLIP_VERTICAL * self.uv_transform;
538+
}
539+
}
540+
541+
/// Consumes the material and returns a material with flipped texture coordinates
542+
pub fn flipped(mut self, horizontal: bool, vertical: bool) -> Self {
543+
self.flip(horizontal, vertical);
544+
self
545+
}
546+
}
547+
490548
impl Default for StandardMaterial {
491549
fn default() -> Self {
492550
StandardMaterial {

0 commit comments

Comments
 (0)