@energy8platform/golem 0.7.0 → 0.8.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.
@@ -236,7 +236,7 @@ var Key = z.object({
236
236
  /** Optional independent easing for numeric-array components (for example Spine RGBA curves). */
237
237
  componentEase: z.array(Ease).min(1).optional()
238
238
  });
239
- var Interpolation = { interpolation: z.enum(["segment", "pchip"]).optional() };
239
+ var Interpolation = { interpolation: z.enum(["segment", "pchip", "spline"]).optional() };
240
240
  var Track = z.discriminatedUnion("target", [
241
241
  z.object({ target: z.literal("control"), id: Id, prop: z.literal("value"), ...Interpolation, keys: z.array(Key).min(1) }),
242
242
  z.object({ target: z.literal("bone"), id: Id, prop: BoneProp, ...Interpolation, keys: z.array(Key).min(1) }),
@@ -657,6 +657,57 @@ function smoothValue(track, i, t, component, loop) {
657
657
  const u = (t - k[i].t) / h(i), u2 = u * u, u3 = u2 * u;
658
658
  return (2 * u3 - 3 * u2 + 1) * v(i) + (u3 - 2 * u2 + u) * h(i) * tangent(i) + (-2 * u3 + 3 * u2) * v(i + 1) + (u3 - u2) * h(i) * tangent(i + 1);
659
659
  }
660
+ function tridiagonal(a, b, c, r) {
661
+ const n = b.length, cp = [], x = [];
662
+ let beta = b[0];
663
+ x[0] = r[0] / beta;
664
+ for (let i = 1; i < n; i++) {
665
+ cp[i] = c[i - 1] / beta;
666
+ beta = b[i] - a[i] * cp[i];
667
+ x[i] = (r[i] - a[i] * x[i - 1]) / beta;
668
+ }
669
+ for (let i = n - 2; i >= 0; i--) x[i] -= cp[i + 1] * x[i + 1];
670
+ return x;
671
+ }
672
+ function splineMoments(t, v, periodic) {
673
+ const n = t.length, h = (j) => t[j + 1] - t[j], d = (j) => (v[j + 1] - v[j]) / h(j);
674
+ if (!periodic) {
675
+ const a2 = [], b2 = [], c2 = [], r2 = [];
676
+ for (let i = 0; i < n; i++) {
677
+ const hp = i > 0 ? h(i - 1) : 0, hn = i < n - 1 ? h(i) : 0;
678
+ a2.push(hp);
679
+ b2.push(2 * (hp + hn));
680
+ c2.push(hn);
681
+ r2.push(6 * ((i < n - 1 ? d(i) : 0) - (i > 0 ? d(i - 1) : 0)));
682
+ }
683
+ return tridiagonal(a2, b2, c2, r2);
684
+ }
685
+ const m = n - 1, a = [], b = [], c = [], r = [];
686
+ for (let i = 0; i < m; i++) {
687
+ const p = (i + m - 1) % m;
688
+ a.push(h(p));
689
+ b.push(2 * (h(p) + h(i)));
690
+ c.push(h(i));
691
+ r.push(6 * (d(i) - d(p)));
692
+ }
693
+ const top = a[0], bottom = c[m - 1], gamma = -b[0];
694
+ const bb = [...b];
695
+ bb[0] -= gamma;
696
+ bb[m - 1] -= bottom * top / gamma;
697
+ const x = tridiagonal(a, bb, c, r), u = new Array(m).fill(0);
698
+ u[0] = gamma;
699
+ u[m - 1] = bottom;
700
+ const z2 = tridiagonal(a, bb, c, u);
701
+ const f = (x[0] + top * x[m - 1] / gamma) / (1 + z2[0] + top * z2[m - 1] / gamma);
702
+ const M = x.map((xi, i) => xi - f * z2[i]);
703
+ return [...M, M[0]];
704
+ }
705
+ function splineValue(track, i, t, component, loop) {
706
+ const k = track.keys, ts = k.map((x) => x.t), vs = k.map((x) => component === void 0 ? x.v : x.v[component]);
707
+ const M = splineMoments(ts, vs, loop && k.length > 3 && vs[0] === vs[k.length - 1]);
708
+ const H = ts[i + 1] - ts[i], A = (ts[i + 1] - t) / H, B = (t - ts[i]) / H;
709
+ return A * vs[i] + B * vs[i + 1] + ((A ** 3 - A) * M[i] + (B ** 3 - B) * M[i + 1]) * H * H / 6;
710
+ }
660
711
  function sample(track, t, loop = false) {
661
712
  const keys = track.keys;
662
713
  if (t <= keys[0].t) return keys[0].v;
@@ -668,11 +719,13 @@ function sample(track, t, loop = false) {
668
719
  if (a.ease === "step") return a.v;
669
720
  if (typeof a.v === "number" && typeof b.v === "number") {
670
721
  if (track.interpolation === "pchip") return smoothValue(track, i, t, void 0, loop);
722
+ if (track.interpolation === "spline") return splineValue(track, i, t, void 0, loop);
671
723
  const u = ease(a.ease)((t - a.t) / (b.t - a.t));
672
724
  return a.v + (b.v - a.v) * u;
673
725
  }
674
726
  if (Array.isArray(a.v) && Array.isArray(b.v) && typeof a.v[0] === "number") {
675
727
  if (track.interpolation === "pchip") return a.v.map((_, j) => smoothValue(track, i, t, j, loop));
728
+ if (track.interpolation === "spline") return a.v.map((_, j) => splineValue(track, i, t, j, loop));
676
729
  const phase = (t - a.t) / (b.t - a.t), av = a.v, bv = b.v;
677
730
  return av.map((v, k) => v + ((bv[k] ?? v) - v) * ease(a.componentEase?.[k] ?? a.ease)(phase));
678
731
  }
@@ -1923,6 +1976,7 @@ function createGolemNode(ticker) {
1923
1976
  ticker.remove(s.tick);
1924
1977
  s.player.stop();
1925
1978
  s.player.view.destroy({ children: true });
1979
+ view.boundsArea = null;
1926
1980
  };
1927
1981
  const animationOf = (doc, wanted) => wanted && doc.animations.some((a) => a.id === wanted) ? wanted : doc.animations[0]?.id;
1928
1982
  const node = {
@@ -1953,6 +2007,7 @@ function createGolemNode(ticker) {
1953
2007
  const player = new RigPlayer(got.doc, got.textures);
1954
2008
  player.view.position.set(-got.doc.meta.width / 2, -got.doc.meta.height / 2);
1955
2009
  view.addChild(player.view);
2010
+ view.boundsArea = view.getLocalBounds().rectangle.clone();
1956
2011
  const state = { rig: got, player, speed: 1, controls: "{}", tick: (t) => player.tick(t.deltaMS / 1e3 * state.speed) };
1957
2012
  ticker.add(state.tick);
1958
2013
  live.set(view, s = state);