-
-
Save DGriffin91/badc83c09e1579a9443153d4b4d89d87 to your computer and use it in GitHub Desktop.
Basic Letterboxing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// License: Apache-2.0 / MIT | |
use bevy::{ | |
core_pipeline::clear_color::ClearColorConfig, | |
prelude::*, | |
render::{camera::RenderTarget, render_resource::*, view::RenderLayers}, | |
}; | |
fn main() { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_startup_system(setup) | |
.run(); | |
} | |
/// set up a simple 3D scene | |
fn setup( | |
mut commands: Commands, | |
asset_server: Res<AssetServer>, | |
mut texture_atlases: ResMut<Assets<TextureAtlas>>, | |
mut images: ResMut<Assets<Image>>, | |
) { | |
// The size of the rendered area | |
let size = Extent3d { | |
width: 400, | |
height: 300, | |
..default() | |
}; | |
// This is the texture that will be rendered to | |
let mut image = Image { | |
texture_descriptor: TextureDescriptor { | |
label: None, | |
size, | |
dimension: TextureDimension::D2, | |
format: TextureFormat::Bgra8UnormSrgb, | |
mip_level_count: 1, | |
sample_count: 1, | |
usage: TextureUsages::TEXTURE_BINDING | |
| TextureUsages::COPY_DST | |
| TextureUsages::RENDER_ATTACHMENT, | |
}, | |
..default() | |
}; | |
// fill image.data with zeroes | |
image.resize(size); | |
let image_handle = images.add(image); | |
// Main Camera | |
commands | |
.spawn_bundle(Camera2dBundle { | |
camera: Camera { | |
priority: -1, | |
target: RenderTarget::Image(image_handle.clone()), | |
..default() | |
}, | |
..default() | |
}) | |
// UI is assumed to be done on the "full sized primary window", so Bevy can't yet meaningfully render it to | |
// a smaller image. This will be improved soon. | |
.insert(UiCameraConfig { show_ui: false }); | |
commands | |
.spawn_bundle(Camera2dBundle { | |
camera_2d: Camera2d { | |
clear_color: ClearColorConfig::Custom(Color::rgb(0.1, 0.1, 0.1)), | |
..default() | |
}, | |
..default() | |
}) | |
.insert(RenderLayers::none()); | |
commands.spawn_bundle(ImageBundle { | |
style: Style { | |
size: Size::new(Val::Auto, Val::Auto), | |
margin: UiRect::all(Val::Auto), | |
..default() | |
}, | |
image: image_handle.into(), | |
..default() | |
}); | |
let texture_handle = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png"); | |
let texture_atlas = TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1); | |
let texture_atlas_handle = texture_atlases.add(texture_atlas); | |
// Spawn sprite | |
commands.spawn_bundle(SpriteSheetBundle { | |
texture_atlas: texture_atlas_handle, | |
..default() | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment