Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the prepass normal texture in main pass when possible #8231

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/bevy_pbr/src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_app::{App, Plugin};
use bevy_asset::{AddAsset, AssetEvent, AssetServer, Assets, Handle};
use bevy_core_pipeline::{
core_3d::{AlphaMask3d, Opaque3d, Transparent3d},
prepass::NormalPrepass,
tonemapping::{DebandDither, Tonemapping},
};
use bevy_derive::{Deref, DerefMut};
Expand Down Expand Up @@ -380,6 +381,7 @@ pub fn queue_material_meshes<M: Material>(
Option<&Tonemapping>,
Option<&DebandDither>,
Option<&EnvironmentMapLight>,
Option<&NormalPrepass>,
&mut RenderPhase<Opaque3d>,
&mut RenderPhase<AlphaMask3d>,
&mut RenderPhase<Transparent3d>,
Expand All @@ -393,6 +395,7 @@ pub fn queue_material_meshes<M: Material>(
tonemapping,
dither,
environment_map,
normal_prepass,
mut opaque_phase,
mut alpha_mask_phase,
mut transparent_phase,
Expand All @@ -405,6 +408,10 @@ pub fn queue_material_meshes<M: Material>(
let mut view_key = MeshPipelineKey::from_msaa_samples(msaa.samples())
| MeshPipelineKey::from_hdr(view.hdr);

if normal_prepass.is_some() {
view_key |= MeshPipelineKey::NORMAL_PREPASS;
}

let environment_map_loaded = match environment_map {
Some(environment_map) => environment_map.is_loaded(&images),
None => false,
Expand Down
4 changes: 4 additions & 0 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ impl SpecializedMeshPipeline for MeshPipeline {
let mut shader_defs = Vec::new();
let mut vertex_attributes = Vec::new();

if key.contains(MeshPipelineKey::NORMAL_PREPASS) {
shader_defs.push("LOAD_PREPASS_NORMALS".into());
}

if layout.contains(Mesh::ATTRIBUTE_POSITION) {
shader_defs.push("VERTEX_POSITIONS".into());
vertex_attributes.push(Mesh::ATTRIBUTE_POSITION.at_shader_location(0));
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import bevy_pbr::fog
#import bevy_pbr::pbr_functions

#import bevy_pbr::prepass_utils

struct FragmentInput {
@builtin(front_facing) is_front: bool,
@builtin(position) frag_coord: vec4<f32>,
Expand Down Expand Up @@ -69,11 +71,16 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
#endif
pbr_input.frag_coord = in.frag_coord;
pbr_input.world_position = in.world_position;

#ifdef LOAD_PREPASS_NORMALS
pbr_input.world_normal = prepass_normal(in.frag_coord, 0u);
#else // LOAD_PREPASS_NORMALS
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
);
#endif // LOAD_PREPASS_NORMALS

pbr_input.is_orthographic = view.projection[3].w == 1.0;

Expand Down