3 Replies - 805 Views - Last Post: 28 July 2012 - 07:52 AM

#1 rex64  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 152
  • Joined: 31-January 12

XNA 2D Gradient

Posted 27 July 2012 - 06:29 PM

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!
Is This A Good Question/Topic? 0
  • +

Replies To: XNA 2D Gradient

#2 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

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.

/// <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
Was This Post Helpful? 0
  • +
  • -

#3 rex64  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 152
  • Joined: 31-January 12

Re: XNA 2D Gradient

Posted 28 July 2012 - 07:41 AM

How do you draw a color array? I am currently using spriteBatch.Draw.
Was This Post Helpful? 0
  • +
  • -

#4 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1