Skip to content

Instantly share code, notes, and snippets.

@menangen
Created May 25, 2019 19:09
Show Gist options
  • Save menangen/55f291f2c55fbe8661e7d642a248e895 to your computer and use it in GitHub Desktop.
Save menangen/55f291f2c55fbe8661e7d642a248e895 to your computer and use it in GitHub Desktop.
GKRidgedNoiseSource bug in Apple SpriteKit
import PlaygroundSupport
import Cocoa
import SpriteKit
import GameplayKit
class GameScene: SKScene {
let spriteSize: CGFloat = 128
let mapSize = double2(2, 2)
func getMapTexture(_ source: GKCoherentNoiseSource) -> SKTexture
{
let noise = GKNoise(source)
let noiseMap = GKNoiseMap(noise,
size: mapSize,
origin: double2(0.0, 0.0),
sampleCount: int2(128, 128),
seamless: false)
return SKTexture(noiseMap: noiseMap)
}
func showSpriteNoise(_ source: GKCoherentNoiseSource, _ freq: Double) {
let isRidgedNoise: Bool = source is GKRidgedNoiseSource
let noiseTexture = getMapTexture(source)
let spriteNode = SKSpriteNode(
texture: noiseTexture,
size: CGSize(width: spriteSize, height: spriteSize))
// shift to Right
let xShift: CGFloat = freq == 2.0 ? 128 : 0
// shift to Up
let yShift: CGFloat = isRidgedNoise ? 128 : 0
spriteNode.position = CGPoint(
x: spriteSize / 2.0 + xShift,
y: spriteSize / 2.0 + yShift)
addChild(spriteNode)
}
func showPerlinSprites(freqList: [ Double ]) {
for freq in freqList {
let perlinNoise = GKPerlinNoiseSource(
frequency: freq,
octaveCount: 6,
persistence: 0.75,
lacunarity: 2.0,
seed: 123)
showSpriteNoise(perlinNoise, freq)
let bugRidgedNoise = GKRidgedNoiseSource(
frequency: freq,
octaveCount: 4,
lacunarity: 2.0,
seed: 111)
showSpriteNoise(bugRidgedNoise, freq)
}
}
override func didMove(to view: SKView) {
showPerlinSprites(freqList: [1.0, 2.0])
}
}
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 256, height: 256))
for freq in 1...2 {
let labelF = NSTextField()
labelF.frame = CGRect(origin: CGPoint(x: freq == 2 ? 128 : 0, y: 256 - 18), size: CGSize(width: 100, height: 10))
labelF.stringValue = freq == 2 ? "2ϝ" : "ϝ"
labelF.font = NSFont(name: "Menlo", size: 12)
labelF.backgroundColor = .black
labelF.sizeToFit()
sceneView.addSubview(labelF)
}
let scene = GameScene(size: sceneView.frame.size)
scene.scaleMode = .fill
sceneView.presentScene(scene)
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment