From e9751d0cd01bfcb89c11022e98e45e4dab2fabd1 Mon Sep 17 00:00:00 2001
From: KillzXGaming <millernathan35@gmail.com>
Date: Wed, 29 Jan 2020 20:01:33 -0500
Subject: [PATCH] Add .mod stuff

---
 .../FileFormats/Pikmin1/MOD.cs                | 25 ++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/File_Format_Library/FileFormats/Pikmin1/MOD.cs b/File_Format_Library/FileFormats/Pikmin1/MOD.cs
index 0a39d5c0..be02d698 100644
--- a/File_Format_Library/FileFormats/Pikmin1/MOD.cs
+++ b/File_Format_Library/FileFormats/Pikmin1/MOD.cs
@@ -90,6 +90,8 @@ namespace FirstPlugin
         private Vector3[] Vertices;
         private Vector3[] VertexNormals;
         private Vector4[] Colors;
+        private Envelope[] Envelopes;
+
         private enum ChunkNames
         {
             Header,
@@ -152,7 +154,7 @@ namespace FirstPlugin
 
                 string[] JointNames = new string[0];
                 Joint[] Joints = new Joint[0];
-                Envelope[] Envelopes = new Envelope[0];
+                Envelopes = new Envelope[0];
                 while (reader.EndOfStream == false)
                 {
                     long chunkStart = reader.Position;
@@ -219,6 +221,18 @@ namespace FirstPlugin
                 }
                 Skeleton.reset();
                 Skeleton.update();
+
+                foreach (var mesh in Renderer.Meshes)
+                {
+                    for (int v = 0; v < mesh.vertices.Count; v++) {
+                        var vertex = mesh.vertices[v];
+                        if (vertex.boneIds.Count == 1)
+                        {
+                            var transform = Skeleton.bones[vertex.boneIds[0]].Transform;
+                            vertex.pos = Vector3.TransformPosition(vertex.pos, transform);
+                        }
+                    }
+                }
             }
         }
 
@@ -365,6 +379,15 @@ namespace FirstPlugin
                                         pos = Vertices[vtxIdx]
                                     };
 
+                                    int envIdx = 0;
+                                    if (Envelopes.Length > envIdx)
+                                    {
+                                        for (int i = 0; i < Envelopes[envIdx].Indices?.Length; i++) {
+                                            newVertex.boneIds.Add(Envelopes[envIdx].Indices[i]);
+                                            newVertex.boneWeights.Add(Envelopes[envIdx].Weights[i]);
+                                        }
+                                    }
+
                                     if (VertexNormals != null)
                                         newVertex.nrm = VertexNormals[nrmIdx];
                                     if (Colors != null)