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