簡體   English   中英

如何將黑白圖像加載到金屬紋理中

[英]How to load black white image into metal texture

我使用兩種方法在金屬中加載黑白圖像紋理,如下所示:

 //1.
    mtltexture01 = [textureLoader newTextureWithName:@"texture02"
                                         scaleFactor:1.0
                                              bundle:nil
                                             options:textureLoaderOptions
                                               error:&error];



 //2.
    UIImage *img = [UIImage imageNamed:@"texture02"];

    mtltexture01 = [textureLoader newTextureWithCGImage:img.CGImage options:textureLoaderOptions error:&error];

但都崩潰了,錯誤日志是

"Error Domain=MTKTextureLoaderErrorDomain Code=0 "圖像解碼失敗" UserInfo={NSLocalizedDescription=圖像解碼失敗,MTKTextureLoaderErrorKey=圖像解碼失敗}",

如何解決這個問題? 此外,如果我將彩色圖像加載到金屬中,它就會運行。 在此處輸入圖像描述

-(id<MTLTexture>)textureWithName:(NSString*)imgname UsingDevice:(id<MTLDevice>)device {

     MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:device];

     NSDictionary *textureLoaderOptions = @{
         MTKTextureLoaderOptionTextureUsage : @(MTLTextureUsageShaderRead),
         MTKTextureLoaderOptionTextureStorageMode : @(MTLStorageModePrivate)
     };

     return [textureLoader newTextureWithName:imgname
         scaleFactor:1.0
         bundle:nil
         options:textureLoaderOptions
         error:nil];
}

並在您的金屬配置中

id<MTLTexture> mtltexture01;

mtltexture01 = [self textureWithName:@"texture02" UsingDevice:device];

請記住, texture02是一個文件名,該文件需要在您的應用資產中可用。 您可以將圖像作為 MTLTexture 存儲到 Xcode 中的資產中,因此轉換在構建時完成。

還要檢查圖像是否至少包含適當的不透明度,並且是 PNG、JPEG 或 TIFF 格式之一。 當紋理僅包含灰色/黑色 colors 並且使用 0% 透明度時,存在已知的加載程序問題。

稍后將您的紋理集成到 renderEncoder/commandEncoder

截圖:Xcode 資產紋理配置器在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM