-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsky_texture.rs
More file actions
216 lines (195 loc) · 6.39 KB
/
sky_texture.rs
File metadata and controls
216 lines (195 loc) · 6.39 KB
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
use bevy::{
asset::RenderAssetUsages,
camera::visibility::RenderLayers,
image::ImageSampler,
prelude::*,
render::render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
window::WindowResized,
};
use crate::plugin::SkyboxMagnetTag;
#[derive(Resource)]
pub struct FullSkyTextureHandle {
pub render_target: Handle<Image>,
}
/// tag to find camera that render the sky
#[derive(Component)]
pub struct FullSkyCameraTag;
// Use a high-numbered, distinct render layer.
pub const FULL_SKY_RENDER_LAYER: RenderLayers = RenderLayers::layer(8);
#[derive(Resource, Clone)]
pub struct SkyTexturePluginSettings {
pub sky_render_layer: RenderLayers,
// order for camera that renders the entire sky, into a render texture
pub full_sky_camera_order: isize,
// order for the final camera that render the resulting sky texture
// onto the screen
pub final_camera_order: isize,
}
impl Default for SkyTexturePluginSettings {
fn default() -> Self {
Self {
sky_render_layer: FULL_SKY_RENDER_LAYER,
full_sky_camera_order: -2,
final_camera_order: -1,
}
}
}
#[derive(Clone)]
pub struct SkyTexturePlugin {
pub settings: SkyTexturePluginSettings,
}
impl Default for SkyTexturePlugin {
fn default() -> Self {
Self {
settings: SkyTexturePluginSettings::default(),
}
}
}
/// holds the quad where we render the sky
#[derive(Component)]
pub struct FullSkySpriteTag;
impl Plugin for SkyTexturePlugin {
fn build(&self, app: &mut App) {
app.insert_resource(self.settings.clone());
app.insert_resource(FullSkyTextureHandle {
render_target: Handle::default(),
});
app.add_systems(PreStartup, spawn_full_sky_texture);
app.add_systems(Startup, spawn_full_sky_camera.after(spawn_full_sky_texture));
app.add_systems(
Startup,
spawn_full_sky_screen_quad.after(spawn_full_sky_texture),
);
app.add_systems(
PostUpdate,
(
full_sky_camera_follow_primary.before(TransformSystems::Propagate),
resize_full_sky_on_window_change,
),
);
}
}
// System to spawn the full sky target texture at the start
pub fn spawn_full_sky_texture(
mut images: ResMut<Assets<Image>>,
mut texture_handle: ResMut<FullSkyTextureHandle>,
// Use the window to get an initial size, full-resolution is best for the sky.
primary_windows: Query<&Window, With<bevy::window::PrimaryWindow>>,
) {
let (width, height) = primary_windows
.single()
.map_or((1.0, 1.0), |v| (v.width(), v.height()));
let size = Extent3d {
width: width.max(1.0) as u32,
height: height.max(1.0) as u32,
..default()
};
let mut sky_image = Image::new_fill(
size,
TextureDimension::D2,
&[0, 0, 0, 0], // Start with a black/clear texture
TextureFormat::Rgba8UnormSrgb, // Use RGBA for the final sky color
RenderAssetUsages::default(),
);
sky_image.sampler = ImageSampler::linear();
sky_image.texture_descriptor.usage =
TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;
texture_handle.render_target = images.add(sky_image);
}
// System to resize the full sky texture when the window changes
fn resize_full_sky_on_window_change(
mut resize_events: MessageReader<WindowResized>,
mut images: ResMut<Assets<Image>>,
sky_handles: Res<FullSkyTextureHandle>,
primary_windows: Query<&Window, With<bevy::window::PrimaryWindow>>,
) {
let mut update_texture = false;
for event in resize_events.read() {
if primary_windows.get(event.window).is_ok() {
update_texture = true;
break;
}
}
if !update_texture {
return;
}
let Ok(window) = primary_windows.single() else {
return;
};
let width = window.width() as u32;
let height = window.height() as u32;
if width == 0 || height == 0 {
return;
}
if let Some(image) = images.get_mut(&sky_handles.render_target) {
image.resize(Extent3d {
width,
height,
depth_or_array_layers: 1,
});
}
}
fn spawn_full_sky_camera(
mut commands: Commands,
full_sky_handle: Res<FullSkyTextureHandle>,
settings: Res<SkyTexturePluginSettings>,
) {
// draw the full sky to a texture, instead of drawing directly to screen
commands.spawn((
Name::new("camera_full_sky"),
Camera3d::default(),
FullSkyCameraTag,
Camera {
order: settings.full_sky_camera_order,
target: full_sky_handle.render_target.clone().into(),
clear_color: ClearColorConfig::Custom(Color::NONE),
..default()
},
Transform::default(),
settings.sky_render_layer.clone(), // The camera also needs the render layer
));
}
fn full_sky_camera_follow_primary(
primary_cameras: Query<&Transform, (With<SkyboxMagnetTag>, Without<FullSkyCameraTag>)>,
mut sky_cameras: Query<&mut Transform, (With<FullSkyCameraTag>, Without<SkyboxMagnetTag>)>,
) {
if let Some(cam_tf) = primary_cameras.iter().next() {
for mut sky_tf in sky_cameras.iter_mut() {
*sky_tf = *cam_tf;
}
}
}
fn spawn_full_sky_screen_quad(
mut commands: Commands,
full_sky_handle: Res<FullSkyTextureHandle>,
settings: Res<SkyTexturePluginSettings>,
) {
commands.spawn((
Name::new("sky_screen_sprite"),
FullSkySpriteTag,
Sprite {
image: full_sky_handle.render_target.clone(),
custom_size: Some(vec2(1.0, 1.0)),
..default()
},
Transform::default(),
));
// this camera renders the sky that we put into the full_sky_handle.render_target
// should render before you draw your game scene
commands.spawn((
Name::new("Camera_fullsky_screenquad"),
Camera2d,
Camera {
order: settings.final_camera_order,
clear_color: ClearColorConfig::Custom(Color::BLACK),
..default()
},
Projection::Orthographic(OrthographicProjection {
scaling_mode: bevy::camera::ScalingMode::Fixed {
width: 1.0,
height: 1.0,
},
..OrthographicProjection::default_2d()
}),
));
}