CpuAnimatedModelProcessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.Collections.Generic;
  17. using System.ComponentModel;
  18. using Microsoft.Xna.Framework.Content.Pipeline;
  19. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  20. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using nkast.Aether.Content.Pipeline.Animation;
  23. using nkast.Aether.Content.Pipeline.Serialization;
  24. using nkast.Aether.Content.Pipeline.Graphics;
  25. namespace nkast.Aether.Content.Pipeline.Processors
  26. {
  27. [ContentProcessor(DisplayName = "CPU AnimatedModel - Aether")]
  28. class CpuAnimatedModelProcessor : DynamicModelProcessor, IContentProcessor
  29. {
  30. private int _maxBones = SkinnedEffect.MaxBones;
  31. private int _generateKeyframesFrequency = 0;
  32. private bool _fixRealBoneRoot = false;
  33. // used to avoid creating clones/duplicates of the same VertexBufferContent
  34. Dictionary<VertexBufferContent, CpuAnimatedVertexBufferContent> _vertexBufferCache = new Dictionary<VertexBufferContent,CpuAnimatedVertexBufferContent>();
  35. [DefaultValue(DynamicModelContent.BufferType.DynamicWriteOnly)]
  36. public new DynamicModelContent.BufferType VertexBufferType
  37. {
  38. get { return base.VertexBufferType; }
  39. set { base.VertexBufferType = value; }
  40. }
  41. [DefaultValue(DynamicModelContent.BufferType.Default)]
  42. public new DynamicModelContent.BufferType IndexBufferType
  43. {
  44. get { return base.IndexBufferType; }
  45. set { base.IndexBufferType = value; }
  46. }
  47. #if !PORTABLE
  48. [DisplayName("MaxBones")]
  49. #endif
  50. [DefaultValue(SkinnedEffect.MaxBones)]
  51. public virtual int MaxBones
  52. {
  53. get { return _maxBones; }
  54. set { _maxBones = value; }
  55. }
  56. #if !PORTABLE
  57. [DisplayName("Generate Keyframes Frequency")]
  58. #endif
  59. [DefaultValue(0)] // (0=no, 30=30fps, 60=60fps)
  60. public virtual int GenerateKeyframesFrequency
  61. {
  62. get { return _generateKeyframesFrequency; }
  63. set { _generateKeyframesFrequency = value; }
  64. }
  65. #if !PORTABLE
  66. [DisplayName("Fix BoneRoot from MG importer")]
  67. #endif
  68. [DefaultValue(false)]
  69. public virtual bool FixRealBoneRoot
  70. {
  71. get { return _fixRealBoneRoot; }
  72. set { _fixRealBoneRoot = value; }
  73. }
  74. public CpuAnimatedModelProcessor()
  75. {
  76. VertexBufferType = DynamicModelContent.BufferType.DynamicWriteOnly;
  77. IndexBufferType = DynamicModelContent.BufferType.Default;
  78. }
  79. object IContentProcessor.Process(object input, ContentProcessorContext context)
  80. {
  81. var model = Process((NodeContent)input, context);
  82. var outputModel = new DynamicModelContent(model);
  83. foreach(var mesh in outputModel.Meshes)
  84. {
  85. foreach(var part in mesh.MeshParts)
  86. {
  87. ProcessVertexBuffer(outputModel, context, part);
  88. ProcessIndexBuffer(outputModel, context, part);
  89. }
  90. }
  91. // import animation
  92. var animationProcessor = new AnimationsProcessor();
  93. animationProcessor.MaxBones = this.MaxBones;
  94. animationProcessor.GenerateKeyframesFrequency = this.GenerateKeyframesFrequency;
  95. animationProcessor.FixRealBoneRoot = this._fixRealBoneRoot;
  96. var animation = animationProcessor.Process((NodeContent)input, context);
  97. outputModel.Tag = animation;
  98. //ProcessNode((NodeContent)input);
  99. return outputModel;
  100. }
  101. protected override void ProcessVertexBuffer(DynamicModelContent dynamicModel, ContentProcessorContext context, DynamicModelMeshPartContent part)
  102. {
  103. if (VertexBufferType != DynamicModelContent.BufferType.Default)
  104. {
  105. // Replace the default VertexBufferContent with CpuAnimatedVertexBufferContent.
  106. CpuAnimatedVertexBufferContent vb;
  107. if (!_vertexBufferCache.TryGetValue(part.VertexBuffer, out vb))
  108. {
  109. vb = new CpuAnimatedVertexBufferContent(part.VertexBuffer);
  110. vb.IsWriteOnly = (VertexBufferType == DynamicModelContent.BufferType.DynamicWriteOnly);
  111. _vertexBufferCache[part.VertexBuffer] = vb;
  112. }
  113. part.VertexBuffer = vb;
  114. }
  115. }
  116. }
  117. }