fix-skeleton-bugs #13

Merged
kanym merged 11 commits from fix-skeleton-bugs into main 2026-08-05 18:39:16 +02:00
Showing only changes of commit 5c75170a59 - Show all commits

fix(skeleton): pin the whole skull to the head bone, and report how bones land

The previous attempt only helped bones that matched nothing, and the mandible did
not move - which says the jaw was matching something directly all along, and
being bound to it. A creature jaw is authored against its own skull, so whatever
it lands on, a bone-for-bone match included, puts the mandible near the face
rather than on it.

Jaw, teeth, tongue and chin are now pinned to the head bone as a group, before
any name matching runs, so the skull renders as one piece. It cannot open any
more, which is a fair trade for being attached.

If that is still not it, the shape of the problem is not visible in a screenshot:
it matters whether the jaw has a bone at all, whether it matched, and what it
matched. The body build now reports exactly that, once per model per session -
every skull part and every bone that fell through to the root - so the next look
starts from data instead of another guess.
kanym 2026-08-05 17:39:30 +02:00

View file

@ -15,6 +15,10 @@ namespace RacesOfValheim
private static readonly System.Collections.Generic.HashSet<string> warnedMissingModels =
new System.Collections.Generic.HashSet<string>();
// Model parts whose bone mapping has already been reported this session.
private static readonly System.Collections.Generic.HashSet<string> loggedBoneMaps =
new System.Collections.Generic.HashSet<string>();
private static void WarnMissingModelOnce(string prefabName)
{
if (!warnedMissingModels.Add(prefabName)) return;
@ -295,6 +299,8 @@ namespace RacesOfValheim
// Map bones by name
Transform[] playerBones = __instance.m_bodyModel.bones;
LogBoneMapping(rules.ModelPrefab, newSmr, playerBones);
Transform[] newBones = new Transform[newSmr.bones.Length];
for (int i = 0; i < newSmr.bones.Length; i++)
{
@ -396,23 +402,36 @@ namespace RacesOfValheim
catch (System.Exception e) { }
}
// Parts of a skull that must ride the head as one piece. A creature's jaw is authored
// against its own skull, so binding it to a same-named bone on the player - or letting
// it fall through to the root - puts the mandible somewhere near the face rather than
// on it. Pinning the whole group to the head keeps the skull in one piece; the jaw
// stops being able to open, which is a fair trade for it being attached.
private static readonly string[] SkullParts = { "jaw", "teeth", "tooth", "tongue", "chin", "mandible" };
/// <summary>
/// Finds the player bone a creature bone should be skinned to.
///
/// A creature rig carries bones a human rig has no answer for - a jaw above all, but
/// also teeth and tails. Those used to fall back to bones[0], the root, which skinned
/// the mandible to the hips and left it hanging off the side of the skull. Follow the
/// creature's own hierarchy upward instead until something matches, so a jaw lands on
/// the head and rides it. It cannot open any more, which no player will notice, and it
/// is attached, which every player did.
/// </summary>
private static Transform ResolveBone(Transform[] playerBones, Transform sourceBone)
{
if (sourceBone == null) return playerBones[0];
string name = sourceBone.name.ToLower();
foreach (string part in SkullParts)
{
if (!name.Contains(part)) continue;
Transform head = FindBoneByName(playerBones, "Head");
if (head != null) return head;
break;
}
Transform direct = FindBoneByName(playerBones, sourceBone.name);
if (direct != null) return direct;
// Nothing by that name. Follow the creature's own hierarchy upward until something
// matches, so an unmapped part rides its nearest real parent rather than being
// dumped on bones[0], which is the root.
for (Transform parent = sourceBone.parent; parent != null; parent = parent.parent)
{
Transform match = FindBoneByName(playerBones, parent.name);
@ -422,6 +441,42 @@ namespace RacesOfValheim
return playerBones[0];
}
/// <summary>
/// Reports how a model's bones landed on the player rig, once per model per session.
/// The mandible problem is not diagnosable from a screenshot - it matters whether the
/// jaw has a bone at all, whether it matched something, and what it matched - and this
/// answers all three in one line each.
/// </summary>
private static void LogBoneMapping(string modelName, SkinnedMeshRenderer smr, Transform[] playerBones)
{
if (!loggedBoneMaps.Add($"{modelName}/{smr.name}")) return;
Jotunn.Logger.LogInfo($"[Skeleton] '{modelName}' part '{smr.name}': {smr.bones.Length} bones.");
foreach (Transform bone in smr.bones)
{
if (bone == null) continue;
string lower = bone.name.ToLower();
bool interesting = false;
foreach (string part in SkullParts)
{
if (lower.Contains(part)) { interesting = true; break; }
}
Transform resolved = ResolveBone(playerBones, bone);
bool isRootFallback = resolved == playerBones[0] &&
FindBoneByName(playerBones, bone.name) == null;
if (!interesting && !isRootFallback) continue;
Jotunn.Logger.LogInfo(
$"[Skeleton] '{bone.name}' -> '{(resolved != null ? resolved.name : "null")}'" +
(isRootFallback ? " (ROOT FALLBACK)" : "") +
(interesting ? " (skull part)" : ""));
}
}
private static Transform FindBoneByName(Transform[] bones, string skelBoneName)
{
// Exact match first