From 31e39b3308fb425ab70ab8d386c6db09018ae051 Mon Sep 17 00:00:00 2001 From: KillzXGaming Date: Sun, 9 Oct 2022 13:17:09 -0400 Subject: [PATCH] More adjustments to fix potential errors --- .../FileFormats/MT/Model/MaterialList.cs | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/File_Format_Library/FileFormats/MT/Model/MaterialList.cs b/File_Format_Library/FileFormats/MT/Model/MaterialList.cs index 2fbeb3bd..16cb6530 100644 --- a/File_Format_Library/FileFormats/MT/Model/MaterialList.cs +++ b/File_Format_Library/FileFormats/MT/Model/MaterialList.cs @@ -83,11 +83,11 @@ namespace CafeLibrary.M2 for (int j = 0; j < parameters.Length; j++) { if (parameters[j].ToString().StartsWith("34397848")) //tAlbedoMap - material.DiffuseMap = this.Textures[(int)parameters[j - 1] - 1]; + material.DiffuseMap = FindTextureParam(parameters, j); if (parameters[j].ToString().StartsWith("577110")) //tNormalMap - material.NormalMap = this.Textures[(int)parameters[j - 1] - 1]; - // if (parameters[j].ToString().StartsWith("24862")) //tSpecularMap - // material.SpecularMap = this.Textures[(int)parameters[j - 1] - 1]; + material.NormalMap = FindTextureParam(parameters, j); + if (parameters[j].ToString().StartsWith("24862")) //tSpecularMap + material.SpecularMap = FindTextureParam(parameters, j); } } else @@ -96,17 +96,33 @@ namespace CafeLibrary.M2 for (int j = 0; j < parameters.Length; j++) { if (parameters[j].ToString().StartsWith("34397848")) //tAlbedoMap - material.DiffuseMap = this.Textures[(int)parameters[j - 1] - 1]; + material.DiffuseMap = FindTextureParam(parameters, j); if (parameters[j].ToString().StartsWith("5771109")) //tNormalMap - material.NormalMap = this.Textures[(int)parameters[j - 1] - 1]; - // if (parameters[j].ToString().StartsWith("2486240")) //tSpecularMap - // material.SpecularMap = this.Textures[(int)parameters[j - 1] - 1]; + material.NormalMap = FindTextureParam(parameters, j); + if (parameters[j].ToString().StartsWith("2486240")) //tSpecularMap + material.SpecularMap = FindTextureParam(parameters, j); } } } } } + private Texture FindTextureParam(ulong[] parameters, int id) + { + var index = (uint)parameters[id - 1] - 1; + if (index < Textures.Count) //Index before texture hash. Sometimes not always the case? + return Textures[(int)index]; + return null; + } + + private Texture FindTextureParam(uint[] parameters, int id) + { + var index = parameters[id - 1] - 1; + if (index < Textures.Count) + return Textures[(int)index]; + return null; + } + private uint ReadUint(FileReader reader) { if (_hasUint64Offsets)