Skip to content

Commit

Permalink
Write release note for PR 10392 (CPU Drawing) (#1854)
Browse files Browse the repository at this point in the history
Co-authored-by: Alice Cecile <[email protected]>
  • Loading branch information
inodentry and alice-i-cecile authored Nov 27, 2024
1 parent e776c9c commit 93aa1bc
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
61 changes: 61 additions & 0 deletions release-content/0.15/release-notes/10392_CPU_Drawing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
There are many situations where you might want to just set the color of pixels
from CPU code. Procedural assets, certain art styles, or simply because
it is easier. No need to bother with shaders and materials, when you just want
to change a few specific pixels!

In previous versions of Bevy, this was difficult and tedious. Bevy gives you
access to the raw data bytes of an [`Image`], but you had to compute the byte
offset corresponding to your desired pixel coordinate, make sure to encode your
bytes with respect to the [`TextureFormat`], etc. Very low level!

In Bevy 0.15, there are now user-friendly APIs for reading and writing the
colors of pixels in an [`Image`]. The tricky low-level details are dealt with
for you! You can even use `bevy_color`'s fancy color space APIs!

```rust
fn my_system(mut images: ResMut<Assets<Image>>, mut commands: Commands) {
// Create a new image.
let mut image = Image::new_fill(
// 64x64 size
Extent3d {
width: 64,
height: 64,
depth_or_array_layers: 1,
},
TextureDimension::D2,
&Srgba::WHITE.to_u8_array(),
TextureFormat::Rgba8UnormSrgb,
RenderAssetUsages::all(),
);

// This is new:

// Make the pixel at x: 23, y: 32 magenta
image.set_color_at(23, 32, Color::srgb(1.0, 0.0, 1.0))
.expect("Error writing color");

// Set the pixel at 10,10 to a color specified using the Oklch color space:
image.set_color_at(10, 10, Color::oklch(0.3, 0.2, 0.5))
.expect("Error writing color");

// read the bytes of the pixel we just wrote:
let bytes = image.pixel_bytes(UVec3::new(10, 10, 0)).unwrap();

// read the (approximate) color back (as sRGB):
let color = image.get_color_at(10, 10);

// We could add our new image to Bevy's assets
// and spawn a sprite to display it:
commands.spawn(Sprite {
image: images.add(image),
..default()
});
}
```

Note: The [`Color`]-based methods are lossy. They have to convert to/from the
[`Image`]'s [`TextureFormat`]. If you read back the color you wrote, it will be slightly different.

[`Image`]: https://docs.rs/bevy/0.15.0-rc.3/bevy/prelude/struct.Image.html
[`TextureFormat`]: https://docs.rs/bevy/0.15.0-rc.3/bevy/render/render_resource/enum.TextureFormat.html
[`Color`]: https://docs.rs/bevy/0.15.0-rc.3/bevy/color/enum.Color.html
7 changes: 7 additions & 0 deletions release-content/0.15/release-notes/_release-notes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ contributors = ["@kristoff3r", "@tychedelia", "@JMS55"]
prs = [14876]
file_name = "14876_Add_Order_Independent_Transparency.md"

[[release_notes]]
title = "User-Friendly CPU Drawing"
authors = ["@inodentry"]
contributors = []
prs = [10392]
file_name = "10392_CPU_Drawing.md"

[[release_notes]]
title = "Entity Picking / Selection"
authors = ["@aevyrie", "@NthTensor", "@TotalKrill", "@jnhyatt", "@Jondolf"]
Expand Down

0 comments on commit 93aa1bc

Please sign in to comment.