@@ -9,6 +9,8 @@ fn main() {
9
9
. add_plugin ( FrameTimeDiagnosticsPlugin :: default ( ) )
10
10
. add_plugin ( LogDiagnosticsPlugin :: default ( ) )
11
11
. add_startup_system ( setup)
12
+ . add_system ( move_camera)
13
+ . add_system ( print_mesh_count)
12
14
. run ( ) ;
13
15
}
14
16
@@ -17,8 +19,8 @@ fn setup(
17
19
mut meshes : ResMut < Assets < Mesh > > ,
18
20
mut materials : ResMut < Assets < StandardMaterial > > ,
19
21
) {
20
- const WIDTH : usize = 100 ;
21
- const HEIGHT : usize = 100 ;
22
+ const WIDTH : usize = 200 ;
23
+ const HEIGHT : usize = 200 ;
22
24
let mesh = meshes. add ( Mesh :: from ( shape:: Cube { size : 1.0 } ) ) ;
23
25
let material = materials. add ( StandardMaterial {
24
26
base_color : Color :: PINK ,
@@ -28,17 +30,88 @@ fn setup(
28
30
for y in 0 ..HEIGHT {
29
31
// cube
30
32
commands. spawn_bundle ( PbrBundle {
31
- mesh : mesh. clone ( ) ,
32
- material : material. clone ( ) ,
33
+ mesh : mesh. clone_weak ( ) ,
34
+ material : material. clone_weak ( ) ,
33
35
transform : Transform :: from_xyz ( ( x as f32 ) * 2.0 , ( y as f32 ) * 2.0 , 0.0 ) ,
34
36
..default ( )
35
37
} ) ;
38
+ commands. spawn_bundle ( PbrBundle {
39
+ mesh : mesh. clone_weak ( ) ,
40
+ material : material. clone_weak ( ) ,
41
+ transform : Transform :: from_xyz (
42
+ ( x as f32 ) * 2.0 ,
43
+ HEIGHT as f32 * 2.0 ,
44
+ ( y as f32 ) * 2.0 ,
45
+ ) ,
46
+ ..Default :: default ( )
47
+ } ) ;
48
+ commands. spawn_bundle ( PbrBundle {
49
+ mesh : mesh. clone_weak ( ) ,
50
+ material : material. clone_weak ( ) ,
51
+ transform : Transform :: from_xyz ( ( x as f32 ) * 2.0 , 0.0 , ( y as f32 ) * 2.0 ) ,
52
+ ..Default :: default ( )
53
+ } ) ;
54
+ commands. spawn_bundle ( PbrBundle {
55
+ mesh : mesh. clone_weak ( ) ,
56
+ material : material. clone_weak ( ) ,
57
+ transform : Transform :: from_xyz ( 0.0 , ( x as f32 ) * 2.0 , ( y as f32 ) * 2.0 ) ,
58
+ ..Default :: default ( )
59
+ } ) ;
36
60
}
37
61
}
38
62
63
+ // add one cube, the only one with strong handles
64
+ // also serves as a reference point during rotation
65
+ commands. spawn_bundle ( PbrBundle {
66
+ mesh,
67
+ material,
68
+ transform : Transform {
69
+ translation : Vec3 :: new ( 0.0 , HEIGHT as f32 * 2.0 , 0.0 ) ,
70
+ scale : Vec3 :: splat ( 5.0 ) ,
71
+ ..Default :: default ( )
72
+ } ,
73
+ ..Default :: default ( )
74
+ } ) ;
75
+
39
76
// camera
40
77
commands. spawn_bundle ( PerspectiveCameraBundle {
41
- transform : Transform :: from_xyz ( 80.0 , 80.0 , 300.0 ) ,
78
+ transform : Transform :: from_xyz ( WIDTH as f32 , HEIGHT as f32 , WIDTH as f32 ) ,
42
79
..default ( )
43
80
} ) ;
81
+
82
+ commands. spawn_bundle ( DirectionalLightBundle {
83
+ ..Default :: default ( )
84
+ } ) ;
85
+ }
86
+
87
+ // System for rotating the camera
88
+ fn move_camera ( time : Res < Time > , mut camera_query : Query < & mut Transform , With < Camera > > ) {
89
+ let mut camera_transform = camera_query. single_mut ( ) ;
90
+ camera_transform. rotate ( Quat :: from_rotation_z ( time. delta_seconds ( ) * 0.5 ) ) ;
91
+ camera_transform. rotate ( Quat :: from_rotation_x ( time. delta_seconds ( ) * 0.5 ) ) ;
92
+ }
93
+
94
+ // System for printing the number of meshes on every tick of the timer
95
+ fn print_mesh_count (
96
+ time : Res < Time > ,
97
+ mut timer : Local < PrintingTimer > ,
98
+ sprites : Query < ( & Handle < Mesh > , & ComputedVisibility ) > ,
99
+ ) {
100
+ timer. 0 . tick ( time. delta ( ) ) ;
101
+
102
+ if timer. 0 . just_finished ( ) {
103
+ info ! (
104
+ "Meshes: {} - Visible Meshes {}" ,
105
+ sprites. iter( ) . len( ) ,
106
+ sprites. iter( ) . filter( |( _, cv) | cv. is_visible) . count( ) ,
107
+ ) ;
108
+ }
109
+ }
110
+
111
+ struct PrintingTimer ( Timer ) ;
112
+
113
+ impl Default for PrintingTimer {
114
+ fn default ( ) -> Self {
115
+ Self ( Timer :: from_seconds ( 1.0 , true ) )
116
+ }
44
117
}
0 commit comments