Last active
August 31, 2019 11:49
-
-
Save kubadlo/19585cd35d8468283b89 to your computer and use it in GitHub Desktop.
SmoothStep implementation in Apple Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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