bevy/examples/2d
Ida "Iyes 31409ebc61
Add Image methods for easy access to a pixel's color (#10392)
# Objective

If you want to draw / generate images from the CPU, such as:
 - to create procedurally-generated assets
- for games whose artstyle is best implemented by poking pixels directly
from the CPU, instead of using shaders

It is currently very unergonomic to do in Bevy, because you have to deal
with the raw bytes inside `image.data`, take care of the pixel format,
etc.

## Solution

This PR adds some helper methods to `Image` for pixel manipulation.
These methods allow you to use Bevy's user-friendly `Color` struct to
read and write the colors of pixels, at arbitrary coordinates (specified
as `UVec3` to support any texture dimension). They handle
encoding/decoding to the `Image`s `TextureFormat`, incl. any sRGB
conversion.

While we are at it, also add methods to help with direct access to the
raw bytes. It is now easy to compute the offset where the bytes of a
specific pixel coordinate are found, or to just get a Rust slice to
access them.

Caveat: `Color` roundtrips are obviously going to be lossy for non-float
`TextureFormat`s. Using `set_color_at` followed by `get_color_at` will
return a different value, due to the data conversions involved (such as
`f32` -> `u8` -> `f32` for the common `Rgba8UnormSrgb` texture format).
Be careful when comparing colors (such as checking for a color you wrote
before)!

Also adding a new example: `cpu_draw` (under `2d`), to showcase these
new APIs.

---

## Changelog

### Added

 - `Image` APIs for easy access to the colors of specific pixels.

---------

Co-authored-by: Pascal Hertleif <killercup@gmail.com>
Co-authored-by: François <mockersf@gmail.com>
Co-authored-by: ltdk <usr@ltdk.xyz>
2024-10-07 14:38:41 +00:00
..
2d_shapes.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
2d_viewport_to_world.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
bloom_2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
bounding_2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
cpu_draw.rs Add Image methods for easy access to a pixel's color (#10392) 2024-10-07 14:38:41 +00:00
custom_gltf_vertex_attribute.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
mesh2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
mesh2d_alpha_mode.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
mesh2d_arcs.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
mesh2d_manual.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
mesh2d_vertex_color_texture.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
move_sprite.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
pixel_grid_snap.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
rotation.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite_animation.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite_flipping.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite_sheet.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite_slice.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
sprite_tile.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
text2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
texture_atlas.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
transparency_2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00
wireframe_2d.rs Migrate cameras to required components (#15641) 2024-10-05 01:59:52 +00:00