TexturePlusAlphaProcessor.cs 3.9 KB

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