|
| 1 | +use bevy_app::{App, Plugin}; |
| 2 | +use bevy_asset::{load_internal_asset, Handle, HandleUntyped}; |
| 3 | +use bevy_core_pipeline::prelude::Camera3d; |
| 4 | +use bevy_ecs::{prelude::Component, query::With}; |
| 5 | +use bevy_reflect::{Reflect, TypeUuid}; |
| 6 | +use bevy_render::{ |
| 7 | + extract_component::{ExtractComponent, ExtractComponentPlugin}, |
| 8 | + render_asset::RenderAssets, |
| 9 | + render_resource::{ |
| 10 | + BindGroupEntry, BindGroupLayoutEntry, BindingResource, BindingType, SamplerBindingType, |
| 11 | + Shader, ShaderStages, TextureSampleType, TextureViewDimension, |
| 12 | + }, |
| 13 | + texture::{FallbackImageCubemap, Image}, |
| 14 | +}; |
| 15 | + |
| 16 | +pub const ENVIRONMENT_MAP_SHADER_HANDLE: HandleUntyped = |
| 17 | + HandleUntyped::weak_from_u64(Shader::TYPE_UUID, 154476556247605696); |
| 18 | + |
| 19 | +pub struct EnvironmentMapPlugin; |
| 20 | + |
| 21 | +impl Plugin for EnvironmentMapPlugin { |
| 22 | + fn build(&self, app: &mut App) { |
| 23 | + load_internal_asset!( |
| 24 | + app, |
| 25 | + ENVIRONMENT_MAP_SHADER_HANDLE, |
| 26 | + "environment_map.wgsl", |
| 27 | + Shader::from_wgsl |
| 28 | + ); |
| 29 | + |
| 30 | + app.register_type::<EnvironmentMapLight>() |
| 31 | + .add_plugin(ExtractComponentPlugin::<EnvironmentMapLight>::default()); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// Environment map based ambient lighting representing light from distant scenery. |
| 36 | +/// |
| 37 | +/// When added to a 3D camera, this component adds indirect light |
| 38 | +/// to every point of the scene (including inside, enclosed areas) based on |
| 39 | +/// an environment cubemap texture. This is similiar to [`crate::AmbientLight`], but |
| 40 | +/// higher quality, and is intended for outdoor scenes. |
| 41 | +/// |
| 42 | +/// The environment map must be prefiltered into a diffuse and specular cubemap based on the |
| 43 | +/// [split-sum approximation](https://cdn2.unrealengine.com/Resources/files/2013SiggraphPresentationsNotes-26915738.pdf). |
| 44 | +/// |
| 45 | +/// To prefilter your environment map, you can use `KhronosGroup`'s [glTF-IBL-Sampler](https://github.com/KhronosGroup/glTF-IBL-Sampler). |
| 46 | +/// The diffuse map uses the Lambertian distribution, and the specular map uses the GGX distribution. |
| 47 | +/// |
| 48 | +/// `KhronosGroup` also has several prefiltered environment maps that can be found [here](https://github.com/KhronosGroup/glTF-Sample-Environments). |
| 49 | +#[derive(Component, Reflect, Clone)] |
| 50 | +pub struct EnvironmentMapLight { |
| 51 | + pub diffuse_map: Handle<Image>, |
| 52 | + pub specular_map: Handle<Image>, |
| 53 | +} |
| 54 | + |
| 55 | +impl EnvironmentMapLight { |
| 56 | + /// Whether or not all textures neccesary to use the environment map |
| 57 | + /// have been loaded by the asset server. |
| 58 | + pub fn is_loaded(&self, images: &RenderAssets<Image>) -> bool { |
| 59 | + images.get(&self.diffuse_map).is_some() && images.get(&self.specular_map).is_some() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl ExtractComponent for EnvironmentMapLight { |
| 64 | + type Query = &'static Self; |
| 65 | + type Filter = With<Camera3d>; |
| 66 | + type Out = Self; |
| 67 | + |
| 68 | + fn extract_component(item: bevy_ecs::query::QueryItem<'_, Self::Query>) -> Option<Self::Out> { |
| 69 | + Some(item.clone()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +pub fn get_bindings<'a>( |
| 74 | + environment_map_light: Option<&EnvironmentMapLight>, |
| 75 | + images: &'a RenderAssets<Image>, |
| 76 | + fallback_image_cubemap: &'a FallbackImageCubemap, |
| 77 | + bindings: [u32; 3], |
| 78 | +) -> [BindGroupEntry<'a>; 3] { |
| 79 | + let (diffuse_map, specular_map) = match ( |
| 80 | + environment_map_light.and_then(|env_map| images.get(&env_map.diffuse_map)), |
| 81 | + environment_map_light.and_then(|env_map| images.get(&env_map.specular_map)), |
| 82 | + ) { |
| 83 | + (Some(diffuse_map), Some(specular_map)) => { |
| 84 | + (&diffuse_map.texture_view, &specular_map.texture_view) |
| 85 | + } |
| 86 | + _ => ( |
| 87 | + &fallback_image_cubemap.texture_view, |
| 88 | + &fallback_image_cubemap.texture_view, |
| 89 | + ), |
| 90 | + }; |
| 91 | + |
| 92 | + [ |
| 93 | + BindGroupEntry { |
| 94 | + binding: bindings[0], |
| 95 | + resource: BindingResource::TextureView(diffuse_map), |
| 96 | + }, |
| 97 | + BindGroupEntry { |
| 98 | + binding: bindings[1], |
| 99 | + resource: BindingResource::TextureView(specular_map), |
| 100 | + }, |
| 101 | + BindGroupEntry { |
| 102 | + binding: bindings[2], |
| 103 | + resource: BindingResource::Sampler(&fallback_image_cubemap.sampler), |
| 104 | + }, |
| 105 | + ] |
| 106 | +} |
| 107 | + |
| 108 | +pub fn get_bind_group_layout_entries(bindings: [u32; 3]) -> [BindGroupLayoutEntry; 3] { |
| 109 | + [ |
| 110 | + BindGroupLayoutEntry { |
| 111 | + binding: bindings[0], |
| 112 | + visibility: ShaderStages::FRAGMENT, |
| 113 | + ty: BindingType::Texture { |
| 114 | + sample_type: TextureSampleType::Float { filterable: true }, |
| 115 | + view_dimension: TextureViewDimension::Cube, |
| 116 | + multisampled: false, |
| 117 | + }, |
| 118 | + count: None, |
| 119 | + }, |
| 120 | + BindGroupLayoutEntry { |
| 121 | + binding: bindings[1], |
| 122 | + visibility: ShaderStages::FRAGMENT, |
| 123 | + ty: BindingType::Texture { |
| 124 | + sample_type: TextureSampleType::Float { filterable: true }, |
| 125 | + view_dimension: TextureViewDimension::Cube, |
| 126 | + multisampled: false, |
| 127 | + }, |
| 128 | + count: None, |
| 129 | + }, |
| 130 | + BindGroupLayoutEntry { |
| 131 | + binding: bindings[2], |
| 132 | + visibility: ShaderStages::FRAGMENT, |
| 133 | + ty: BindingType::Sampler(SamplerBindingType::Filtering), |
| 134 | + count: None, |
| 135 | + }, |
| 136 | + ] |
| 137 | +} |
0 commit comments