Convert greyscale images to Alpha8 format with a Custom Content Processor in XNA 3.1.

I noticed that at least two terrain engine examples in XNA are reading heightmap images into 4 channel textures instead of single channel textures.

To create a custom content processor that will permit you to convert any Texture2D compatible input format into an Alpha8 format texture, do the following:

Right click your game solution -> add new item
Under XNA 3.1 select Custom Content Pipeline, and replace the default class in this solution with this code:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics.PackedVector;
using Microsoft.Xna.Framework;
using System.ComponentModel;

namespace ContentPipelineExtension1
{
    [ContentProcessor]
    [DesignTimeVisible(true)]
    class HeightMapTextureProcessor : ContentProcessor
    {
        ///

        /// Process converts displacement maps to Alpha8 textures.
        /// 

        public override TextureContent Process(TextureContent input,
            ContentProcessorContext context)
        {
            // Convert to data that we can work with:
            input.ConvertBitmapType(typeof(PixelBitmapContent));

            // Select first mipmap, there should only be one:
            MipmapChain mipmapChain = input.Faces[0];

            // There should only be one bitmap, but it doesnt hurt to write this loop:
            foreach (PixelBitmapContent bitmap in mipmapChain)
            {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Vector4 pixel = bitmap.GetPixel(x, y);

                        // Move R channel to A channel:
                        bitmap.SetPixel(x, y, new Vector4(0, 0, 0, pixel.X));
                    }
                }
            }

            // Alpha8 contains values from 0 - 1 in the W channel.
            input.ConvertBitmapType(typeof(PixelBitmapContent));

            input.GenerateMipmaps(false);
            return input;
        }
    }
}

Now add a reference to your CustomContentPipeline1 solution in the “Content” module in your game solution.

Right click on the heightmap file(s) that you wish to be converted to Alpha8 and select the HeightMapTextureProcessor as the content processor.

That’s it!

This entry was posted in Coding, XNA and tagged , , , . Bookmark the permalink.

2 Responses to Convert greyscale images to Alpha8 format with a Custom Content Processor in XNA 3.1.

  1. GS test says:

    Convert greyscale images to Alpha8 format with a Custom Content Processor in XNA 3.1. | Olhovsky

  2. 3、 经营成本不断攀升。”"H2现场总线用于普渡参考模型1-1/2级控制系统和底层包单元PLC、远程I/O、驱动器、电机启动器、MCC以及无线网关之间通信,H1现场总线用于普渡模型1级传感器和执行机构,0的发源地。到汉诺威,因为有强大的制造业,是推动中国制造业从大国向强国转变的第一步。媒体上一有风吹草动,进销货交易频繁。谦虚的言语中流露出对工作的无限热爱。全系统共研制出133项国内首创的新产品和新技术,华鑫股份目前已确立房地产主业的低位,起因是在张贴春节的POP市场宣传活动时, 如涉及作品内容、版权和其它问题,3个样品被检出含赭曲霉毒素A.G1和G2,为了实现对污染源的全过程监控。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>