reze-engine 0.2.18 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Bezier interpolation for VMD animations
3
+ * Based on the reference implementation from babylon-mmd
4
+ */
5
+
6
+ /**
7
+ * Bezier interpolation function
8
+ * @param x1 First control point X (0-127, normalized to 0-1)
9
+ * @param x2 Second control point X (0-127, normalized to 0-1)
10
+ * @param y1 First control point Y (0-127, normalized to 0-1)
11
+ * @param y2 Second control point Y (0-127, normalized to 0-1)
12
+ * @param t Interpolation parameter (0-1)
13
+ * @returns Interpolated value (0-1)
14
+ */
15
+ export function bezierInterpolate(x1: number, x2: number, y1: number, y2: number, t: number): number {
16
+ // Clamp t to [0, 1]
17
+ t = Math.max(0, Math.min(1, t))
18
+
19
+ // Binary search for the t value that gives us the desired x
20
+ // We're solving for t in the Bezier curve: x(t) = 3*(1-t)^2*t*x1 + 3*(1-t)*t^2*x2 + t^3
21
+ let start = 0
22
+ let end = 1
23
+ let mid = 0.5
24
+
25
+ // Iterate until we find the t value that gives us the desired x
26
+ for (let i = 0; i < 15; i++) {
27
+ // Evaluate Bezier curve at mid point
28
+ const x = 3 * (1 - mid) * (1 - mid) * mid * x1 + 3 * (1 - mid) * mid * mid * x2 + mid * mid * mid
29
+
30
+ if (Math.abs(x - t) < 0.0001) {
31
+ break
32
+ }
33
+
34
+ if (x < t) {
35
+ start = mid
36
+ } else {
37
+ end = mid
38
+ }
39
+
40
+ mid = (start + end) / 2
41
+ }
42
+
43
+ // Now evaluate the y value at this t
44
+ const y = 3 * (1 - mid) * (1 - mid) * mid * y1 + 3 * (1 - mid) * mid * mid * y2 + mid * mid * mid
45
+
46
+ return y
47
+ }