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

Switch normal mapping to shader flag instead of shaderdef #8106

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 13 additions & 10 deletions assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,25 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
pbr_input.world_position = in.world_position;
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
pbr_input.material.flags
);

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

pbr_input.N = apply_normal_mapping(
pbr_input.material.flags,
pbr_input.world_normal,
var N = normalize(pbr_input.world_normal);
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif
#endif
in.uv,
);
if (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_NORMAL_MAP_TEXTURE_BIT) != 0u {
let tangent_normal = textureSample(normal_map_texture, normal_map_sampler, in.uv).rgb;
N = apply_normal_mapping(
pbr_input.material.flags,
pbr_input.world_normal,
in.world_tangent,
tangent_normal,
);
}
#endif // VERTEX_TANGENTS
pbr_input.N = N;
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);

return tone_mapping(pbr(pbr_input));
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_pbr/src/pbr_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ bitflags::bitflags! {
const TWO_COMPONENT_NORMAL_MAP = (1 << 6);
const FLIP_NORMAL_MAP_Y = (1 << 7);
const FOG_ENABLED = (1 << 8);
const NORMAL_MAP_TEXTURE = (1 << 9);
const ALPHA_MODE_RESERVED_BITS = (Self::ALPHA_MODE_MASK_BITS << Self::ALPHA_MODE_SHIFT_BITS); // ← Bitmask reserving bits for the `AlphaMode`
const ALPHA_MODE_OPAQUE = (0 << Self::ALPHA_MODE_SHIFT_BITS); // ← Values are just sequential values bitshifted into
const ALPHA_MODE_MASK = (1 << Self::ALPHA_MODE_SHIFT_BITS); // the bitmask, and can range from 0 to 7.
Expand Down Expand Up @@ -358,6 +359,9 @@ impl AsBindGroupShaderType<StandardMaterialUniform> for StandardMaterial {
if self.occlusion_texture.is_some() {
flags |= StandardMaterialFlags::OCCLUSION_TEXTURE;
}
if self.normal_map_texture.is_some() {
flags |= StandardMaterialFlags::NORMAL_MAP_TEXTURE;
}
if self.double_sided {
flags |= StandardMaterialFlags::DOUBLE_SIDED;
}
Expand Down Expand Up @@ -435,13 +439,6 @@ impl Material for StandardMaterial {
_layout: &MeshVertexBufferLayout,
key: MaterialPipelineKey<Self>,
) -> Result<(), SpecializedMeshPipelineError> {
if key.bind_group_data.normal_map {
if let Some(fragment) = descriptor.fragment.as_mut() {
fragment
.shader_defs
.push("STANDARDMATERIAL_NORMAL_MAP".into());
}
}
descriptor.primitive.cull_mode = key.bind_group_data.cull_mode;
if let Some(label) = &mut descriptor.label {
*label = format!("pbr_{}", *label).into();
Expand Down
31 changes: 19 additions & 12 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,31 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
pbr_input.world_position = in.world_position;
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
material.flags,
);

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

pbr_input.N = apply_normal_mapping(
material.flags,
pbr_input.world_normal,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif
#endif
var N = normalize(pbr_input.world_normal);

#ifdef VERTEX_UVS
in.uv,
#endif
);
#ifdef VERTEX_TANGENTS
if (material.flags & STANDARD_MATERIAL_FLAGS_NORMAL_MAP_TEXTURE_BIT) != 0u {
// NOTE: Do NOT normalize this value before applying normal mapping
// http://www.mikktspace.com/
let tangent_normal = textureSample(normal_map_texture, normal_map_sampler, in.uv).rgb;
N = apply_normal_mapping(
material.flags,
pbr_input.world_normal,
in.world_tangent,
tangent_normal,
);
}
#endif // VERTEX_TANGENTS
#endif // VERTEX_UVS

pbr_input.N = N;
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);
pbr_input.occlusion = occlusion;

Expand Down
42 changes: 16 additions & 26 deletions crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,32 @@ fn alpha_discard(material: StandardMaterial, output_color: vec4<f32>) -> vec4<f3

fn prepare_world_normal(
world_normal: vec3<f32>,
double_sided: bool,
is_front: bool,
standard_material_flags: u32,
) -> vec3<f32> {
var output: vec3<f32> = world_normal;

// if we don't have vertex tangents, we can't be normal mapping
#ifndef VERTEX_TANGENTS
#ifndef STANDARDMATERIAL_NORMAL_MAP
let flags = standard_material_flags;
let double_sided = (flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u;
let normal_map = (flags & STANDARD_MATERIAL_FLAGS_NORMAL_MAP_TEXTURE_BIT) != 0u;

// NOTE: When NOT using normal-mapping, if looking at the back face of a double-sided
// material, the normal needs to be inverted. This is a branchless version of that.
output = (f32(!double_sided || is_front) * 2.0 - 1.0) * output;
#endif
// material, the normal needs to be inverted.
if double_sided && !normal_map && !is_front {
output *= -1.0;
}
#endif
return output;
}

fn apply_normal_mapping(
standard_material_flags: u32,
world_normal: vec3<f32>,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
world_tangent: vec4<f32>,
#endif
#endif
#ifdef VERTEX_UVS
uv: vec2<f32>,
#endif
// normal in tangent space, as provided by a standard normal map
tangent_normal: vec3<f32>,
) -> vec3<f32> {
// NOTE: The mikktspace method of normal mapping explicitly requires that the world normal NOT
// be re-normalized in the fragment shader. This is primarily to match the way mikktspace
Expand All @@ -62,23 +63,15 @@ fn apply_normal_mapping(
// unless you really know what you are doing.
// http://www.mikktspace.com/
var N: vec3<f32> = world_normal;
var Nt: vec3<f32> = tangent_normal;

#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
// NOTE: The mikktspace method of normal mapping explicitly requires that these NOT be
// normalized nor any Gram-Schmidt applied to ensure the vertex normal is orthogonal to the
// vertex tangent! Do not change this code unless you really know what you are doing.
// http://www.mikktspace.com/
var T: vec3<f32> = world_tangent.xyz;
var B: vec3<f32> = world_tangent.w * cross(N, T);
#endif
#endif

#ifdef VERTEX_TANGENTS
#ifdef VERTEX_UVS
#ifdef STANDARDMATERIAL_NORMAL_MAP
// Nt is the tangent-space normal.
var Nt = textureSample(normal_map_texture, normal_map_sampler, uv).rgb;
if (standard_material_flags & STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP) != 0u {
// Only use the xy components and derive z for 2-component normal maps.
Nt = vec3<f32>(Nt.rg * 2.0 - 1.0, 0.0);
Expand All @@ -95,12 +88,9 @@ fn apply_normal_mapping(
// calculates the normal maps so there is no error introduced. Do not change this code
// unless you really know what you are doing.
// http://www.mikktspace.com/
N = Nt.x * T + Nt.y * B + Nt.z * N;
#endif
#endif
#endif
N = normalize(Nt.x * T + Nt.y * B + Nt.z * N);

return normalize(N);
return N;
}

// NOTE: Correctly calculates the view vector depending on whether
Expand Down
24 changes: 14 additions & 10 deletions crates/bevy_pbr/src/render/pbr_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,26 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
if (material.flags & STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u {
let world_normal = prepare_world_normal(
in.world_normal,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
material.flags,
);

let normal = apply_normal_mapping(
material.flags,
world_normal,
var normal = normalize(world_normal);
#ifdef VERTEX_UVS
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif // STANDARDMATERIAL_NORMAL_MAP
if (material.flags & STANDARD_MATERIAL_FLAGS_NORMAL_MAP_TEXTURE_BIT) != 0u {
// NOTE: Do NOT normalize this value before applying normal mapping
// http://www.mikktspace.com/
let tangent_normal = textureSample(normal_map_texture, normal_map_sampler, in.uv).rgb;
normal = apply_normal_mapping(
material.flags,
world_normal,
in.world_tangent,
tangent_normal,
);
}
#endif // VERTEX_TANGENTS
#ifdef VERTEX_UVS
in.uv,
#endif // VERTEX_UVS
);

return vec4(normal * 0.5 + vec3(0.5), 1.0);
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render/pbr_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STANDARD_MATERIAL_FLAGS_UNLIT_BIT: u32 = 32u;
const STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP: u32 = 64u;
const STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y: u32 = 128u;
const STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT: u32 = 256u;
const STANDARD_MATERIAL_FLAGS_NORMAL_MAP_TEXTURE_BIT: u32 = 512u;
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_RESERVED_BITS: u32 = 3758096384u; // (0b111u32 << 29)
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE: u32 = 0u; // (0u32 << 29)
const STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK: u32 = 536870912u; // (1u32 << 29)
Expand Down