C# 设置像素着色器均匀变量
我想这是个愚蠢的问题。我有一个自定义着色器,如下所示: sampler2D InputTexture; float parameter1, parameter2 etc float4 main(float2 uv : TEXCOORD) : COLOR { float4 result = blah-blah-blah some calculations using parameter1, parameter2 etc. return result; } class MyShaderEffect : ShaderEffect { private PixelShader _pixelShader = new PixelShader(); public readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(MyShaderEffect), 0); public MyShaderEffect() { _pixelShader.UriSource = new Uri("MyShader.ps", UriKind.Relative); this.PixelShader = _pixelShader; this.UpdateShaderValue(InputProperty); } public Brush Input { get { return (Brush)this.GetValue(InputProperty); } set { this.SetValue(InputProperty, value); } } } 采样器2D输入纹理; 浮动参数1、参数2等 浮动4主(浮动2 uv:TEXCOORD):颜色 { float4 result=blah blah blah使用参数1、参数2等进行一些计算。 返回结果; } 我试图通过包装器使用它,包装器如下所示: sampler2D InputTexture; float parameter1, parameter2 etc float4 main(float2 uv : TEXCOORD) : COLOR { float4 result = blah-blah-blah some calculations using parameter1, parameter2 etc. return result; } class MyShaderEffect : ShaderEffect { private PixelShader _pixelShader = new PixelShader(); public readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(MyShaderEffect), 0); public MyShaderEffect() { _pixelShader.UriSource = new Uri("MyShader.ps", UriKind.Relative); this.PixelShader = _pixelShader; this.UpdateShaderValue(InputProperty); } public Brush Input { get { return (Brush)this.GetValue(InputProperty); } set { this.SetValue(InputProperty, value); } } } 类MyShaderEffect:ShaderEffect { 私有PixelShader _PixelShader=新PixelShader(); public readonly DependencyProperty InputProperty=ShaderEffect.RegisterPixelShaderSamplerProperty(“输入”,typeof(MyShaderEffect),0); 公共MyShaderEffect() { _pixelShader.UriSource=新Uri(“MyShader.ps”,UriKind.Relative); this.PixelShader=\u PixelShader; 此.UpdateShaderValue(InputProperty); } 公共刷输入 { 获取{return(Brush)this.GetValue(InputProperty);} set{this.SetValue(InputProperty,value);} } } 因此,我的问题是:如何从C#程序设置这些着色器参数?它就在C# 设置像素着色器均匀变量,c#,wpf,shader,pixel-shader,C#,Wpf,Shader,Pixel Shader,我想这是个愚蠢的问题。我有一个自定义着色器,如下所示: sampler2D InputTexture; float parameter1, parameter2 etc float4 main(float2 uv : TEXCOORD) : COLOR { float4 result = blah-blah-blah some calculations using parameter1, parameter2 etc. return result; } class MySh
ShaderEffect
类的。您需要为每个参数创建依赖项属性。例如:
class MyShaderEffect
{
public MyShaderEffect()
{
PixelShader = _pixelShader;
UpdateShaderValue(InputProperty);
UpdateShaderValue(ThresholdProperty);
}
public double Threshold
{
get { return (double)GetValue(ThresholdProperty); }
set { SetValue(ThresholdProperty, value); }
}
public static readonly DependencyProperty ThresholdProperty =
DependencyProperty.Register("Threshold", typeof(double), typeof(MyShaderEffect),
new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0)));
}
PixelShaderConstantCallback
中的0
指的是您在HLSL中使用的寄存器:
float threshold : register(c0);
这样,WPF知道在属性更改时更新着色器。在构造函数中调用UpdateShaderValue
以最初传递值也很重要