Skip to content

Commit

Permalink
flipping texture coords methods has been added to the StandardMaterial
Browse files Browse the repository at this point in the history
  • Loading branch information
bugsweeper committed Apr 10, 2024
1 parent 11817f4 commit 98fdc6f
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy_asset::Asset;
use bevy_color::Alpha;
use bevy_math::{Affine2, Mat3, Vec4};
use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4};
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use bevy_render::{
mesh::MeshVertexBufferLayoutRef, render_asset::RenderAssets, render_resource::*,
Expand Down Expand Up @@ -487,6 +487,64 @@ pub struct StandardMaterial {
pub uv_transform: Affine2,
}

impl StandardMaterial {
/// Horizontal flipping transform
///
/// Multiplying this with another Affine2 returns transformation with horizontally flipped texture coords
pub const FLIP_HORIZONTAL: Affine2 = Affine2 {
matrix2: Mat2::from_cols(Vec2::new(-1.0, 0.0), Vec2::Y),
translation: Vec2::X,
};

/// Vertical flipping transform
///
/// Multiplying this with another Affine2 returns transformation with vertically flipped texture coords
pub const FLIP_VERTICAL: Affine2 = Affine2 {
matrix2: Mat2::from_cols(Vec2::X, Vec2::new(0.0, -1.0)),
translation: Vec2::Y,
};

/// Flipping X 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped X coords
pub const FLIP_X: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::new(-1.0, 0.0, 0.0), Vec3::Y, Vec3::Z),
translation: Vec3::X,
};

/// Flipping Y 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped Y coords
pub const FLIP_Y: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::X, Vec3::new(0.0, -1.0, 0.0), Vec3::Z),
translation: Vec3::Y,
};

/// Flipping Z 3D transform
///
/// Multiplying this with another Affine3 returns transformation with flipped Z coords
pub const FLIP_Z: Affine3 = Affine3 {
matrix3: Mat3::from_cols(Vec3::X, Vec3::Y, Vec3::new(0.0, 0.0, -1.0)),
translation: Vec3::Z,
};

/// Flip the texture coordinates of the material.
pub fn flip(&mut self, horizontal: bool, vertical: bool) {
if horizontal {
self.uv_transform = Self::FLIP_HORIZONTAL * self.uv_transform;
}
if vertical {
self.uv_transform = Self::FLIP_VERTICAL * self.uv_transform;
}
}

/// Consumes the material and returns a material with flipped texture cooridnates

Check warning on line 541 in crates/bevy_pbr/src/pbr_material.rs

View workflow job for this annotation

GitHub Actions / typos

"cooridnates" should be "coordinates".
pub fn flipped(mut self, horizontal: bool, vertical: bool) -> Self {
self.flip(horizontal, vertical);
self
}
}

impl Default for StandardMaterial {
fn default() -> Self {
StandardMaterial {
Expand Down

0 comments on commit 98fdc6f

Please sign in to comment.