如何让3d model动起来,其实就是要给modelbone设置动作,这样整个model就会动起来了。

一.获取Bones

fbx文件中可以看到所有bones的名称,这样就可以通过名称来获取到bones

   turretBone = tankModel.Bones["turret_geo"];

   cannonBone = tankModel.Bones["canon_geo"];

   hatchBone = tankModel.Bones["hatch_geo"];

 

二.保存bones原始Transform

turretTransform = turretBone.Transform;

   cannonTransform = cannonBone.Transform;

   hatchTransform = hatchBone.Transform;

 

三.Bones设置动作

//设置动作的变化值

turretRotationValue = (float)Math.Sin(time * 0.333f) * 1.25f;

   cannonRotationValue = (float)Math.Sin(time * 0.25f) * 0.333f - 0.333f;

   hatchRotationValue = MathHelper.Clamp((float)Math.Sin(time * 2) * 2, -1, 0);

   //创建动作矩阵

   turretRotation = Matrix.CreateRotationY(turretRotationValue);

   cannonRotation = Matrix.CreateRotationX(cannonRotationValue);

   hatchRotation = Matrix.CreateRotationX(hatchRotationValue);

 

四.将设置的动作赋给bonesTransform

turretBone.Transform = turretRotation * turretTransform;

   cannonBone.Transform = cannonRotation * cannonTransform;

   hatchBone.Transform = hatchRotation * hatchTransform;

 

由以上的实现代码可以看出,如果想让3d model 动起来,只要给model所包含的bones赋予动作,以及动作的变化量,就可以了。

 

示例下载地址:http://www.52winphone.com/bbs/viewthread.php?tid=300&extra=page%3D1

posted on 2011-03-10 15:00  小镇  阅读(2206)  评论(0编辑  收藏  举报