<UserControl x: Class = "SilverlightForHabr.MainPage"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns: d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
mc: Ignorable = "d"
d: DesignHeight = "550" d: DesignWidth = "700">
<Grid x: Name = "LayoutRoot" Background = "White">
<StackPanel>
<TextBlock Text = "Silverlight + Augmented reality + Shaders" HorizontalAlignment = "Center" />
<Grid Width = "640" Height = "480">
<Rectangle Name = "Viewport" Stroke = "Black" StrokeThickness = "2" />
<Canvas>
<Image Name = "Logo" Source = "habr.png" Width = "300" Height = "300" />
</ Canvas>
</ Grid>
<Button Content = "Let's start" Padding = "5" Margin = "5" HorizontalAlignment = "Center" />
</ StackPanel>
</ Grid>
</ UserControl>
Loaded = "UserControl_Loaded"> and <Button Content = "Let's start" Padding = "5" Margin = "5" HorizontalAlignment = "Center" Click = "onStartBtnClick" />
private void UserControl_Loaded (object sender, RoutedEventArgs e)
{
captureSource = new CaptureSource ();
captureSource.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice (); // bind to the camera by default
var vidBrush = new VideoBrush ();
vidBrush.SetSource (captureSource); // for the brush, specify the video from the camera as the source
Viewport.Fill = vidBrush; // paint on our rectangle
}
private void onStartBtnClick (object sender, RoutedEventArgs e)
{
if (CaptureDeviceConfiguration.RequestDeviceAccess ()) // ask the user whether it is possible to work with the camera
{
captureSource.Start (); // if yes, then Let's go!
}
}
private void UserControl_Loaded (object sender, RoutedEventArgs e)
{
captureSource = new CaptureSource ();
captureSource.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice ();
var vidBrush = new VideoBrush ();
vidBrush.SetSource (captureSource);
Viewport.Fill = vidBrush;
arDetector = new CaptureSourceMarkerDetector ();
var marker = Marker.LoadFromResource ("Marker_L_16x16segments_80width.pat", 16, 16, 80);
arDetector.Initialize (captureSource, 1, 4000, marker);
arDetector.MarkersDetected + = (s, me) =>
{
Dispatcher.BeginInvoke (() =>
{
var dr = me.DetectionResults;
if (dr.HasResults)
{
var centerAtOrigin = Matrix3DFactory.CreateTranslation (-Logo.ActualWidth * 0.5, -Logo.ActualHeight * 0.5, 0);
var scale = Matrix3DFactory.CreateScale (0.5, -0.5, 0.5);
var world = centerAtOrigin * scale * dr [0] .Transformation;
var vp = Matrix3DFactory.CreateViewportTransformation (Viewport.ActualWidth, Viewport.ActualHeight);
var m = Matrix3DFactory.CreateViewportProjection (world, Matrix3D.Identity, arDetector.Projection, vp);
Logo.Projection = new Matrix3DProjection {ProjectionMatrix = m};
}
});
};
}

/// <class> ZoomBlurEffect </ class>
/// <description> An effect that applies a radial blur to the input. </ description>
// ------------------------------------------------ -----------------------------------------
// Shader constant register mappings (scalars - float, double, Point, Color, Point3D, etc.)
// ------------------------------------------------ -----------------------------------------
/// <summary> The center of the blur. </ summary>
/// <minValue> 0 </ minValue>
/// <maxValue> 0.2 </ maxValue>
/// <defaultValue> 0.9,0.6 </ defaultValue>
float2 Center: register (C0);
/// <summary> The amount of blur. </ summary>
/// <minValue> 0 </ minValue>
/// <maxValue> 0.2 </ maxValue>
/// <defaultValue> 0.1 </ defaultValue>
float BlurAmount: register (C1);
// ------------------------------------------------ --------------------------------------
// Sampler Inputs (Brushes, including ImplicitInput)
// ------------------------------------------------ --------------------------------------
sampler2D inputSource: register (S0);
// ------------------------------------------------ --------------------------------------
// Pixel Shader
// ------------------------------------------------ --------------------------------------
float4 main (float2 uv: TEXCOORD): COLOR
{
float4 c = 0;
uv - = Center;
for (int i = 0; i <15; i ++)
{
float scale = 1.0 + BlurAmount * (i / 14.0);
c + = tex2D (inputSource, uv * scale + Center);
}
c / = 15;
return c;
}
<Button Content = "Enable Shader" Name = "ShadersBtn" Padding = "5" HorizontalAlignment = "Center" Click = "SwitchOnShaders" />
private void SwitchOnShaders (object sender, RoutedEventArgs e)
{
if (Viewport.Effect == zb)
{
Viewport.Effect = null;
ShadersBtn.Content = "Enable Shader";
return;
}
zb.Center = new Point ();
zb.BlurAmount = 0.2;
Viewport.Effect = zb;
ShadersBtn.Content = "Disable Shader";
}
Source: https://habr.com/ru/post/111178/
All Articles