在使用Apple的纹理导入器或我自己的时候,用软件(带有透明的bg)或Photoshop(保存为PNG)绘制的白色软边圆圈在渲染时会将其半透明颜色替换为黑色.
下面是Xcode的Metal调试器的屏幕抓取,你可以在发送到着色器之前看到纹理.
在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, 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) …Run Code Online (Sandbox Code Playgroud) 无论出于何种原因,我都遇到了金属中 alpha 混合的问题。我正在绘制 MTKView,对于我创建的每个管道,我执行以下操作:
descriptor.colorAttachments[0].blendingEnabled = YES;
descriptor.colorAttachments[0].rgbBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].alphaBlendOperation = MTLBlendOperationAdd;
descriptor.colorAttachments[0].sourceRGBBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorSourceAlpha;
descriptor.colorAttachments[0].destinationRGBBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
descriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorOneMinusSourceAlpha;
Run Code Online (Sandbox Code Playgroud)
但是,无论出于何种原因,这都不会导致 alpha 测试发生。您甚至可以检查帧调试器,您将看到 alpha 为 0 的顶点被绘制为黑色而不是透明。
我的一个想法是,某些几何体最终位于完全相同的 z 平面上,因此如果 alpha 混合在同一 z 平面上不起作用,则可能会导致问题。但我不认为那是一回事。
为什么 alpha 混合不起作用?
我希望像透明玻璃一样混合。这样想。