Last active
October 19, 2022 04:05
-
-
Save rparrett/b63aa60802ad337840922f9f47f68ee7 to your computer and use it in GitHub Desktop.
2d shapes for bevy 0.8 blog
This file contains 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::{prelude::*, sprite::MaterialMesh2dBundle}; | |
fn main() { | |
App::new() | |
.insert_resource(ClearColor(Color::rgba(0.2, 0.2, 0.2, 1.0))) | |
.add_plugins(DefaultPlugins) | |
.add_startup_system(setup) | |
.run(); | |
} | |
fn setup( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<ColorMaterial>>, | |
) { | |
commands.spawn_bundle(Camera2dBundle::default()); | |
let radius = 120.; | |
let shapes = [ | |
( | |
meshes.add(shape::Circle::new(radius).into()).into(), | |
materials.add(ColorMaterial::from(Color::hex("FF1A1A").unwrap())), | |
), | |
( | |
meshes | |
.add(shape::RegularPolygon::new(radius, 5).into()) | |
.into(), | |
materials.add(ColorMaterial::from(Color::hex("FF821A").unwrap())), | |
), | |
( | |
meshes | |
.add(shape::RegularPolygon::new(radius, 6).into()) | |
.into(), | |
materials.add(ColorMaterial::from(Color::hex("12B2B2").unwrap())), | |
), | |
( | |
meshes | |
.add(shape::RegularPolygon::new(radius, 8).into()) | |
.into(), | |
materials.add(ColorMaterial::from(Color::hex("16D816").unwrap())), | |
), | |
]; | |
let w = 1280. + 200.; | |
let spacing: f32 = w / (shapes.len() + 1) as f32; | |
for (i, (shape, color)) in shapes.into_iter().enumerate() { | |
let x = (i + 1) as f32 * spacing - w / 2.; | |
commands.spawn_bundle(MaterialMesh2dBundle { | |
mesh: shape, | |
material: color, | |
transform: Transform::from_xyz(x, 0., 0.), | |
..default() | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment