mirror of
https://github.com/bevyengine/bevy
synced 2024-11-15 09:27:41 +00:00
31409ebc61
# 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> |
||
---|---|---|
.. | ||
2d_shapes.rs | ||
2d_viewport_to_world.rs | ||
bloom_2d.rs | ||
bounding_2d.rs | ||
cpu_draw.rs | ||
custom_gltf_vertex_attribute.rs | ||
mesh2d.rs | ||
mesh2d_alpha_mode.rs | ||
mesh2d_arcs.rs | ||
mesh2d_manual.rs | ||
mesh2d_vertex_color_texture.rs | ||
move_sprite.rs | ||
pixel_grid_snap.rs | ||
rotation.rs | ||
sprite.rs | ||
sprite_animation.rs | ||
sprite_flipping.rs | ||
sprite_sheet.rs | ||
sprite_slice.rs | ||
sprite_tile.rs | ||
text2d.rs | ||
texture_atlas.rs | ||
transparency_2d.rs | ||
wireframe_2d.rs |