|
| 1 | +//! A simple 3D scene with light shining over a cube sitting on a plane. |
| 2 | +//! |
| 3 | +//! This example is intended to demonstrate the bare minimum setup required to enable Solari, |
| 4 | +//! Bevy's real-time raytraced dynamic lighting solution. |
| 5 | +
|
| 6 | +use bevy::{ |
| 7 | + camera::CameraMainTextureUsages, color::palettes::css, prelude::*, |
| 8 | + render::render_resource::TextureUsages, solari::prelude::*, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Real-time raytracing produces noisy output because it cannot trace enough rays per pixel in a single frame. |
| 12 | +/// Instead, it distributes work stochastically across frames. |
| 13 | +/// Therefore, a denoiser is required to achieve high-quality image. |
| 14 | +/// DLSS Ray Reconstruction provides hardware-accelerated denoising. |
| 15 | +
|
| 16 | +#[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] |
| 17 | +use bevy::anti_alias::dlss::{ |
| 18 | + Dlss, DlssProjectId, DlssRayReconstructionFeature, DlssRayReconstructionSupported, |
| 19 | +}; |
| 20 | + |
| 21 | +fn main() { |
| 22 | + let mut app = App::new(); |
| 23 | + |
| 24 | + #[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] |
| 25 | + app.insert_resource(DlssProjectId(bevy_asset::uuid::uuid!( |
| 26 | + "bd7a4665-6340-46f3-b4f8-c6d0f7101c27" // Don't copy paste this - generate your own UUID! |
| 27 | + ))); |
| 28 | + |
| 29 | + app.add_plugins(DefaultPlugins) |
| 30 | + .add_plugins(SolariPlugins) |
| 31 | + .add_systems(Startup, setup) |
| 32 | + .run(); |
| 33 | +} |
| 34 | + |
| 35 | +/// set up a simple 3D scene |
| 36 | +fn setup( |
| 37 | + mut commands: Commands, |
| 38 | + mut meshes: ResMut<Assets<Mesh>>, |
| 39 | + mut materials: ResMut<Assets<StandardMaterial>>, |
| 40 | + #[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] dlss_rr_supported: Option< |
| 41 | + Res<DlssRayReconstructionSupported>, |
| 42 | + >, |
| 43 | +) { |
| 44 | + let cube = meshes.add( |
| 45 | + Cuboid::from_length(1.0) |
| 46 | + .mesh() |
| 47 | + .build() |
| 48 | + // Solari requires ATTRIBUTE_TANGENT |
| 49 | + .with_generated_tangents() |
| 50 | + .expect("Cuboid mesh has ATTRIBUTE_UV_0"), |
| 51 | + ); |
| 52 | + |
| 53 | + // base |
| 54 | + commands.spawn(( |
| 55 | + Mesh3d(cube.clone()), |
| 56 | + RaytracingMesh3d(cube.clone()), |
| 57 | + MeshMaterial3d(materials.add(StandardMaterial { |
| 58 | + base_color: css::DARK_GRAY.into(), |
| 59 | + perceptual_roughness: 0.0, |
| 60 | + metallic: 0.15, |
| 61 | + ..default() |
| 62 | + })), |
| 63 | + Transform::from_xyz(0.0, -0.1, 0.0).with_scale(Vec3::splat(7.0).with_y(0.2)), |
| 64 | + )); |
| 65 | + // cube |
| 66 | + commands.spawn(( |
| 67 | + Mesh3d(cube.clone()), |
| 68 | + RaytracingMesh3d(cube.clone()), |
| 69 | + MeshMaterial3d(materials.add(Color::from(css::LIGHT_BLUE))), |
| 70 | + Transform::from_xyz(1.0, 0.5, -0.25), |
| 71 | + )); |
| 72 | + // emissive light |
| 73 | + commands.spawn(( |
| 74 | + Mesh3d(cube.clone()), |
| 75 | + RaytracingMesh3d(cube), |
| 76 | + MeshMaterial3d(materials.add(StandardMaterial { |
| 77 | + base_color: css::RED.into(), |
| 78 | + emissive: LinearRgba::from(css::RED) * 100_000.0, |
| 79 | + ..default() |
| 80 | + })), |
| 81 | + Transform::from_xyz(-1.0, 0.25, 0.25).with_scale(Vec3::splat(0.5)), |
| 82 | + )); |
| 83 | + // directional light |
| 84 | + commands.spawn(( |
| 85 | + DirectionalLight { |
| 86 | + illuminance: light_consts::lux::HALLWAY, |
| 87 | + ..default() |
| 88 | + }, |
| 89 | + Transform::from_xyz(-2.5, 4.5, -3.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 90 | + )); |
| 91 | + // camera |
| 92 | + let mut _camera = commands.spawn(( |
| 93 | + Camera3d::default(), |
| 94 | + Transform::from_xyz(-2.5, 4.5, 9.0).looking_at(Vec3::ZERO, Vec3::Y), |
| 95 | + CameraMainTextureUsages::default().with(TextureUsages::STORAGE_BINDING), |
| 96 | + Msaa::Off, |
| 97 | + SolariLighting::default(), |
| 98 | + )); |
| 99 | + |
| 100 | + // Using DLSS Ray Reconstruction for denoising (and cheaper rendering via upscaling) is _highly_ recommended when using Solari |
| 101 | + #[cfg(all(feature = "dlss", not(feature = "force_disable_dlss")))] |
| 102 | + if dlss_rr_supported.is_some() { |
| 103 | + _camera.insert(Dlss::<DlssRayReconstructionFeature> { |
| 104 | + perf_quality_mode: Default::default(), |
| 105 | + reset: Default::default(), |
| 106 | + _phantom_data: Default::default(), |
| 107 | + }); |
| 108 | + } |
| 109 | +} |
0 commit comments