Как использовать рендеринг SCNRenderer и CIContext в конвейере Metal? - PullRequest
0 голосов
/ 12 января 2019

Я новичок в металле и пытаюсь дважды рисовать на MTLTexture до present() и commit(). Первый отрисовка с использованием SCNRenderer.render для рисования фона из SCNScene, затем вторая с использованием CIContext.render с отфильтрованным по глубине изображением пользователя с прозрачностью.

Мой renderPassDescriptor настроен как:

    // SCNRenderer
    let viewport = CGRect(x: 0, y: 0, width: CGFloat(currentDrawable.texture.width), height: CGFloat(currentDrawable.texture.width))

    //write to offscreenTexture, clear the texture before rendering using red, store the result
    let renderPassDescriptor = MTLRenderPassDescriptor()
    renderPassDescriptor.colorAttachments[0].texture = currentDrawable.texture
    renderPassDescriptor.colorAttachments[0].loadAction = .clear
    renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1, 0, 0, 1.0); // red
    renderPassDescriptor.colorAttachments[0].storeAction = .store

    scnRenderer.render(atTime: 0, viewport: viewport, commandBuffer: commandBuffer, passDescriptor: renderPassDescriptor)

    // CIContext render
    ciContext.render(ciImage, to: currentDrawable.texture, commandBuffer: commandBuffer, bounds: ciImage.extent, colorSpace: colorSpace)

Если я переключу порядок scnRenderer.render и ciContext, который когда-либо последний, появится на экране, как и ожидалось.

Поскольку во втором рисовании из CIContext используется изображение с прозрачностью, я включил альфа-смешение, следуя этой статье .

// Enable alpha Blending
        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

Это функция сквозного фрагмента шейдера:

fragment float4 passThroughFragment(ColorInOut in [[ stage_in ]],
                                texture2d<float> texture [[ texture(0) ]]) {
constexpr sampler colorSampler;
float4 color = texture.sample(colorSampler, in.texCoord);
return color;
}

Как предотвратить прозрачность от CIImage от перезаписи цвета фрагмента, нарисованного для сцены SCNS? Спасибо.

...