动态改变Metal中顶点颜色的正确方法是什么?

时间:2015-04-10 05:17:45

标签: ios swift metal

我想进入Metal。我已经完成了一些OpenGL,但它已经好几年了,所以我无法想出这样做的正确方法。我一直在使用Ray Wenderlich的金属教程介绍(http://www.raywenderlich.com/81399/ios-8-metal-tutorial-swift-moving-to-3d

我有一个Vertex结构:

struct Vertex
{
    var x, y, z: Float
    var r, g, b, a: Float

    func floatBuffer() -> Array<Float>
    {
        return [x, y, z, r, g, b, a]
    }
}

和顶点缓冲类:

class VertexBuffer
{
    var vertexData = Array<Float>()

    init(vertices: Array<Vertex>)
    {
        var vertexData = Array<Float>()

        for vertex in vertices
        {
            vertexData += vertex.floatBuffer()
        }

        self.vertexData = vertexData
    }

    func updateColorOfVertexAtIndex(index: Int, newRed: CGFloat, newGreen: CGFloat, newBlue: CGFloat, newAlpha: CGFloat)
    {
        let vertexDataIndex = index * 7

        if (vertexDataIndex < self.vertexData.count)
        {
            self.vertexData[vertexDataIndex + 3] = Float(newRed)
            self.vertexData[vertexDataIndex + 4] = Float(newGreen)
            self.vertexData[vertexDataIndex + 5] = Float(newBlue)
            self.vertexData[vertexDataIndex + 6] = Float(newAlpha)
        }
    }

    func updateColorOfVertexAtIndex(index: Int, newColor: UIColor)
    {
        var red: CGFloat = 0.0
        var green: CGFloat = 0.0
        var blue: CGFloat = 0.0
        var alpha: CGFloat = 0.0

        newColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        self.updateColorOfVertexAtIndex(index, newRed: red, newGreen: green, newBlue: blue, newAlpha: alpha)
    }
}

根据其他一些应用程序的状态,我想更新一个或多个顶点的RGBA。使用VertexBuffer.updateColor...方法,我将更新VertexBuffer.vertexData中的相应RGBA值。我有一个CADisplayLink连接到下面的函数:

func diffusionLoop()
{
    self.updateVertexData()

    var vertexData = self.inkVertexBuffer.vertexData

    let dataSize = vertexData.count * sizeofValue(vertexData[0])
    let vertexBuffer = self.device.newBufferWithBytes(vertexData, length: dataSize, options: nil)


    autoreleasepool { () -> () in
        self.renderColorsWithVertexBuffer(vertexBuffer)
    }
}

func renderColorsWithVertexBuffer(vertexBuffer: MTLBuffer)
    {
        let renderPassDescriptor = MTLRenderPassDescriptor()
        let drawable = self.metalLayer.nextDrawable()

        renderPassDescriptor.colorAttachments[0].texture = drawable.texture
        renderPassDescriptor.colorAttachments[0].loadAction = .Clear
        renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(1.0, 1.0, 1.0, 0.0)
        renderPassDescriptor.colorAttachments[0].storeAction = .Store

        let commandBuffer = self.commandQueue.commandBuffer()

        if let renderEncoder = commandBuffer.renderCommandEncoderWithDescriptor(renderPassDescriptor) {
            renderEncoder.setRenderPipelineState(self.pipelineState)
            renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
            renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: self.vertexCount, instanceCount: self.triangleCount)
            renderEncoder.endEncoding()
        }

        commandBuffer.presentDrawable(drawable)
        commandBuffer.commit()
    }

问题是,我不知道这是否是正确的方法。我的目的是经常更新某些顶点的颜色,但是每次修改顶点数据并创建一个MTLBuffer的答案是什么?或其他什么?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:2)

理想情况下,您不希望更新 vertexBuffer 的值。在我看来,你要做的最好的方法是将不可变数据与可变数据分开。

所以我会用颜色值创建单独的统一缓冲区并更新它而不是 vertexBuffer 本身。

你还说“ 基于我想要更新一个或多个顶点的RGBA的其他一些应用程序的状态...... ”,它可能会更好将那些“状态”作为常量传递给着色器和着色器内部,以计算每个顶点或片段的颜色。

顶点着色器有:

unsigned int vid [[ vertex_id ]]

您可以使用它明确设置每个顶点的颜色。