金属掩蔽 iOS
Masking in Metal iOS
我在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
鉴于你没有在你的 return 中使用 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
这将为您提供 "default" 行为,来源混合:source.rgba + destination.rgba
我在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
鉴于你没有在你的 return 中使用 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
这将为您提供 "default" 行为,来源混合:source.rgba + destination.rgba