-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
284 lines (232 loc) · 8.57 KB
/
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use bevy::prelude::*;
use bevy::render::mesh::Indices;
use bevy::render::mesh::PrimitiveTopology;
use bevy::diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin};
// use bevy::app::ScheduleRunnerSettings;
// use bevy::utils::Duration;
// use bevy::core::FixedTimestep;
fn main() {
App::new()
// .insert_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64( 1.0 / 20.0, ))) // no different
// .with_run_criteria(FixedTimestep::step( (1.0/60.0) as f64))
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
// Show Framerate in Console
// .add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_startup_system(setup)
.add_system(movement)
.add_system(ui_system)
.run();
}
#[derive(Component)]
struct Movable;
#[derive(Component)]
struct StatsText;
fn create_ui(asset_server: &Res<AssetServer>) -> TextBundle {
//
TextBundle {
text: Text {
sections: vec![
TextSection {
value: "".to_string(),
style: TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 40.0,
color: Color::rgb(0.0, 1.0, 1.0),
},
},
],
..default()
},
style: Style {
position_type: PositionType::Absolute,
position: Rect {
top: Val::Px(5.0),
left: Val::Px(5.0),
..default()
},
..default()
},
..default()
}
}
fn _create_tree() -> Mesh {
// 2__3
// | /|
// |/ |
// 0--1
let w = 2.0;
let h = 6.0;
let n = 0.0;
let positions = vec![
/* x y z */
/*0:*/ [-w, n, n ],
/*1:*/ [ w, n, n ],
/*2:*/ [-w, h, n ],
/*3:*/ [ w, h, n ],
/*4:*/ [ n, n, w ],
/*5:*/ [ n, n, -w ],
/*6:*/ [ n, h, w ],
/*7:*/ [ n, h, -w ],
];
let uvs = vec![
/* u v is related to x y in this case */
/*0:*/ [ 1., 1. ],
/*1:*/ [ 0., 1. ],
/*2:*/ [ 1., 0. ],
/*3:*/ [ 0., 0. ],
/*4:*/ [ 1., 1. ],
/*5:*/ [ 0., 1. ],
/*6:*/ [ 1., 0. ],
/*7:*/ [ 0., 0. ],
];
let indices = vec![
0, 1, 3,
0, 3, 2,
4, 5, 7,
4, 7, 6,
];
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
mesh.set_indices(Some(Indices::U32(indices)));
// pub fn compute_flat_normals(&mut self) Panics if Indices are set ==>> NOT set !!! todo: issue?
/**** THIS (MORE VERTICES)
// compute only works with duplicate!
mesh.duplicate_vertices(); // ERROR bevy_pbr::material: Mesh is missing requested attribute: Vertex_Normal (MeshVertexAttributeId(1), pipeline type: Some("bevy_pbr::material::MaterialPipeline<bevy_pbr::pbr_material::StandardMaterial>"))
mesh.compute_flat_normals(); // thread 'TaskPool (0)' panicked at 'assertion failed: `(left == right)` // left: `8`, // right: `6`: MeshVertexAttributeId(1) has a different vertex count (6) than other attributes (8) in this mesh.', /Users/karlos/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_render-0.7.0/src/mesh/mesh/mod.rs:208:17
OR THAT: ****/
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
]);
mesh
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// UI with FPS
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(create_ui(&asset_server)).insert(StatsText);
// 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()
});
// tree(s)
let texture_handle = asset_server.load("arbaro_tree_broad_leaved.png");
for n in 0..1000 { // ein mal Quad 10000 war ok. 10000 macht 13 FPS statt <=30. Äh!! Nur die sichtbaren zählen?!
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(_create_tree()),
material: materials.add(
StandardMaterial {
base_color_texture: Some(texture_handle.clone() ),
alpha_mode: bevy::pbr::AlphaMode::Mask(0.5), // Opaque, Mask(0.5), Blend,
double_sided: true, // needed to have both sides equal lighted
cull_mode: None, // No cull of the back side. Default is: Some(bevy::render::render_resource::Face::Back),
..default()
}
),
transform: Transform::from_xyz(0.0, 0.0, -n as f32),
..default()
})
.insert(Movable)
;
/******
let size = 3.0;
// 1st side
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Quad { size: bevy::math::vec2(size, size), flip: false })),
material: materials.add(
StandardMaterial {
base_color_texture: Some(texture_handle.clone() ),
alpha_mode: bevy::pbr::AlphaMode::Mask(0.5), // Opaque, Mask(0.5), Blend,
double_sided: true, // needed to have both sides equal lighted
cull_mode: None, // No cull of the back side. Default is: Some(bevy::render::render_resource::Face::Back),
..default()
}
),
transform: Transform::from_xyz(0.0, size/2., -n as f32),
..default()
})
.insert(Movable)
;
// 2nd side crossed
let rotation = Quat::from_euler(EulerRot::ZYX, 0.0, std::f32::consts::FRAC_PI_2, 0.0);
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Quad { size: bevy::math::vec2(size, size), flip: false })),
material: materials.add(
StandardMaterial {
base_color_texture: Some(texture_handle.clone() ),
alpha_mode: bevy::pbr::AlphaMode::Mask(0.5), // Opaque, Mask(0.5), Blend,
double_sided: true, // needed to have both sides equal lighted
cull_mode: None, // No cull of the back side. Default is: Some(bevy::render::render_resource::Face::Back),
..default()
}
),
transform:
Transform::from_xyz(0.0, size/2., -n as f32) *
Transform::from_rotation(rotation),
..default()
})
.insert(Movable)
;
******/
}
//// light ////
// Shadows do not work correct on my Macbook Air native, but in the browser it is ok.
let mut _shadows = true;
#[cfg(not(target_arch = "wasm32"))]
{ _shadows = false; }
commands.spawn_bundle(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: _shadows,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
});
//// camera ////
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-8.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
}
fn movement(
_input: Res<Input<KeyCode>>,
time: Res<Time>,
mut query: Query<&mut Transform, With<Movable>>,
) {
for mut transform in query.iter_mut() {
//println!("xx {:?}", transform);
let delta_y = 1.00*time.delta_seconds();
let delta_rotation = Quat::from_euler(EulerRot::ZYX, 0.0, delta_y, 0.0);
transform.rotation *= delta_rotation; // multiply! means addition
let scale = transform.scale.x * (1.-0.02*time.delta_seconds()); // just for fun
transform.scale = Vec3::new(scale,scale,scale);
}
}
fn ui_system(
diagnostics: Res<Diagnostics>,
mut query: Query<&mut Text, With<StatsText>>,
) {
let mut text = query.single_mut();
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() {
text.sections[0].value = format!("FPS: {:.2}", average);
}
};
}