Sprite Batch Does Not Change Alpha

I ran into a problem when SpriteBatch does not draw the specified Trail with the changed alpha. What I'm trying to do is the “fade effect” when the “Item” alpha is reduced so that it becomes more transparent until it is destroyed. However, this does not change the alpha on it? Alpha decreases, but the alpha value of the color does not change, it remains the same color, and then disappears

Here's what happens: http://dl.dropbox.com/u/14970061/Untitled.jpg

And here is what I am trying to do http://dl.dropbox.com/u/14970061/Untitled2.jpg

Here's a cutout of the related code I'm currently using.

spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); for (int i = 0; i < Trails.Count; i++) { Trail Item = Trails[i]; if (Item.alpha < 1) { Trails.RemoveAt(i); i--; continue; } Item.alpha -= 255 * (float)gameTime.ElapsedGameTime.TotalSeconds; Color color = new Color(255, 0, 0, Item.alpha); spriteBatch.Draw(simpleBullet, Item.position, color); } spriteBatch.End(); 
+4
source share
4 answers

Do not use NonPremultiplied unless you need to! Leave it as an AlphaBlend . Read “Pre-Multiplied Alpha” and how it was added in XNA 4.0 .

The correct solution to your problem is to use the multiply operator for your color:

 Color color = Color.Red * Item.alpha/255f; 

Or use the equivalent Lerp function to interpolate it on transparency:

 Color color = Color.Lerp(Color.Red, Color.Transparent, Item.alpha/255f); 

(In addition, if you changed the blend state to non-premultiplied to be true, you would have to change the content import to not exceed your textures, and make sure your content has mixed data around its transparent edges.)

+1
source

Make sure your spriteBatch.Begin() call contains the necessary parameters:

 spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); 
+1
source

The alpha range is between 1 (completely opaque) and 0 (completely transparent), as well as its float, which I consider. Thus, you go beyond your range.

edit: try decreasing it by 0.1 and if it is less than or equal to zero, delete it

0
source

Turns out it worked, I just used the wrong BlendState, I switched to BlendState.NonPremultiplied and now it works.

0
source

Source: https://habr.com/ru/post/1345730/


All Articles