Skip to content

Commit 7c6a80a

Browse files
committed
Fixes and improvments from codereview
Fixed not normalizing normal in fragment shader Many docstring and comment improvements
1 parent 69af890 commit 7c6a80a

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

crates/bevy_pbr/src/material.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ pub struct StandardMaterial {
4242
#[render_resources(ignore)]
4343
#[shader_def]
4444
pub unlit: bool,
45-
/// This allows for flat shading even with indiced meshes.
45+
/// Flat shading makes the shader generate normals per face instead of per vertex.
46+
/// This means that you get a uniform color over the whole triangle (think Virtua Racing).
47+
/// The method uses fragment shader derivatives which can cause some artifacts in some cases.
48+
/// An alternative is using `Mesh::duplicate_vertices()` and `Mesh::compute_flat_normals()`
49+
/// and setting this setting to false, this will make sure no artifacts occur.
4650
#[render_resources(ignore)]
4751
#[shader_def]
4852
pub flat_shading: bool,

crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,8 @@ void main() {
373373

374374
# ifdef STANDARDMATERIAL_FLAT_SHADING
375375
vec3 N = normalize(cross(dFdy(v_WorldPosition), dFdx(v_WorldPosition)));
376-
# endif
377-
# ifndef STANDARDMATERIAL_FLAT_SHADING
378-
vec3 N = v_WorldNormal;
376+
# else
377+
vec3 N = normalize(v_WorldNormal);
379378
# endif
380379

381380
# ifdef STANDARDMATERIAL_NORMAL_MAP

examples/3d/flat_shading.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use bevy::prelude::*;
22

3+
/// This example shows the difference between flat shading
4+
/// and smooth shading (default) in `StandardMaterial`.
5+
/// Flat shading gives a much more "Polygonal" or "Retro" look to meshes.
36
fn main() {
47
App::new()
58
.insert_resource(Msaa { samples: 4 })
@@ -14,30 +17,31 @@ fn setup(
1417
mut meshes: ResMut<Assets<Mesh>>,
1518
mut materials: ResMut<Assets<StandardMaterial>>,
1619
) {
17-
// flat
20+
// Flat shaded icosphere (ORANGE)
1821
commands.spawn_bundle(PbrBundle {
1922
mesh: meshes.add(Mesh::from(shape::Icosphere {
2023
radius: 0.5,
2124
subdivisions: 4,
2225
})),
2326
material: materials.add(StandardMaterial {
24-
base_color: Color::rgb(0.8, 0.7, 0.6),
27+
base_color: Color::ORANGE,
2528
flat_shading: true,
2629
..Default::default()
2730
}),
2831
transform: Transform::from_xyz(-0.55, 0.5, 0.0),
2932
..Default::default()
3033
});
31-
// smooth
34+
// Smooth shaded icosphere (BLUE)
3235
commands.spawn_bundle(PbrBundle {
3336
mesh: meshes.add(Mesh::from(shape::Icosphere {
3437
radius: 0.5,
3538
subdivisions: 4,
3639
})),
37-
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
40+
material: materials.add(Color::BLUE.into()),
3841
transform: Transform::from_xyz(0.55, 0.5, 0.0),
3942
..Default::default()
4043
});
44+
4145
// plane
4246
commands.spawn_bundle(PbrBundle {
4347
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Example | File | Description
107107
`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
108108
`wireframe` | [`3d/wireframe.rs`](./3d/wireframe.rs) | Showcases wireframe rendering
109109
`z_sort_debug` | [`3d/z_sort_debug.rs`](./3d/z_sort_debug.rs) | Visualizes camera Z-ordering
110-
`flat_shading` | [`3d/flat_shading.rs`](./3d/flat_shading.rs) | Simple 3D scene showing flat and normal (smooth) shading
110+
`flat_shading` | [`3d/flat_shading.rs`](./3d/flat_shading.rs) | Simple 3D scene showing flat and smooth (default) shading
111111

112112
## Application
113113

0 commit comments

Comments
 (0)