Use new model joint application in meshbuilder

This commit is contained in:
Jarrod Doyle 2024-12-23 17:51:25 +00:00
parent a8bdf37097
commit 0f9467b8c4
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 6 additions and 61 deletions

View File

@ -120,11 +120,13 @@ public class MeshBuilder
continue; continue;
} }
var model = new ModelFile(modelPath);
model.ApplyJoints(joints);
// TODO: Handle failing to find model more gracefully // TODO: Handle failing to find model more gracefully
var pos = brush.position; var pos = brush.position;
var rot = brush.angle; var rot = brush.angle;
var scale = scaleProp?.value ?? Vector3.One; var scale = scaleProp?.value ?? Vector3.One;
var model = new ModelFile(modelPath);
pos -= model.Header.Center; pos -= model.Header.Center;
// Calculate base model transform // Calculate base model transform
@ -136,65 +138,6 @@ public class MeshBuilder
var transPart = Matrix4x4.CreateTranslation(pos); var transPart = Matrix4x4.CreateTranslation(pos);
var modelTrans = scalePart * rotPart * transPart; var modelTrans = scalePart * rotPart * transPart;
// Calculate base transforms for every subobj (including joint)
var objCount = model.Objects.Length;
var subObjTransforms = new Matrix4x4[objCount];
for (var i = 0; i < objCount; i++)
{
var subObj = model.Objects[i];
var objTrans = Matrix4x4.Identity;
if (subObj.Joint != -1)
{
var ang = float.DegreesToRadians(joints[subObj.Joint]);
// TODO: Is this correct? Should I use a manual rotation matrix?
var jointRot = Matrix4x4.CreateFromYawPitchRoll(0, ang, 0);
objTrans = jointRot * subObj.Transform;
}
subObjTransforms[i] = objTrans;
}
// Build map of objects to their parent id
var parentIds = new int[objCount];
for (var i = 0; i < objCount; i++)
{
parentIds[i] = -1;
}
for (var i = 0; i < objCount; i++)
{
var subObj = model.Objects[i];
var childIdx = subObj.Child;
while (childIdx != -1)
{
parentIds[childIdx] = i;
childIdx = model.Objects[childIdx].Next;
}
}
// Apply sub object transforms + the base object transform to each vertex
for (var i = 0; i < objCount; i++)
{
var subObj = model.Objects[i];
var transform = subObjTransforms[i];
var parentId = parentIds[i];
while (parentId != -1)
{
transform *= subObjTransforms[parentId];
parentId = parentIds[parentId];
}
transform *= modelTrans;
var start = subObj.PointIdx;
var end = start + subObj.PointCount;
for (var j = start; j < end; j++)
{
var v = model.Vertices[j];
model.Vertices[j] = Vector3.Transform(v, transform);
}
}
// for each polygon slam its vertices and indices :) // for each polygon slam its vertices and indices :)
foreach (var poly in model.Polygons) foreach (var poly in model.Polygons)
{ {
@ -202,7 +145,9 @@ public class MeshBuilder
polyVertices.EnsureCapacity(poly.VertexCount); polyVertices.EnsureCapacity(poly.VertexCount);
foreach (var idx in poly.VertexIndices) foreach (var idx in poly.VertexIndices)
{ {
polyVertices.Add(model.Vertices[idx]); var vertex = model.Vertices[idx];
vertex = Vector3.Transform(vertex, modelTrans);
polyVertices.Add(vertex);
} }
AddPolygon(polyVertices, SurfaceType.Solid); AddPolygon(polyVertices, SurfaceType.Solid);