Example #1
0
        private CompositionBrush CreateNormalMapBrush(Compositor compositor, ICompositionSurface normalMapImage, ICompositionSurface colorMapImage)
        {
            var colorMapParameter  = new CompositionEffectSourceParameter("ColorMap");
            var normalMapParameter = new CompositionEffectSourceParameter("NormalMap");

            var compositeEffect = new ArithmeticCompositeEffect()
            {
                Source1 = colorMapParameter,
                Source2 = new SceneLightingEffect()
                {
                    NormalMapSource = normalMapParameter
                }
            };

            var normalMapBrush = compositor.CreateSurfaceBrush(normalMapImage);
            var colorMapBrush  = compositor.CreateSurfaceBrush(colorMapImage);

            normalMapBrush.Stretch = CompositionStretch.Fill;
            colorMapBrush.Stretch  = CompositionStretch.Fill;

            var brush = compositor.CreateEffectFactory(compositeEffect).CreateBrush();

            brush.SetSourceParameter(colorMapParameter.Name, colorMapBrush);
            brush.SetSourceParameter(normalMapParameter.Name, normalMapBrush);

            return(brush);
        }
Example #2
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);
        }
        public static CompositionEffectBrush CreateCompositeImageBrush(this Compositor compositor, List<Tuple<string, string>> images)
        {
            var effect = new CompositeEffect { Mode = CanvasComposite.DestinationIn };

            foreach (var image in images)
            {
                var param = new CompositionEffectSourceParameter(image.Item1);
                effect.Sources.Add(param);
            }

            var effectFactory = compositor.CreateEffectFactory(effect);
            var brush = effectFactory.CreateBrush();

            foreach (var image in images)
            {
                brush.SetSourceParameter(image.Item1, compositor.CreateImageBrush(image.Item2));
            }

            return brush;
        }
Example #4
0
        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));
        }
Example #5
0
        private void EffectToString(StringBuilder sb, int indent, IGraphicsEffect effect, CompositionEffectBrush brush)
        {
            const int indentIncrement = 4;
            Type      type            = effect.GetType();

            sb.AppendFormat("{0}{1}\r\n{0}{{\r\n", Indent(indent), type.Name);

            Dictionary <string, object> expandedProperties = new Dictionary <string, object>();

            indent += indentIncrement;
            foreach (PropertyInfo info in effect.GetType().GetProperties())
            {
                string propertyName = info.Name.ToLower();
                if (propertyName == "cacheoutput" ||
                    propertyName == "bufferprecision" ||
                    propertyName == "colorhdr" ||
                    propertyName == "issupported" ||
                    propertyName == "clampoutput" ||
                    propertyName == "name" ||
                    propertyName == "alphamode")
                {
                    continue;
                }

                object obj = info.GetValue(effect);
                if (obj != null)
                {
                    if (obj is IGraphicsEffect || obj is IList <IGraphicsEffectSource> )
                    {
                        expandedProperties.Add(info.Name, obj);
                    }
                    else
                    {
                        if (obj is CompositionEffectSourceParameter)
                        {
                            CompositionEffectSourceParameter param = (CompositionEffectSourceParameter)obj;
                            CompositionBrush sourceBrush           = brush.GetSourceParameter(param.Name);

                            string s = String.Format("{0}{1} :\r\n{0}{{\r\n", Indent(indent), info.Name);
                            sb.Append(s);
                            BrushToString(sb, indent + indentIncrement, sourceBrush);
                            sb.AppendFormat("{0}}}\r\n", Indent(indent));
                        }
                        else
                        {
                            sb.AppendFormat("{0}{1} : {2}\r\n", Indent(indent), info.Name, Helpers.ToString(obj));
                        }
                    }
                }
            }

            // Moved all of the nested source properties to the end of the list
            foreach (KeyValuePair <string, object> entry in expandedProperties)
            {
                string name = entry.Key;
                object obj  = entry.Value;

                if (obj is IGraphicsEffect)
                {
                    string s = String.Format("{0}{1} :\r\n{0}{{\r\n", Indent(indent), name);
                    sb.Append(s);
                    EffectToString(sb, indent + indentIncrement, (IGraphicsEffect)obj, brush);
                    sb.AppendFormat("{0}}}\r\n", Indent(indent));
                }
                else if (obj is IList <IGraphicsEffectSource> )
                {
                    IList <IGraphicsEffectSource> list = (IList <IGraphicsEffectSource>)obj;

                    sb.AppendFormat("{0}{1} :\r\n{0}[\r\n", Indent(indent), name);
                    foreach (IGraphicsEffectSource source in list)
                    {
                        EffectToString(sb, indent + indentIncrement, (IGraphicsEffect)source, brush);
                    }
                    sb.AppendFormat("{0}]\r\n", Indent(indent));
                }
            }

            indent -= indentIncrement;
            sb.AppendFormat("{0}}}\r\n", Indent(indent));
        }