Skip to content

Commit

Permalink
Weapon attach fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PassiveModding committed Mar 30, 2024
1 parent bd27be5 commit 67d79fc
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
11 changes: 4 additions & 7 deletions Meddle/Meddle.Plugin/Models/Attach.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public unsafe class Attach
{
public int ExecuteType { get; }

public Transform OffsetTransform { get; }
public Transform? OffsetTransform { get; }
public byte PartialSkeletonIdx { get; }
public ushort BoneIdx { get; }

Expand All @@ -21,15 +21,12 @@ public Attach(CSAttach* attach)
// 3 => Fashion Accessories
// 4 => Weapon
ExecuteType = attach->ExecuteType;

// TODO: Deconstruct union based on type
var att = attach->SkeletonBoneAttachments[0];
OffsetTransform = new(att.ChildTransform);
if (ExecuteType == 0)
return;


//if (ExecuteType != 4)
// return;
var att = attach->SkeletonBoneAttachments[0];
OffsetTransform = new(att.ChildTransform);

PartialSkeletonIdx = att.SkeletonIdx;
BoneIdx = att.BoneIdx;
Expand Down
32 changes: 24 additions & 8 deletions Meddle/Meddle.Plugin/Services/ExportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ await Task.Run(() =>
var child = attachedChildren[i];
var attachName = skeleton.PartialSkeletons[child.Attach.PartialSkeletonIdx]
.HkSkeleton!.BoneNames[child.Attach.BoneIdx];
var childInfo = HandleAttachedChild(child, i, attachName, scene, boneMap, rootBone);
var childInfo = HandleAttachedChild(child, i, attachName, scene, boneMap);

Check warning on line 106 in Meddle/Meddle.Plugin/Services/ExportManager.cs

View workflow job for this annotation

GitHub Actions / Build (7.0.x, latest)

Possible null reference argument for parameter 'attachName' in '(BoneNodeBuilder rootBone, Matrix4x4 worldPosition, IReadOnlyList<BoneNodeBuilder> childBoneMap) ExportManager.HandleAttachedChild(AttachedChild child, int i, string attachName, SceneBuilder scene, BoneNodeBuilder[]? boneMap)'.

foreach (var model in child.Models)
{
logger.Debug($"Handling child model {model.Path}");
Expand Down Expand Up @@ -166,13 +167,15 @@ private void ExportInternal(
HandleModel(logger, config, model, character.RaceCode, scene, boneMap, Matrix4x4.Identity,
character.CustomizeParameter);
}, config.ParallelBuild);

for (var i = 0; i < character.AttachedChildren.Count; i++)
{
var child = character.AttachedChildren[i];
if (child.Attach.ExecuteType != 4) continue;
var attachName = character.Skeleton.PartialSkeletons[child.Attach.PartialSkeletonIdx]
.HkSkeleton!.BoneNames[child.Attach.BoneIdx];
var childInfo = HandleAttachedChild(child, i, attachName, scene, boneMap, rootBone);
var childInfo = HandleAttachedChild(child, i, attachName, scene, boneMap);

foreach (var model in child.Models)
{
logger.Debug($"Handling child model {model.Path}");
Expand Down Expand Up @@ -241,20 +244,33 @@ private void HandleModel(
int i,
string attachName,
SceneBuilder scene,
BoneNodeBuilder[]? boneMap,
NodeBuilder? rootBone)
BoneNodeBuilder[]? boneMap)
{
var childBoneMap = ModelUtility.GetBoneMap(child.Skeleton, out var childRoot);
childRoot!.SetSuffixRecursively(i);

if (rootBone == null || boneMap == null)
if (child.Attach.OffsetTransform is { } ct)
{
// This appears to fix weapon attaches
childRoot.WithLocalScale(ct.Scale);
childRoot.WithLocalRotation(ct.Rotation);
childRoot.WithLocalTranslation(ct.Translation);
if (childRoot.AnimationTracksNames.Contains("pose"))
{
childRoot.UseScale().UseTrackBuilder("pose").WithPoint(0, ct.Scale);
childRoot.UseRotation().UseTrackBuilder("pose").WithPoint(0, ct.Rotation);
childRoot.UseTranslation().UseTrackBuilder("pose").WithPoint(0, ct.Translation);
}
}

var childBone = boneMap?.FirstOrDefault(b => b.BoneName.Equals(attachName, StringComparison.Ordinal));
if (childBone == null)
{
scene.AddNode(childRoot);
}
else
{
var boneTarget = boneMap.First(b => b.BoneName.Equals(attachName, StringComparison.Ordinal));
boneTarget.AddNode(childRoot);
childBone.AddNode(childRoot);
}

var transform = Matrix4x4.Identity;
Expand Down
25 changes: 17 additions & 8 deletions Meddle/Meddle.Plugin/UI/CharacterTab.Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,28 @@ private void DrawAttachView(CharacterTree tree, ref bool[] set, out IExportReque
var bone = skeleton.HkSkeleton!.BoneNames[child.Attach.BoneIdx];

ImGui.TableNextColumn();
var check = set[i];
if (ImGui.Checkbox($"##{child.GetHashCode()}", ref check))

// other execute types are a lil broken still
if (child.Attach.ExecuteType == 4)
{
set[i] = check;
}
var check = set[i];
if (ImGui.Checkbox($"##{child.GetHashCode()}", ref check))
{
set[i] = check;
}

ImGui.SameLine();
ImGui.SameLine();
}

if (ImGui.CollapsingHeader($"Attach at {bone ?? "unknown"}##{child.GetHashCode()}"))
{
ImGui.Text($"Position: {child.Attach.OffsetTransform.Translation}");
ImGui.Text($"Rotation: {child.Attach.OffsetTransform.Rotation}");
ImGui.Text($"Scale: {child.Attach.OffsetTransform.Scale}");
if (child.Attach.OffsetTransform is { } ct)
{
ImGui.Text($"Position: {ct.Translation}");
ImGui.Text($"Rotation: {ct.Rotation}");
ImGui.Text($"Scale: {ct.Scale}");
}

ImGui.Text($"Execute Type: {child.Attach.ExecuteType}");
if (DrawExportButton($"Export##{child.GetHashCode()}", ExportManager.IsExporting))
{
Expand Down
9 changes: 6 additions & 3 deletions Meddle/Meddle.Plugin/UI/ObjectTab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ private unsafe void DrawCharacterInfo(CSCharacter* character)
var bone = skeleton.HkSkeleton!.BoneNames[child.Attach.BoneIdx];
if (ImGui.CollapsingHeader($"Attach at {bone ?? "unknown"}##{child.GetHashCode()}"))
{
if (child.Attach.OffsetTransform is { } ct)
{
ImGui.Text($"Position: {ct.Translation}");
ImGui.Text($"Rotation: {ct.Rotation}");
ImGui.Text($"Scale: {ct.Scale}");
}

ImGui.Text($"Position: {child.Attach.OffsetTransform.Translation}");
ImGui.Text($"Rotation: {child.Attach.OffsetTransform.Rotation}");
ImGui.Text($"Scale: {child.Attach.OffsetTransform.Scale}");
ImGui.Text($"Execute Type: {child.Attach.ExecuteType}");
using var table = ImRaii.Table("##attachmodels", 1, ImGuiTableFlags.Borders);
foreach (var model in child.Models)
Expand Down
1 change: 1 addition & 0 deletions Meddle/Meddle.Plugin/Utility/ModelUtility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Meddle.Plugin.Models;
using Meddle.Plugin.Xande;
using SharpGLTF.Scenes;
using SharpGLTF.Transforms;

namespace Meddle.Plugin.Utility;

Expand Down

0 comments on commit 67d79fc

Please sign in to comment.