Permalink
Browse files

Refactoring for HeadIK

- Add properties.
- Replace quaternion functions to optimized.
- Remove debug codes.

Remove compiler warnings.
  • Loading branch information...
1 parent e5c83bd commit 9c2a0f6bbac1c428b57213c5a133c72394377b57 Nora committed Mar 23, 2016
@@ -199,8 +199,8 @@ void _OnInspectorGUI_Basic()
EditorUtil.GUI.Field( "Automatic Prepare Humanoid", ref settings.automaticPrepareHumanoid );
EditorUtil.GUI.Field( "Automatic Configure Spine Enabled", ref settings.automaticConfigureSpineEnabled );
- EditorUtil.GUI.Field( "Automatic Configure Roll Enabled", ref settings.automaticConfigureRollEnabled );
- EditorUtil.GUI.Field( "Roll Enabled", ref settings.rollEnabled );
+ EditorUtil.GUI.Field( "Automatic Configure Roll Bones Enabled", ref settings.automaticConfigureRollBonesEnabled );
+ EditorUtil.GUI.Field( "Roll Bones Enabled", ref settings.rollBonesEnabled );
_Header( "BodyIK" );
@@ -266,7 +266,7 @@ void _OnInspectorGUI_Basic()
EditorUtil.GUI.Slider( "Upper Eyes Limit Yaw", ref settings.bodyIK.upperEyesLimitYaw, 0.0f, 89.99f );
EditorUtil.GUI.Slider( "Upper Eyes Limit Pitch Up", ref settings.bodyIK.upperEyesLimitPitchUp, 0.0f, 89.99f );
EditorUtil.GUI.Slider( "Upper Eyes Limit Pitch Down", ref settings.bodyIK.upperEyesLimitPitchDown, 0.0f, 89.99f );
- EditorUtil.GUI.Slider( "Upper Eyes Range Angle", ref settings.bodyIK.upperEyesRangeAngle, 90.0f, 180.0f );
+ EditorUtil.GUI.Slider( "Upper Eyes Trace Angle", ref settings.bodyIK.upperEyesTraceAngle, 90.0f, 180.0f );
_Header( "LimbIK" );
@@ -336,7 +336,7 @@ void _OnInspectorGUI_Basic()
EditorUtil.GUI.Slider( "Head Limit Roll", ref settings.headIK.headLimitRoll, 0.0f, 89.99f );
EditorUtil.GUI.Slider( "Eyes To Head Yaw Rate", ref settings.headIK.eyesToHeadYawRate, 0.0f, 1.0f );
EditorUtil.GUI.Slider( "Eyes To Head Pitch Rate", ref settings.headIK.eyesToHeadPitchRate, 0.0f, 1.0f );
- EditorUtil.GUI.Slider( "Eyes Range Angle", ref settings.headIK.eyesRangeAngle, 90.0f, 180.0f );
+ EditorUtil.GUI.Slider( "Eyes Trace Angle", ref settings.headIK.eyesTraceAngle, 90.0f, 180.0f );
}
#if SAFULLBODYIK_DEBUG
@@ -13,13 +13,23 @@ public class FullBodyIKUnityChan : FullBodyIK
Vector3 _headBoneLossyScale = Vector3.one;
bool _isHeadBoneLossyScaleFuzzyIdentity = true;
- public static Vector3 _unityChan_leftEyeDefaultLocalPosition = new Vector3( -0.042531f + 0.024f, 0.048524f, 0.047682f - 0.02f );
- public static Vector3 _unityChan_rightEyeDefaultLocalPosition = new Vector3( 0.042531f - 0.024f, 0.048524f, 0.047682f - 0.02f );
+ Quaternion _headToLeftEyeRotation = Quaternion.identity;
+ Quaternion _headToRightEyeRotation = Quaternion.identity;
+
Vector3 _unityChan_leftEyeDefaultPosition = Vector3.zero;
Vector3 _unityChan_rightEyeDefaultPosition = Vector3.zero;
- Quaternion _headToLeftEyeRotation = Quaternion.identity;
- Quaternion _headToRightEyeRotation = Quaternion.identity;
+ static Vector3 _unityChan_leftEyeDefaultLocalPosition = new Vector3( -0.042531f + 0.024f, 0.048524f, 0.047682f - 0.02f );
+ static Vector3 _unityChan_rightEyeDefaultLocalPosition = new Vector3( 0.042531f - 0.024f, 0.048524f, 0.047682f - 0.02f );
+
+ static readonly float _unityChan_eyesHorzLimitTheta = Mathf.Sin( 40.0f * Mathf.Deg2Rad );
+ static readonly float _unityChan_eyesVertLimitTheta = Mathf.Sin( 4.5f * Mathf.Deg2Rad );
+ const float _unityChan_eyesYawRate = 0.796f;
+ const float _unityChan_eyesPitchRate = 0.28f;
+ const float _unityChan_eyesYawOuterRate = 0.096f;
+ const float _unityChan_eyesYawInnerRate = 0.065f;
+ const float _unityChan_eyesMoveXInnerRate = 0.063f * 0.1f;
+ const float _unityChan_eyesMoveXOuterRate = 0.063f * 0.1f;
public override bool _IsHiddenCustomEyes()
{
@@ -80,14 +90,17 @@ public override void _ResetCustomEyes()
SAFBIKMatSetRotMultInv1( out headBasis, ref headWorldRotation, ref headBone._defaultRotation );
Vector3 worldPotision;
+ Quaternion worldRotation;
SAFBIKMatMultVecPreSubAdd( out worldPotision, ref headBasis, ref leftEyeBone._defaultPosition, ref headBone._defaultPosition, ref headWorldPosition );
leftEyeBone.worldPosition = worldPotision;
- leftEyeBone.worldRotation = headWorldRotation * _headToLeftEyeRotation;
+ SAFBIKQuatMult( out worldRotation, ref headWorldRotation, ref _headToLeftEyeRotation );
+ leftEyeBone.worldRotation = worldRotation;
SAFBIKMatMultVecPreSubAdd( out worldPotision, ref headBasis, ref rightEyeBone._defaultPosition, ref headBone._defaultPosition, ref headWorldPosition );
rightEyeBone.worldPosition = worldPotision;
- rightEyeBone.worldRotation = headWorldRotation * _headToRightEyeRotation;
+ SAFBIKQuatMult( out worldRotation, ref headWorldRotation, ref _headToRightEyeRotation );
+ rightEyeBone.worldRotation = worldRotation;
}
}
@@ -105,56 +118,6 @@ public override void _SolveCustomEyes( ref Matrix3x3 neckBasis, ref Matrix3x3 he
rightEyeBone != null && rightEyeBone.transformIsAlive &&
eyesEffector != null ) {
- Vector3 leftEyePosition = new Vector3( -0.042531f, 0.048524f, 0.047682f );
- Vector3 rightEyePosition = new Vector3( 0.042531f, 0.048524f, 0.047682f );
-
- float _eyesHorzLimitAngle = 40.0f;
- float _eyesVertLimitAngle = 4.5f;
- float _eyesXRate = 0.796f;
- float _eyesYRate = 0.28f;
- float _eyesOuterXRotRate = 0.096f;
- float _eyesInnerXRotRate = 0.065f;
- float _eyesXOffset = -0.024f;
- float _eyesYOffset = 0.0f;
- float _eyesZOffset = -0.02f;
-
- internalValues.UpdateDebugValue( "_eyesHorzLimitAngle", ref _eyesHorzLimitAngle );
- internalValues.UpdateDebugValue( "_eyesVertLimitAngle", ref _eyesVertLimitAngle );
- internalValues.UpdateDebugValue( "_eyesXRate", ref _eyesXRate );
- internalValues.UpdateDebugValue( "_eyesYRate", ref _eyesYRate );
- internalValues.UpdateDebugValue( "_eyesOuterXRotRate", ref _eyesOuterXRotRate );
- internalValues.UpdateDebugValue( "_eyesInnerXRotRate", ref _eyesInnerXRotRate );
- internalValues.UpdateDebugValue( "_eyesXOffset", ref _eyesXOffset );
- internalValues.UpdateDebugValue( "_eyesYOffset", ref _eyesYOffset );
- internalValues.UpdateDebugValue( "_eyesZOffset", ref _eyesZOffset );
-
- float _innerMoveXRate = 0.063f;
- float _outerMoveXRate = 0.063f;
-
- internalValues.UpdateDebugValue( "_innerMoveXRate", ref _innerMoveXRate );
- internalValues.UpdateDebugValue( "_outerMoveXRate", ref _outerMoveXRate );
-
- _innerMoveXRate *= 0.1f;
- _outerMoveXRate *= 0.1f;
-
- _eyesHorzLimitAngle *= Mathf.Deg2Rad;
- _eyesVertLimitAngle *= Mathf.Deg2Rad;
-
- float _eyesHorzLimit = Mathf.Sin( _eyesHorzLimitAngle );
- float _eyesVertLimit = Mathf.Sin( _eyesVertLimitAngle );
-
- leftEyePosition.x -= _eyesXOffset;
- rightEyePosition.x += _eyesXOffset;
- leftEyePosition.y += _eyesYOffset;
- rightEyePosition.y += _eyesYOffset;
- leftEyePosition.z += _eyesZOffset;
- rightEyePosition.z += _eyesZOffset;
-
- Vector3 leftEyeDefaultPosition = _unityChan_leftEyeDefaultPosition;
- Vector3 rightEyeDefaultPosition = _unityChan_rightEyeDefaultPosition;
- leftEyePosition = _unityChan_leftEyeDefaultPosition;
- rightEyePosition = _unityChan_rightEyeDefaultPosition;
-
Vector3 headWorldPosition, neckBoneWorldPosition = neckBone.worldPosition;
SAFBIKMatMultVecPreSubAdd( out headWorldPosition, ref neckBasis, ref headBone._defaultPosition, ref neckBone._defaultPosition, ref neckBoneWorldPosition );
Vector3 eyesPosition;
@@ -167,7 +130,9 @@ public override void _SolveCustomEyes( ref Matrix3x3 neckBasis, ref Matrix3x3 he
SAFBIKMatMultVecInv( out eyesDir, ref headBaseBasis, ref eyesDir );
- SAFBIKVecNormalize( ref eyesDir );
+ if( !SAFBIKVecNormalize( ref eyesDir ) ) {
+ eyesDir = new Vector3( 0.0f, 0.0f, 1.0f );
+ }
if( eyesEffector.positionWeight < 1.0f - IKEpsilon ) {
Vector3 tempDir = Vector3.Lerp( new Vector3( 0.0f, 0.0f, 1.0f ), eyesDir, eyesEffector.positionWeight );
@@ -177,55 +142,44 @@ public override void _SolveCustomEyes( ref Matrix3x3 neckBasis, ref Matrix3x3 he
}
_LimitXY_Square( ref eyesDir,
- Mathf.Sin( _eyesHorzLimitAngle ),
- Mathf.Sin( _eyesHorzLimitAngle ),
- Mathf.Sin( _eyesVertLimitAngle ),
- Mathf.Sin( _eyesVertLimitAngle ) );
-
- float moveX = Mathf.Clamp( eyesDir.x * _eyesXRate, -_eyesHorzLimit, _eyesHorzLimit );
- float moveY = Mathf.Clamp( eyesDir.y * _eyesYRate, -_eyesVertLimit, _eyesVertLimit );
- float moveZ = -Mathf.Max( 1.0f - eyesDir.z, 1.0f - _eyesVertLimit ); // Reuse _eyesVertLimit.
+ _unityChan_eyesHorzLimitTheta,
+ _unityChan_eyesHorzLimitTheta,
+ _unityChan_eyesVertLimitTheta,
+ _unityChan_eyesVertLimitTheta );
+
+ float moveX = eyesDir.x * _unityChan_eyesYawRate;
+ if( moveX < -_unityChan_eyesHorzLimitTheta ) {
+ moveX = -_unityChan_eyesHorzLimitTheta;
+ } else if( moveX > _unityChan_eyesHorzLimitTheta ) {
+ moveX = _unityChan_eyesHorzLimitTheta;
+ }
- eyesDir.x *= _eyesXRate;
- eyesDir.y *= _eyesYRate;
+ eyesDir.x *= _unityChan_eyesYawRate;
+ eyesDir.y *= _unityChan_eyesPitchRate;
Vector3 leftEyeDir = eyesDir;
Vector3 rightEyeDir = eyesDir;
if( eyesDir.x >= 0.0f ) {
- leftEyeDir.x *= _eyesInnerXRotRate;
- rightEyeDir.x *= _eyesOuterXRotRate;
+ leftEyeDir.x *= _unityChan_eyesYawInnerRate;
+ rightEyeDir.x *= _unityChan_eyesYawOuterRate;
} else {
- leftEyeDir.x *= _eyesOuterXRotRate;
- rightEyeDir.x *= _eyesInnerXRotRate;
+ leftEyeDir.x *= _unityChan_eyesYawOuterRate;
+ rightEyeDir.x *= _unityChan_eyesYawInnerRate;
}
SAFBIKVecNormalize2( ref leftEyeDir, ref rightEyeDir );
SAFBIKMatMultVec( out leftEyeDir, ref headBaseBasis, ref leftEyeDir );
SAFBIKMatMultVec( out rightEyeDir, ref headBaseBasis, ref rightEyeDir );
- float leftXRate = (moveX >= 0.0f) ? _innerMoveXRate : _outerMoveXRate;
- float rightXRate = (moveX >= 0.0f) ? _outerMoveXRate : _innerMoveXRate;
-
- {
- Vector3 xDir = headBasis.column0;
- Vector3 yDir = headBasis.column1;
- Vector3 zDir = leftEyeDir;
- SAFBIKComputeBasisLockZ( out leftEyeBaseBasis, ref xDir, ref yDir, ref zDir );
- }
-
- {
- Vector3 xDir = headBasis.column0;
- Vector3 yDir = headBasis.column1;
- Vector3 zDir = rightEyeDir;
- SAFBIKComputeBasisLockZ( out rightEyeBaseBasis, ref xDir, ref yDir, ref zDir );
- }
+ float leftXRate = (moveX >= 0.0f) ? _unityChan_eyesMoveXInnerRate : _unityChan_eyesMoveXOuterRate;
+ float rightXRate = (moveX >= 0.0f) ? _unityChan_eyesMoveXOuterRate : _unityChan_eyesMoveXInnerRate;
- Vector3 leftEyeWorldPosition;
- Vector3 rightEyeWorldPosition;
+ SAFBIKComputeBasisLockZ( out leftEyeBaseBasis, ref headBasis.column0, ref headBasis.column1, ref leftEyeDir );
+ SAFBIKComputeBasisLockZ( out rightEyeBaseBasis, ref headBasis.column0, ref headBasis.column1, ref rightEyeDir );
- leftEyeWorldPosition = headBaseBasis.column0 * (leftXRate * moveX);
- rightEyeWorldPosition = headBaseBasis.column0 * (rightXRate * moveX);
+ Vector3 leftEyeWorldPosition = headBaseBasis.column0 * (leftXRate * moveX);
+ Vector3 rightEyeWorldPosition = headBaseBasis.column0 * (rightXRate * moveX);
if( !_isHeadBoneLossyScaleFuzzyIdentity ) {
leftEyeWorldPosition = Scale( ref leftEyeWorldPosition, ref _headBoneLossyScale );
@@ -245,12 +199,12 @@ public override void _SolveCustomEyes( ref Matrix3x3 neckBasis, ref Matrix3x3 he
Vector3 worldPosition;
Quaternion worldRotation;
- SAFBIKMatMultVecPreSubAdd( out worldPosition, ref leftEyeBasis, ref leftEyeBone._defaultPosition, ref leftEyeDefaultPosition, ref leftEyeWorldPosition );
+ SAFBIKMatMultVecPreSubAdd( out worldPosition, ref leftEyeBasis, ref leftEyeBone._defaultPosition, ref _unityChan_leftEyeDefaultPosition, ref leftEyeWorldPosition );
leftEyeBone.worldPosition = worldPosition;
SAFBIKMatMultGetRot( out worldRotation, ref leftEyeBaseBasis, ref leftEyeBone._baseToWorldBasis );
leftEyeBone.worldRotation = worldRotation;
- SAFBIKMatMultVecPreSubAdd( out worldPosition, ref rightEyeBasis, ref rightEyeBone._defaultPosition, ref rightEyeDefaultPosition, ref rightEyeWorldPosition );
+ SAFBIKMatMultVecPreSubAdd( out worldPosition, ref rightEyeBasis, ref rightEyeBone._defaultPosition, ref _unityChan_rightEyeDefaultPosition, ref rightEyeWorldPosition );
rightEyeBone.worldPosition = worldPosition;
SAFBIKMatMultGetRot( out worldRotation, ref rightEyeBaseBasis, ref rightEyeBone._baseToWorldBasis );
rightEyeBone.worldRotation = worldRotation;
View
@@ -145,8 +145,8 @@ public class Settings
public bool automaticPrepareHumanoid = true;
public bool automaticConfigureSpineEnabled = false;
- public bool automaticConfigureRollEnabled = false;
- public bool rollEnabled = false;
+ public bool automaticConfigureRollBonesEnabled = false;
+ public bool rollBonesEnabled = false;
public bool createEffectorTransform = true;
@@ -173,7 +173,7 @@ public class BodyIK
public float upperBodyMovingfixRate = 1.0f;
public float upperHeadMovingfixRate = 0.8f;
public float upperCenterLegTranslateRate = 0.5f;
- public float upperSpineTranslateRate = 0.6f;
+ public float upperSpineTranslateRate = 0.65f;
public float upperCenterLegRotateRate = 0.6f;
public float upperSpineRotateRate = 0.9f;
public float upperPostTranslateRate = 1.0f;
@@ -193,7 +193,7 @@ public class BodyIK
public bool spineLimitEnabled = true;
public bool spineAccurateLimitEnabled = false;
public float spineLimitAngleX = 40.0f;
- public float spineLimitAngleY = 20.0f;
+ public float spineLimitAngleY = 25.0f;
public float upperContinuousPreTranslateRate = 0.2f;
public float upperContinuousPreTranslateStableRate = 0.65f;
@@ -211,7 +211,7 @@ public class BodyIK
public float upperEyesLimitYaw = 80.0f;
public float upperEyesLimitPitchUp = 10.0f;
public float upperEyesLimitPitchDown = 45.0f;
- public float upperEyesRangeAngle = 160.0f;
+ public float upperEyesTraceAngle = 160.0f;
}
[System.Serializable]
@@ -286,7 +286,14 @@ public class HeadIK
public float eyesToHeadYawRate = 0.8f;
public float eyesToHeadPitchRate = 0.5f;
- public float eyesRangeAngle = 110.0f;
+ public float eyesTraceAngle = 110.0f;
+
+ public float eyesLimitYaw = 40.0f;
+ public float eyesLimitPitch = 12.0f;
+ public float eyesYawRate = 0.796f;
+ public float eyesPitchRate = 0.729f;
+ public float eyesYawOuterRate = 0.356f;
+ public float eyesYawInnerRate = 0.212f;
}
[System.Serializable]
@@ -412,7 +419,7 @@ public class BodyIK
public CachedDegreesToSin upperEyesLimitYaw = CachedDegreesToSin.zero;
public CachedDegreesToSin upperEyesLimitPitchUp = CachedDegreesToSin.zero;
public CachedDegreesToSin upperEyesLimitPitchDown = CachedDegreesToSin.zero;
- public CachedDegreesToCos upperEyesRangeTheta = CachedDegreesToCos.zero;
+ public CachedDegreesToCos upperEyesTraceTheta = CachedDegreesToCos.zero;
public CachedDegreesToSin upperDirXLimitThetaY = CachedDegreesToSin.zero;
@@ -465,8 +472,8 @@ public void Update( Settings.BodyIK settingsBodyIK )
if( upperEyesLimitPitchDown._degrees != settingsBodyIK.upperEyesLimitPitchDown ) {
upperEyesLimitPitchDown._Reset( settingsBodyIK.upperEyesLimitPitchDown );
}
- if( upperEyesRangeTheta._degrees != settingsBodyIK.upperEyesRangeAngle ) {
- upperEyesRangeTheta._Reset( settingsBodyIK.upperEyesRangeAngle );
+ if( upperEyesTraceTheta._degrees != settingsBodyIK.upperEyesTraceAngle ) {
+ upperEyesTraceTheta._Reset( settingsBodyIK.upperEyesTraceAngle );
}
if( spineLimitAngleX._a != settingsBodyIK.spineLimitAngleX ) {
@@ -552,7 +559,10 @@ public class HeadIK
public CachedDegreesToSin headLimitPitchDownTheta = CachedDegreesToSin.zero;
public CachedDegreesToSin headLimitRollTheta = CachedDegreesToSin.zero;
- public CachedDegreesToCos eyesRangeTheta = CachedDegreesToCos.zero;
+ public CachedDegreesToCos eyesTraceTheta = CachedDegreesToCos.zero;
+
+ public CachedDegreesToSin eyesLimitYawTheta = CachedDegreesToSin.zero;
+ public CachedDegreesToSin eyesLimitPitchTheta = CachedDegreesToSin.zero;
public void Update( Settings.HeadIK settingsHeadIK )
{
@@ -581,8 +591,15 @@ public void Update( Settings.HeadIK settingsHeadIK )
headLimitRollTheta._Reset( settingsHeadIK.headLimitRoll );
}
- if( eyesRangeTheta._degrees != settingsHeadIK.eyesRangeAngle ) {
- eyesRangeTheta._Reset( settingsHeadIK.eyesRangeAngle );
+ if( eyesTraceTheta._degrees != settingsHeadIK.eyesTraceAngle ) {
+ eyesTraceTheta._Reset( settingsHeadIK.eyesTraceAngle );
+ }
+
+ if( eyesLimitYawTheta._degrees != settingsHeadIK.eyesLimitYaw ) {
+ eyesLimitYawTheta._Reset( settingsHeadIK.eyesLimitYaw );
+ }
+ if( eyesLimitPitchTheta._degrees != settingsHeadIK.eyesLimitPitch ) {
+ eyesLimitPitchTheta._Reset( settingsHeadIK.eyesLimitPitch );
}
}
}
@@ -1139,7 +1156,7 @@ public void ConfigureBoneTransforms()
}
}
- if( settings.automaticConfigureRollEnabled ) {
+ if( settings.automaticConfigureRollBonesEnabled ) {
var tempBones = new List<Transform>();
for( int side = 0; side != 2; ++side ) {
Oops, something went wrong.

0 comments on commit 9c2a0f6

Please sign in to comment.