Peds no longer sharing skeletons, Cutscene object AnimHash conflict resolution

This commit is contained in:
dexy 2019-11-26 17:00:41 +11:00
parent 17dcdd88a1
commit 91873ea87a
7 changed files with 215 additions and 71 deletions

View File

@ -510,7 +510,7 @@ namespace CodeWalker.GameFiles
// structure data // structure data
public uint VFT { get; set; } public uint VFT { get; set; }
public uint Unknown_4h { get; set; } // 0x00000001 public uint Unknown_4h { get; set; } = 1; // 0x00000001
public uint Unknown_8h { get; set; } // 0x00000000 public uint Unknown_8h { get; set; } // 0x00000000
public uint Unknown_Ch { get; set; } // 0x00000000 public uint Unknown_Ch { get; set; } // 0x00000000
public ulong BoneTagsPointer { get; set; } public ulong BoneTagsPointer { get; set; }
@ -527,7 +527,7 @@ namespace CodeWalker.GameFiles
public MetaHash Unknown_50h { get; set; } public MetaHash Unknown_50h { get; set; }
public MetaHash Unknown_54h { get; set; } public MetaHash Unknown_54h { get; set; }
public MetaHash Unknown_58h { get; set; } public MetaHash Unknown_58h { get; set; }
public ushort Unknown_5Ch { get; set; } // 0x0001 public ushort Unknown_5Ch { get; set; } = 1; // 0x0001
public ushort BonesCount { get; set; } public ushort BonesCount { get; set; }
public ushort ChildIndicesCount { get; set; } public ushort ChildIndicesCount { get; set; }
public ushort Unknown_62h { get; set; } // 0x0000 public ushort Unknown_62h { get; set; } // 0x0000
@ -553,6 +553,9 @@ namespace CodeWalker.GameFiles
public Dictionary<ushort, Bone> BonesMap { get; set; }//for convienience finding bones by tag public Dictionary<ushort, Bone> BonesMap { get; set; }//for convienience finding bones by tag
public Matrix3_s[] BoneTransforms; //for rendering
/// <summary> /// <summary>
/// Reads the data-block from a stream. /// Reads the data-block from a stream.
@ -601,35 +604,9 @@ namespace CodeWalker.GameFiles
this.ChildIndices = reader.ReadShortsAt(this.ChildIndicesPointer, this.ChildIndicesCount); this.ChildIndices = reader.ReadShortsAt(this.ChildIndicesPointer, this.ChildIndicesCount);
if ((Bones != null) && (ParentIndices != null)) AssignBoneParents();
{
var maxcnt = Math.Min(Bones.Count, ParentIndices.Length);
for (int i = 0; i < maxcnt; i++)
{
var bone = Bones[i];
var pind = ParentIndices[i];
if ((pind >= 0) && (pind < Bones.Count))
{
bone.Parent = Bones[pind];
}
}
}
BonesMap = new Dictionary<ushort, Bone>();
if (Bones != null)
{
for (int i = 0; i < Bones.Count; i++)
{
var bone = Bones[i];
BonesMap[bone.Tag] = bone;
bone.UpdateAnimTransform();
bone.BindTransformInv = (i < TransformationsInverted?.Length) ? TransformationsInverted[i] : Matrix.Invert(bone.AnimTransform);
bone.BindTransformInv.M44 = 1.0f;
bone.UpdateSkinTransform();
}
}
BuildBonesMap();
} }
/// <summary> /// <summary>
@ -708,6 +685,165 @@ namespace CodeWalker.GameFiles
} }
return list.ToArray(); return list.ToArray();
} }
public void AssignBoneParents()
{
if ((Bones != null) && (ParentIndices != null))
{
var maxcnt = Math.Min(Bones.Count, ParentIndices.Length);
for (int i = 0; i < maxcnt; i++)
{
var bone = Bones[i];
var pind = ParentIndices[i];
if ((pind >= 0) && (pind < Bones.Count))
{
bone.Parent = Bones[pind];
}
}
}
}
public void BuildBonesMap()
{
BonesMap = new Dictionary<ushort, Bone>();
if (Bones != null)
{
for (int i = 0; i < Bones.Count; i++)
{
var bone = Bones[i];
BonesMap[bone.Tag] = bone;
bone.UpdateAnimTransform();
bone.BindTransformInv = (i < TransformationsInverted?.Length) ? TransformationsInverted[i] : Matrix.Invert(bone.AnimTransform);
bone.BindTransformInv.M44 = 1.0f;
bone.UpdateSkinTransform();
}
}
}
public void ResetBoneTransforms()
{
if (Bones?.Data == null) return;
foreach (var bone in Bones.Data)
{
bone.ResetAnimTransform();
}
UpdateBoneTransforms();
}
public void UpdateBoneTransforms()
{
if (Bones?.Data == null) return;
if ((BoneTransforms == null) || (BoneTransforms.Length != Bones.Data.Count))
{
BoneTransforms = new Matrix3_s[Bones.Data.Count];
}
for (int i = 0; i < Bones.Data.Count; i++)
{
var bone = Bones.Data[i];
Matrix b = bone.SkinTransform;
Matrix3_s bt = new Matrix3_s();
bt.Row1 = b.Column1;
bt.Row2 = b.Column2;
bt.Row3 = b.Column3;
BoneTransforms[i] = bt;
}
}
public Skeleton Clone()
{
var skel = new Skeleton();
skel.Count1 = Count1;
skel.Count2 = Count2;
skel.Unknown_1Ch = Unknown_1Ch;
skel.Unknown_50h = Unknown_50h;
skel.Unknown_54h = Unknown_54h;
skel.Unknown_58h = Unknown_58h;
skel.BonesCount = BonesCount;
skel.ChildIndicesCount = ChildIndicesCount;
if (BoneTags != null)
{
skel.BoneTags = new ResourcePointerArray64<SkeletonBoneTag>();
if (BoneTags.data_items != null)
{
skel.BoneTags.data_items = new SkeletonBoneTag[BoneTags.data_items.Length];
for (int i = 0; i < BoneTags.data_items.Length; i++)
{
var obt = BoneTags.data_items[i];
var nbt = new SkeletonBoneTag();
skel.BoneTags.data_items[i] = nbt;
while (obt != null)
{
nbt.BoneTag = obt.BoneTag;
nbt.BoneIndex = obt.BoneIndex;
obt = obt.LinkedTag;
if (obt != null)
{
var nxt = new SkeletonBoneTag();
nbt.LinkedTag = nxt;
nbt = nxt;
}
}
}
}
}
if (Bones != null)
{
skel.Bones = new ResourceSimpleArray<Bone>();
if (Bones.Data != null)
{
skel.Bones.Data = new List<Bone>();
for (int i = 0; i < Bones.Data.Count; i++)
{
var ob = Bones.Data[i];
var nb = new Bone();
nb.Rotation = ob.Rotation;
nb.Translation = ob.Translation;
nb.Scale = ob.Scale;
nb.NextSiblingIndex = ob.NextSiblingIndex;
nb.ParentIndex = ob.ParentIndex;
nb.Flags = ob.Flags;
nb.Index = ob.Index;
nb.Tag = ob.Tag;
nb.Index2 = ob.Index2;
nb.Name = ob.Name;
nb.AnimRotation = ob.AnimRotation;
nb.AnimTranslation = ob.AnimTranslation;
nb.AnimScale = ob.AnimScale;
nb.AnimTransform = ob.AnimTransform;
nb.BindTransformInv = ob.BindTransformInv;
nb.SkinTransform = ob.SkinTransform;
skel.Bones.Data.Add(nb);
}
}
}
skel.TransformationsInverted = (Matrix[])TransformationsInverted.Clone();
skel.Transformations = (Matrix[])Transformations.Clone();
skel.ParentIndices = (short[])ParentIndices.Clone();
skel.ChildIndices = (short[])ChildIndices.Clone();
skel.AssignBoneParents();
skel.BuildBonesMap();
return skel;
}
} }
[TypeConverter(typeof(ExpandableObjectConverter))] public class SkeletonBoneTag : ResourceSystemBlock [TypeConverter(typeof(ExpandableObjectConverter))] public class SkeletonBoneTag : ResourceSystemBlock
@ -800,15 +936,11 @@ namespace CodeWalker.GameFiles
} }
// structure data // structure data
//public float RotationX { get; set; }
//public float RotationY { get; set; }
//public float RotationZ { get; set; }
//public float RotationW { get; set; }
public Quaternion Rotation { get; set; } public Quaternion Rotation { get; set; }
public Vector3 Translation { get; set; } public Vector3 Translation { get; set; }
public uint Unknown_1Ch { get; set; } // 0x00000000 RHW? public uint Unknown_1Ch { get; set; } // 0x00000000 RHW?
public Vector3 Scale { get; set; } public Vector3 Scale { get; set; }
public float Unknown_2Ch { get; set; } // 1.0 RHW? public float Unknown_2Ch { get; set; } = 1.0f; // 1.0 RHW?
public short NextSiblingIndex { get; set; } //limb end index? IK chain? public short NextSiblingIndex { get; set; } //limb end index? IK chain?
public short ParentIndex { get; set; } public short ParentIndex { get; set; }
public uint Unknown_34h { get; set; } // 0x00000000 public uint Unknown_34h { get; set; } // 0x00000000

View File

@ -29,11 +29,13 @@ namespace CodeWalker.World
public Drawable[] Drawables { get; set; } = new Drawable[12]; public Drawable[] Drawables { get; set; } = new Drawable[12];
public Texture[] Textures { get; set; } = new Texture[12]; public Texture[] Textures { get; set; } = new Texture[12];
public bool EnableRootMotion { get; set; } = false; //used to toggle whether or not to include root motion when playing animations public bool EnableRootMotion { get; set; } = false; //used to toggle whether or not to include root motion when playing animations
public Skeleton Skeleton { get; set; } = null;
public Vector3 Position { get; set; } = Vector3.Zero; public Vector3 Position { get; set; } = Vector3.Zero;
public Quaternion Rotation { get; set; } = Quaternion.Identity; public Quaternion Rotation { get; set; } = Quaternion.Identity;
public void Init(string name, GameFileCache gfc) public void Init(string name, GameFileCache gfc)
{ {
var hash = JenkHash.GenHash(name.ToLowerInvariant()); var hash = JenkHash.GenHash(name.ToLowerInvariant());
@ -108,6 +110,8 @@ namespace CodeWalker.World
} }
Skeleton = Yft?.Fragment?.Drawable?.Skeleton?.Clone();
MetaHash cliphash = JenkHash.GenHash("idle"); MetaHash cliphash = JenkHash.GenHash("idle");
ClipMapEntry cme = null; ClipMapEntry cme = null;
Ycd?.ClipMap?.TryGetValue(cliphash, out cme); Ycd?.ClipMap?.TryGetValue(cliphash, out cme);

View File

@ -68,6 +68,7 @@ namespace CodeWalker.Rendering
//public Dictionary<uint, Texture> TextureDict { get; private set; } //public Dictionary<uint, Texture> TextureDict { get; private set; }
//public long EmbeddedTextureSize { get; private set; } //public long EmbeddedTextureSize { get; private set; }
public Skeleton Skeleton { get; set; } = null;
public bool HasSkeleton; public bool HasSkeleton;
public bool HasTransforms; public bool HasTransforms;
@ -77,8 +78,6 @@ namespace CodeWalker.Rendering
public ClipMapEntry ClipMapEntry; public ClipMapEntry ClipMapEntry;
public Dictionary<ushort, RenderableModel> ModelBoneLinks; public Dictionary<ushort, RenderableModel> ModelBoneLinks;
public Matrix3_s[] BoneTransforms;
public List<Bone> Bones;
public bool EnableRootMotion = false; //used to toggle whether or not to include root motion when playing animations public bool EnableRootMotion = false; //used to toggle whether or not to include root motion when playing animations
@ -240,7 +239,7 @@ namespace CodeWalker.Rendering
HasSkeleton = hasskeleton; HasSkeleton = hasskeleton;
HasTransforms = hastransforms; HasTransforms = hastransforms;
Bones = skeleton?.Bones?.Data; Skeleton = skeleton;
//calculate transforms for the models if there are any. (TODO: move this to a method for re-use...) //calculate transforms for the models if there are any. (TODO: move this to a method for re-use...)
@ -362,30 +361,18 @@ namespace CodeWalker.Rendering
public void ResetBoneTransforms() public void ResetBoneTransforms()
{ {
if (Bones == null) return; if (Skeleton == null) return;
foreach (var bone in Bones) Skeleton.ResetBoneTransforms();
{
bone.ResetAnimTransform();
}
UpdateBoneTransforms(); UpdateBoneTransforms();
} }
private void UpdateBoneTransforms() private void UpdateBoneTransforms()
{ {
if (Bones == null) return; if (Skeleton?.Bones?.Data == null) return;
if ((BoneTransforms == null) || (BoneTransforms.Length != Bones.Count))
{ Skeleton.UpdateBoneTransforms();
BoneTransforms = new Matrix3_s[Bones.Count];
} var bones = Skeleton.Bones?.Data;
for (int i = 0; i < Bones.Count; i++) var bonetransforms = Skeleton.BoneTransforms;
{
var bone = Bones[i];
Matrix b = bone.SkinTransform;
Matrix3_s bt = new Matrix3_s();
bt.Row1 = b.Column1;
bt.Row2 = b.Column2;
bt.Row3 = b.Column3;
BoneTransforms[i] = bt;
}
var drawbl = Key; var drawbl = Key;
if (AllModels == null) return; if (AllModels == null) return;
@ -398,7 +385,7 @@ namespace CodeWalker.Rendering
var geom = model.Geometries[g]; var geom = model.Geometries[g];
var boneids = geom?.DrawableGeom?.BoneIds; var boneids = geom?.DrawableGeom?.BoneIds;
if (boneids == null) continue; if (boneids == null) continue;
if (boneids.Length != Bones.Count) if (boneids.Length != bones.Count)
{ {
var idc = boneids.Length; var idc = boneids.Length;
if (geom.BoneTransforms == null) if (geom.BoneTransforms == null)
@ -408,9 +395,9 @@ namespace CodeWalker.Rendering
for (int b = 0; b < idc; b++) for (int b = 0; b < idc; b++)
{ {
var id = boneids[b]; var id = boneids[b];
if (id < BoneTransforms.Length) if (id < bonetransforms.Length)
{ {
geom.BoneTransforms[b] = BoneTransforms[id]; geom.BoneTransforms[b] = bonetransforms[id];
if (id != b) if (id != b)
{ } { }
} }
@ -492,7 +479,7 @@ namespace CodeWalker.Rendering
var frame = anim.GetFramePosition(t); var frame = anim.GetFramePosition(t);
var dwbl = this.Key; var dwbl = this.Key;
var skel = dwbl?.Skeleton; var skel = Skeleton;
var bones = skel?.Bones; var bones = skel?.Bones;
if (bones == null) if (bones == null)
{ return; } { return; }

View File

@ -2785,7 +2785,7 @@ namespace CodeWalker.Rendering
ac.EnableRootMotion = ped.EnableRootMotion; ac.EnableRootMotion = ped.EnableRootMotion;
} }
var skel = ped.Yft?.Fragment?.Drawable?.Skeleton; var skel = ped.Skeleton;
if (skel != null) if (skel != null)
{ {
if (drawable.Skeleton == null) if (drawable.Skeleton == null)

View File

@ -574,9 +574,9 @@ namespace CodeWalker.Rendering
public override void SetModelVars(DeviceContext context, RenderableModel model) public override void SetModelVars(DeviceContext context, RenderableModel model)
{ {
if (model.Owner.BoneTransforms != null) if (model.Owner.Skeleton?.BoneTransforms != null)
{ {
SetBoneMatrices(context, model.Owner.BoneTransforms); SetBoneMatrices(context, model.Owner.Skeleton.BoneTransforms);
defaultBoneMatricesBound = false; defaultBoneMatricesBound = false;
} }
else if (!defaultBoneMatricesBound) else if (!defaultBoneMatricesBound)

View File

@ -233,9 +233,9 @@ namespace CodeWalker.Rendering
public override void SetModelVars(DeviceContext context, RenderableModel model) public override void SetModelVars(DeviceContext context, RenderableModel model)
{ {
if (model.Owner.BoneTransforms != null) if (model.Owner.Skeleton?.BoneTransforms != null)
{ {
SetBoneMatrices(context, model.Owner.BoneTransforms); SetBoneMatrices(context, model.Owner.Skeleton.BoneTransforms);
defaultBoneMatricesBound = false; defaultBoneMatricesBound = false;
} }
else if (!defaultBoneMatricesBound) else if (!defaultBoneMatricesBound)

View File

@ -469,7 +469,7 @@ namespace CodeWalker.World
{ {
if (obj.Ped != null) if (obj.Ped != null)
{ {
ycd.CutsceneMap.TryGetValue(obj.Ped.NameHash, out cme); ycd.CutsceneMap.TryGetValue(obj.AnimHash, out cme);
var pos = Position; var pos = Position;
var rot = Rotation; var rot = Rotation;
if (cme != null) if (cme != null)
@ -488,7 +488,7 @@ namespace CodeWalker.World
} }
if (obj.Prop != null) if (obj.Prop != null)
{ {
ycd.CutsceneMap.TryGetValue(obj.Prop.Archetype?.Hash ?? 0, out cme); ycd.CutsceneMap.TryGetValue(obj.AnimHash, out cme);
var pos = Position; var pos = Position;
var rot = Rotation; var rot = Rotation;
if (cme != null) if (cme != null)
@ -989,11 +989,29 @@ namespace CodeWalker.World
if (Objects == null) return; if (Objects == null) return;
var refCounts = new Dictionary<MetaHash, int>();
foreach (var obj in Objects.Values) foreach (var obj in Objects.Values)
{ {
var sobj = new CutsceneObject(); var sobj = new CutsceneObject();
sobj.Init(obj, GameFileCache); sobj.Init(obj, GameFileCache);
SceneObjects[sobj.ObjectID] = sobj; SceneObjects[sobj.ObjectID] = sobj;
if (sobj.AnimHash != 0)
{
int refcount = 0;
var hash = sobj.AnimHash;
refCounts.TryGetValue(hash, out refcount);
if (refcount > 0)
{
var newstr = hash.ToString() + "^" + refcount.ToString();
sobj.AnimHash = JenkHash.GenHash(newstr);
}
refcount++;
refCounts[hash] = refcount;
}
} }
} }
} }
@ -1011,6 +1029,7 @@ namespace CodeWalker.World
public YmapEntityDef Prop { get; set; } public YmapEntityDef Prop { get; set; }
public MetaHash AnimHash { get; set; }
public ClipMapEntry AnimClip { get; set; } public ClipMapEntry AnimClip { get; set; }
@ -1107,15 +1126,17 @@ namespace CodeWalker.World
Ped.SetComponentDrawable(4, 19, 0, 0, gfc); Ped.SetComponentDrawable(4, 19, 0, 0, gfc);
Ped.SetComponentDrawable(6, null, null, gfc); Ped.SetComponentDrawable(6, null, null, gfc);
} }
AnimHash = ped.StreamingName;
} }
private void InitProp(CutPropModelObject prop, GameFileCache gfc) private void InitProp(CutPropModelObject prop, GameFileCache gfc)
{ {
var archetypeName = prop.StreamingName;
Prop = new YmapEntityDef(); Prop = new YmapEntityDef();
Prop.SetArchetype(gfc.GetArchetype(archetypeName)); Prop.SetArchetype(gfc.GetArchetype(prop.StreamingName));
AnimHash = prop.StreamingName;
} }
private void InitVehicle(CutVehicleModelObject veh, GameFileCache gfc) private void InitVehicle(CutVehicleModelObject veh, GameFileCache gfc)