如何渲染SCNRenderer生成的MTLTexture?

时间:2018-06-03 14:14:48

标签: ios scenekit metal avassetwriter metalkit

目前我有一些带有一些物体的3D场景。我想将它渲染为2D纹理(例如MTLTexture)。之后我想在屏幕上像实时图像(例如MTKView或SKScene)一样在屏幕上显示这个渲染的纹理,同时将它写在电影中。

现在我有SCNRenderer和offscreenTexture进行渲染:

func setupMetal() {
    device = MTLCreateSystemDefaultDevice()
    commandQueue = device.makeCommandQueue()
    renderer = SCNRenderer(device: device, options: nil)
}

func setupTexture() {
    var rawData0 = [UInt8](repeating: 0, count: Int(textureSizeX) * Int(textureSizeY) * 4)

    let bytesPerRow = 4 * Int(textureSizeX)
    let bitmapInfo = CGBitmapInfo.byteOrder32Big.rawValue | CGImageAlphaInfo.premultipliedLast.rawValue

    let context = CGContext(data: &rawData0, width: Int(textureSizeX), height: Int(textureSizeY), bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: rgbColorSpace, bitmapInfo: bitmapInfo)!
    context.setFillColor(UIColor.black.cgColor)
    context.fill(CGRect(x: 0, y: 0, width: CGFloat(textureSizeX), height: CGFloat(textureSizeY)))

    let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor(pixelFormat: MTLPixelFormat.rgba8Unorm, width: Int(textureSizeX), height: Int(textureSizeY), mipmapped: false)

    textureDescriptor.usage = MTLTextureUsage(rawValue: MTLTextureUsage.renderTarget.rawValue | MTLTextureUsage.shaderRead.rawValue)

    let textureA = device.makeTexture(descriptor: textureDescriptor)

    let region = MTLRegionMake2D(0, 0, Int(textureSizeX), Int(textureSizeY))
    textureA?.replace(region: region, mipmapLevel: 0, withBytes: &rawData0, bytesPerRow: Int(bytesPerRow))

    offscreenTexture = textureA
}

private func initializeRenderPipelineState() {
    guard let device = device, let library = device.makeDefaultLibrary() else { return }

    let pipelineDescriptor = MTLRenderPipelineDescriptor()
    pipelineDescriptor.sampleCount = 1
    pipelineDescriptor.colorAttachments[0].pixelFormat = .bgr10_xr_srgb
    pipelineDescriptor.depthAttachmentPixelFormat = .invalid

    pipelineDescriptor.vertexFunction = library.makeFunction(name: "mapTexture")
    pipelineDescriptor.fragmentFunction = library.makeFunction(name: "displayTexture")

    do {
        renderPipelineState = try device.makeRenderPipelineState(descriptor: pipelineDescriptor)
    }
    catch {
        assertionFailure("Failed creating a render state pipeline. Can't render the texture without one.")
        return
    }
}

和MTKView在屏幕上显示:

func initializeMetalView() {
    metalView = MTKView(frame: bounds, device: device)
    metalView.delegate = self
    metalView.framebufferOnly = true
    metalView.colorPixelFormat = .bgr10_xr_srgb
    metalView.contentScaleFactor = UIScreen.main.scale
    metalView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    insertSubview(metalView, at: 0)
}

在每一帧上我试图将场景绘制到纹理并以MTKView

呈现
func draw(in view: MTKView) {
    let commandBuffer = commandQueue.makeCommandBuffer()

    let viewport = CGRect(x: 0, y: 0, width: CGFloat(textureSizeX), height: CGFloat(textureSizeY))

    //write to offscreenTexture
    let renderPassDescriptor = MTLRenderPassDescriptor()
    renderPassDescriptor.colorAttachments[0].texture = offscreenTexture
    renderPassDescriptor.colorAttachments[0].loadAction = .clear
    renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0.0)
    renderPassDescriptor.colorAttachments[0].storeAction = .store

    renderer.scene = scene
    renderer.pointOfView = scene.rootNode.childNodes[0]
    renderer.render(atTime: 0, viewport: viewport, commandBuffer: commandBuffer!, passDescriptor: renderPassDescriptor)

    guard let currentRenderPassDescriptor = metalView.currentRenderPassDescriptor, let currentDrawable = metalView.currentDrawable, let renderPipelineState = renderPipelineState else {
            return
    }

    let encoder = commandBuffer?.makeRenderCommandEncoder(descriptor: currentRenderPassDescriptor)
    encoder?.pushDebugGroup("RenderFrame")
    encoder?.setRenderPipelineState(renderPipelineState)
    encoder?.setFragmentTexture(offscreenTexture, index: 0)
    encoder?.drawPrimitives(type: .triangleStrip, vertexStart: 0, vertexCount: 4, instanceCount: 1)
    encoder?.popDebugGroup()
    encoder?.endEncoding()

    commandBuffer?.present(currentDrawable)
    commandBuffer?.commit()
}

这种技术甚至有效,但速度太慢。 我设法使用适当的fps与另一个sceneView。我创建了一个平面并将offscreen纹理附加为此平面的材质。但我想在2D引擎中做到这一点(因为我认为它应该更简单)(因为我希望能够像2D图像一样在屏幕上移动它)。

P.S。如果你对SCNView一无所知,请看看我的另一个问题。也许你会建议一个更好的解决方案。 基本上我想要 1)向用户呈现全屏背景和3D对象(使用透视投影)(简单) 2)我希望能够将SCNView的PART渲染到电影中。 (我知道如何使用全屏纹理,但我没有找到快速解决方案如何裁剪它)

更新 我看到这个电话中的主要问题:

guard let currentDrawable = metalView.currentDrawable else { return }

当我的申请需要大约" 0.013198"秒,但过了一段时间它只需要" 5.388259e-05"秒,一切都很完美。

0 个答案:

没有答案