1use argh::FromArgs;
4use bevy::{
5 core_pipeline::prepass::{DeferredPrepass, DepthPrepass, MotionVectorPrepass},
6 pbr::{DefaultOpaqueRendererMethod, Lightmap},
7 prelude::*,
8};
9
10#[derive(FromArgs, Resource)]
12struct Args {
13 #[argh(switch)]
15 deferred: bool,
16 #[argh(switch)]
18 bicubic: bool,
19}
20
21fn main() {
22 #[cfg(not(target_arch = "wasm32"))]
23 let args: Args = argh::from_env();
24 #[cfg(target_arch = "wasm32")]
25 let args: Args = Args::from_args(&[], &[]).unwrap();
26
27 let mut app = App::new();
28 app.add_plugins(DefaultPlugins)
29 .insert_resource(AmbientLight::NONE);
30
31 if args.deferred {
32 app.insert_resource(DefaultOpaqueRendererMethod::deferred());
33 }
34
35 app.insert_resource(args)
36 .add_systems(Startup, setup)
37 .add_systems(Update, add_lightmaps_to_meshes)
38 .run();
39}
40
41fn setup(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
42 commands.spawn(SceneRoot(asset_server.load(
43 GltfAssetLabel::Scene(0).from_asset("models/CornellBox/CornellBox.glb"),
44 )));
45
46 let mut camera = commands.spawn((
47 Camera3d::default(),
48 Transform::from_xyz(-278.0, 273.0, 800.0),
49 ));
50
51 if args.deferred {
52 camera.insert((
53 DepthPrepass,
54 MotionVectorPrepass,
55 DeferredPrepass,
56 Msaa::Off,
57 ));
58 }
59}
60
61fn add_lightmaps_to_meshes(
62 mut commands: Commands,
63 asset_server: Res<AssetServer>,
64 mut materials: ResMut<Assets<StandardMaterial>>,
65 meshes: Query<
66 (Entity, &Name, &MeshMaterial3d<StandardMaterial>),
67 (With<Mesh3d>, Without<Lightmap>),
68 >,
69 args: Res<Args>,
70) {
71 let exposure = 250.0;
72 for (entity, name, material) in meshes.iter() {
73 if &**name == "large_box" {
74 materials.get_mut(material).unwrap().lightmap_exposure = exposure;
75 commands.entity(entity).insert(Lightmap {
76 image: asset_server.load("lightmaps/CornellBox-Large.zstd.ktx2"),
77 bicubic_sampling: args.bicubic,
78 ..default()
79 });
80 continue;
81 }
82
83 if &**name == "small_box" {
84 materials.get_mut(material).unwrap().lightmap_exposure = exposure;
85 commands.entity(entity).insert(Lightmap {
86 image: asset_server.load("lightmaps/CornellBox-Small.zstd.ktx2"),
87 bicubic_sampling: args.bicubic,
88 ..default()
89 });
90 continue;
91 }
92
93 if name.starts_with("cornell_box") {
94 materials.get_mut(material).unwrap().lightmap_exposure = exposure;
95 commands.entity(entity).insert(Lightmap {
96 image: asset_server.load("lightmaps/CornellBox-Box.zstd.ktx2"),
97 bicubic_sampling: args.bicubic,
98 ..default()
99 });
100 continue;
101 }
102 }
103}