bevy_sky_gradient 0.1.0

Sky rendering plugin for bevy featuring, sky gradient, aurora / northern lights, stars, sun, day-night cycle
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use bevy::{
    asset::RenderAssetUsages,
    image::ImageSampler,
    prelude::*,
    render::{
        render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages},
        view::RenderLayers,
    },
};

use crate::{
    ambient_driver::AmbientDriverPlugin,
    aurora::AuroraPlugin,
    bind_groups::{GradientBindGroup, StarsBindGroup},
    cycle::SkyCyclePlugin,
    gradient_driver::GradientDriverPlugin,
    gradient_material::{FullGradientMaterial, GradientMaterialPlugin},
    noise::{NoiseHandles, NoisePlugin, NoiseSettings},
    presets::SkyPresetPlugin,
    sky_material::FullSkyMaterial,
    sky_texture::{SkyTexturePlugin, SkyTexturePluginSettings},
    sun::SunDriverPlugin,
    utils,
};

#[derive(Clone, Resource)]
pub struct SkySettings {
    pub camera_gradient_order: isize,
    pub skybox_gradient_render_layer: RenderLayers,
    pub spawn_default_skybox: bool,
    pub stars_bind_group: StarsBindGroup,
}

impl Default for SkySettings {
    fn default() -> Self {
        Self {
            camera_gradient_order: 3,
            spawn_default_skybox: true,
            skybox_gradient_render_layer: RenderLayers::layer(6),
            stars_bind_group: StarsBindGroup::default(),
        }
    }
}

///! controlls what features you want.  
///! you might not want to use the default Cycle/SunDriver/GradientDriver/Aurora for example
///! then you can skip that plugin and implement your own.
pub struct SkyPluginBuilder {
    pub settings: SkySettings,
    ///! if enabled, the full sky is rendered to a texture
    ///! usefull if you need to sample the sky for a fog effect for example
    pub render_sky_to_texture: bool,
    pub use_preset_plugin: bool,
    pub noise: NoisePlugin,
    pub aurora: Option<AuroraPlugin>,
    pub cycle: Option<SkyCyclePlugin>,
    pub sun_driver: Option<SunDriverPlugin>,
    pub gradient_driver: Option<GradientDriverPlugin>,
    pub ambient_driver: Option<AmbientDriverPlugin>,
}

impl Default for SkyPluginBuilder {
    fn default() -> Self {
        Self::all_features()
    }
}

impl SkyPluginBuilder {
    pub fn no_features() -> Self {
        Self {
            settings: SkySettings::default(),
            noise: NoisePlugin::default(),
            aurora: None,
            cycle: None,
            sun_driver: None,
            gradient_driver: None,
            use_preset_plugin: false,
            render_sky_to_texture: false,
            ambient_driver: None,
        }
    }

    pub fn all_features() -> Self {
        Self {
            settings: SkySettings::default(),
            noise: NoisePlugin::default(),
            aurora: Some(AuroraPlugin::default()),
            cycle: Some(SkyCyclePlugin::default()),
            sun_driver: Some(SunDriverPlugin::default()),
            gradient_driver: Some(GradientDriverPlugin::default()),
            use_preset_plugin: true,
            render_sky_to_texture: false,
            ambient_driver: Some(AmbientDriverPlugin::default()),
        }
    }

    pub fn set_sky_settings(mut self, sky_settings: SkySettings) -> Self {
        self.settings = sky_settings;
        self
    }

    pub fn set_spawn_default_skybox(mut self, spawn_default_skybox: bool) -> Self {
        self.settings.spawn_default_skybox = spawn_default_skybox;
        self
    }

    pub fn with_render_sky_to_texture(mut self) -> Self {
        self.render_sky_to_texture = true;
        self
    }

    pub fn build(self) -> SkyPlugin {
        SkyPlugin { sky_builder: self }
    }

    pub fn set_presets(mut self, use_presets_plugin: bool) -> Self {
        self.use_preset_plugin = use_presets_plugin;
        self
    }

    pub fn with_noise_settings(mut self, noise_settings: NoiseSettings) -> Self {
        self.noise.noise_settings = noise_settings;
        self
    }

    pub fn set_sun_driver(mut self, sun_driver: SunDriverPlugin) -> Self {
        self.sun_driver = Some(sun_driver);
        self
    }

    pub fn set_cycle(mut self, cycle: SkyCyclePlugin) -> Self {
        self.cycle = Some(cycle);
        self
    }

    pub fn set_aurora(mut self, aurora_plugin: AuroraPlugin) -> Self {
        self.aurora = Some(aurora_plugin);
        self
    }

    pub fn set_gradient_driver(mut self, gradient_driver: GradientDriverPlugin) -> Self {
        self.gradient_driver = Some(gradient_driver);
        self
    }

    pub fn set_ambient_driver(mut self, ambient_plugin: AmbientDriverPlugin) -> Self {
        self.ambient_driver = Some(ambient_plugin);
        self
    }
}

///! sets up all you need to show a gradient skybox
pub struct SkyPlugin {
    ///! if true, an entity skybox will spawn
    // pub spawn_default_skybox: bool,
    // pub spawn_default_skybox_gradient: bool,
    pub sky_builder: SkyPluginBuilder,
}

impl SkyPlugin {
    pub fn builder() -> SkyPluginBuilder {
        SkyPluginBuilder::no_features()
    }
    pub fn builder_all_features() -> SkyPluginBuilder {
        SkyPluginBuilder::all_features()
    }
}

impl Plugin for SkyPlugin {
    fn build(&self, app: &mut App) {
        app.insert_resource(self.sky_builder.settings.clone());
        app.add_plugins(self.sky_builder.noise.clone());
        app.add_plugins(SkyPresetPlugin);
        app.add_plugins(GradientMaterialPlugin);

        if self.sky_builder.render_sky_to_texture {
            app.add_plugins(SkyTexturePlugin::default());
        }

        if let Some(aurora_plugin) = &self.sky_builder.aurora {
            app.add_plugins(aurora_plugin.clone());
        }
        if let Some(cycle_plugin) = &self.sky_builder.cycle {
            app.add_plugins(cycle_plugin.clone());
        }
        if let Some(sun_driver) = &self.sky_builder.sun_driver {
            if self.sky_builder.cycle.is_none() {
                error!("sun driver requires cycle plugin. prepare for crash");
            }
            app.add_plugins(sun_driver.clone());
        }
        if let Some(gradient_driver) = &self.sky_builder.gradient_driver {
            if self.sky_builder.gradient_driver.is_none() {
                error!("gradient driver requires cycle plugin. prepare for crash");
            }
            app.add_plugins(gradient_driver.clone());
        }
        if let Some(ambient_driver_plugin) = &self.sky_builder.ambient_driver {
            app.add_plugins(ambient_driver_plugin.clone());
        }

        app.insert_resource(AuroraTextureHandle {
            render_target: Handle::default(),
        });
        app.insert_resource(GradientTextureHandle {
            render_target: Handle::default(),
        });
        app.add_systems(PreStartup, spawn_aurora_texture);
        app.add_systems(PreStartup, spawn_gradient_texture);

        // app.add_systems(Startup, crate::assets::initialize_shaders);
        app.add_plugins(crate::assets::SkyAssetsPlugin);
        app.add_plugins(MaterialPlugin::<FullSkyMaterial>::default());
        if self.sky_builder.settings.spawn_default_skybox {
            app.add_systems(Startup, spawn_default_skybox);
        }
        app.add_systems(Startup, spawn_default_skybox_gradient);

        app.add_systems(
            PostUpdate,
            (sky_follow_camera, gradient_follow_camera).before(TransformSystem::TransformPropagate),
        );
    }
}

impl Default for SkyPlugin {
    fn default() -> Self {
        Self {
            sky_builder: SkyPluginBuilder::default(),
        }
    }
}

///! attach to your main camera for the skybox to auto move to
#[derive(Component)]
pub struct SkyboxMagnetTag;

fn spawn_default_skybox(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut sky_materials: ResMut<Assets<FullSkyMaterial>>,
    noise_handles: Res<NoiseHandles>,
    aurora_handles: Res<AuroraTextureHandle>,
    gradient_texture_handle: Res<GradientTextureHandle>,
    sky_texture_plugin_settings: Option<Res<SkyTexturePluginSettings>>,
    sky_settings: Res<SkySettings>,
) {
    let mut skybox_commands = commands.spawn((
        Name::new("sky_gradient_skybox"),
        Mesh3d(meshes.add(utils::default_sky_mesh())),
        MeshMaterial3d(sky_materials.add(FullSkyMaterial {
            noise3_image: noise_handles.noise3.clone(),
            voronoi3_image: noise_handles.voronoi3.clone(),
            aurora_image: aurora_handles.render_target.clone(),
            gradient_image: gradient_texture_handle.render_target.clone(),
            stars: sky_settings.stars_bind_group.clone(),
            ..default()
        })),
    ));
    if let Some(settings) = sky_texture_plugin_settings {
        skybox_commands.insert(settings.sky_render_layer.clone());
    }
}
#[derive(Component)]
pub struct GradientCameraTag;

fn spawn_default_skybox_gradient(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut gradient_materials: ResMut<Assets<FullGradientMaterial>>,
    gradient_texture_handle: Res<GradientTextureHandle>,
    sky_settings: Res<SkySettings>,
) {
    commands.spawn((
        Name::new("sky_skybox_full_gradient"),
        Mesh3d(meshes.add(utils::default_sky_mesh())),
        MeshMaterial3d(gradient_materials.add(FullGradientMaterial {
            gradient_bind_group: GradientBindGroup::default(),
        })),
        sky_settings.skybox_gradient_render_layer.clone(),
    ));

    commands.spawn((
        Name::new("camera_gradient"),
        Camera3d::default(),
        GradientCameraTag,
        Camera {
            order: sky_settings.camera_gradient_order,
            target: gradient_texture_handle.render_target.clone().into(),
            clear_color: ClearColorConfig::Custom(Color::NONE),
            ..default()
        },
        Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)).looking_at(Vec3::ZERO, Vec3::Y),
        sky_settings.skybox_gradient_render_layer.clone(),
    ));
}

// aurora texture is defined by sky, and the aurora render into it. it needs to be defined by the sky plugin
#[derive(Resource)]
pub struct AuroraTextureHandle {
    pub render_target: Handle<Image>,
}

// spawn the aurora target texture, if not used, it's just a blank 2x2 texture
pub fn spawn_aurora_texture(
    mut images: ResMut<Assets<Image>>,
    mut aurora_texture_handle: ResMut<AuroraTextureHandle>,
) {
    let size = Extent3d {
        width: 2,
        height: 2,
        ..default()
    };

    let mut aurora_image = Image::new_fill(
        size,
        TextureDimension::D2,
        &[0, 0, 0, 0],
        TextureFormat::Bgra8UnormSrgb,
        RenderAssetUsages::default(),
    );
    aurora_image.sampler = ImageSampler::linear();
    aurora_image.texture_descriptor.usage =
        TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;

    let aurora_image_handle = images.add(aurora_image);
    aurora_texture_handle.render_target = aurora_image_handle;
}

#[derive(Resource)]
pub struct GradientTextureHandle {
    pub render_target: Handle<Image>,
}

pub fn spawn_gradient_texture(
    mut images: ResMut<Assets<Image>>,
    mut aurora_texture_handle: ResMut<GradientTextureHandle>,
) {
    let size = Extent3d {
        width: 2,
        height: 2,
        ..default()
    };

    let mut aurora_image = Image::new_fill(
        size,
        TextureDimension::D2,
        &[0, 0, 0, 0],
        TextureFormat::Bgra8UnormSrgb,
        RenderAssetUsages::default(),
    );
    aurora_image.sampler = ImageSampler::linear();
    aurora_image.texture_descriptor.usage =
        TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST | TextureUsages::RENDER_ATTACHMENT;

    let aurora_image_handle = images.add(aurora_image);
    aurora_texture_handle.render_target = aurora_image_handle;
}

fn sky_follow_camera(
    camera_query: Query<&Transform, (With<SkyboxMagnetTag>, With<Camera>)>,
    mut sky_query: Query<&mut Transform, (Without<Camera>, With<MeshMaterial3d<FullSkyMaterial>>)>,
    mut warned_once: Local<bool>,
) {
    let mut count = 0;
    for main_cam_transform in camera_query.iter() {
        count += 1;
        for mut sky_transform in &mut sky_query {
            sky_transform.translation = main_cam_transform.translation;
        }
    }
    if count == 0 {
        if !*warned_once {
            warn!("SkyPlugin: no camera with SkyBoxMagnetTag to transform to");
            *warned_once = true;
        }
    } else if count > 1 {
        if !*warned_once {
            warn!("SkyPlugin: MORE THAN 1 CAMERA WITH SkyBoxMagnetTag");
            *warned_once = true;
        }
    }
}

fn gradient_follow_camera(
    primary_cameras: Query<
        (&Transform, &Camera, &Projection),
        (Without<GradientCameraTag>, With<SkyboxMagnetTag>),
    >,
    mut gradient_camera: Query<(&mut Transform, &Camera, &mut Projection), With<GradientCameraTag>>,
    mut gradient_mesh: Query<
        &mut Transform,
        (Without<Camera>, With<MeshMaterial3d<FullGradientMaterial>>),
    >,
) {
    if let Some((cam_tf, _camera, cam_proj)) = primary_cameras.iter().next() {
        for (mut aurora_tf, _cam, mut aurora_projection) in gradient_camera.iter_mut() {
            // ensure same projection
            *aurora_projection = cam_proj.clone();
            *aurora_tf = *cam_tf;
            for mut aurora_tf in gradient_mesh.iter_mut() {
                aurora_tf.translation = cam_tf.translation;
            }
        }
    }
}