如何使用SKKeyframeSequence绘制渐变:根据Apple文档

时间:2022-06-07 11:28:50

The Apple docs on SKKeyframeSequence have brief sample code designed to create a gradient:

SKKeyframeSequence上的Apple文档有简短的示例代码,用于创建渐变:

let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
                                                        SKColor.yellow,
                                                        SKColor.red,
                                                        SKColor.blue],
                                       times: [0, 0.25, 0.5, 1])
colorSequence.interpolationMode = .linear
stride(from: 0, to: 1, by: 0.001).forEach {
    let color = colorSequence.sample(atTime: CGFloat($0)) as! SKColor
}

When combined with a drawing system of some sort, this is said to output this: 如何使用SKKeyframeSequence绘制渐变:根据Apple文档 How can this be drawn from the sampling of the sequence of colours in the demo code?

当与某种绘图系统结合使用时,据说可以输出:如何从演示代码中的颜色序列采样中得出它?

ps I don't have any clue how to draw this with SpriteKit objects, hence the absence of attempted code. I'm not asking for code, just an answer on how to use this 'array' of colours to create a gradient that can be used as a texture in SpriteKit.

ps我没有任何线索如何用SpriteKit对象绘制它,因此没有尝试代码。我不是要求代码,只是回答如何使用这个'数组'颜色来创建一个可以在SpriteKit中用作纹理的渐变。

1 个解决方案

#1


5  

The colors are different for some reason, but here is what I came up with using their source code:

由于某种原因,颜色是不同的,但这是我使用他们的源代码提出的:

PG setup:

import SpriteKit
import PlaygroundSupport

let sceneView = SKView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 1000, height: 450)))
let scene     = SKScene(size: CGSize(width: 1000, height: 450))

LOADSCENE: do {
  scene.backgroundColor = .white
  scene.anchorPoint = CGPoint(x: 0, y: 0.5)
  scene.physicsWorld.gravity = CGVector.zero
  sceneView.presentScene(scene)

  PlaygroundPage.current.liveView = sceneView
}

Solution:

// Utility func:
func drawLine(from point1: CGPoint, to point2: CGPoint, color: SKColor) {
  let linePath = CGMutablePath()
  linePath.move(to: point1)
  linePath.addLine(to: point2)

  let newLine = SKShapeNode(path: linePath)
  newLine.strokeColor = color
  newLine.lineWidth = 1
  newLine.zPosition = 10
  scene.addChild(newLine)
  newLine.position.x = point1.x

}

// Holds our soon-to-be-generated colors:
var colors = [SKColor]()

LOADCOLORS: do {
  let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
                                                          SKColor.yellow,
                                                          SKColor.red,
                                                          SKColor.blue],
                                         times: [0, 0.25, 0.5, 1])

  colorSequence.interpolationMode = .linear
  stride(from: 0, to: 1, by: 0.001).forEach {
    colors.append(colorSequence.sample(atTime: CGFloat($0)) as! SKColor)
  }
}

DRAWGRAD: do {
  for i in 1...999 {
    let p1 = CGPoint(x: CGFloat(i), y: scene.frame.minY)
    let p2 = CGPoint(x: CGFloat(i), y: scene.frame.maxY)
    drawLine(from: p1, to: p2, color: colors[i])
  }

  print("Give me my 25 cookie points, please and TY")
}

如何使用SKKeyframeSequence绘制渐变:根据Apple文档

You should then be able to get this as a texture as such:

然后,您应该能够将其作为纹理:

let texture = sceneView.texture(from: scene)

Rendering this took about a million years to render on my gen2 i5 at 2.6ghz for some reason. Will have to look into that, unless it was just a PG bug...

由于某种原因,渲染这个花了大约一百万年才能在我的gen2 i5上以2.6ghz渲染。将不得不调查,除非它只是一个PG错误...

#1


5  

The colors are different for some reason, but here is what I came up with using their source code:

由于某种原因,颜色是不同的,但这是我使用他们的源代码提出的:

PG setup:

import SpriteKit
import PlaygroundSupport

let sceneView = SKView(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 1000, height: 450)))
let scene     = SKScene(size: CGSize(width: 1000, height: 450))

LOADSCENE: do {
  scene.backgroundColor = .white
  scene.anchorPoint = CGPoint(x: 0, y: 0.5)
  scene.physicsWorld.gravity = CGVector.zero
  sceneView.presentScene(scene)

  PlaygroundPage.current.liveView = sceneView
}

Solution:

// Utility func:
func drawLine(from point1: CGPoint, to point2: CGPoint, color: SKColor) {
  let linePath = CGMutablePath()
  linePath.move(to: point1)
  linePath.addLine(to: point2)

  let newLine = SKShapeNode(path: linePath)
  newLine.strokeColor = color
  newLine.lineWidth = 1
  newLine.zPosition = 10
  scene.addChild(newLine)
  newLine.position.x = point1.x

}

// Holds our soon-to-be-generated colors:
var colors = [SKColor]()

LOADCOLORS: do {
  let colorSequence = SKKeyframeSequence(keyframeValues: [SKColor.green,
                                                          SKColor.yellow,
                                                          SKColor.red,
                                                          SKColor.blue],
                                         times: [0, 0.25, 0.5, 1])

  colorSequence.interpolationMode = .linear
  stride(from: 0, to: 1, by: 0.001).forEach {
    colors.append(colorSequence.sample(atTime: CGFloat($0)) as! SKColor)
  }
}

DRAWGRAD: do {
  for i in 1...999 {
    let p1 = CGPoint(x: CGFloat(i), y: scene.frame.minY)
    let p2 = CGPoint(x: CGFloat(i), y: scene.frame.maxY)
    drawLine(from: p1, to: p2, color: colors[i])
  }

  print("Give me my 25 cookie points, please and TY")
}

如何使用SKKeyframeSequence绘制渐变:根据Apple文档

You should then be able to get this as a texture as such:

然后,您应该能够将其作为纹理:

let texture = sceneView.texture(from: scene)

Rendering this took about a million years to render on my gen2 i5 at 2.6ghz for some reason. Will have to look into that, unless it was just a PG bug...

由于某种原因,渲染这个花了大约一百万年才能在我的gen2 i5上以2.6ghz渲染。将不得不调查,除非它只是一个PG错误...