What is a high performance way to create a 2D gradient from 2 colors (including alpha transparency)?
I really don't know the best technique but from my research I have seen stencil buffer, and shaders as a couple possible choices, but could not find a simple tutorial or code. Thanks!
XNA 2D Gradient
Page 1 of 13 Replies - 805 Views - Last Post: 28 July 2012 - 07:52 AM
Replies To: XNA 2D Gradient
#2
Re: XNA 2D Gradient
Posted 27 July 2012 - 11:21 PM
This is what I used for top down gradient, there are more generic methods so you can use one method for any type of gradient, but I never needed it.
Please note this will simply return a grey scale gradient which you can then apply a shade to, which will then display a colour -> black gradient
/// <summary>
/// Gets an array of colors for a Top Down Gradient.
/// </summary>
/// <param name="width">The width of the color array.</param>
/// <param name="height">The height of the color array.</param>
/// <returns>Color Array.</returns>
private static Color[] GetGradientColors(uint width, uint height)
{
//Declare variables
Color[] result;
float
increment;
int color;
//Determine that both height and width are greater than 0
if (!(width > 0 && height > 0))
//exit the function with a null color array
{ return null; }
//Setup the result array
result = new Color[width * height];
//Calculate the increment values
increment = (float)255 / (result.Length);
//Loop through each color
for (int i = 0; i < result.Length; i++)
{
color = (int)(increment * i);
result[i] = new Color(
color,
color,
color);
}
//return the color
return result;
}
Please note this will simply return a grey scale gradient which you can then apply a shade to, which will then display a colour -> black gradient
#3
Re: XNA 2D Gradient
Posted 28 July 2012 - 07:41 AM
How do you draw a color array? I am currently using spriteBatch.Draw.
#4
Re: XNA 2D Gradient
Posted 28 July 2012 - 07:52 AM
use create a texture using
"new Texture2D(.....)" width,height and a graphics device is needed
Then you assign the colors
"newTexture.SetData(colors);"
Something along those lines
"new Texture2D(.....)" width,height and a graphics device is needed
Then you assign the colors
"newTexture.SetData(colors);"
Something along those lines
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|