Skip to content

Commit b1ab859

Browse files
committed
Add simple Solari example: 3d_scene but with raytracing
1 parent 4553e66 commit b1ab859

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

Cargo.toml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,21 @@ required-features = ["bevy_solari", "https", "free_camera"]
15091509
name = "Solari"
15101510
description = "Demonstrates realtime dynamic raytraced lighting using Bevy Solari."
15111511
category = "3D Rendering"
1512-
wasm = false # Raytracing is not supported on the web
1512+
# Raytracing is not supported on the web
1513+
wasm = false
1514+
1515+
[[example]]
1516+
name = "solari_3d_scene"
1517+
path = "examples/3d/solari_3d_scene.rs"
1518+
doc-scrape-examples = true
1519+
required-features = ["bevy_solari"]
1520+
1521+
[package.metadata.example.solari_3d_scene]
1522+
name = "Solari 3D Scene"
1523+
description = "Simple 3D scene with basic shapes and realtime dynamic raytraced lighting using Bevy Solari"
1524+
category = "3D Rendering"
1525+
# Raytracing is not supported on the web
1526+
wasm = false
15131527

15141528
[[example]]
15151529
name = "spherical_area_lights"

examples/3d/solari_3d_scene.rs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Example | Description
194194
[Shadow Caster and Receiver](../examples/3d/shadow_caster_receiver.rs) | Demonstrates how to prevent meshes from casting/receiving shadows in a 3d scene
195195
[Skybox](../examples/3d/skybox.rs) | Load a cubemap texture onto a cube like a skybox and cycle through different compressed texture formats.
196196
[Solari](../examples/3d/solari.rs) | Demonstrates realtime dynamic raytraced lighting using Bevy Solari.
197+
[Solari 3D Scene](../examples/3d/solari_3d_scene.rs) | Simple 3D scene with basic shapes and realtime dynamic raytraced lighting using Bevy Solari
197198
[Specular Tint](../examples/3d/specular_tint.rs) | Demonstrates specular tints and maps
198199
[Spherical Area Lights](../examples/3d/spherical_area_lights.rs) | Demonstrates how point light radius values affect light behavior
199200
[Split Screen](../examples/3d/split_screen.rs) | Demonstrates how to render two cameras to the same window to accomplish "split screen"

0 commit comments

Comments
 (0)