| 1 |
|
| 2 |
|
| 3 |
use bevy::{ |
| 4 |
input::mouse::{AccumulatedMouseMotion, MouseButtonInput}, |
| 5 |
math::prelude::*, |
| 6 |
mesh::SphereKind, |
| 7 |
prelude::*, |
| 8 |
}; |
| 9 |
use chacha20::ChaCha8Rng; |
| 10 |
use rand::{distr::Distribution, SeedableRng}; |
| 11 |
|
| 12 |
fn main() { |
| 13 |
App::new() |
| 14 |
.add_plugins(DefaultPlugins) |
| 15 |
.add_systems(Startup, setup) |
| 16 |
.add_systems(Update, (handle_mouse, handle_keypress)) |
| 17 |
.run(); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
#[derive(Resource)] |
| 22 |
enum Mode { |
| 23 |
Interior, |
| 24 |
Boundary, |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[derive(Resource)] |
| 29 |
struct SampledShape(Cuboid); |
| 30 |
|
| 31 |
|
| 32 |
#[derive(Resource)] |
| 33 |
struct RandomSource(ChaCha8Rng); |
| 34 |
|
| 35 |
|
| 36 |
#[derive(Resource)] |
| 37 |
struct PointMesh(Handle<Mesh>); |
| 38 |
|
| 39 |
|
| 40 |
#[derive(Resource)] |
| 41 |
struct PointMaterial(Handle<StandardMaterial>); |
| 42 |
|
| 43 |
|
| 44 |
#[derive(Component)] |
| 45 |
struct SamplePoint; |
| 46 |
|
| 47 |
|
| 48 |
#[derive(Resource)] |
| 49 |
struct MousePressed(bool); |
| 50 |
|
| 51 |
fn setup( |
| 52 |
mut commands: Commands, |
| 53 |
mut meshes: ResMut<Assets<Mesh>>, |
| 54 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 55 |
) { |
| 56 |
|
| 57 |
let seeded_rng = ChaCha8Rng::seed_from_u64(19878367467712); |
| 58 |
commands.insert_resource(RandomSource(seeded_rng)); |
| 59 |
|
| 60 |
|
| 61 |
commands.spawn(( |
| 62 |
Mesh3d(meshes.add(Plane3d::default().mesh().size(12.0, 12.0))), |
| 63 |
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))), |
| 64 |
Transform::from_xyz(0.0, -2.5, 0.0), |
| 65 |
)); |
| 66 |
|
| 67 |
|
| 68 |
let shape = Cuboid::from_length(2.9); |
| 69 |
commands.insert_resource(SampledShape(shape)); |
| 70 |
|
| 71 |
|
| 72 |
commands.spawn(( |
| 73 |
Mesh3d(meshes.add(shape)), |
| 74 |
MeshMaterial3d(materials.add(StandardMaterial { |
| 75 |
base_color: Color::srgba(0.2, 0.1, 0.6, 0.3), |
| 76 |
alpha_mode: AlphaMode::Blend, |
| 77 |
cull_mode: None, |
| 78 |
..default() |
| 79 |
})), |
| 80 |
)); |
| 81 |
|
| 82 |
|
| 83 |
commands.spawn(( |
| 84 |
PointLight { |
| 85 |
shadow_maps_enabled: true, |
| 86 |
..default() |
| 87 |
}, |
| 88 |
Transform::from_xyz(4.0, 8.0, 4.0), |
| 89 |
)); |
| 90 |
|
| 91 |
|
| 92 |
commands.spawn(( |
| 93 |
Camera3d::default(), |
| 94 |
Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 95 |
)); |
| 96 |
|
| 97 |
|
| 98 |
commands.insert_resource(PointMesh( |
| 99 |
meshes.add( |
| 100 |
Sphere::new(0.03) |
| 101 |
.mesh() |
| 102 |
.kind(SphereKind::Ico { subdivisions: 3 }), |
| 103 |
), |
| 104 |
)); |
| 105 |
commands.insert_resource(PointMaterial(materials.add(StandardMaterial { |
| 106 |
base_color: Color::srgb(1.0, 0.8, 0.8), |
| 107 |
metallic: 0.8, |
| 108 |
..default() |
| 109 |
}))); |
| 110 |
|
| 111 |
|
| 112 |
commands.spawn(( |
| 113 |
Text::new( |
| 114 |
"Controls:\n\ |
| 115 |
M: Toggle between sampling boundary and interior.\n\ |
| 116 |
R: Restart (erase all samples).\n\ |
| 117 |
S: Add one random sample.\n\ |
| 118 |
D: Add 100 random samples.\n\ |
| 119 |
Rotate camera by holding left mouse and panning left/right.", |
| 120 |
), |
| 121 |
Node { |
| 122 |
position_type: PositionType::Absolute, |
| 123 |
top: px(12), |
| 124 |
left: px(12), |
| 125 |
..default() |
| 126 |
}, |
| 127 |
)); |
| 128 |
|
| 129 |
|
| 130 |
commands.insert_resource(Mode::Interior); |
| 131 |
|
| 132 |
|
| 133 |
commands.insert_resource(MousePressed(false)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
fn handle_keypress( |
| 138 |
mut commands: Commands, |
| 139 |
keyboard: Res<ButtonInput<KeyCode>>, |
| 140 |
mut mode: ResMut<Mode>, |
| 141 |
shape: Res<SampledShape>, |
| 142 |
mut random_source: ResMut<RandomSource>, |
| 143 |
sample_mesh: Res<PointMesh>, |
| 144 |
sample_material: Res<PointMaterial>, |
| 145 |
samples: Query<Entity, With<SamplePoint>>, |
| 146 |
) { |
| 147 |
|
| 148 |
if keyboard.just_pressed(KeyCode::KeyR) { |
| 149 |
for entity in &samples { |
| 150 |
commands.entity(entity).despawn(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
if keyboard.just_pressed(KeyCode::KeyS) { |
| 156 |
let rng = &mut random_source.0; |
| 157 |
|
| 158 |
|
| 159 |
let sample: Vec3 = match *mode { |
| 160 |
Mode::Interior => shape.0.sample_interior(rng), |
| 161 |
Mode::Boundary => shape.0.sample_boundary(rng), |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
commands.spawn(( |
| 166 |
Mesh3d(sample_mesh.0.clone()), |
| 167 |
MeshMaterial3d(sample_material.0.clone()), |
| 168 |
Transform::from_translation(sample), |
| 169 |
SamplePoint, |
| 170 |
)); |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
if keyboard.just_pressed(KeyCode::KeyD) { |
| 183 |
let mut rng = &mut random_source.0; |
| 184 |
|
| 185 |
|
| 186 |
let samples: Vec<Vec3> = match *mode { |
| 187 |
Mode::Interior => { |
| 188 |
let dist = shape.0.interior_dist(); |
| 189 |
dist.sample_iter(&mut rng).take(100).collect() |
| 190 |
} |
| 191 |
Mode::Boundary => { |
| 192 |
let dist = shape.0.boundary_dist(); |
| 193 |
dist.sample_iter(&mut rng).take(100).collect() |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
for sample in samples { |
| 199 |
commands.spawn(( |
| 200 |
Mesh3d(sample_mesh.0.clone()), |
| 201 |
MeshMaterial3d(sample_material.0.clone()), |
| 202 |
Transform::from_translation(sample), |
| 203 |
SamplePoint, |
| 204 |
)); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if keyboard.just_pressed(KeyCode::KeyM) { |
| 213 |
match *mode { |
| 214 |
Mode::Interior => *mode = Mode::Boundary, |
| 215 |
Mode::Boundary => *mode = Mode::Interior, |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
fn handle_mouse( |
| 222 |
accumulated_mouse_motion: Res<AccumulatedMouseMotion>, |
| 223 |
mut mouse_button_inputs: MessageReader<MouseButtonInput>, |
| 224 |
mut camera_transform: Single<&mut Transform, With<Camera>>, |
| 225 |
mut mouse_pressed: ResMut<MousePressed>, |
| 226 |
) { |
| 227 |
|
| 228 |
for mouse_button_input in mouse_button_inputs.read() { |
| 229 |
if mouse_button_input.button != MouseButton::Left { |
| 230 |
continue; |
| 231 |
} |
| 232 |
*mouse_pressed = MousePressed(mouse_button_input.state.is_pressed()); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
if !mouse_pressed.0 { |
| 237 |
return; |
| 238 |
} |
| 239 |
if accumulated_mouse_motion.delta != Vec2::ZERO { |
| 240 |
let displacement = accumulated_mouse_motion.delta.x; |
| 241 |
camera_transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(-displacement / 150.)); |
| 242 |
} |
| 243 |
} |