AnimationClip WriteCurveToClip(AnimationClip aC,int boneIndex)
{
AnimationClip result = aC;
AnimationCurve aCurve;
Keyframe[] keysX = new Keyframe[animClip.totalSamples],keysY = new Keyframe[animClip.totalSamples],keysZ = new Keyframe[animClip.totalSamples];
nextSampleTime = 0f;
bool rootConstraint = false;
#if UNITY_EDITOR
if(boneIndex ==0 && useRootConstraints)
rootConstraint = true;
//write position of this bone for all samples
//===================================================================================
if(bakingProperties.y || rootConstraint){
for(int i=0;i<keysX.Length;i++){
keysX[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].pos.x);
keysY[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].pos.y);
keysZ[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].pos.z);
nextSampleTime+=sampleStep;
}
if(rootConstraint && rootPositionConstraint.x ==true)
keysX = new Keyframe[0];
if(rootConstraint && rootPositionConstraint.y ==true)
keysY = new Keyframe[0];
if(rootConstraint && rootPositionConstraint.z ==true)
keysZ = new Keyframe[0];
if(keysX.Length>0){
aCurve = new AnimationCurve(keysX);
//result.SetCurve(skeletonInfo.bonesPaths[boneIndex],typeof(Transform),"localPosition.x",aCurve);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalPosition.x"),aCurve);
}
if(keysY.Length>0){
aCurve = new AnimationCurve(keysY);
//result.SetCurve(skeletonInfo.bonesPaths[boneIndex],typeof(Transform),"localPosition.y",aCurve);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalPosition.y"),aCurve);
}
if(keysZ.Length>0){
aCurve = new AnimationCurve(keysZ);
//result.SetCurve(skeletonInfo.bonesPaths[boneIndex],typeof(Transform),"localPosition.z",aCurve);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalPosition.z"),aCurve);
}
}
//=====================================================================================
//write rotation of this bone for all samples
if(bakingProperties.x || rootConstraint){
keysX = new Keyframe[animClip.totalSamples];
keysY = new Keyframe[animClip.totalSamples];
keysZ = new Keyframe[animClip.totalSamples];
//Keyframe[] keysW = new Keyframe[animClip.totalSamples];
nextSampleTime = 0f;
Vector3 testAxis = Vector3.zero;
float angle = 0f;
for(int i=0;i<keysX.Length;i++){
tempRot.Set (animClip.bonesStates[boneIndex].samples[i].rot.x,animClip.bonesStates[boneIndex].samples[i].rot.y,
animClip.bonesStates[boneIndex].samples[i].rot.z,animClip.bonesStates[boneIndex].samples[i].rot.w);
/*
tempRot.ToAngleAxis(out angle,out testAxis);
if(angle>180f){
angle = (360f-angle)*-1f;
tempRot = Quaternion.AngleAxis(angle,testAxis);
}
*/
testAxis = tempRot.eulerAngles;
if(testAxis.x>180f){
testAxis.x = (360f-testAxis.x)*-1;
}
if(testAxis.y>180f){
testAxis.y = (360f-testAxis.y)*-1;
}
if(testAxis.z>180f){
testAxis.z = (360f-testAxis.z)*-1;
}
keysX[i] = new Keyframe(nextSampleTime,testAxis.x);
keysY[i] = new Keyframe(nextSampleTime,testAxis.y);
keysZ[i] = new Keyframe(nextSampleTime,testAxis.z);
nextSampleTime+=sampleStep;
}
if(rootConstraint && rootRotationConstraint.x == true){
keysX = new Keyframe[0];
}
if(keysX.Length>0){
aCurve = new AnimationCurve(keysX);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"localEulerAnglesBaked.x"),aCurve);
}
if(rootConstraint && rootRotationConstraint.y != true){
keysY = new Keyframe[0];
}
if(keysY.Length>0){
aCurve = new AnimationCurve(keysY);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"localEulerAnglesBaked.y"),aCurve);
}
if(rootConstraint && rootRotationConstraint.z == true){
keysZ = new Keyframe[0];
}
if(keysZ.Length>0){
aCurve = new AnimationCurve(keysZ);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"localEulerAnglesBaked.z"),aCurve);
}
}
//======================================================================================
//=====================================================================================
//write scale of this bone for all samples
if(bakingProperties.z || rootConstraint){
keysX = new Keyframe[animClip.totalSamples];
keysY = new Keyframe[animClip.totalSamples];
keysZ = new Keyframe[animClip.totalSamples];
nextSampleTime = 0f;
for(int i=0;i<keysX.Length;i++){
keysX[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].scale.x);
keysY[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].scale.y);
keysZ[i] = new Keyframe(nextSampleTime,animClip.bonesStates[boneIndex].samples[i].scale.z);
nextSampleTime+=sampleStep;
}
if(!rootConstraint ||(rootConstraint && rootScaleConstraint.x ==false)){
aCurve = new AnimationCurve(keysX);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalScale.x"),aCurve);
}
if(!rootConstraint ||(rootConstraint && rootScaleConstraint.y ==false)){
aCurve = new AnimationCurve(keysY);
//result.SetCurve(skeletonInfo.bonesPaths[boneIndex],typeof(Transform),"localScale.y",aCurve);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalScale.y"),aCurve);
}
if(!rootConstraint ||(rootConstraint && rootScaleConstraint.z ==false)){
aCurve = new AnimationCurve(keysZ);
//result.SetCurve(skeletonInfo.bonesPaths[boneIndex],typeof(Transform),"localScale.z",aCurve);
UnityEditor.AnimationUtility.SetEditorCurve(result,UnityEditor.EditorCurveBinding.FloatCurve(skeletonInfo.bonesPaths[boneIndex],
typeof(Transform),"m_LocalScale.z"),aCurve);
}
}
//======================================================================================
#endif
return result;
}