Skip to content

Commit 40df90c

Browse files
authored
Make bevy_mikktspace optional (#21390)
# Objective - allow users to cut unneeded deps ## Solution - feature gate ## Testing - clearcoat example uses generated tangents, still works.
1 parent 6b34012 commit 40df90c

File tree

7 files changed

+33
-24
lines changed

7 files changed

+33
-24
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ default = [
149149
"bevy_scene",
150150
"bevy_image",
151151
"bevy_mesh",
152+
"bevy_mikktspace",
152153
"bevy_camera",
153154
"bevy_light",
154155
"bevy_shader",
@@ -272,6 +273,9 @@ bevy_image = ["bevy_internal/bevy_image"]
272273
# Provides a mesh format and some primitive meshing routines.
273274
bevy_mesh = ["bevy_internal/bevy_mesh"]
274275

276+
# Provides vertex tangent generation for use with bevy_mesh.
277+
bevy_mikktspace = ["bevy_internal/bevy_mikktspace"]
278+
275279
# Provides camera and visibility types, as well as culling primitives.
276280
bevy_camera = ["bevy_internal/bevy_camera"]
277281

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ bevy_text = [
222222
bevy_ui = ["dep:bevy_ui", "bevy_text", "bevy_sprite"]
223223
bevy_mesh = ["dep:bevy_mesh", "bevy_image"]
224224
bevy_animation = ["dep:bevy_animation", "bevy_mesh"]
225+
bevy_mikktspace = ["bevy_mesh?/bevy_mikktspace"]
225226
bevy_window = ["dep:bevy_window", "dep:bevy_a11y", "bevy_image"]
226227
bevy_winit = ["dep:bevy_winit", "bevy_window"]
227228
bevy_camera = ["dep:bevy_camera", "bevy_mesh", "bevy_window"]

crates/bevy_mesh/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ bevy_math = { path = "../bevy_math", version = "0.18.0-dev" }
1717
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev" }
1818
bevy_ecs = { path = "../bevy_ecs", version = "0.18.0-dev" }
1919
bevy_transform = { path = "../bevy_transform", version = "0.18.0-dev" }
20-
bevy_mikktspace = { version = "0.17.0-dev" }
20+
bevy_mikktspace = { version = "0.17.0-dev", optional = true }
2121
bevy_derive = { path = "../bevy_derive", version = "0.18.0-dev" }
2222
bevy_platform = { path = "../bevy_platform", version = "0.18.0-dev", default-features = false, features = [
2323
"std",

crates/bevy_mesh/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod components;
77
mod conversions;
88
mod index;
99
mod mesh;
10+
#[cfg(feature = "bevy_mikktspace")]
1011
mod mikktspace;
1112
#[cfg(feature = "morph")]
1213
pub mod morph;
@@ -20,6 +21,7 @@ use bitflags::bitflags;
2021
pub use components::*;
2122
pub use index::*;
2223
pub use mesh::*;
24+
#[cfg(feature = "bevy_mikktspace")]
2325
pub use mikktspace::*;
2426
pub use primitives::*;
2527
pub use vertex::*;

crates/bevy_mesh/src/mesh.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use bevy_transform::components::Transform;
22
pub use wgpu_types::PrimitiveTopology;
33

44
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,
99
};
1010
#[cfg(feature = "serialize")]
1111
use crate::SerializedMeshAttributeData;
@@ -942,8 +942,9 @@ impl Mesh {
942942
///
943943
/// Sets the [`Mesh::ATTRIBUTE_TANGENT`] attribute if successful.
944944
/// 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)?;
947948
self.insert_attribute(Mesh::ATTRIBUTE_TANGENT, tangents);
948949
Ok(())
949950
}
@@ -955,7 +956,8 @@ impl Mesh {
955956
/// (Alternatively, you can use [`Mesh::generate_tangents`] to mutate an existing mesh in-place)
956957
///
957958
/// 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> {
959961
self.generate_tangents()?;
960962
Ok(self)
961963
}
@@ -1414,6 +1416,21 @@ impl Mesh {
14141416
}
14151417
}
14161418

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+
14171434
impl core::ops::Mul<Mesh> for Transform {
14181435
type Output = Mesh;
14191436

crates/bevy_mesh/src/mikktspace.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use super::{Indices, Mesh, VertexAttributeValues};
2-
use bevy_math::Vec3;
32
use thiserror::Error;
43
use wgpu_types::{PrimitiveTopology, VertexFormat};
54

@@ -127,18 +126,3 @@ pub(crate) fn generate_tangents_for_mesh(
127126

128127
Ok(mikktspace_mesh.tangents)
129128
}
130-
131-
/// Correctly scales and renormalizes an already normalized `normal` by the scale determined by its reciprocal `scale_recip`
132-
pub(crate) fn scale_normal(normal: Vec3, scale_recip: Vec3) -> Vec3 {
133-
// This is basically just `normal * scale_recip` but with the added rule that `0. * anything == 0.`
134-
// This is necessary because components of `scale_recip` may be infinities, which do not multiply to zero
135-
let n = Vec3::select(normal.cmpeq(Vec3::ZERO), Vec3::ZERO, normal * scale_recip);
136-
137-
// If n is finite, no component of `scale_recip` was infinite or the normal was perpendicular to the scale
138-
// else the scale had at least one zero-component and the normal needs to point along the direction of that component
139-
if n.is_finite() {
140-
n.normalize_or_zero()
141-
} else {
142-
Vec3::select(n.abs().cmpeq(Vec3::INFINITY), n.signum(), Vec3::ZERO).normalize()
143-
}
144-
}

docs/cargo_features.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The default feature set enables most of the expected features of a game engine,
2929
|bevy_log|Enable integration with `tracing` and `log`|
3030
|bevy_mesh|Provides a mesh format and some primitive meshing routines.|
3131
|bevy_mesh_picking_backend|Provides an implementation for picking meshes|
32+
|bevy_mikktspace|Provides vertex tangent generation for use with bevy_mesh.|
3233
|bevy_pbr|Adds PBR rendering|
3334
|bevy_picking|Provides picking functionality|
3435
|bevy_post_process|Provides post process effects such as depth of field, bloom, chromatic aberration.|

0 commit comments

Comments
 (0)