public void SetVideoSurface(IVideoSurface videoSurface) { if (!(videoSurface is VideoSurface)) { throw new ArgumentException("Not a valid video surface"); } var canvas = (VideoSurface)videoSurface; RenderSurface = canvas; var size = new Size(canvas.ActualWidth, canvas.ActualHeight); _player.SetSurfaceSize(size); var compositor = ElementCompositionPreview.GetElementVisual(canvas).Compositor; var surface = _player.GetSurface(compositor); _spriteVisual = compositor.CreateSpriteVisual(); _spriteVisual.Size = new Vector2((float)canvas.ActualWidth, (float)canvas.ActualHeight); CompositionBrush brush = compositor.CreateSurfaceBrush(surface.CompositionSurface); _spriteVisual.Brush = brush; var container = compositor.CreateContainerVisual(); container.Children.InsertAtTop(_spriteVisual); ElementCompositionPreview.SetElementChildVisual(canvas, container); }
private SpriteVisual SetupStageSide(StageSide side) { SpriteVisual visual = _compositor.CreateSpriteVisual(); Vector3 offset = Vector3.Zero; Vector2 size = Vector2.Zero; CompositionBrush brush = null; string comment = string.Empty; switch (side) { case StageSide.Backdrop: offset = new Vector3(_stageWidth / 2, _stageBackdropHeight / 2, -1 * _stageFloorDepth); size = new Vector2(_stageWidth, _stageBackdropHeight); brush = _compositor.CreateSurfaceBrush(_surfaceFactory.CreateUriSurface(new Uri(BackgropImage), new Size(_stageWidth, _stageBackdropHeight)).Surface); comment = "Backdrop"; break; case StageSide.Floor: offset = new Vector3(_stageWidth / 2, _stageBackdropHeight * 1.5f, -1 * _stageFloorDepth); size = new Vector2(_stageWidth, _stageFloorDepth); brush = _compositor.CreateSurfaceBrush(_surfaceFactory.CreateUriSurface(new Uri(FloorImage), new Size(_stageWidth, _stageFloorDepth)).Surface); comment = "Floor"; visual.RotationAngleInDegrees = 90f; visual.RotationAxis = _rotationAxisX; break; } visual.Offset = offset; visual.Size = size; visual.Brush = brush; visual.Comment = comment; _stageContainer.Children.InsertAtTop(visual); return(visual); }
public void Remove() { // Remove effect if (null != this._foregroundElement) { this._foregroundElement.Opacity = 1.0f; } if (null != this._blurVisual) { this._blurVisual.Brush = null; this._blurVisual?.StopAnimation("Size"); } this._bindSizeAnimation?.Dispose(); this._bindSizeAnimation = null; this._effectBrush?.Dispose(); this._effectBrush = null; this._backdropBrush?.Dispose(); this._backdropBrush = null; this._blurEffect?.Dispose(); this._blurEffect = null; // NOTE: No API to remove the child visual //this._blurVisual?.Dispose(); //this._blurVisual = null; }
private void ClearResources() { _imageBrush?.Dispose(); _imageBrush = null; _surfaceSource?.Dispose(); _surfaceSource = null; }
public BackDrop() { m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement); Compositor = m_rootVisual.Compositor; m_blurVisual = Compositor.CreateSpriteVisual(); if (ApiInformation.IsMethodPresent("Windows.UI.Composition.Compositor", "CreateBackdropBrush")) { var brush = BuildBlurBrush(); brush.SetSourceParameter("source", Compositor.CreateBackdropBrush()); m_blurBrush = brush; m_blurVisual.Brush = m_blurBrush; } else { m_blurBrush = Compositor.CreateColorBrush(Colors.Black); m_blurVisual.Brush = m_blurBrush; } ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual); this.Loading += OnLoading; this.Unloaded += OnUnloaded; }
private void UpdateShadowMask() { if (_castingElement != null) { CompositionBrush mask = null; if (_castingElement is Image) { mask = ((Image)_castingElement).GetAlphaMask(); } else if (_castingElement is Shape) { mask = ((Shape)_castingElement).GetAlphaMask(); } else if (_castingElement is TextBlock) { mask = ((TextBlock)_castingElement).GetAlphaMask(); } _dropShadow.Mask = mask; } else { _dropShadow.Mask = null; } }
private void InitializeDropShadow(UIElement shadowHost, CompositionBrush shadowTargetBrush) { Visual hostVisual = ElementCompositionPreview.GetElementVisual(shadowHost); Compositor compositor = hostVisual.Compositor; // Create a drop shadow var dropShadow = compositor.CreateDropShadow(); dropShadow.Color = Color.FromArgb(102, 0, 0, 0); dropShadow.BlurRadius = 4.0f; // Associate the shape of the shadow with the shape of the target element dropShadow.Mask = shadowTargetBrush; // Create a Visual to hold the shadow var shadowVisual = compositor.CreateSpriteVisual(); shadowVisual.Shadow = dropShadow; // Add the shadow as a child of the host in the visual tree ElementCompositionPreview.SetElementChildVisual(shadowHost, shadowVisual); // Make sure size of shadow host and shadow visual always stay in sync var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); shadowVisual.StartAnimation("Size", bindSizeAnimation); }
public BackDrop() { #if DEBUG if (DesignMode.DesignModeEnabled) { return; } #endif m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement); Compositor = m_rootVisual.Compositor; m_blurVisual = Compositor.CreateSpriteVisual(); 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; ElementCompositionPreview.SetElementChildVisual(this as UIElement, m_blurVisual); this.Loading += OnLoading; this.Unloaded += OnUnloaded; }
protected override void OnConnected() { // Delay creating composition resources until they're required. if (CompositionBrush == null) { var backdrop = Window.Current.Compositor.CreateBackdropBrush(); // Use a Win2D blur affect applied to a CompositionBackdropBrush. var graphicsEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = (float)this.BlurAmount, Source = new CompositionEffectSourceParameter("backdrop") }; var effectFactory = Window.Current.Compositor.CreateEffectFactory(graphicsEffect, new[] { "Blur.BlurAmount" }); var effectBrush = effectFactory.CreateBrush(); effectBrush.SetSourceParameter("backdrop", backdrop); CompositionBrush = effectBrush; if (BlurAmountExpression != null) { CompositionBrush.StartAnimation("Blur.BlurAmount", BlurAmountExpression); } } }
/// <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; }
private void UpdateShadowMask() { if (shadowVisual == null || !(shadowVisual.Brush is CompositionEffectBrush effectBrush)) { return; } CompositionBrush newMask = null; if (IsMasked && Mask != null) { newMask = Mask; } else if (IsMasked && Content != null) { if (Content is Image) { newMask = ((Image)Content).GetAlphaMask(); } else if (Content is Shape) { newMask = ((Shape)Content).GetAlphaMask(); } else if (Content is TextBlock) { newMask = ((TextBlock)Content).GetAlphaMask(); } } try { effectBrush.SetSourceParameter(effectSourceName, newMask); } catch (Exception) { } }
private void SetImageUri() { ClearResources(); if (ImageUri == null) { return; } var uri = ImageUri; try { var src = CreateSurface(); _surfaceSource = src as IDisposable; var surfaceBrush = _compositor.CreateSurfaceBrush(src); surfaceBrush.VerticalAlignmentRatio = 0.0f; surfaceBrush.HorizontalAlignmentRatio = 0.0f; surfaceBrush.Stretch = CompositionStretch.None; _imageBrush = surfaceBrush; UpdateBrush(); } catch { // no image for you, soz. } }
public CompositionImage() { this.DefaultStyleKey = typeof(CompositionImage); this.Background = new SolidColorBrush(Colors.Transparent); this._stretchMode = CompositionStretch.Uniform; this.Loading += CompImage_Loading; this.Unloaded += CompImage_Unloaded; this.SizeChanged += CompImage_SizeChanged; _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; // Intialize the statics as needed if (!_staticsInitialized) { _defaultPlaceholderBrush = _compositor.CreateColorBrush(Colors.DarkGray); TimeSpan duration = TimeSpan.FromMilliseconds(1000); _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeOutAnimation.InsertKeyFrame(0, 1); _fadeOutAnimation.InsertKeyFrame(1, 0); _fadeOutAnimation.Duration = duration; _scaleAnimation = _compositor.CreateVector2KeyFrameAnimation(); _scaleAnimation.InsertKeyFrame(0, new Vector2(1.25f, 1.25f)); _scaleAnimation.InsertKeyFrame(1, new Vector2(1, 1)); _scaleAnimation.Duration = duration; _staticsInitialized = true; } _placeholderDelay = TimeSpan.FromMilliseconds(50); _surfaceBrush = _compositor.CreateSurfaceBrush(null); }
protected override void OnDisconnected() { if (CompositionBrush != null) { IsConnected = false; canvasDevice.Dispose(); canvasDevice = null; graphicsDevice.Dispose(); graphicsDevice = null; surfaceBrush1.Dispose(); surfaceBrush1 = null; surfaceBrush2.Dispose(); surfaceBrush2 = null; surface1.Dispose(); surface1 = null; surface2.Dispose(); surface2 = null; Source1Animation.Dispose(); Source1Animation = null; Source2Animation.Dispose(); Source2Animation = null; colorBrush1.Dispose(); colorBrush1 = null; colorBrush2.Dispose(); colorBrush2 = null; CompositionBrush.Dispose(); CompositionBrush = null; } }
protected override void OnDisconnected() { CoreWindow.GetForCurrentThread().Activated -= AcrylicBrush_Activated; CoreWindow.GetForCurrentThread().VisibilityChanged -= AcrylicBrush_VisibilityChanged; CompositionBrush.Dispose(); CompositionBrush = null; }
private void UpdateShadowMask() { if (!IsSupported) { return; } if (Content != null) { CompositionBrush mask = null; if (Content is Image) { mask = ((Image)Content).GetAlphaMask(); } else if (Content is Shape) { mask = ((Shape)Content).GetAlphaMask(); } else if (Content is TextBlock) { mask = ((TextBlock)Content).GetAlphaMask(); } _dropShadow.Mask = mask; } else { _dropShadow.Mask = null; } }
internal CompositionBrush[] GeneratePerBarLinearGradient(int numBrushes, List <Color> colors) { var lgb = compositor.CreateLinearGradientBrush(); lgb.RotationAngleInDegrees = 45; int i = 0; foreach (Color color in colors) { float offset = i / ((float)colors.Count - 1); var stop = compositor.CreateColorGradientStop(offset, color); i++; lgb.ColorStops.Add(stop); } var brushes = new CompositionBrush[numBrushes]; for (int j = 0; j < numBrushes; j++) { brushes[j] = lgb; } return(brushes); }
protected override void OnAttached() { var blurAmount = (double)Element.GetValue(RoutingEffects.ViewBlur.BlurAmountProperty); rootVisual = ElementCompositionPreview.GetElementVisual(Container); Compositor = rootVisual.Compositor; blurVisual = Compositor.CreateSpriteVisual(); var brush = BuildBlurBrush(); brush.SetSourceParameter("source", Compositor.CreateBackdropBrush()); blurBrush = brush; blurVisual.Brush = blurBrush; ElementCompositionPreview.SetElementChildVisual(Container, blurVisual); Container.Loading += OnLoading; Container.Unloaded += OnUnloaded; blurBrush.Properties.InsertScalar("Blur.BlurAmount", (float)blurAmount); rootVisual.Properties.InsertScalar("BlurAmount", (float)blurAmount); SetUpPropertySetExpressions(); }
private void UpdateShadowMask() { if (Content != null && IsMasked) { CompositionBrush mask = null; if (Content is Image image) { mask = image.GetAlphaMask(); } else if (Content is Shape shape) { mask = shape.GetAlphaMask(); } else if (Content is TextBlock textBlock) { mask = textBlock.GetAlphaMask(); } _dropShadow.Mask = mask; } else { _dropShadow.Mask = null; } }
static public void SetBrush(this UIElement element, CompositionBrush brush) { var spriteVisual = CreateSpriteVisual(element); spriteVisual.Brush = brush; ElementCompositionPreview.SetElementChildVisual(element, spriteVisual); }
public BackDrop() { m_rootVisual = ElementCompositionPreview.GetElementVisual(this as UIElement); Compositor = m_rootVisual.Compositor; m_blurVisual = Compositor.CreateSpriteVisual(); #if SDKVERSION_INSIDER 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; }
// Test noise cache is functional: // 1. Validate two AcrylicBrushes both use the same noise // 2. Validate destroying these AB's and creating a new one still use the same noise. void RunAcrylicNoiseCache() { AcrylicBrush acrylicBrush1 = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.Backdrop, FallbackColor = Colors.Blue, TintColor = Colors.Green, TintOpacity = 0.1 }; AcrylicBrush acrylicBrush2 = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.Backdrop, FallbackColor = Colors.Blue, TintColor = Colors.Bisque, TintOpacity = 1 }; Rectangle1.Fill = acrylicBrush1; _acrylicTestApi.AcrylicBrush = acrylicBrush1; CompositionBrush noiseBrush1 = UpdateNoiseBrush(); if (_acrylicTestApi.IsUsingAcrylicBrush == false) { TestResult.Text = "AcrylicNoiseCache: Skipped"; return; } Rectangle1.Stroke = acrylicBrush2; _acrylicTestApi.AcrylicBrush = acrylicBrush2; CompositionBrush noiseBrush2 = UpdateNoiseBrush(); // Make sure two brushes conccurrently in tree use the same noise bool result1 = Object.ReferenceEquals(noiseBrush1, noiseBrush2); // Destroy existing brushes Rectangle1.Fill = new SolidColorBrush(Colors.PaleGoldenrod); Rectangle1.Stroke = new SolidColorBrush(Colors.PaleGoldenrod); acrylicBrush1 = null; acrylicBrush2 = null; _acrylicTestApi.AcrylicBrush = null; GC.Collect(); AcrylicBrush acrylicBrush3 = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.Backdrop, FallbackColor = Colors.Blue, TintColor = Colors.DarkOliveGreen, TintOpacity = 0.25 }; Rectangle1.Fill = acrylicBrush3; _acrylicTestApi.AcrylicBrush = acrylicBrush3; CompositionBrush noiseBrush3 = UpdateNoiseBrush(); // Make sure new brush still uses the same noise bool result2 = Object.ReferenceEquals(noiseBrush2, noiseBrush3); bool result = result1 && result2; string resultString = System.String.Format("({0},{1})", result1, result2); TestResult.Text = "AcrylicNoiseCache: " + (result ? "Passed" : ("Failed " + resultString)); }
public BackDrop() { 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; }
private static CompositionBrush CreateBlurBrush() { if (_blurBrush != null) { return(_blurBrush); } var blurEffect = new GaussianBlurEffect { Name = "Blur", BlurAmount = 15.0f, BorderMode = EffectBorderMode.Hard, Optimization = EffectOptimization.Balanced, Source = new CompositionEffectSourceParameter("source") }; var blendEffect = new BlendEffect { Background = blurEffect, Foreground = new ColorSourceEffect { Name = "Color", Color = Color.FromArgb(64, 0, 0, 0) }, Mode = BlendEffectMode.SoftLight }; var blurEffectFactory = _compositor.CreateEffectFactory(blendEffect); var blurBrush = blurEffectFactory.CreateBrush(); var backdropBrush = _compositor.CreateBackdropBrush(); blurBrush.SetSourceParameter("source", backdropBrush); _blurBrush = blurBrush; return(_blurBrush); }
/// <summary> /// Deconstructs the Composition Brush. /// </summary> protected override void OnDisconnected() { base.OnDisconnected(); if (_device != null) { _device.DeviceLost -= CanvasDevice_DeviceLost; _device = null; } if (_graphics != null) { _graphics.RenderingDeviceReplaced -= CanvasDevice_RenderingDeviceReplaced; _graphics = null; } // Dispose of composition resources when no longer in use. if (CompositionBrush != null) { CompositionBrush.Dispose(); CompositionBrush = null; } if (_surfaceBrush != null) { _surfaceBrush.Dispose(); _surfaceBrush = null; } }
protected override void OnDisconnected() { Window.Current.SizeChanged -= OnWindowSizeChanged; CompositionBrush?.Dispose(); CompositionBrush = null; }
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); }
private void ApplyInternal( float blurAmount) { // SOURCE: https://msdn.microsoft.com/en-us/windows/uwp/graphics/using-the-visual-layer-with-xaml // Get the host visual and compositor Visual hostVisual = ElementCompositionPreview.GetElementVisual(this._foregroundElement); Compositor compositor = hostVisual.Compositor; // Create a blur effect this._blurEffect = new GaussianBlurEffect { BlurAmount = blurAmount, 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 = global::Windows.UI.Color.FromArgb(255, 0xee, 0xee, 0xee), } } }; // Create an instance of the effect using (var effectFactory = compositor.CreateEffectFactory(this._blurEffect)) { this._effectBrush = effectFactory.CreateBrush(); } // Set the effect source to a CompositionBackdropBrush this._backdropBrush = compositor.CreateBackdropBrush(); this._effectBrush.SetSourceParameter("backdropBrush", this._backdropBrush); // Get any existing child sprite visual this._blurVisual = (SpriteVisual)ElementCompositionPreview.GetElementChildVisual(this._foregroundElement); if (null == this._blurVisual) { // Create a sprite visual to contain the effect this._blurVisual = compositor.CreateSpriteVisual(); this._blurVisual.Brush = this._effectBrush; // Add the sprite visual as a child of the host visual in the visual tree ElementCompositionPreview.SetElementChildVisual(this._foregroundElement, this._blurVisual); } else { this._blurVisual.Brush = this._effectBrush; } // Make sure size of the host and effect visuals always stay in sync this._bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size"); this._bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual); this._blurVisual.StartAnimation("Size", this._bindSizeAnimation); }
private void ApplyForeground() { if (Foreground is SolidColorBrush solid) { _foreground = Window.Current.Compositor.CreateColorBrush(solid.Color); OnValueChanged(Value, Value); } }
protected override void OnDisconnected() { if (CompositionBrush != null) { CompositionBrush.Dispose(); CompositionBrush = null; } }
private Bar[] CreateBars(float[] data) { //Clear hashmap barValueMap.Clear(); // TODO break out into separate UpdateColors method? var barBrushHelper = new BarGraphUtility.BarBrushHelper(_compositor); CompositionBrush[] brushes = new CompositionBrush[data.Length]; switch (_graphBarStyle) { case GraphBarStyle.Single: brushes = barBrushHelper.GenerateSingleColorBrush(data.Length, _graphBarColors[0]); break; case GraphBarStyle.Random: brushes = barBrushHelper.GenerateRandomColorBrushes(data.Length); break; case GraphBarStyle.PerBarLinearGradient: brushes = barBrushHelper.GeneratePerBarLinearGradient(data.Length, _graphBarColors); break; case GraphBarStyle.AmbientAnimatingPerBarLinearGradient: brushes = barBrushHelper.GenerateAmbientAnimatingPerBarLinearGradient(data.Length, _graphBarColors); break; case GraphBarStyle.SharedLinearGradient: brushes = barBrushHelper.GenerateSharedLinearGradient(data.Length); break; case GraphBarStyle.TintedBlur: brushes = barBrushHelper.GenerateTintedBlur(data.Length); break; default: brushes = barBrushHelper.GenerateSingleColorBrush(data.Length, _graphBarColors[0]); break; } var maxValue = GetMaxBarValue(data); var bars = new Bar[data.Length]; for (int i = 0; i < data.Length; i++) { var xOffset = _shapeGraphOffsetX + _barSpacing + (_barWidth + _barSpacing) * i; var height = GetAdjustedBarHeight(maxValue, _graphData[i]); var bar = new BarGraphUtility.Bar(_compositor, _shapeGraphContainerHeight, height, _barWidth, "something", _graphData[i], brushes[i]); bar.Root.Offset = new System.Numerics.Vector3(xOffset, _shapeGraphContainerHeight + _shapeGraphOffsetY, 0); barValueMap.Add(i, bar); bars[i] = bar; } return(bars); }
public CompositionImage() { this.DefaultStyleKey = typeof(CompositionImage); this.Background = new SolidColorBrush(Colors.Transparent); this._stretchMode = CompositionStretch.Uniform; this.Loading += CompImage_Loading; this.Unloaded += CompImage_Unloaded; this.SizeChanged += CompImage_SizeChanged; _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor; // Intialize the statics as needed if (!_staticsInitialized) { _defaultPlaceholderBrush = _compositor.CreateColorBrush(Colors.DarkGray); TimeSpan duration = TimeSpan.FromMilliseconds(1000); _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation(); _fadeOutAnimation.InsertKeyFrame(0, 1); _fadeOutAnimation.InsertKeyFrame(1, 0); _fadeOutAnimation.Duration = duration; _scaleAnimation = _compositor.CreateVector2KeyFrameAnimation(); _scaleAnimation.InsertKeyFrame(0, new Vector2(1.25f, 1.25f)); _scaleAnimation.InsertKeyFrame(1, new Vector2(1, 1)); _scaleAnimation.Duration = duration; _staticsInitialized = true; } // Initialize the surface loader if needed if (!SurfaceLoader.IsInitialized) { SurfaceLoader.Initialize(ElementCompositionPreview.GetElementVisual(this).Compositor); } _placeholderDelay = TimeSpan.FromMilliseconds(50); _surfaceBrush = _compositor.CreateSurfaceBrush(null); }
private void SamplePage_Loaded(object sender, RoutedEventArgs e) { //get interop compositor _compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor; //get image brush and image visual _image = Image; _imageVisual = _image.SpriteVisual; _imageBrush = _image.SurfaceBrush; //load height map to surface brush _imageLoader = ImageLoaderFactory.CreateImageLoader(_compositor); _normalMapSurface = _imageLoader.CreateManagedSurfaceFromUri(new Uri("ms-appx:///Samples/SDK Insider/ImageLightingPlayground/rocks_NM_height.png")); _normalMapBrush = _compositor.CreateSurfaceBrush(); _normalMapBrush.Surface = _normalMapSurface.Surface; //set up lighting effects: // DISTANT DIFFUSE // Effect description var distantDiffuseDesc = new ArithmeticCompositeEffect { Source1 = new CompositionEffectSourceParameter("Image"), Source2 = new DistantDiffuseEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Source1Amount = 0, Source2Amount = 0, MultiplyAmount = 1 }; //Create (Light)EffectBrush from EffectFactory and specify animatable properties _distantDiffuseLightBrush = _compositor.CreateEffectFactory( distantDiffuseDesc, new[] { "light.Azimuth", "light.Elevation", "light.DiffuseAmount", "light.LightColor" } ).CreateBrush(); //set source parameters to image and normal map _distantDiffuseLightBrush.SetSourceParameter( "Image", _imageBrush); _distantDiffuseLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); // DISTANT SPECULAR // Effect description var distantSpecularDesc = new BlendEffect { Foreground = new CompositionEffectSourceParameter("Image"), Background = new DistantSpecularEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Mode = BlendEffectMode.Screen }; //Create (Light)EffectBrush from EffectFactory and specify animatable properties _distantSpecularLightBrush = _compositor.CreateEffectFactory( distantSpecularDesc, new[] { "light.Azimuth", "light.Elevation", "light.SpecularExponent", "light.SpecularAmount", "light.LightColor" } ).CreateBrush(); //set source parameters to image and normal map _distantSpecularLightBrush.SetSourceParameter( "Image", _imageBrush); _distantSpecularLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); // POINT DIFFUSE // Effect description var pointDiffuseDesc = new ArithmeticCompositeEffect { Source1 = new CompositionEffectSourceParameter("Image"), Source2 = new PointDiffuseEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Source1Amount = 0, Source2Amount = 0, MultiplyAmount = 1 }; //Create (Light)EffectBrush from EffectFactory and specify animatable properties _pointDiffuseLightBrush = _compositor.CreateEffectFactory( pointDiffuseDesc, new[] { "light.LightPosition", "light.DiffuseAmount", "light.HeightMapScale", "light.LightColor" } ).CreateBrush(); // set source parameters to image and normal map _pointDiffuseLightBrush.SetSourceParameter( "Image", _imageBrush); _pointDiffuseLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); // POINT SPECUALR // Effect description var pointSpecularDesc = new BlendEffect { Foreground = new CompositionEffectSourceParameter("Image"), Background = new PointSpecularEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Mode = BlendEffectMode.Screen }; //Create (Light)EffectBrush from EffectFactory and specify animatable properties _pointSpecularLightBrush = _compositor.CreateEffectFactory( pointSpecularDesc, new[] { "light.LightPosition", "light.SpecularExponent", "light.SpecularAmount", "light.HeightMapScale", "light.LightColor" } ).CreateBrush(); // set source parameters to image and normal map _pointSpecularLightBrush.SetSourceParameter( "Image", _imageBrush); _pointSpecularLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); //SPOT DIFFUSE // Effect description var spotDiffuseDesc = new ArithmeticCompositeEffect { Source1 = new CompositionEffectSourceParameter("Image"), Source2 = new SpotDiffuseEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Source1Amount = 0, Source2Amount = 0, MultiplyAmount = 1 }; //Create (Light)EffectBrush from EffectFactory and specify animatable properties _spotDiffuseLightBrush = _compositor.CreateEffectFactory( spotDiffuseDesc, new[] { "light.LightPosition", "light.LightTarget", "light.Focus", "light.LimitingConeAngle", "light.DiffuseAmount", "light.HeightMapScale", "light.LightColor" } ).CreateBrush(); // set source parameters to image and normal map _spotDiffuseLightBrush.SetSourceParameter( "Image", _imageBrush); _spotDiffuseLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); // SPOT SPECULAR // Effect description var spotSpecularDesc = new BlendEffect { Foreground = new CompositionEffectSourceParameter("Image"), Background = new SpotSpecularEffect { Name = "light", Source = new CompositionEffectSourceParameter("NormalMap") }, Mode = BlendEffectMode.Screen }; //Create (Light)EffectBrush from EffectFactory _spotSpecularLightBrush = _compositor.CreateEffectFactory( spotSpecularDesc, new[] { "light.LightPosition", "light.LightTarget", "light.Focus", "light.LimitingConeAngle", "light.SpecularExponent", "light.SpecularAmount", "light.HeightMapScale", "light.LightColor" } ).CreateBrush(); // set source parameters to image and normal map _spotSpecularLightBrush.SetSourceParameter( "Image", _imageBrush); _spotSpecularLightBrush.SetSourceParameter( "NormalMap", _normalMapBrush); // For simplying UI states switch, put light parameter grids in an array _lightParamsGrids = new Grid[(int)LightType.NumLightTypes]; _lightParamsGrids[(int)LightType.NoLight] = null; _lightParamsGrids[(int)LightType.DistantDiffuse] = DistantDiffuseParams; _lightParamsGrids[(int)LightType.DistantSpecular] = DistantSpecularParams; _lightParamsGrids[(int)LightType.PointDiffuse] = PointDiffuseParams; _lightParamsGrids[(int)LightType.PointSpecular] = PointSpecularParams; _lightParamsGrids[(int)LightType.SpotDiffuse] = SpotDiffuseParams; _lightParamsGrids[(int)LightType.SpotSpecular] = SpotSpecularParams; // Same as grids _lightBrushes = new CompositionBrush[(int)LightType.NumLightTypes]; _lightBrushes[(int)LightType.NoLight] = _imageBrush; _lightBrushes[(int)LightType.DistantDiffuse] = _distantDiffuseLightBrush; _lightBrushes[(int)LightType.DistantSpecular] = _distantSpecularLightBrush; _lightBrushes[(int)LightType.PointDiffuse] = _pointDiffuseLightBrush; _lightBrushes[(int)LightType.PointSpecular] = _pointSpecularLightBrush; _lightBrushes[(int)LightType.SpotDiffuse] = _spotDiffuseLightBrush; _lightBrushes[(int)LightType.SpotSpecular] = _spotSpecularLightBrush; //Initialize values for all light types InitializeValues(); }
private void MainGridLoaded(object sender, RoutedEventArgs e) { m_compositor = ElementCompositionPreview.GetElementVisual(MainGrid).Compositor; m_root = m_compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(MainGrid, m_root); Size imageSize; m_noEffectBrush = CreateBrushFromAsset( "Bruno'sFamily2015 (13)-X2.jpg", out imageSize); m_imageAspectRatio = (imageSize.Width == 0 && imageSize.Height == 0) ? 1 : imageSize.Width / imageSize.Height; m_sprite = m_compositor.CreateSpriteVisual(); ResizeImage(new Size(MainGrid.ActualWidth, MainGrid.ActualHeight)); m_root.Children.InsertAtTop(m_sprite); // Image with alpha channel as an mask. var alphaMaskEffectDesc = new CompositeEffect { Mode = CanvasComposite.DestinationIn, Sources = { new CompositionEffectSourceParameter("Image"), new Transform2DEffect { Name = "MaskTransform", Source = new CompositionEffectSourceParameter("Mask") } } }; m_alphaMaskEffectBrush = m_compositor.CreateEffectFactory( alphaMaskEffectDesc, new[] { "MaskTransform.TransformMatrix" } ).CreateBrush(); m_alphaMaskEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); m_alphaMaskEffectBrush.SetSourceParameter( "Mask", CreateBrushFromAsset("CircleMask.png")); // Arithmetic operations between two images. var arithmeticEffectDesc = new ArithmeticCompositeEffect { Name = "effect", ClampOutput = false, Source1 = new CompositionEffectSourceParameter("Source1"), Source2 = new CompositionEffectSourceParameter("Source2") }; m_arithmeticEffectBrush = m_compositor.CreateEffectFactory( arithmeticEffectDesc, new[] { "effect.MultiplyAmount", "effect.Source1Amount", "effect.Source2Amount", "effect.Offset" } ).CreateBrush(); m_arithmeticEffectBrush.SetSourceParameter( "Source1", m_noEffectBrush); m_arithmeticEffectBrush.SetSourceParameter( "Source2", CreateBrushFromAsset("_P2A8041.jpg")); // Creates a blend effect that combines two images. var foregroundBrush = CreateBrushFromAsset("Checkerboard_100x100.png"); m_blendEffectBrushes = new CompositionEffectBrush[m_supportedBlendModes.Length]; for (int i = 0; i < m_supportedBlendModes.Length; i++) { var blendEffectDesc = new BlendEffect { Mode = m_supportedBlendModes[i], Background = new CompositionEffectSourceParameter("Background"), Foreground = new CompositionEffectSourceParameter("Foreground") }; m_blendEffectBrushes[i] = m_compositor.CreateEffectFactory( blendEffectDesc ).CreateBrush(); m_blendEffectBrushes[i].SetSourceParameter( "Background", m_noEffectBrush); m_blendEffectBrushes[i].SetSourceParameter( "Foreground", foregroundBrush); } // Generates an image containing a solid color. var colorSourceEffectDesc = new ColorSourceEffect // FloodEffect { Name = "effect" }; m_colorSourceEffectBrush = m_compositor.CreateEffectFactory( colorSourceEffectDesc, new[] { "effect.Color" } ).CreateBrush(); // Changes the contrast of an image. var contrastEffectDesc = new ContrastEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_contrastEffectBrush = m_compositor.CreateEffectFactory( contrastEffectDesc, new[] { "effect.Contrast" } ).CreateBrush(); m_contrastEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Changes the exposure of an image. var exposureEffectDesc = new ExposureEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_exposureEffectBrush = m_compositor.CreateEffectFactory( exposureEffectDesc, new[] { "effect.Exposure" } ).CreateBrush(); m_exposureEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Alters the colors of an image by applying a per-channel gamma transfer function. var gammaTransferEffectDesc = new GammaTransferEffect { Name = "effect", RedDisable = false, GreenDisable = false, BlueDisable = false, AlphaDisable = false, Source = new CompositionEffectSourceParameter("Image") }; m_gammaTransferEffectBrush = m_compositor.CreateEffectFactory( gammaTransferEffectDesc, new[] { "effect.RedAmplitude", "effect.RedExponent", "effect.RedOffset", "effect.GreenAmplitude", "effect.GreenExponent", "effect.GreenOffset", "effect.BlueAmplitude", "effect.BlueExponent", "effect.BlueOffset" } ).CreateBrush(); m_gammaTransferEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Converts an image to 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); // Alters the color of an image by rotating its hue values. var hueRotationEffectDesc = new HueRotationEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_hueRotationEffectBrush = m_compositor.CreateEffectFactory( hueRotationEffectDesc, new[] { "effect.Angle" } ).CreateBrush(); m_hueRotationEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Inverts the colors of an image. var invertEffectDesc = new InvertEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_invertEffectBrush = m_compositor.CreateEffectFactory( invertEffectDesc ).CreateBrush(); m_invertEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Alters the saturation of an image. var saturationEffectDesc = new SaturationEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_saturateEffectBrush = m_compositor.CreateEffectFactory( saturationEffectDesc, new[] { "effect.Saturation" } ).CreateBrush(); m_saturateEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Converts an image to sepia tones. var supportedAlphaModes = new[] { CanvasAlphaMode.Premultiplied, CanvasAlphaMode.Straight }; m_sepiaEffectBrushes = new CompositionEffectBrush[supportedAlphaModes.Length]; for (int i = 0; i < supportedAlphaModes.Length; i++) { var sepiaEffectDesc = new SepiaEffect { Name = "effect", AlphaMode = supportedAlphaModes[i], Source = new CompositionEffectSourceParameter("Image") }; m_sepiaEffectBrushes[i] = m_compositor.CreateEffectFactory( sepiaEffectDesc, new[] { "effect.Intensity" } ).CreateBrush(); m_sepiaEffectBrushes[i].SetSourceParameter( "Image", m_noEffectBrush); } // Adjusts the temperature and/or tint of an image. var temperatureAndTintEffectDesc = new TemperatureAndTintEffect { Name = "effect", Source = new CompositionEffectSourceParameter("Image") }; m_temperatureAndTintEffectBrush = m_compositor.CreateEffectFactory( temperatureAndTintEffectDesc, new[] { "effect.Temperature", "effect.Tint" } ).CreateBrush(); m_temperatureAndTintEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // Applies a 2D affine transform matrix to an image. var transform2DEffectDesc = new Transform2DEffect { TransformMatrix = new Matrix3x2( -1, 0, 0, 1, m_sprite.Size.X, 0), Source = new CompositionEffectSourceParameter("Image") }; m_transform2DEffectBrush = m_compositor.CreateEffectFactory( transform2DEffectDesc ).CreateBrush(); m_transform2DEffectBrush.SetSourceParameter( "Image", m_noEffectBrush); // For simplying UI states switch, put effect parameter grids in an array m_effectParamsGrids = new Grid[(int)EffectType.NumEffectTypes]; m_effectParamsGrids[(int)EffectType.NoEffect] = null; m_effectParamsGrids[(int)EffectType.AlphaMask] = AlphaMaskParams; m_effectParamsGrids[(int)EffectType.Arithmetic] = ArithmeticParams; m_effectParamsGrids[(int)EffectType.Blend] = BlendParams; m_effectParamsGrids[(int)EffectType.ColorSource] = ColorSourceParams; m_effectParamsGrids[(int)EffectType.Contrast] = ContrastParams; m_effectParamsGrids[(int)EffectType.Exposure] = ExposureParams; m_effectParamsGrids[(int)EffectType.GammaTransfer] = GammaTransferParams; m_effectParamsGrids[(int)EffectType.Grayscale] = null; m_effectParamsGrids[(int)EffectType.HueRotation] = HueRotationParams; m_effectParamsGrids[(int)EffectType.Invert] = null; m_effectParamsGrids[(int)EffectType.Saturation] = SaturationParams; m_effectParamsGrids[(int)EffectType.Sepia] = SepiaParams; m_effectParamsGrids[(int)EffectType.TemperatureAndTint] = TemperatureAndTintParams; m_effectParamsGrids[(int)EffectType.Transform2D] = null; // Same as grids m_effectBrushes = new CompositionBrush[(int)EffectType.NumEffectTypes]; m_effectBrushes[(int)EffectType.NoEffect] = m_noEffectBrush; m_effectBrushes[(int)EffectType.AlphaMask] = m_alphaMaskEffectBrush; m_effectBrushes[(int)EffectType.Arithmetic] = m_arithmeticEffectBrush; m_effectBrushes[(int)EffectType.Blend] = m_blendEffectBrushes[m_activeBlendMode]; m_effectBrushes[(int)EffectType.ColorSource] = m_colorSourceEffectBrush; m_effectBrushes[(int)EffectType.Contrast] = m_contrastEffectBrush; m_effectBrushes[(int)EffectType.Exposure] = m_exposureEffectBrush; m_effectBrushes[(int)EffectType.GammaTransfer] = m_gammaTransferEffectBrush; m_effectBrushes[(int)EffectType.Grayscale] = m_grayscaleEffectBrush; m_effectBrushes[(int)EffectType.HueRotation] = m_hueRotationEffectBrush; m_effectBrushes[(int)EffectType.Invert] = m_invertEffectBrush; m_effectBrushes[(int)EffectType.Saturation] = m_saturateEffectBrush; m_effectBrushes[(int)EffectType.Sepia] = m_sepiaEffectBrushes[m_activeSepiaAlphaMode]; m_effectBrushes[(int)EffectType.TemperatureAndTint] = m_temperatureAndTintEffectBrush; m_effectBrushes[(int)EffectType.Transform2D] = m_transform2DEffectBrush; this.InitializeValues(); }