Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bevy_light/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bevy_color = { path = "../bevy_color", version = "0.18.0-dev", features = [

# other
tracing = { version = "0.1", default-features = false }
wgpu-types = { version = "27", default-features = false }

[features]
default = []
Expand Down
71 changes: 70 additions & 1 deletion crates/bevy_light/src/probe.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use bevy_asset::Handle;
use bevy_asset::{Assets, Handle, RenderAssetUsages};
use bevy_camera::visibility::Visibility;
use bevy_color::{Color, ColorToPacked, Srgba};
use bevy_ecs::prelude::*;
use bevy_image::Image;
use bevy_math::{Quat, UVec2};
use bevy_reflect::prelude::*;
use bevy_transform::components::Transform;
use wgpu_types::{
Extent3d, TextureDimension, TextureFormat, TextureViewDescriptor, TextureViewDimension,
};

/// A marker component for a light probe, which is a cuboid region that provides
/// global illumination to all fragments inside it.
Expand Down Expand Up @@ -96,6 +100,71 @@ pub struct EnvironmentMapLight {
pub affects_lightmapped_mesh_diffuse: bool,
}

impl EnvironmentMapLight {
/// An environment map with a uniform color, useful for uniform ambient lighting.
pub fn solid_color(assets: &mut Assets<Image>, color: Color) -> Self {
Self::hemispherical_gradient(assets, color, color, color)
}

/// An environment map with a hemispherical gradient, fading between the sky and ground colors
/// at the horizon. Useful as a very simple 'sky'.
pub fn hemispherical_gradient(
assets: &mut Assets<Image>,
top_color: Color,
mid_color: Color,
bottom_color: Color,
) -> Self {
let handle = assets.add(Self::hemispherical_gradient_cubemap(
top_color,
mid_color,
bottom_color,
));

Self {
diffuse_map: handle.clone(),
specular_map: handle,
..Default::default()
}
}

pub(crate) fn hemispherical_gradient_cubemap(
top_color: Color,
mid_color: Color,
bottom_color: Color,
) -> Image {
let top_color: Srgba = top_color.into();
let mid_color: Srgba = mid_color.into();
let bottom_color: Srgba = bottom_color.into();
Image {
texture_view_descriptor: Some(TextureViewDescriptor {
dimension: Some(TextureViewDimension::Cube),
..Default::default()
}),
..Image::new(
Extent3d {
width: 1,
height: 1,
depth_or_array_layers: 6,
},
TextureDimension::D2,
[
mid_color,
mid_color,
top_color,
bottom_color,
mid_color,
mid_color,
]
.into_iter()
.flat_map(Srgba::to_u8_array)
Comment thread
atlv24 marked this conversation as resolved.
Outdated
.collect(),
TextureFormat::Rgba8UnormSrgb,
Comment thread
atlv24 marked this conversation as resolved.
Outdated
RenderAssetUsages::RENDER_WORLD,
)
}
}
}

impl Default for EnvironmentMapLight {
fn default() -> Self {
EnvironmentMapLight {
Expand Down