views:

401

answers:

1

I would like to recreate a conical/circular gradient in WPF. I've looked into inheriting System.Windows.Media.GradientBrush - which can be inherited from - but uses a lot of internal plumbing to get the job done (inherited from System.Windows.Media.Brush)

Any ideas on how to achieve this would be appreciated (preferrably without resorting to bitmaps)

Cheers.

Dan

This question was asked some time back in July (http://stackoverflow.com/questions/1187597/circular-gradient-and-wpf) but I didn't want to resurrect an old question.

+2  A: 

You could create a custom Effect. I would recommend downloading Shazzam you will also need the DirectX SDK. Give the element a horizontal gradient then apply the effect to transform it into a cone gradient.

/// <class>AngleGradient</class>
/// <description>Renders an angle gradient.</description>
//-----------------------------------------------------------------------------------------
// Shader constant register mappings (scalars - float, double, Point, Color, Point3D, etc.)
//-----------------------------------------------------------------------------------------
/// <summary>The centre of the gradient.</summary>
/// <minValue>0</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0.5,0.5</defaultValue>
float2 Centre : register(C0);

/// <summary>The start angle.</summary>
/// <minValue>0</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0</defaultValue>
float Angle : register(C1);

//--------------------------------------------------------------------------------------
// Sampler Inputs (Brushes, including ImplicitInput)
//--------------------------------------------------------------------------------------

sampler1D implicitInputSampler : register(S0);
static const float PI = 3.14159265f;
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
float4 main(float2 uv : TEXCOORD) : COLOR
{
    float angle = atan2(uv.y-Centre.y, uv.x-Centre.x)+PI;
    angle = (angle/(2*PI)) + Angle; 
    return tex1D(implicitInputSampler,min(angle > 1 ? angle-1 : angle,0.99));
Kris