[英]Metal texture - Draw and Erase
I am trying to modify Apple's sample GLPaint (paint app using OpenGL) to use Metal instead of OpenGL. 我正在尝试将Apple的示例GLPaint(使用OpenGL的绘画应用程序)修改为使用Metal而不是OpenGL。 I am able to render a brush stroke to screen using Metal, but am having difficulties "erasing" it. 我可以使用Metal渲染笔触到屏幕上,但是很难“擦除”它。
In Metal, I am using the following blending parameters: 在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
The render function uses the above pipeline descriptor: 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()
How can I create a pipeline descriptor to "erase" portions of the previously rendered texture? 如何创建管道描述符以“擦除”先前渲染的纹理的某些部分? In OpenGL, I was able to toggle between "drawing" and "erasing" by performing the following: 在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);
}
I tried creating a second renderPipeline in Metal that is used for "erasing". 我尝试在Metal中创建第二个renderPipeline,用于“擦除”。 I used the blending parameters below, but it is not working. 我在下面使用了混合参数,但是它不起作用。
renderPipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .zero
renderPipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
renderPipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
Summary: I am rendering a Metal texture to screen but do not now how to set blending parameters "erase" selected regions of the previously drawn texture. 简介:我正在渲染“金属”纹理以显示在屏幕上,但现在不介绍如何设置混合参数来“擦除”先前绘制的纹理的选定区域。
i setup only this sourceAlphaBlendFactor = .zero, whithout sourceRGBBlendFactor. 我只设置了这个sourceAlphaBlendFactor = .zero,而没有设置sourceRGBBlendFactor。 For example I used code from https://github.com/codelynx/MetalPaint 例如我使用了https://github.com/codelynx/MetalPaint中的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.