-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec5d0ef
commit 47a93ca
Showing
5 changed files
with
209 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use bevy::{prelude::*, render::camera::Camera}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(follow) | ||
.run(); | ||
} | ||
|
||
#[derive(Component)] | ||
struct Follow; | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
let texture_handle = asset_server.load("branding/icon.png"); | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
commands | ||
.spawn_bundle(SpriteBundle { | ||
texture: texture_handle, | ||
..Default::default() | ||
}) | ||
.insert(Follow); | ||
} | ||
|
||
fn follow( | ||
mut q: Query<&mut Transform, With<Follow>>, | ||
q_camera: Query<(&Camera, &GlobalTransform)>, | ||
windows: Res<Windows>, | ||
images: Res<Assets<Image>>, | ||
mut evr_cursor: EventReader<CursorMoved>, | ||
) { | ||
let (camera, camera_transform) = q_camera.single(); | ||
if let Some(cursor) = evr_cursor.iter().next() { | ||
for mut transform in q.iter_mut() { | ||
let point: Option<Vec3> = | ||
camera.screen_to_point_2d(cursor.position, &windows, &images, camera_transform); | ||
println!("Point {:?}", point); | ||
if let Some(point) = point { | ||
transform.translation = point; | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use bevy::{prelude::*, render::camera::Camera, render::primitives::Plane}; | ||
|
||
fn main() { | ||
App::new() | ||
.insert_resource(Msaa { samples: 4 }) | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(follow) | ||
.run(); | ||
} | ||
|
||
#[derive(Component)] | ||
struct Follow; | ||
|
||
/// set up a simple 3D scene | ||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<StandardMaterial>>, | ||
) { | ||
// plane | ||
commands.spawn_bundle(PbrBundle { | ||
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })), | ||
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), | ||
..Default::default() | ||
}); | ||
// cube | ||
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(Follow); | ||
// light | ||
commands.spawn_bundle(PointLightBundle { | ||
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 follow( | ||
mut q: Query<&mut Transform, With<Follow>>, | ||
q_camera: Query<(&Camera, &GlobalTransform)>, | ||
windows: Res<Windows>, | ||
images: Res<Assets<Image>>, | ||
mut evr_cursor: EventReader<CursorMoved>, | ||
) { | ||
// Assumes there is at least one camera | ||
let (camera, camera_transform) = q_camera.iter().next().unwrap(); | ||
if let Some(cursor) = evr_cursor.iter().next() { | ||
for mut transform in q.iter_mut() { | ||
let point: Option<Vec3> = camera.screen_to_point_on_plane( | ||
cursor.position, | ||
Plane::new(Vec4::new(0., 1., 0., 1.)), | ||
&windows, | ||
&images, | ||
camera_transform, | ||
); | ||
if let Some(point) = point { | ||
transform.translation = point + Vec3::new(0., 0.5, 0.); | ||
} | ||
} | ||
} | ||
} |