簡體   English   中英

Metal 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中獲得類似的輸出時。 任何人都可以在這方面幫助我。

Renderpipeline描述符

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

鑒於您未在退貨中使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM