DynamicModelProcessor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #region License
  2. // Copyright 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 System.Collections.ObjectModel;
  19. using System.ComponentModel;
  20. using Microsoft.Xna.Framework.Content.Pipeline;
  21. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  22. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  23. using nkast.Aether.Content.Pipeline.Graphics;
  24. namespace nkast.Aether.Content.Pipeline.Serialization
  25. {
  26. [ContentProcessor(DisplayName = "DynamicModel - Aether")]
  27. public class DynamicModelProcessor : ModelProcessor, IContentProcessor
  28. {
  29. DynamicModelContent.BufferType _vertexBufferType = DynamicModelContent.BufferType.Dynamic;
  30. DynamicModelContent.BufferType _indexBufferType = DynamicModelContent.BufferType.Dynamic;
  31. // used to avoid creating clones/duplicates of the same VertexBufferContent
  32. Dictionary<VertexBufferContent, DynamicVertexBufferContent> _vertexBufferCache = new Dictionary<VertexBufferContent, DynamicVertexBufferContent>();
  33. // used to avoid creating clones/duplicates of the same Index Buffer
  34. Dictionary<Collection<int>, DynamicIndexBufferContent> _indexBufferCache = new Dictionary<Collection<int>, DynamicIndexBufferContent>();
  35. #if WINDOWS
  36. // override OutputType
  37. [Browsable(false)]
  38. #endif
  39. Type IContentProcessor.OutputType { get { return typeof(DynamicModelContent); } }
  40. [DefaultValue(DynamicModelContent.BufferType.Dynamic)]
  41. public virtual DynamicModelContent.BufferType VertexBufferType
  42. {
  43. get { return _vertexBufferType; }
  44. set { _vertexBufferType = value; }
  45. }
  46. [DefaultValue(DynamicModelContent.BufferType.Dynamic)]
  47. public virtual DynamicModelContent.BufferType IndexBufferType
  48. {
  49. get { return _indexBufferType; }
  50. set { _indexBufferType = value; }
  51. }
  52. public DynamicModelProcessor()
  53. {
  54. }
  55. object IContentProcessor.Process(object input, ContentProcessorContext context)
  56. {
  57. var model = Process((NodeContent)input, context);
  58. var dynamicModel = new DynamicModelContent(model);
  59. foreach(var mesh in dynamicModel.Meshes)
  60. {
  61. foreach(var part in mesh.MeshParts)
  62. {
  63. ProcessVertexBuffer(dynamicModel, context, part);
  64. ProcessIndexBuffer(dynamicModel, context, part);
  65. }
  66. }
  67. return dynamicModel;
  68. }
  69. protected virtual void ProcessVertexBuffer(DynamicModelContent dynamicModel, ContentProcessorContext context, DynamicModelMeshPartContent part)
  70. {
  71. if(VertexBufferType != DynamicModelContent.BufferType.Default)
  72. {
  73. // Replace the default VertexBufferContent with CpuAnimatedVertexBufferContent.
  74. DynamicVertexBufferContent vb;
  75. if (!_vertexBufferCache.TryGetValue(part.VertexBuffer, out vb))
  76. {
  77. vb = new DynamicVertexBufferContent(part.VertexBuffer);
  78. vb.IsWriteOnly = (VertexBufferType == DynamicModelContent.BufferType.DynamicWriteOnly);
  79. _vertexBufferCache[part.VertexBuffer] = vb;
  80. }
  81. part.VertexBuffer = vb;
  82. }
  83. }
  84. protected virtual void ProcessIndexBuffer(DynamicModelContent dynamicModel, ContentProcessorContext context, DynamicModelMeshPartContent part)
  85. {
  86. if(IndexBufferType != DynamicModelContent.BufferType.Default)
  87. {
  88. DynamicIndexBufferContent ib;
  89. if (!_indexBufferCache.TryGetValue(part.IndexBuffer, out ib))
  90. {
  91. ib = new DynamicIndexBufferContent(part.IndexBuffer);
  92. ib.IsWriteOnly = (IndexBufferType == DynamicModelContent.BufferType.DynamicWriteOnly);
  93. _indexBufferCache[part.IndexBuffer] = ib;
  94. }
  95. part.IndexBuffer = ib;
  96. }
  97. }
  98. }
  99. }