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
+2 -1
View File
@@ -96,7 +96,8 @@ private:
// 单位阵(见 extension/src/metin2_anim.cpp 与 test/rendering/README.md 的实测)。
void sample_pose(const Skeleton& sk, const Animation& an, float t,
std::vector<Mat4>& world, std::vector<Mat4>& skin,
const Mat4* root_offset = nullptr);
const Mat4* root_offset = nullptr,
bool underflow_loop = false, bool overflow_loop = false);
void bind_pose(const Skeleton& sk, std::vector<Mat4>& world, std::vector<Mat4>& skin);
// 打印辅助
+21 -3
View File
@@ -105,9 +105,14 @@ struct Curve {
std::vector<float> knots; // KnotCount 个
std::vector<float> controls; // KnotCount * dim 个
bool knots_sorted = true; // 升序 → eval 二分找 knot;否则按原样线性扫
bool keyframed = false; // DaKeyframes32f:按帧号 floor(t/dt) 取,不走 B 样条
bool empty() const { return knots.empty() || dim == 0; }
// t 处求值,写 dim 个 float 到 out(identity 由调用方给)。
void eval(float t, float* out) const;
// underflow_loop/overflow_loop = granny Control 的循环状态(见 control_loop_state):
// 窗口越过曲线首/尾时,从曲线另一端取 knot(±duration)而不是重复端点。
// duration = Animation::duration(granny 的 LocalDuration)。
void eval(float t, float* out, bool underflow_loop = false, bool overflow_loop = false,
float duration = 0.0f) const;
};
struct TextTrackEntry { float time_stamp = 0.0f; std::string text; };
@@ -123,7 +128,8 @@ struct BoneTrack {
Curve orientation; // dim 4(四元数 x,y,z,w)
Curve scale_shear; // dim 9(3x3)或 dim 3
// t 处的 SRT(缺的曲线取单位分量,flags 只标出有曲线的分量)。sample_local 的 4x4 = compose(这个)。
void sample(float t, Transform& out) const;
void sample(float t, Transform& out, bool underflow_loop = false, bool overflow_loop = false,
float duration = 0.0f) const;
};
// Preserve group boundaries: an animation can contain an auxiliary object group before its model.
@@ -152,9 +158,21 @@ struct Animation {
std::vector<AnimationTrackGroup> track_groups;
// t 处每个 track 的局部 4x4(granny_transform 组合语义,行主序)。
// out 按 tracks 顺序;bone_index 见各 track。
void sample_local(float t, std::vector<Mat4>& out) const;
// underflow_loop/overflow_loop 见 Curve::eval;循环播放时两者都传 true。
void sample_local(float t, std::vector<Mat4>& out,
bool underflow_loop = false, bool overflow_loop = false) const;
};
// granny GetControlLoopState:loop_count==0(无限循环)→ 两端都循环;否则第 loop_index 遍
// 前面还有遍数才 underflow,后面还有遍数才 overflow。force_clamped(Control Flags 0x20)→ 都不循环。
inline void control_loop_state(int loop_count, int loop_index, bool force_clamped,
bool& underflow_loop, bool& overflow_loop) {
if (force_clamped) { underflow_loop = overflow_loop = false; return; }
if (loop_count == 0) { underflow_loop = overflow_loop = true; return; }
underflow_loop = loop_index > 0;
overflow_loop = loop_index < loop_count - 1;
}
// 顶层 granny_material(driven by root Materials[])。name + 解析出的漫反射贴图。
// root Materials[] 之外、只经 Maps[] / mesh MaterialBindings 可达的子材质追加在后面。
struct MaterialMap {