Skip to content

Commit 1d0d8a3

Browse files
committed
Spherical area lights example (#3498)
I was putting together the Bevy 0.6 release blog post and wanted a simple area light radius example, so here we are :) ![image](https://user-images.githubusercontent.com/2694663/147786883-63454290-b5b1-4456-9762-0b0b47e16bb5.png)
1 parent c641a90 commit 1d0d8a3

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ path = "examples/3d/shadow_biases.rs"
181181
name = "shadow_caster_receiver"
182182
path = "examples/3d/shadow_caster_receiver.rs"
183183

184+
[[example]]
185+
name = "spherical_area_lights"
186+
path = "examples/3d/spherical_area_lights.rs"
187+
184188
[[example]]
185189
name = "texture"
186190
path = "examples/3d/texture.rs"

examples/3d/spherical_area_lights.rs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use bevy::prelude::*;
2+
3+
fn main() {
4+
App::new()
5+
.insert_resource(ClearColor(Color::BLACK))
6+
.add_plugins(DefaultPlugins)
7+
.add_startup_system(setup)
8+
.run();
9+
}
10+
11+
fn setup(
12+
mut commands: Commands,
13+
mut meshes: ResMut<Assets<Mesh>>,
14+
mut materials: ResMut<Assets<StandardMaterial>>,
15+
) {
16+
// camera
17+
commands.spawn_bundle(PerspectiveCameraBundle {
18+
transform: Transform::from_xyz(1.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
19+
..Default::default()
20+
});
21+
22+
// plane
23+
commands.spawn_bundle(PbrBundle {
24+
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
25+
material: materials.add(StandardMaterial {
26+
base_color: Color::rgb(0.2, 0.2, 0.2),
27+
perceptual_roughness: 0.08,
28+
..Default::default()
29+
}),
30+
..Default::default()
31+
});
32+
33+
const COUNT: usize = 6;
34+
let position_range = -4.0..4.0;
35+
let radius_range = 0.0..0.8;
36+
let pos_len = position_range.end - position_range.start;
37+
let radius_len = radius_range.end - radius_range.start;
38+
let mesh = meshes.add(Mesh::from(shape::UVSphere {
39+
sectors: 128,
40+
stacks: 64,
41+
..Default::default()
42+
}));
43+
44+
for i in 0..COUNT {
45+
let percent = i as f32 / COUNT as f32;
46+
let radius = radius_range.start + percent * radius_len;
47+
48+
// sphere light
49+
commands
50+
.spawn_bundle(PbrBundle {
51+
mesh: mesh.clone(),
52+
material: materials.add(StandardMaterial {
53+
base_color: Color::rgb(0.5, 0.5, 1.0),
54+
unlit: true,
55+
..Default::default()
56+
}),
57+
transform: Transform::from_xyz(position_range.start + percent * pos_len, 0.6, 0.0)
58+
.with_scale(Vec3::splat(radius)),
59+
..Default::default()
60+
})
61+
.with_children(|children| {
62+
children.spawn_bundle(PointLightBundle {
63+
point_light: PointLight {
64+
intensity: 1500.0,
65+
radius,
66+
color: Color::rgb(0.2, 0.2, 1.0),
67+
..Default::default()
68+
},
69+
..Default::default()
70+
});
71+
});
72+
}
73+
}

examples/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ Example | File | Description
105105
`pbr` | [`3d/pbr.rs`](./3d/pbr.rs) | Demonstrates use of Physically Based Rendering (PBR) properties
106106
`shadow_caster_receiver` | [`3d/shadow_caster_receiver.rs`](./3d/shadow_caster_receiver.rs) | Demonstrates how to prevent meshes from casting/receiving shadows in a 3d scene
107107
`shadow_biases` | [`3d/shadow_biases.rs`](./3d/shadow_biases.rs) | Demonstrates how shadow biases affect shadows in a 3d scene
108+
`spherical_area_lights` | [`3d/spherical_area_lights.rs`](./3d/spherical_area_lights.rs) | Demonstrates how point light radius values affect light behavior.
108109
`texture` | [`3d/texture.rs`](./3d/texture.rs) | Shows configuration of texture materials
109110
`update_gltf_scene` | [`3d/update_gltf_scene.rs`](./3d/update_gltf_scene.rs) | Update a scene from a gltf file, either by spawning the scene as a child of another entity, or by accessing the entities of the scene
110111
`wireframe` | [`3d/wireframe.rs`](./3d/wireframe.rs) | Showcases wireframe rendering

0 commit comments

Comments
 (0)