149 lines
5.8 KiB
C#
149 lines
5.8 KiB
C#
using System.Windows.Media.Effects;
|
||
using Melskin.Appearance;
|
||
|
||
namespace Melskin.Effects;
|
||
|
||
/// <summary>
|
||
/// LightedSurfaceEffect 类用于创建一个光照表面的效果。它继承自 ShaderEffect,利用像素着色器来实现特定的视觉效果。
|
||
/// 该效果能够根据鼠标位置、UI尺寸、光源大小、强度和颜色等参数动态改变表面的光照效果。
|
||
/// </summary>
|
||
public class LightedSurfaceEffect : ShaderEffect
|
||
{
|
||
/// <summary>
|
||
/// 输入属性,用于指定光照表面效果所应用的画刷。此属性允许设置一个Brush对象作为像素着色器的输入源,从而影响最终渲染的视觉效果。
|
||
/// </summary>
|
||
public Brush Input
|
||
{
|
||
get => (Brush)GetValue(InputProperty);
|
||
set => SetValue(InputProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 输入属性的依赖属性标识符,用于定义画刷输入源。此属性允许设置一个Brush对象作为像素着色器的数据源,进而影响光照表面效果的视觉呈现。
|
||
/// </summary>
|
||
public static readonly DependencyProperty InputProperty
|
||
= RegisterPixelShaderSamplerProperty("Input",
|
||
typeof(LightedSurfaceEffect), 0);
|
||
|
||
|
||
/// <summary>
|
||
/// 鼠标相对控件位置
|
||
/// </summary>
|
||
public Point MousePosition
|
||
{
|
||
get => (Point)GetValue(MousePositionProperty);
|
||
set => SetValue(MousePositionProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 用于定义鼠标位置的依赖属性,该属性影响光照表面效果的位置计算。通过更改此属性的值,可以动态更新光照效果中鼠标位置相关的视觉表现。
|
||
/// </summary>
|
||
public static readonly DependencyProperty MousePositionProperty =
|
||
DependencyProperty.Register(nameof(MousePosition),
|
||
typeof(Point), typeof(LightedSurfaceEffect),
|
||
new PropertyMetadata(new Point(100,20),
|
||
PixelShaderConstantCallback(0)));
|
||
|
||
|
||
/// <summary>
|
||
/// 控件的大小,表示为宽度和高度的点。
|
||
/// </summary>
|
||
public Point UiSize
|
||
{
|
||
get => (Point)GetValue(UiSizeProperty);
|
||
set => SetValue(UiSizeProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// UI尺寸属性,用于指定应用光照效果的控件大小。此属性影响光照效果在UI元素上的呈现方式。
|
||
/// </summary>
|
||
public static readonly DependencyProperty UiSizeProperty =
|
||
DependencyProperty.Register(nameof(UiSize),
|
||
typeof(Point), typeof(LightedSurfaceEffect),
|
||
new PropertyMetadata(new Point(200,40),
|
||
PixelShaderConstantCallback(1)));
|
||
|
||
/// <summary>
|
||
/// 光照大小
|
||
/// </summary>
|
||
|
||
public double LightSize
|
||
{
|
||
get => (double)GetValue(LightSizeProperty);
|
||
set => SetValue(LightSizeProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 光源大小属性,用于控制光照效果中光源的尺寸。此属性影响光照覆盖范围,值越大则光照区域越广。
|
||
/// </summary>
|
||
public static readonly DependencyProperty LightSizeProperty =
|
||
DependencyProperty.Register(nameof(LightSize),
|
||
typeof(double), typeof(LightedSurfaceEffect),
|
||
new PropertyMetadata(40.0,
|
||
PixelShaderConstantCallback(2)));
|
||
|
||
|
||
/// <summary>
|
||
/// 光照效果的强度
|
||
/// </summary>
|
||
public double Intensity
|
||
{
|
||
get => (double)GetValue(IntensityProperty);
|
||
set => SetValue(IntensityProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 光照强度属性。此属性控制光照效果的强度,值越大表示光照越强。
|
||
/// </summary>
|
||
// Using a DependencyProperty as the backing store for Intensity.
|
||
public static readonly DependencyProperty IntensityProperty =
|
||
DependencyProperty.Register(nameof(Intensity),
|
||
typeof(double), typeof(LightedSurfaceEffect),
|
||
new PropertyMetadata(1.0,
|
||
PixelShaderConstantCallback(3)));
|
||
|
||
|
||
/// <summary>
|
||
/// 光源颜色,用于定义光照效果的颜色。
|
||
/// </summary>
|
||
public Color LightColor
|
||
{
|
||
get => (Color)GetValue(LightColorProperty);
|
||
set => SetValue(LightColorProperty, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 光源颜色属性,用于定义光照效果的颜色。此属性影响光照在表面上的视觉呈现。
|
||
/// </summary>
|
||
public static readonly DependencyProperty LightColorProperty =
|
||
DependencyProperty.Register(nameof(LightColor),
|
||
typeof(Color), typeof(LightedSurfaceEffect),
|
||
new PropertyMetadata(Color.FromArgb(255,255,255,255),
|
||
PixelShaderConstantCallback(4)));
|
||
|
||
|
||
/// <summary>
|
||
/// LightedSurfaceEffect 类用于创建一个光照表面的效果。它继承自 ShaderEffect,利用像素着色器来实现特定的视觉效果。
|
||
/// 该效果能够根据鼠标位置、UI尺寸、光源大小、强度和颜色等参数动态改变表面的光照效果。
|
||
/// </summary>
|
||
/// <remarks>
|
||
/// 通过设置不同的属性值可以控制光照效果的具体表现形式,包括光源的位置(MousePosition)、UI组件的尺寸(UiSize)、
|
||
/// 光源的大小(LightSize)、光照强度(Intensity)以及光源的颜色(LightColor)。这些属性的变化会实时影响到呈现出来的光照效果,
|
||
/// 使得用户界面更加生动和互动。
|
||
/// </remarks>
|
||
public LightedSurfaceEffect()
|
||
{
|
||
var shaderUri = $"pack://application:,,,/{ThemeManager.LibraryNamespace};component/Shaders/LightedSurfaceEffect.ps";
|
||
PixelShader = new PixelShader()
|
||
{
|
||
UriSource = new Uri(shaderUri)
|
||
};
|
||
|
||
UpdateShaderValue(InputProperty);
|
||
UpdateShaderValue(MousePositionProperty);
|
||
UpdateShaderValue(UiSizeProperty);
|
||
UpdateShaderValue(LightSizeProperty);
|
||
UpdateShaderValue(LightColorProperty);
|
||
UpdateShaderValue(IntensityProperty);
|
||
}
|
||
} |