在使用Apple 或我自己的纹理导入器时,在软件(带有透明背景)或 Photoshop(另存为 PNG)中绘制的白色软边圆在渲染时将其半透明颜色替换为黑色.
下面是 Xcode 的 Metal 调试器的屏幕截图,您可以在发送到着色器之前看到纹理。
在 Xcode、finder 中,当放入 UIImageView 时,源纹理没有环。但是在 UIImage -> CGContex -> MTLTexture 过程中的某个地方(我特别想的是 MTLTexture 部分)透明部分变暗了。
在过去的几天里,我一直在努力改变一切,但我无法弄清楚。
为了透明(哈),这是我个人的导入代码
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, 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(width), 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, 0, width, height)
texture.replaceRegion(region, mipmapLevel: 0, withBytes: pixelsData, bytesPerRow: rowBytes)
return texture
}
}
但我不认为这是问题所在(因为它是 Apple 在 Swift 中的副本,而我使用的是他们的,没有任何区别)。
任何线索都会非常有帮助。