|
1 | 1 | use bevy_asset::Asset;
|
2 | 2 | use bevy_color::Alpha;
|
3 |
| -use bevy_math::{Affine2, Mat3, Vec4}; |
| 3 | +use bevy_math::{Affine2, Affine3, Mat2, Mat3, Vec2, Vec3, Vec4}; |
4 | 4 | use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
5 | 5 | use bevy_render::{
|
6 | 6 | mesh::MeshVertexBufferLayoutRef, render_asset::RenderAssets, render_resource::*,
|
@@ -487,6 +487,66 @@ pub struct StandardMaterial {
|
487 | 487 | pub uv_transform: Affine2,
|
488 | 488 | }
|
489 | 489 |
|
| 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 | + |
490 | 550 | impl Default for StandardMaterial {
|
491 | 551 | fn default() -> Self {
|
492 | 552 | StandardMaterial {
|
|
0 commit comments