Skip to content

Instantly share code, notes, and snippets.

@jakobhellermann
Created April 3, 2021 10:23
Show Gist options
  • Save jakobhellermann/753a55665a4a4873c3ad3054cde6592d to your computer and use it in GitHub Desktop.
Save jakobhellermann/753a55665a4a4873c3ad3054cde6592d to your computer and use it in GitHub Desktop.
use bevy::prelude::*;
fn main() {
App::build()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(spawn_child.system())
.run();
}
struct TheParent;
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands
.spawn()
.insert(GlobalTransform::default())
.insert(Transform::from_scale(Vec3::splat(2.0)))
.insert(TheParent);
// .with_children(|_| ());
// light
commands.spawn_bundle(LightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}
fn spawn_child(
mut finished: Local<bool>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
parent: Query<Entity, With<TheParent>>,
) {
if *finished {
return;
}
*finished = true;
let parent = parent.single().unwrap();
let id = commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
})
.insert(Parent(parent))
.id();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment