random_sampling.rs:
use bevy::{
input::mouse::{MouseButtonInput, MouseMotion},
math::prelude::*,
prelude::*,
render::mesh::SphereKind,
};
use rand::{distributions::Distribution, SeedableRng};
use rand_chacha::ChaCha8Rng;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (handle_mouse, handle_keypress))
.run();
}
#[derive(Resource)]
enum Mode {
Interior,
Boundary,
}
#[derive(Resource)]
struct SampledShape(Cuboid);
#[derive(Resource)]
struct RandomSource(ChaCha8Rng);
#[derive(Resource)]
struct PointMesh(Handle<Mesh>);
#[derive(Resource)]
struct PointMaterial(Handle<StandardMaterial>);
#[derive(Component)]
struct SamplePoint;
#[derive(Resource)]
struct MousePressed(bool);
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712);
commands.insert_resource(RandomSource(seeded_rng));
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(12.0, 12.0)),
material: materials.add(Color::srgb(0.3, 0.5, 0.3)),
transform: Transform::from_xyz(0.0, -2.5, 0.0),
..default()
});
let shape = Cuboid::from_length(2.9);
commands.insert_resource(SampledShape(shape));
commands.spawn(PbrBundle {
mesh: meshes.add(shape),
material: materials.add(StandardMaterial {
base_color: Color::srgba(0.2, 0.1, 0.6, 0.3),
alpha_mode: AlphaMode::Blend,
cull_mode: None,
..default()
}),
..default()
});
commands.spawn(PointLightBundle {
point_light: PointLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.insert_resource(PointMesh(
meshes.add(
Sphere::new(0.03)
.mesh()
.kind(SphereKind::Ico { subdivisions: 3 }),
),
));
commands.insert_resource(PointMaterial(materials.add(StandardMaterial {
base_color: Color::srgb(1.0, 0.8, 0.8),
metallic: 0.8,
..default()
})));
commands.spawn(
TextBundle::from_section(
"Controls:\n\
M: Toggle between sampling boundary and interior.\n\
R: Restart (erase all samples).\n\
S: Add one random sample.\n\
D: Add 100 random samples.\n\
Rotate camera by panning left/right.",
TextStyle::default(),
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
commands.insert_resource(Mode::Interior);
commands.insert_resource(MousePressed(false));
}
#[allow(clippy::too_many_arguments)]
fn handle_keypress(
mut commands: Commands,
keyboard: Res<ButtonInput<KeyCode>>,
mut mode: ResMut<Mode>,
shape: Res<SampledShape>,
mut random_source: ResMut<RandomSource>,
sample_mesh: Res<PointMesh>,
sample_material: Res<PointMaterial>,
samples: Query<Entity, With<SamplePoint>>,
) {
if keyboard.just_pressed(KeyCode::KeyR) {
for entity in &samples {
commands.entity(entity).despawn();
}
}
if keyboard.just_pressed(KeyCode::KeyS) {
let rng = &mut random_source.0;
let sample: Vec3 = match *mode {
Mode::Interior => shape.0.sample_interior(rng),
Mode::Boundary => shape.0.sample_boundary(rng),
};
commands.spawn((
PbrBundle {
mesh: sample_mesh.0.clone(),
material: sample_material.0.clone(),
transform: Transform::from_translation(sample),
..default()
},
SamplePoint,
));
}
if keyboard.just_pressed(KeyCode::KeyD) {
let mut rng = &mut random_source.0;
let samples: Vec<Vec3> = match *mode {
Mode::Interior => {
let dist = shape.0.interior_dist();
dist.sample_iter(&mut rng).take(100).collect()
}
Mode::Boundary => {
let dist = shape.0.boundary_dist();
dist.sample_iter(&mut rng).take(100).collect()
}
};
for sample in samples {
commands.spawn((
PbrBundle {
mesh: sample_mesh.0.clone(),
material: sample_material.0.clone(),
transform: Transform::from_translation(sample),
..default()
},
SamplePoint,
));
}
}
if keyboard.just_pressed(KeyCode::KeyM) {
match *mode {
Mode::Interior => *mode = Mode::Boundary,
Mode::Boundary => *mode = Mode::Interior,
}
}
}
fn handle_mouse(
mut button_events: EventReader<MouseButtonInput>,
mut motion_events: EventReader<MouseMotion>,
mut camera: Query<&mut Transform, With<Camera>>,
mut mouse_pressed: ResMut<MousePressed>,
) {
for button_event in button_events.read() {
if button_event.button != MouseButton::Left {
continue;
}
*mouse_pressed = MousePressed(button_event.state.is_pressed());
}
if !mouse_pressed.0 {
return;
}
let displacement: f32 = motion_events.read().map(|motion| motion.delta.x).sum();
let mut camera_transform = camera.single_mut();
camera_transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(-displacement / 150.));
}