简体   繁体   English

如何将黑白图像加载到金属纹理中

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

I use two methods to load black white image texture in metal as below:我使用两种方法在金属中加载黑白图像纹理,如下所示:

 //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];

but both crash, the error log is但都崩溃了,错误日志是

"Error Domain=MTKTextureLoaderErrorDomain Code=0 "Image decoding failed" UserInfo={NSLocalizedDescription=Image decoding failed, MTKTextureLoaderErrorKey=Image decoding failed}", "Error Domain=MTKTextureLoaderErrorDomain Code=0 "图像解码失败" UserInfo={NSLocalizedDescription=图像解码失败,MTKTextureLoaderErrorKey=图像解码失败}",

how to fix this issue?如何解决这个问题? Also if I load the colorful image into metal, it runs.此外,如果我将彩色图像加载到金属中,它就会运行。 在此处输入图像描述

-(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];
}

and in your metal configurations并在您的金属配置中

id<MTLTexture> mtltexture01;

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

Keep in mind texture02 is a filename and the file needs to be available in your apps assets.请记住, texture02是一个文件名,该文件需要在您的应用资产中可用。 You can store the image as MTLTexture into your asset in Xcode and so the conversion is done at build time.您可以将图像作为 MTLTexture 存储到 Xcode 中的资产中,因此转换在构建时完成。

Also check if the image contains at least proper opacity and is one of PNG, JPEG, or TIFF format.还要检查图像是否至少包含适当的不透明度,并且是 PNG、JPEG 或 TIFF 格式之一。 There is known loader trouble when textures contain only gray/ black colors and 0% transparency is used.当纹理仅包含灰色/黑色 colors 并且使用 0% 透明度时,存在已知的加载程序问题。

later integrate your texture to the renderEncoder/commandEncoder稍后将您的纹理集成到 renderEncoder/commandEncoder

screenshot: Xcode Assets Texture Configurator截图:Xcode 资产纹理配置器在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM