只是使Metal Frag着色器成为透明颜色?

Fat*_*tie 2 ios metal

我有一个简单的管道描述符

 let p = MTLRenderPipelineDescriptor()
 ...
 p.colorAttachments[0].pixelFormat = .bgra8Unorm
Run Code Online (Sandbox Code Playgroud)

背景为纯蓝色

 let bg = MTLClearColor(red: 0, green: 0, blue: 1, alpha: 1)

 let pass = MTLRenderPassDescriptor()
 pass.colorAttachments[0].loadAction = .clear
 pass.colorAttachments[0].clearColor = bg
Run Code Online (Sandbox Code Playgroud)

我只是在上面画一个多边形,说一个红色的:

fragment half4 fattie_fragment() {
    return half4(1,0,0, 1);
}
Run Code Online (Sandbox Code Playgroud)

还是绿色...

fragment half4 fattie_fragment() {
    return half4(0,1,0, 1);
}
Run Code Online (Sandbox Code Playgroud)

What to do in Metal if you want the triangle color to be transparent?

fragment half4 fattie_fragment() {
    return half4(0,1,0, 0.275);
}
Run Code Online (Sandbox Code Playgroud)

(Note - TBC I don't want the overall view to be transparent. Simply the shader, the drawn triangle.)

war*_*enm 8

配置渲染管线描述符时,启用混合并选择一组合适的混合因子和操作。例如:

let p = MTLRenderPipelineDescriptor()
...
p.colorAttachments[0].isBlendingEnabled = true
p.colorAttachments[0].rgbBlendOperation = .add
p.colorAttachments[0].alphaBlendOperation = .add
p.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
p.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
p.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
p.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
Run Code Online (Sandbox Code Playgroud)