|
| 1 | +/** |
| 2 | + * Spherical-quadrangle (squad) quaternion interpolation — Shoemake's C1 |
| 3 | + * quaternion spline. Given a keyframe and its two neighbors, `squadControl` |
| 4 | + * derives the intermediate control quaternion; `squad` blends one segment. |
| 5 | + * |
| 6 | + * All functions return NEW quaternions (or write into a caller `out`); the |
| 7 | + * shared keyframe quaternions are never mutated. |
| 8 | + */ |
| 9 | + |
| 10 | +import * as THREE from "three"; |
| 11 | + |
| 12 | +/** Ensure `b` is in the same hemisphere as `a` (shortest-path continuity). */ |
| 13 | +function alignHemisphere(a: THREE.Quaternion, b: THREE.Quaternion): THREE.Quaternion { |
| 14 | + const out = b.clone(); |
| 15 | + if (a.dot(out) < 0) out.set(-out.x, -out.y, -out.z, -out.w); |
| 16 | + return out; |
| 17 | +} |
| 18 | + |
| 19 | +/** q^-1 for a UNIT quaternion is its conjugate. */ |
| 20 | +function conjugate(q: THREE.Quaternion): THREE.Quaternion { |
| 21 | + return new THREE.Quaternion(-q.x, -q.y, -q.z, q.w); |
| 22 | +} |
| 23 | + |
| 24 | +/** Natural log of a unit quaternion → a pure quaternion (w = 0). */ |
| 25 | +function logUnit(q: THREE.Quaternion): THREE.Quaternion { |
| 26 | + const v = new THREE.Vector3(q.x, q.y, q.z); |
| 27 | + const vLen = v.length(); |
| 28 | + const w = THREE.MathUtils.clamp(q.w, -1, 1); |
| 29 | + if (vLen < 1e-8) return new THREE.Quaternion(0, 0, 0, 0); |
| 30 | + const theta = Math.atan2(vLen, w); |
| 31 | + const k = theta / vLen; |
| 32 | + return new THREE.Quaternion(v.x * k, v.y * k, v.z * k, 0); |
| 33 | +} |
| 34 | + |
| 35 | +/** Exp of a pure quaternion (w = 0) → a unit quaternion. */ |
| 36 | +function expPure(q: THREE.Quaternion): THREE.Quaternion { |
| 37 | + const v = new THREE.Vector3(q.x, q.y, q.z); |
| 38 | + const theta = v.length(); |
| 39 | + if (theta < 1e-8) return new THREE.Quaternion(0, 0, 0, 1); |
| 40 | + const s = Math.sin(theta) / theta; |
| 41 | + return new THREE.Quaternion(v.x * s, v.y * s, v.z * s, Math.cos(theta)); |
| 42 | +} |
| 43 | + |
| 44 | +function mul(a: THREE.Quaternion, b: THREE.Quaternion): THREE.Quaternion { |
| 45 | + return a.clone().multiply(b); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Shoemake control quaternion for `cur`: |
| 50 | + * s = cur * exp( -( log(cur^-1 * next) + log(cur^-1 * prev) ) / 4 ) |
| 51 | + * Neighbors are hemisphere-aligned to `cur` first for shortest-path continuity. |
| 52 | + */ |
| 53 | +export function squadControl( |
| 54 | + prev: THREE.Quaternion, |
| 55 | + cur: THREE.Quaternion, |
| 56 | + next: THREE.Quaternion, |
| 57 | +): THREE.Quaternion { |
| 58 | + const p = alignHemisphere(cur, prev); |
| 59 | + const n = alignHemisphere(cur, next); |
| 60 | + const inv = conjugate(cur); |
| 61 | + const logNext = logUnit(mul(inv, n)); |
| 62 | + const logPrev = logUnit(mul(inv, p)); |
| 63 | + const sum = new THREE.Quaternion( |
| 64 | + -(logNext.x + logPrev.x) / 4, |
| 65 | + -(logNext.y + logPrev.y) / 4, |
| 66 | + -(logNext.z + logPrev.z) / 4, |
| 67 | + 0, |
| 68 | + ); |
| 69 | + return mul(cur, expPure(sum)).normalize(); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Squad blend of one segment: slerp(slerp(q0,q1,t), slerp(s0,s1,t), 2t(1-t)). |
| 74 | + * Endpoints `q0`,`q1`; their controls `s0`,`s1`. Returns q0 at t=0, q1 at t=1. |
| 75 | + */ |
| 76 | +export function squad( |
| 77 | + q0: THREE.Quaternion, |
| 78 | + s0: THREE.Quaternion, |
| 79 | + s1: THREE.Quaternion, |
| 80 | + q1: THREE.Quaternion, |
| 81 | + t: number, |
| 82 | + out: THREE.Quaternion = new THREE.Quaternion(), |
| 83 | +): THREE.Quaternion { |
| 84 | + const q1a = alignHemisphere(q0, q1); |
| 85 | + const a = new THREE.Quaternion().slerpQuaternions(q0, q1a, t); |
| 86 | + const b = new THREE.Quaternion().slerpQuaternions(s0, alignHemisphere(s0, s1), t); |
| 87 | + return out.slerpQuaternions(a, alignHemisphere(a, b), 2 * t * (1 - t)); |
| 88 | +} |
0 commit comments