【问题标题】:How to load HDR file texture for Metal iOS & Mac?如何为 Metal iOS 和 Mac 加载 HDR 文件纹理?
【发布时间】:2020-11-17 02:32:45
【问题描述】:

对于 OpenGL,我们可以简单地使用 stb_image。也许,它也适用于 Metal,但需要做一些额外的工作。

我不想包含更多库。所以我尝试了MTKTextureLoader,但没有出现错误。

MTKTextureLoaderErrorKey=Image decoding failed

在 Metal API 中使用 HDR 纹理文件的正确方法是什么?

【问题讨论】:

    标签: ios objective-c macos metal


    【解决方案1】:

    由于没有人回答这个问题,我将在下面发布解决方案。

    MTKTextureLoader *loader = [[MTKTextureLoader alloc] initWithDevice: _device];
            
    NSDictionary *textureLoaderOptions = @ {
                MTKTextureLoaderOptionSRGB: @NO,
                MTKTextureLoaderOptionAllocateMipmaps: @YES,
                MTKTextureLoaderOptionGenerateMipmaps: @YES,
                MTKTextureLoaderOptionTextureUsage : @(MTLTextureUsageShaderRead),
                MTKTextureLoaderOptionTextureStorageMode : @(MTLStorageModePrivate),
                MTKTextureLoaderOptionOrigin: MTKTextureLoaderOriginFlippedVertically };
    
    let pathHDR = [NSBundle.mainBundle pathForResource:@"filename" ofType:@"hdr"];
    
    #if TARGET_OS_OSX
        let urlHDR = [[NSURL alloc] initFileURLWithPath:pathHDR];
        let imageData = [[[NSImage alloc] initWithContentsOfURL:urlHDR] TIFFRepresentation];
    #else
        //let imageData = [[NSData alloc] initWithContentsOfFile:pathHDR];
        let image = [[UIImage alloc] initWithContentsOfFile:pathHDR];
        let imageData = UIImageJPEGRepresentation(image, 1.0);
        //UIImagePNGRepresentation(image);
    #endif
    
    _textureHDR = [loader newTextureWithData:imageData options:textureLoaderOptions error:&ERROR];
    

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多