public Particle(int x, int y, int idxBitmap, float orientationx, float orientationy, CanvasRenderTarget source, float particlespeed = 0.25f, bool particlefall = false) { particleBitmap = idxBitmap; Radius = (particleBitmap + 1) * 4; X = x; Y = y; OrientationX = orientationx; OrientationY = orientationy; //Here is all the turbulence, without this is linear random = generator.Next(2, 50); factor = 0.7f * random / 10.0f; var octaves = generator.Next(1, 5); turbulence = new Transform2DEffect() { Source = new TurbulenceEffect() { Octaves = octaves } }; displacement = new DisplacementMapEffect() { Source = source, Displacement = turbulence }; speed = particlespeed; fall = particlefall; }
private void Draw(CanvasControl sender, CanvasDrawEventArgs args) { if (_contentPresenter == null) return; var border = VisualTreeHelper.GetChild(_contentPresenter, 0) as Border; if (border == null) return; var borderPoint = border.TransformToVisual(this).TransformPoint(new Point(0, 0)); var cl = new CanvasCommandList(sender); using (var clds = cl.CreateDrawingSession()) { clds.FillRoundedRectangle(new Rect(borderPoint.X, borderPoint.Y, border.ActualWidth, border.ActualHeight), (float)border.CornerRadius.TopLeft, (float)border.CornerRadius.TopLeft, Color.FromArgb(128, 0, 0, 0)); } var shadowEffect = new Transform2DEffect { Source = new Transform2DEffect { Source = new ShadowEffect { BlurAmount = 2, ShadowColor = Color.FromArgb(160, 0, 0, 0), Source = cl }, //TODO not doing any scaling right now, confirm with larger shadows TransformMatrix = Matrix3x2.CreateScale(1.0f, new Vector2((float)(border.ActualWidth / 2), ((float)border.ActualHeight / 2))) }, TransformMatrix = Matrix3x2.CreateTranslation(0, 1) }; args.DrawingSession.DrawImage(shadowEffect); // args.DrawingSession.DrawImage(cl); }
public Renderer(CoreWindow window) { this.window = window; window.PointerPressed += window_PointerPressed; window.PointerMoved += window_PointerMoved; window.PointerReleased += window_PointerReleased; device = new CanvasDevice(); swapChainManager = new SwapChainManager(window, device); var effect = new GaussianBlurEffect() { BlurAmount = 5, Source = inputEffect }; outputEffect = new Transform2DEffect() { Source = effect }; imageBrush = new CanvasImageBrush(device); imageBrush.Opacity = 0.99f; }
public async void DrawEffect() { if (Target == null) { return; } if (MainContainer != null) { foreach (var child in MainContainer.Children) { if (child is SpriteVisual) { (child as SpriteVisual).Brush.Dispose(); } } RemoveEffect(); } RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Target); var pixels = (await bitmap.GetPixelsAsync()).ToArray(); Size srcSize = new Size(bitmap.PixelWidth, bitmap.PixelHeight); Size decSize = new Size((int)BlurAmount * 10 + bitmap.PixelWidth, (int)BlurAmount * 10 + bitmap.PixelHeight); Size blurSize = new Size(decSize.Width - srcSize.Width, decSize.Height - srcSize.Height); Point transform = new Point(blurSize.Width/2, blurSize.Height/2); transform.X += Math.Cos(Direction / 360 * 2 * Math.PI) * Depth; transform.Y += Math.Sin(Direction / 360 * 2 * Math.PI) * Depth; ContainerVisual visual = Target.GetVisual() as ContainerVisual; Compositor compositor = visual.Compositor; CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget offscreen = new CanvasRenderTarget(device, (int)decSize.Width, (int)decSize.Height, 96); using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { Transform2DEffect finalEffect = new Transform2DEffect() { Source = new Microsoft.Graphics.Canvas.Effects.ShadowEffect() { Source = CanvasBitmap.CreateFromBytes(ds, pixels, (int)srcSize.Width, (int)srcSize.Height, DirectXPixelFormat.B8G8R8A8UIntNormalized), BlurAmount = (float)BlurAmount, ShadowColor = ShadowColor }, TransformMatrix = Matrix3x2.CreateTranslation((float)blurSize.Width / 2, (float)blurSize.Height / 2) }; ds.DrawImage(finalEffect); } var effectPixels = offscreen.GetPixelBytes(); var imageFactory = Microsoft.UI.Composition.Toolkit.CompositionImageFactory.CreateCompositionImageFactory(compositor); var effectImage = imageFactory.CreateImageFromPixels(effectPixels, (int)decSize.Width, (int)decSize.Height); var effectbrush = compositor.CreateSurfaceBrush(effectImage.Surface); var effectVisual = compositor.CreateSpriteVisual(); effectVisual.Brush = effectbrush; effectVisual.Offset = new Vector3( -(float)transform.X, -(float)transform.Y, 0); effectVisual.Size = new Vector2((float)decSize.Width, (float)decSize.Height); var srcImage = imageFactory.CreateImageFromPixels(pixels, (int)srcSize.Width, (int)srcSize.Height); var srcbrush = compositor.CreateSurfaceBrush(srcImage.Surface); var srcVisual = compositor.CreateSpriteVisual(); srcVisual.Brush = srcbrush; srcVisual.Offset = new Vector3(0, 0, 0); srcVisual.Size = new Vector2((int)srcSize.Width, (int)srcSize.Height); if (MainContainer == null) { MainContainer = compositor.CreateContainerVisual(); ElementCompositionPreview.SetElementChildVisual(Target, MainContainer); } MainContainer.Children.InsertAtTop(srcVisual); MainContainer.Children.InsertAtBottom(effectVisual); }
void CreateLinearGradient(ICanvasResourceCreator resourceCreator) { var commandList = new CanvasCommandList(resourceCreator); using (var drawingSession = commandList.CreateDrawingSession()) { var brush = new CanvasLinearGradientBrush(resourceCreator, Colors.White, Colors.Black) { StartPoint = new Vector2(-tigerSize.X / 4, 0), EndPoint = new Vector2(tigerSize.X * 5 / 4, 0), }; drawingSession.FillRectangle(bitmapTiger.Bounds, brush); } // Wrap the gradient with a border effect to avoid edge artifacts as we rotate it. linearGradient = new Transform2DEffect { Source = new BorderEffect { Source = commandList } }; }
void CreateBrushes(CanvasAnimatedControl sender, CanvasBitmap bitmapTiger) { var bitmapSize = bitmapTiger.Size; var scale = (radius * 2) / (float)bitmapSize.Height; var backgroundEffect = new Transform2DEffect() { Source = bitmapTiger, TransformMatrix = Matrix3x2.CreateScale(scale, scale) * Matrix3x2.CreateTranslation(center - radius, center - radius) }; backgroundBrush = new CanvasImageBrush(sender, backgroundEffect) { SourceRectangle = new Rect(0, 0, size, size), Opacity = 0.6f }; hueRotationEffect = new HueRotationEffect() { Source = backgroundEffect, Angle = (float)Math.PI * 0.5f }; var foregroundEffect = new GaussianBlurEffect() { Source = hueRotationEffect, BlurAmount = 10 }; foregroundBrush = new CanvasImageBrush(sender, foregroundEffect) { SourceRectangle = new Rect(0, 0, size, size) }; }
private ICanvasImage CreateDisplacementMap() { var turbulence = new Transform2DEffect { Source = new TurbulenceEffect { Octaves = 4 } }; var displacementEffect = new DisplacementMapEffect { Source = bitmapTiger, Displacement = turbulence, XChannelSelect = EffectChannelSelect.Red, YChannelSelect = EffectChannelSelect.Green, }; // Animation moves the displacement map from side to side, which creates a rippling effect. // It also gradually changes the displacement amount. animationFunction = elapsedTime => { turbulence.TransformMatrix = Matrix3x2.CreateTranslation((float)Math.Cos(elapsedTime) * 50 - 50, (float)Math.Sin(elapsedTime) * 50 - 50); displacementEffect.Amount = (float)Math.Sin(elapsedTime * 0.7) * 50 + 75; currentEffectSize = bitmapTiger.Size.ToVector2() + new Vector2(displacementEffect.Amount / 2); }; return displacementEffect; }
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, }; }
/// <summary> /// Generate the flame effect graph. This method is called before the text command list /// (input) is created. /// </summary> private void CreateFlameEffect() { // Thicken the text. morphology = new MorphologyEffect { // The Source property is set by SetupText(). Mode = MorphologyEffectMode.Dilate, Width = 7, Height = 1 }; // Blur, then colorize the text from black to red to orange as the alpha increases. var colorize = new ColorMatrixEffect { Source = new GaussianBlurEffect { Source = morphology, BlurAmount = 3f }, ColorMatrix = new Matrix5x4 { M11 = 0f, M12 = 0f, M13 = 0f, M14 = 0f, M21 = 0f, M22 = 0f, M23 = 0f, M24 = 0f, M31 = 0f, M32 = 0f, M33 = 0f, M34 = 0f, M41 = 0f, M42 = 1f, M43 = 0f, M44 = 1f, M51 = 1f, M52 = -0.5f, M53 = 0f, M54 = 0f } }; // Generate a Perlin noise field (see flamePosition). // Animate the noise by modifying flameAnimation's transform matrix at render time. flameAnimation = new Transform2DEffect { Source = new BorderEffect { Source = new TurbulenceEffect { Frequency = new Vector2(0.109f, 0.109f), Size = new Vector2(500.0f, 80.0f) }, // Use Mirror extend mode to allow us to spatially translate the noise // without any visible seams. ExtendX = CanvasEdgeBehavior.Mirror, ExtendY = CanvasEdgeBehavior.Mirror } }; // Give the flame its wavy appearance by generating a displacement map from the noise // (see flameAnimation) and applying this to the text. // Stretch and position this flame behind the original text. flamePosition = new Transform2DEffect { Source = new DisplacementMapEffect { Source = colorize, Displacement = flameAnimation, Amount = 40.0f } // Set the transform matrix at render time as it depends on window size. }; // Composite the text over the flames. composite = new CompositeEffect() { Sources = { flamePosition, null } // Replace null with the text command list when it is created. }; }
public void SetEncodingProperties(VideoEncodingProperties encodingProperties, IDirect3DDevice device) { canvasDevice = CanvasDevice.CreateFromDirect3D11Device(device); numColumns = (uint)(encodingProperties.Width / pixelsPerTile); numRows = (uint)(encodingProperties.Height / pixelsPerTile); transforms = new Transform2DEffect[numColumns, numRows]; crops = new CropEffect[numColumns, numRows]; for (uint i = 0; i < numColumns; i++) { for (uint j = 0; j < numRows; j++) { crops[i, j] = new CropEffect(); crops[i, j].SourceRectangle = new Rect(i * pixelsPerTile, j * pixelsPerTile, pixelsPerTile, pixelsPerTile); transforms[i, j] = new Transform2DEffect(); transforms[i, j].Source = crops[i, j]; } } }
public void ProcessFrame(ProcessVideoFrameContext context) { using (CanvasBitmap input = CanvasBitmap.CreateFromDirect3D11Surface(canvasDevice, context.InputFrame.Direct3DSurface)) using (CanvasRenderTarget output = CanvasRenderTarget.CreateFromDirect3D11Surface(canvasDevice, context.OutputFrame.Direct3DSurface)) using (CanvasDrawingSession ds = output.CreateDrawingSession()) { ds.Clear(Colors.Black); var mirrorEffect = new Transform2DEffect() { Source = input, TransformMatrix = new Matrix3x2(-1, 0, 0, 1, output.SizeInPixels.Width, 0), }; ds.DrawImage(mirrorEffect); } }
void CreateEffects() { // The Game of Life is a cellular automaton with very simple rules. // Each cell (pixel) can be either alive (white) or dead (black). // The state is updated by: // // - for each cell, count how many of its 8 neighbors are alive // - if less than two, the cell dies from loneliness // - if exactly two, the cell keeps its current state // - if exactly three, the cell become alive // - if more than three, the cell dies from overcrowding // Step 1: use a convolve matrix to count how many neighbors are alive. This filter // also includes the state of the current cell, but with a lower weighting. The result // is an arithmetic encoding where (value / 2) indicates how many neighbors are alive, // and (value % 2) is the state of the cell itself. This is divided by 18 to make it // fit within 0-1 color range. countNeighborsEffect = new ConvolveMatrixEffect { KernelMatrix = new float[] { 2, 2, 2, 2, 1, 2, 2, 2, 2 }, Divisor = 18, BorderMode = EffectBorderMode.Hard, }; // Step 2: use a color transfer table to map the different states produced by the // convolve matrix to whether the cell should live or die. Each pair of entries in // this table corresponds to a certain number of live neighbors. The first of the // pair is the result if the current cell is dead, or the second if it is alive. float[] transferTable = { 0, 0, // 0 live neighbors -> dead cell 0, 0, // 1 live neighbors -> dead cell 0, 1, // 2 live neighbors -> cell keeps its current state 1, 1, // 3 live neighbors -> live cell 0, 0, // 4 live neighbors -> dead cell 0, 0, // 5 live neighbors -> dead cell 0, 0, // 6 live neighbors -> dead cell 0, 0, // 7 live neighbors -> dead cell 0, 0, // 8 live neighbors -> dead cell }; liveOrDieEffect = new DiscreteTransferEffect { Source = countNeighborsEffect, RedTable = transferTable, GreenTable = transferTable, BlueTable = transferTable, }; // Step 3: the algorithm is implemented in terms of white = live, // black = dead, but we invert these colors before displaying the // result, just 'cause I think it looks better that way. invertEffect = new LinearTransferEffect { RedSlope = -1, RedOffset = 1, GreenSlope = -1, GreenOffset = 1, BlueSlope = -1, BlueOffset = 1, }; // Step 4: insert our own DPI compensation effect to stop the system trying to // automatically convert DPI for us. The Game of Life simulation always works // in pixels (96 DPI) regardless of display DPI. Normally, the system would // handle this mismatch automatically and scale the image up as needed to fit // higher DPI displays. We don't want that behavior here, because it would use // a linear filter while we want nearest neighbor. So we insert a no-op DPI // converter of our own. This overrides the default adjustment by telling the // system the source image is already the same DPI as the destination canvas // (even though it really isn't). We'll handle any necessary scaling later // ourselves, using Transform2DEffect to control the interpolation mode. var dpiCompensationEffect = new DpiCompensationEffect { Source = invertEffect, SourceDpi = new Vector2(canvas.Dpi), }; // Step 5: a transform matrix scales up the simulation rendertarget and moves // it to the right part of the screen. This uses nearest neighbor filtering // to avoid unwanted blurring of the cell shapes. transformEffect = new Transform2DEffect { Source = dpiCompensationEffect, InterpolationMode = CanvasImageInterpolation.NearestNeighbor, }; }
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 void Loadd2() { var MAIN = new Transform2DEffect { Source = Shape, }; //直接读CanvasRenderTarget又不行(0x88990025) sessionblend.Mutiply也没有 //blendcopy要读底图颜色不过UpdateCanvas太慢了 已经写不出橡皮擦了 ICanvasImage ccc() { 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, }); } var ECAN = new TileEffect { Source = new GaussianBlurEffect { Source = Canvas, } }; var ERAS = new CompositeEffect { Sources = { new ArithmeticCompositeEffect { Source1 = new ArithmeticCompositeEffect{ MultiplyAmount = 0, Source1 = new CompositeEffect { Sources = { new PremultiplyEffect { Source = new ColorSourceEffect{ Color = color } }, new LinearTransferEffect { Source = ECAN, AlphaSlope = blend } } }, Source1Amount = 1 - dilution, Source2 = ECAN, Source2Amount = dilution, }, Source2 = new InvertEffect { Source = new LuminanceToAlphaEffect{ Source = ccc() } }, MultiplyAmount = 1, Source1Amount = 0, Source2Amount = 0, }, new CompositeEffect { Sources = { Canvas, new LuminanceToAlphaEffect { Source = ccc(), }, }, Mode = CanvasComposite.DestinationOut } }, Mode = CanvasComposite.Add }; var brush = new CanvasImageBrush(device) { Image = ERAS, SourceRectangle = new Rect(new Point(0, 0), Canvas.Size) }; dynamicbrush = (p, v, size) => { checkRandom(out Matrix3x2 x, ref p, ref v, ref size); ECAN.SourceRectangle = new Rect(p.X + v.X * size, p.Y + v.Y * size, 1, 1); MAIN.TransformMatrix = x; UpdateCanvas(); // session.Blend = CanvasBlend.Copy; // session.FillCircle(p, size*2, brush); session.DrawImage(ERAS, CanvasComposite.Copy); }; }
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(); }
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 CreateCrossFade() { if (!CrossFadeEffect.IsSupported) { return CreateNotSupportedMessage(requiresWin10_14393); } textLabel = requiresWin10_14393; var upsideDownTiger = new Transform2DEffect { Source = bitmapTiger, TransformMatrix = Matrix3x2.CreateScale(1, -1, bitmapTiger.Size.ToVector2() / 2) }; var crossFadeEffect = new CrossFadeEffect { Source1 = bitmapTiger, Source2 = upsideDownTiger }; // Animation changes the crossfade amount. animationFunction = elapsedTime => { crossFadeEffect.CrossFade = 0.5f + (float)Math.Sin(elapsedTime * 2) / 2; }; return crossFadeEffect; }
async Task CreateResourcesAsync(CanvasAnimatedControl sender) { Arms_AR = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/arms/AR.png")); Arms_Pistol = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/arms/Pistol.png")); Arms_SMG = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/arms/SMG.png")); Arms_Fists = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/arms/Fists.png")); reloadArms(); Bullet = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Bullets/bullet.png")); UI_AR = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/gun-1.png")); UI_SMG = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/gun-2.png")); UI_Pistol = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/gun-3.png")); UI_Fists = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/fists.png")); Bullets = ImageManipulation.img(Bullet); Enemy1 = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_hayri.png")); ARWeapon.SetImage(await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Weapons/gun-1.png"))); SMGWeapon.SetImage(await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Weapons/gun-2.png"))); PistolWeapon.SetImage(await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Weapons/gun-3.png"))); if (ChooseCharacter.PlayerCharacter.Equals("Jeroen")) // If Jeroen is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_jeroen.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Jeroen.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Jeffrey")) // If Jeffrey is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_jeffrey.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Jeffrey.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Daan")) // If Daan is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_daan.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Daan.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Jordy")) // // If Jordy is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_jordy.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Jordy.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Matthew")) // If Matthew is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_matthew.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Matthew.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Hayri")) // If Hayri is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_hayri.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Hayri.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Max")) // If Max is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_max.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/Max.png")); // Set the sprite, and set in in the UI } else if (ChooseCharacter.PlayerCharacter.Equals("Samus")) // If Samus is chooses as as the player { Player_sprite = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/spr_samus.png")); Char_UI = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Char/Char_UI/samus.png")); // Set the sprite, and set in in the UI } Health_Full = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/Health/health-full.png")); Health_Half = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/Health/health-half.png")); Health_Empty = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/UI/Health/health-empty.png")); // Images for the Health // So that this isn't done on each frame, but only once. foreach (Tile t in Levels.Levels.tiles.Values) // Loop through all tiles { await t.InitBitmap(sender).AsAsyncAction(); // Load the tiles } }
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; }
void CreateLinearGradient(ICanvasResourceCreator resourceCreator) { var commandList = new CanvasCommandList(resourceCreator); using (var drawingSession = commandList.CreateDrawingSession()) { var brush = new CanvasLinearGradientBrush(resourceCreator, Colors.White, Colors.Black) { StartPoint = new Vector2(-tigerSize.X / 4, 0), EndPoint = new Vector2(tigerSize.X * 5 / 4, 0), }; // Expand the bounds to avoid going past the edge of the gradient as we rotate it. var expandedBounds = new Rect((-tigerSize).ToPoint(), (tigerSize * 2).ToPoint()); drawingSession.FillRectangle(expandedBounds, brush); } linearGradient = new Transform2DEffect { Source = commandList }; }
private ICanvasImage CreateShadow() { var renderTarget = new CanvasRenderTarget(canvas, 360, 150); using (var ds = renderTarget.CreateDrawingSession()) { ds.Clear(Color.FromArgb(0, 0, 0, 0)); ds.DrawText("This text is drawn onto a rendertarget", 10, 10, Colors.White); ds.DrawText("with a different color per line,", 10, 40, Colors.Red); ds.DrawText("after which a drop shadow is", 10, 70, Colors.Green); ds.DrawText("generated using image effects.", 10, 100, Colors.Blue); } var shadowEffect = new Transform2DEffect { Source = new ShadowEffect { Source = renderTarget, BlurAmount = 2 }, TransformMatrix = Matrix3x2.CreateTranslation(3, 3) }; var whiteBackground = new CropEffect { Source = new ColorSourceEffect { Color = Colors.White }, SourceRectangle = renderTarget.Bounds }; var compositeEffect = new CompositeEffect { Sources = { whiteBackground, shadowEffect, renderTarget } }; animationFunction = elapsedTime => { }; currentEffectSize = renderTarget.Size.ToVector2(); return compositeEffect; }
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 / 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(); }