@@ -406,12 +406,9 @@ class Avatar {
406406 armature . updateMatrix ( ) ;
407407
408408 Head . traverse ( o => {
409- o . savedPosition = new THREE . Vector3 ( ) ;
410- o . savedMatrixWorld = new THREE . Matrix4 ( ) ;
409+ o . savedPosition = o . position . clone ( ) ;
410+ o . savedMatrixWorld = o . matrixWorld . clone ( ) ;
411411 } ) ;
412- if ( options . decapitate ) {
413- this . decapitate ( ) ;
414- }
415412
416413 const hairBones = tailBones . filter ( bone => / h a i r / i. test ( bone . name ) ) . map ( bone => {
417414 for ( ; bone ; bone = bone . parent ) {
@@ -734,6 +731,11 @@ class Avatar {
734731
735732 this . shoulderTransforms . Start ( ) ;
736733 this . legsManager . Start ( ) ;
734+
735+ this . decapitated = false ;
736+ if ( options . decapitate ) {
737+ this . decapitate ( ) ;
738+ }
737739 }
738740 update ( ) {
739741// return;
@@ -791,15 +793,17 @@ class Avatar {
791793 }
792794
793795 if ( this . options . hair ) {
796+ const _getMatrixWorld = o => this . decapitated ? o . savedMatrixWorld : o . matrixWorld ;
797+
794798 const hipsRotation = this . modelBones . Hips . quaternion ;
795- const scale = localVector . setFromMatrixScale ( this . modelBones . Head . matrixWorld ) ; ;
799+ const scale = localVector . setFromMatrixScale ( _getMatrixWorld ( this . modelBones . Head ) ) ;
796800 const _processHairBone = ( hairBone , children ) => {
797- const p = localVector2 . setFromMatrixPosition ( hairBone . matrixWorld ) ;
801+ const p = localVector2 . setFromMatrixPosition ( _getMatrixWorld ( hairBone ) ) ;
798802
799803 for ( let i = 0 ; i < children . length ; i ++ ) {
800804 const childHairBone = children [ i ] ;
801805
802- const px = localVector3 . setFromMatrixPosition ( childHairBone . matrixWorld ) ;
806+ const px = localVector3 . setFromMatrixPosition ( _getMatrixWorld ( childHairBone ) ) ;
803807 const hairDistance = px . distanceTo ( p ) ;
804808 const hairDirection = localVector4 . copy ( px ) . sub ( p ) . normalize ( ) ;
805809
@@ -828,7 +832,7 @@ class Avatar {
828832 ) ) ,
829833 childHairBone . initialWorldQuaternion
830834 ) ;
831- childHairBone . matrixWorld . compose ( p2 , q2 , scale ) ;
835+ _getMatrixWorld ( childHairBone ) . compose ( p2 , q2 , scale ) ;
832836 }
833837 for ( let i = 0 ; i < children . length ; i ++ ) {
834838 const childHairBone = children [ i ] ;
@@ -912,18 +916,24 @@ class Avatar {
912916 }
913917
914918 decapitate ( ) {
915- rig . modelBones . Head . traverse ( o => {
916- o . savedPosition . copy ( o . position ) ;
917- o . savedMatrixWorld . copy ( o . matrixWorld ) ;
918- o . position . set ( NaN , NaN , NaN ) ;
919- o . matrixWorld . set ( NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN ) ;
920- } ) ;
919+ if ( ! this . decapitated ) {
920+ this . modelBones . Head . traverse ( o => {
921+ o . savedPosition . copy ( o . position ) ;
922+ o . savedMatrixWorld . copy ( o . matrixWorld ) ;
923+ o . position . set ( NaN , NaN , NaN ) ;
924+ o . matrixWorld . set ( NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN , NaN ) ;
925+ } ) ;
926+ this . decapitated = true ;
927+ }
921928 }
922929 undecapitate ( ) {
923- rig . modelBones . Head . traverse ( o => {
924- o . position . copy ( o . savedPosition ) ;
925- o . matrixWorld . copy ( o . savedMatrixWorld ) ;
926- } ) ;
930+ if ( this . decapitated ) {
931+ this . modelBones . Head . traverse ( o => {
932+ o . position . copy ( o . savedPosition ) ;
933+ o . matrixWorld . copy ( o . savedMatrixWorld ) ;
934+ } ) ;
935+ this . decapitated = false ;
936+ }
927937 }
928938}
929939export default Avatar ;
0 commit comments