0

I have the following code written in C#/WinUI 3:

<Grid Name="_MainGrid">
    <Rectangle Name="_Test" Width="500" Height="500" Fill="Green"/>
</Grid>
Visual visualTest = ElementCompositionPreview.GetElementVisual(_Test);
Compositor compositorTest = visualTest.Compositor;
ContainerVisual containerVisualTest = (ContainerVisual)ElementCompositionPreview.GetElementVisual(_Test);

PointSpecularEffect light = new PointSpecularEffect()
{
    Name = "Light",
    Source = new CompositionEffectSourceParameter("source"),
    LightColor = Colors.Red,
    SpecularAmount = 2,
    SpecularExponent = 2
};

CompositionEffectFactory lightFactory = compositorTest.CreateEffectFactory(light);
CompositionEffectBrush lightBrush = lightFactory.CreateBrush();

SpriteVisual lightVisual = compositorTest.CreateSpriteVisual();
lightVisual.Size = new Vector2(500, 500);
lightVisual.Brush = lightBrush;

containerVisualTest.Children.InsertAtTop(lightVisual);

But this code crashes on the following line with error System.ArgumentException: "Parameter specified incorrectly.":

CompositionEffectFactory lightFactory = compositorTest.CreateEffectFactory(light);

Moreover, other effects work correctly, such as GaussianBlurEffect or SaturationEffect. The problem arises with lighting effects. I had a similar problem when I tried to apply a GaussianBlurEffect to the FillBrush property of the CompositionSpriteShape class, which ended up being used in the ShapeVisual class. After researching the topic, I found out that this effect is only applicable to SpriteVisual. So, maybe there are some limitations with the PointSpecularEffect class too? Although, on the other hand, I'm just trying to create a factory with this effect, what restrictions can there be? Why is a factory with a light effect not created? Can you please tell me how to make this effect work?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • I have the same problem with PointSpecularEffect, even with completely different code paths (not using Win2D), just Direct2D and Direct Composition, and it works with other effects. Looks like a bug of some sort, you should report to Microsoft. – Simon Mourier Aug 26 '23 at 16:43
  • @SimonMourier, I tried CanvasDrawingSession.DrawImage(lightEffect). After that I created a SurfaceBrush based on the drawn image and applied this brush to the SpriteVisual. The program does not crash, but it seems to me that the effect is displayed incorrectly, maybe I just applied the wrong settings. Still, I think this is more of a workaround than a normal solution. – Георгий Попов Aug 26 '23 at 19:37
  • I suggest you could post the issue to the [Github Issue](https://github.com/microsoft/Win2D/issues) for better help. – Jeaninez - MSFT Aug 28 '23 at 06:54
  • @Jeaninez-MSFT - this is not a Win2D problem, it's a Direct2D/DirectComposition problem. – Simon Mourier Aug 29 '23 at 22:49

0 Answers0