/// Calculate normals for the given buffers representing a mesh, assuming smooth shading and per-vertex normals.
///
/// If zeroed is true, the normals will not be zeroed before hand. If this is falsely set, it is safe, just returns incorrect results.
///
/// # Safety
///
/// The following must be true:
/// - Normals and positions must be the same length.
/// - All indices must be in-bounds for the buffers.
Calculate normals for the given buffers representing a mesh, assuming smooth shading and per-vertex normals.
If zeroed is true, the normals will not be zeroed before hand. If this is falsely set, it is safe, just returns incorrect results.
Calculate normals for the given buffers representing a mesh assuming smooth shading and per - vertex normals . If zeroed is true the normals will not be zeroed before hand . If this is falsely set it is safe just returns incorrect results .
pub unsafe fn calculate_normals_for_buffers(
        normals: &mut [Vec3],
        positions: &[Vec3],
        indices: &[u32],
        zeroed: bool,
    ) {
        debug_assert_eq!(normals.len(), positions.len());
        if !zeroed {
            for norm in normals.iter_mut() {
                *norm = Vec3::ZERO;
            }
        }
        for idx in indices.chunks_exact(3) {
            let (idx0, idx1, idx2) = match *idx {
                [idx0, idx1, idx2] => (idx0, idx1, idx2),
                // SAFETY: This is guaranteed by chunks_exact(3)
                _ => std::hint::unreachable_unchecked(),
            };
            // SAFETY: The conditions of this function assert all thes indices are in-bounds
            let pos1 = *positions.get_unchecked(idx0 as usize);
            let pos2 = *positions.get_unchecked(idx1 as usize);
            let pos3 = *positions.get_unchecked(idx2 as usize);
            let edge1 = pos2 - pos1;
            let edge2 = pos3 - pos1;
            let normal = edge1.cross(edge2);
            // SAFETY: The conditions of this function assert all thes indices are in-bounds
            *normals.get_unchecked_mut(idx0 as usize) += normal;
            *normals.get_unchecked_mut(idx1 as usize) += normal;
            *normals.get_unchecked_mut(idx2 as usize) += normal;
        }
        for normal in normals.iter_mut() {
            *normal = normal.normalize();
        }
    }
pub unsafe fn calculate_normals_for_buffers ( normals : & mut [ Vec3 ] , positions : & [ Vec3 ] , indices : & [ u32 ] , zeroed : bool , ) { debug_assert_eq ! ( normals len ( ) positions len ( ) ) ; if ! zeroed { for norm in normals . iter_mut ( ) { * norm = Vec3 :: ZERO ; } } for idx in indices . chunks_exact ( 3 ) { let ( idx0 , idx1 , idx2 ) = match * idx { [ idx0 , idx1 , idx2 ] => ( idx0 , idx1 , idx2 ) , // SAFETY: This is guaranteed by chunks_exact(3) _ => std :: hint :: unreachable_unchecked ( ) , } ; // SAFETY: The conditions of this function assert all thes indices are in-bounds let pos1 = * positions . get_unchecked ( idx0 as usize ) ; let pos2 = * positions . get_unchecked ( idx1 as usize ) ; let pos3 = * positions . get_unchecked ( idx2 as usize ) ; let edge1 = pos2 - pos1 ; let edge2 = pos3 - pos1 ; let normal = edge1 . cross ( edge2 ) ; // SAFETY: The conditions of this function assert all thes indices are in-bounds * normals . get_unchecked_mut ( idx0 as usize ) += normal ; * normals . get_unchecked_mut ( idx1 as usize ) += normal ; * normals . get_unchecked_mut ( idx2 as usize ) += normal ; } for normal in normals . iter_mut ( ) { * normal = normal . normalize ( ) ; } }
Calculate normals for the given buffers representing a mesh, assuming smooth shading and per-vertex normals.
Calculate normals for the given buffers representing a mesh assuming smooth shading and per - vertex normals .
normals
&mut [Vec3]
positions
&[Vec3]
indices
&[u32]
zeroed
bool
safety
The following must be true:
Normals and positions must be the same length.
All indices must be in-bounds for the buffers.
The following must be true : Normals and positions must be the same length . All indices must be in - bounds for the buffers .