Advertisement
Guest User

Untitled

a guest
Apr 29th, 2015
429
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 KB | None | 0 0
  1.  
  2.         int findPos(double time, NodeAnimationChannel nodeAnim)
  3.         {
  4.             for (int i = 0; i < nodeAnim.PositionKeyCount - 1; i++)
  5.             {
  6.                 if (time >= nodeAnim.PositionKeys[i].Time && time < nodeAnim.PositionKeys[i + 1].Time)
  7.                 {
  8.                     return i;
  9.                 }
  10.             }
  11.             return 0;
  12.         }
  13.  
  14.         Vector3D lerpPos(double aTime, NodeAnimationChannel nodeAnim)
  15.         {
  16.             if (nodeAnim.PositionKeyCount == 0)
  17.             {
  18.                 return new Vector3D(0, 0, 0);
  19.             }
  20.             if (nodeAnim.PositionKeyCount == 1)
  21.             {
  22.                 return nodeAnim.PositionKeys[0].Value;
  23.             }
  24.             int index = findPos(aTime, nodeAnim);
  25.             int nextIndex = index + 1;
  26.             if (nextIndex >= nodeAnim.PositionKeyCount)
  27.             {
  28.                 return nodeAnim.PositionKeys[0].Value;
  29.             }
  30.             double deltaT = nodeAnim.PositionKeys[nextIndex].Time - nodeAnim.PositionKeys[index].Time;
  31.             double factor = (aTime - nodeAnim.PositionKeys[index].Time) / deltaT;
  32.             if (factor < 0 || factor > 1)
  33.             {
  34.                 return nodeAnim.PositionKeys[0].Value;
  35.             }
  36.             Vector3D start = nodeAnim.PositionKeys[index].Value;
  37.             Vector3D end = nodeAnim.PositionKeys[nextIndex].Value;
  38.             Vector3D deltaV = end - start;
  39.             return start + (float)factor * deltaV;
  40.         }
  41.  
  42.         int findRotate(double time, NodeAnimationChannel nodeAnim)
  43.         {
  44.             for (int i = 0; i < nodeAnim.RotationKeyCount - 1; i++)
  45.             {
  46.                 if (time >= nodeAnim.RotationKeys[i].Time && time < nodeAnim.RotationKeys[i + 1].Time)
  47.                 {
  48.                     return i;
  49.                 }
  50.             }
  51.             return 0;
  52.         }
  53.  
  54.         Assimp.Quaternion lerpRotate(double aTime, NodeAnimationChannel nodeAnim)
  55.         {
  56.             if (nodeAnim.RotationKeyCount == 0)
  57.             {
  58.                 return new Assimp.Quaternion();
  59.             }
  60.             if (nodeAnim.RotationKeyCount == 1)
  61.             {
  62.                 return nodeAnim.RotationKeys[0].Value;
  63.             }
  64.             int index = findRotate(aTime, nodeAnim);
  65.             int nextIndex = index + 1;
  66.             if (nextIndex >= nodeAnim.ScalingKeyCount)
  67.             {
  68.                 return nodeAnim.RotationKeys[0].Value;
  69.             }
  70.             double deltaT = nodeAnim.RotationKeys[nextIndex].Time - nodeAnim.RotationKeys[index].Time;
  71.             double factor = (aTime - nodeAnim.RotationKeys[index].Time) / deltaT;
  72.             if (factor < 0 || factor > 1)
  73.             {
  74.                 return nodeAnim.RotationKeys[0].Value;
  75.             }
  76.             Assimp.Quaternion start = nodeAnim.RotationKeys[index].Value;
  77.             Assimp.Quaternion end = nodeAnim.RotationKeys[nextIndex].Value;
  78.             Assimp.Quaternion res = Assimp.Quaternion.Slerp(start, end, (float)factor);
  79.             res.Normalize();
  80.             return res;
  81.         }
  82.  
  83.         int findScale(double time, NodeAnimationChannel nodeAnim)
  84.         {
  85.             for (int i = 0; i < nodeAnim.ScalingKeyCount - 1; i++)
  86.             {
  87.                 if (time >= nodeAnim.RotationKeys[i].Time && time < nodeAnim.ScalingKeys[i + 1].Time)
  88.                 {
  89.                     return i;
  90.                 }
  91.             }
  92.             return 0;
  93.         }
  94.  
  95.         Vector3D lerpScale(double aTime, NodeAnimationChannel nodeAnim)
  96.         {
  97.             if (nodeAnim.ScalingKeyCount == 0)
  98.             {
  99.                 return new Vector3D(1, 1, 1);
  100.             }
  101.             if (nodeAnim.ScalingKeyCount == 1)
  102.             {
  103.                 return nodeAnim.ScalingKeys[0].Value;
  104.             }
  105.             int index = findScale(aTime, nodeAnim);
  106.             int nextIndex = index + 1;
  107.             if (nextIndex >= nodeAnim.ScalingKeyCount)
  108.             {
  109.                 return nodeAnim.ScalingKeys[0].Value;
  110.             }
  111.             double deltaT = nodeAnim.ScalingKeys[nextIndex].Time - nodeAnim.ScalingKeys[index].Time;
  112.             double factor = (aTime - nodeAnim.ScalingKeys[index].Time) / deltaT;
  113.             if (factor < 0 || factor > 1)
  114.             {
  115.                 return nodeAnim.ScalingKeys[0].Value;
  116.             }
  117.             Vector3D start = nodeAnim.ScalingKeys[index].Value;
  118.             Vector3D end = nodeAnim.ScalingKeys[nextIndex].Value;
  119.             Vector3D deltaV = end - start;
  120.             Vector3D scale = start + (float)factor * deltaV;
  121.             return scale;
  122.         }
  123.  
  124.         Matrix4 globalInverse = Matrix4.Identity;
  125.  
  126.         public void UpdateTransforms(double aTime, Node pNode, Matrix4 transf)
  127.         {
  128.             try
  129.             {
  130.                 string nodename = pNode.Name;
  131.                 if (OriginalModel.AnimationCount == 0)
  132.                 {
  133.                     return;
  134.                 }
  135.                 Animation pAnim = OriginalModel.Animations[0];
  136.                 Matrix4 nodeTransf = convert(pNode.Transform);
  137.                 NodeAnimationChannel pNodeAnim = FindNodeAnim(pAnim, nodename);
  138.                 if (pNodeAnim != null)
  139.                 {
  140.                     Vector3D scaling = lerpScale(aTime, pNodeAnim);
  141.                     Matrix4 scale = Matrix4.CreateScale(scaling.X, scaling.Y, scaling.Z);
  142.                     Assimp.Quaternion rot = lerpRotate(aTime, pNodeAnim);
  143.                     Matrix4 rotation = convert(new Matrix4x4(rot.GetMatrix()));
  144.                     Vector3D pos = lerpPos(aTime, pNodeAnim);
  145.                     Matrix4 translation = Matrix4.CreateTranslation(pos.X, pos.Y, pos.Z);
  146.                     nodeTransf = translation * rotation * scale;
  147.                 }
  148.                 Matrix4 global = transf * nodeTransf;
  149.                 foreach (ModelMesh mesh in Meshes)
  150.                 {
  151.                     int pos;
  152.                     if (mesh.BoneLookup.TryGetValue(nodename, out pos))
  153.                     {
  154.                         mesh.Bones[pos].Transform = globalInverse * global * convert(mesh.Bones[pos].Internal.OffsetMatrix);
  155.                     }
  156.                 }
  157.                 for (int i = 0; i < pNode.ChildCount; i++)
  158.                 {
  159.                     UpdateTransforms(aTime, pNode.Children[i], global);
  160.                 }
  161.             }
  162.             catch (Exception ex)
  163.             {
  164.                 SysConsole.Output(OutputType.ERROR, ex.ToString());
  165.             }
  166.         }
  167.  
  168.         NodeAnimationChannel FindNodeAnim(Animation pAnim, string nodeName)
  169.         {
  170.             for (int i = 0; i < pAnim.NodeAnimationChannelCount; i++)
  171.             {
  172.                 NodeAnimationChannel nac = pAnim.NodeAnimationChannels[i];
  173.                 if (nac.NodeName == nodeName)
  174.                 {
  175.                     return nac;
  176.                 }
  177.             }
  178.             return null;
  179.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement