TilemapProcessor.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #region License
  2. // Copyright 2021 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 = "Tilemap Processor - Aether")]
  25. public class TilemapProcessor : 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(TilemapContent); } }
  33. #if WINDOWS
  34. // override OutputType
  35. [Browsable(false)]
  36. #endif
  37. Type IContentProcessor.OutputType { get { return typeof(TilemapContent); } }
  38. [DefaultValue(true)]
  39. public bool MipmapsPerSprite
  40. {
  41. get { return _mipmapsPerSprite; }
  42. set { _mipmapsPerSprite = value; }
  43. }
  44. public TilemapProcessor()
  45. {
  46. }
  47. object IContentProcessor.Process(object input, ContentProcessorContext context)
  48. {
  49. return Process((TilemapContent)input, context);
  50. }
  51. public TilemapContent Process(TilemapContent input, ContentProcessorContext context)
  52. {
  53. if (MipmapsPerSprite && GenerateMipmaps)
  54. foreach (var texture in input.DestinationTiles)
  55. texture.SrcTexture.GenerateMipmaps(false);
  56. var output = input;
  57. if (GenerateMipmaps)
  58. {
  59. if (MipmapsPerSprite)
  60. {
  61. var maxTileWidth = 1;
  62. var maxTileHeight = 1;
  63. foreach (var tile in input.DestinationTiles)
  64. {
  65. maxTileWidth = Math.Max(maxTileWidth, tile.DstBounds.Width);
  66. maxTileHeight = Math.Max(maxTileHeight, tile.DstBounds.Height);
  67. }
  68. for (int mipLevel = 1; ; mipLevel++)
  69. {
  70. int mipLevel2 = (int)Math.Pow(2, mipLevel);
  71. Rectangle size = new Rectangle(0, 0, output.TextureAtlas.Faces[0][0].Width, output.TextureAtlas.Faces[0][0].Height);
  72. size.Width /= mipLevel2;
  73. size.Height /= mipLevel2;
  74. if ((maxTileWidth / mipLevel2) < 1 && (maxTileHeight / mipLevel2) < 1)
  75. break;
  76. var mipmapBmp = new PixelBitmapContent<Color>(size.Width, size.Height);
  77. foreach (var tile in input.DestinationTiles)
  78. {
  79. if (mipLevel >= tile.SrcTexture.Faces[0].Count) continue;
  80. var srcBmp = tile.SrcTexture.Faces[0][mipLevel];
  81. var srcBounds = new Rectangle(0, 0, srcBmp.Width, srcBmp.Height);
  82. var dstBounds = tile.DstBounds;
  83. dstBounds.X = (int)Math.Ceiling((float)dstBounds.X / mipLevel2);
  84. dstBounds.Y = (int)Math.Ceiling((float)dstBounds.Y / mipLevel2);
  85. dstBounds.Width = (int)(dstBounds.Width / mipLevel2);
  86. dstBounds.Height = (int)(dstBounds.Height / mipLevel2);
  87. // snap image to bottom
  88. dstBounds.Width = srcBounds.Width;
  89. dstBounds.Y += (dstBounds.Height - srcBounds.Height);
  90. dstBounds.Height = srcBounds.Height;
  91. if (dstBounds.Width == 0 || dstBounds.Height == 0)
  92. continue;
  93. //if (dstBounds.Width > 0 && dstBounds.Height > 0)
  94. BitmapContent.Copy(srcBmp, srcBounds, mipmapBmp, dstBounds);
  95. }
  96. output.TextureAtlas.Mipmaps.Add(mipmapBmp);
  97. }
  98. var outputFace0 = output.TextureAtlas.Faces[0];
  99. while (outputFace0[outputFace0.Count - 1].Width > 1 || outputFace0[outputFace0.Count - 1].Height > 1)
  100. {
  101. var lastMipmap = outputFace0[outputFace0.Count - 1];
  102. var w = Math.Max(1, lastMipmap.Width/2);
  103. var h = Math.Max(1, lastMipmap.Height/2);
  104. var mipmapBmp = new PixelBitmapContent<Color>(w, h);
  105. //PixelBitmapContent<Color>.Copy(lastMipmap, mipmapBmp);
  106. output.TextureAtlas.Mipmaps.Add(mipmapBmp);
  107. }
  108. }
  109. else
  110. {
  111. output.TextureAtlas.GenerateMipmaps(false);
  112. }
  113. }
  114. // Workaround MonoGame TextureProcessor bug.
  115. // MonoGame TextureProcessor overwrites existing mipmaps.
  116. if (GenerateMipmaps && MipmapsPerSprite)
  117. {
  118. GenerateMipmaps = false;
  119. base.Process(output.TextureAtlas, context);
  120. GenerateMipmaps = true;
  121. }
  122. else
  123. {
  124. base.Process(output.TextureAtlas, context);
  125. }
  126. return output;
  127. }
  128. }
  129. }