/// <inheritdoc cref="XamlCompositionBrushBase"/> protected override void OnConnected() { if (CompositionBrush == null) { // Effects setup SceneLightingEffect sceneLightingEffect = new SceneLightingEffect // Base lighting effect { Name = "Light", SpecularShine = (float)SpecularShine, SpecularAmount = (float)SpecularAmount, DiffuseAmount = (float)DiffuseAmount, AmbientAmount = 0 }; _LuminanceEffect = new LuminanceToAlphaEffect { Source = sceneLightingEffect }; // Map the bright areas of the light to an opacity mask _InverseEffect = new InvertEffect { Source = _LuminanceEffect }; // Invert the colors to make the brighter areas white _Factory = Window.Current.Compositor.CreateEffectFactory(_InverseEffect, new[] { "Light.DiffuseAmount", "Light.SpecularShine", "Light.SpecularAmount" }); // Create and store the brush _Brush = _Factory.CreateBrush(); CompositionBrush = _Brush; } base.OnConnected(); }
private void Page_Loaded(object sender, RoutedEventArgs e) { //控件事件 lta = new LuminanceToAlphaEffect { Source = App.Model.SecondSourceRenderTarget, }; Render(); }
private ICanvasImage CreateLuminanceToAlpha() { var contrastAdjustedTiger = new LinearTransferEffect { Source = bitmapTiger, RedOffset = -3, GreenOffset = -3, BlueOffset = -3, }; var tigerAlpha = new LuminanceToAlphaEffect { Source = contrastAdjustedTiger }; var tigerAlphaWithWhiteRgb = new LinearTransferEffect { Source = tigerAlpha, RedOffset = 1, GreenOffset = 1, BlueOffset = 1, RedSlope = 0, GreenSlope = 0, BlueSlope = 0, }; var recombinedRgbAndAlpha = new ArithmeticCompositeEffect { Source1 = tigerAlphaWithWhiteRgb, Source2 = bitmapTiger, }; var movedTiger = new Transform2DEffect { Source = recombinedRgbAndAlpha }; const float turbulenceSize = 128; var backgroundImage = new CropEffect { Source = new TileEffect { Source = new TurbulenceEffect { Octaves = 8, Size = new Vector2(turbulenceSize), Tileable = true }, SourceRectangle= new Rect(0, 0, turbulenceSize, turbulenceSize) }, SourceRectangle = new Rect((bitmapTiger.Size.ToVector2() * -0.5f).ToPoint(), (bitmapTiger.Size.ToVector2() * 1.5f).ToPoint()) }; var tigerOnBackground = new BlendEffect { Foreground = movedTiger, Background = backgroundImage }; // Animation moves the alpha bitmap around, and alters color transfer settings to change how solid it is. animationFunction = elapsedTime => { contrastAdjustedTiger.RedSlope = contrastAdjustedTiger.GreenSlope = contrastAdjustedTiger.BlueSlope = ((float)Math.Sin(elapsedTime * 0.9) + 2) * 3; var dx = (float)Math.Cos(elapsedTime) * 50; var dy = (float)Math.Sin(elapsedTime) * 50; movedTiger.TransformMatrix = Matrix3x2.CreateTranslation(dx, dy); }; return tigerOnBackground; }
private ICanvasImage CreateLighting() { var heightMap = new LuminanceToAlphaEffect { Source = bitmapTiger }; var distantDiffuseEffect = new DistantDiffuseEffect { Source = heightMap, HeightMapScale = 2 }; var distantSpecularEffect = new DistantSpecularEffect { Source = heightMap, SpecularExponent = 16 }; var pointDiffuseEffect = new PointDiffuseEffect { Source = heightMap, HeightMapScale = 2 }; var pointSpecularEffect = new PointSpecularEffect { Source = heightMap, SpecularExponent = 16 }; var spotDiffuseEffect = new SpotDiffuseEffect { Source = heightMap, HeightMapScale = 2, LimitingConeAngle = 0.25f, LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2 }; var spotSpecularEffect = new SpotSpecularEffect { Source = heightMap, SpecularExponent = 16, LimitingConeAngle = 0.25f, LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2 }; // Lay out all the different light types in a grid. var xgap = (float)bitmapTiger.Size.Width * 1.1f; var ygap = (float)bitmapTiger.Size.Height * 1.1f; var compositeEffect = new CompositeEffect { Sources = { AddTextOverlay(distantDiffuseEffect, -xgap, -ygap), AddTextOverlay(distantSpecularEffect, -xgap, 0), AddTextOverlay(pointDiffuseEffect, -0, -ygap), AddTextOverlay(pointSpecularEffect, -0, 0), AddTextOverlay(spotDiffuseEffect, xgap, -ygap), AddTextOverlay(spotSpecularEffect, xgap, 0), CombineDiffuseAndSpecular(distantDiffuseEffect, distantSpecularEffect, -xgap, ygap), CombineDiffuseAndSpecular(pointDiffuseEffect, pointSpecularEffect, 0, ygap), CombineDiffuseAndSpecular(spotDiffuseEffect, spotSpecularEffect, xgap, ygap), } }; ICanvasImage finalEffect = compositeEffect; // Check the screen size, and scale down our output if the screen is too small to fit the whole thing as-is. var xScaleFactor = (float)canvas.ActualWidth / 750; var yScaleFactor = (float)canvas.ActualHeight / 500; var scaleFactor = Math.Min(xScaleFactor, yScaleFactor); if (scaleFactor < 1) { finalEffect = new Transform2DEffect { Source = compositeEffect, TransformMatrix = Matrix3x2.CreateScale(scaleFactor, bitmapTiger.Size.ToVector2() / 2) }; } // Animation changes the light directions. animationFunction = elapsedTime => { distantDiffuseEffect.Azimuth = distantSpecularEffect.Azimuth = elapsedTime % ((float)Math.PI * 2); distantDiffuseEffect.Elevation = distantSpecularEffect.Elevation = (float)Math.PI / 4 + (float)Math.Sin(elapsedTime / 2) * (float)Math.PI / 8; pointDiffuseEffect.LightPosition = pointSpecularEffect.LightPosition = spotDiffuseEffect.LightPosition = spotSpecularEffect.LightPosition = new Vector3((float)Math.Cos(elapsedTime), (float)Math.Sin(elapsedTime), 1) * 100; }; return finalEffect; }
private ICanvasImage CreateLuminanceToAlpha() { var contrastAdjustedTiger = new LinearTransferEffect { Source = bitmapTiger, RedOffset = -3, GreenOffset = -3, BlueOffset = -3, }; var tigerAlpha = new LuminanceToAlphaEffect { Source = contrastAdjustedTiger }; var tigerAlphaWithWhiteRgb = new LinearTransferEffect { Source = tigerAlpha, RedOffset = 1, GreenOffset = 1, BlueOffset = 1, RedSlope = 0, GreenSlope = 0, BlueSlope = 0, }; var recombinedRgbAndAlpha = new ArithmeticCompositeEffect { Source1 = tigerAlphaWithWhiteRgb, Source2 = bitmapTiger, }; var movedTiger = new Transform2DEffect { Source = recombinedRgbAndAlpha }; const float turbulenceSize = 128; var backgroundImage = new CropEffect { Source = new TileEffect { Source = new TurbulenceEffect { Octaves = 8, Size = new Vector2(turbulenceSize), Tileable = true }, SourceRectangle = new Rect(0, 0, turbulenceSize, turbulenceSize) }, SourceRectangle = new Rect((bitmapTiger.Size.ToVector2() * -0.5f).ToPoint(), (bitmapTiger.Size.ToVector2() * 1.5f).ToPoint()) }; var tigerOnBackground = new BlendEffect { Foreground = movedTiger, Background = backgroundImage }; // Animation moves the alpha bitmap around, and alters color transfer settings to change how solid it is. animationFunction = elapsedTime => { contrastAdjustedTiger.RedSlope = contrastAdjustedTiger.GreenSlope = contrastAdjustedTiger.BlueSlope = ((float)Math.Sin(elapsedTime * 0.9) + 2) * 3; var dx = (float)Math.Cos(elapsedTime) * 50; var dy = (float)Math.Sin(elapsedTime) * 50; movedTiger.TransformMatrix = Matrix3x2.CreateTranslation(dx, dy); }; return(tigerOnBackground); }
private ICanvasImage CreateLighting() { var heightMap = new LuminanceToAlphaEffect { Source = bitmapTiger }; var distantDiffuseEffect = new DistantDiffuseEffect { Source = heightMap, HeightMapScale = 2 }; var distantSpecularEffect = new DistantSpecularEffect { Source = heightMap, SpecularExponent = 16 }; var pointDiffuseEffect = new PointDiffuseEffect { Source = heightMap, HeightMapScale = 2 }; var pointSpecularEffect = new PointSpecularEffect { Source = heightMap, SpecularExponent = 16 }; var spotDiffuseEffect = new SpotDiffuseEffect { Source = heightMap, HeightMapScale = 2, LimitingConeAngle = 0.25f, LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2 }; var spotSpecularEffect = new SpotSpecularEffect { Source = heightMap, SpecularExponent = 16, LimitingConeAngle = 0.25f, LightTarget = new Vector3(bitmapTiger.Size.ToVector2(), 0) / 2 }; // Lay out all the different light types in a grid. var xgap = (float)bitmapTiger.Size.Width * 1.1f; var ygap = (float)bitmapTiger.Size.Height * 1.1f; var compositeEffect = new CompositeEffect { Sources = { AddTextOverlay(distantDiffuseEffect, -xgap, -ygap), AddTextOverlay(distantSpecularEffect, -xgap, 0), AddTextOverlay(pointDiffuseEffect, -0, -ygap), AddTextOverlay(pointSpecularEffect, -0, 0), AddTextOverlay(spotDiffuseEffect, xgap, -ygap), AddTextOverlay(spotSpecularEffect, xgap, 0), CombineDiffuseAndSpecular(distantDiffuseEffect, distantSpecularEffect, -xgap,ygap), CombineDiffuseAndSpecular(pointDiffuseEffect, pointSpecularEffect, 0,ygap), CombineDiffuseAndSpecular(spotDiffuseEffect, spotSpecularEffect, xgap, ygap), } }; ICanvasImage finalEffect = compositeEffect; // Check the screen size, and scale down our output if the screen is too small to fit the whole thing as-is. var xScaleFactor = (float)canvas.ActualWidth / 750; var yScaleFactor = (float)canvas.ActualHeight / 500; var scaleFactor = Math.Min(xScaleFactor, yScaleFactor); if (scaleFactor < 1) { finalEffect = new Transform2DEffect { Source = compositeEffect, TransformMatrix = Matrix3x2.CreateScale(scaleFactor, bitmapTiger.Size.ToVector2() / 2) }; } // Animation changes the light directions. animationFunction = elapsedTime => { distantDiffuseEffect.Azimuth = distantSpecularEffect.Azimuth = elapsedTime % ((float)Math.PI * 2); distantDiffuseEffect.Elevation = distantSpecularEffect.Elevation = (float)Math.PI / 4 + (float)Math.Sin(elapsedTime / 2) * (float)Math.PI / 8; pointDiffuseEffect.LightPosition = pointSpecularEffect.LightPosition = spotDiffuseEffect.LightPosition = spotSpecularEffect.LightPosition = new Vector3((float)Math.Cos(elapsedTime), (float)Math.Sin(elapsedTime), 1) * 100; }; return(finalEffect); }
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)); }
public CanvasRenderTarget apply3DLightingEffects(CanvasBitmap workingBitmap) { //CanvasBitmap workingBitmap = SelectWorkingBitmap(useOriginalBitmap); if (workingBitmap != null) { int ww = (int)workingBitmap.SizeInPixels.Width; int hh = (int)workingBitmap.SizeInPixels.Height; //LuminanceToAlphaEffect heightField = new LuminanceToAlphaEffect(); //heightField.Source = workingBitmap; LuminanceToAlphaEffect heightMap = new LuminanceToAlphaEffect(); heightMap.Source = workingBitmap; GaussianBlurEffect heightField = new GaussianBlurEffect(); heightField.BlurAmount = (float)gaussianBlurAmount; heightField.Source = heightMap; heightField.BorderMode = EffectBorderMode.Soft; DistantDiffuseEffect distantDiffuseEffect = new DistantDiffuseEffect(); distantDiffuseEffect.Source = heightField; distantDiffuseEffect.HeightMapScale = (float)distantDiffuseEffectHeightMapScale; distantDiffuseEffect.HeightMapInterpolationMode = CanvasImageInterpolation.HighQualityCubic; distantDiffuseEffect.Azimuth = (float)distantAzimuth; distantDiffuseEffect.Elevation = (float)distantElevation; distantDiffuseEffect.LightColor = distantLightColor; distantDiffuseEffect.HeightMapKernelSize = new System.Numerics.Vector2((float)distantDiffuseKernelWidth, (float)distantDiffuseKernelWidth); //distantDiffuseEffect.DiffuseAmount = (float) distantDiffuseAmount; DistantSpecularEffect distantSpecularEffect = new DistantSpecularEffect(); distantSpecularEffect.Source = heightField; distantSpecularEffect.SpecularExponent = (float)distantSpecularEffectSpecularExponent; distantSpecularEffect.HeightMapInterpolationMode = CanvasImageInterpolation.HighQualityCubic; distantSpecularEffect.HeightMapKernelSize = new System.Numerics.Vector2((float)distantSpecularKernelWidth, (float)distantSpecularKernelWidth); distantSpecularEffect.HeightMapScale = (float)distantSpecularHeightMapScale; distantSpecularEffect.Azimuth = (float)distantAzimuth; distantSpecularEffect.Elevation = (float)distantElevation; //distantSpecularEffect.SpecularAmount = (float)distantSpecularAmount; //distantSpecularEffect.LightColor = distantLightColor; ArithmeticCompositeEffect arithmeticCompositeEffect = new ArithmeticCompositeEffect(); arithmeticCompositeEffect.Source1 = distantDiffuseEffect; //arithmeticCompositeEffect.Source1 = blendedDiffuseEffect; arithmeticCompositeEffect.Source2 = distantSpecularEffect; arithmeticCompositeEffect.Source1Amount = 1; arithmeticCompositeEffect.Source2Amount = 1; arithmeticCompositeEffect.MultiplyAmount = 0; SaturationEffect saturationEffect = new SaturationEffect(); saturationEffect.Source = workingBitmap; saturationEffect.Saturation = (float)distantDiffuseSaturation; ArithmeticCompositeEffect blendedDiffuseEffect = new ArithmeticCompositeEffect(); //blendedDiffuseEffect.Source1 = workingBitmap; blendedDiffuseEffect.Source1 = saturationEffect; blendedDiffuseEffect.Source2 = arithmeticCompositeEffect; blendedDiffuseEffect.Source1Amount = 0; blendedDiffuseEffect.Source2Amount = 0; blendedDiffuseEffect.MultiplyAmount = 1; //if (canvasRenderTarget != null) // canvasRenderTarget.Dispose(); CanvasRenderTarget canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), ww, hh, canvasBitmap.Dpi); using (var session = canvasRenderTarget.CreateDrawingSession()) { //session.DrawImage(arithmeticCompositeEffect); session.DrawImage(blendedDiffuseEffect); } return(canvasRenderTarget); } return(null); }
public void Loadd() { MAIN = new Transform2DEffect { Source = Shape, }; var c = color; c.A = (byte)(255 * (density)); ICanvasImage Main() { if (Texture == null) { return(MAIN); } return(new ArithmeticCompositeEffect { Source1 = MAIN, Source2 = new TileEffect { Source = Texture, SourceRectangle = new Rect(0, 0, Texture.Size.Width, Texture.Size.Height) }, MultiplyAmount = 1, Source1Amount = 0, Source2Amount = 0, }); } ICanvasImage Color() { var cc = new PremultiplyEffect { Source = new ColorSourceEffect { Color = c } }; if (blend == 0 && dilution == 0) { ECAN = null; return(cc); } ECAN = new TileEffect { Source = new GaussianBlurEffect { Source = Canvas, } }; return(new ArithmeticCompositeEffect { Source1 = new CompositeEffect { Sources = { new PremultiplyEffect { Source = cc }, new LinearTransferEffect { Source = ECAN, AlphaSlope = blend } } }, MultiplyAmount = 0, Source1Amount = 1 - dilution, Source2 = ECAN, Source2Amount = dilution, }); } MASK = new LuminanceToAlphaEffect { Source = Main(), }; ERAS = new ArithmeticCompositeEffect { Source1 = Color(), Source2 = new InvertEffect { Source = new LuminanceToAlphaEffect { Source = Main(), } }, MultiplyAmount = 1, Source1Amount = 0, Source2Amount = 0, }; }