TextureAtlasProcessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ComponentModel;
  18. using Microsoft.Xna.Framework;
  19. using Microsoft.Xna.Framework.Content.Pipeline;
  20. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  21. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  22. using tainicom.Aether.Content.Pipeline.Atlas;
  23. namespace tainicom.Aether.Content.Pipeline
  24. {
  25. [ContentProcessor(DisplayName = "AtlasProcessor - Aether")]
  26. public class AtlasProcessor : TextureProcessor, IContentProcessor
  27. {
  28. private bool _mipmapsPerSprite = true;
  29. #if WINDOWS
  30. // override InputType
  31. [Browsable(false)]
  32. #endif
  33. Type IContentProcessor.InputType { get { return typeof(TextureAtlasContent); } }
  34. #if WINDOWS
  35. // override OutputType
  36. [Browsable(false)]
  37. #endif
  38. Type IContentProcessor.OutputType { get { return typeof(TextureAtlasContent); } }
  39. [DefaultValue(true)]
  40. public new bool MipmapsPerSprite
  41. {
  42. get { return _mipmapsPerSprite; }
  43. set { _mipmapsPerSprite = value; }
  44. }
  45. public AtlasProcessor()
  46. {
  47. }
  48. object IContentProcessor.Process(object input, ContentProcessorContext context)
  49. {
  50. return Process((TextureAtlasContent)input, context);
  51. }
  52. public TextureAtlasContent Process(TextureAtlasContent input, ContentProcessorContext context)
  53. {
  54. if (MipmapsPerSprite && GenerateMipmaps)
  55. foreach (var texture in input.Textures)
  56. texture.Texture.GenerateMipmaps(false);
  57. var output = input;
  58. if (GenerateMipmaps)
  59. {
  60. if (MipmapsPerSprite)
  61. {
  62. var maxSpriteWidth = 1;
  63. var maxSpriteHeight = 1;
  64. foreach (var sprite in input.Textures)
  65. {
  66. var face0 = sprite.Texture.Faces[0];
  67. maxSpriteWidth = Math.Max(maxSpriteWidth, face0[0].Width);
  68. maxSpriteHeight = Math.Max(maxSpriteHeight, face0[0].Height);
  69. }
  70. for (int mipLevel = 1; ; mipLevel++)
  71. {
  72. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  73. Rectangle size = new Rectangle(0, 0, input.Width, input.Height);
  74. size.Width /= mipLevel2;
  75. size.Height /= mipLevel2;
  76. if ((maxSpriteWidth / mipLevel2) < 1 && (maxSpriteHeight / mipLevel2) < 1) break;
  77. var mipmapBmp = new PixelBitmapContent<Color>(size.Width, size.Height);
  78. foreach (var sprite in input.Sprites)
  79. {
  80. if (mipLevel >= sprite.Texture.Faces[0].Count) continue;
  81. var srcBmp = sprite.Texture.Faces[0][mipLevel];
  82. var srcRect = new Rectangle(0, 0, srcBmp.Width, srcBmp.Height);
  83. var destRect = sprite.DestinationRectangle;
  84. destRect.X = (int)Math.Ceiling((float)destRect.X / mipLevel2);
  85. destRect.Y = (int)Math.Ceiling((float)destRect.Y / mipLevel2);
  86. destRect.Width = (int)(destRect.Width / mipLevel2);
  87. destRect.Height = (int)(destRect.Height / mipLevel2);
  88. if (destRect.Width > 1 && destRect.Height > 1)
  89. BitmapContent.Copy(srcBmp, srcRect, mipmapBmp, destRect);
  90. }
  91. output.Mipmaps.Add(mipmapBmp);
  92. }
  93. var outputFace0 = output.Faces[0];
  94. while (outputFace0[outputFace0.Count - 1].Width > 1 || outputFace0[outputFace0.Count - 1].Height > 1)
  95. {
  96. var lastMipmap = outputFace0[outputFace0.Count - 1];
  97. var w = Math.Max(1, lastMipmap.Width/2);
  98. var h = Math.Max(1, lastMipmap.Height/2);
  99. var mipmapBmp = new PixelBitmapContent<Color>(w, h);
  100. //PixelBitmapContent<Color>.Copy(lastMipmap, mipmapBmp);
  101. output.Mipmaps.Add(mipmapBmp);
  102. }
  103. }
  104. else
  105. {
  106. output.GenerateMipmaps(false);
  107. }
  108. }
  109. base.Process(output, context);
  110. return output;
  111. }
  112. }
  113. }