開発環境 |
Microsoft Visual Studio Express 2013 for Windows Desktop |
実行環境 |
Microsoft Windows 8.1 (64bit) |
プロジェクトの種類 |
Visual C#/WPF アプリケーション |
プロジェクト名 |
ShaderEffectDemo |
以下のファイルをプロジェクトに追加する。
sample.jpg |
任意の画像ファイル |
ThresholdEffect.cs |
クラス |
ThresholdEffect.fx |
|
ThresholdEffect.fx.ps |
ビルド アクション:Resource |
参考
MainWindow.xaml
<Window x:Class="ShaderEffectDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ShaderEffectDemo"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:ThresholdEffect x:Key="thresholdEffect" Threshold="0.25" BlankColor="Orange" />
</Window.Resources>
<Grid Effect="{StaticResource thresholdEffect}">
<Image Source="sample.jpg" />
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace ShaderEffectDemo
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
ThresholdEffect.cs
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
namespace ShaderEffectDemo
{
public class ThresholdEffect : ShaderEffect
{
private static PixelShader _pixelShader =
new PixelShader() { UriSource = MakePackUri("ThresholdEffect.fx.ps")};
public ThresholdEffect()
{
PixelShader = _pixelShader;
UpdateShaderValue(InputProperty);
UpdateShaderValue(ThresholdProperty);
UpdateShaderValue(BlankColorProperty);
}
private static Uri MakePackUri(string relativeFile)
{
Assembly a = typeof(ThresholdEffect).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(ThresholdEffect), 0);
//----------------------------------------------------------------------
public double Threshold
{
get { return (double)GetValue(ThresholdProperty); }
set { SetValue(ThresholdProperty, value); }
}
public static readonly DependencyProperty ThresholdProperty =
DependencyProperty.Register("Threshold", typeof(double), typeof(ThresholdEffect),
new UIPropertyMetadata(0.5, PixelShaderConstantCallback(0)));
//----------------------------------------------------------------------
public Color BlankColor
{
get { return (Color)GetValue(BlankColorProperty); }
set { SetValue(BlankColorProperty, value); }
}
public static readonly DependencyProperty BlankColorProperty =
DependencyProperty.Register("BlankColor", typeof(Color), typeof(ThresholdEffect),
new UIPropertyMetadata(Colors.Transparent, PixelShaderConstantCallback(1)));
}
}
ThresholdEffect.fx
sampler2D implicitInput : register(s0);
float threshold : register(c0);
float4 blankColor : register(c1);
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D(implicitInput, uv);
float intensity = (color.r + color.g + color.b) / 3;
float4 result;
if (intensity > threshold)
{
result = color;
}
else
{
result = blankColor;
}
return result;
}
makefx.bat
"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\fxc.exe"^
/T ps_2_0 /E main /Fo ThresholdEffect.fx.ps ThresholdEffect.fx
pause
最終更新:2014年05月30日 11:26