Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alpha / Transparency & MTKView?

Tags:

alpha

metal

I have a Metal fragment shader that returns some transparent colors with an alpha channel and I'd like to reveal a UIView under the MTKView, but they only background result I get is black and "error noise".

MTLRenderPipelineDescriptor:

pipelineStateDescriptor.isAlphaToCoverageEnabled = true
pipelineStateDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
pipelineStateDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

MTLRenderPassDescriptor:

renderPassDescriptor.colorAttachments[0].loadAction = .clear
renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0, green: 0, blue: 0, alpha: 0)

If I change the clear color I can see it under the transparent colors, tho if I skip the clear color I see "error noise". Does the clear color alpha channel actually do anything?

Does anyone know how to make a MTKView transparent?

Update:

Here's the magical property to make a MTKView transparent:

self.isOpaque = false
like image 525
Heestand XYZ Avatar asked Dec 05 '17 00:12

Heestand XYZ


2 Answers

If a UIView may have transparent content or otherwise fail to fill itself with opaque drawing, then it should set its opaque (isOpaque) property to false so that it will be properly composited with whatever is behind it. Since, MTKView is a subclass of UIView, this applies to it as well.

like image 162
Ken Thomases Avatar answered Oct 05 '22 03:10

Ken Thomases


First you should change the opaque of the window. Then you should locate the MTKView instance, and modify its layer's opaque to NO. That should work.


- (void)viewWillAppear
{
    [_view.window setOpaque:NO];
    _view.window.backgroundColor = [NSColor clearColor];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    MTKView *view = (MTKView *)self.view;
    view.layer.opaque = NO;
}
like image 23
Henry Xiao Avatar answered Oct 05 '22 01:10

Henry Xiao