Issue with Texture2D.FromStream

Hi Guys,

I use a system for showing damage on meshes which uses the alpha channel.

So you create a skin for the mesh like this.

Then you cut holes in it using the alpha channel.

In the shader I override the alpha channel with a value based on damage levels.

However when I display the mesh in Monogame I get this

You can see that the parts with alpha < 1 are darker.

I have studied my shader in detail and I am confident that is not the issue, in fact I cannot see any possible way it could be. Even bad shader compilation wouldn’t achieve what I am seeing.

So I am pretty sure it is caused by pre-multiplying the alpha channel.

The horrendous bug that Microsoft created in XNA 4.0 just so XNA could run on low powered mobile devices.

My guess is that if the texture is imported from a stream, it automatically does the multiply.

In the content importer (which I cannot use in this case) you have an option to turn off the pre-multiply.

I think we need another method on Texture2D, FromStream(GraphicsDevice,Stream,Options);

Don’t know what I am going to do as a work around for the moment though…

Looking at the code, I really don’t like this

// XNA blacks out any pixels with an alpha of zero. if (channels == 4) { fixed (byte* b = &data[0]) { for (var i = 0; i < data.Length; i += 4) { if (b[i + 3] == 0) { b[i + 0] = 0; b[i + 1] = 0; b[i + 2] = 0; } } } }

Confirmed, that code is my problem,.

I grabbed the source code and commented out those lines and now it is fine.

By the way big high five to all of you for the source code download. Took me five minutes to grab the source code and make my own build.

That’s orders of magnitude easier than many commercial platforms I have worked on.