Alpha / Transparency & MTKView?
Asked Answered
C

2

12

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
Crowson answered 5/12, 2017 at 0:0 Comment(2)
Are you using a subclass of MTKView which overrides the opaque property to return false?Angeloangelology
The isOpaque property did it! ThanksCrowson
A
13

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.

Angeloangelology answered 5/12, 2017 at 13:50 Comment(0)
S
2

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;
}
Stringpiece answered 16/11, 2021 at 4:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.