Skip to content

Commit

Permalink
fix bone positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
krogenth committed Jul 20, 2024
1 parent 7ea92c8 commit ec1796f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/G2DataGUI.Common/Data/Common/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ public class Vector3
public float Y { get; set; }
public float Z { get; set; }

public static Vector3 operator+(Vector3 left, Vector3 right)
{
return new Vector3
{
X = left.X + right.X,
Y = left.Y + right.Y,
Z = left.Z + right.Z,
};
}

public static Vector3 operator-(Vector3 left, Vector3 right)
{
return new Vector3
{
X = left.X - right.X,
Y = left.Y - right.Y,
Z = left.Z - right.Z,
};
}

public static Vector3 ReadVector3(Stream reader)
{
Vector3 vector3 = new()
Expand Down
2 changes: 1 addition & 1 deletion src/G2DataGUI.Common/Data/Maps/Maps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private void ReadMaps()
CollectionRefreshed?.Invoke(this, EventArgs.Empty);

using var modelFile = File.Open(
"F:\\programming\\languages\\c#\\programs\\G2DataGUI\\build\\Debug\\net7.0\\models\\2a00.chr_model_1.nj",
"F:\\projects\\G2DataGUI\\build\\Debug\\net7.0\\models\\2a00.chr_model_1.nj",
FileMode.Open,
FileAccess.Read);
NJCM model = NJCM.ReadNJCM(modelFile, 0);
Expand Down
23 changes: 13 additions & 10 deletions src/G2DataGUI.Common/Data/Models/NJSBone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,42 @@ namespace G2DataGUI.Common.Data.Models;
public class NJSBone
{
public uint Flags { get; set; }
public NJSMesh Model { get; set; } = null;
public NJSBone Parent { get; set; }
public NJSMesh Mesh { get; set; } = null;
public Vector3 Position { get; set; }
public Vector3 Angle { get; set; }
public Vector3 Scale { get; set; }
public NJSBone Child { get; set; } = null;
public NJSBone Sibling { get; set; } = null;
public Vector3 AbsolutePosition => Parent is not null ? Position + Parent.AbsolutePosition : Position;

public static NJSBone ReadNJSObject(Stream reader, long instanceOffset)
public static NJSBone ReadNJSObject(Stream reader, long instanceOffset, NJSBone parent = null)
{
NJSBone obj = new()
NJSBone bone = new()
{
Flags = reader.ReadRawUInt(),
Parent = parent,
};

var modelOffset = reader.ReadRawUInt();
if (modelOffset > 0)
{
long position = reader.Position;
reader.Seek(instanceOffset + modelOffset, SeekOrigin.Begin);
obj.Model = NJSMesh.ReadNJSModel(reader, instanceOffset);
bone.Mesh = NJSMesh.ReadNJSModel(reader, instanceOffset, bone);
reader.Seek(position, SeekOrigin.Begin);
}

obj.Position = Vector3.ReadVector3(reader);
obj.Angle = Vector3.ReadVector3(reader);
obj.Scale = Vector3.ReadVector3(reader);
bone.Position = Vector3.ReadVector3(reader);
bone.Angle = Vector3.ReadVector3(reader);
bone.Scale = Vector3.ReadVector3(reader);

var childOffset = reader.ReadRawUInt();
if (childOffset > 0)
{
long position = reader.Position;
reader.Seek(instanceOffset + childOffset, SeekOrigin.Begin);
obj.Child = ReadNJSObject(reader, instanceOffset);
bone.Child = ReadNJSObject(reader, instanceOffset, bone);
reader.Seek(position, SeekOrigin.Begin);
}

Expand All @@ -52,10 +55,10 @@ public static NJSBone ReadNJSObject(Stream reader, long instanceOffset)
{
long position = reader.Position;
reader.Seek(instanceOffset + siblingOffset, SeekOrigin.Begin);
obj.Sibling = ReadNJSObject(reader, instanceOffset);
bone.Sibling = ReadNJSObject(reader, instanceOffset, bone);
reader.Seek(position, SeekOrigin.Begin);
}

return obj;
return bone;
}
}
10 changes: 7 additions & 3 deletions src/G2DataGUI.Common/Data/Models/NJSMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ public class NJSMesh
// indices
public Vector3 Center { get; set; }
public float Radius { get; set; }
public NJSBone ParentBone { get; set; }

public static NJSMesh ReadNJSModel(Stream reader, long instanceOffset)
public static NJSMesh ReadNJSModel(Stream reader, long instanceOffset, NJSBone parentBone)
{
NJSMesh model = new();
NJSMesh model = new()
{
ParentBone = parentBone,
};

var vertexOffset = reader.ReadRawUInt();
if (vertexOffset > 0)
{
var position = reader.Position;
reader.Seek(instanceOffset + vertexOffset, SeekOrigin.Begin);
model.VertexList = NJSVertexList.ReadNJSVertexList(reader);
model.VertexList = NJSVertexList.ReadNJSVertexList(reader, parentBone);
reader.Seek(position, SeekOrigin.Begin);
}

Expand Down
9 changes: 9 additions & 0 deletions src/G2DataGUI.Common/Data/Models/NJSVertex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using G2DataGUI.Common.Data.Common;

namespace G2DataGUI.Common.Data.Models;
public class NJSVertex
{
public Vector3 Position { get; set; }
public Vector3 AbsolutePosition => Position + ParentBone.AbsolutePosition;
public NJSBone ParentBone { get; set; }
}
9 changes: 6 additions & 3 deletions src/G2DataGUI.Common/Data/Models/NJSVertexList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class NJSVertexList
/// <summary>
/// Should end with a 0xFF000000?
/// </summary>
public List<Vector3> VertexList { get; set; } = new List<Vector3>();
public List<NJSVertex> VertexList { get; set; } = new List<NJSVertex>();

public static NJSVertexList ReadNJSVertexList(Stream reader)
public static NJSVertexList ReadNJSVertexList(Stream reader, NJSBone parentBone)
{
NJSVertexList list = new()
{
Expand All @@ -44,7 +44,10 @@ public static NJSVertexList ReadNJSVertexList(Stream reader)

for (int index = 0; index < list.Count; index++)
{
list.VertexList.Add(Vector3.ReadVector3(reader));
list.VertexList.Add(new NJSVertex{
Position = Vector3.ReadVector3(reader),
ParentBone = parentBone,
});
}

return list;
Expand Down

0 comments on commit ec1796f

Please sign in to comment.