bevy/crates/bevy_mikktspace/src/lib.rs
Rich Churcher fd329c0426
Allow to expect (adopted) (#15301)
# Objective

> Rust 1.81 released the #[expect(...)] attribute, which works like
#[allow(...)] but throws a warning if the lint isn't raised. This is
preferred to #[allow(...)] because it tells us when it can be removed.

- Adopts the parts of #15118 that are complete, and updates the branch
so it can be merged.
- There were a few conflicts, let me know if I misjudged any of 'em.

Alice's
[recommendation](https://github.com/bevyengine/bevy/issues/15059#issuecomment-2349263900)
seems well-taken, let's do this crate by crate now that @BD103 has done
the lion's share of this!

(Relates to, but doesn't yet completely finish #15059.)

Crates this _doesn't_ cover:

- bevy_input
- bevy_gilrs
- bevy_window
- bevy_winit
- bevy_state
- bevy_render
- bevy_picking
- bevy_core_pipeline
- bevy_sprite
- bevy_text
- bevy_pbr
- bevy_ui
- bevy_gltf
- bevy_gizmos
- bevy_dev_tools
- bevy_internal
- bevy_dylib

---------

Co-authored-by: BD103 <59022059+BD103@users.noreply.github.com>
Co-authored-by: Ben Frankel <ben.frankel7@gmail.com>
Co-authored-by: Antony <antony.m.3012@gmail.com>
2024-09-20 19:16:42 +00:00

98 lines
3.1 KiB
Rust

#![allow(
unsafe_op_in_unsafe_fn,
clippy::all,
clippy::undocumented_unsafe_blocks,
clippy::ptr_cast_constness,
// FIXME(15321): solve CI failures, then replace with `#![expect()]`.
missing_docs
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![doc(
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
use glam::{Vec2, Vec3};
mod generated;
/// The interface by which mikktspace interacts with your geometry.
pub trait Geometry {
/// Returns the number of faces.
fn num_faces(&self) -> usize;
/// Returns the number of vertices of a face.
fn num_vertices_of_face(&self, face: usize) -> usize;
/// Returns the position of a vertex.
fn position(&self, face: usize, vert: usize) -> [f32; 3];
/// Returns the normal of a vertex.
fn normal(&self, face: usize, vert: usize) -> [f32; 3];
/// Returns the texture coordinate of a vertex.
fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2];
/// Sets the generated tangent for a vertex.
/// Leave this function unimplemented if you are implementing
/// `set_tangent_encoded`.
fn set_tangent(
&mut self,
tangent: [f32; 3],
_bi_tangent: [f32; 3],
_f_mag_s: f32,
_f_mag_t: f32,
bi_tangent_preserves_orientation: bool,
face: usize,
vert: usize,
) {
let sign = if bi_tangent_preserves_orientation {
1.0
} else {
-1.0
};
self.set_tangent_encoded([tangent[0], tangent[1], tangent[2], sign], face, vert);
}
/// Sets the generated tangent for a vertex with its bi-tangent encoded as the 'W' (4th)
/// component in the tangent. The 'W' component marks if the bi-tangent is flipped. This
/// is called by the default implementation of `set_tangent`; therefore, this function will
/// not be called by the crate unless `set_tangent` is unimplemented.
fn set_tangent_encoded(&mut self, _tangent: [f32; 4], _face: usize, _vert: usize) {}
}
/// Generates tangents for the input geometry.
///
/// # Errors
///
/// Returns `false` if the geometry is unsuitable for tangent generation including,
/// but not limited to, lack of vertices.
#[allow(unsafe_code)]
pub fn generate_tangents<I: Geometry>(geometry: &mut I) -> bool {
unsafe { generated::genTangSpace(geometry, 180.0) }
}
fn get_position<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
geometry.position(face, vert).into()
}
fn get_tex_coord<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
let tex_coord: Vec2 = geometry.tex_coord(face, vert).into();
let val = tex_coord.extend(1.0);
val
}
fn get_normal<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
geometry.normal(face, vert).into()
}
fn index_to_face_vert(index: usize) -> (usize, usize) {
(index >> 2, index & 0x3)
}
fn face_vert_to_index(face: usize, vert: usize) -> usize {
face << 2 | vert & 0x3
}