像素着色器效果不适用于红色,绿色和白色蓝色

时间:2014-03-26 11:47:06

标签: wpf image c#-4.0 hlsl pixel-shader

我真的在寻找应用颜色Red, Green & Blue的着色器效果问题的解决方案。当我移动Red, Green & Blue的滑块时,图像没有变化。

Note: Brightness and Contrast is working in this code.

ShaderEffect类的代码:

 public class BrightContrastEffect : ShaderEffect
    {
        private static PixelShader m_shader = new PixelShader() { UriSource = MakePackUri("bricon.ps") };

        public BrightContrastEffect()
        {
            PixelShader = m_shader;
            UpdateShaderValue(InputProperty);
            UpdateShaderValue(BrightnessProperty);
            UpdateShaderValue(ContrastProperty);
            UpdateShaderValue(RedProperty);
            UpdateShaderValue(GreenProperty);
            UpdateShaderValue(BlueProperty);
        }

        public static Uri MakePackUri(string relativeFile)
        {
            Assembly a = typeof(BrightContrastEffect).Assembly;
            string assemblyShortName = a.ToString().Split(',')[0];
            string uriString = "pack://application:,,,/" +assemblyShortName +";component/" +relativeFile;
            return new Uri(uriString);
        }

        public Brush Input
        {
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }
        }

        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(BrightContrastEffect), 0);

        public float Brightness
        {
            get { return (float)GetValue(BrightnessProperty); }
            set { SetValue(BrightnessProperty, value); }
        }

        public static readonly DependencyProperty BrightnessProperty = DependencyProperty.Register("Brightness", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(0)));

        public float Contrast
        {
            get { return (float)GetValue(ContrastProperty); }
            set { SetValue(ContrastProperty, value); }
        }

        public static readonly DependencyProperty ContrastProperty = DependencyProperty.Register("Contrast", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(1)));


        public float Red
        {
            get { return (float)GetValue(RedProperty); }
            set { SetValue(RedProperty, value); }
        }

        public static readonly DependencyProperty RedProperty = DependencyProperty.Register("Red", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(2)));

        public float Green
        {
            get { return (float)GetValue(GreenProperty); }
            set { SetValue(RedProperty, value); }
        }

        public static readonly DependencyProperty GreenProperty = DependencyProperty.Register("Green", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(3)));

        public float Blue
        {
            get { return (float)GetValue(BlueProperty); }
            set { SetValue(BlueProperty, value); }
        }

        public static readonly DependencyProperty BlueProperty = DependencyProperty.Register("Blue", typeof(double), typeof(BrightContrastEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(4)));

    }
}

XAML代码:

<Image Name="ViewedPhoto" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Stretch="Uniform"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
            <Image.Effect>
                <l:BrightContrastEffect 
                    Brightness="{Binding ElementName=bVal, Path=Value}"
                    Contrast="{Binding ElementName=cVal, Path=Value}"
                    Red="{Binding ElementName=rVal, Path=Value}"
                    Green="{Binding ElementName=gVal, Path=Value}"
                    Blue="{Binding ElementName=blVal, Path=Value}"
                    />
            </Image.Effect>
</Image>
<!-- Sliders for Reg, Green & Blue -->
<TextBlock Text="Red"/>
<Slider Maximum="1" Minimum="-1" Name="rVal" TickFrequency="1" TickPlacement="BottomRight"/>
<TextBox Text="{Binding ElementName=rVal, Path=Value, 
UpdateSourceTrigger=PropertyChanged}"  TextAlignment="Right" Width="30" />

<TextBlock Text="Green"/>
<Slider Maximum="1" Minimum="-1" Name="gVal" TickFrequency="1" TickPlacement="BottomRight"/>
<TextBox Text="{Binding ElementName=gVal, Path=Value, 
UpdateSourceTrigger=PropertyChanged}"  TextAlignment="Right" Width="30" />

<TextBlock Text="Blue"/>
<Slider Maximum="1" Minimum="-1" Name="blVal" TickFrequency="1" TickPlacement="BottomRight"/>
<TextBox Text="{Binding ElementName=blVal, Path=Value, 
UpdateSourceTrigger=PropertyChanged}"  TextAlignment="Right" Width="30" />

HLSL着色器代码:

float Brightness : register(C0);
float Contrast : register(C1);
float Red: register(C2);
float Green: register(C3);
float Blue: register(C4);

sampler2D Texture1Sampler : register(S0);

float4 main(float2 uv : TEXCOORD) : COLOR
{

    float4 pixelColor = tex2D(Texture1Sampler, uv);
    pixelColor.rgb /= pixelColor.a;

    // Apply contrast.
    pixelColor.rgb = ((pixelColor.rgb - 0.5f) * max(Contrast, 0)) + 0.5f;

    // Apply brightness.
    pixelColor.rgb += Brightness;

    // Return final pixel color.
    pixelColor.rgb *= pixelColor.a;

    pixelColor.r += Red;
    pixelColor.g += Green;
    pixelColor.b += Blue;

//return float4(Red, Green Blue, 1);
    return pixelColor;
}

1 个答案:

答案 0 :(得分:1)

根据Nico Schertler

的建议,这是答案
float Brightness : register(C0);
float Contrast : register(C1);
float Red: register(C2);
float Green: register(C3);
float Blue: register(C4);

sampler2D Texture1Sampler : register(S0);

float4 main(float2 uv : TEXCOORD) : COLOR
{

    float4 pixelColor = tex2D(Texture1Sampler, uv);
    pixelColor.rgb /= pixelColor.a;

    // Apply contrast.
    //pixelColor.rgb = ((pixelColor.rgb - 1.5f) * max(Contrast, 1)) + 1.5f;
    pixelColor.rgb = ((pixelColor.rgb - 0.5f) * max(Contrast+1, 0)) + 0.5f;

    // Apply brightness.
    pixelColor.rgb += Brightness;


    // Return final pixel color.
    pixelColor.rgb *= pixelColor.a;

    pixelColor.r += Red;
    pixelColor.g += Green;
    pixelColor.b += Blue;

//return float4(Red, Green Blue, 1);
    return pixelColor;
}