What are the risks of loading textures from images at runtime in XNA?

眉间皱痕 提交于 2019-12-07 14:41:30

问题


What is there to know about (apart from only being able to do that on PC) loading textures from images at runtime?


回答1:


I've been exclusively loading my textures from streams as well, and the only "danger" that I can come up with is Premultiplied Alphas. You'll either need to process each texture as you load it, or render with premultiplied alphas disabled. Shawn Hargreaves wrote a great blog article on this subject. In the comments, he mentions:

If you don't go through our Content Pipeline, you have to handle the format conversion yourself, or just not use premultiplied alpha

So initializing your sprite batch(es) with BlendState.NonPremultiplied should work.

In my game, I have processed each texture when I load it.

EDIT: Here's my method:

private static void PreMultiplyAlphas(Texture2D ret)
{
    var data = new Byte4[ret.Width * ret.Height];
    ret.GetData(data);
    for (var i = 0; i < data.Length; i++)
    {
        var vec = data[i].ToVector4();
        var alpha = vec.W / 255.0f;
        var a = (Int32)(vec.W);
        var r = (Int32)(alpha * vec.X);
        var g = (Int32)(alpha * vec.Y);
        var b = (Int32)(alpha * vec.Z);
        data[i].PackedValue = (UInt32)((a << 24) + (b << 16) + (g << 8) + r);
    }
    ret.SetData(data);
}

As you can see, all it does is multiply the color channels by the alpha, then stuff it back into the texture. Without this, your sprites will likely appear brighter/darker? than they should. (disclaimer: I didn't write the method above, a friend of mine did)



来源:https://stackoverflow.com/questions/12078206/what-are-the-risks-of-loading-textures-from-images-at-runtime-in-xna

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!