-
Notifications
You must be signed in to change notification settings - Fork 8
/
asteroids.rs
154 lines (140 loc) · 4.86 KB
/
asteroids.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
use bevy::{
math::{vec2, vec4},
prelude::*,
render::{camera::ScalingMode, render_resource::AsBindGroup},
sprite::{Material2d, Material2dPlugin},
};
// use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_pancam::{PanCam, PanCamPlugin};
use noisy_bevy::{simplex_noise_2d_seeded, NoisyShaderPlugin};
fn main() {
App::new()
.register_type::<AsteroidParams>()
.insert_resource(ClearColor(Color::BLACK))
.add_plugins((
DefaultPlugins,
NoisyShaderPlugin,
PanCamPlugin,
Material2dPlugin::<AsteroidBackgroundMaterial>::default(),
// WorldInspectorPlugin::new()
))
.add_systems(Startup, setup)
.add_systems(Update, expand_asteroids)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn((
Camera2d,
OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical {
viewport_height: 70.0,
},
..OrthographicProjection::default_2d()
},
PanCam::default(),
));
commands.spawn(AsteroidBundle::default());
}
#[derive(Asset, AsBindGroup, Reflect, Debug, Clone)]
struct AsteroidBackgroundMaterial {
#[uniform(0)]
params: Vec4,
}
impl Material2d for AsteroidBackgroundMaterial {
fn vertex_shader() -> bevy::render::render_resource::ShaderRef {
"examples/asteroid_background.wgsl".into()
}
fn fragment_shader() -> bevy::render::render_resource::ShaderRef {
"examples/asteroid_background.wgsl".into()
}
}
#[derive(Component, Reflect, Debug, Clone)]
#[reflect(Component)]
struct AsteroidParams {
frequency_scale: f32,
amplitude_scale: f32,
radius: f32,
seed: u32,
}
impl Default for AsteroidParams {
fn default() -> Self {
Self {
frequency_scale: 0.1,
amplitude_scale: 2.8,
radius: 14.0,
seed: 0,
}
}
}
#[derive(Bundle)]
struct AsteroidBundle {
name: Name,
transform: Transform,
global_transform: GlobalTransform,
visibility: Visibility,
view_visibility: ViewVisibility,
inherited_visibility: InheritedVisibility,
params: AsteroidParams,
}
impl Default for AsteroidBundle {
fn default() -> Self {
Self {
name: Name::new("Asteroid"),
transform: default(),
global_transform: default(),
visibility: default(),
params: default(),
view_visibility: default(),
inherited_visibility: default(),
}
}
}
// turns compact model representation into something we can see on screen
fn expand_asteroids(
changed_asteroids: Query<(Entity, &AsteroidParams), Changed<AsteroidParams>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut asteroid_materials: ResMut<Assets<AsteroidBackgroundMaterial>>,
) {
for (asteroid_entity, params) in changed_asteroids.iter() {
let max_half_size = params.radius as i32 + 1;
commands.entity(asteroid_entity).despawn_descendants();
commands.entity(asteroid_entity).with_children(|asteroid| {
for x in -max_half_size..=max_half_size {
for y in -max_half_size..=max_half_size {
let p = vec2(x as f32, y as f32);
let o = simplex_noise_2d_seeded(p * params.frequency_scale, params.seed as f32)
* params.amplitude_scale;
// let o = noisy_bevy::fbm_simplex_2d(p * params.frequency_scale, 3, 2., 0.5)
// * params.amplitude_scale;
if ((x * x + y * y) as f32) < (params.radius + o).powi(2) {
asteroid.spawn((
Sprite {
color: Color::WHITE.with_luminance(0.2),
custom_size: Some(Vec2::splat(1.)),
..default()
},
Transform::from_translation(Vec3::new(x as f32, y as f32, 100.)),
));
}
}
}
// we are making a new material each time we make an asteroid
// this doesn't really scale well, but works fine for an example
let material_handle = asteroid_materials.add(AsteroidBackgroundMaterial {
params: vec4(
params.frequency_scale,
params.amplitude_scale,
params.radius,
params.seed as f32,
),
});
let quad_handle = meshes.add(Mesh::from(Rectangle::from_size(Vec2::new(100.0, 100.0))));
asteroid.spawn((
Mesh2d(quad_handle),
MeshMaterial2d(material_handle),
Transform::from_translation(Vec3::new(0.0, 0.0, 1.5)),
));
});
}
}