我正在使用自定义片段着色器沿SKShapeNode
的圆形路径创建颜色渐变。结果如下:
如何在SpriteKit或自定义着色器中消除锯齿?
P.S。我希望它也可以在watchOS上运行。
片段着色器:
void main() {
float normalizedPosition = v_path_distance / u_path_length;
gl_FragColor = vec4(normalizedPosition, normalizedPosition, normalizedPosition, 1.0);
}
ViewController:
import UIKit
import SpriteKit
class ViewController: UIViewController {
var skView: SKView {
return view as! SKView
}
var scene: SKScene!
override func viewDidLoad() {
super.viewDidLoad()
scene = SKScene(size: view.frame.size)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
skView.presentScene(scene)
// Create node
let ringNode = SKShapeNode(circleOfRadius: scene.size.width / 4)
ringNode.position = CGPoint(x: scene.frame.midX, y: scene.frame.midY)
ringNode.lineWidth = 30
// Apply shader
ringNode.strokeShader = SKShader(fileNamed: "fragment.fsh")
// Add node to scene
scene.addChild(ringNode)
}
}