问题
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