-
For context, I'm trying to build a tech demo for a 2D pixel-art game. My idea was to generate a sprite for each chunk and paint its pixels accordingly. const CHUNK_SIZE = 32;
// Multiply by 4 for RGBA channels?
let mut pixel_data = Vec::with_capacity(CHUNK_SIZE * CHUNK_SIZE);
for y in 0..CHUNK_SIZE {
for x in 0..CHUNK_SIZE {
// Fancy algorithm
// Push pixel to vec
}
}
// Add pixel data to sprite 👈
// Spawn sprite Any ideas? |
Beta Was this translation helpful? Give feedback.
Answered by
Aiden2207
Mar 13, 2023
Replies: 1 comment 1 reply
-
You can create a texture from a dynamic pub fn setup_graphics(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
const CHUNK_SIZE = 32;
let mut pixels = Vec::with_capacity(CHUNK_SIZE * CHUNK_SIZE * 4);
for y in 0..CHUNK_SIZE {
for x in 0..CHUNK_SIZE {
// Fancy algorithm
// Push pixel to vec
// Bear in mind, pixels are 4 bytes long, at least the way things are set up below
}
}
let size = Extent3d {
width: 32,
height: 32,
..default()
};
let mut image = Image {
data: pixels,
texture_descriptor: TextureDescriptor {
size,
label: None,
dimension: TextureDimension::D2,
format: TextureFormat::Bgra8UnormSrgb, // obviously, if you use a different color scale put the appropriate one here
mip_level_count: 1,
sample_count: 1,
usage: TextureUsages::RENDER_ATTACHMENT //I think these flags are right, but no promises
| TextureUsages::COPY_SRC
| TextureUsages::TEXTURE_BINDING,
view_formats: &[TextureFormat::Bgra8UnormSrgb], // best to keep this one the same as `format` unless you know what you are doing
},
..default()
};
let image_handle = images.add(image); //now the image is an asset and can be used in textures
// Spawns the sprite at the origin, obviously, you probably want to add some other components, move it, or maybe even just stash the image handle for later
commands
.spawn(SpriteBundle {
texture: image_handle.clone(),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
});
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
pmorim
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can create a texture from a dynamic
Image
. You'd probably want something like this: