Skip to content

Commit 5a36a61

Browse files
committed
Revert "bevy_pbr: Fix tangent and normal normalization (bevyengine#5666)"
This reverts commit 681c9c6.
1 parent f6ef378 commit 5a36a61

File tree

4 files changed

+14
-55
lines changed

4 files changed

+14
-55
lines changed

crates/bevy_pbr/src/render/mesh.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use bevy_ecs::{
1818
query::ROQueryItem,
1919
system::{lifetimeless::*, SystemParamItem, SystemState},
2020
};
21-
use bevy_math::{Mat3A, Mat4, Vec2};
21+
use bevy_math::{Mat4, Vec2};
2222
use bevy_reflect::TypeUuid;
2323
use bevy_render::{
2424
extract_component::{ComponentUniforms, DynamicUniformIndex, UniformComponentPlugin},
@@ -134,9 +134,6 @@ bitflags::bitflags! {
134134
#[repr(transparent)]
135135
struct MeshFlags: u32 {
136136
const SHADOW_RECEIVER = (1 << 0);
137-
// Indicates the sign of the determinant of the 3x3 model matrix. If the sign is positive,
138-
// then the flag should be set, else it should not be set.
139-
const SIGN_DETERMINANT_MODEL_3X3 = (1 << 31);
140137
const NONE = 0;
141138
const UNINITIALIZED = 0xFFFF;
142139
}
@@ -167,14 +164,11 @@ pub fn extract_meshes(
167164
{
168165
let transform = transform.compute_matrix();
169166
let previous_transform = previous_transform.map(|t| t.0).unwrap_or(transform);
170-
let mut flags = if not_receiver.is_some() {
167+
let flags = if not_receiver.is_some() {
171168
MeshFlags::empty()
172169
} else {
173170
MeshFlags::SHADOW_RECEIVER
174171
};
175-
if Mat3A::from_mat4(transform).determinant().is_sign_positive() {
176-
flags |= MeshFlags::SIGN_DETERMINANT_MODEL_3X3;
177-
}
178172
let uniform = MeshUniform {
179173
flags: flags.bits,
180174
transform,

crates/bevy_pbr/src/render/mesh_functions.wgsl

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,20 @@ fn mesh_position_local_to_clip(model: mat4x4<f32>, vertex_position: vec4<f32>) -
1717
}
1818

1919
fn mesh_normal_local_to_world(vertex_normal: vec3<f32>) -> vec3<f32> {
20-
// NOTE: The mikktspace method of normal mapping requires that the world normal is
21-
// re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
22-
// and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
23-
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
24-
// unless you really know what you are doing.
25-
// http://www.mikktspace.com/
26-
return normalize(
27-
mat3x3<f32>(
28-
mesh.inverse_transpose_model[0].xyz,
29-
mesh.inverse_transpose_model[1].xyz,
30-
mesh.inverse_transpose_model[2].xyz
31-
) * vertex_normal
32-
);
33-
}
34-
35-
// Calculates the sign of the determinant of the 3x3 model matrix based on a
36-
// mesh flag
37-
fn sign_determinant_model_3x3() -> f32 {
38-
// bool(u32) is false if 0u else true
39-
// f32(bool) is 1.0 if true else 0.0
40-
// * 2.0 - 1.0 remaps 0.0 or 1.0 to -1.0 or 1.0 respectively
41-
return f32(bool(mesh.flags & MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT)) * 2.0 - 1.0;
20+
return mat3x3<f32>(
21+
mesh.inverse_transpose_model[0].xyz,
22+
mesh.inverse_transpose_model[1].xyz,
23+
mesh.inverse_transpose_model[2].xyz
24+
) * vertex_normal;
4225
}
4326

4427
fn mesh_tangent_local_to_world(model: mat4x4<f32>, vertex_tangent: vec4<f32>) -> vec4<f32> {
45-
// NOTE: The mikktspace method of normal mapping requires that the world tangent is
46-
// re-normalized in the vertex shader to match the way mikktspace bakes vertex tangents
47-
// and normal maps so that the exact inverse process is applied when shading. Blender, Unity,
48-
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
49-
// unless you really know what you are doing.
50-
// http://www.mikktspace.com/
5128
return vec4<f32>(
52-
normalize(
53-
mat3x3<f32>(
54-
model[0].xyz,
55-
model[1].xyz,
56-
model[2].xyz
57-
) * vertex_tangent.xyz
58-
),
59-
// NOTE: Multiplying by the sign of the determinant of the 3x3 model matrix accounts for
60-
// situations such as negative scaling.
61-
vertex_tangent.w * sign_determinant_model_3x3()
29+
mat3x3<f32>(
30+
model[0].xyz,
31+
model[1].xyz,
32+
model[2].xyz
33+
) * vertex_tangent.xyz,
34+
vertex_tangent.w
6235
);
6336
}

crates/bevy_pbr/src/render/mesh_types.wgsl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@ struct SkinnedMesh {
1515
#endif
1616

1717
const MESH_FLAGS_SHADOW_RECEIVER_BIT: u32 = 1u;
18-
// 2^31 - if the flag is set, the sign is positive, else it is negative
19-
const MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT: u32 = 2147483648u;

crates/bevy_pbr/src/render/pbr_functions.wgsl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,7 @@ fn apply_normal_mapping(
5555
uv: vec2<f32>,
5656
#endif
5757
) -> vec3<f32> {
58-
// NOTE: The mikktspace method of normal mapping explicitly requires that the world normal NOT
59-
// be re-normalized in the fragment shader. This is primarily to match the way mikktspace
60-
// bakes vertex tangents and normal maps so that this is the exact inverse. Blender, Unity,
61-
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
62-
// unless you really know what you are doing.
63-
// http://www.mikktspace.com/
64-
var N: vec3<f32> = world_normal;
58+
var N: vec3<f32> = normalize(world_normal);
6559

6660
#ifdef VERTEX_TANGENTS
6761
#ifdef STANDARDMATERIAL_NORMAL_MAP

0 commit comments

Comments
 (0)