【问题标题】:preferred method to load a texture with metal用金属加载纹理的首选方法
【发布时间】:2016-08-14 05:28:07
【问题描述】:

我正在尝试像这样将资产图像加载到金属中:

let textureLoader = MTKTextureLoader(device: context.device)
do{
    let image = UIImage(named: name)
    try texture = textureLoader.newTextureWithCGImage(image!.CGImage!, options: [:])
}catch let error{
    print("Failed to create texture, error \(error)")
}

我能够显示图像,但颜色被置换,就好像文件包含 RGB 数据但它被解释为 BGR 数据一样。我知道金属层的默认色彩空间是BGRA8Unorm,但我不知道如何强制以该格式加载图像。

【问题讨论】:

    标签: swift image graphics metal


    【解决方案1】:

    你应该使用MTKTextureLoader上的指定类方法从一个URL创建一个新的MTLTexture

    let device = MTLCreateSystemDefaultDevice()!
    let loader = MTKTextureLoader(device: device)
    let url = NSBundle.mainBundle().URLForResource("pic", withExtension: "jpg")!
    let texture = try! loader.newTextureWithContentsOfURL(url, options: nil)
    

    【讨论】:

    • 这实际上是我尝试的第一件事,但我无法以这种方式找到图像。我收到错误fatal error: unexpectedly found nil while unwrapping an Optional value。我的图片位于Assets.xcassets。如何正确指定该文件夹的路径?
    • 目前没有简单的方法来获取资产目录中资产的 URL。如果您使用资产目录,则应使用 methods that knows how to load named assets 之一。
    • 我认为该方法尚未实现。我的MTKTextureLoader 实例没有该功能。我想我会尝试将图像存储在资产文件夹之外
    • 要获取我的资产的 URL,我只需在文件名中包含资产文件夹名称,如下所示: let url = NSBundle.mainBundle().URLForResource("Assets.xcassets/pic", withExtension: "jpg")
    猜你喜欢
    • 2020-11-30
    • 2013-08-24
    • 1970-01-01
    • 2020-10-29
    • 2018-07-30
    • 2018-08-12
    • 2018-04-25
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多