Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 金属α混合不起作用_Ios_Objective C_Graphics_Metal - Fatal编程技术网

Ios 金属α混合不起作用

Ios 金属α混合不起作用,ios,objective-c,graphics,metal,Ios,Objective C,Graphics,Metal,无论出于什么原因,我在金属中有阿尔法混合的问题。我正在绘制MTKView,对于我创建的每个管道,我执行以下操作: descriptor.colorAttachments[0].blendingEnabled = YES; descriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd; descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOpera

无论出于什么原因,我在金属中有阿尔法混合的问题。我正在绘制MTKView,对于我创建的每个管道,我执行以下操作:

descriptor.colorAttachments[0].blendingEnabled = YES;
descriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
然而,无论出于何种原因,这都不会导致阿尔法测试发生。甚至可以在帧调试器中进行检查,您将看到alpha为0的顶点被绘制为黑色而不是透明

我的一个想法是,某些几何体最终位于完全相同的z平面上,因此如果alpha混合在同一z平面上不起作用,则可能会导致问题。但我不认为这是一件事

为什么alpha混合不起作用

我希望它们能像透明玻璃一样混合在一起。像这样想


α混合是一种依赖于顺序的透明度技术。这意味着(半)透明对象不能像(更昂贵的)与顺序无关的透明技术那样以任意顺序渲染

  • 确保透明2D对象(例如圆形、矩形等)具有不同的深度值。(这样您可以自己定义绘图顺序。否则,绘图顺序取决于排序算法的实现和排序前的初始顺序。)
  • 根据这些2D对象的深度值从后向前排序
  • 使用alpha混合从后向前绘制2D对象(画家的算法)。(当然,2D对象需要alpha值<1才能实际看到一些混合。)
  • alpha混合的混合状态正确:

    // The blend formula is defined as:
    // (source.rgb * sourceRGBBlendFactor  )   rgbBlendOperation (destination.rgb * destinationRGBBlendFactor  ) 
    // (source.a   * sourceAlphaBlendFactor) alphaBlendOperation (destination.a   * destinationAlphaBlendFactor)
    // <=>
    // (source.rgba * source.a) + (destination.rgba * (1-source.a))
    
    descriptor.colorAttachments[0].blendingEnabled             = YES;
    descriptor.colorAttachments[0].rgbBlendOperation           = MTLBlendOperationAdd;
    descriptor.colorAttachments[0].alphaBlendOperation         = MTLBlendOperationAdd;
    descriptor.colorAttachments[0].sourceRGBBlendFactor        = MTLBlendFactorSourceAlpha;
    descriptor.colorAttachments[0].sourceAlphaBlendFactor      = MTLBlendFactorSourceAlpha;
    descriptor.colorAttachments[0].destinationRGBBlendFactor   = MTLBlendFactorOneMinusSourceAlpha;
    descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
    
    //混合公式定义为:
    //(source.rgb*sourceRGBBlendFactor)rgbblendoction(destination.rgb*destinationRGBBlendFactor)
    //(source.a*sourceAlphaBlendFactor)alphaBlendOperation(destination.a*destinationAlphaBlendFactor)
    // 
    //(source.rgba*source.a)+(destination.rgba*(1-source.a))
    descriptor.colorAttachments[0]。blendingEnabled=YES;
    descriptor.colorAttachments[0].rgbblendooperation=mtlblendooperationadd;
    descriptor.colorAttachments[0]。alphaBlendOperation=MTLBlendOperationAdd;
    descriptor.colorAttachments[0].sourceRGBBlendFactor=MTLBlendFactorSourceAlpha;
    descriptor.colorAttachments[0]。sourceAlphaBlendFactor=MTLBlendFactorSourceAlpha;
    descriptor.colorAttachments[0]。destinationRGBBlendFactor=MTLBlendFactorOnEminenusSourceAlpha;
    descriptor.colorAttachments[0]。destinationAlphaBlendFactor=MTLBlendFactorOnEminenusSourceAlpha;
    
    您是否希望视图的某些部分最终透明,以便其后面的视图/窗口可见?或者在正在绘制的同一视图中是否存在某些现有渲染?颜色附件的加载操作是什么?如果是“透明”,什么是透明色?透明色是黑色的背景色。加载动作的每一帧都被清除,因此所有内容都被清除为黑色。有许多二维三角形,每个三角形都具有不同的透明度级别。所以希望它们的透明度是可见的。考虑到你正在使用的混合,如果一个三角形是用黑色的透明度绘制的,你可以通过三角形“看到”黑色。它不会在黑色上打一个透明的洞。我再次问:您是否希望视图最终部分透明,以便其后面的视图/窗口可见?正确。问题是,两个半透明度的三角形相互重叠,会形成一个颜色较深的黑色三角形。从本质上说,较小深度的玻璃阻挡了较大深度的玻璃对颜色的贡献。我基本上是在寻找玻璃重叠的外观。我用图片更新了问题。在swift中,这给出了:>