Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added safety check
Browse files Browse the repository at this point in the history
d87 committed Sep 29, 2024
1 parent faa8c25 commit 8b36b57
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions CustomizePlus/Armatures/Data/ModelBone.cs
Original file line number Diff line number Diff line change
@@ -218,6 +218,8 @@ public hkQsTransformf GetGameTransform(CharacterBase* cBase, PoseType refFrame)

if (targetPose == null) return Constants.NullTransform;

if (BoneIndex >= targetPose->Skeleton->Bones.Length) return Constants.NullTransform;

return refFrame switch
{
PoseType.Local => targetPose->LocalPose[BoneIndex],
@@ -238,6 +240,9 @@ public hkQsTransformf GetGameTransform(CharacterBase* cBase, PoseType refFrame)
if (targetPose == null) return null;
const PropagateOrNot DO_NOT_PROPAGATE = 0;

// It's really gonna crash without it, skeleton changes aren't getting picked up fast enough
if (BoneIndex >= targetPose->Skeleton->Bones.Length) return null;

return refFrame switch
{
PoseType.Local => targetPose->AccessBoneLocalSpace(BoneIndex),
@@ -326,7 +331,9 @@ public unsafe void PropagateChildren(CharacterBase* cBase, hkQsTransformf* trans
// Bone parenting
// Adapted from Anamnesis Studio code shared by Yuki - thank you!

// Original Parent Bone position after it had its offsets applied
var sourcePos = transform->Translation.ToVector3();

var deltaRot = transform->Rotation.ToQuaternion() / initialRot;
var deltaPos = sourcePos - initialPos;
var deltaScale = transform->Scale.ToVector3() / initialScale;
@@ -335,23 +342,26 @@ public unsafe void PropagateChildren(CharacterBase* cBase, hkQsTransformf* trans
{
// Plugin.Logger.Debug($"Propagating to {child.BoneName}...");
var access = child.GetGameTransformAccess(cBase, PoseType.Model);
if (access != null)
{

var offset = access->Translation.ToVector3() - sourcePos;
var offset = access->Translation.ToVector3() - sourcePos;

var matrix = InteropAlloc.GetMatrix(access);
if (propagateScale)
{
var scaleMatrix = Matrix4x4.CreateScale(deltaScale, Vector3.Zero);
matrix *= scaleMatrix;
offset = Vector3.Transform(offset, scaleMatrix);
}
if (propagateRotation)
{
matrix *= Matrix4x4.CreateFromQuaternion(deltaRot);
offset = Vector3.Transform(offset, deltaRot);
var matrix = InteropAlloc.GetMatrix(access);
if (propagateScale)
{
var scaleMatrix = Matrix4x4.CreateScale(deltaScale, Vector3.Zero);
matrix *= scaleMatrix;
offset = Vector3.Transform(offset, scaleMatrix);
}
if (propagateRotation)
{
matrix *= Matrix4x4.CreateFromQuaternion(deltaRot);
offset = Vector3.Transform(offset, deltaRot);
}
matrix.Translation = deltaPos + sourcePos + offset;
InteropAlloc.SetMatrix(access, matrix);
}
matrix.Translation = deltaPos + sourcePos + offset;
InteropAlloc.SetMatrix(access, matrix);
}
}

0 comments on commit 8b36b57

Please sign in to comment.