我在opengl中做了如下蒙版
vec4 patCol = texture2D(pattern, samplePos);
vec4 maskCol = texture2D(tex0, texCoordVarying);
gl_FragColor=vec4(patCol.xyz, patCol.w);
我想在iOS中进行相同的遮罩,maskCol中的纹理是半透明的。当我无法在Metal中获得类似的输出时。有人可以帮我吗?
渲染管线描述符
let pipelineDescriptor = MTLRenderPipelineDescriptor()
pipelineDescriptor.fragmentFunction = fragmentFunction
pipelineDescriptor.vertexFunction = vertexFunction
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
pipelineDescriptor.sampleCount = 1
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .one
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .one
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
答案 0 :(得分:1)
鉴于您未在退货中使用maskCol,我假设您正在尝试执行以下操作:
float4 patCol...
float4 maskCol...
return float4(patCol.rgb, maskCol.a);
也就是说,从蒙版中获取alpha值并将其应用于源patCol ...
要使其正常工作,您需要在pipelineDescriptor中设置混合选项:
pipelineDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
pipelineDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
pipelineDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
这将为您提供“默认”行为,即源混合,即:source.rgba + destination.rgba