From 85f00ab9d74472a826ab5b5d1fb187a0459ad49d Mon Sep 17 00:00:00 2001
From: ZZYZX <zzyzx@virtual>
Date: Sun, 27 May 2018 21:05:57 +0300
Subject: [PATCH] Added multiple texture support for Unreal models

---
 Source/Core/GZBuilder/md3/ModelReader.cs      | 78 ++++++++++++-------
 Source/Core/Properties/AssemblyInfo.cs        |  4 +-
 .../BuilderModes/Properties/AssemblyInfo.cs   |  2 +-
 3 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/Source/Core/GZBuilder/md3/ModelReader.cs b/Source/Core/GZBuilder/md3/ModelReader.cs
index 7aec30ccf..a3a18bef5 100755
--- a/Source/Core/GZBuilder/md3/ModelReader.cs
+++ b/Source/Core/GZBuilder/md3/ModelReader.cs
@@ -313,20 +313,6 @@ namespace CodeImp.DoomBuilder.GZBuilder.MD3
             stream_d.Position += 12; // padding[12]
 
             long start_d = stream_d.Position;
-            uint numGroups = 0;
-
-            // some black magic
-            bool[] used = new bool[256];
-            for (int i = 0; i < d3d_numpolys; i++)
-            {
-                stream_d.Position = start_d + 16 * i;
-                stream_d.Position += 14;
-                byte texnum = br_d.ReadByte();
-                used[texnum] = true;
-            }
-
-            for (int i = 0; i < 256; i++)
-                if (used[i]) numGroups++;
 
             // read a3d header
             uint a3d_numframes = br_a.ReadUInt16();
@@ -410,27 +396,63 @@ namespace CodeImp.DoomBuilder.GZBuilder.MD3
                 vertices[i].nz = -nsum.z / total;
             }
 
-            // wtf is skin groups?
-            // don't do this for now
+            Dictionary<int, int> textureGroupRemap = new Dictionary<int, int>();
+            int topGrp = 0;
+            for (int i = 0; i < polys.Length; i++)
+            {
+                if (textureGroupRemap.ContainsKey(polys[i].TexNum))
+                    continue;
+                textureGroupRemap[polys[i].TexNum] = topGrp++;
+            }
+
+            if (skins == null)
+            {
+                List<WorldVertex> out_verts = new List<WorldVertex>();
+                List<int> out_polys = new List<int>();
 
-            List<WorldVertex> out_verts = new List<WorldVertex>();
-            List<int> out_polys = new List<int>();
+                for (int i = 0; i < polys.Length; i++)
+                {
+                    for (int j = 0; j < 3; j++)
+                    {
+                        WorldVertex vx = vertices[polys[i].V[j]];
+                        vx.u = polys[i].S[j];
+                        vx.v = polys[i].T[j];
+                        out_polys.Add(out_verts.Count);
+                        out_verts.Add(vx);
+                    }
+                }
 
-            for (int i = 0; i < polys.Length; i++)
+                CreateMesh(device, ref result, out_verts, out_polys);
+                result.Skins.Add("");
+            }
+            else
             {
-                for (int j = 0; j < 3; j++)
+                for (int k = 0; k < topGrp; k++)
                 {
-                    WorldVertex vx = vertices[polys[i].V[j]];
-                    vx.u = polys[i].S[j];
-                    vx.v = polys[i].T[j];
-                    out_polys.Add(out_verts.Count);
-                    out_verts.Add(vx);
+                    List<WorldVertex> out_verts = new List<WorldVertex>();
+                    List<int> out_polys = new List<int>();
+
+                    for (int i = 0; i < polys.Length; i++)
+                    {
+
+                        if (textureGroupRemap[polys[i].TexNum] != k)
+                            continue;
+
+                        for (int j = 0; j < 3; j++)
+                        {
+                            WorldVertex vx = vertices[polys[i].V[j]];
+                            vx.u = polys[i].S[j];
+                            vx.v = polys[i].T[j];
+                            out_polys.Add(out_verts.Count);
+                            out_verts.Add(vx);
+                        }
+                    }
+
+                    CreateMesh(device, ref result, out_verts, out_polys);
+                    result.Skins.Add(skins.ContainsKey(k)?skins[k].ToLowerInvariant():string.Empty);
                 }
             }
 
-            CreateMesh(device, ref result, out_verts, out_polys);
-            result.Skins.Add("");
-
             return result;
         }
 
diff --git a/Source/Core/Properties/AssemblyInfo.cs b/Source/Core/Properties/AssemblyInfo.cs
index 5e1d9080c..fe650852f 100755
--- a/Source/Core/Properties/AssemblyInfo.cs
+++ b/Source/Core/Properties/AssemblyInfo.cs
@@ -30,6 +30,6 @@ using CodeImp.DoomBuilder;
 //      Build Number
 //      Revision
 //
-[assembly: AssemblyVersion("2.3.0.3018")]
+[assembly: AssemblyVersion("2.3.0.3022")]
 [assembly: NeutralResourcesLanguageAttribute("en")]
-[assembly: AssemblyHash("e9c83d0")]
+[assembly: AssemblyHash("634ba54")]
diff --git a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
index d2f49b5cd..aa6799efa 100755
--- a/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
+++ b/Source/Plugins/BuilderModes/Properties/AssemblyInfo.cs
@@ -29,5 +29,5 @@ using System.Resources;
 //      Build Number
 //      Revision
 //
-[assembly: AssemblyVersion("2.3.0.3018")]
+[assembly: AssemblyVersion("2.3.0.3022")]
 [assembly: NeutralResourcesLanguageAttribute("en")]
-- 
GitLab