Skip to content

Instantly share code, notes, and snippets.

@kubadlo
Last active August 31, 2019 11:49
Show Gist options
  • Save kubadlo/19585cd35d8468283b89 to your computer and use it in GitHub Desktop.
Save kubadlo/19585cd35d8468283b89 to your computer and use it in GitHub Desktop.
SmoothStep implementation in Apple Swift
// SmoothStep implementation in Apple Swift
// Based on Wikipedia page https://en.wikipedia.org/wiki/Smoothstep
import Foundation
// Returns the input value clamped to the lower and higher limits
func clamp<T: Comparable>(value: T, low: T, high: T) -> T {
return min(max(value, low), high)
}
// function(t) = 3t^2 - 2t^2
func smoothStep(edge0: Float, edge1: Float, x: Float) -> Float {
let val = clamp((x - edge0) / (edge1 - edge0), low: 0.0, high: 1.0)
return val * val * (3 - 2 * val)
}
// function(t) = 6t^5 - 15t^4 + 10t^3
func smootherStep(edge0: Float, edge1: Float, x: Float) -> Float {
let val = clamp((x - edge0) / (edge1 - edge0), low: 0.0, high: 1.0)
return val * val * val * (val * (val * 6 - 15) + 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment