CpuAnimatedModelProcessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. [DisplayName("MaxBones")]
  48. [DefaultValue(SkinnedEffect.MaxBones)]
  49. public virtual int MaxBones
  50. {
  51. get { return _maxBones; }
  52. set { _maxBones = value; }
  53. }
  54. [DisplayName("Generate Keyframes Frequency")]
  55. [DefaultValue(0)] // (0=no, 30=30fps, 60=60fps)
  56. public virtual int GenerateKeyframesFrequency
  57. {
  58. get { return _generateKeyframesFrequency; }
  59. set { _generateKeyframesFrequency = value; }
  60. }
  61. [DisplayName("Fix BoneRoot from MG importer")]
  62. [DefaultValue(false)]
  63. public virtual bool FixRealBoneRoot
  64. {
  65. get { return _fixRealBoneRoot; }
  66. set { _fixRealBoneRoot = value; }
  67. }
  68. public CpuAnimatedModelProcessor()
  69. {
  70. VertexBufferType = DynamicModelContent.BufferType.DynamicWriteOnly;
  71. IndexBufferType = DynamicModelContent.BufferType.Default;
  72. }
  73. object IContentProcessor.Process(object input, ContentProcessorContext context)
  74. {
  75. ModelContent model = Process((NodeContent)input, context);
  76. DynamicModelContent outputModel = new DynamicModelContent(model);
  77. foreach(DynamicModelMeshContent mesh in outputModel.Meshes)
  78. {
  79. foreach(DynamicModelMeshPartContent part in mesh.MeshParts)
  80. {
  81. ProcessVertexBuffer(outputModel, context, part);
  82. ProcessIndexBuffer(outputModel, context, part);
  83. }
  84. }
  85. // import animation
  86. AnimationsProcessor animationProcessor = new AnimationsProcessor();
  87. animationProcessor.MaxBones = this.MaxBones;
  88. animationProcessor.GenerateKeyframesFrequency = this.GenerateKeyframesFrequency;
  89. animationProcessor.FixRealBoneRoot = this._fixRealBoneRoot;
  90. AnimationsContent animation = animationProcessor.Process((NodeContent)input, context);
  91. outputModel.Tag = animation;
  92. //ProcessNode((NodeContent)input);
  93. return outputModel;
  94. }
  95. protected override void ProcessVertexBuffer(DynamicModelContent dynamicModel, ContentProcessorContext context, DynamicModelMeshPartContent part)
  96. {
  97. if (VertexBufferType != DynamicModelContent.BufferType.Default)
  98. {
  99. // Replace the default VertexBufferContent with CpuAnimatedVertexBufferContent.
  100. CpuAnimatedVertexBufferContent vb;
  101. if (!_vertexBufferCache.TryGetValue(part.VertexBuffer, out vb))
  102. {
  103. vb = new CpuAnimatedVertexBufferContent(part.VertexBuffer);
  104. vb.IsWriteOnly = (VertexBufferType == DynamicModelContent.BufferType.DynamicWriteOnly);
  105. _vertexBufferCache[part.VertexBuffer] = vb;
  106. }
  107. part.VertexBuffer = vb;
  108. }
  109. }
  110. }
  111. }