Skip to content

Commit 6cd98b3

Browse files
authored
Add TextureAtlasSources::uv_rect to get Rect in UV coords (#18178)
# Objective I'm building a bloxel game in which I (currently) use a texture atlas to render the blocks the world is made of. While I was coding it, I was using the `TextureAtlas...` types to build the terrain's texture atlas at runtime as shown in the [example](https://github.com/bevyengine/bevy/blob/latest/examples/2d/texture_atlas.rs). But when I was using it to build a 3D mesh out of the blocks, I found that there was no easy way get the texture rect in UV coordinates, only in pixels via `texture_rect()`. I had to resort to writing code like this: ```rs let size = layout.size.as_vec2(); if let Some(rect) = sources.texture_rect(layout, texture) { let rect = rect.as_rect(); let uvs = Rect::from_corners(rect.min / size, rect.max / size); // use the UVs here, such as to build vertex buffer } ``` That is, until I wrote a helper function that's practically identical to the one in this PR. ## Solution Add a `uv_rect` function to `TextureAtlasSources` that will return a `Rect` with coordinates in the range of 0.0 to 1.0 – that is, UV coordinates – which can then be used directly to build `Vec2` UV values to put into a buffer and send to the GPU. I'm a little unsure about the wording of the `texture_rect` documentation but I kept it intact and based mine on it. If you think this could be improved and have some advice, I'd love to include that in this PR. ## Testing I've not done any testing with the updated bevy branch, other than seeing that the original helper function (identical in functionality) worked in my currently very small project, and making sure `cargo build` doesn't error, but I'm new to making changes to Bevy so unsure if this is sufficient. ## Showcase ![image](https://github.com/user-attachments/assets/a6d25608-e4ea-4cfd-ba1f-911dc4430138)
1 parent cca5813 commit 6cd98b3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

crates/bevy_image/src/texture_atlas.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy_app::prelude::*;
22
use bevy_asset::{Asset, AssetApp as _, AssetId, Assets, Handle};
3-
use bevy_math::{URect, UVec2};
3+
use bevy_math::{Rect, URect, UVec2};
44
use bevy_platform_support::collections::HashMap;
55
#[cfg(feature = "bevy_reflect")]
66
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
@@ -51,14 +51,28 @@ impl TextureAtlasSources {
5151
})
5252
}
5353

54-
/// Retrieves the texture *section* rectangle of the given `texture` handle.
54+
/// Retrieves the texture *section* rectangle of the given `texture` handle in pixels.
5555
pub fn texture_rect(
5656
&self,
5757
layout: &TextureAtlasLayout,
5858
texture: impl Into<AssetId<Image>>,
5959
) -> Option<URect> {
6060
layout.textures.get(self.texture_index(texture)?).cloned()
6161
}
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+
}
6276
}
6377

6478
/// Stores a map used to lookup the position of a texture in a [`TextureAtlas`].

0 commit comments

Comments
 (0)