Ios MTKView透明度
我无法让我的MTKView清楚它的背景。我已经将视图及其层的isOpaque设置为false,将背景色设置为clear,并尝试了在google/stackoverflow上找到的多种解决方案(大多数在下面的代码中,如loadAction和clearColor of color attachment),但没有任何效果 所有背景色设置似乎都被忽略。设置MTLRenderPassColorAttachmentDescriptor的loadAction和clearColor不会起任何作用 我想在MTKView下绘制我的常规UIView。我错过了什么Ios MTKView透明度,ios,swift,opacity,metal,mtkview,Ios,Swift,Opacity,Metal,Mtkview,我无法让我的MTKView清楚它的背景。我已经将视图及其层的isOpaque设置为false,将背景色设置为clear,并尝试了在google/stackoverflow上找到的多种解决方案(大多数在下面的代码中,如loadAction和clearColor of color attachment),但没有任何效果 所有背景色设置似乎都被忽略。设置MTLRenderPassColorAttachmentDescriptor的loadAction和clearColor不会起任何作用 我想在MTKV
// initialization
let metal = MTKView(frame: self.view.bounds)
metal.device = MTLCreateSystemDefaultDevice()
self.renderer = try! MetalRenderer(mtkView: metal)
metal.delegate = self.renderer
self.view.addSubview(metal);
<代码>导入基础
进口金属套件
进口单指令多数据
公共枚举MetalError:错误{
案例mtkViewError
案例渲染器
}
内部类MetalRenderer:NSObject,MTKViewDelegate{
私有let命令队列:MTLCommandQueue;
私有let管道状态:MTLRenderPipelineState
私有变量视口大小:SIMD2=SIMD2(x:10,y:10);
私有弱var mtkView:mtkView?
init(mtkView:mtkView)抛出{
guard let device=mtkView.device else{
打印(“未找到设备错误”)
抛出MetalError.mtkViewError
}
self.mtkView=mtkView
//在项目中加载扩展名为.metal的所有着色器文件。
guard let defaultLibrary=device.makeDefaultLibrary()else{
打印(“找不到库”)
抛出MetalError.mtkViewError
}
让vertexFunction=defaultLibrary.makeFunction(名称:“vertexShader”)
让fragmentFunction=defaultLibrary.makeFunction(名称:“fragmentShader”)
mtkView.layer.isOpaque=false;
mtkView.layer.backgroundColor=UIColor.clear.cgColor
mtkView.isOpaque=false;
mtkView.backgroundColor=.clear
让pipelineStateDescriptor=MTLRenderPipelineDescriptor();
pipelinestatescriptor.label=“Pipeline”;
pipelineStateDescriptor.vertexFunction=vertexFunction;
pipelineStateDescriptor.fragmentFunction=fragmentFunction;
pipelineStateDescriptor.isAlphaToCoverageEnabled=true
pipelineStateDescriptor.colorAttachments[0]。pixelFormat=.bgra8Unorm;
pipelineStateDescriptor.colorAttachments[0]。isBlendingEnabled=true;
pipelineStateDescriptor.colorAttachments[0]。destinationRGBBlendFactor=.oneMinusSourceAlpha;
pipelineStateDescriptor.colorAttachments[0]。destinationAlphaBlendFactor=.oneMinusSourceAlpha;
pipelineState=try!device.makeRenderPipelineState(描述符:pipelineStateDescriptor);
guard let queue=device.makeCommandQueue()else{
打印(“生成命令队列错误”)
抛出MetalError.mtkViewError
}
commandQueue=队列
}
func mtkView(view:mtkView,drawableSizeWillChange size:CGSize){
viewportSize.x=UInt32(size.width)
viewportSize.y=UInt32(size.height)
}
func绘图(视图中:MTKView){
让顶点:[顶点]=[
顶点(位置:SIMD3(x:250.0,y:-250.0,z:0)),
顶点(位置:SIMD3(x:-250.0,y:-250.0,z:0)),
顶点(位置:SIMD3(x:0.0,y:250.0,z:0)),
]
guard let commandBuffer=commandQueue.makeCommandBuffer()else{
打印(“无法创建命令缓冲区”)
返回
}
//为当前可绘制对象的每个渲染过程创建新的命令缓冲区。
commandBuffer.label=“MyCommand”;
//获取从视图的可绘制纹理生成的renderPassDescriptor。
guard let renderPassDescriptor=view.currentRenderPassDescriptor else{
打印(“无法创建渲染过程描述符”)
返回
}
guard let RenderCoder=commandBuffer.MakerRenderCommandEncoder(描述符:renderPassDescriptor)else{
打印(“无法创建渲染编码器”)
返回
}
renderPassDescriptor.colorAttachments[0]。loadAction=.clear
renderPassDescriptor.colorAttachments[0].clearColor=MTLClearColorMake(1.0,0.0,0.5)
rendercoder.label=“myrendercoder”;
//设置要绘制到的可绘制区域。
RenderCoder.setViewport(MTLViewport(原点:0.0,原点:0.0,宽度:Double(viewportSize.x),高度:Double(viewportSize.y),znear:0.0,zfar:1.0))
RenderCoder.setRenderPipelineState(pipelineState)
//传入参数数据。
RenderCoder.setVertexBytes(顶点,长度:MemoryLayout.size*vertices.count,索引:Int(VertexInputIndexVertices.rawValue))
RenderCoder.setVertexBytes(&viewportSize,长度:MemoryLayout.size,索引:Int(VertexInputIndexViewportSize.rawValue))
RenderCoder.drawPrimitives(类型:MTLPrimitiveType.triangle,顶点起点:0,顶点计数:3)
renderCoder.endencode()
//在帧缓冲区完成后,使用当前可绘制文件安排一个显示。
guard let drawable=view.currentDrawable-else{
打印(“无法获取当前可绘制的”)
返回
}
commandBuffer.present(可提取)
//在此处完成渲染并将命令缓冲区推送到GPU。
commandBuffer.commit()
}
}
多亏了Frank,答案是只设置视图本身的clearColor属性,我没有设置。我还删除了MTLRenderPipelineDescriptor中的大多数调整,现在的代码是:
let pipelineStateDescriptor = MTLRenderPipelineDescriptor();
pipelineStateDescriptor.label = "Pipeline";
pipelineStateDescriptor.vertexFunction = vertexFunction;
pipelineStateDescriptor.fragmentFunction = fragmentFunction;
pipelineStateDescriptor.colorAttachments[0].pixelFormat =
mtkView.colorPixelFormat;
此外,无需从currentRenderPassDescriptor更改MTLRenderPassDescriptor
编辑:还要确保将MTKView的isOpaque属性也设置为false。您是否尝试过直接设置
MTKView的clearColor
?lol我显然错过了该属性:)它实际上改变了颜色,但alpha值0仍然不能使其透明,我将进一步实验。我当前正在设置此选项,以便获得白色背景:mtkView.clearColor=MTLClearColorMake(1.0、1.0、1.0、0.0)
let pipelineStateDescriptor = MTLRenderPipelineDescriptor();
pipelineStateDescriptor.label = "Pipeline";
pipelineStateDescriptor.vertexFunction = vertexFunction;
pipelineStateDescriptor.fragmentFunction = fragmentFunction;
pipelineStateDescriptor.colorAttachments[0].pixelFormat =
mtkView.colorPixelFormat;