金属纹理缓冲区没有在Swift 2.2中刷新

时间:2016-12-02 07:55:45

标签: swift texture-mapping texture2d metal

我使用Metal来显示纹理的3D对象。 在第一次加载时,纹理显示正常。但是如果我用新纹理改变运行时间,那么新纹理就会与旧纹理合并。

我的代码是:

let texture = MetalTexture(resourceName: "", ext: "", mipmaped: true)
        //  texture.path = "\(NSBundle.mainBundle().bundlePath)/smlroot/models/bodies/FB020.png"
        texture.path = pngPath
        texture.loadTexture(device: device, commandQ: commandQ, flip: false)
        super.readPly(vertices,faces: faces, texture: texture.texture)

这里我每次更改纹理运行时都会创建一个新的纹理对象。然后 使用此代码加载它:

func loadTexture(device device: MTLDevice, commandQ: MTLCommandQueue, flip: Bool){

    let image = UIImage(contentsOfFile: path)?.CGImage

    let colorSpace = CGColorSpaceCreateDeviceRGB()

    width = CGImageGetWidth(image!)
    height = CGImageGetHeight(image!)

    let rowBytes = width * bytesPerPixel

    let context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, rowBytes, colorSpace, CGImageAlphaInfo.PremultipliedLast.rawValue)
    let bounds = CGRect(x: 0, y: 0, width: Int(width), height: Int(height))
    CGContextClearRect(context!, bounds)

    if flip == false{
      CGContextTranslateCTM(context!, 0, CGFloat(self.height))
      CGContextScaleCTM(context!, 1.0, -1.0)
    }

    CGContextDrawImage(context!, bounds, image!)

    let texDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.RGBA8Unorm, width: Int(width), height: Int(height), mipmapped: isMipmaped)
    target = texDescriptor.textureType
    texture = device.newTextureWithDescriptor(texDescriptor)

    let pixelsData = CGBitmapContextGetData(context!)
    let region = MTLRegionMake2D(0, 0, Int(width), Int(height))
    texture.replaceRegion(region, mipmapLevel: 0, withBytes: pixelsData, bytesPerRow: Int(rowBytes))

    if (isMipmaped == true){
      generateMipMapLayersUsingSystemFunc(texture, device: device, commandQ: commandQ, block: { (buffer) -> Void in
        print("mips generated")
      })
    }

    print("mipCount:\(texture.mipmapLevelCount)")
  }

渲染对象时我使用波纹管加载它:

renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
            renderEncoder.setCullMode(.None)
            renderEncoder.setFragmentTexture(texture, atIndex: 0)
            if let samplerState = samplerState{
                renderEncoder.setFragmentSamplerState(samplerState, atIndex: 0)
            }
            let nodeModelMatrix = self.modelMatrix(rotationY)
            nodeModelMatrix.multiplyLeft(parentModelViewMatrix)

            let uniformBuffer = bufferProvider.nextUniformsBuffer(projectionMatrix, modelViewMatrix: nodeModelMatrix, light: light)

            renderEncoder.setVertexBuffer(uniformBuffer, offset: 0, atIndex: 1)

            renderEncoder.setFragmentBuffer(uniformBuffer, offset: 0, atIndex: 1)

            //change this.
            //renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: vertexCount)
            renderEncoder.drawIndexedPrimitives(.Triangle, indexCount: indexCount, indexType: .UInt32, indexBuffer: indexBuffer, indexBufferOffset: 0)

我得到了重叠的纹理。任何人都可以帮我确定问题吗?

enter image description here

0 个答案:

没有答案
相关问题