Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int findPos(double time, NodeAnimationChannel nodeAnim)
- {
- for (int i = 0; i < nodeAnim.PositionKeyCount - 1; i++)
- {
- if (time >= nodeAnim.PositionKeys[i].Time && time < nodeAnim.PositionKeys[i + 1].Time)
- {
- return i;
- }
- }
- return 0;
- }
- Vector3D lerpPos(double aTime, NodeAnimationChannel nodeAnim)
- {
- if (nodeAnim.PositionKeyCount == 0)
- {
- return new Vector3D(0, 0, 0);
- }
- if (nodeAnim.PositionKeyCount == 1)
- {
- return nodeAnim.PositionKeys[0].Value;
- }
- int index = findPos(aTime, nodeAnim);
- int nextIndex = index + 1;
- if (nextIndex >= nodeAnim.PositionKeyCount)
- {
- return nodeAnim.PositionKeys[0].Value;
- }
- double deltaT = nodeAnim.PositionKeys[nextIndex].Time - nodeAnim.PositionKeys[index].Time;
- double factor = (aTime - nodeAnim.PositionKeys[index].Time) / deltaT;
- if (factor < 0 || factor > 1)
- {
- return nodeAnim.PositionKeys[0].Value;
- }
- Vector3D start = nodeAnim.PositionKeys[index].Value;
- Vector3D end = nodeAnim.PositionKeys[nextIndex].Value;
- Vector3D deltaV = end - start;
- return start + (float)factor * deltaV;
- }
- int findRotate(double time, NodeAnimationChannel nodeAnim)
- {
- for (int i = 0; i < nodeAnim.RotationKeyCount - 1; i++)
- {
- if (time >= nodeAnim.RotationKeys[i].Time && time < nodeAnim.RotationKeys[i + 1].Time)
- {
- return i;
- }
- }
- return 0;
- }
- Assimp.Quaternion lerpRotate(double aTime, NodeAnimationChannel nodeAnim)
- {
- if (nodeAnim.RotationKeyCount == 0)
- {
- return new Assimp.Quaternion();
- }
- if (nodeAnim.RotationKeyCount == 1)
- {
- return nodeAnim.RotationKeys[0].Value;
- }
- int index = findRotate(aTime, nodeAnim);
- int nextIndex = index + 1;
- if (nextIndex >= nodeAnim.ScalingKeyCount)
- {
- return nodeAnim.RotationKeys[0].Value;
- }
- double deltaT = nodeAnim.RotationKeys[nextIndex].Time - nodeAnim.RotationKeys[index].Time;
- double factor = (aTime - nodeAnim.RotationKeys[index].Time) / deltaT;
- if (factor < 0 || factor > 1)
- {
- return nodeAnim.RotationKeys[0].Value;
- }
- Assimp.Quaternion start = nodeAnim.RotationKeys[index].Value;
- Assimp.Quaternion end = nodeAnim.RotationKeys[nextIndex].Value;
- Assimp.Quaternion res = Assimp.Quaternion.Slerp(start, end, (float)factor);
- res.Normalize();
- return res;
- }
- int findScale(double time, NodeAnimationChannel nodeAnim)
- {
- for (int i = 0; i < nodeAnim.ScalingKeyCount - 1; i++)
- {
- if (time >= nodeAnim.RotationKeys[i].Time && time < nodeAnim.ScalingKeys[i + 1].Time)
- {
- return i;
- }
- }
- return 0;
- }
- Vector3D lerpScale(double aTime, NodeAnimationChannel nodeAnim)
- {
- if (nodeAnim.ScalingKeyCount == 0)
- {
- return new Vector3D(1, 1, 1);
- }
- if (nodeAnim.ScalingKeyCount == 1)
- {
- return nodeAnim.ScalingKeys[0].Value;
- }
- int index = findScale(aTime, nodeAnim);
- int nextIndex = index + 1;
- if (nextIndex >= nodeAnim.ScalingKeyCount)
- {
- return nodeAnim.ScalingKeys[0].Value;
- }
- double deltaT = nodeAnim.ScalingKeys[nextIndex].Time - nodeAnim.ScalingKeys[index].Time;
- double factor = (aTime - nodeAnim.ScalingKeys[index].Time) / deltaT;
- if (factor < 0 || factor > 1)
- {
- return nodeAnim.ScalingKeys[0].Value;
- }
- Vector3D start = nodeAnim.ScalingKeys[index].Value;
- Vector3D end = nodeAnim.ScalingKeys[nextIndex].Value;
- Vector3D deltaV = end - start;
- Vector3D scale = start + (float)factor * deltaV;
- return scale;
- }
- Matrix4 globalInverse = Matrix4.Identity;
- public void UpdateTransforms(double aTime, Node pNode, Matrix4 transf)
- {
- try
- {
- string nodename = pNode.Name;
- if (OriginalModel.AnimationCount == 0)
- {
- return;
- }
- Animation pAnim = OriginalModel.Animations[0];
- Matrix4 nodeTransf = convert(pNode.Transform);
- NodeAnimationChannel pNodeAnim = FindNodeAnim(pAnim, nodename);
- if (pNodeAnim != null)
- {
- Vector3D scaling = lerpScale(aTime, pNodeAnim);
- Matrix4 scale = Matrix4.CreateScale(scaling.X, scaling.Y, scaling.Z);
- Assimp.Quaternion rot = lerpRotate(aTime, pNodeAnim);
- Matrix4 rotation = convert(new Matrix4x4(rot.GetMatrix()));
- Vector3D pos = lerpPos(aTime, pNodeAnim);
- Matrix4 translation = Matrix4.CreateTranslation(pos.X, pos.Y, pos.Z);
- nodeTransf = translation * rotation * scale;
- }
- Matrix4 global = transf * nodeTransf;
- foreach (ModelMesh mesh in Meshes)
- {
- int pos;
- if (mesh.BoneLookup.TryGetValue(nodename, out pos))
- {
- mesh.Bones[pos].Transform = globalInverse * global * convert(mesh.Bones[pos].Internal.OffsetMatrix);
- }
- }
- for (int i = 0; i < pNode.ChildCount; i++)
- {
- UpdateTransforms(aTime, pNode.Children[i], global);
- }
- }
- catch (Exception ex)
- {
- SysConsole.Output(OutputType.ERROR, ex.ToString());
- }
- }
- NodeAnimationChannel FindNodeAnim(Animation pAnim, string nodeName)
- {
- for (int i = 0; i < pAnim.NodeAnimationChannelCount; i++)
- {
- NodeAnimationChannel nac = pAnim.NodeAnimationChannels[i];
- if (nac.NodeName == nodeName)
- {
- return nac;
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement