Skip to content

texture atlas top & left padding#23056

Open
Wuketuke wants to merge 7 commits into
bevyengine:mainfrom
Wuketuke:issue-22776_texture-atlas-padding
Open

texture atlas top & left padding#23056
Wuketuke wants to merge 7 commits into
bevyengine:mainfrom
Wuketuke:issue-22776_texture-atlas-padding

Conversation

@Wuketuke
Copy link
Copy Markdown
Contributor

@Wuketuke Wuketuke commented Feb 19, 2026

fixes #22776

A TextureAtlas can now have padding on the top and left edge.
Since the user probably wants to set this together with the normal padding, i changed the TextureAtlasBuilder::padding function so that both the initial padding and the per-image padding gets set together.
Should the user wish to have a different initial padding value, i added a initial_padding function

Testing

i ran the 2d/texture_atlas.rs example, and saved each image to a file.
i then manually checked if the padding is inserted correctly
for that i slightly modified the example code like this

fn create_texture_atlas( ... ) {
    // ...
    // line 247
    let (texture_atlas_layout, texture_atlas_sources, texture) =
        texture_atlas_builder.build().unwrap();

    let nr = counter();
    println!("SAVING IMAGE");
    texture
        .convert(bevy_render::render_resource::TextureFormat::Rgba8UnormSrgb)
        .unwrap()
        .try_into_dynamic()
        .unwrap()
        .save(format!("TEMP_{nr}.png"))
        .unwrap();

    let texture = textures.add(texture);

    // ...
}

static TEMP: std::sync::OnceLock<std::sync::Mutex<u8>> = std::sync::OnceLock::new();
fn counter() -> u8 {
    let x = TEMP.get_or_init(|| std::sync::Mutex::new(0));
    let mut x = x.lock().unwrap();
    let val = *x;
    *x += 1;
    val
}

@kfc35 kfc35 added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 19, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in Rendering Feb 19, 2026
@@ -104,8 +104,20 @@ impl<'a> TextureAtlasBuilder<'a> {
/// Sets the amount of padding in pixels to add between the textures in the texture atlas.
///
/// The `x` value provide will be added to the right edge, while the `y` value will be added to the bottom edge.
///
/// calling this function will also set the initial padding (on the top and left edge).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// calling this function will also set the initial padding (on the top and left edge).
/// calling this function will also set the initial padding (`x` for the left edge and `y` for the top)

@@ -104,8 +104,20 @@ impl<'a> TextureAtlasBuilder<'a> {
/// Sets the amount of padding in pixels to add between the textures in the texture atlas.
///
/// The `x` value provide will be added to the right edge, while the `y` value will be added to the bottom edge.
///
/// calling this function will also set the initial padding (on the top and left edge).
/// call [`initial_padding`] to set that value only
Copy link
Copy Markdown
Contributor

@kfc35 kfc35 Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// call [`initial_padding`] to set that value only
/// call [`initial_padding`](TextureAtlasBuilder::initial_padding) to set the left and top padding values only

self
}

/// Sets the amount of padding in pixels to add on the top and left edge of the texture atlas.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Sets the amount of padding in pixels to add on the top and left edge of the texture atlas.
/// Sets the amount of padding in pixels to add on the left and top edge of the texture atlas.

just to make it consistent that the “first” aka x number is for the left edge

@@ -114,13 +126,14 @@ impl<'a> TextureAtlasBuilder<'a> {
texture: &Image,
packed_location: &PackedLocation,
padding: UVec2,
init_padding: UVec2,
) -> TextureAtlasBuilderResult<()> {
let rect_width = (packed_location.width() - padding.x) as usize;

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe Feb 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it still pack the rects with padding only added on the bottom and right. Rectangle Pack is passed a single TargetBin with its size equal to the atlas target size minus initial_padding. Then the returned packed rects are displaced right and down by intial_padding to leave the top left padded region clear.

@kfc35 kfc35 requested a review from ickshonpe February 20, 2026 01:02
Copy link
Copy Markdown
Contributor

@ickshonpe ickshonpe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an atlas test thing to testbed_2d (#23074) and everything looks good:

Image Image Image

The default texture atlas packing behaviour is changed, so we need a migration guide. Just has to explain that padding is now added on the top and left edges, and that users can call initial_padding(UVec2::ZERO) to get the old behaviour.

Comment on lines +46 to +47
/// The padding along the left and top edges of the `TextureAtlas`
initial_padding: UVec2,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any reason we would want the top left to have a different padding thickness. It might be clearer to just make this a bool, and reuse the value from the padding field.

@ickshonpe ickshonpe added the M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Feb 20, 2026
github-merge-queue Bot pushed a commit that referenced this pull request Feb 21, 2026
…23091)

# Objective

- #22939 has been introducing diffs in the testbed_ui Text example, but
the only thing that was changing was the order that glyphs are allocated
to the font atlas. The font atlas **should be** resilient to this and
produce no diffs in that case.
- Another PR had similar diffs and also seemed to be due to changing the
order of glyphs being allocated.

## Solution

- Previously `DynamicTextureAtlasBuilder` would allocate glyph size +
padding, and then copy the glyph into that rectangle - but this means
that copying would start at (0,0) meaning we would "blend" with the top
and left edges (which breaks things).
- Now we copy to (padding, padding).
- Remove (padding, padding) allocate-able space from the bottom and
right to ensure we don't accidentally put textures on the right or
bottom edges.

## Testing

- Added unit tests to ensure that allocation works and that padding
works.
- The rendering change here is quite interesting!
https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23091
Some cases, like the `l` in `black` in the Text screenshot look
**substantially** better!

Note this is orthogonal to #23056 which is about `TextureAtlasBuilder`,
not `DynamicTextureAtlasBuilder`.
@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Uncontroversial This work is generally agreed upon and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 23, 2026
@ickshonpe
Copy link
Copy Markdown
Contributor

ickshonpe commented Feb 25, 2026

Added this to the release note included with #23132

@github-actions
Copy link
Copy Markdown
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23056

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

1 similar comment
@github-actions
Copy link
Copy Markdown
Contributor

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23056

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

Your PR caused a change in the graphical output of an example or rendering test. This might be intentional, but it could also mean that something broke!
You can review it at https://pixel-eagle.com/project/B04F67C0-C054-4A6F-92EC-F599FEC2FD1D?filter=PR-23056

If it's expected, please add the M-Deliberate-Rendering-Change label.

If this change seems unrelated to your PR, you can consider updating your PR to target the latest main branch, either by rebasing or merging main into it.

pull Bot pushed a commit to octoape/bevy that referenced this pull request May 8, 2026
# Objective

- Migration guide merged in release-content

## Solution

- Move it
- Add a CI check that will block new merges

Opened PRs that sill change that folder:
- bevyengine#23467
- bevyengine#23445
- bevyengine#23373
- bevyengine#23137
- bevyengine#23132
- bevyengine#23056
- bevyengine#22917
- bevyengine#22852
- bevyengine#22782
- bevyengine#22670
- bevyengine#22557
- bevyengine#22500
- bevyengine#21929
- bevyengine#21912
- bevyengine#21897
- bevyengine#21893
- bevyengine#21890
- bevyengine#21889
- bevyengine#21839
- bevyengine#21811
- bevyengine#21772

None of them are likely to be merged in the 0.19
@ickshonpe
Copy link
Copy Markdown
Contributor

@Wuketuke do you still want to finish this, or should someone else take it over?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Rendering Drawing game state to the screen C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Uncontroversial This work is generally agreed upon

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

bevy_image::TextureAtlasBuilder: Symmetric texture atlas padding

5 participants