SuRGeoNix / Flyleaf

Media Player .NET Library for WinUI 3/ WPF/WinForms (based on FFmpeg/DirectX)
GNU Lesser General Public License v3.0
708 stars 97 forks source link

Opacity of player and Video with Alpha #332

Open Sash0check opened 1 year ago

Sash0check commented 1 year ago

Hello! I can't cahnge opacity of player element in WPF. It works on child elements but video not. And second question: my transparent video in dxv format showing with black background can i fix this? Thanks

SuRGeoNix commented 1 year ago

Hi @Sash0check, currently flyleaf does not support transparency.

signumnova commented 11 months ago

I have started experimenting with this, since I want to make the entire video semi transparent with something else sitting behind it, and that isn't possible currently either. It involves tinkering with things deep down in the rendering level though, including (probably) setting up a BlendState on the DirectX device context.

SuRGeoNix commented 11 months ago

I think I've done it in the past, by settings the color.a to transparent in the pixel shader https://github.com/SuRGeoNix/Flyleaf/blob/master/FlyleafLib/MediaFramework/MediaRenderer/ShaderCompiler.cs#L367 and using the composition swap chain. I will need to review it and might added in the future.

signumnova commented 11 months ago

In terms of making the video play with an overall opacity of <100% this turned out to be quite simple in the end, and did not involve changing the pixel shader or renderer code. All it needed was two things inside FlyleafHost:

  1. Surface.AllowsTransparency=true regardless of whether rounded corners being used.
  2. A new call to SetLayeredWindowAttributes(SurfaceHandle, 0, opacity, 2) immediately after adding the "Layered" style to the surface. (Opacity = 0..255)

The app level window that holds the FlyleafHost also needs to have Transparency enabled and Opacity=0 to get a fully translucent effect to desktop level.

Turning this from a PoC to a feature should make it possible to achieve fade-up/fade-down and cross-fade effects between overlapping players.

I don't know yet if the same thing will work with the WinFormsHost, that may still be subject to the classic WPF/WinForms airspace issue.

One thing it doesn't quite do is allow for the non-active portion of the player window to be totally transparent, as in when the video aspect doesn't fit the window shape. (Even though you can specify an alpha in the Player.Config.Video.BackgroundColour the renderer does not currently honour this, it just comes out solid but is subject to the alpha of the Surface behind it in the same way that the video itself is handled). As for videos that themselves contain an alpha channel, this doesn't currently work either for the same reason - may just need a bit more work on the pixel shader to handle the alpha plane.

LazyCuteLion commented 1 month ago

@SuRGeoNix this commit[1b3f7a9] work with [Config.Video.BackgroundColor],but hope to set Transparent and mix with the host window background.i try to do everything(baidu、bing 、stackoverflow……),but come to nothing. however,can add "SoftwareHost"? like [ffmediaelement.ImageHost],it use “WriteableBitmap” for rendering.It works well with videos that have transparent channel. as a developer,you know,the designer,they are so stupid and think we create animation so easy.but sadly,most scene can not do as like as two peas.we have to got some shortcut.use the mov is the way.now i use ffmediaelement,but i find your project ,liked immediately when tried it.

LazyCuteLion commented 1 month ago

oh,i got a way! #492