TextureAtlasProcessor.cs 5.6 KB

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