ClipReader.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #region License
  2. // Copyright 2011-2016 Kastellanos Nikolaos
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Microsoft.Xna.Framework.Content;
  18. namespace nkast.Aether.Animation.Content
  19. {
  20. public class ClipReader : ContentTypeReader<Clip>
  21. {
  22. protected override Clip Read(ContentReader input, Clip existingInstance)
  23. {
  24. Clip animationClip = existingInstance;
  25. if (existingInstance == null)
  26. {
  27. TimeSpan duration = ReadDuration(input);
  28. Keyframe[] keyframes = ReadKeyframes(input, null);
  29. animationClip = new Clip(duration, keyframes);
  30. }
  31. else
  32. {
  33. animationClip.Duration = ReadDuration(input);
  34. ReadKeyframes(input, animationClip.Keyframes);
  35. }
  36. return animationClip;
  37. }
  38. private TimeSpan ReadDuration(ContentReader input)
  39. {
  40. return new TimeSpan(input.ReadInt64());
  41. }
  42. private Keyframe[] ReadKeyframes(ContentReader input, Keyframe[] existingInstance)
  43. {
  44. Keyframe[] keyframes = existingInstance;
  45. int count = input.ReadInt32();
  46. if (keyframes == null)
  47. keyframes = new Keyframe[count];
  48. for (int i = 0; i < count; i++)
  49. {
  50. keyframes[i]._bone = input.ReadInt32();
  51. keyframes[i]._time = new TimeSpan(input.ReadInt64());
  52. keyframes[i]._transform.M11 = input.ReadSingle();
  53. keyframes[i]._transform.M12 = input.ReadSingle();
  54. keyframes[i]._transform.M13 = input.ReadSingle();
  55. keyframes[i]._transform.M14 = 0;
  56. keyframes[i]._transform.M21 = input.ReadSingle();
  57. keyframes[i]._transform.M22 = input.ReadSingle();
  58. keyframes[i]._transform.M23 = input.ReadSingle();
  59. keyframes[i]._transform.M24 = 0;
  60. keyframes[i]._transform.M31 = input.ReadSingle();
  61. keyframes[i]._transform.M32 = input.ReadSingle();
  62. keyframes[i]._transform.M33 = input.ReadSingle();
  63. keyframes[i]._transform.M34 = 0;
  64. keyframes[i]._transform.M41 = input.ReadSingle();
  65. keyframes[i]._transform.M42 = input.ReadSingle();
  66. keyframes[i]._transform.M43 = input.ReadSingle();
  67. keyframes[i]._transform.M44 = 1;
  68. }
  69. return keyframes;
  70. }
  71. }
  72. }