-
Notifications
You must be signed in to change notification settings - Fork 6
/
3d.rs
179 lines (140 loc) · 5.67 KB
/
3d.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// This example shows how to create 3D sprites.
#[path = "./common/mod.rs"]
pub mod common;
use bevy::{prelude::*, sprite::Anchor};
use bevy_spritesheet_animation::prelude::*;
use rand::{seq::SliceRandom, Rng};
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin::default(),
))
.add_systems(Startup, spawn_sprites)
.add_systems(Update, (update_on_keypress, orbit, draw_gizmos))
.run();
}
fn spawn_sprites(
mut commands: Commands,
mut library: ResMut<AnimationLibrary>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
assets: Res<AssetServer>,
) {
// 3D sprites require a 3D camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 1000.0, 4000.0).looking_at(Vec3::ZERO, Dir3::Y),
));
// Create an animation as usual
let spritesheet = Spritesheet::new(8, 8);
let clip = Clip::from_frames(spritesheet.row(3));
let clip_id = library.register_clip(clip);
let animation = Animation::from_clip(clip_id);
let animation_id = library.register_animation(animation);
// Create an image and a texture atlas like you would for any Bevy sprite
let image = assets.load("character.png");
let atlas = TextureAtlas {
layout: atlas_layouts.add(spritesheet.atlas_layout(96, 96)),
..default()
};
// Spawn 3D sprites
// A few 3D sprites orbiting around the center with various parameters
let sprites = [
Sprite3d::from_atlas_image(image.clone(), atlas.clone()),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_flip(true, false),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_flip(false, true),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_flip(true, true),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::BottomLeft),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::BottomCenter),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::BottomRight),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::CenterLeft),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::Center),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::CenterRight),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::TopLeft),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::TopCenter),
Sprite3d::from_atlas_image(image.clone(), atlas.clone()).with_anchor(Anchor::TopRight),
Sprite3d::from_atlas_image(image.clone(), atlas.clone())
.with_custom_size(Vec2::new(100.0, 400.0)),
];
let sprite_count = sprites.len();
for (i, sprite) in sprites.into_iter().enumerate() {
commands.spawn((
sprite,
SpritesheetAnimation::from_id(animation_id),
Orbit {
start_angle: i as f32 * std::f32::consts::TAU / sprite_count as f32,
},
));
}
// A non-animated 3D sprite in the center
commands.spawn(
Sprite3d::from_atlas_image(image.clone(), atlas.clone())
.with_color(Color::linear_rgb(1.0, 0.0, 0.0)),
);
// Help text
commands.spawn((Text(
"C: random colors\nX: flip on X\nY: flip on Y\nA: random anchors\nS: random sizes\nR: reset".to_owned()),
TextFont::from_font_size(30.0)
));
}
fn update_on_keypress(keyboard: Res<ButtonInput<KeyCode>>, mut sprites: Query<&mut Sprite3d>) {
let mut rng = rand::thread_rng();
for mut sprite in &mut sprites {
// Random color
if keyboard.just_pressed(KeyCode::KeyC) {
sprite.color = Color::linear_rgb(rng.gen(), rng.gen(), rng.gen());
}
// Flip
if keyboard.just_pressed(KeyCode::KeyX) {
sprite.flip_x = !sprite.flip_x;
}
if keyboard.just_pressed(KeyCode::KeyY) {
sprite.flip_y = !sprite.flip_y;
}
// Random anchors
if keyboard.just_pressed(KeyCode::KeyA) {
static ANCHORS: [Anchor; 9] = [
Anchor::BottomLeft,
Anchor::BottomCenter,
Anchor::BottomRight,
Anchor::CenterLeft,
Anchor::Center,
Anchor::CenterRight,
Anchor::TopLeft,
Anchor::TopCenter,
Anchor::TopRight,
];
sprite.anchor = ANCHORS.choose(&mut rng).unwrap().clone();
}
// Random size
if keyboard.just_pressed(KeyCode::KeyS) {
sprite.custom_size = Some(Vec2::new(
rng.gen_range(100.0..1000.0),
rng.gen_range(100.0..1000.0),
));
}
// Reset
if keyboard.just_pressed(KeyCode::KeyR) {
sprite.color = Color::WHITE;
sprite.flip_x = false;
sprite.flip_y = false;
sprite.custom_size = None;
sprite.anchor = Anchor::default();
}
}
}
#[derive(Component)]
struct Orbit {
start_angle: f32,
}
fn orbit(time: Res<Time>, mut query: Query<(&Orbit, &mut Transform)>) {
for (orbit, mut transform) in &mut query {
transform.translation.x = (orbit.start_angle + time.elapsed_secs()).cos() * 1500.0;
transform.translation.z = (orbit.start_angle + time.elapsed_secs()).sin() * 1500.0;
}
}
fn draw_gizmos(mut gizmos: Gizmos, sprites: Query<&Transform, With<Sprite3d>>) {
for &transform in &sprites {
gizmos.axes(transform, 100.0);
}
}