microsoft / WindowsCompositionSamples

The Windows Composition Samples have moved here: https://github.com/microsoft/WindowsAppSDK-Samples/tree/main/Samples/SceneGraph
https://github.com/microsoft/WindowsAppSDK-Samples/tree/main/Samples/SceneGraph
MIT License
1.12k stars 287 forks source link

Question about CompositionSpriteShape #275

Closed devsko closed 6 years ago

devsko commented 6 years ago

I am playing with the new Composition shape feature in SDK 17115. It works great. What i cannot figure out is how i can use a shape as mask for a DropShadow. Is that possible?

Thanks

sohchatt commented 6 years ago

The new CompositionShape APIs do not allow you to create a mask from a CompositionShape. Are you trying to get a ShapeVisual to cast a shadow? Or are you interesting solely in masking a shadow?

devsko commented 6 years ago

I have an existing control that draws some Win2D geometries to a CompositionDrawingSurface, creates a SurfaceBrush which is used (a) as source for a EffectBrush and (b) as mask for a DropShadow. I thought something like this would be possible with the new shapes. I tried a SpriteVisual containing a ShapeVisual with a DropShadow and CompositionDropShadowSourcePolicy.InheritFromVisualContent. But this produces no shadow at all.

sohchatt commented 6 years ago

Instead of a SpriteVisual, you'll need to use a LayerVisual. For instance:

            // Create LayerVisual for casting Shadow
            var layerVisual = compositor.CreateLayerVisual();
            layerVisual.Size = shapeVisual.Size;
            layerVisual.Children.InsertAtTop(shapeVisual); // Insert ShapeVisual containing tree of Shapes into Layer

            // Create a Shadow with the SourcePolicy set to InheritFromVisualContent
            var shadow = compositor.CreateDropShadow();
            shadow.SourcePolicy = CompositionDropShadowSourcePolicy.InheritFromVisualContent;

            layerVisual.Shadow = shadow;

Do note that a LayerVisual results in offscreen rendering and should be used judiciously.

devsko commented 6 years ago

Great, this works. Thank you. I found a weird behavior with LayerVisual, but it has nothing to do with shapes. When I have a minimal repro I will create another issue.