2
\$\begingroup\$

How to draw a diagonal fade-in/fade-out in 2D using MonoGame draw API?

For a simple (non-directional) fade-in/out drawing the background with alpha variation do the tricks, but what is the standard way to fade-in/fade-out directionally in 2D?

(The background is a texture, not a unicolored background)

\$\endgroup\$
7
  • \$\begingroup\$ How about covering the screen with a texture of the desired characteristics, and moving it to achieve the desired effect? \$\endgroup\$ Commented Oct 6, 2014 at 10:20
  • \$\begingroup\$ @PandaPajama So what would be the desired characteristics to achieve that ? \$\endgroup\$
    – anopse
    Commented Oct 6, 2014 at 10:34
  • \$\begingroup\$ I don't know, you're the one making the game, so you tell me. I don't know what you mean by "diagonal fade-in", but in general you can make transition effects by overlaying a quad over the entire screen that does the effect you want. I guess some kind of diagonal alpha gradient? \$\endgroup\$ Commented Oct 6, 2014 at 10:37
  • \$\begingroup\$ It might be something like applying a diagonal gradiant on the alpha channel of the background texture, but I don't know how I can create gradiant on alpha channel at runtime. \$\endgroup\$
    – anopse
    Commented Oct 6, 2014 at 11:21
  • \$\begingroup\$ I don't know what you mean by "background texture", but I'm talking about an overlay, which is something you put in front of everything else. And who said you need to create it at runtime? you can create it as a texture and load it and display. \$\endgroup\$ Commented Oct 6, 2014 at 11:22

1 Answer 1

2
\$\begingroup\$

Creating an horizontal or vertical gradient is very simple. Anything else is going to be a little more complicated.

If you are looking to strictly create an horizontal or vertical gradient, you can do it as follows. This will create a RenderTarget for a horizontal gradient:

//create a blank 1x1 white texture
Texture2D BlankTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
Color[] color = new Color[1];
color[0] = Color.White;
BlankTexture.SetData(color);

//Set gradient colors
Color ColorA = Color.Blue;
Color ColorB = Color.Red;

//Create a rendertarget for horizontal gradient
Render = new RenderTarget2D(GraphicsDevice, 1, GraphicsDevice.Viewport.Height);

//Draw horizontal gradient
GraphicsDevice.SetRenderTarget(Render);
SpriteBatch.Begin();

for (int i = 0; i < Render.Height; i++)
    SpriteBatch.Draw(BlankTexture, new Rectangle(0, i, GraphicsDevice.Viewport.Width, 1), Color.Lerp(ColorA, ColorB, i / (float)Render.Height));

SpriteBatch.End();
GraphicsDevice.SetRenderTarget(null);

Then you can draw this across your screen, by stretching it along your viewport to create a smooth gradient effect:

SpriteBatch.Begin();
SpriteBatch.Draw(Render, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);
SpriteBatch.End();
\$\endgroup\$
2
  • \$\begingroup\$ I wish to use a specific texture for the overlay (not a simple gradient using two color, so I would need to apply the gradient as the alpha channel of my texture), and I also I wish it to be diagonal not horizontal or vertical. \$\endgroup\$
    – anopse
    Commented Oct 6, 2014 at 11:38
  • \$\begingroup\$ Yeah I figured, I opted to present this solution in case it was a viable option to you. In the case of a diagonal fade, it would be a lot easier to generate the texture in a paint application. Unless you are up for the challenge of generating them yourself, which could be very rewarding. \$\endgroup\$
    – jgallant
    Commented Oct 6, 2014 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .