Advertisement
Guest User

XNA - DXT5 Normal Map Content Processor

a guest
Sep 30th, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. [ContentProcessor( DisplayName = "Texture - DXT Normal Map" )]
  2. public class DxtNormalMapProcessor : TextureProcessor {
  3.  
  4.     // PUBLIC DATA
  5.  
  6.     // Hard-coded parameter values - optional:
  7.  
  8.     public override bool GenerateMipmaps {
  9.         get { return true; }
  10.         set { }
  11.     }
  12.  
  13.     public override bool ColorKeyEnabled {
  14.         get { return false; }
  15.         set { }
  16.     }
  17.  
  18.     public override bool PremultiplyAlpha {
  19.         get { return false; }
  20.         set { }
  21.     }
  22.  
  23.     public override TextureProcessorOutputFormat TextureFormat {
  24.         get { return TextureProcessorOutputFormat.DxtCompressed; }
  25.         set { }
  26.     }
  27.  
  28.  
  29.  
  30.     // PUBLIC METHODS
  31.  
  32.     /// <summary>
  33.     /// Processes the texture for DXT5 normal map compression. Moves X to green and Y to alpha.
  34.     /// </summary>
  35.     public override TextureContent Process( TextureContent input, ContentProcessorContext context ) {
  36.  
  37.         // Move X to green and Y to alpha
  38.         foreach ( MipmapChain face in input.Faces ) {
  39.             foreach ( BitmapContent bitmap in face ) {
  40.                 byte[] data = bitmap.GetPixelData();
  41.                 for ( int i = 0; i < data.Length; i += 4 ) {
  42.                     int red = i, green = i + 1, blue = i + 2, alpha = i + 3;
  43.                     data[ alpha ] = data[ green ];
  44.                     data[ green ] = data[ red ];
  45.                     data[ red ] = 0;
  46.                     data[ blue ] = 0;
  47.                 }
  48.                 bitmap.SetPixelData( data );
  49.             }
  50.         }
  51.  
  52.         // Do base class processing
  53.         TextureContent output = base.Process( input, context );
  54.  
  55.         // Return result
  56.         return output;
  57.     }
  58.  
  59. } // end class
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement