Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在金属材质的另一个图像上以透明方式blit一个图像?_Ios_Image_Metal_Metalkit - Fatal编程技术网

Ios 如何在金属材质的另一个图像上以透明方式blit一个图像?

Ios 如何在金属材质的另一个图像上以透明方式blit一个图像?,ios,image,metal,metalkit,Ios,Image,Metal,Metalkit,每当我尝试blit一个在金属中有透明部分的图像时,透明部分就会被替换为黑色 为了清楚起见,我的图像不包含任何半透明像素。它们要么是纯色的,要么是完全透明的 我有一个简单的ViewController,可以设置Metal并尝试blit 2个图像。但是第二张图片的透明部分是黑色的,并且没有显示下面的第一张图片 - (void)viewDidLoad { [super viewDidLoad]; id <MTLDevice> device = MTLCreateSyste

每当我尝试blit一个在金属中有透明部分的图像时,透明部分就会被替换为黑色

为了清楚起见,我的图像不包含任何半透明像素。它们要么是纯色的,要么是完全透明的

我有一个简单的ViewController,可以设置Metal并尝试blit 2个图像。但是第二张图片的透明部分是黑色的,并且没有显示下面的第一张图片

- (void)viewDidLoad
{
    [super viewDidLoad];

    id <MTLDevice> device = MTLCreateSystemDefaultDevice();
    self.commandQueue = [device newCommandQueue];
    MTKTextureLoader *textureLoader = [[MTKTextureLoader alloc] initWithDevice:device];
    self.texture = [textureLoader newTextureWithCGImage:[UIImage imageNamed:@"image1"].CGImage options:nil error:nil];
    self.texture2 = [textureLoader newTextureWithCGImage:[UIImage imageNamed:@"image2"].CGImage options:nil error:nil];

    self.metalView = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) device:device];
    self.metalView.framebufferOnly = NO;
    self.metalView.delegate = self;
    self.metalView.colorPixelFormat = MTLPixelFormatBGRA8Unorm_sRGB;
    [self.view addSubview:self.metalView];
}

- (void)drawInMTKView:(MTKView *)view
{
    id<MTLCommandBuffer> commandBuffer = [self.commandQueue commandBuffer];
    id<MTLBlitCommandEncoder> blitEncoder = [commandBuffer blitCommandEncoder];

    [blitEncoder copyFromTexture:self.texture sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(self.texture.width, self.texture.height, self.texture.depth) toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0, 0, 0)];
    [blitEncoder copyFromTexture:self.texture2 sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(self.texture2.width, self.texture2.height, self.texture2.depth) toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(20, 20, 0)];
    [blitEncoder endEncoding];

    [commandBuffer presentDrawable:self.metalView.currentDrawable];
    [commandBuffer commit];
}
-(void)viewDidLoad
{
[超级视图下载];
id设备=MTLCreateSystemDefaultDevice();
self.commandQueue=[device newCommandQueue];
MTKTextureLoader*textureLoader=[[MTKTextureLoader alloc]initWithDevice:device];
self.texture=[textureLoader newTextureWithCGImage:[UIImage ImageName:@“image1”]。CGImage选项:nil错误:nil];
self.texture2=[textureLoader newTextureWithCGImage:[UIImage ImageName:@“image2”]。CGImage选项:无错误:无];
self.metalView=[[MTKView alloc]initWithFrame:CGRectMake(0,0,100,100)设备:设备];
self.metalView.framebufferOnly=否;
self.metalView.delegate=self;
self.metalView.colorPixelFormat=MTLPixelFormatBGRA8Unorm\u sRGB;
[self.view addSubview:self.metalView];
}
-(无效)drawInMTKView:(MTKView*)视图
{
id commandBuffer=[self.commandQueue commandBuffer];
id blitEncoder=[commandBuffer blitCommandEncoder];
[blitEncoder copyFromTexture:self.texture sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0)sourceSize:MTLSizeMake(self.texture.width,self.texture.depth)toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(0,0,0)];
[blitEncoder copyFromTexture:self.texture2 sourceSlice:0 sourceLevel:0 sourceOrigin:MTLOriginMake(0,0,0)sourceSize:MTLSizeMake(self.texture2.width,self.texture2.height,self.texture2.depth)toTexture:self.metalView.currentDrawable.texture destinationSlice:0 destinationLevel:0 destinationOrigin:MTLOriginMake(20,0)];
[blitEncoder endEncoding];
[commandBuffer presentDrawable:self.metalView.currentDrawable];
[命令缓冲区提交];
}

任何帮助都将不胜感激

根据定义,布告取代了整个目的地区域。如果要排除第二个纹理的透明像素,则需要使用alpha混合(或通过
discard_fragment
进行alpha测试)或计算着色器进行渲染。感谢您的回复Warren。顺便说一句,我喜欢你的金属教程,它们在学习金属时是无价的。此后,我用一个简单的四边形设置了一个金属场景,通过着色器显示一个图像,并且由于您的实例化教程,我能够使其中几个场景同时显示。当这样做的时候,我能够在管道上设置混合选项,现在透明度工作正常。您认为这是一种足够快的方法,还是使用discard_fragment shader进行咬合会更有效?我认为在这里混合是更好的选择,即使在二进制(alpha=0或alpha=1)情况下也是如此。根据定义,Blitting会替换整个目标区域。如果要排除第二个纹理的透明像素,则需要使用alpha混合(或通过
discard_fragment
进行alpha测试)或计算着色器进行渲染。感谢您的回复Warren。顺便说一句,我喜欢你的金属教程,它们在学习金属时是无价的。此后,我用一个简单的四边形设置了一个金属场景,通过着色器显示一个图像,并且由于您的实例化教程,我能够使其中几个场景同时显示。当这样做的时候,我能够在管道上设置混合选项,现在透明度工作正常。您认为这是一种足够快的方法,还是使用discard_fragment shader进行咬合会更有效?我认为在这里混合是更好的选择,即使在二进制(alpha=0或alpha=1)情况下也是如此。