gr2: align curve sampling with Granny 2.11 (loop window, cubic, degree 0)

- Curve::eval rebuilds the B-spline window like ConstructBSplineBuffers:
  clamped ends repeat the endpoint; underflow/overflow looping takes knots
  from the other end (+-duration, skipping the duplicated last knot); equal
  trailing knots get +1e-5.
- Degree 3 (CubicCoefficients) supported; degrees 0..3 share one path.
- Degree 0 takes the control of the first knot > t (SampleBSpline0 = pi[0]);
  DaKeyframes32f curves are marked keyframed and keep frame trunc(t/dt).
- Loop flags threaded through BoneTrack::sample, Animation::sample_local and
  sample_pose; control_loop_state() mirrors GetControlLoopState.
- Gr2ModelInstance::SetAnimationLoop(); animated attachments (fmod) loop.
- Quaternion continuity stays on for every span (Granny: buffer path only).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 13:39:33 +09:00
co-authored by Claude Opus 5.5
parent 86759bb726
commit de8fa40098
7 changed files with 252 additions and 82 deletions
+138 -73
View File
@@ -200,14 +200,16 @@ void File::Impl::read_curve2(Ref curve_obj, Ref curve_type, float keyframe_dt, C
if (v == "DaKeyframes32f") {
// 关键帧:knot 不存,第 i 帧在 i*TimeStep(AVA2 全库 19045 条都满足 N-1 == Duration/TimeStep)。
// Degree 0 = 逐帧保持,见 Curve::eval。
// granny CurveIsKeyframed:按帧号取(SetContextFrameIndex 截断 t/dt),见 Curve::eval。
uint16_t dim16 = 0;
uint32_t nc = 0;
const uint8_t* pc = arr("Controls", 4, nc);
if (!u16m("Dimension", dim16) || dim16 == 0 || !pc || nc % dim16) return;
for (uint32_t i = 0; i < nc / dim16; ++i) out.knots.push_back(float(i) * keyframe_dt);
for (uint32_t i = 0; i < nc; ++i) out.controls.push_back(rf(pc, i));
return finish(dim16);
finish(dim16);
out.keyframed = !out.empty();
return;
}
// 其余都是量化 knot + KnotsControls 一块数组:先 N 个 knot,再 N*每knot分量数 个 control。
@@ -438,65 +440,119 @@ bool File::Impl::extract_animation(Ref anim_obj, Ref anim_type, const Skeleton*
// ── T7b:曲线解码 ─────────────────────────────────────────────────────
// Metin2 曲线全是 OldCurveType(B 样条:Degree + Knots[] + Controls[knot*dim]);
// AVA2 的 curve2 变体由 read_curve2 解成同样的 knots/controls。
// 求值照 granny_bspline.cpp SampleBSpline{0,1,2}xN + granny_bspline_inlines.h
// {Linear,Quadratic}Coefficients。degree 0=常量/阶梯,1=线性,2=二次 B 样条。
// KnotIndex = "第一个 > t 的 knot"(granny CurveFindKnot),窗口越界按端点 clamp。
// 求值照 granny2.dll 2.11 的曲线采样器(FUN_1005d950):
// keyframed → 第 floor(t/dt) 帧(SetContextFrameIndex 截断);
// 否则 k = CurveFindKnot(t) = 第一个 > t 的 knot(clamp 到 n-1),
// ConstructBSplineBuffers 取 [k-D, k+D) 的窗口(D=0 时宽 1),再 SampleBSpline{0..3}。
// 窗口越过首/尾:不循环时重复端点;underflow_loop/overflow_loop 时从曲线另一端取
// (跳过末尾那个与首点重复的 knot),knot 时间 ±duration。尾部截出的最后两个 knot 相等时
// 末一个 +1e-5(防除零,与 granny 相同)。
// degree 0 取 pi[0],即第一个 > t 的 knot 的控制点(不是"最后一个 <= t")。
//
// dim==4(四元数):blend 之前对本 span 参与的 degree+1 个控制点做
// EnsureQuaternionContinuity(granny_math.cpp)—— 逐点若与前一点点积 < 0 则整体取反。
// 少了这一步,跨半球(q vs -q)的控制点线性混合会收缩到近零,归一后得到一个
// 合法但方向错约 180° 的旋转(表情动作里脖子/头会折叠塌陷)。
void Curve::eval(float t, float* out) const {
const uint32_t n = (uint32_t)knots.size();
// granny 只在走缓冲区(窗口越界)时做这一步,这里有意总做:数据本身连续时是空操作,
// 而 Metin2 的部分曲线相邻控制点跨半球,不做会让 blend 收缩到近零、归一后转错约 180°
// (表情动作里脖子/头会折叠塌陷)。
//
// 与 granny 的差异(只为健壮):分母为 0 时系数取 0;degree>0 时不循环的一侧 t clamp 到首/尾 knot。
void Curve::eval(float t, float* out, bool underflow_loop, bool overflow_loop, float duration) const {
const int n = (int)knots.size();
if (n == 0 || dim == 0) return;
auto Kc = [&](int i) -> float {
i = i < 0 ? 0 : (i >= (int)n ? (int)n - 1 : i);
return knots[(uint32_t)i];
// Metin2 的 ScaleShearCurve 是 9 维;目前支持的维度为 position=3、orientation=4、scale/shear=9。
if (dim > 9 || controls.size() < size_t(n) * dim) return;
auto normalize4 = [&](float* q) {
float m = std::sqrt(q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]);
if (m > 1e-12f) for (int d = 0; d < 4; ++d) q[d] /= m;
};
auto copy_ctrl = [&](int i) {
for (uint32_t d = 0; d < dim; ++d) out[d] = controls[size_t(i) * dim + d];
};
if (n == 1) { // 常量
for (uint32_t d = 0; d < dim; ++d) out[d] = controls[d];
return;
}
if (degree <= 0) { // 阶梯(granny SampleBSpline0:最后一个 <= t 的 knot)
uint32_t k = 0;
if (keyframed) { // 按帧号取
int f = 0;
if (knots_sorted) {
const auto it = std::upper_bound(knots.begin(), knots.end(), t);
k = it == knots.begin() ? 0 : uint32_t(it - knots.begin()) - 1;
f = it == knots.begin() ? 0 : int(it - knots.begin()) - 1;
} else {
for (uint32_t j = 0; j < n; ++j) if (knots[j] <= t) k = j;
for (int j = 0; j < n; ++j) if (knots[j] <= t) f = j;
}
for (uint32_t d = 0; d < dim; ++d) out[d] = controls[k * dim + d];
copy_ctrl(f);
return;
}
if (t <= knots[0]) t = knots[0];
if (t >= knots[n - 1]) t = knots[n - 1];
// 第一个 > t 的 knot(找不到 → n-1);升序时二分
int i = (int)n - 1;
const int D = degree < 0 ? 0 : (degree > 3 ? 3 : degree);
if (n == 1) { // 常量(D>0 的单 knot 曲线 granny 走 B 样条,结果仍是该点,四元数归一)
copy_ctrl(0);
if (D > 0 && dim == 4) normalize4(out);
return;
}
// degree 0 不 clamp:t < 首 knot 时 granny 取 knot 0,clamp 会变成 knot 1。
if (D > 0 && !underflow_loop && t < knots[0]) t = knots[0];
if (D > 0 && !overflow_loop && t > knots[n - 1]) t = knots[n - 1];
// CurveFindKnot:第一个 > t 的 knot,找不到 → n-1
int k = n - 1;
if (knots_sorted) {
const int k = int(std::upper_bound(knots.begin(), knots.end(), t) - knots.begin());
if (k < (int)n) i = k;
const int u = int(std::upper_bound(knots.begin(), knots.end(), t) - knots.begin());
if (u < n) k = u;
} else {
for (uint32_t k = 0; k < n; ++k) if (knots[k] > t) { i = (int)k; break; }
for (int j = 0; j < n; ++j) if (knots[j] > t) { k = j; break; }
}
if (i < 1) i = 1;
const int span = (degree == 1) ? 2 : 3; // 本 span 的控制点数 = degree+1
const int first = (degree == 1) ? (i - 1) : (i - 2);
// 拷本 span 的控制点(越界按端点 clamp),四元数做半球连续化
// Metin2 的 ScaleShearCurve 是 9 维;旧的 3*4 缓冲区会在这里发生
// 栈溢出(degree=2 时写入 27 个 float),随后被 __stack_chk_fail 终止。
// 目前支持的曲线维度为 position=3、orientation=4、scale/shear=9。
if (dim > 9) return;
float cp[3 * 9];
for (int s = 0; s < span; ++s) {
int ci = first + s;
ci = ci < 0 ? 0 : (ci >= (int)n ? (int)n - 1 : ci);
for (uint32_t d = 0; d < dim; ++d) cp[s * dim + d] = controls[(uint32_t)ci * dim + d];
// ConstructBSplineBuffers
const int W = D == 0 ? 1 : 2 * D;
const int n_eff = overflow_loop ? n - 1 : n; // 循环时末 knot = 下一遍的首 knot
const int m_loop = n - 1; // Prev/Next 的 knot 数(>1 时减 1)
float bt[6], bp[6 * 9];
const int start0 = k - D;
int local_c = 0, cnt = W, s0 = start0;
if (s0 < 0) { local_c = -s0; cnt = W + s0; s0 = 0; }
const bool over = start0 + W > n_eff;
if (over) cnt = n_eff - s0;
for (int i = 0; i < cnt; ++i) {
bt[local_c + i] = knots[s0 + i];
for (uint32_t d = 0; d < dim; ++d) bp[(local_c + i) * dim + d] = controls[size_t(s0 + i) * dim + d];
}
if (over && cnt > 1 && bt[local_c + cnt - 1] == bt[local_c + cnt - 2])
bt[local_c + cnt - 1] += 1e-5f;
// underflow:向前填 [0, local_c)
{
int j = m_loop - 1;
float off = -duration;
for (int i = local_c - 1; i >= 0; --i) {
if (!underflow_loop) {
bt[i] = bt[local_c];
for (uint32_t d = 0; d < dim; ++d) bp[i * dim + d] = bp[local_c * dim + d];
continue;
}
bt[i] = knots[j] + off;
for (uint32_t d = 0; d < dim; ++d) bp[i * dim + d] = controls[size_t(j) * dim + d];
if (--j < 0) { j = m_loop - 1; off -= duration; }
}
}
// overflow:向后填 (last, W)
{
const int last = local_c + cnt - 1;
int j = 0;
float off = duration;
for (int i = last + 1; i < W; ++i) {
if (!overflow_loop) {
bt[i] = bt[last];
for (uint32_t d = 0; d < dim; ++d) bp[i * dim + d] = bp[last * dim + d];
continue;
}
bt[i] = knots[j] + off;
for (uint32_t d = 0; d < dim; ++d) bp[i * dim + d] = controls[size_t(j) * dim + d];
if (++j == m_loop) { j = 0; off += duration; }
}
}
const float* ti = bt + D; // ti[-D .. D-1]
float* cp = bp; // 本 span 的控制点 pi[-D .. 0] = cp[0 .. D]
if (dim == 4) {
for (int s = 1; s < span; ++s) {
for (int s = 1; s <= D; ++s) {
const float* p = &cp[(s - 1) * 4];
float* q = &cp[s * 4];
if (p[0]*q[0] + p[1]*q[1] + p[2]*q[2] + p[3]*q[3] < 0.0f)
@@ -504,42 +560,50 @@ void Curve::eval(float t, float* out) const {
}
}
if (degree == 1) {
float denom = Kc(i) - Kc(i - 1);
float c0 = denom != 0.0f ? (t - Kc(i - 1)) / denom : 0.0f;
c0 = c0 < 0 ? 0 : (c0 > 1 ? 1 : c0);
float c1 = 1.0f - c0;
for (uint32_t d = 0; d < dim; ++d)
out[d] = c1 * cp[0 * dim + d] + c0 * cp[1 * dim + d];
} else { // degree >= 2 → 二次(Metin2 无 degree 3)
float ti_2 = Kc(i - 2), ti_1 = Kc(i - 1), ti = Kc(i), ti1 = Kc(i + 1);
auto fdiv = [](float a, float b) { return b != 0.0f ? a / b : 0.0f; };
float L0 = fdiv(t - ti_1, ti - ti_1);
float L1_1 = fdiv(t - ti_2, ti - ti_2);
float L1_2 = fdiv(t - ti_1, ti1 - ti_1);
float tmp = (L1_1 + L0) - L0 * L1_1;
float c0 = L0 * L1_2; // ci
float c1 = tmp - c0; // ci_1
float c2 = 1.0f - tmp; // ci_2
for (uint32_t d = 0; d < dim; ++d)
out[d] = c2 * cp[0 * dim + d] + c1 * cp[1 * dim + d] + c0 * cp[2 * dim + d];
auto fdiv = [](float a, float b) { return b != 0.0f ? a / b : 0.0f; };
float c[4]; // c[s] 乘 cp[s]
if (D == 0) {
c[0] = 1.0f;
} else if (D == 1) { // LinearCoefficients
const float a = fdiv(t - ti[-1], ti[0] - ti[-1]);
c[0] = 1.0f - a; c[1] = a;
} else if (D == 2) { // QuadraticCoefficients
const float L0 = fdiv(t - ti[-1], ti[0] - ti[-1]);
const float L1_1 = fdiv(t - ti[-2], ti[0] - ti[-2]);
const float L1_2 = fdiv(t - ti[-1], ti[1] - ti[-1]);
const float tmp = (L1_1 + L0) - L0 * L1_1;
c[2] = L0 * L1_2;
c[1] = tmp - c[2];
c[0] = 1.0f - tmp;
} else { // CubicCoefficients(FUN_10018a20)
const float a = fdiv(t - ti[-1], ti[0] - ti[-1]);
const float b = fdiv(t - ti[-2], ti[0] - ti[-2]);
const float cc = fdiv(t - ti[-1], ti[1] - ti[-1]);
const float d = fdiv(t - ti[-2], ti[1] - ti[-2]);
const float e = fdiv(t - ti[-3], ti[0] - ti[-3]);
const float g = fdiv(t - ti[-1], ti[2] - ti[-1]);
c[0] = (1 - e) * (1 - b) * (1 - a);
c[1] = (1 - a) * b * (1 - d) + (1 - b) * (1 - a) * e + (1 - cc) * a * (1 - d);
c[2] = ((1 - cc) * a + (1 - a) * b) * d + (1 - g) * cc * a;
c[3] = cc * a * g;
}
if (dim == 4) { // 四元数:归一
float m = std::sqrt(out[0]*out[0] + out[1]*out[1] + out[2]*out[2] + out[3]*out[3]);
if (m > 1e-12f) for (int d = 0; d < 4; ++d) out[d] /= m;
for (uint32_t dd = 0; dd < dim; ++dd) {
float v = 0.0f;
for (int s = 0; s <= D; ++s) v += c[s] * cp[s * dim + dd];
out[dd] = v;
}
if (dim == 4 && D > 0) normalize4(out);
}
// t 处的 SRT。position 缺省 (0,0,0),orientation 缺省单位四元数,scaleshear 缺省 I。
void BoneTrack::sample(float t, Transform& out) const {
void BoneTrack::sample(float t, Transform& out, bool ul, bool ol, float dur) const {
out = Transform{};
if (!position.empty()) { position.eval(t, out.position); out.flags |= 0x1; }
if (!orientation.empty()) { orientation.eval(t, out.orientation); out.flags |= 0x2; }
if (!position.empty()) { position.eval(t, out.position, ul, ol, dur); out.flags |= 0x1; }
if (!orientation.empty()) { orientation.eval(t, out.orientation, ul, ol, dur); out.flags |= 0x2; }
if (!scale_shear.empty()) {
if (scale_shear.dim == 9) { scale_shear.eval(t, out.scale_shear); out.flags |= 0x4; }
if (scale_shear.dim == 9) { scale_shear.eval(t, out.scale_shear, ul, ol, dur); out.flags |= 0x4; }
else if (scale_shear.dim == 3) {
float s[3]; scale_shear.eval(t, s);
float s[3]; scale_shear.eval(t, s, ul, ol, dur);
out.scale_shear[0] = s[0]; out.scale_shear[4] = s[1]; out.scale_shear[8] = s[2];
out.flags |= 0x4;
}
@@ -548,11 +612,12 @@ void BoneTrack::sample(float t, Transform& out) const {
// t 处每个 track 的局部 4x4(BuildCompositeTransform4x4 语义:C 上 3x3 = transpose(R3*SS3),
// 平移在 elem 12..14)。
void Animation::sample_local(float t, std::vector<Mat4>& out) const {
void Animation::sample_local(float t, std::vector<Mat4>& out,
bool underflow_loop, bool overflow_loop) const {
out.resize(tracks.size());
Transform x;
for (size_t ti = 0; ti < tracks.size(); ++ti) {
tracks[ti].sample(t, x);
tracks[ti].sample(t, x, underflow_loop, overflow_loop, duration);
out[ti] = compose(x);
}
}
+2 -2
View File
@@ -165,9 +165,9 @@ void bind_pose(const Skeleton& sk, std::vector<Mat4>& world, std::vector<Mat4>&
void sample_pose(const Skeleton& sk, const Animation& an, float t,
std::vector<Mat4>& world, std::vector<Mat4>& skin,
const Mat4* root_offset) {
const Mat4* root_offset, bool underflow_loop, bool overflow_loop) {
std::vector<Mat4> track_local;
an.sample_local(t, track_local);
an.sample_local(t, track_local, underflow_loop, overflow_loop);
std::vector<Mat4> local(sk.bones.size());
for (size_t i = 0; i < sk.bones.size(); ++i) local[i] = sk.bones[i].local_transform; // 默认 = bind