pub fn compute_tangents(&mut self) -> Result<()> {
        if self.normals.is_none() || self.uvs.is_none() {
            Err(Error::FailedComputingTangents)?;
        }
        let mut tan1 = vec![Vec3::new(0.0, 0.0, 0.0); self.positions.len()];
        let mut tan2 = vec![Vec3::new(0.0, 0.0, 0.0); self.positions.len()];

        self.for_each_triangle(|i0, i1, i2| {
            let (a, b, c) = match self.positions {
                Positions::F32(ref positions) => (positions[i0], positions[i1], positions[i2]),
                Positions::F64(ref positions) => {
                    let (a, b, c) = (positions[i0], positions[i1], positions[i2]);
                    (
                        Vec3::new(a.x as f32, a.y as f32, a.z as f32),
                        Vec3::new(b.x as f32, b.y as f32, b.z as f32),
                        Vec3::new(c.x as f32, c.y as f32, c.z as f32),
                    )
                }
            };
            let uva = self.uvs.as_ref().unwrap()[i0];
            let uvb = self.uvs.as_ref().unwrap()[i1];
            let uvc = self.uvs.as_ref().unwrap()[i2];

            let ba = b - a;
            let ca = c - a;

            let uvba = uvb - uva;
            let uvca = uvc - uva;

            let d = uvba.x * uvca.y - uvca.x * uvba.y;
            if d.abs() > 0.00001 {
                let r = 1.0 / d;
                let sdir = (ba * uvca.y - ca * uvba.y) * r;
                let tdir = (ca * uvba.x - ba * uvca.x) * r;
                tan1[i0] += sdir;
                tan1[i1] += sdir;
                tan1[i2] += sdir;
                tan2[i0] += tdir;
                tan2[i1] += tdir;
                tan2[i2] += tdir;
            }
        });

        let mut tangents = vec![Vec4::new(0.0, 0.0, 0.0, 0.0); self.positions.len()];
        self.for_each_vertex(|index| {
            let normal = self.normals.as_ref().unwrap()[index];
            let t = tan1[index];
            let tangent = (t - normal * normal.dot(t)).normalize();
            let handedness = if normal.cross(tangent).dot(tan2[index]) < 0.0 {
                1.0
            } else {
                -1.0
            };
            tangents[index] = tangent.extend(handedness);
        });

        self.tangents = Some(tangents);
        Ok(())
    }
///
/// Computes the per vertex tangents and updates the tangents of the mesh.
/// It will override the current tangents if they already exist.
///
Computes the per vertex tangents and updates the tangents of the mesh.
It will override the current tangents if they already exist.
Computes the per vertex tangents and updates the tangents of the mesh . It will override the current tangents if they already exist .
pub fn compute_tangents(&mut self) -> Result<()> {
        if self.normals.is_none() || self.uvs.is_none() {
            Err(Error::FailedComputingTangents)?;
        }
        let mut tan1 = vec![Vec3::new(0.0, 0.0, 0.0); self.positions.len()];
        let mut tan2 = vec![Vec3::new(0.0, 0.0, 0.0); self.positions.len()];
        self.for_each_triangle(|i0, i1, i2| {
            let (a, b, c) = match self.positions {
                Positions::F32(ref positions) => (positions[i0], positions[i1], positions[i2]),
                Positions::F64(ref positions) => {
                    let (a, b, c) = (positions[i0], positions[i1], positions[i2]);
                    (
                        Vec3::new(a.x as f32, a.y as f32, a.z as f32),
                        Vec3::new(b.x as f32, b.y as f32, b.z as f32),
                        Vec3::new(c.x as f32, c.y as f32, c.z as f32),
                    )
                }
            };
            let uva = self.uvs.as_ref().unwrap()[i0];
            let uvb = self.uvs.as_ref().unwrap()[i1];
            let uvc = self.uvs.as_ref().unwrap()[i2];
            let ba = b - a;
            let ca = c - a;
            let uvba = uvb - uva;
            let uvca = uvc - uva;
            let d = uvba.x * uvca.y - uvca.x * uvba.y;
            if d.abs() > 0.00001 {
                let r = 1.0 / d;
                let sdir = (ba * uvca.y - ca * uvba.y) * r;
                let tdir = (ca * uvba.x - ba * uvca.x) * r;
                tan1[i0] += sdir;
                tan1[i1] += sdir;
                tan1[i2] += sdir;
                tan2[i0] += tdir;
                tan2[i1] += tdir;
                tan2[i2] += tdir;
            }
        });
        let mut tangents = vec![Vec4::new(0.0, 0.0, 0.0, 0.0); self.positions.len()];
        self.for_each_vertex(|index| {
            let normal = self.normals.as_ref().unwrap()[index];
            let t = tan1[index];
            let tangent = (t - normal * normal.dot(t)).normalize();
            let handedness = if normal.cross(tangent).dot(tan2[index]) < 0.0 {
                1.0
            } else {
                -1.0
            };
            tangents[index] = tangent.extend(handedness);
        });
        self.tangents = Some(tangents);
        Ok(())
    }
pub fn compute_tangents ( & mut self ) -> Result < ( ) > { if self . normals . is_none ( ) || self . uvs . is_none ( ) { Err ( Error :: FailedComputingTangents ) ? ; } let mut tan1 = vec ! [ Vec3 new ( 0.0 0.0 0.0 ) self positions len ( ) ] ; let mut tan2 = vec ! [ Vec3 new ( 0.0 0.0 0.0 ) self positions len ( ) ] ; self . for_each_triangle ( | i0 , i1 , i2 | { let ( a , b , c ) = match self . positions { Positions :: F32 ( ref positions ) => ( positions [ i0 ] , positions [ i1 ] , positions [ i2 ] ) , Positions :: F64 ( ref positions ) => { let ( a , b , c ) = ( positions [ i0 ] , positions [ i1 ] , positions [ i2 ] ) ; ( Vec3 :: new ( a . x as f32 , a . y as f32 , a . z as f32 ) , Vec3 :: new ( b . x as f32 , b . y as f32 , b . z as f32 ) , Vec3 :: new ( c . x as f32 , c . y as f32 , c . z as f32 ) , ) } } ; let uva = self . uvs . as_ref ( ) . unwrap ( ) [ i0 ] ; let uvb = self . uvs . as_ref ( ) . unwrap ( ) [ i1 ] ; let uvc = self . uvs . as_ref ( ) . unwrap ( ) [ i2 ] ; let ba = b - a ; let ca = c - a ; let uvba = uvb - uva ; let uvca = uvc - uva ; let d = uvba . x * uvca . y - uvca . x * uvba . y ; if d . abs ( ) > 0.00001 { let r = 1.0 / d ; let sdir = ( ba * uvca . y - ca * uvba . y ) * r ; let tdir = ( ca * uvba . x - ba * uvca . x ) * r ; tan1 [ i0 ] += sdir ; tan1 [ i1 ] += sdir ; tan1 [ i2 ] += sdir ; tan2 [ i0 ] += tdir ; tan2 [ i1 ] += tdir ; tan2 [ i2 ] += tdir ; } } ) ; let mut tangents = vec ! [ Vec4 new ( 0.0 0.0 0.0 0.0 ) self positions len ( ) ] ; self . for_each_vertex ( | index | { let normal = self . normals . as_ref ( ) . unwrap ( ) [ index ] ; let t = tan1 [ index ] ; let tangent = ( t - normal * normal . dot ( t ) ) . normalize ( ) ; let handedness = if normal . cross ( tangent ) . dot ( tan2 [ index ] ) < 0.0 { 1.0 } else { - 1.0 } ; tangents [ index ] = tangent . extend ( handedness ) ; } ) ; self . tangents = Some ( tangents ) ; Ok ( ( ) ) }
Computes the per vertex tangents and updates the tangents of the mesh.
Computes the per vertex tangents and updates the tangents of the mesh .
&mut self
&mut self