在使用苹果纹理导入器或其他导入器时,一个在软件中绘制的带有白色柔边圆圈(透明背景)或在Photoshop中保存为PNG格式的图像,当被渲染后,它的半透明颜色在Metal中会被替换为黑色。
下面是从Xcode的Metal调试器中截图,你可以看到发送到着色器之前的纹理。 图片位于此处(我没有足够高的排名来嵌入) 在Xcode、Finder和UIImageView中,源纹理没有圆环。但是在UIImage -> CGContext -> MTLTexture的过程中(我特别想到MTLTexture部分),透明区域会变暗。
我已经改变了所有能改变的东西几天了,但是还是无法解决问题。
为了更加透明,请看一下我的个人导入代码。
下面是从Xcode的Metal调试器中截图,你可以看到发送到着色器之前的纹理。 图片位于此处(我没有足够高的排名来嵌入) 在Xcode、Finder和UIImageView中,源纹理没有圆环。但是在UIImage -> CGContext -> 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
}
}
但我认为这不是问题所在(因为它是用Swift复制的苹果代码,而且我已经用他们的代码代替了,没有任何区别)。
任何线索都将非常有帮助。