-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple.rs
93 lines (89 loc) · 3.09 KB
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Simple Example
//!
//! Current defaults:
//!
//! `Key1`: For Edit Mode (Allows editing existing meshes created with this plugin)
//! `Key2`: For Create Mode (Allows creating new meshes created with this plugin)
//! `MouseButton::Left` Click on Canvas: [Create Mode] Used to create vertex.
//! `MouseButton::Right` Click on Canvas: [Create Mode] Used to close the polygon and extrude it into a Mesh.
//! `CtrlLeft` + `LMB` Click: [Edit Mode] Insert new vertex on edge
//! `AltLeft` + `LMB` Click: [Edit Mode] Delete existing vertex.
use bevy::prelude::*;
use bevy_mesh_drawing::prelude::{
Canvas, MeshDrawingCamera, MeshDrawingPlugin, MeshDrawingPluginInputBinds,
MeshDrawingPluginSettings, PolygonalMesh,
};
pub fn main() {
App::new() // App
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
title: "simple mesh drawing".to_string(),
..default()
}),
..default()
}))
.add_plugins(MeshDrawingPlugin)
.insert_resource(MeshDrawingPluginSettings {
extrude_size: 2.0, // config extrude height
// config input binds...
input_binds: MeshDrawingPluginInputBinds {
edit_mode_switch_key: KeyCode::Digit1, // config key to switch to edit mode
create_mode_switch_key: KeyCode::Digit2, // config key to switch to create mode
..default()
},
..default()
})
.insert_resource(ClearColor(Color::srgb(0.0, 0.0, 0.0)))
.add_systems(Startup, setup)
.add_systems(Update, handle_polygonal_mesh_add)
.run();
}
/// Setup scene.
///
/// set up a simple 3D scene.
pub fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// Ground canvas
commands.spawn((
Name::new("Ground Canvas"),
PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(20.0, 20.0)),
material: materials.add(Color::srgba(0.3, 0.5, 0.3, 1.0)),
..default()
},
Canvas, // Mark this entity to allow drawing on it.
));
// camera
commands.spawn((
Name::new("Camera"),
Camera3dBundle {
transform: Transform::from_translation(Vec3::splat(10.))
.looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
MeshDrawingCamera, // Mark camera for use with drawing.
));
// light
commands.spawn((
Name::new("Light"),
PointLightBundle {
point_light: PointLight {
intensity: 500000.0,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
},
));
}
/// Drawn meshes will be created with [`PolygonalMesh`] component.
pub fn handle_polygonal_mesh_add(query: Query<Entity, Added<PolygonalMesh>>) {
for entity in query.iter() {
// Use the created mesh here...
info!("Created polygonal mesh: {:?}", entity);
}
}