从 SKTexture(data:size:) 创建的 SKSpriteNode 存在 Alpha(不透明度)问题
SKSpriteNode created from SKTexture(data:size:) issues with Alpha (Opacity)
我在从 pixelData 创建 SKSpriteNode 时遇到问题。
下面是我的代码。
var pixelData: [UInt32] = [UInt32](count: 256, repeatedValue: 0xff0000ff)
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0xff00ff00))
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0xffff0000))
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0x000000ff))
let data = NSData(bytes: pixelData, length: pixelData.count * sizeof(UInt32))
let texture = SKTexture(data: data, size: CGSize(width: 32, height: 32))
let node = SKSpriteNode(texture: texture, size: CGSize(width: 32, height: 32))
node.position.x = self.frame.midX
node.position.y = self.frame.midY
self.addChild(node)
底部三行(即 pixelData 数组的前 3 部分,0xff0000ff
、0xff00ff00
、0xffff0000
:红色、绿色和蓝色).
但是,我希望顶行是完全透明的:0x000000ff
,这应该表示 rgba(1, 0, 0, 0)。但它显示为:
我不知道为什么会这样。我对 pixelData 的理解有误吗?还是SpriteKit的bug什么的?
非常感谢您的帮助!
来自将像素数据转换为 SKTexture
、
的便捷方法的文档
The color components should have been already multiplied by the alpha value.
因此,值 0x000000ff
未定义,因为如果 alpha 值为 0,则红色分量不能 0xff
。此外,用十六进制值,尤其是当组件顺序相反时。
或者,您可以定义一个数据结构来表示一种颜色,然后在初始化期间将每个颜色分量预乘以 alpha 值:
struct Color {
var red:UInt8
var green:UInt8
var blue:UInt8
var alpha:UInt8
// You can omit the parameters that have default values.
init(red:UInt8=0, green:UInt8=0, blue:UInt8=0, alpha:UInt8=UInt8.max) {
let alphaValue:Float = Float(alpha) / 255
self.red = UInt8(round(Float(red) * alphaValue))
self.green = UInt8(round(Float(blue) * alphaValue))
self.blue = UInt8(round(Float(green) * alphaValue))
self.alpha = alpha
}
}
然后您可以使用 "same" 值作为您的代码定义和初始化 pixelData
数组
var pixelData:[Color] = [Color](count: 256, repeatedValue: Color(red: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(green: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(blue: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(red: UInt8.max, alpha: 0))
并使用
创建 NSData
对象
let data = NSData(bytes: pixelData, length: pixelData.count * sizeof(Color))
我在从 pixelData 创建 SKSpriteNode 时遇到问题。
下面是我的代码。
var pixelData: [UInt32] = [UInt32](count: 256, repeatedValue: 0xff0000ff)
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0xff00ff00))
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0xffff0000))
pixelData.appendContentsOf([UInt32](count: 256, repeatedValue: 0x000000ff))
let data = NSData(bytes: pixelData, length: pixelData.count * sizeof(UInt32))
let texture = SKTexture(data: data, size: CGSize(width: 32, height: 32))
let node = SKSpriteNode(texture: texture, size: CGSize(width: 32, height: 32))
node.position.x = self.frame.midX
node.position.y = self.frame.midY
self.addChild(node)
底部三行(即 pixelData 数组的前 3 部分,0xff0000ff
、0xff00ff00
、0xffff0000
:红色、绿色和蓝色).
但是,我希望顶行是完全透明的:0x000000ff
,这应该表示 rgba(1, 0, 0, 0)。但它显示为:
我不知道为什么会这样。我对 pixelData 的理解有误吗?还是SpriteKit的bug什么的?
非常感谢您的帮助!
来自将像素数据转换为 SKTexture
、
The color components should have been already multiplied by the alpha value.
因此,值 0x000000ff
未定义,因为如果 alpha 值为 0,则红色分量不能 0xff
。此外,用十六进制值,尤其是当组件顺序相反时。
或者,您可以定义一个数据结构来表示一种颜色,然后在初始化期间将每个颜色分量预乘以 alpha 值:
struct Color {
var red:UInt8
var green:UInt8
var blue:UInt8
var alpha:UInt8
// You can omit the parameters that have default values.
init(red:UInt8=0, green:UInt8=0, blue:UInt8=0, alpha:UInt8=UInt8.max) {
let alphaValue:Float = Float(alpha) / 255
self.red = UInt8(round(Float(red) * alphaValue))
self.green = UInt8(round(Float(blue) * alphaValue))
self.blue = UInt8(round(Float(green) * alphaValue))
self.alpha = alpha
}
}
然后您可以使用 "same" 值作为您的代码定义和初始化 pixelData
数组
var pixelData:[Color] = [Color](count: 256, repeatedValue: Color(red: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(green: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(blue: UInt8.max))
pixelData += [Color](count:256, repeatedValue: Color(red: UInt8.max, alpha: 0))
并使用
创建NSData
对象
let data = NSData(bytes: pixelData, length: pixelData.count * sizeof(Color))