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
use super::{CameraProjection, ScalingMode};
use crate::{
camera::{Camera, DepthCalculation, OrthographicProjection, PerspectiveProjection},
primitives::Frustum,
view::VisibleEntities,
};
use bevy_ecs::reflect::ReflectComponent;
use bevy_ecs::{bundle::Bundle, prelude::Component};
use bevy_math::Vec3;
use bevy_reflect::Reflect;
use bevy_transform::components::{GlobalTransform, Transform};
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct Camera3d;
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct Camera2d;
#[derive(Bundle)]
pub struct PerspectiveCameraBundle<M: Component> {
pub camera: Camera,
pub perspective_projection: PerspectiveProjection,
pub visible_entities: VisibleEntities,
pub frustum: Frustum,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub marker: M,
}
impl Default for PerspectiveCameraBundle<Camera3d> {
fn default() -> Self {
PerspectiveCameraBundle::new_3d()
}
}
impl PerspectiveCameraBundle<Camera3d> {
pub fn new_3d() -> Self {
PerspectiveCameraBundle::new()
}
}
impl<M: Component + Default> PerspectiveCameraBundle<M> {
pub fn new() -> Self {
let perspective_projection = PerspectiveProjection::default();
let view_projection = perspective_projection.get_projection_matrix();
let frustum = Frustum::from_view_projection(
&view_projection,
&Vec3::ZERO,
&Vec3::Z,
perspective_projection.far(),
);
PerspectiveCameraBundle {
camera: Camera {
near: perspective_projection.near,
far: perspective_projection.far,
..Default::default()
},
perspective_projection,
visible_entities: VisibleEntities::default(),
frustum,
transform: Default::default(),
global_transform: Default::default(),
marker: M::default(),
}
}
}
#[derive(Bundle)]
pub struct OrthographicCameraBundle<M: Component> {
pub camera: Camera,
pub orthographic_projection: OrthographicProjection,
pub visible_entities: VisibleEntities,
pub frustum: Frustum,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub marker: M,
}
impl OrthographicCameraBundle<Camera3d> {
pub fn new_3d() -> Self {
let orthographic_projection = OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical,
depth_calculation: DepthCalculation::Distance,
..Default::default()
};
let view_projection = orthographic_projection.get_projection_matrix();
let frustum = Frustum::from_view_projection(
&view_projection,
&Vec3::ZERO,
&Vec3::Z,
orthographic_projection.far(),
);
OrthographicCameraBundle {
camera: Camera {
near: orthographic_projection.near,
far: orthographic_projection.far,
..Default::default()
},
orthographic_projection,
visible_entities: VisibleEntities::default(),
frustum,
transform: Default::default(),
global_transform: Default::default(),
marker: Camera3d,
}
}
}
impl OrthographicCameraBundle<Camera2d> {
pub fn new_2d() -> Self {
let far = 1000.0;
let orthographic_projection = OrthographicProjection {
far,
depth_calculation: DepthCalculation::ZDifference,
..Default::default()
};
let transform = Transform::from_xyz(0.0, 0.0, far - 0.1);
let view_projection =
orthographic_projection.get_projection_matrix() * transform.compute_matrix().inverse();
let frustum = Frustum::from_view_projection(
&view_projection,
&transform.translation,
&transform.back(),
orthographic_projection.far(),
);
OrthographicCameraBundle {
camera: Camera {
near: orthographic_projection.near,
far: orthographic_projection.far,
..Default::default()
},
orthographic_projection,
visible_entities: VisibleEntities::default(),
frustum,
transform,
global_transform: Default::default(),
marker: Camera2d,
}
}
}