1
1
use crate :: {
2
- camera:: {
3
- Camera , CameraPlugin , DepthCalculation , OrthographicProjection , PerspectiveProjection ,
4
- ScalingMode ,
5
- } ,
2
+ camera:: { Camera , DepthCalculation , OrthographicProjection , PerspectiveProjection } ,
6
3
primitives:: Frustum ,
7
4
view:: VisibleEntities ,
8
5
} ;
9
- use bevy_ecs:: bundle:: Bundle ;
6
+ use bevy_ecs:: { bundle:: Bundle , prelude :: Component } ;
10
7
use bevy_math:: Vec3 ;
11
8
use bevy_transform:: components:: { GlobalTransform , Transform } ;
12
9
13
- use super :: CameraProjection ;
10
+ use super :: { CameraProjection , ScalingMode } ;
11
+
12
+ #[ derive( Component , Default ) ]
13
+ pub struct Camera3d ;
14
+
15
+ #[ derive( Component , Default ) ]
16
+ pub struct Camera2d ;
14
17
15
18
/// Component bundle for camera entities with perspective projection
16
19
///
17
20
/// Use this for 3D rendering.
18
21
#[ derive( Bundle ) ]
19
- pub struct PerspectiveCameraBundle {
22
+ pub struct PerspectiveCameraBundle < M : Component > {
20
23
pub camera : Camera ,
21
24
pub perspective_projection : PerspectiveProjection ,
22
25
pub visible_entities : VisibleEntities ,
23
26
pub frustum : Frustum ,
24
27
pub transform : Transform ,
25
28
pub global_transform : GlobalTransform ,
29
+ pub marker : M ,
30
+ }
31
+
32
+ impl Default for PerspectiveCameraBundle < Camera3d > {
33
+ fn default ( ) -> Self {
34
+ PerspectiveCameraBundle :: new_3d ( )
35
+ }
26
36
}
27
37
28
- impl PerspectiveCameraBundle {
38
+ impl PerspectiveCameraBundle < Camera3d > {
29
39
pub fn new_3d ( ) -> Self {
30
- Default :: default ( )
40
+ PerspectiveCameraBundle :: new ( )
31
41
}
42
+ }
32
43
33
- pub fn with_name ( name : & str ) -> Self {
44
+ impl < M : Component + Default > PerspectiveCameraBundle < M > {
45
+ pub fn new ( ) -> Self {
34
46
let perspective_projection = PerspectiveProjection :: default ( ) ;
35
47
let view_projection = perspective_projection. get_projection_matrix ( ) ;
36
48
let frustum = Frustum :: from_view_projection (
@@ -41,7 +53,6 @@ impl PerspectiveCameraBundle {
41
53
) ;
42
54
PerspectiveCameraBundle {
43
55
camera : Camera {
44
- name : Some ( name. to_string ( ) ) ,
45
56
near : perspective_projection. near ,
46
57
far : perspective_projection. far ,
47
58
..Default :: default ( )
@@ -51,30 +62,56 @@ impl PerspectiveCameraBundle {
51
62
frustum,
52
63
transform : Default :: default ( ) ,
53
64
global_transform : Default :: default ( ) ,
65
+ marker : M :: default ( ) ,
54
66
}
55
67
}
56
68
}
57
69
58
- impl Default for PerspectiveCameraBundle {
59
- fn default ( ) -> Self {
60
- PerspectiveCameraBundle :: with_name ( CameraPlugin :: CAMERA_3D )
61
- }
62
- }
63
-
64
70
/// Component bundle for camera entities with orthographic projection
65
71
///
66
72
/// Use this for 2D games, isometric games, CAD-like 3D views.
67
73
#[ derive( Bundle ) ]
68
- pub struct OrthographicCameraBundle {
74
+ pub struct OrthographicCameraBundle < M : Component > {
69
75
pub camera : Camera ,
70
76
pub orthographic_projection : OrthographicProjection ,
71
77
pub visible_entities : VisibleEntities ,
72
78
pub frustum : Frustum ,
73
79
pub transform : Transform ,
74
80
pub global_transform : GlobalTransform ,
81
+ pub marker : M ,
82
+ }
83
+
84
+ impl OrthographicCameraBundle < Camera3d > {
85
+ pub fn new_3d ( ) -> Self {
86
+ let orthographic_projection = OrthographicProjection {
87
+ scaling_mode : ScalingMode :: FixedVertical ,
88
+ depth_calculation : DepthCalculation :: Distance ,
89
+ ..Default :: default ( )
90
+ } ;
91
+ let view_projection = orthographic_projection. get_projection_matrix ( ) ;
92
+ let frustum = Frustum :: from_view_projection (
93
+ & view_projection,
94
+ & Vec3 :: ZERO ,
95
+ & Vec3 :: Z ,
96
+ orthographic_projection. far ( ) ,
97
+ ) ;
98
+ OrthographicCameraBundle {
99
+ camera : Camera {
100
+ near : orthographic_projection. near ,
101
+ far : orthographic_projection. far ,
102
+ ..Default :: default ( )
103
+ } ,
104
+ orthographic_projection,
105
+ visible_entities : VisibleEntities :: default ( ) ,
106
+ frustum,
107
+ transform : Default :: default ( ) ,
108
+ global_transform : Default :: default ( ) ,
109
+ marker : Camera3d ,
110
+ }
111
+ }
75
112
}
76
113
77
- impl OrthographicCameraBundle {
114
+ impl OrthographicCameraBundle < Camera2d > {
78
115
/// Create an orthographic projection camera to render 2D content.
79
116
///
80
117
/// The projection creates a camera space where X points to the right of the screen,
@@ -112,7 +149,6 @@ impl OrthographicCameraBundle {
112
149
) ;
113
150
OrthographicCameraBundle {
114
151
camera : Camera {
115
- name : Some ( CameraPlugin :: CAMERA_2D . to_string ( ) ) ,
116
152
near : orthographic_projection. near ,
117
153
far : orthographic_projection. far ,
118
154
..Default :: default ( )
@@ -122,58 +158,7 @@ impl OrthographicCameraBundle {
122
158
frustum,
123
159
transform,
124
160
global_transform : Default :: default ( ) ,
125
- }
126
- }
127
-
128
- pub fn new_3d ( ) -> Self {
129
- let orthographic_projection = OrthographicProjection {
130
- scaling_mode : ScalingMode :: FixedVertical ,
131
- depth_calculation : DepthCalculation :: Distance ,
132
- ..Default :: default ( )
133
- } ;
134
- let view_projection = orthographic_projection. get_projection_matrix ( ) ;
135
- let frustum = Frustum :: from_view_projection (
136
- & view_projection,
137
- & Vec3 :: ZERO ,
138
- & Vec3 :: Z ,
139
- orthographic_projection. far ( ) ,
140
- ) ;
141
- OrthographicCameraBundle {
142
- camera : Camera {
143
- name : Some ( CameraPlugin :: CAMERA_3D . to_string ( ) ) ,
144
- near : orthographic_projection. near ,
145
- far : orthographic_projection. far ,
146
- ..Default :: default ( )
147
- } ,
148
- orthographic_projection,
149
- visible_entities : VisibleEntities :: default ( ) ,
150
- frustum,
151
- transform : Default :: default ( ) ,
152
- global_transform : Default :: default ( ) ,
153
- }
154
- }
155
-
156
- pub fn with_name ( name : & str ) -> Self {
157
- let orthographic_projection = OrthographicProjection :: default ( ) ;
158
- let view_projection = orthographic_projection. get_projection_matrix ( ) ;
159
- let frustum = Frustum :: from_view_projection (
160
- & view_projection,
161
- & Vec3 :: ZERO ,
162
- & Vec3 :: Z ,
163
- orthographic_projection. far ( ) ,
164
- ) ;
165
- OrthographicCameraBundle {
166
- camera : Camera {
167
- name : Some ( name. to_string ( ) ) ,
168
- near : orthographic_projection. near ,
169
- far : orthographic_projection. far ,
170
- ..Default :: default ( )
171
- } ,
172
- orthographic_projection,
173
- visible_entities : VisibleEntities :: default ( ) ,
174
- frustum,
175
- transform : Default :: default ( ) ,
176
- global_transform : Default :: default ( ) ,
161
+ marker : Camera2d ,
177
162
}
178
163
}
179
164
}
0 commit comments