private void Initialize() { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _frostEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = 0.0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter("backdropBrush") }; // Create an instance of the effect and set its source to a CompositionBackdropBrush _effectFactory = _compositor.CreateEffectFactory(_frostEffect); _backdropBrush = _compositor.CreateHostBackdropBrush(); _effectBrush = _effectFactory.CreateBrush(); // Create a Visual to contain the frosted glass effect _frostVisual = _compositor.CreateSpriteVisual(); _frostVisual.Brush = _effectBrush; _frostVisual.Size = new Vector2((float)ActualWidth, (float)ActualHeight); _effectBrush.SetSourceParameter("backdropBrush", _backdropBrush); // Add the blur as a child of the host in the visual tree ElementCompositionPreview.SetElementChildVisual(this, _frostVisual); }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = _compositor.CreateCompositionGenerator(); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; _center = new Vector2(150f); // Create the combined geometry _ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); _ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); var combinedGeometry = _ellipse1.CombineWith(_ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); _outerGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((RenderGrid1.ActualWidth - _width) / 2).ToSingle(), ((RenderGrid1.ActualHeight - _height) / 2).ToSingle(), 0); _animatedMaskSurface = _generator.CreateMaskSurface(_visual1.Size.ToSize(), excludedGeometry); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(_animatedMaskSurface); var source = _compositor.CreateColorBrush(Color.FromArgb(255, 192, 0, 0)); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _visual1.Brush = maskBrush; ElementCompositionPreview.SetElementChildVisual(RenderGrid1, _visual1); // Create custom shaped visual using CompositionMaskBrush _visual2 = _compositor.CreateSpriteVisual(); _visual2.Size = new Vector2(_width, _height); _visual2.Brush = _compositor.CreateMaskedBackdropBrush(_animatedMaskSurface, Colors.AntiqueWhite, 10, _backdropBrush); _container = _compositor.CreateContainerVisual(); _container.Size = new Vector2(_width, _height); _container.Offset = new Vector3(((RenderGrid2.ActualWidth - _width) / 2).ToSingle(), ((RenderGrid2.ActualHeight - _height) / 2).ToSingle(), 0); var bgVisual = _compositor.CreateSpriteVisual(); bgVisual.Size = _container.Size * 0.6f; bgVisual.Brush = _compositor.CreateColorBrush(Colors.Maroon); bgVisual.Offset = new Vector3(_container.Size * 0.2f, 0); _container.Children.InsertAtTop(bgVisual); _container.Children.InsertAtTop(_visual2); ElementCompositionPreview.SetElementChildVisual(RenderGrid2, _container); }
private void applyBlurBackDrop(Panel panel) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _blurSprite = _compositor.CreateSpriteVisual(); _blurSprite.Size = new Vector2((float)panel.ActualWidth, (float)panel.ActualHeight); ElementCompositionPreview.SetElementChildVisual(panel, _blurSprite); // Create an effect description GaussianBlurEffect blurEffect = new GaussianBlurEffect() { Name = "Blur", BlurAmount = 2.0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced }; blurEffect.Source = new CompositionEffectSourceParameter("source"); CompositionEffectFactory blurEffectFactory = _compositor.CreateEffectFactory(blurEffect); CompositionEffectBrush blurBrush = blurEffectFactory.CreateBrush(); // Create a BackdropBrush and bind it to the EffectSourceParameter “source” CompositionBackdropBrush backdropBrush = _compositor.CreateBackdropBrush(); blurBrush.SetSourceParameter("source", backdropBrush); _blurSprite.Brush = blurBrush; }
public static async Task <AttachedAnimatableCompositionEffect <T> > AttachCompositionAnimatableInAppCustomAcrylicEffectAsync <TSource, T>( [NotNull] this TSource element, [NotNull] T target, float on, float off, bool initiallyVisible, Color color, float colorMix, [CanBeNull] CanvasControl canvas, [NotNull] Uri uri, int timeThreshold = 1000, bool reload = false, bool disposeOnUnload = false) where TSource : FrameworkElement where T : FrameworkElement { // Get the compositor Visual visual = await DispatcherHelper.GetFromUIThreadAsync(element.GetVisual); Compositor compositor = visual.Compositor; // Create the blur effect and the effect factory CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush(); GaussianBlurEffect blurEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = 0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter(nameof(backdropBrush)) }; const String animationPropertyName = "Blur.BlurAmount"; // Prepare the dictionary with the parameters to add IDictionary <String, CompositionBrush> sourceParameters = new Dictionary <String, CompositionBrush> { { nameof(backdropBrush), backdropBrush } }; // Get the noise brush using Win2D IGraphicsEffect source = await AcrylicEffectHelper.ConcatenateEffectWithTintAndBorderAsync(compositor, blurEffect, sourceParameters, color, colorMix, canvas, uri, timeThreshold, reload); // Make sure the Win2D brush was loaded correctly CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(source, new[] { animationPropertyName }); // Create the effect factory and apply the final effect CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); foreach (KeyValuePair <String, CompositionBrush> pair in sourceParameters) { effectBrush.SetSourceParameter(pair.Key, pair.Value); } // Assign the effect to a brush and display it SpriteVisual sprite = compositor.CreateSpriteVisual(); sprite.Brush = effectBrush; await AddToTreeAndBindSizeAsync(target.GetVisual(), target, sprite); if (initiallyVisible) { await DispatcherHelper.RunOnUIThreadAsync(() => element.Opacity = 1); } return(new AttachedAnimatableCompositionEffect <T>(target, sprite, new CompositionAnimationParameters(animationPropertyName, on, off), disposeOnUnload)); }
protected override void OnConnected() { Compositor compositor = Window.Current.Compositor; // CompositionCapabilities: Are Effects supported? bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported(); if (usingFallback) { // If Effects are not supported, use Fallback Solid Color CompositionBrush = compositor.CreateColorBrush(FallbackColor); return; } // Define Effect graph var graphicsEffect = new BlendEffect { Mode = BlendEffectMode.LinearBurn, Background = new ColorSourceEffect() { Name = "Tint", Color = Windows.UI.Colors.Silver, }, Foreground = new GaussianBlurEffect() { Name = "Blur", Source = new CompositionEffectSourceParameter("Backdrop"), BlurAmount = 0, BorderMode = EffectBorderMode.Hard, } }; // Create EffectFactory and EffectBrush CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" }); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); // Create BackdropBrush CompositionBackdropBrush backdrop = compositor.CreateBackdropBrush(); effectBrush.SetSourceParameter("backdrop", backdrop); // Trivial looping animation to demonstrate animated effects TimeSpan _duration = TimeSpan.FromSeconds(5); ScalarKeyFrameAnimation blurAnimation = compositor.CreateScalarKeyFrameAnimation(); blurAnimation.InsertKeyFrame(0, 0); blurAnimation.InsertKeyFrame(0.5f, 10f); blurAnimation.InsertKeyFrame(1, 0); blurAnimation.IterationBehavior = AnimationIterationBehavior.Forever; blurAnimation.Duration = _duration; effectBrush.Properties.StartAnimation("Blur.BlurAmount", blurAnimation); // Set EffectBrush to paint Xaml UIElement CompositionBrush = effectBrush; }
private void BackgroundController_AcrylicPresenterWasSet(object sender, EventArgs e) { Visual ElementVisual = ElementCompositionPreview.GetElementVisual(CompositionAcrylicPresenter); Compositor VisualCompositor = ElementVisual.Compositor; CompositionBackdropBrush BackdropBrush = VisualCompositor.CreateHostBackdropBrush(); float AcrylicAmount; if (double.TryParse(Convert.ToString(ApplicationData.Current.LocalSettings.Values["BackgroundTintLuminosity"]), out double TintLuminosity)) { AcrylicAmount = Convert.ToSingle(TintLuminosity); } else { AcrylicAmount = 0.8f; } GaussianBlurEffect BlurEffect = new GaussianBlurEffect() { BlurAmount = 10f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new ArithmeticCompositeEffect { Name = "Mix", MultiplyAmount = 0, Source1 = new CompositionEffectSourceParameter("backdropBrush"), Source2 = new ColorSourceEffect { Name = "Tint", Color = (Color)AcrylicBackgroundBrush.GetValue(AcrylicBrush.TintColorProperty) }, Source1Amount = AcrylicAmount, Source2Amount = 1 - AcrylicAmount } }; CompositionAcrylicBrush = VisualCompositor.CreateEffectFactory(BlurEffect, new[] { "Mix.Source1Amount", "Mix.Source2Amount", "Tint.Color" }).CreateBrush(); CompositionAcrylicBrush.SetSourceParameter("backdropBrush", BackdropBrush); SpriteVisual SpVisual = VisualCompositor.CreateSpriteVisual(); SpVisual.Brush = CompositionAcrylicBrush; ElementCompositionPreview.SetElementChildVisual(CompositionAcrylicPresenter, SpVisual); ExpressionAnimation bindSizeAnimation = VisualCompositor.CreateExpressionAnimation("ElementVisual.Size"); bindSizeAnimation.SetReferenceParameter("ElementVisual", ElementVisual); SpVisual.StartAnimation("Size", bindSizeAnimation); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(TintLuminosityOpacity))); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(AcrylicColor))); }
public GlassPanel() { this.InitializeComponent(); hostVisual = ElementCompositionPreview.GetElementVisual(GlassHost); compositor = hostVisual.Compositor; // Create a Visual to contain the frosted glass effect glassVisual = compositor.CreateSpriteVisual(); backdropBrush = compositor.CreateBackdropBrush(); }
public static async ValueTask <AttachedStaticCompositionEffect <T> > AttachCompositionHostBackdropBlurEffect <T>( [NotNull] this T element, bool disposeOnUnload = false) where T : FrameworkElement { // Setup the host backdrop effect Visual visual = ElementCompositionPreview.GetElementVisual(element); Compositor compositor = visual.Compositor; CompositionBackdropBrush brush = compositor.CreateHostBackdropBrush(); SpriteVisual sprite = compositor.CreateSpriteVisual(); sprite.Brush = brush; await AddToTreeAndBindSizeAsync(visual, element, sprite); return(new AttachedStaticCompositionEffect <T>(element, sprite, disposeOnUnload)); }
private void DisconnectAcryilicBrush() { if (backdropBrush != null) { backdropBrush.Dispose(); backdropBrush = null; } if (CompositionBrush != null) { CompositionBrush.Dispose(); CompositionBrush = null; } }
override protected void OnConnected() { Compositor compositor = Window.Current.Compositor; // CompositionCapabilities: Are HostBackdrop Effects supported? bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsFast(); if (usingFallback) { // If Effects are not supported, use Fallback Solid Color CompositionBrush = compositor.CreateColorBrush(FallbackColor); return; } // Define Effect graph var graphicsEffect = new BlendEffect { Mode = BlendEffectMode.Overlay, Background = new CompositionEffectSourceParameter("Backdrop"), Foreground = new ColorSourceEffect() { Name = "OverlayColor", Color = OverlayColor, }, }; // Create EffectFactory and EffectBrush CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "OverlayColor.Color" }); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); // Create HostBackdropBrush, a kind of BackdropBrush that provides blurred backdrop content CompositionBackdropBrush hostBrush = compositor.CreateHostBackdropBrush(); effectBrush.SetSourceParameter("Backdrop", hostBrush); // Set EffectBrush as the brush that XamlCompBrushBase paints onto Xaml UIElement CompositionBrush = effectBrush; // When the Window loses focus, animate HostBackdrop to FallbackColor Window.Current.CoreWindow.Activated += CoreWindow_Activated; // Configure color animation to for state change _stateChangeAnimation = compositor.CreateColorKeyFrameAnimation(); _stateChangeAnimation.InsertKeyFrame(0, OverlayColor); _stateChangeAnimation.InsertKeyFrame(1, FallbackColor); _stateChangeAnimation.Duration = TimeSpan.FromSeconds(1); }
protected override void OnConnected() { Compositor compositor = Window.Current.Compositor; // CompositionCapabilities: Are Effects supported? bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported(); FallbackColor = Color.FromArgb(100, 100, 100, 100); if (usingFallback) { // If Effects are not supported, use Fallback Solid Color CompositionBrush = compositor.CreateColorBrush(FallbackColor); return; } // Define Effect graph var graphicsEffect = new BlendEffect { Mode = BlendEffectMode.SoftLight, Background = new ColorSourceEffect() { Name = "Tint", //Color = Color.FromArgb(180, 255, 255, 255), Color = Color.FromArgb(150, 255, 255, 255), }, Foreground = new GaussianBlurEffect() { Name = "Blur", Source = new CompositionEffectSourceParameter("Backdrop"), BlurAmount = 20, BorderMode = EffectBorderMode.Hard, } }; // Create EffectFactory and EffectBrush CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); // Create BackdropBrush CompositionBackdropBrush backdrop = compositor.CreateBackdropBrush(); effectBrush.SetSourceParameter("Backdrop", backdrop); // Set EffectBrush as the brush that XamlCompBrushBase paints onto Xaml UIElement CompositionBrush = effectBrush; }
private void InitializeComponentUIElement1(UIElement uIElement) { Visual hostVisual = ElementCompositionPreview.GetElementVisual(uIElement); Compositor compositor = hostVisual.Compositor; string name = "backdropBrush"; GaussianBlurEffect glassEffect = new GaussianBlurEffect() { BlurAmount = 20, BorderMode = EffectBorderMode.Hard, Source = new ArithmeticCompositeEffect { //是否使用混合模糊,value=1时加入前景模糊,value=0时不加入前景模糊 MultiplyAmount = 1, //设置背景模糊程度 Source1Amount = 0f, //设置前景模糊程度 Source2Amount = 1f, //加入关联的名称 Source1 = new CompositionEffectSourceParameter(name), //设置前景的颜色 Source2 = new ColorSourceEffect { Color = Color.FromArgb(150, 255, 255, 255) } } }; var effectFactory = compositor.CreateEffectFactory(glassEffect); //创建渲染目标实例 CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush(); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter(name, backdropBrush); SpriteVisual glassVisual = compositor.CreateSpriteVisual(); glassVisual.Brush = effectBrush; ElementCompositionPreview.SetElementChildVisual(uIElement, glassVisual); var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); glassVisual.StartAnimation("Size", bindSizeAnimation); }
public static void InitializeBlur(UIElement uiElement) { Visual hostVisual = ElementCompositionPreview.GetElementVisual(uiElement); Compositor compositor = hostVisual.Compositor; string name = "backdropBrush"; GaussianBlurEffect gausianBlur = new GaussianBlurEffect() { BlurAmount = 30f, BorderMode = EffectBorderMode.Hard, Source = new ArithmeticCompositeEffect() { MultiplyAmount = 0, Source1Amount = 1f, Source1 = new CompositionEffectSourceParameter(name), Source2Amount = 0f, Source2 = new ColorSourceEffect() { Color = Color.FromArgb(255, 245, 245, 245) } } }; CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(gausianBlur); CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush(); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter(name, backdropBrush); SpriteVisual glassVisual = compositor.CreateSpriteVisual(); glassVisual.Brush = effectBrush; ElementCompositionPreview.SetElementChildVisual(uiElement, glassVisual); ExpressionAnimation bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); glassVisual.StartAnimation("Size", bindSizeAnimation); effectFactory.Dispose(); gausianBlur.Dispose(); backdropBrush.Dispose(); }
private void BlurifyWithBackdrop() { try { _compositor = ElementCompositionPreview.GetElementVisual(_element).Compositor; GaussianBlurEffect blurEffect = new GaussianBlurEffect() { Name = "Blur", BlurAmount = 3.0f, BorderMode = EffectBorderMode.Soft, Optimization = EffectOptimization.Balanced }; blurEffect.Source = new CompositionEffectSourceParameter("source"); CompositionEffectFactory blurEffectFactory = _compositor.CreateEffectFactory(blurEffect); CompositionEffectBrush blurBrush = blurEffectFactory.CreateBrush(); CompositionBackdropBrush backdropBrush = _compositor.CreateBackdropBrush(); blurBrush.SetSourceParameter("source", backdropBrush); _hostSprite = _compositor.CreateSpriteVisual(); _hostSprite.Size = new Vector2((float)_element.ActualWidth, (float)_element.ActualHeight); _hostSprite.Brush = blurBrush; _element.SizeChanged += (sender, args) => { _hostSprite.Size = new Vector2((float)_element.ActualWidth, (float)_element.ActualHeight); }; ElementCompositionPreview.SetElementChildVisual(_element, _hostSprite); } catch (Exception) { //not CU } }
public static async Task <AttachedStaticCompositionEffect <T> > AttachCompositionInAppCustomAcrylicEffectAsync <TSource, T>( [NotNull] this TSource element, [NotNull] T target, float blur, int ms, Color color, float colorMix, float?saturation, [CanBeNull] CanvasControl canvas, [NotNull] Uri uri, int timeThreshold = 1000, bool reload = false, bool fadeIn = false, bool disposeOnUnload = false) where TSource : FrameworkElement where T : FrameworkElement { // Percentage check if (saturation < 0 || saturation > 1) { throw new ArgumentOutOfRangeException("The input saturation value must be in the [0,1] range"); } if (colorMix <= 0 || colorMix >= 1) { throw new ArgumentOutOfRangeException("The mix factors must be in the [0,1] range"); } if (timeThreshold <= 0) { throw new ArgumentOutOfRangeException("The time threshold must be a positive number"); } // Setup the compositor Visual visual = ElementCompositionPreview.GetElementVisual(element); Compositor compositor = visual.Compositor; // Prepare a luminosity to alpha effect to adjust the background contrast CompositionBackdropBrush backdropBrush = compositor.CreateBackdropBrush(); const String blurName = "Blur", blurParameterName = "Blur.BlurAmount"; GaussianBlurEffect blurEffect = new GaussianBlurEffect { Name = blurName, BlurAmount = 0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter(nameof(backdropBrush)) }; // Background with blur and tint overlay IDictionary <String, CompositionBrush> sourceParameters = new Dictionary <String, CompositionBrush> { { nameof(backdropBrush), backdropBrush } }; // Get the noise brush using Win2D IGraphicsEffect source = await AcrylicEffectHelper.ConcatenateEffectWithTintAndBorderAsync(compositor, blurEffect, sourceParameters, color, colorMix, canvas, uri, timeThreshold, reload); // Add the final saturation effect if needed if (saturation != null) { SaturationEffect saturationEffect = new SaturationEffect { Saturation = saturation.Value, Source = source }; source = saturationEffect; } // Make sure the Win2D brush was loaded correctly CompositionEffectFactory factory = compositor.CreateEffectFactory(source, new[] { blurParameterName }); // Create the effect factory and apply the final effect CompositionEffectBrush effectBrush = factory.CreateBrush(); foreach (KeyValuePair <String, CompositionBrush> pair in sourceParameters) { effectBrush.SetSourceParameter(pair.Key, pair.Value); } // Create the sprite to display and add it to the visual tree SpriteVisual sprite = compositor.CreateSpriteVisual(); sprite.Brush = effectBrush; // Assign the visual if (fadeIn) { sprite.StopAnimation("Opacity"); sprite.Opacity = 0; } await AddToTreeAndBindSizeAsync(target.GetVisual(), target, sprite); if (fadeIn) { // Fade the effect in ScalarKeyFrameAnimation opacityAnimation = sprite.Compositor.CreateScalarKeyFrameAnimation(1, 0, TimeSpan.FromMilliseconds(ms), null, sprite.GetEasingFunction(EasingFunctionNames.Linear)); sprite.StartAnimation("Opacity", opacityAnimation); } // Animate the blur and return the result effectBrush.StartAnimationAsync(blurParameterName, blur, TimeSpan.FromMilliseconds(ms)).Forget(); return(new AttachedStaticCompositionEffect <T>(target, sprite, disposeOnUnload)); }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.Single(), CompositionGrid1.ActualHeight.Single()); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.45f, _height * 0.45f); _bgVisual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); _bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); container1.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var compositionMask = _generator.CreateMask(_visual1.Size.ToSize(), ellipseGeometry); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateMaskedBackdropBrush(compositionMask, Colors.AntiqueWhite, 30f, _backdropBrush); container1.Children.InsertAtTop(_visual1); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, container1); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.Single(), CompositionGrid2.ActualHeight.Single()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).Single(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).Single(), 0); //var radians = ((45f * Math.PI) / 180).Single(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMask(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }
private async void MainPage_Loaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionMaskFactory.GetCompositionMaskGenerator(_compositor); _backdropBrush = _compositor.CreateBackdropBrush(); //Create the visual _visual = _compositor.CreateSpriteVisual(); _visual.Size = new Vector2(400, 400); _visual.Offset = new Vector3(200, 0, 0); // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, 200, 200, 150, 75); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, 200, 200, 75, 150); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the CompositionMask _compositionMask = await _generator.CreateMaskAsync(_visual.Size.ToSize(), _combinedGeometry); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(_compositionMask.Surface); var source1 = _compositor.CreateColorBrush(Colors.Blue); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source1; _visual.Brush = maskBrush; ElementCompositionPreview.SetElementChildVisual(CanvasCtrl, _visual); var container = _compositor.CreateContainerVisual(); container.Size = new Vector2(1000, 1000); // Background visual var bgVisual = _compositor.CreateSpriteVisual(); bgVisual.Size = new Vector2(200, 200); bgVisual.Offset = new Vector3(300, 100, 0); bgVisual.Brush = source1; container.Children.InsertAtTop(bgVisual); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(400, 400); _animatedVisual.Offset = new Vector3(200, 0, 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, 400, 400); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = await _generator.CreateMaskAsync(_animatedVisual.Size.ToSize(), excludedGeometry); //// Create SurfaceBrush from CompositionMask //var animatedMask = _compositor.CreateSurfaceBrush(_animatedCompositionMask.Surface); //var source2 = _compositor.CreateColorBrush(Colors.Red); //// Create mask brush //var animatedMaskBrush = _compositor.CreateMaskBrush(); //animatedMaskBrush.Mask = animatedMask; //animatedMaskBrush.Source = source2; _animatedVisual.Brush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Color.FromArgb(240, 232, 232, 232), 20f, _backdropBrush); container.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(AnimatedCanvasCtrl, container); }
public static async Task <AttachedStaticCompositionEffect <T> > AttachCompositionCustomAcrylicEffectAsync <T>( [NotNull] this T element, Color color, float colorMix, [CanBeNull] CanvasControl canvas, [NotNull] Uri uri, int timeThreshold = 1000, bool reload = false, bool disposeOnUnload = false) where T : FrameworkElement { // Percentage check if (colorMix <= 0 || colorMix >= 1) { throw new ArgumentOutOfRangeException("The mix factors must be in the [0,1] range"); } if (timeThreshold <= 0) { throw new ArgumentOutOfRangeException("The time threshold must be a positive number"); } // Setup the compositor Visual visual = ElementCompositionPreview.GetElementVisual(element); Compositor compositor = visual.Compositor; // Prepare a luminosity to alpha effect to adjust the background contrast CompositionBackdropBrush hostBackdropBrush = compositor.CreateHostBackdropBrush(); CompositionEffectSourceParameter backgroundParameter = new CompositionEffectSourceParameter(nameof(hostBackdropBrush)); LuminanceToAlphaEffect alphaEffect = new LuminanceToAlphaEffect { Source = backgroundParameter }; OpacityEffect opacityEffect = new OpacityEffect { Source = alphaEffect, Opacity = 0.4f // Reduce the amount of the effect to avoid making bright areas completely black }; // Layer [0,1,3] - Desktop background with blur and tint overlay BlendEffect alphaBlend = new BlendEffect { Background = backgroundParameter, Foreground = opacityEffect, Mode = BlendEffectMode.Overlay }; IDictionary <String, CompositionBrush> sourceParameters = new Dictionary <String, CompositionBrush> { { nameof(hostBackdropBrush), hostBackdropBrush } }; // Get the noise brush using Win2D IGraphicsEffect source = await AcrylicEffectHelper.ConcatenateEffectWithTintAndBorderAsync(compositor, alphaBlend, sourceParameters, color, colorMix, canvas, uri, timeThreshold, reload); // Make sure the Win2D brush was loaded correctly CompositionEffectFactory factory = compositor.CreateEffectFactory(source); // Create the effect factory and apply the final effect CompositionEffectBrush effectBrush = factory.CreateBrush(); foreach (KeyValuePair <String, CompositionBrush> pair in sourceParameters) { effectBrush.SetSourceParameter(pair.Key, pair.Value); } // Create the sprite to display and add it to the visual tree SpriteVisual sprite = compositor.CreateSpriteVisual(); sprite.Brush = effectBrush; await AddToTreeAndBindSizeAsync(visual, element, sprite); return(new AttachedStaticCompositionEffect <T>(element, sprite, disposeOnUnload)); }
protected override void OnConnected() { Compositor compositor = Window.Current.Compositor; // CompositionCapabilities: Are Effects supported? bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported(); FallbackColor = Color.FromArgb(100, 100, 100, 100); if (usingFallback) { // If Effects are not supported, use Fallback Solid Color CompositionBrush = compositor.CreateColorBrush(FallbackColor); return; } // Define Effect graph // COLOR SWAP Matrix5x4 mat = new Matrix5x4() { M11 = 0, M12 = 0f, M13 = 1f, M14 = 0, M21 = 0f, M22 = 1f, M23 = 0f, M24 = 0, M31 = 1f, M32 = 0f, M33 = 0f, M34 = 0, M41 = 0f, M42 = 0f, M43 = 0f, M44 = 1, M51 = 0, M52 = 0, M53 = 0, M54 = 0 }; //// COLOR MASK (DOESNT WORK ???) // Matrix5x4 mat = new Matrix5x4() // { // M11 = 1, M12 = 0f, M13 = 0f, M14 = 2, // M21 = 0f, M22 = 1f, M23 = 0f, M24 = -1, // M31 = 0f, M32 = 0f, M33 = 1f, M34 = -1, // M41 = 0f, M42 = 0f, M43 = 0f, M44 = 0, // M51 = 0, M52 = 0, M53 = 0, M54 = 0 // }; //// REDFILTER //Matrix5x4 mat = new Matrix5x4() // { // M11 = 1, M12 = 0f, M13 = 0f, M14 = 0, // M21 = 0f, M22 = 0f, M23 = 0f, M24 = 0, // M31 = 0f, M32 = 0f, M33 = 0f, M34 = 0, // M41 = 0f, M42 = 0f, M43 = 0f, M44 = 1, // M51 = 0, M52 = 0, M53 = 0, M54 = 0 // }; IGraphicsEffect graphicsEffect = new ColorMatrixEffect() { ColorMatrix = mat, Source = new CompositionEffectSourceParameter("ImageSource") }; //// COLORHIGHLIGHT //IGraphicsEffect graphicsEffect = new ArithmeticCompositeEffect() //{ // MultiplyAmount = 0, // Source1Amount = 1, // Source2Amount = 1, // Source1 = new GammaTransferEffect() // { // RedExponent = 7, // BlueExponent = 7, // GreenExponent = 7, // RedAmplitude = 3, // GreenAmplitude = 3, // BlueAmplitude = 3, // Source = new CompositionEffectSourceParameter("ImageSource") // }, // Source2 = new SaturationEffect() // { // Saturation = 0, // Source = new CompositionEffectSourceParameter("ImageSource") // } //}; // Create the effect factory and instantiate a brush CompositionEffectFactory _effectFactory = compositor.CreateEffectFactory(graphicsEffect, null); CompositionEffectBrush effectBrush = _effectFactory.CreateBrush(); // Create BackdropBrush CompositionBackdropBrush backdrop = compositor.CreateHostBackdropBrush(); effectBrush.SetSourceParameter("ImageSource", backdrop); // Set EffectBrush as the brush that XamlCompBrushBase paints onto Xaml UIElement CompositionBrush = effectBrush; }
/// <summary> /// Creates a new wrapper instance with the given parameters /// </summary> /// <param name="pipeline">The current effects pipeline</param> /// <param name="brush">The host backdrop brush instance</param> public HostBackdropInstanceWrapper(IGraphicsEffectSource pipeline, CompositionBackdropBrush brush) { Pipeline = pipeline; Brush = brush; }
private void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = _compositor.CreateCompositionGenerator(); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.ToSingle(), CompositionGrid1.ActualHeight.ToSingle()); var parentVisual = _compositor.CreateSpriteVisual(); parentVisual.Size = new Vector2(_width, _height); parentVisual.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).ToSingle(), ((CompositionGrid1.ActualHeight - _height) / 2).ToSingle(), 0); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.5f, _height * 0.5f); _bgVisual1.Offset = new Vector3(((parentVisual.Size.X - _width * 0.5f) / 2f), ((parentVisual.Size.Y - _height * 0.5f) / 2f), 0f); //_bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); // Create the CompositionMask var gaussianMask = _generator.CreateGaussianMaskSurface(); // Create SurfaceBrush from CompositionMask var mask = _compositor.CreateSurfaceBrush(gaussianMask.Surface); //mask = ((CompositionBrush)_compositor.CreateGaussianMaskedBackdropBrush(gaussianMask, Colors.AntiqueWhite, 10, _backdropBrush)); var source = _compositor.CreateColorBrush(Colors.DarkOrange); // Create mask brush var maskBrush = _compositor.CreateMaskBrush(); maskBrush.Mask = mask; maskBrush.Source = source; _bgVisual1.Brush = maskBrush; parentVisual.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); //_visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).ToSingle(), // ((CompositionGrid1.ActualHeight - _height) / 2).ToSingle(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var rectGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 50, 50, _width - 50, _height - 50); var compositionMask = _generator.CreateGaussianMaskSurface(_visual1.Size.ToSize(), ellipseGeometry, new Vector2(20), 20); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateGaussianMaskedBackdropBrush(compositionMask, Colors.Transparent, 20f, _backdropBrush); parentVisual.Children.InsertAtTop(_visual1); var visual = _compositor.CreateSpriteVisual(); visual.Size = new Vector2(400, 400); visual.Offset = new Vector3(((3 * CompositionGrid1.ActualWidth / 4f) - (_width / 2)).ToSingle(), ((3 * CompositionGrid1.ActualHeight / 4f) - (_height / 2)).ToSingle(), 0); var roundRectGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width / 8f, _height / 8f); var maskSurface = _generator.CreateMaskSurface(visual.Size.ToSize(), roundRectGeometry); var frostedBrush = _compositor.CreateFrostedGlassBrush(maskSurface, Colors.DarkGray, 30f, _backdropBrush); var shadow = _compositor.CreateDropShadow(); shadow.Opacity = 0.5f; shadow.Color = Colors.Black; shadow.Offset = new Vector3(10, 10, 0); shadow.BlurRadius = 15; shadow.Mask = frostedBrush.GetSourceParameter("mask"); visual.Brush = frostedBrush; visual.Shadow = shadow; container1.Children.InsertAtTop(visual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, parentVisual); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.ToSingle(), CompositionGrid2.ActualHeight.ToSingle()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).ToSingle(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).ToSingle(), 0); //var radians = ((45f * Math.PI) / 180).ToSingle(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).ToSingle(), ((CompositionGrid2.ActualHeight - _height) / 2).ToSingle(), 0); // Create the Excluded geometry _outerGeometry = CanvasObject.CreateSquircle(_generator.Device, 0, 0, _width, _height, _width / 8f, _height / 8f); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMaskSurface(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }
private void ConnectAcrylicBrush(bool useFallback) { DisconnectAcryilicBrush(); bool isWindowed = BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop; if (useFallback == false && PowerManager.EnergySaverStatus != EnergySaverStatus.On && !(UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Touch && isWindowed)) { ArithmeticCompositeEffect crossFadeEffect = new ArithmeticCompositeEffect { MultiplyAmount = 0f, Source1Amount = 0.6f, Source2Amount = 0.4f, Source1 = new ColorSourceEffect { Color = TintColor }, Source2 = new ArithmeticCompositeEffect { MultiplyAmount = 0f, Source1Amount = sc_noiseOpacity, Source2Amount = 0.98f, Source1 = new BorderEffect { Source = new CompositionEffectSourceParameter("image"), ExtendX = CanvasEdgeBehavior.Wrap, ExtendY = CanvasEdgeBehavior.Wrap, }, Source2 = new BlendEffect { Mode = BlendEffectMode.Exclusion, Foreground = new ColorSourceEffect { Color = Sc_exclusionColor }, Background = new SaturationEffect { Saturation = sc_saturation, Source = new GaussianBlurEffect { BlurAmount = sc_blurRadius, BorderMode = EffectBorderMode.Hard, Name = "Blur", Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter("source") } } } } }; CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(crossFadeEffect); effectBrush = effectFactory.CreateBrush(); LoadedImageSurface imagesurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///UnitedCodebase/Assets/NoiseAsset_256X256_PNG.png")); CompositionSurfaceBrush imagebrush = compositor.CreateSurfaceBrush(imagesurface); imagebrush.Stretch = CompositionStretch.None; effectBrush.SetSourceParameter("image", imagebrush); if (BackgroundSource == CustomAcrylicBackgroundSource.Hostbackdrop) { backdropBrush = compositor.CreateHostBackdropBrush(); effectBrush.SetSourceParameter("source", backdropBrush); } else { backdropBrush = compositor.CreateBackdropBrush(); effectBrush.SetSourceParameter("source", backdropBrush); } CompositionBrush = effectBrush; } else { CompositionBrush = compositor.CreateColorBrush(FallbackColor); } }
private async void OnLoaded(object sender, RoutedEventArgs e) { _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor); _backdropBrush = _compositor.CreateBackdropBrush(); _width = 300f; _height = 300f; // Create the combined geometry var ellipse1 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.45f * _width, 0.225f * _height); var ellipse2 = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.225f * _width, 0.45f * _height); _combinedGeometry = ellipse1.CombineWith(ellipse2, Matrix3x2.Identity, CanvasGeometryCombine.Union); // Create the container to host the visuals var container1 = _compositor.CreateContainerVisual(); container1.Size = new Vector2(CompositionGrid1.ActualWidth.Single(), CompositionGrid1.ActualHeight.Single()); // Background Visual _bgVisual1 = _compositor.CreateSpriteVisual(); _bgVisual1.Size = new Vector2(_width * 0.45f, _height * 0.45f); _bgVisual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); _bgVisual1.Brush = _compositor.CreateColorBrush(Colors.DarkOrange); container1.Children.InsertAtBottom(_bgVisual1); // Create custom shaped visual using CompositionMaskBrush _visual1 = _compositor.CreateSpriteVisual(); _visual1.Size = new Vector2(_width, _height); _visual1.Offset = new Vector3(((CompositionGrid1.ActualWidth - _width) / 2).Single(), ((CompositionGrid1.ActualHeight - _height) / 2).Single(), 0); // Create the CompositionMask var ellipseGeometry = CanvasGeometry.CreateEllipse(_generator.Device, _width / 2, _height / 2, 0.4f * _width, 0.4f * _height); var compositionMask = _generator.CreateMaskSurface(_visual1.Size.ToSize(), ellipseGeometry); // Create Masked BackdropBrush from CompositionMask _visual1.Brush = _compositor.CreateMaskedBackdropBrush(compositionMask, Colors.AntiqueWhite, 30f, _backdropBrush); container1.Children.InsertAtTop(_visual1); var visual = _compositor.CreateSpriteVisual(); visual.Size = new Vector2(400, 400); visual.Offset = new Vector3(((3 * CompositionGrid1.ActualWidth / 4f) - (_width / 2)).Single(), ((3 * CompositionGrid1.ActualHeight / 4f) - (_height / 2)).Single(), 0); var roundRectGeometry = CanvasGeometry.CreateRoundedRectangle(_generator.Device, 0, 0, _width, _height, 25, 25); var maskSurface = _generator.CreateMaskSurface(visual.Size.ToSize(), roundRectGeometry); var frostedBrush = _compositor.CreateFrostedGlassBrush(maskSurface, Colors.AntiqueWhite, 30f, _backdropBrush); var shadow = _compositor.CreateDropShadow(); shadow.Opacity = 0.5f; shadow.Color = Colors.Black; shadow.Offset = new Vector3(10, 10, 0); shadow.BlurRadius = 15; shadow.Mask = frostedBrush.GetSourceParameter("mask"); visual.Brush = frostedBrush; visual.Shadow = shadow; container1.Children.InsertAtTop(visual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid1, container1); // Initialize the visuals for the Animated Canvas // Create the container to host the visuals var container2 = _compositor.CreateContainerVisual(); container2.Size = new Vector2(CompositionGrid2.ActualWidth.Single(), CompositionGrid2.ActualHeight.Single()); // Background Visual _bgVisual2 = _compositor.CreateSpriteVisual(); _bgVisual2.Size = new Vector2(_width * Factor, _height * Factor); _bgVisual2.Offset = new Vector3(((CompositionGrid2.ActualWidth - (_width * Factor)) / 2).Single(), ((CompositionGrid2.ActualHeight - (_height * Factor)) / 2).Single(), 0); //var radians = ((45f * Math.PI) / 180).Single(); //var bgGeometry = _combinedGeometry.Transform(Matrix3x2.CreateRotation(radians, new Vector2(_width / 2, _height / 2))); //var bgMask = await _generator.CreateMaskAsync(_bgVisual2.Size.ToSize(), bgGeometry, Colors.DarkRed); //_bgVisual2.Brush = _compositor.CreateSurfaceBrush(bgMask.Surface); _bgVisual2.Brush = _compositor.CreateColorBrush(Colors.LawnGreen); container2.Children.InsertAtBottom(_bgVisual2); // Animated Visual _animatedVisual = _compositor.CreateSpriteVisual(); _animatedVisual.Size = new Vector2(_width, _height); _animatedVisual.Offset = new Vector3(((CompositionGrid2.ActualWidth - _width) / 2).Single(), ((CompositionGrid2.ActualHeight - _height) / 2).Single(), 0); // Create the Excluded geometry _outerGeometry = CanvasGeometry.CreateRectangle(_generator.Device, 0, 0, _width, _height); var excludedGeometry = _outerGeometry.CombineWith(_combinedGeometry, Matrix3x2.Identity, CanvasGeometryCombine.Exclude); // Create the CompositionMask _animatedCompositionMask = _generator.CreateMaskSurface(_animatedVisual.Size.ToSize(), excludedGeometry); var animBrush = _compositor.CreateMaskedBackdropBrush(_animatedCompositionMask, Colors.AntiqueWhite, 10f, _backdropBrush); _animatedVisual.Brush = animBrush; container2.Children.InsertAtTop(_animatedVisual); ElementCompositionPreview.SetElementChildVisual(CompositionGrid2, container2); }
/// <summary> /// Creates a custom shaped Frosted Glass Effect Brush using BackdropBrush and a Mask /// </summary> /// <param name="compositor">Compositor</param> /// <param name="mask">IMaskSurface</param> /// <param name="blendColor">Color to blend in the BackdropBrush</param> /// <param name="blurAmount">Blur Amount of the Backdrop Brush</param> /// <param name="backdropBrush">Backdrop Brush (optional). If not provided, then compositor creates it.</param> /// <param name="multiplyAmount">MultiplyAmount of the ArithmeticCompositeEffect</param> /// <param name="colorAmount">Source1Amount of the ArithmeticCompositeEffect</param> /// <param name="backdropAmount">Source2Amount of the ArithmeticCompositeEffect</param> /// <returns>CompositionEffectBrush</returns> public static CompositionEffectBrush CreateFrostedGlassBrush(this Compositor compositor, IMaskSurface mask, Color blendColor, float blurAmount, CompositionBackdropBrush backdropBrush = null, float multiplyAmount = 0, float colorAmount = 0.5f, float backdropAmount = 0.5f) { // Create a frosty glass effect var frostEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = blurAmount, BorderMode = EffectBorderMode.Hard, Source = new ArithmeticCompositeEffect { Name = "Source", MultiplyAmount = multiplyAmount, Source1Amount = backdropAmount, Source2Amount = colorAmount, Source1 = new CompositionEffectSourceParameter("backdrop"), Source2 = new ColorSourceEffect { Name = "BlendColor", Color = blendColor } } }; // Composite Effect var effect = new CompositeEffect { Mode = CanvasComposite.DestinationIn, Sources = { frostEffect, new CompositionEffectSourceParameter("mask") } }; // Create Effect Factory var factory = compositor.CreateEffectFactory(effect, new[] { "Blur.BlurAmount", "BlendColor.Color" }); // Create Effect Brush var brush = factory.CreateBrush(); // Set the BackDropBrush // If no backdrop brush is provided, create one brush.SetSourceParameter("backdrop", backdropBrush ?? compositor.CreateBackdropBrush()); // Set the Mask // Create SurfaceBrush from CompositionMask var maskBrush = compositor.CreateSurfaceBrush(mask.Surface); brush.SetSourceParameter("mask", maskBrush); return(brush); }
/// <summary> /// Creates a custom shaped Effect Brush using BackdropBrush and a Mask /// </summary> /// <param name="compositor">Compositor</param> /// <param name="mask">IMaskSurface</param> /// <param name="blendColor">Color to blend in the BackdropBrush</param> /// <param name="blurAmount">Blur Amount of the Backdrop Brush</param> /// <param name="backdropBrush">Backdrop Brush (optional). If not provided, then compositor creates it.</param> /// <returns>CompositionEffectBrush</returns> public static CompositionEffectBrush CreateMaskedBackdropBrush(this Compositor compositor, IMaskSurface mask, Color blendColor, float blurAmount, CompositionBackdropBrush backdropBrush = null) { // Blur Effect var blurEffect = new GaussianBlurEffect() { Name = "Blur", BlurAmount = blurAmount, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter("backdrop"), }; // Blend Effect var blendEffect = new BlendEffect { Foreground = new ColorSourceEffect { Name = "Color", Color = blendColor }, Background = blurEffect, Mode = BlendEffectMode.Multiply }; // Composite Effect var effect = new CompositeEffect { Mode = CanvasComposite.DestinationIn, Sources = { blendEffect, new CompositionEffectSourceParameter("mask") } }; // Create Effect Factory var factory = compositor.CreateEffectFactory(effect, new[] { "Blur.BlurAmount", "Color.Color" }); // Create Effect Brush var brush = factory.CreateBrush(); // Set the BackDropBrush // If no backdrop brush is provided, create one brush.SetSourceParameter("backdrop", backdropBrush ?? compositor.CreateBackdropBrush()); // Set the Mask // Create SurfaceBrush from IMaskSurface var maskBrush = compositor.CreateSurfaceBrush(mask.Surface); brush.SetSourceParameter("mask", maskBrush); return(brush); }
private void InitializeFrostedGlass(UIElement glassHost) { //https://msdn.microsoft.com/en-us/windows/uwp/graphics/using-the-visual-layer-with-xaml hostVisual = ElementCompositionPreview.GetElementVisual(glassHost); compositor = hostVisual.Compositor; // Create a glass effect, requires Win2D NuGet package var glassEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = 10.0f, //original value: 15.0f BorderMode = EffectBorderMode.Hard, Source = new ArithmeticCompositeEffect { MultiplyAmount = 0, Source1Amount = 0.5f, Source2Amount = 0.5f, Source1 = new CompositionEffectSourceParameter("backdropBrush"), Source2 = new ColorSourceEffect { Name = "NewColor", Color = blurColor } } }; // Create an instance of the effect and set its source to a CompositionBackdropBrush effectFactory = compositor.CreateEffectFactory(glassEffect, new[] { "Blur.BlurAmount", "NewColor.Color" }); backdropBrush = compositor.CreateBackdropBrush(); effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter("backdropBrush", backdropBrush); // Create a Visual to contain the frosted glass effect glassVisual = compositor.CreateSpriteVisual(); glassVisual.Brush = effectBrush; if (Animate) { // https://blogs.windows.com/buildingapps/2016/09/12/creating-beautiful-effects-for-uwp/ ColorKeyFrameAnimation colorAnimation = compositor.CreateColorKeyFrameAnimation(); colorAnimation.InsertKeyFrame(0.0f, lastBlurColor); colorAnimation.InsertKeyFrame(1.0f, blurColor); colorAnimation.Duration = TimeSpan.FromSeconds(5); effectBrush.StartAnimation("NewColor.Color", colorAnimation); //ScalarKeyFrameAnimation blurAnimation = compositor.CreateScalarKeyFrameAnimation(); //blurAnimation.InsertKeyFrame(0.0f, 0.0f); //blurAnimation.InsertKeyFrame(0.5f, 100.0f); //blurAnimation.InsertKeyFrame(1.0f, 0.0f); //blurAnimation.Duration = TimeSpan.FromSeconds(4); //blurAnimation.StopBehavior = AnimationStopBehavior.SetToFinalValue; //effectBrush.StartAnimation("Blur.BlurAmount", blurAnimation); } // Add the blur as a child of the host in the visual tree ElementCompositionPreview.SetElementChildVisual(glassHost, glassVisual); // Make sure size of glass host and glass visual always stay in sync var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); glassVisual.StartAnimation("Size", bindSizeAnimation); IsGlassOn = true; }
protected override void OnConnected() { Compositor compositor = Window.Current.Compositor; // CompositionCapabilities: Are Effects supported? bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported(); FallbackColor = Color.FromArgb(100, 60, 60, 60); if (usingFallback) { // If Effects are not supported, use Fallback Solid Color CompositionBrush = compositor.CreateColorBrush(FallbackColor); return; } // Load NormalMap onto an ICompositionSurface using LoadedImageSurface _surface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///Assets/Damaged_NormalMap.jpg"), new Size(400, 400)); // Load Surface onto SurfaceBrush CompositionSurfaceBrush normalMap = compositor.CreateSurfaceBrush(_surface); normalMap.Stretch = CompositionStretch.Uniform; // Define Effect graph const float glassLightAmount = 0.5f; const float glassBlurAmount = 0.95f; Color tintColor = Color.FromArgb(255, 128, 128, 128); var graphicsEffect = new ArithmeticCompositeEffect() { Name = "LightComposite", Source1Amount = 1, Source2Amount = glassLightAmount, MultiplyAmount = 0, Source1 = new ArithmeticCompositeEffect() { Name = "BlurComposite", Source1Amount = 1 - glassBlurAmount, Source2Amount = glassBlurAmount, MultiplyAmount = 0, Source1 = new ColorSourceEffect() { Name = "Tint", Color = tintColor, }, Source2 = new GaussianBlurEffect() { BlurAmount = 20, Source = new CompositionEffectSourceParameter("Backdrop"), Optimization = EffectOptimization.Balanced, BorderMode = EffectBorderMode.Hard, }, }, Source2 = new SceneLightingEffect() { AmbientAmount = 0.15f, DiffuseAmount = 1, SpecularAmount = 0.1f, NormalMapSource = new CompositionEffectSourceParameter("NormalMap") }, }; // Create EffectFactory and EffectBrush CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect); CompositionEffectBrush effectBrush = effectFactory.CreateBrush(); // Create BackdropBrush CompositionBackdropBrush backdrop = compositor.CreateBackdropBrush(); // Set Sources to Effect effectBrush.SetSourceParameter("NormalMap", normalMap); effectBrush.SetSourceParameter("Backdrop", backdrop); // Set EffectBrush as the brush that XamlCompBrushBase paints onto Xaml UIElement CompositionBrush = effectBrush; }
/// <summary> /// Creates a custom shaped Effect Brush using BackdropBrush and an IGaussianMaskSurface /// </summary> /// <param name="compositor">Compositor</param> /// <param name="mask">IMaskSurface</param> /// <param name="blendColor">Color to blend in the BackdropBrush</param> /// <param name="blurRadius">Blur Amount of the Backdrop Brush</param> /// <param name="backdropBrush">Backdrop Brush (optional). If not provided, then compositor creates it.</param> /// <returns>CompositionEffectBrush</returns> public static CompositionEffectBrush CreateGaussianMaskedBackdropBrush(this Compositor compositor, IGaussianMaskSurface mask, Color blendColor, float blurRadius, CompositionBackdropBrush backdropBrush = null) { return(CompositionExtensions.CreateBackdropBrush(compositor, mask, blendColor, blurRadius, backdropBrush)); }