2025-08-12 23:08:54 +08:00
|
|
|
|
using System.Windows.Media.Effects;
|
2026-01-02 17:30:41 +08:00
|
|
|
|
using Melskin.Appearance;
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2026-01-02 17:30:41 +08:00
|
|
|
|
namespace Melskin.Effects;
|
2025-08-12 23:08:54 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// LightedSurfaceEffect 类用于创建一个光照表面的效果。它继承自 ShaderEffect,利用像素着色器来实现特定的视觉效果。
|
|
|
|
|
|
/// 该效果能够根据鼠标位置、UI尺寸、光源大小、强度和颜色等参数动态改变表面的光照效果。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class LightedSurfaceEffect : ShaderEffect
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 输入属性,用于指定光照表面效果所应用的画刷。此属性允许设置一个Brush对象作为像素着色器的输入源,从而影响最终渲染的视觉效果。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public Brush Input
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-08-12 23:08:54 +08:00
|
|
|
|
get => (Brush)GetValue(InputProperty);
|
|
|
|
|
|
set => SetValue(InputProperty, value);
|
|
|
|
|
|
}
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 输入属性的依赖属性标识符,用于定义画刷输入源。此属性允许设置一个Brush对象作为像素着色器的数据源,进而影响光照表面效果的视觉呈现。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public static readonly DependencyProperty InputProperty
|
|
|
|
|
|
= RegisterPixelShaderSamplerProperty("Input",
|
|
|
|
|
|
typeof(LightedSurfaceEffect), 0);
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-12 23:08:54 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 鼠标相对控件位置
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Point MousePosition
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Point)GetValue(MousePositionProperty);
|
|
|
|
|
|
set => SetValue(MousePositionProperty, value);
|
|
|
|
|
|
}
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 用于定义鼠标位置的依赖属性,该属性影响光照表面效果的位置计算。通过更改此属性的值,可以动态更新光照效果中鼠标位置相关的视觉表现。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public static readonly DependencyProperty MousePositionProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(MousePosition),
|
|
|
|
|
|
typeof(Point), typeof(LightedSurfaceEffect),
|
|
|
|
|
|
new PropertyMetadata(new Point(100,20),
|
|
|
|
|
|
PixelShaderConstantCallback(0)));
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 控件的大小,表示为宽度和高度的点。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public Point UiSize
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Point)GetValue(UiSizeProperty);
|
|
|
|
|
|
set => SetValue(UiSizeProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// UI尺寸属性,用于指定应用光照效果的控件大小。此属性影响光照效果在UI元素上的呈现方式。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public static readonly DependencyProperty UiSizeProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(UiSize),
|
|
|
|
|
|
typeof(Point), typeof(LightedSurfaceEffect),
|
|
|
|
|
|
new PropertyMetadata(new Point(200,40),
|
2025-07-11 09:20:23 +08:00
|
|
|
|
PixelShaderConstantCallback(1)));
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光照大小
|
|
|
|
|
|
/// </summary>
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public double LightSize
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(LightSizeProperty);
|
|
|
|
|
|
set => SetValue(LightSizeProperty, value);
|
|
|
|
|
|
}
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光源大小属性,用于控制光照效果中光源的尺寸。此属性影响光照覆盖范围,值越大则光照区域越广。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public static readonly DependencyProperty LightSizeProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(LightSize),
|
|
|
|
|
|
typeof(double), typeof(LightedSurfaceEffect),
|
|
|
|
|
|
new PropertyMetadata(40.0,
|
|
|
|
|
|
PixelShaderConstantCallback(2)));
|
2025-07-11 09:20:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光照效果的强度
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public double Intensity
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (double)GetValue(IntensityProperty);
|
|
|
|
|
|
set => SetValue(IntensityProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光照强度属性。此属性控制光照效果的强度,值越大表示光照越强。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
// 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,
|
2025-07-11 09:20:23 +08:00
|
|
|
|
PixelShaderConstantCallback(3)));
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光源颜色,用于定义光照效果的颜色。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public Color LightColor
|
|
|
|
|
|
{
|
|
|
|
|
|
get => (Color)GetValue(LightColorProperty);
|
|
|
|
|
|
set => SetValue(LightColorProperty, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-20 12:10:13 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 光源颜色属性,用于定义光照效果的颜色。此属性影响光照在表面上的视觉呈现。
|
|
|
|
|
|
/// </summary>
|
2025-08-12 23:08:54 +08:00
|
|
|
|
public static readonly DependencyProperty LightColorProperty =
|
|
|
|
|
|
DependencyProperty.Register(nameof(LightColor),
|
|
|
|
|
|
typeof(Color), typeof(LightedSurfaceEffect),
|
|
|
|
|
|
new PropertyMetadata(Color.FromArgb(255,255,255,255),
|
2025-07-11 09:20:23 +08:00
|
|
|
|
PixelShaderConstantCallback(4)));
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-12 23:08:54 +08:00
|
|
|
|
/// <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()
|
2025-07-11 09:20:23 +08:00
|
|
|
|
{
|
2025-08-12 23:08:54 +08:00
|
|
|
|
UriSource = new Uri(shaderUri)
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
UpdateShaderValue(InputProperty);
|
|
|
|
|
|
UpdateShaderValue(MousePositionProperty);
|
|
|
|
|
|
UpdateShaderValue(UiSizeProperty);
|
|
|
|
|
|
UpdateShaderValue(LightSizeProperty);
|
|
|
|
|
|
UpdateShaderValue(LightColorProperty);
|
|
|
|
|
|
UpdateShaderValue(IntensityProperty);
|
2025-07-11 09:20:23 +08:00
|
|
|
|
}
|
2025-08-12 23:08:54 +08:00
|
|
|
|
}
|