金属质感-绘制和擦除

时间:2018-09-11 00:54:30

标签: ios swift metal

我正在尝试将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

render函数使用上面的管道描述符:

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

image shows how "erase" is not working.

摘要:我正在渲染要显示的金属纹理,但现在不介绍如何设置混合参数来“擦除”先前绘制的纹理的选定区域。

1 个答案:

答案 0 :(得分:0)

i仅设置此sourceAlphaBlendFactor = .zero,而没有设置sourceRGBBlendFactor。例如,我使用了https://github.com/codelynx/MetalPaint

中的代码