|
1 | 1 | use bevy_app::prelude::*;
|
2 | 2 | use bevy_asset::{Asset, AssetApp as _, AssetId, Assets, Handle};
|
3 |
| -use bevy_math::{URect, UVec2}; |
| 3 | +use bevy_math::{Rect, URect, UVec2}; |
4 | 4 | use bevy_platform_support::collections::HashMap;
|
5 | 5 | #[cfg(feature = "bevy_reflect")]
|
6 | 6 | use bevy_reflect::{std_traits::ReflectDefault, Reflect};
|
@@ -51,14 +51,28 @@ impl TextureAtlasSources {
|
51 | 51 | })
|
52 | 52 | }
|
53 | 53 |
|
54 |
| - /// Retrieves the texture *section* rectangle of the given `texture` handle. |
| 54 | + /// Retrieves the texture *section* rectangle of the given `texture` handle in pixels. |
55 | 55 | pub fn texture_rect(
|
56 | 56 | &self,
|
57 | 57 | layout: &TextureAtlasLayout,
|
58 | 58 | texture: impl Into<AssetId<Image>>,
|
59 | 59 | ) -> Option<URect> {
|
60 | 60 | layout.textures.get(self.texture_index(texture)?).cloned()
|
61 | 61 | }
|
| 62 | + |
| 63 | + /// Retrieves the texture *section* rectangle of the given `texture` handle in UV coordinates. |
| 64 | + /// These are within the range [0..1], as a fraction of the entire texture atlas' size. |
| 65 | + pub fn uv_rect( |
| 66 | + &self, |
| 67 | + layout: &TextureAtlasLayout, |
| 68 | + texture: impl Into<AssetId<Image>>, |
| 69 | + ) -> Option<Rect> { |
| 70 | + self.texture_rect(layout, texture).map(|rect| { |
| 71 | + let rect = rect.as_rect(); |
| 72 | + let size = layout.size.as_vec2(); |
| 73 | + Rect::from_corners(rect.min / size, rect.max / size) |
| 74 | + }) |
| 75 | + } |
62 | 76 | }
|
63 | 77 |
|
64 | 78 | /// Stores a map used to lookup the position of a texture in a [`TextureAtlas`].
|
|
0 commit comments