首页 > 解决方案 > 金属质感 - 绘制和擦除

问题描述

我正在尝试修改 Apple 的示例 GLPaint(使用 OpenGL 绘制应用程序)以使用 Metal 而不是 OpenGL。我可以使用 Metal 将笔触渲染到屏幕上,但在“擦除”它时遇到了困难。

在 Metal 中,我使用以下混合参数:

renderPipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
renderPipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

渲染函数使用上述管道描述符:

let descriptor = MTLRenderPassDescriptor()

descriptor.colorAttachments[0].loadAction = .load
descriptor.colorAttachments[0].clearColor = MTLClearColor(red: 0.0, green: 0.0, blue: 0.0, alpha:0.0)
descriptor.colorAttachments[0].storeAction = .store
descriptor.colorAttachments[0].texture = colorAttachmentTexture

let renderCommandEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: descriptor)
renderCommandEncoder?.setRenderPipelineState( blendRGBAndAlphaPipelineState)
    texturedQuad.encodeDrawCommands(encoder: renderCommandEncoder!, texture: texture)
    renderCommandEncoder?.endEncoding()

如何创建管道描述符以“擦除”先前渲染纹理的部分?在 OpenGL 中,我可以通过执行以下操作在“绘图”和“擦除”之间切换:

if(eraserEnabled)
{
    glColor4f(0,0,0,1);
    glBlendFunc( GL_ZERO,GL_ONE_MINUS_SRC_ALPHA);
}
else
{

    // Set color of the brush 
    glColor4f(1,0,0,1);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

我尝试在 Metal 中创建第二个用于“擦除”的 renderPipeline。我使用了下面的混合参数,但它不起作用。

renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha

图像显示了如何

摘要:我正在将金属纹理渲染到屏幕上,但现在不知道如何设置混合参数“擦除”先前绘制的纹理的选定区域。

标签: iosswiftmetal

解决方案


我只设置了这个 sourceAlphaBlendFactor = .zero,没有 sourceRGBBlendFactor。例如,我使用了来自https://github.com/codelynx/MetalPaint的代码


推荐阅读