How to implement a conical/cone/circular gradient in WPF

ε祈祈猫儿з 提交于 2019-12-06 07:18:24

问题


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 (Circular Gradient and WPF) but I didn't want to resurrect an old question.


回答1:


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));


来源:https://stackoverflow.com/questions/2184127/how-to-implement-a-conical-cone-circular-gradient-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!