ios - 金属质感 - 绘制和擦除
问题描述
我正在尝试修改 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
摘要:我正在将金属纹理渲染到屏幕上,但现在不知道如何设置混合参数“擦除”先前绘制的纹理的选定区域。
解决方案
我只设置了这个 sourceAlphaBlendFactor = .zero,没有 sourceRGBBlendFactor。例如,我使用了来自https://github.com/codelynx/MetalPaint的代码
推荐阅读
- android - 获取设备 emulator-5554 处于脱机状态
- fluentui-react - Fluent UI 中的砌体布局?
- python - 在 Spacy 中训练两个连续的 NER 管道
- r - 向下钻取图中的单击事件
- protractor - 在 Protractor 中运行“keyDown(protractor.Key.CONTROL)”时出现“UnsupportedOperationError: sendKeysToActiveElement”错误
- python - 如何将列表列表中的负数转换为0?
- c# - 无法正确单步执行静态构造函数中的代码(VS2019、C#、.NET 4.7.2)
- css - 如何在 R Shiny App 中添加水平和垂直滚动条以通过滚动显示完整的绘图
- flutter - Flutter GETX:每次我们导航到其他页面/路由时如何删除初始化控制器
- terminal - 如何在 awesomeWM 中向我的 alacritty 终端添加填充