@@ -2,10 +2,10 @@ use bevy_transform::components::Transform;
2
2
pub use wgpu_types:: PrimitiveTopology ;
3
3
4
4
use super :: {
5
- generate_tangents_for_mesh , scale_normal , triangle_area_normal , triangle_normal , FourIterators ,
6
- GenerateTangentsError , Indices , MeshAttributeData , MeshTrianglesError , MeshVertexAttribute ,
7
- MeshVertexAttributeId , MeshVertexBufferLayout , MeshVertexBufferLayoutRef ,
8
- MeshVertexBufferLayouts , MeshWindingInvertError , VertexAttributeValues , VertexBufferLayout ,
5
+ triangle_area_normal , triangle_normal , FourIterators , Indices , MeshAttributeData ,
6
+ MeshTrianglesError , MeshVertexAttribute , MeshVertexAttributeId , MeshVertexBufferLayout ,
7
+ MeshVertexBufferLayoutRef , MeshVertexBufferLayouts , MeshWindingInvertError ,
8
+ VertexAttributeValues , VertexBufferLayout ,
9
9
} ;
10
10
#[ cfg( feature = "serialize" ) ]
11
11
use crate :: SerializedMeshAttributeData ;
@@ -942,8 +942,9 @@ impl Mesh {
942
942
///
943
943
/// Sets the [`Mesh::ATTRIBUTE_TANGENT`] attribute if successful.
944
944
/// Requires a [`PrimitiveTopology::TriangleList`] topology and the [`Mesh::ATTRIBUTE_POSITION`], [`Mesh::ATTRIBUTE_NORMAL`] and [`Mesh::ATTRIBUTE_UV_0`] attributes set.
945
- pub fn generate_tangents ( & mut self ) -> Result < ( ) , GenerateTangentsError > {
946
- let tangents = generate_tangents_for_mesh ( self ) ?;
945
+ #[ cfg( feature = "bevy_mikktspace" ) ]
946
+ pub fn generate_tangents ( & mut self ) -> Result < ( ) , super :: GenerateTangentsError > {
947
+ let tangents = super :: generate_tangents_for_mesh ( self ) ?;
947
948
self . insert_attribute ( Mesh :: ATTRIBUTE_TANGENT , tangents) ;
948
949
Ok ( ( ) )
949
950
}
@@ -955,7 +956,8 @@ impl Mesh {
955
956
/// (Alternatively, you can use [`Mesh::generate_tangents`] to mutate an existing mesh in-place)
956
957
///
957
958
/// Requires a [`PrimitiveTopology::TriangleList`] topology and the [`Mesh::ATTRIBUTE_POSITION`], [`Mesh::ATTRIBUTE_NORMAL`] and [`Mesh::ATTRIBUTE_UV_0`] attributes set.
958
- pub fn with_generated_tangents ( mut self ) -> Result < Mesh , GenerateTangentsError > {
959
+ #[ cfg( feature = "bevy_mikktspace" ) ]
960
+ pub fn with_generated_tangents ( mut self ) -> Result < Mesh , super :: GenerateTangentsError > {
959
961
self . generate_tangents ( ) ?;
960
962
Ok ( self )
961
963
}
@@ -1414,6 +1416,21 @@ impl Mesh {
1414
1416
}
1415
1417
}
1416
1418
1419
+ /// Correctly scales and renormalizes an already normalized `normal` by the scale determined by its reciprocal `scale_recip`
1420
+ pub ( crate ) fn scale_normal ( normal : Vec3 , scale_recip : Vec3 ) -> Vec3 {
1421
+ // This is basically just `normal * scale_recip` but with the added rule that `0. * anything == 0.`
1422
+ // This is necessary because components of `scale_recip` may be infinities, which do not multiply to zero
1423
+ let n = Vec3 :: select ( normal. cmpeq ( Vec3 :: ZERO ) , Vec3 :: ZERO , normal * scale_recip) ;
1424
+
1425
+ // If n is finite, no component of `scale_recip` was infinite or the normal was perpendicular to the scale
1426
+ // else the scale had at least one zero-component and the normal needs to point along the direction of that component
1427
+ if n. is_finite ( ) {
1428
+ n. normalize_or_zero ( )
1429
+ } else {
1430
+ Vec3 :: select ( n. abs ( ) . cmpeq ( Vec3 :: INFINITY ) , n. signum ( ) , Vec3 :: ZERO ) . normalize ( )
1431
+ }
1432
+ }
1433
+
1417
1434
impl core:: ops:: Mul < Mesh > for Transform {
1418
1435
type Output = Mesh ;
1419
1436
0 commit comments