Skip to content

Commit

Permalink
Add support for flat shading with indiced meshes
Browse files Browse the repository at this point in the history
Use the partial derivative (dFdx, dFdy) for computing facet normals
in the fragment shader.

I made this as an alternative for bevyengine#1808 when for example
making procedually generated terrain.
  • Loading branch information
sweepline committed Oct 22, 2021
1 parent 6a8a8c9 commit 94a53f8
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ path = "examples/3d/wireframe.rs"
name = "z_sort_debug"
path = "examples/3d/z_sort_debug.rs"

[[example]]
name = "flat_shading"
path = "examples/3d/flat_shading.rs"

# Application
[[example]]
name = "custom_loop"
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub struct StandardMaterial {
#[render_resources(ignore)]
#[shader_def]
pub unlit: bool,
/// This allows for flat shading even with indiced meshes.
#[render_resources(ignore)]
#[shader_def]
pub flat_shading: bool,
}

impl Default for StandardMaterial {
Expand Down Expand Up @@ -69,6 +73,7 @@ impl Default for StandardMaterial {
emissive: Color::BLACK,
emissive_texture: None,
unlit: false,
flat_shading: false,
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ struct PointLight {
vec4 color;
vec4 lightParams;
};

struct DirectionalLight {
vec4 direction;
vec4 color;
};

layout(location = 0) in vec3 v_WorldPosition;

#ifndef STANDARDMATERIAL_FLAT_SHADING
layout(location = 1) in vec3 v_WorldNormal;
#endif
layout(location = 2) in vec2 v_Uv;

#ifdef STANDARDMATERIAL_NORMAL_MAP
Expand Down Expand Up @@ -368,7 +371,12 @@ void main() {

float roughness = perceptualRoughnessToRoughness(perceptual_roughness);

vec3 N = normalize(v_WorldNormal);
# ifdef STANDARDMATERIAL_FLAT_SHADING
vec3 N = normalize(cross(dFdy(v_WorldPosition), dFdx(v_WorldPosition)));
# endif
# ifndef STANDARDMATERIAL_FLAT_SHADING
vec3 N = v_WorldNormal;
# endif

# ifdef STANDARDMATERIAL_NORMAL_MAP
vec3 T = normalize(v_WorldTangent.xyz);
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.vert
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ layout(location = 3) in vec4 Vertex_Tangent;
#endif

layout(location = 0) out vec3 v_WorldPosition;
#ifndef STANDARDMATERIAL_FLAT_SHADING
layout(location = 1) out vec3 v_WorldNormal;
#endif
layout(location = 2) out vec2 v_Uv;

layout(set = 0, binding = 0) uniform CameraViewProj {
Expand All @@ -27,7 +29,9 @@ layout(set = 2, binding = 0) uniform Transform {
void main() {
vec4 world_position = Model * vec4(Vertex_Position, 1.0);
v_WorldPosition = world_position.xyz;
#ifndef STANDARDMATERIAL_FLAT_SHADING
v_WorldNormal = mat3(Model) * Vertex_Normal;
#endif
v_Uv = Vertex_Uv;
#ifdef STANDARDMATERIAL_NORMAL_MAP
v_WorldTangent = vec4(mat3(Model) * Vertex_Tangent.xyz, Vertex_Tangent.w);
Expand Down
63 changes: 63 additions & 0 deletions examples/3d/flat_shading.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use bevy::{
prelude::*,
render::{
mesh::{Indices, VertexAttributeValues},
pipeline::PrimitiveTopology,
},
};

fn main() {
App::new()
.insert_resource(Msaa { samples: 4 })
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}

/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// flat
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 0.5,
subdivisions: 4,
})),
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.8, 0.7, 0.6),
flat_shading: true,
..Default::default()
}),
transform: Transform::from_xyz(-0.55, 0.5, 0.0),
..Default::default()
});
// smooth
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 0.5,
subdivisions: 4,
})),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.55, 0.5, 0.0),
..Default::default()
});
// plane
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
});
// light
commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(5.0, 5.0, 5.0),
..Default::default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(1.0, 3.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
}

0 comments on commit 94a53f8

Please sign in to comment.