diff --git a/KeepersCompound.Lightmapper/MeshBuilder.cs b/KeepersCompound.Lightmapper/MeshBuilder.cs index b552293..1f64e09 100644 --- a/KeepersCompound.Lightmapper/MeshBuilder.cs +++ b/KeepersCompound.Lightmapper/MeshBuilder.cs @@ -120,11 +120,13 @@ public class MeshBuilder continue; } + var model = new ModelFile(modelPath); + model.ApplyJoints(joints); + // TODO: Handle failing to find model more gracefully var pos = brush.position; var rot = brush.angle; var scale = scaleProp?.value ?? Vector3.One; - var model = new ModelFile(modelPath); pos -= model.Header.Center; // Calculate base model transform @@ -136,65 +138,6 @@ public class MeshBuilder var transPart = Matrix4x4.CreateTranslation(pos); 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 :) foreach (var poly in model.Polygons) { @@ -202,7 +145,9 @@ public class MeshBuilder polyVertices.EnsureCapacity(poly.VertexCount); 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);