这个 SKShapeNode 渐变着色代码有什么问题?
What's wrong with this SKShapeNode gradient coloring code?
我正在尝试创建渐变色 SKShapeNode
。
我从 theGoogle 中找到了下面的代码,但它仅在 color1 为黑色时有效,例如定义如下:
CIColor.init(red: 0, green: 0, blue: 0, alpha: 1.0)
第二种颜色可以是任何颜色。
enum GradientDirection {
case up
case left
case upLeft
case upRight
}
extension SKTexture {
convenience init(size:CGSize,color1:CIColor,color2:CIColor,direction:GradientDirection = .up) {
let coreImageContext = CIContext(options: nil)
let gradientFilter = CIFilter(name: "CILinearGradient")
gradientFilter!.setDefaults()
var startVector:CIVector
var endVector:CIVector
switch direction {
case .up:
startVector = CIVector(x: size.width/2, y: 0)
endVector = CIVector(x: size.width/2, y: size.height)
case .left:
startVector = CIVector(x: size.width, y: size.height/2)
endVector = CIVector(x: 0, y: size.height/2)
case .upLeft:
startVector = CIVector(x: size.width, y: 0)
endVector = CIVector(x: 0, y: size.height)
case .upRight:
startVector = CIVector(x: 0, y: 0)
endVector = CIVector(x: size.width, y: size.height)
}
gradientFilter!.setValue(startVector, forKey: "inputPoint0")
gradientFilter!.setValue(endVector, forKey: "inputPoint1")
gradientFilter!.setValue(color1, forKey: "inputColor0")
gradientFilter!.setValue(color2, forKey: "inputColor1")
let cgimg = coreImageContext.createCGImage(gradientFilter!.outputImage!, fromRect: CGRect(x: 0, y: 0, width: size.width, height: size.height))
self.init(CGImage:cgimg)
}
}
我希望颜色不是黑色。我不知道这段代码有什么问题。我定义 SKShapeNode
如下:
let textureSize = CGSize(width: shapeNode.frame.width, height: shapeNode.frame.height)
let bottomColor = CIColor.init(red: 0, green: 0, blue: 0, alpha: 1.0)
let topColor = CIColor.init(red: 225, green: 255, blue: 255, alpha: 1.0)
let shapeTexture = SKTexture(size:textureSize, color1:bottomColor, color2:topColor, direction:GradientDirection.up)
shapeNode.fillTexture = shapeTexture
self.addChild(shapeNode)
感谢所有帮助!
CIColor 的 RGB 值应在 [0, 1] 中。 255 不是有效值。
我正在尝试创建渐变色 SKShapeNode
。
我从 theGoogle 中找到了下面的代码,但它仅在 color1 为黑色时有效,例如定义如下:
CIColor.init(red: 0, green: 0, blue: 0, alpha: 1.0)
第二种颜色可以是任何颜色。
enum GradientDirection {
case up
case left
case upLeft
case upRight
}
extension SKTexture {
convenience init(size:CGSize,color1:CIColor,color2:CIColor,direction:GradientDirection = .up) {
let coreImageContext = CIContext(options: nil)
let gradientFilter = CIFilter(name: "CILinearGradient")
gradientFilter!.setDefaults()
var startVector:CIVector
var endVector:CIVector
switch direction {
case .up:
startVector = CIVector(x: size.width/2, y: 0)
endVector = CIVector(x: size.width/2, y: size.height)
case .left:
startVector = CIVector(x: size.width, y: size.height/2)
endVector = CIVector(x: 0, y: size.height/2)
case .upLeft:
startVector = CIVector(x: size.width, y: 0)
endVector = CIVector(x: 0, y: size.height)
case .upRight:
startVector = CIVector(x: 0, y: 0)
endVector = CIVector(x: size.width, y: size.height)
}
gradientFilter!.setValue(startVector, forKey: "inputPoint0")
gradientFilter!.setValue(endVector, forKey: "inputPoint1")
gradientFilter!.setValue(color1, forKey: "inputColor0")
gradientFilter!.setValue(color2, forKey: "inputColor1")
let cgimg = coreImageContext.createCGImage(gradientFilter!.outputImage!, fromRect: CGRect(x: 0, y: 0, width: size.width, height: size.height))
self.init(CGImage:cgimg)
}
}
我希望颜色不是黑色。我不知道这段代码有什么问题。我定义 SKShapeNode
如下:
let textureSize = CGSize(width: shapeNode.frame.width, height: shapeNode.frame.height)
let bottomColor = CIColor.init(red: 0, green: 0, blue: 0, alpha: 1.0)
let topColor = CIColor.init(red: 225, green: 255, blue: 255, alpha: 1.0)
let shapeTexture = SKTexture(size:textureSize, color1:bottomColor, color2:topColor, direction:GradientDirection.up)
shapeNode.fillTexture = shapeTexture
self.addChild(shapeNode)
感谢所有帮助!
CIColor 的 RGB 值应在 [0, 1] 中。 255 不是有效值。