欢迎来到天天文库
浏览记录
ID:11444363
大小:87.00 KB
页数:12页
时间:2018-07-12
《xna动画的实现7优化动画的播放》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、XNA动画的实现7——优化动画的播放优化1:使用四元数表示旋转前面的文章中我们使用欧拉角表示旋转,需要三个浮点数、分别表示绕x、y、z轴的旋转或Yaw、Pitch、Roll,本文我们使用四元数表示旋转,四元数需要4个浮点数,虽然比欧拉角多使用一个浮点数导致耗费的内存增加,但四元数可以平滑插值,避免使用欧拉角带来的抖动、万向节锁的缺陷。四元数背后的数学比较复杂,大致可以理解成一个旋转轴加一个旋转量,其中前三个分量表示旋转轴,第四个分量表示绕这个轴的旋转值。例如绕y轴旋转90度,用欧拉角表示为(0,Math
2、Helper.ToRadians(90),0),与它等价的四元数为(sinθ/2nx,sinθ/2ny,sinθ/2nzcosθ/2),其中n为旋转轴向量,θ为旋转角度,即(0,0.707,0,0.707),在XNA框架已经内置了创建四元数的方法,我们使用Quaternion.CreateFromYawPitchRoll(MathHelper.ToRadians(90),0,0)就可以生成这个值,使用Quaternion.CreateFromAxisAngle(),Quaternion.CreateFr
3、omRotationMatrix()方法亦可。关键帧类Keyframe也要做相应的调整,将:publicVector3Rotation{get;privateset;}修改为:publicQuaternionRotation{get;internalset;}还有一个变化是将private变为internal,这是为了优化2中的使用,使它可以在类库内部可写。动画播放类AnimationPlayerBase中的插值算法也需做相应调整,将:rotation=Vector3.Lerp(keyframes[Cu
4、rrentKeyframeIndex].Rotation,keyframes[CurrentKeyframeIndex+1].Rotation,amt);transform=Matrix.CreateScale(scale)*Matrix.CreateFromYawPitchRoll(rotation.Y,rotation.X,rotation.Z)*Matrix.CreateTranslation(position);修改为:rotation=Quaternion.Slerp(keyframes[Cu
5、rrentKeyframeIndex].Rotation,keyframes[CurrentKeyframeIndex+1].Rotation,amt);transform=Matrix.CreateScale(scale)*Matrix.CreateFromQuaternion(rotation)*Matrix.CreateTranslation(position);使用了Quaternion的Slerp方法替代了Vector3的Lerp方法。优化2:在手动定义的动画数据上附加模型原始变换矩阵12/1
6、2在上一篇文章中我们已经提到,因为模型的原始变换矩阵是不变的,我们完全可以事先将它附加到关键帧中,这样在更新动画数据时就可以减少计算量。我们在类库AnimationClassLibrary中添加一个辅助类AnimationHelper,代码如下://////动画处理的辅助类。///publicclassAnimationHelper{//////将模型的初始变换矩阵附加到每个关键帧上。//////7、ationData">要处理的动画数据,类型为AnimationData///模型的初始变换矩阵publicstaticvoidAddModelTransform(AnimationDataanimationData,Matrix[]originalModelTransform){foreach(KeyValuePairanimationinanimatio8、nData.ModelAnimationClips){ProcessAnimationClip(animation.Value,originalModelTransform);}}privatestaticvoidProcessAnimationClip(AnimationClipanimationClip,Matrix[]originalModelTransform){//遍历动画片段的每个关键帧foreach(Keyframekeyfr
7、ationData">要处理的动画数据,类型为AnimationData///模型的初始变换矩阵publicstaticvoidAddModelTransform(AnimationDataanimationData,Matrix[]originalModelTransform){foreach(KeyValuePairanimationinanimatio
8、nData.ModelAnimationClips){ProcessAnimationClip(animation.Value,originalModelTransform);}}privatestaticvoidProcessAnimationClip(AnimationClipanimationClip,Matrix[]originalModelTransform){//遍历动画片段的每个关键帧foreach(Keyframekeyfr
此文档下载收益归作者所有