NormalMapTextureProcessor.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // NormalMapTextureProcessor.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using Microsoft.Xna.Framework.Content.Pipeline;
  13. using Microsoft.Xna.Framework.Content.Pipeline.Processors;
  14. using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
  15. using Microsoft.Xna.Framework.Graphics.PackedVector;
  16. using Microsoft.Xna.Framework;
  17. using System.ComponentModel;
  18. namespace NormalMappingEffectPipeline
  19. {
  20. /// <summary>
  21. /// The NormalMapTextureProcessor takes in an encoded normal map, and outputs
  22. /// a texture in the NormalizedByte4 format. Every pixel in the source texture
  23. /// is remapped so that values ranging from 0 to 1 will range from -1 to 1.
  24. /// </summary>
  25. [ContentProcessor]
  26. [DesignTimeVisible(false)]
  27. class NormalMapTextureProcessor : ContentProcessor<TextureContent,TextureContent>
  28. {
  29. /// <summary>
  30. /// Process converts the encoded normals to the NormalizedByte4 format and
  31. /// generates mipmaps.
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <param name="context"></param>
  35. /// <returns></returns>
  36. public override TextureContent Process(TextureContent input,
  37. ContentProcessorContext context)
  38. {
  39. // convert to vector4 format, so that we know what kind of data we're
  40. // working with.
  41. input.ConvertBitmapType(typeof(PixelBitmapContent<Vector4>));
  42. // expand the encoded normals; values ranging from 0 to 1 should be
  43. // expanded to range to -1 to 1.
  44. // NOTE: in almost all cases, the input normalmap will be a
  45. // Texture2DContent, and will only have one face. just to be safe,
  46. // we'll do the conversion for every face in the texture.
  47. foreach (MipmapChain mipmapChain in input.Faces)
  48. {
  49. foreach (PixelBitmapContent<Vector4> bitmap in mipmapChain)
  50. {
  51. for (int x = 0; x < bitmap.Width; x++)
  52. {
  53. for (int y = 0; y < bitmap.Height; y++)
  54. {
  55. Vector4 encoded = bitmap.GetPixel(x, y);
  56. bitmap.SetPixel(x, y, 2 * encoded - Vector4.One);
  57. }
  58. }
  59. }
  60. }
  61. // now that the conversion to -1 to 1 ranges is finished, convert to the
  62. // runtime-ready format NormalizedByte4.
  63. // It is possible to perform the conversion to NormalizedByte4
  64. // in the inner loop above by copying to a new TextureContent. For
  65. // the sake of simplicity, we do it the slower way.
  66. input.ConvertBitmapType(typeof(PixelBitmapContent<NormalizedByte4>));
  67. input.GenerateMipmaps(false);
  68. return input;
  69. }
  70. }
  71. }