| Name | Message | Date |
|---|---|---|
| 📄 main.rs | 6 days ago |
📄
src/main.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
use bevy::{ DefaultPlugins, app::{App, Startup, Update}, asset::AssetServer, camera::{Camera, Camera3d, OrthographicProjection, Projection, ScalingMode}, ecs::{ component::Component, query::With, system::{Commands, Res, Single}, }, gltf::GltfAssetLabel, light::DirectionalLight, math::{Dir3, Vec3, primitives::InfinitePlane3d}, scene::SceneRoot, transform::components::{GlobalTransform, Transform}, window::Window, }; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, control_dog) .run(); } fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { commands.spawn(( Camera3d::default(), Projection::from(OrthographicProjection { scaling_mode: ScalingMode::FixedVertical { viewport_height: 10.0, }, ..OrthographicProjection::default_3d() }), Transform::from_xyz(0.0, 10.0, 0.0).looking_to(Vec3::NEG_Y, Vec3::Z), )); commands.spawn(( SceneRoot(asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/animal-dog.glb"))), Transform::from_xyz(0.0, 0.0, 0.0), Dog::default(), )); commands.spawn(( DirectionalLight { illuminance: 15000.0, ..Default::default() }, Transform::from_xyz(0.0, 0.0, 0.0).looking_to(Vec3::NEG_Y, Vec3::Z), )); } #[derive(Component, Default)] struct Dog; fn control_dog( window: Single<&Window>, camera: Single<(&Camera, &GlobalTransform)>, mut dog: Single<&mut Transform, With<Dog>>, ) { let (camera, camera_transform) = *camera; let Some(cursor_position) = window .cursor_position() .and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok()) else { return; }; let Some(cursor_position) = cursor_position.plane_intersection_point(Vec3::ZERO, InfinitePlane3d { normal: Dir3::Y }) else { return; }; dog.look_at(-cursor_position, Vec3::Y); }