ios - 内核中的透明图像 Alpha 问题
问题描述
我正在阅读透明纹理,其中 alpha 因位置而异。我正在将其写入内核函数中的另一个透明纹理。类型为 BGRA8。当我写的时候,白色代替了 alpha 为 0 的地方。
我添加了一个条件,例如当我返回源图像 alpha 零时。在那段时间,我在所需的纹理周围有一个白色边框。
怎么解决 ?
可能是什么问题。
kernel void computeTool(constant float4 *color [[buffer(0)]],
constant float2 *point [[buffer(1)]],
constant int &pointCount [[buffer(2)]],
texture2d<float,access::read_write> des [[texture(0)]],
texture2d<float,access::read> src [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
for (int i = 0; i < pointCount; ++i) {
float2 x = touchPointF(point[i]) ;
if(gid.x > x.x && gid.x < x.x + 150 && gid.y > x.y && gid.y < x.y + 150){
float2 ss = float2(gid)- float2(x);
uint2 ssx = uint2(ss);
float4 srcColor = src.read(ssx) * float4(1.0,0.0,0.0,1.0);
des.write(srcColor, gid);
}
}
}
Src 纹理创建
public func imageToTexture(imageNamed: String, device: MTLDevice) -> MTLTexture {
let image = UIImage(named: imageNamed)!
var pixelBuffer: CVPixelBuffer?
let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferMetalCompatibilityKey: kCFBooleanTrue]
_ = UIScreen.main.scale
let width = Int(image.size.width)
let height = Int(image.size.height)
var status = CVPixelBufferCreate(nil, width, height,
kCVPixelFormatType_32BGRA, attrs as CFDictionary,
&pixelBuffer)
assert(status == noErr)
let coreImage = CIImage(image: image)!
let context = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!)
context.render(coreImage, to: pixelBuffer!)
var textureWrapper: CVMetalTexture?
var textureCache:CVMetalTextureCache?
_ = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &textureCache)
status = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
textureCache!, pixelBuffer!, nil, .bgra8Unorm,
CVPixelBufferGetWidth(pixelBuffer!), CVPixelBufferGetHeight(pixelBuffer!),
0,
&textureWrapper)
let texture = CVMetalTextureGetTexture(textureWrapper!)!
// use texture now for your Metal texture. the texture is now map-bound to the CVPixelBuffer's underlying memory.
return texture
}
我正在写作的目的地纹理
let textureDescriptors = MTLTextureDescriptor()
textureDescriptors.textureType = MTLTextureType.type2D
let screenRatio = UIScreen.main.scale
textureDescriptors.width = Int((DrawingManager.shared.size?.width)!) * Int(screenRatio)
textureDescriptors.height = Int((DrawingManager.shared.size?.height)!) * Int(screenRatio)
textureDescriptors.pixelFormat = .bgra8Unorm
textureDescriptors.storageMode = .private
textureDescriptors.usage = [.renderTarget, .shaderRead]
ssTexture = device.makeTexture(descriptor: textureDescriptors)
解决方案
推荐阅读
- ios - 手机首次解锁时如何使 UIDeviceOrientation 自动变为纵向?
- apache-spark - Spark 上的 Hive:资源不足
- python - 如何复制一行 QtableWidget 并插入到它下面的行?
- reactjs - 如何自动将选择范围扩展到 Draft.js 中的整个链接文本?
- android - Kotlin:如何执行异步函数并等待它完成?
- mechanicalturk - 确定完成 HIT 的工作人员的语言环境
- c# - 如何在 Unity 3D 中暂时使精灵消失?
- ios - 将 Facebook SDK 更新至 0.7.0 - 现在它无法识别 AppDelegate 或 LinkShareContent 中的 SDKApplicationDelegate?
- javascript - Toggle Class on Button click for multiple elements
- python - 带有返回函数的多处理 for 循环,该函数接受多个参数