private async void ApplyEffect(CompositionImage image)
        {
            Task <CompositionDrawingSurface> task = null;

            // If we've requested a load time effect input, kick it off now
            if (_currentTechnique.LoadTimeEffectHandler != null)
            {
                task = SurfaceLoader.LoadFromUri(image.Source, Size.Empty, _currentTechnique.LoadTimeEffectHandler);
            }

            // Create the new brush, set the inputs and set it on the image
            CompositionEffectBrush brush = _currentTechnique.CreateBrush();

            brush.SetSourceParameter("ImageSource", image.SurfaceBrush);
            image.Brush = brush;

            // If we've got an active task, wait for it to finish
            if (task != null)
            {
                CompositionDrawingSurface effectSurface = await task;

                // Set the effect surface as input
                brush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(effectSurface));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes new instance of the <see cref="BaseBackDrop"/> class.
        /// </summary>
        /// <param name="useHostBackdrop">Enables host backdrop is platform supports it.</param>
        public BaseBackDrop(bool useHostBackdrop = false)
        {
            m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement);
            Compositor   = m_rootVisual.Compositor;

            m_blurVisual = Compositor.CreateSpriteVisual();
            m_noiseBrush = Compositor.CreateSurfaceBrush();

            CompositionEffectBrush brush = BuildBlurBrush();

            if (useHostBackdrop && IsHostBackDropSupported)
            {
                brush.SetSourceParameter("source", m_compositor.CreateHostBackdropBrush());
            }
            else
            {
                brush.SetSourceParameter("source", m_compositor.CreateBackdropBrush());
            }

            m_blurBrush        = brush;
            m_blurVisual.Brush = m_blurBrush;

            BlurAmount = 9;
            //TintColor = Colors.Transparent;
            ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual);

            this.Loading  += OnLoading;
            this.Unloaded += OnUnloaded;
        }
Ejemplo n.º 3
0
        public HostBackDrop()
        {
            m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement);
            Compositor   = m_rootVisual.Compositor;

            m_blurVisual = Compositor.CreateSpriteVisual();

#if SDKVERSION_14393
            CompositionEffectBrush brush = BuildBlurBrush();
            try
            {
                brush.SetSourceParameter("source", m_compositor.CreateHostBackdropBrush());
            }
            catch
            {
                brush.SetSourceParameter("source", m_compositor.CreateBackdropBrush());
            }
            m_blurBrush        = brush;
            m_blurVisual.Brush = m_blurBrush;

            // BlurAmount = 9;
            // TintColor = Colors.Transparent;
#else
            m_blurBrush        = Compositor.CreateColorBrush(Colors.Transparent);
            m_blurVisual.Brush = m_blurBrush;
#endif
            ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual);

            this.Loading  += OnLoading;
            this.Unloaded += OnUnloaded;
        }
Ejemplo n.º 4
0
        //LightingTypes _selectedLight = LightingTypes.PointDiffuse;
        private void SetImageEffect(CompositionImage image)
        {
            if (_effectFactory == null)
            {
                return;
            }

            // Create the effect brush and bind the normal map
            CompositionEffectBrush brush = _effectFactory.CreateBrush();

            //ComboBoxItem item = LightingSelection.SelectedValue as ComboBoxItem;
            //switch ((LightingTypes)item.Tag)
            switch (SelectedLight)
            {
            case LightingTypes.SpotLightSpecular:
            case LightingTypes.PointSpecular:
            case LightingTypes.DistantDiffuse:
            case LightingTypes.DistantSpecular:
                brush.SetSourceParameter("NormalMap", _circleNormalsBrush);
                break;

            default:
                brush.SetSourceParameter("NormalMap", _flatNormalsBrush);
                break;
            }

            // Update the CompositionImage to use the custom effect brush
            image.Brush = brush;
        }
        private async void OnUriChanged()
        {
            if (_uri == null)
            {
                ReleaseContent();
                return;
            }

            _surface = await SurfaceLoader.LoadFromUri(_uri);

            if (_loadEffectDelegate != null)
            {
                _effectSurface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);
            }

            // Async operations may take a while.  If we've unloaded, return now.
            if (_unloaded)
            {
                ReleaseContent();
                return;
            }

            // Apply stretching policy
            ApplyStretchingPolicy(new Size(ActualWidth, ActualHeight));

            if (_effectBrush == null)
            {
                CompositionSurfaceBrush brush = (CompositionSurfaceBrush)_sprite.Brush;

                if (brush != null)
                {
                    brush.Surface = _surface;
                }
                else
                {
                    _sprite.Brush = _compositor.CreateSurfaceBrush(_surface);
                }
            }
            else
            {
                _effectBrush.SetSourceParameter("ImageSource", _compositor.CreateSurfaceBrush(_surface));

                if (_effectSurface != null)
                {
                    _effectBrush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(_effectSurface));
                }
                _sprite.Brush = _effectBrush;
            }
        }
        public async void SetEffectBrush(CompositionEffectBrush brush)
        {
            // Release previous brush if set
            if (_effectBrush != null)
            {
                _effectBrush.Dispose();
                _effectBrush = null;
            }

            if (_effectSurface != null)
            {
                _effectSurface.Dispose();
                _effectSurface = null;
            }

            _effectBrush = brush;

            // Set the new brush
            if (_sprite != null)
            {
                // Release current brush
                if (_sprite.Brush != null)
                {
                    _sprite.Brush.Dispose();
                }

                if (_surface != null)
                {
                    // If the effect brush is cleared, create a surface brush
                    if (_effectBrush == null)
                    {
                        _sprite.Brush = _compositor.CreateSurfaceBrush(_surface);
                    }
                    else
                    {
                        _effectBrush.SetSourceParameter("ImageSource", _compositor.CreateSurfaceBrush(_surface));

                        if (_loadEffectDelegate != null)
                        {
                            _effectSurface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);

                            _effectBrush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(_effectSurface));
                        }
                        _sprite.Brush = _effectBrush;
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public BackDrop()
        {
            if (ApiInformation.IsMethodPresent("Windows.UI.Composition.Compositor", "CreateBackdropBrush"))
            {
                m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement);
                Compositor   = m_rootVisual.Compositor;

                m_blurBrush = BuildBlurBrush();


                m_blurBrush.SetSourceParameter("source", m_compositor.CreateBackdropBrush());


                m_blurVisual       = Compositor.CreateSpriteVisual();
                m_blurVisual.Brush = m_blurBrush;

                BlurAmount = 9;
                TintColor  = Colors.Transparent;

                ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual);

                this.Loading  += OnLoading;
                this.Unloaded += OnUnloaded;
            }
            else
            {
            }
        }
        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;
        }
Ejemplo n.º 9
0
        private void Department_Loaded(object sender, RoutedEventArgs e)
        {
            effectVisual = compositor.CreateSpriteVisual();
            var destinationBrush = compositor.CreateBackdropBrush();
            //Create the Effect you want
            var graphicsEffect = new GaussianBlurEffect
            {
                Name         = "Blur",
                BlurAmount   = 0f,
                BorderMode   = EffectBorderMode.Hard,
                Optimization = EffectOptimization.Balanced,
                Source       = new CompositionEffectSourceParameter("Background")
            };



            effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
            effectBrush   = effectFactory.CreateBrush();
            effectBrush.SetSourceParameter("Background", destinationBrush);

            effectVisual.Brush = effectBrush;
            effectVisual.Size  = new System.Numerics.Vector2(900, 400);

            ElementCompositionPreview.SetElementChildVisual(CoverImage, effectVisual);

            //Create Spring Animation for nature increase and decrease value
            _springAnimation        = compositor.CreateSpringScalarAnimation();
            _springAnimation.Period = TimeSpan.FromSeconds(0.70);
            //_springAnimation.DampingRatio = 0.25f;
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        private void Load_Completed(LoadedImageSurface sender, LoadedImageSourceLoadCompletedEventArgs e)
        {
            IsImageLoading = false;

            if (e.Status == LoadedImageSourceLoadStatus.Success)
            {
                var compositor = Window.Current.Compositor;
                var brush      = compositor.CreateSurfaceBrush(_surface);
                brush.Stretch = CompositionStretch.UniformToFill;

                // Create effects chain.
                saturationEffect = new SaturationEffect()
                {
                    Name       = "SaturationEffect",
                    Saturation = (float)SaturationAmount,
                    Source     = new CompositionEffectSourceParameter("image")
                };
                contrastEffect = new ContrastEffect()
                {
                    Name     = "ContrastEffect",
                    Contrast = (float)ContrastAmount,
                    Source   = saturationEffect
                };
                exposureEffect = new ExposureEffect()
                {
                    Name     = "ExposureEffect",
                    Source   = contrastEffect,
                    Exposure = (float)ExposureAmount,
                };
                temperatureAndTintEffect = new TemperatureAndTintEffect()
                {
                    Name        = "TemperatureAndTintEffect",
                    Source      = exposureEffect,
                    Temperature = (float)TemperatureAmount,
                    Tint        = (float)TintAmount
                };
                graphicsEffect = new GaussianBlurEffect()
                {
                    Name       = "Blur",
                    Source     = temperatureAndTintEffect,
                    BlurAmount = (float)BlurAmount,
                    BorderMode = EffectBorderMode.Hard,
                };

                var graphicsEffectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] {
                    "SaturationEffect.Saturation", "ExposureEffect.Exposure", "Blur.BlurAmount",
                    "TemperatureAndTintEffect.Temperature", "TemperatureAndTintEffect.Tint",
                    "ContrastEffect.Contrast"
                });
                combinedBrush = graphicsEffectFactory.CreateBrush();
                combinedBrush.SetSourceParameter("image", brush);

                // Composition Brush is what is being applied to the UI Element.
                CompositionBrush = combinedBrush;
            }
            else
            {
                LoadImageFromPath("ms-appx:///Assets/StoreLogo.png");
            }
        }
Ejemplo n.º 12
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
     effectBrush.Properties.InsertScalar(TemperatureEffectPath, 0f);
     effectBrush.Properties.InsertScalar(TintEffectPath, 0f);
 }
Ejemplo n.º 13
0
        public BackDrop()
        {
            if (DesignMode.DesignModeEnabled)
            {
                return;
            }

            m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement);
            Compositor   = m_rootVisual.Compositor;

            m_blurVisual = Compositor.CreateSpriteVisual();

#if SDKVERSION_14393
            m_noiseBrush = Compositor.CreateSurfaceBrush();

            CompositionEffectBrush brush = BuildBlurBrush();
            brush.SetSourceParameter("source", m_compositor.CreateBackdropBrush());
            m_blurBrush        = brush;
            m_blurVisual.Brush = m_blurBrush;

            BlurAmount = 9;
            TintColor  = Colors.Transparent;
#else
            m_blurBrush        = Compositor.CreateColorBrush(Colors.White);
            m_blurVisual.Brush = m_blurBrush;
#endif
            ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual);

            this.Loading  += OnLoading;
            this.Unloaded += OnUnloaded;
        }
Ejemplo n.º 14
0
        private void InitializeBrushes()
        {
            if (this.DataContext is ViewImageEditorMetadata)
            {
                var viewImageEditorMetadata = (ViewImageEditorMetadata)this.DataContext;

                // noeffect brush
                Size imageSize;
                //m_noEffectBrush = CreateBrushFromAsset("xxxxx.jpg", out imageSize);
                m_noEffectBrush    = CreateBrushFromAsset(viewImageEditorMetadata.Bitmap, out imageSize);
                m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height;

                renderSurface.UpdateLayout();
                ResizeImage(new Size(renderSurface.ActualWidth, renderSurface.ActualHeight));

                // Exposure
                var exposureEffectDesc = new ExposureEffect
                {
                    Name   = "effect",
                    Source = new CompositionEffectSourceParameter("Image")
                };
                m_exposureEffectBrush = m_compositor.CreateEffectFactory(exposureEffectDesc, new[] { "effect.Exposure" }).CreateBrush();
                ChangeExposureValue(0.5f);
                m_exposureEffectBrush.SetSourceParameter("Image", m_noEffectBrush);

                // monochromatic gray
                var grayscaleEffectDesc = new GrayscaleEffect
                {
                    Name   = "effect",
                    Source = new CompositionEffectSourceParameter("Image")
                };
                m_grayscaleEffectBrush = m_compositor.CreateEffectFactory(grayscaleEffectDesc).CreateBrush();
                m_grayscaleEffectBrush.SetSourceParameter("Image", m_noEffectBrush);
            }
        }
        public void CreateAnimation()
        {
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
            var blurEffect = new GaussianBlurEffect
            {
                Name       = "Blur",
                BlurAmount = 5,
                BorderMode = EffectBorderMode.Hard,
                Source     = new CompositionEffectSourceParameter("backdropBrush")
            };
            var animation = _compositor.CreateScalarKeyFrameAnimation();

            animation.InsertKeyFrame(0.0f, 0.0f);
            animation.InsertKeyFrame(1.0f, 100.0f);
            animation.Duration = TimeSpan.FromSeconds(4);

            var effectFactory = _compositor.CreateEffectFactory(blurEffect, new[] { "Blur.BlurAmount" });

            _effectBrush = effectFactory.CreateBrush();
            var backdropBrush = _compositor.CreateBackdropBrush();

            _effectBrush.SetSourceParameter("backdropBrush", backdropBrush);
            var spriteVisual = _compositor.CreateSpriteVisual();

            spriteVisual.Size  = new System.Numerics.Vector2((float)Err_Image.ActualWidth, (float)Err_Image.ActualHeight);
            spriteVisual.Brush = _effectBrush;
            ElementCompositionPreview.SetElementChildVisual(Err_Image, spriteVisual);
            spriteVisual.Brush.StartAnimation("Blur.BlurAmount", animation);
        }
Ejemplo n.º 16
0
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //Create Visual to contain effect
            effectVisual = compositor.CreateSpriteVisual();
            var destinationBrush = compositor.CreateBackdropBrush();
            //Create the Effect you want
            var graphicsEffect = new GaussianBlurEffect
            {
                Name       = "Blur",
                BlurAmount = 0f,
                BorderMode = EffectBorderMode.Hard,
                Source     = new CompositionEffectSourceParameter("Background")
            };



            effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" });
            effectBrush   = effectFactory.CreateBrush();
            effectBrush.SetSourceParameter("Background", destinationBrush);

            effectVisual.Brush = effectBrush;
            //By the default the Visual have the size width and height (defined in Vector 2) 0 and 0
            ResizeVisual();

            ElementCompositionPreview.SetElementChildVisual(Back, effectVisual);

            //Create Spring Animation for nature increase and decrease value
            _springAnimation              = compositor.CreateSpringScalarAnimation();
            _springAnimation.Period       = TimeSpan.FromSeconds(0.5);
            _springAnimation.DampingRatio = 0.75f;
        }
Ejemplo n.º 17
0
        public BackDrop()
        {
            var myBackingVisual = ElementCompositionPreview.GetElementVisual(this as UIElement);

            m_compositor = myBackingVisual.Compositor;
            m_blurBrush  = BuildColoredBlurMixerBrush();
            m_blurBrush.SetSourceParameter("source", m_compositor.CreateBackdropBrush());

            m_blurVisual       = m_compositor.CreateSpriteVisual();
            m_blurVisual.Brush = m_blurBrush;

            m_blurVisual.Properties.InsertScalar("BlurValue", 10.0f);
            m_blurVisual.Properties.InsertScalar("FadeValue", 1.0f);

            SetupPropertySetExpression();

            m_container = m_compositor.CreateContainerVisual();
            m_container.Children.InsertAtTop(m_blurVisual);

            //CreateDropshadow();

            ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_container);

            this.Loading  += OnLoading;
            this.Unloaded += OnUnloaded;
        }
Ejemplo n.º 18
0
        private void InitializeCompositor()
        {
            compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
            InitializeEffects();
            MainImage.Source = item.ImageSource;
            MainImage.InvalidateArrange();

            var destinationBrush = compositor.CreateBackdropBrush();

            var graphicsEffectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] {
                "SaturationEffect.Saturation", "ExposureEffect.Exposure", "Blur.BlurAmount",
                "TemperatureAndTintEffect.Temperature", "TemperatureAndTintEffect.Tint",
                "ContrastEffect.Contrast"
            });

            combinedBrush = graphicsEffectFactory.CreateBrush();
            combinedBrush.SetSourceParameter("Backdrop", destinationBrush);

            var effectSprite = compositor.CreateSpriteVisual();

            effectSprite.Size  = new Vector2((float)item.ImageSource.PixelWidth, (float)item.ImageSource.PixelHeight);
            effectSprite.Brush = combinedBrush;
            ElementCompositionPreview.SetElementChildVisual(MainImage, effectSprite);

            editingInitialized = true;
        }
        public void Do()
        {
            Visual hostVisual = ElementCompositionPreview.GetElementVisual(Err_Image);

            comp = ElementCompositionPreview.GetElementVisual(this).Compositor;
            var blurEffect = new GaussianBlurEffect
            {
                Name       = "Blur",
                BlurAmount = (float)blur_Slider.Value,
                BorderMode = EffectBorderMode.Hard,
                Source     = new CompositionEffectSourceParameter("backdropBrush")
                             //Source = new ArithmeticCompositeEffect
                             //{
                             //    MultiplyAmount = 0,
                             //    Source1Amount = 0.5f,
                             //    Source2Amount = 0.5f,
                             //    Source1 = new CompositionEffectSourceParameter("backdropBrush"),
                             //    Source2 = new ColorSourceEffect
                             //    {
                             //        Color = Color.FromArgb(255,255,255,255)
                             //    }
                             //}
            };
            var effectFactory = comp.CreateEffectFactory(blurEffect, new[] { "Blur.BlurAmount" });

            _effectBrush = effectFactory.CreateBrush();
            var backdropBrush = comp.CreateBackdropBrush();

            _effectBrush.SetSourceParameter("backdropBrush", backdropBrush);
            var blurVisual = comp.CreateSpriteVisual();

            blurVisual.Size  = new System.Numerics.Vector2((float)Err_Image.ActualWidth, (float)Err_Image.ActualHeight);
            blurVisual.Brush = _effectBrush;
            ElementCompositionPreview.SetElementChildVisual(Err_Image, blurVisual);
        }
Ejemplo n.º 20
0
    public MainPage()
    {
        this.InitializeComponent();
        MainGrid.SizeChanged += OnMainGridSizeChanged;
        compositor            = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor;
        // we create the effect.
        // Notice the Source parameter definition. Here we tell the effect that the source will come from another element/object
        var blurEffect = new GaussianBlurEffect
        {
            Name       = "Blur",
            Source     = new CompositionEffectSourceParameter("background"),
            BlurAmount = 100f,
            BorderMode = EffectBorderMode.Hard,
        };
        // we convert the effect to a blur that can be used to paint the visual layer
        var blurEffectFactory = compositor.CreateEffectFactory(blurEffect);

        brush = blurEffectFactory.CreateBrush();
        // We create a special brush to get the image output of the previous layer.
        // we are basically chaining the layer (xaml grid -> rendered bitmap of the grid -> blur effect -> screen)
        var destinationBrush = compositor.CreateBackdropBrush();

        brush.SetSourceParameter("background", destinationBrush);
        // we create the visual sprite that will hold our generated bitmap (the blurred grid)
        // Visual Sprite are "raw" elements so there is no automatic layouting. You have to specify the size yourself
        var blurSprite = compositor.CreateSpriteVisual();

        blurSprite.Size  = new Vector2((float)MainGrid.ActualWidth, (float)MainGrid.ActualHeight);
        blurSprite.Brush = brush;
        // we add our sprite to the rendering pipeline
        ElementCompositionPreview.SetElementChildVisual(MainGrid, blurSprite);
    }
 public override void UpdateResourceBindings(CompositionEffectBrush brush)
 {
     if (_surface != null)
     {
         _brush.Surface = _surface.Surface;
         if (EdgeMode == EdgeMode.Ninegrid)
         {
             _nineGrid.Source = _brush;
             brush.SetSourceParameter(_id + "Image", _nineGrid);
         }
         else
         {
             brush.SetSourceParameter(_id + "Image", _brush);
         }
     }
 }
        protected override void OnConnected()
        {
            // return if Uri String is null or empty
            if (String.IsNullOrEmpty(ImageUriString))
            {
                return;
            }

            Compositor compositor = Window.Current.Compositor;

            // Use LoadedImageSurface API to get ICompositionSurface from image uri provided
            _surface = LoadedImageSurface.StartLoadFromUri(new Uri(ImageUriString));

            // Load Surface onto SurfaceBrush
            _surfaceBrush         = compositor.CreateSurfaceBrush(_surface);
            _surfaceBrush.Stretch = CompositionStretch.UniformToFill;

            // CompositionCapabilities: Are Tint+Temperature and Saturation supported?
            bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsSupported();

            if (usingFallback)
            {
                // If Effects are not supported, Fallback to image without effects
                CompositionBrush = _surfaceBrush;
                return;
            }

            // Define Effect graph
            IGraphicsEffect graphicsEffect = new SaturationEffect
            {
                Name       = "Saturation",
                Saturation = 0.3f,
                Source     = new TemperatureAndTintEffect
                {
                    Name        = "TempAndTint",
                    Temperature = 0,
                    Source      = new CompositionEffectSourceParameter("Surface"),
                }
            };

            // Create EffectFactory and EffectBrush
            CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "TempAndTint.Temperature" });
            CompositionEffectBrush   effectBrush   = effectFactory.CreateBrush();

            effectBrush.SetSourceParameter("Surface", _surfaceBrush);

            // Set EffectBrush to paint Xaml UIElement
            CompositionBrush = effectBrush;

            // Trivial looping animation to demonstrate animated effect
            ScalarKeyFrameAnimation tempAnim = compositor.CreateScalarKeyFrameAnimation();

            tempAnim.InsertKeyFrame(0, 0);
            tempAnim.InsertKeyFrame(0.5f, 1f);
            tempAnim.InsertKeyFrame(1, 0);
            tempAnim.Duration          = TimeSpan.FromSeconds(5);
            tempAnim.IterationBehavior = Windows.UI.Composition.AnimationIterationBehavior.Forever;
            effectBrush.Properties.StartAnimation("TempAndTint.Temperature", tempAnim);
        }
        async Task CreateTintEffectBrushAsync(Uri uri)
        {
            Xamarin.Forms.Image element = this.Element as Xamarin.Forms.Image;

            if (Control == null || Element == null || element.Width < 0 || element.Height < 0)
            {
                return;
            }

            SetupCompositor();

            spriteVisual      = compositor.CreateSpriteVisual();
            spriteVisual.Size = new Vector2((float)element.Width, (float)element.Height);

            imageSurface = await generator.CreateImageSurfaceAsync(uri, new Windows.Foundation.Size(element.Width, element.Height), ImageSurfaceOptions.DefaultOptimized);

            CompositionSurfaceBrush surfaceBrush = compositor.CreateSurfaceBrush(imageSurface.Surface);

            CompositionBrush targetBrush = surfaceBrush;

            if (this.TintColor == Color.Transparent)
            {
                // Don't apply tint effect
                effectBrush = null;
            }
            else
            {
                // Set target brush to tint effect brush

                Windows.UI.Color nativeColor = GetNativeColor(this.TintColor);

                IGraphicsEffect graphicsEffect = new CompositeEffect
                {
                    Mode    = CanvasComposite.DestinationIn,
                    Sources =
                    {
                        new ColorSourceEffect
                        {
                            Name  = "colorSource",
                            Color = nativeColor
                        },
                        new CompositionEffectSourceParameter("mask")
                    }
                };

                CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect,
                                                                                        new[] { "colorSource.Color" });

                effectBrush = effectFactory.CreateBrush();
                effectBrush.SetSourceParameter("mask", surfaceBrush);

                SetTint(nativeColor);

                targetBrush = effectBrush;
            }

            spriteVisual.Brush = targetBrush;
            ElementCompositionPreview.SetElementChildVisual(Control, spriteVisual);
        }
Ejemplo n.º 24
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
     effectBrush.Properties.InsertScalar(RedExponentEffectPath, 1f);
     effectBrush.Properties.InsertScalar(GreenExponentEffectPath, 1f);
     effectBrush.Properties.InsertScalar(BlueExponentEffectPath, 1f);
 }
Ejemplo n.º 25
0
        public override CompositionEffectBrush CreateBrush()
        {
            CompositionEffectBrush brush = base.CreateBrush();

            brush.SetSourceParameter("LightMap", _lightMap.Brush);

            return(brush);
        }
Ejemplo n.º 26
0
        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));
        }
Ejemplo n.º 27
0
        protected override CompositionBrush CreateBrush()
        {
            _compositor = Window.Current.Compositor;

            _foregroundBrush            = _compositor.CreateLinearGradientBrush();
            _foregroundBrush.StartPoint = Vector2.Zero;
            _foregroundBrush.EndPoint   = new Vector2(1.0f);

            _bottomRightGradientStop        = _compositor.CreateColorGradientStop();
            _bottomRightGradientStop.Offset = 0.5f;
            _bottomRightGradientStop.Color  = Green;
            _topLeftradientStop             = _compositor.CreateColorGradientStop();
            _topLeftradientStop.Offset      = 0.5f;
            _topLeftradientStop.Color       = Blue;
            _foregroundBrush.ColorStops.Add(_bottomRightGradientStop);
            _foregroundBrush.ColorStops.Add(_topLeftradientStop);


            _backgroundBrush            = _compositor.CreateLinearGradientBrush();
            _backgroundBrush.StartPoint = new Vector2(1.0f, 0);
            _backgroundBrush.EndPoint   = new Vector2(0, 1.0f);

            _topRightGradientStop          = _compositor.CreateColorGradientStop();
            _topRightGradientStop.Offset   = 0.25f;
            _topRightGradientStop.Color    = Black;
            _bottomLeftGradientStop        = _compositor.CreateColorGradientStop();
            _bottomLeftGradientStop.Offset = 1.0f;
            _bottomLeftGradientStop.Color  = Black;
            _backgroundBrush.ColorStops.Add(_topRightGradientStop);
            _backgroundBrush.ColorStops.Add(_bottomLeftGradientStop);

            var graphicsEffect = new BlendEffect()
            {
                Mode       = BlendEffectMode.Screen,
                Foreground = new CompositionEffectSourceParameter("Main"),
                Background = new CompositionEffectSourceParameter("Tint"),
            };

            var effectFactory = _compositor.CreateEffectFactory(graphicsEffect);

            _brush = effectFactory.CreateBrush();
            _brush.SetSourceParameter("Main", _foregroundBrush);
            _brush.SetSourceParameter("Tint", _backgroundBrush);
            return(_brush);
        }
Ejemplo n.º 28
0
        public CompositionBrush Build(
            Color luminosityColor)
        {
            var adjustedTintOpacity = Math.Clamp(TintOpacity, 0, 1);

            adjustedTintOpacity = 0.3f + 0.7f * adjustedTintOpacity;

            IGraphicsEffectSource backDropEffectSource = new CompositionEffectSourceParameter("Backdrop");

            var luminosityColorEffect = new ColorSourceEffect()
            {
                Name  = "LuminosityColor",
                Color = luminosityColor
            };

            var graphicsEffect = new ArithmeticCompositeEffect
            {
                Source1        = backDropEffectSource,
                Source2        = luminosityColorEffect,
                MultiplyAmount = 0,
                Source1Amount  = 1 - adjustedTintOpacity,
                Source2Amount  = adjustedTintOpacity,
                Offset         = 0
            };

            //IGraphicsEffectSource noiseEffectSource = new CompositionEffectSourceParameter("Noise");
            //var noiseBorderEffect  = new BorderEffect()
            //{
            //    ExtendX = CanvasEdgeBehavior.Wrap,
            //    ExtendY = CanvasEdgeBehavior.Wrap,
            //    Source = noiseEffectSource,
            //};

            //var noiseOpacityEffect = new OpacityEffect()
            //{
            //    Name = "NoiseOpacity",
            //    Opacity = 0.5f,
            //    Source = noiseBorderEffect,
            //};

            //var finalEffect = new CrossFadeEffect()
            //{
            //    Name = "FadeInOut",
            //    Source1 = graphicsEffect,
            //    Source2 = noiseOpacityEffect
            //};

            var effectFactory            = Window.Current.Compositor.CreateEffectFactory(graphicsEffect);
            CompositionEffectBrush brush = effectFactory.CreateBrush();

            var hostBackdropBrush = Window.Current.Compositor.CreateHostBackdropBrush();

            brush.SetSourceParameter("Backdrop", hostBackdropBrush);

            return(brush);
        }
        private void BuildBackground()
        {
            _backgroundBrush         = _compositor.CreateSurfaceBrush(_uriSurface.Surface);
            _backgroundBrush.Stretch = CompositionStretch.Fill;
            _blurBrush.SetSourceParameter("source", _backgroundBrush);

            BackgroundVisual.Brush   = _blurBrush;
            BackgroundVisual.Size    = new Vector2((float)ActualWidth, (float)ActualHeight);
            BackgroundVisual.Opacity = ShowBlurredBackground ? 1.0f : 0;
        }
        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;
        }
Ejemplo n.º 31
0
        private void SetupBlur()
        {
            compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            var blur = new GaussianBlurEffect
            {
                Name = "Blur",
                Source = new CompositionEffectSourceParameter("Backdrop"),
                BlurAmount = 0.0f,
                BorderMode = EffectBorderMode.Hard
            };

            var blend = new BlendEffect
            {
                Name = "Blend",
                Foreground = new ColorSourceEffect
                {
                    Color = Color.FromArgb(128, 30, 30, 220),
                    Name = "ColorSource"
                },
                Background = blur,
                Mode = BlendEffectMode.Overlay
            };
            
            var effectFactory = compositor.CreateEffectFactory(blend, new[] {"Blur.BlurAmount"});
            brush = effectFactory.CreateBrush();

            var backdrop = compositor.CreateBackdropBrush();
            brush.SetSourceParameter("Backdrop", backdrop);

            var sprite = compositor.CreateSpriteVisual();

            sprite.Brush = brush;
            sprite.Size = new Vector2((float) TargetImage.ActualWidth, (float) TargetImage.ActualHeight);

            ElementCompositionPreview.SetElementChildVisual(TargetImage, sprite);
        }
Ejemplo n.º 32
0
        private void BlendSelection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem item = BlendSelection.SelectedValue as ComboBoxItem;
            BlendEffectMode blendmode = (BlendEffectMode)item.Tag;

            // Create a chained effect graph using a BlendEffect, blending color and blur
            var graphicsEffect = new BlendEffect
            {
                Mode = blendmode,
                Background = new ColorSourceEffect()
                {
                    Name = "Tint",
                    Color = Tint.Color,
                },

                Foreground = new GaussianBlurEffect()
                {
                    Name = "Blur",
                    Source = new CompositionEffectSourceParameter("Backdrop"),
                    BlurAmount = (float)BlurAmount.Value,
                    BorderMode = EffectBorderMode.Hard,
                }
            };

            var blurEffectFactory = _compositor.CreateEffectFactory(graphicsEffect, 
                new[] { "Blur.BlurAmount", "Tint.Color"});

            // Create EffectBrush, BackdropBrush and SpriteVisual
            _brush = blurEffectFactory.CreateBrush();

            // If the animation is running, restart it on the new brush
            if (AnimateToggle.IsOn)
            {
                StartBlurAnimation();
            }

            var destinationBrush = _compositor.CreateBackdropBrush();
            _brush.SetSourceParameter("Backdrop", destinationBrush);

            var blurSprite = _compositor.CreateSpriteVisual();
            blurSprite.Size = new Vector2((float)BackgroundImage.ActualWidth, (float)BackgroundImage.ActualHeight);
            blurSprite.Brush = _brush;

            ElementCompositionPreview.SetElementChildVisual(BackgroundImage, blurSprite);
        }
Ejemplo n.º 33
0
        /// <summary>
        /// Initializes the Composition elements
        /// </summary>
        private void InitComposition()
        {
            // Compositor
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
            // CompositionGenerator
            _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor);
            
            // Fade Out Animation
            _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _fadeOutAnimation.InsertKeyFrame(1f, 0);
            _fadeOutAnimation.Duration = TransitionDuration;
            // Fade In Animation
            _fadeInAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _fadeInAnimation.InsertKeyFrame(1f, 1);
            _fadeInAnimation.Duration = TransitionDuration;
            // Color Animation
            _colorAnimation = _compositor.CreateColorKeyFrameAnimation();
            _colorAnimation.Duration = TransitionDuration;
            // Offset Animation
            _offsetAnimation = _compositor.CreateVector3KeyFrameAnimation();
            _offsetAnimation.Target = "Offset";
            _offsetAnimation.Duration = TransitionDuration;
            _offsetAnimation.InsertKeyFrame(1f, Vector3.Zero);
            // Alignment animations
            _alignXAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _alignXAnimation.Duration = AlignmentTransitionDuration;
            _alignYAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _alignYAnimation.Duration = AlignmentTransitionDuration;

            // ZoomIn Animation Group
            _scaleAnimation = _compositor.CreateVector3KeyFrameAnimation();
            _scaleAnimation.Target = "Scale";
            _scaleAnimation.InsertKeyFrame(1f, Vector3.One);
            _scaleAnimation.Duration = TransitionDuration;
            _zoomInAnimationGroup = _compositor.CreateAnimationGroup();
            _zoomInAnimationGroup.Add(_scaleAnimation);
            _zoomInAnimationGroup.Add(_offsetAnimation);

            // Visuals
            _rootContainer = _compositor.CreateContainerVisual();
            _frameLayer = _compositor.CreateLayerVisual();
            _frameBackgroundVisual = _compositor.CreateSpriteVisual();
            _frameContentVisual = _compositor.CreateSpriteVisual();
            _placeholderContentVisual = _compositor.CreateSpriteVisual();
            _placeholderBackgroundVisual = _compositor.CreateSpriteVisual();
            _nextVisualContent = _compositor.CreateSpriteVisual();

            _frameLayer.Children.InsertAtTop(_frameBackgroundVisual);
            _frameLayer.Children.InsertAtTop(_frameContentVisual);
            _frameLayer.Children.InsertAtTop(_placeholderBackgroundVisual);
            _frameLayer.Children.InsertAtTop(_placeholderContentVisual);
            _frameLayer.Children.InsertAtTop(_nextVisualContent);

            // Placeholder content
            _placeholderContentMask = _generator.CreateGeometrySurface(PlaceholderSize, GetPlaceHolderGeometry(),
                PlaceholderColor, PlaceholderBackground);
            _placeholderContentBrush = _compositor.CreateSurfaceBrush(_placeholderContentMask.Surface);
            _placeholderContentVisual.Brush = _placeholderContentBrush;
            // Placeholder background
            _placeholderBackgroundVisual.Brush = _compositor.CreateColorBrush(PlaceholderBackground);

            // By default placeholder visual will not be visible
            HidePlaceholder();

            // Shadow visual
            _shadowVisual = _compositor.CreateSpriteVisual();

            _rootContainer.Children.InsertAtBottom(_shadowVisual);
            _rootContainer.Children.InsertAtTop(_frameLayer);

            _frameBackgroundVisual.Brush = _compositor.CreateColorBrush(FrameBackground);

            // Create the effect to create the opacity mask
            var layerEffect = new CompositeEffect
            {
                // CanvasComposite.DestinationIn - Intersection of source and mask. 
                // Equation: O = MA * S
                // where O - Output pixel, MA - Mask Alpha, S - Source pixel.
                Mode = CanvasComposite.DestinationIn,
                Sources =
                        {
                            new CompositionEffectSourceParameter("source"),
                            new CompositionEffectSourceParameter("mask")
                        }
            };

            var layerEffectFactory = _compositor.CreateEffectFactory(layerEffect);
            _layerEffectBrush = layerEffectFactory.CreateBrush();

            // The mask for the imageFrame
            _frameLayerMask = _generator.CreateMaskSurface(new Size(0, 0), null);
            _layerEffectBrush.SetSourceParameter("mask", _compositor.CreateSurfaceBrush(_frameLayerMask.Surface));
            // Apply the mask effect to the frameLayer
            _frameLayer.Effect = _layerEffectBrush;

            ElementCompositionPreview.SetElementChildVisual(this, _rootContainer);
        }
Ejemplo n.º 34
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
     effectBrush.Properties.InsertScalar(RedExponentEffectPath, 1f);
     effectBrush.Properties.InsertScalar(GreenExponentEffectPath, 1f);
     effectBrush.Properties.InsertScalar(BlueExponentEffectPath, 1f);
 }
Ejemplo n.º 35
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
     effectBrush.Properties.InsertScalar(SaturationEffectPath, 0f);
 }
Ejemplo n.º 36
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
     effectBrush.Properties.InsertScalar(IntensityEffectPath, 0.5f);
 }
Ejemplo n.º 37
0
 private void CreateBrush()
 {
     effectBrush = effectFactory.CreateBrush();
     effectBrush.SetSourceParameter(EffectSource, surfaceBrush);
 }
Ejemplo n.º 38
0
        public async void SetEffectBrush(CompositionEffectBrush brush)
        {
            // Release previous brush if set
            if (_effectBrush != null)
            {
                _effectBrush.Dispose();
                _effectBrush = null;
            }

            if (_effectSurface != null)
            {
                _effectSurface.Dispose();
                _effectSurface = null;
            }

            _effectBrush = brush;

            // Set the new brush
            if (_sprite != null)
            {
                // Release current brush
                if (_sprite.Brush != null)
                {
                    _sprite.Brush.Dispose();
                }

                if (_surface != null)
                {
                    // If the effect brush is cleared, create a surface brush
                    if (_effectBrush == null)
                    {
                        _sprite.Brush = _compositor.CreateSurfaceBrush(_surface);
                    }
                    else
                    {
                        _effectBrush.SetSourceParameter("ImageSource", _compositor.CreateSurfaceBrush(_surface));

                        if (_loadEffectDelegate != null)
                        {                           
                            _effectSurface = await SurfaceLoader.LoadFromUri(_uri, Size.Empty, _loadEffectDelegate);
                            _effectBrush.SetSourceParameter("EffectSource", _compositor.CreateSurfaceBrush(_effectSurface));
                        }
                        _sprite.Brush = _effectBrush;
                    }
                }
            }
        }