AnimationsReader.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 System.Collections.Generic;
  18. using Microsoft.Xna.Framework;
  19. using Microsoft.Xna.Framework.Content;
  20. namespace nkast.Aether.Animation.Content
  21. {
  22. public class AnimationsReader : ContentTypeReader<Animations>
  23. {
  24. protected override Animations Read(ContentReader input, Animations existingInstance)
  25. {
  26. Animations animations = existingInstance;
  27. if (existingInstance == null)
  28. {
  29. Dictionary<string, Clip> clips = ReadAnimationClips(input, null);
  30. List<Matrix> bindPose = ReadBindPose(input, null);
  31. List<Matrix> invBindPose = ReadInvBindPose(input, null);
  32. List<int> skeletonHierarchy = ReadSkeletonHierarchy(input, null);
  33. Dictionary<string, int> boneMap = ReadBoneMap(input, null);
  34. animations = new Animations(bindPose, invBindPose, skeletonHierarchy, boneMap, clips);
  35. }
  36. else
  37. {
  38. ReadAnimationClips(input, animations.Clips);
  39. ReadBindPose(input, animations._bindPose);
  40. ReadInvBindPose(input, animations._invBindPose);
  41. ReadSkeletonHierarchy(input, animations._skeletonHierarchy);
  42. ReadBoneMap(input, animations._boneMap);
  43. }
  44. return animations;
  45. }
  46. private Dictionary<string, Clip> ReadAnimationClips(ContentReader input, Dictionary<string, Clip> existingInstance)
  47. {
  48. Dictionary<string, Clip> animationClips = existingInstance;
  49. int count = input.ReadInt32();
  50. if (animationClips == null)
  51. animationClips = new Dictionary<string, Clip>(count);
  52. for (int i = 0; i < count; i++)
  53. {
  54. string key = input.ReadString();
  55. Clip val = input.ReadObject<Clip>();
  56. if (existingInstance == null)
  57. animationClips.Add(key, val);
  58. else
  59. animationClips[key] = val;
  60. }
  61. return animationClips;
  62. }
  63. private List<Matrix> ReadBindPose(ContentReader input, List<Matrix> existingInstance)
  64. {
  65. List<Matrix> bindPose = existingInstance;
  66. int count = input.ReadInt32();
  67. if (bindPose == null)
  68. bindPose = new List<Matrix>(count);
  69. for (int i = 0; i < count; i++)
  70. {
  71. Matrix val = input.ReadMatrix();
  72. if (existingInstance == null)
  73. bindPose.Add(val);
  74. else
  75. bindPose[i] = val;
  76. }
  77. return bindPose;
  78. }
  79. private List<Matrix> ReadInvBindPose(ContentReader input, List<Matrix> existingInstance)
  80. {
  81. List<Matrix> invBindPose = existingInstance;
  82. int count = input.ReadInt32();
  83. if (invBindPose == null)
  84. invBindPose = new List<Matrix>(count);
  85. for (int i = 0; i < count; i++)
  86. {
  87. Matrix val = input.ReadMatrix();
  88. if (existingInstance == null)
  89. invBindPose.Add(val);
  90. else
  91. invBindPose[i] = val;
  92. }
  93. return invBindPose;
  94. }
  95. private List<int> ReadSkeletonHierarchy(ContentReader input, List<int> existingInstance)
  96. {
  97. List<int> skeletonHierarchy = existingInstance;
  98. int count = input.ReadInt32();
  99. if (skeletonHierarchy == null)
  100. skeletonHierarchy = new List<int>(count);
  101. for (int i = 0; i < count; i++)
  102. {
  103. Int32 val = input.ReadInt32();
  104. if (existingInstance == null)
  105. skeletonHierarchy.Add(val);
  106. else
  107. skeletonHierarchy[i] = val;
  108. }
  109. return skeletonHierarchy;
  110. }
  111. private Dictionary<string, int> ReadBoneMap(ContentReader input, Dictionary<string, int> existingInstance)
  112. {
  113. Dictionary<string, int> boneMap = existingInstance;
  114. int count = input.ReadInt32();
  115. if (boneMap == null)
  116. boneMap = new Dictionary<string, int>(count);
  117. for (int boneIndex = 0; boneIndex < count; boneIndex++)
  118. {
  119. string key = input.ReadString();
  120. if (existingInstance == null)
  121. boneMap.Add(key, boneIndex);
  122. else
  123. boneMap[key] = boneIndex;
  124. }
  125. return boneMap;
  126. }
  127. }
  128. }