Jak*_*yer 11 transparency objective-c ios swift metal
在使用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)
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
}
}
Run Code Online (Sandbox Code Playgroud)
但我不认为这是问题所在(因为它是Swift的Apple副本,而且我使用了它们而没有差别).
任何潜在客户都会非常有帮助.
感谢Jessy,我决定研究一下如何混合Alpha,并弄清楚了。我的纹理在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
Run Code Online (Sandbox Code Playgroud)
由于您使用的是CGContext被配置为使用与alpha通道预乘的颜色,采用标准RGB = src.rgb * src.a + dst.rgb * (1-src.a)会造成变暗的区域,因为该src.rgb值预乘用src.a了。这意味着您想要的是src.rgb + dst.rgb * (1-src.a)这样配置的:
pipeline.colorAttachments[0].isBlendingEnabled = true
pipeline.colorAttachments[0].rgbBlendOperation = .add
pipeline.colorAttachments[0].alphaBlendOperation = .add
pipeline.colorAttachments[0].sourceRGBBlendFactor = .one
pipeline.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
pipeline.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipeline.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
Run Code Online (Sandbox Code Playgroud)
该.one办法离开RGB原样。预乘颜色的原因是,您只需在创建时就将颜色乘一次,而不是每次混合时。您的配置也可以间接实现此目的。
| 归档时间: |
|
| 查看次数: |
2645 次 |
| 最近记录: |