【问题标题】:Init a Metal texture with the type of 3D使用 3D 类型初始化金属纹理
【发布时间】:2017-07-08 17:38:14
【问题描述】:

最近,我在玩 SceneKit,我发现了 colorGrading 属性。医生说

此材质属性的内容值必须是 3D 颜色 查找表,或表示此类表的 2D 纹理图像 横向排列。

并且 3D 颜色查找表可以从金属纹理中读取。

您可以将此立方体格式的数据作为具有 type3D 纹理类型的金属纹理提供。

那么我该如何设置scnCamera.colorGrading.contents 属性。

【问题讨论】:

    标签: ios swift scenekit metal


    【解决方案1】:

    创建 3D 纹理与创建 2D 纹理非常相似,前提是您有一个包含适当布局的图像数据的缓冲区。我假设你已经有了。以下是如何创建纹理本身,将数据复制到其中,并将其设置为颜色分级纹理:

    var dim = 16
    
    var values: UnsafeMutablePointer<Float> = ... // alloc and populate 3D array of pixels
    
    let textureDescriptor = MTLTextureDescriptor()
    textureDescriptor.textureType = .type3D
    textureDescriptor.pixelFormat = .rgba32Float
    textureDescriptor.width = dim
    textureDescriptor.height = dim
    textureDescriptor.depth = dim
    textureDescriptor.usage = .shaderRead
    
    let texture = device.makeTexture(descriptor: textureDescriptor)
    
    texture.replace(region: MTLRegionMake3D(0, 0, 0, dim, dim, dim),
                    mipmapLevel:0,
                    slice:0,
                    withBytes:values,
                    bytesPerRow:dim * MemoryLayout<Float>.size * 4,
                    bytesPerImage:dim * dim * MemoryLayout<Float>.size * 4)
    
    camera.colorGrading.contents = texture
    

    编辑

    这是一个完整的解析器,可以将 .cube 文件转换为适合与此属性一起使用的 MTLTexture

    import Metal
    
    class AdobeLUTParser {
    
        static func texture(withContentsOf url: URL, device: MTLDevice) -> MTLTexture? {
    
            let lutString = try! NSString(contentsOf: url, encoding: String.Encoding.utf8.rawValue)
    
            let lines = lutString.components(separatedBy: "\r\n") as [NSString]
    
            var dim = 2
    
            var values: UnsafeMutablePointer<Float>? = nil
            var index = 0
    
            for line in lines {
    
                if line.length == 0 { continue; } // skip blanks
    
                let firstChar = line.character(at: 0)
    
                if firstChar < 58 /*':'*/ {
                    if values == nil {
                        print("Error: Got data before size in LUT")
                        break;
                    }
    
                    let numbers = line.components(separatedBy: " ") as [NSString]
                    if numbers.count == 3 {
                        let r = numbers[0].floatValue
                        let g = numbers[1].floatValue
                        let b = numbers[2].floatValue
                        let a = Float(1)
    
                        values![index * 4 + 0] = r
                        values![index * 4 + 1] = g
                        values![index * 4 + 2] = b
                        values![index * 4 + 3] = a
    
                        index += 1
                    }
                } else {
                    if line.hasPrefix("LUT_3D_SIZE") {
                        let sizeString = line.components(separatedBy: " ")[1] as NSString
                        dim = Int(sizeString.intValue)
                        if dim < 2 || dim > 512 {
                            print("Error: insane LUT size: \(dim)")
                        }
                        let rawPointer = malloc(dim * dim * dim * 4 * MemoryLayout<Float>.size)
                        values = rawPointer!.bindMemory(to: Float.self, capacity: dim * dim * dim * 4)
                    } else if line.hasPrefix("LUT_1D_SIZE") {
                        print("Error: 1D LUTs not supported")
                        break
                    }
                }
            }
    
            if values == nil {
                print("Did not parse LUT successfully")
                return nil
            }
    
            let textureDescriptor = MTLTextureDescriptor()
            textureDescriptor.textureType = .type3D
            textureDescriptor.pixelFormat = .rgba32Float
            textureDescriptor.width = dim
            textureDescriptor.height = dim
            textureDescriptor.depth = dim
            textureDescriptor.usage = .shaderRead
    
            let texture = device.makeTexture(descriptor: textureDescriptor)
    
            texture.replace(region: MTLRegionMake3D(0, 0, 0, dim, dim, dim),
                            mipmapLevel:0,
                            slice:0,
                            withBytes:values!,
                            bytesPerRow:dim * MemoryLayout<Float>.size * 4,
                            bytesPerImage:dim * dim * MemoryLayout<Float>.size * 4)
    
            return texture
        }
    }
    

    用法:

    let mtlDevice = MTLCreateSystemDefaultDevice()
    
    let lutURL = Bundle.main.url(forResource: "MyGradingTexture", withExtension: "cube")
    
    let lutTexture = AdobeLUTParser.texture(withContentsOf: lutURL!, device: mtlDevice!)
    
    camera.colorGrading.contents = lutTexture
    

    【讨论】:

    • 哦不...文档说有两种方法可以设置此属性。一个是给出一个方形图像,一个是 3D 颜色查找表。我搜索 3D 颜色查找表并下载一些 .cube 文件。我想如果我可以将这些文件设置为此颜色分级。但是医生说应该使用金属。我不知道该怎么做。
    • 我总是收到错误Error: Got data before size in LUT Did not parse LUT successfully
    • 听起来你的立方体文件格式不正确,但在链接过期之前我无法下载它。
    • 谢谢,现在运行良好。但我得到了不好的效果。这似乎是一个错误或 .cube 文件问题。我发布了一个新问题,请检查一下。 Work inside colorGrading Property
    • 因为每个像素有四个分量 (RGBA)。
    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2020-11-18
    • 2017-06-14
    相关资源
    最近更新 更多