Basically, I have a folder of png files and allow the user to create materials from them at runtime. The way I’m doing that is looping through all PNG files in the specified folder and if they contain “_base” load that in. Next I want to see if the files contain the base texture’s name AND the extention “metallic”, “normal”, etc.
My code seems extremely inefficient with a bunch of loops and I’m just looking for a better way of doing it. Maybe using Linq? I’m not sure!
I would appreciate any and all pointers!
Thanks so much!
public IEnumerator initializeCustomTextureLibrary()
{
Debug.Log("INITIALIZING TEXTURES");
textureFilePaths = Directory.GetFiles(texturePath, "*.png", SearchOption.AllDirectories).ToList();
for (int i = 0; i < textureFilePaths.Count; i++)
{
if (textureFilePaths[i].ToLower().Contains("_base")) // if the found texture is the base map...
{
// Get the "prefix" to the material's name and create the material...
string[] fileName = Path.GetFileName(textureFilePaths[i]).Split('_');
Material newMat = new Material(Shader.Find("Universal Render Pipeline/Lit"));
newMat.name = fileName[0];
newMat.mainTexture = HelperMethods.LoadPNG(textureFilePaths[i]);
// Now loop back through and see if any other pngs in the folder contain that prefix plus the suffix of the appropriate map...
foreach(string s in textureFilePaths)
{
if (s.ToLower().Contains(fileName[0]))
{
if(s.ToLower().Contains("_metallic") || s.ToLower().Contains("_specular"))
{
newMat.EnableKeyword("_METALLICGLOSSMAP");
newMat.SetTexture("_MetallicGlossMap", HelperMethods.LoadPNG(s));
}
if (s.ToLower().Contains("_normal") || s.ToLower().Contains("_bump"))
{
newMat.EnableKeyword("_NORMALMAP");
newMat.SetTexture("_BumpMap", HelperMethods.LoadPNG(s));
}
}
}
}
}
Debug.Log("TEXTURES DONE");
yield return null;
}
PS: If anyone knows the shader code for the occlusion map of URP’s standard lit shader, that would be super helpful too!