-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
step.ts
55 lines (51 loc) · 1.29 KB
/
step.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { FnN2, FnN3 } from "@thi.ng/api";
import { clamp01 } from "./interval.js";
/**
* Step/threshold function.
*
* @param edge - threshold
* @param x - test value
* @returns 0, if `x < e`, else 1
*/
export const step: FnN2 = (edge, x) => (x < edge ? 0 : 1);
/**
* GLSL-style smoothStep threshold function.
*
* @param edge - lower threshold
* @param edge2 - upper threshold
* @param x - test value
* @returns 0, if `x < edge1`, 1 if `x > edge2`, else S-curve polynomial interpolation
*/
export const smoothStep: FnN3 = (edge, edge2, x) => {
x = clamp01((x - edge) / (edge2 - edge));
return (3 - 2 * x) * x * x;
};
/**
* Similar to {@link smoothStep} but using different, higher degree polynomial.
*
* @param edge -
* @param edge2 -
* @param x -
*/
export const smootherStep: FnN3 = (edge, edge2, x) => {
x = clamp01((x - edge) / (edge2 - edge));
return x * x * x * (x * (x * 6 - 15) + 10);
};
/**
* Exponential ramp with variable shape
*
* @remarks
* Example configurations:
*
* - S-curve: k=8, n=4
* - Step near 1.0: k=8, n=20
* - Pulse: k=0.005, n=-10
* - Ease-in: k=0.5, n=0.25
*
* Interactive graph: https://www.desmos.com/calculator/gcnuyppycz
*
* @param k -
* @param n -
* @param x -
*/
export const expStep: FnN3 = (k, n, x) => 1 - Math.exp(-k * Math.pow(x, n));