微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 当Alpha值不是1或0时,Metal MTLTexture用黑色替换半透明区域

在使用 Apple’s texture importer或我自己的时候,在软件中绘制的白色软边圆圈(带有透明的bg)或者在Photoshop中(保存为PNG)在渲染时会将其半透明颜色替换为黑色.

下面是Xcode的Metal调试器的屏幕抓取,你可以在发送到着色器之前看到纹理.

Image located here (I’m not high ranked enough to embed)

在Xcode,finder中,当放入UIImageView时,源纹理没有环.但是在UIImage的某个地方 – > CGContex – > MTLTexture过程(我特别考虑MTLTexture部分)透明部分变暗.

在过去的几天里,我一直在撞墙,改变我所能做的一切,但我无法理解.

为了透明(ha),这是我的个人导入代码

import UIKit
import CoreGraphics

class MetalTexture {

    class func imagetoTexture(imageNamed: String,device: MTLDevice) -> MTLTexture {
        let bytesPerPixel = 4
        let bitsPerComponent = 8

        var image = UIImage(named: imageNamed)!

        let width = Int(image.size.width)
        let height = Int(image.size.height)
        let bounds = CGRectMake(0,CGFloat(width),CGFloat(height))

        var rowBytes = width * bytesPerPixel
        var colorSpace = CGColorSpaceCreateDeviceRGB()

        let context = CGBitmapContextCreate(nil,width,height,bitsPerComponent,rowBytes,colorSpace,CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue))

        CGContextClearRect(context,bounds)
        CGContextTranslateCTM(context,CGFloat(height))
        CGContextScaleCTM(context,-1.0,-1.0)
        CGContextDrawImage(context,bounds,image.CGImage)

        var texDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.RGBA8Unorm,width: width,height: height,mipmapped: false)

        var texture = device.newTextureWithDescriptor(texDescriptor)
        texture.label = imageNamed

        var pixelsData = CGBitmapContextGetData(context)

        var region = MTLRegionMake2D(0,height)
        texture.replaceRegion(region,mipmapLevel: 0,withBytes: pixelsData,bytesPerRow: rowBytes)

        return texture
    }
}

但我不认为这是问题所在(因为它是Swift中Apple的副本,而我使用了它们而没有差异).

任何线索都会非常有用.

解决方法

感谢Jessy,我决定看看我是如何混合我的alphas并且我已经弄清楚了.我的纹理在GPU调试器中仍然看起来很暗,但在实际应用中,一切看起来都是正确的.我对管道状态描述符进行了更改,您可以在下面看到.
pipelinestateDescriptor.colorAttachments[0].blendingEnabled = true
pipelinestateDescriptor.colorAttachments[0].rgbBlendOperation = .Add
pipelinestateDescriptor.colorAttachments[0].alphaBlendOperation = .Add
pipelinestateDescriptor.colorAttachments[0].sourceRGBBlendFactor = .DestinationAlpha
pipelinestateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .DestinationAlpha
pipelinestateDescriptor.colorAttachments[0].destinationRGBBlendFactor = .OneMinusSourceAlpha
pipelinestateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .OneMinusBlendAlpha

原文地址:https://www.jb51.cc/iOS/334046.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐