TexturePlusAlphaProcessor.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // TexturePlusAlphaProcessor.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System.IO;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. using Microsoft.Xna.Framework.Content.Pipeline;
  14. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  15. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  16. #endregion
  17. namespace SpriteShaderPipeline
  18. {
  19. /// <summary>
  20. /// Custom content processor reads two bitmaps, one containing color information,
  21. /// the other containing greyscale opacity data, and merges them into a single
  22. /// combined texture that has both RGB color data and an alpha channel.
  23. /// </summary>
  24. [ContentProcessor]
  25. public class TexturePlusAlphaProcessor : TextureProcessor
  26. {
  27. public override TextureContent Process(TextureContent input,
  28. ContentProcessorContext context)
  29. {
  30. TextureContent colorTexture = input;
  31. // The color texture was passed to us as input, but we also need to go
  32. // load in the texture containing our alpha information. We locate this
  33. // using a naming convention: it should have the same name as the main
  34. // color texture, but with a "_alpha" suffix, so for instance "cat.jpg"
  35. // goes with "cat_alpha.jpg".
  36. string colorFilename = colorTexture.Identity.SourceFilename;
  37. string alphaFilename = Path.GetDirectoryName(colorFilename) +
  38. Path.DirectorySeparatorChar +
  39. Path.GetFileNameWithoutExtension(colorFilename) +
  40. "_alpha" +
  41. Path.GetExtension(colorFilename);
  42. // Ask the content pipeline to load in the alpha texture.
  43. ExternalReference<TextureContent> alphaReference;
  44. alphaReference = new ExternalReference<TextureContent>(alphaFilename);
  45. TextureContent alphaTexture;
  46. alphaTexture = context.BuildAndLoadAsset<TextureContent, TextureContent>
  47. (alphaReference, null);
  48. // Convert both textures to Color format, for ease of processing.
  49. colorTexture.ConvertBitmapType(typeof(PixelBitmapContent<Color>));
  50. alphaTexture.ConvertBitmapType(typeof(PixelBitmapContent<Color>));
  51. PixelBitmapContent<Color> colorBitmap, alphaBitmap;
  52. colorBitmap = (PixelBitmapContent<Color>)colorTexture.Faces[0][0];
  53. alphaBitmap = (PixelBitmapContent<Color>)alphaTexture.Faces[0][0];
  54. if ((colorBitmap.Width != alphaBitmap.Width) ||
  55. (colorBitmap.Height != alphaBitmap.Height))
  56. {
  57. throw new InvalidContentException(
  58. "Color and alpha bitmaps are not the same size.");
  59. }
  60. // Merge the two bitmaps.
  61. for (int y = 0; y < colorBitmap.Height; y++)
  62. {
  63. for (int x = 0; x < colorBitmap.Width; x++)
  64. {
  65. Color color = colorBitmap.GetPixel(x, y);
  66. Color alphaAsGreyscale = alphaBitmap.GetPixel(x, y);
  67. byte alpha = (byte)((alphaAsGreyscale.R +
  68. alphaAsGreyscale.G +
  69. alphaAsGreyscale.B) / 3);
  70. Color combinedColor = new Color(color.R, color.G, color.B, alpha);
  71. colorBitmap.SetPixel(x, y, combinedColor);
  72. }
  73. }
  74. // Chain to the base SpriteTextureProcessor.
  75. //return base.Process(colorTexture, context);
  76. return colorTexture;
  77. }
  78. }
  79. }