Skip to content

Instantly share code, notes, and snippets.

@gekidoslair
Created February 18, 2020 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gekidoslair/dfcfd8b3a685e9e5cff605af6b543a10 to your computer and use it in GitHub Desktop.
Save gekidoslair/dfcfd8b3a685e9e5cff605af6b543a10 to your computer and use it in GitHub Desktop.
Add textures to a material based on a naming convention
/// <summary>
/// Grab the end of the string for our naming convention and see if it matches a known convention
/// </summary>
/// <param name="texture"></param>
/// <param name="material"></param>
private static void SetTexture(Texture2D texture, Material material)
{
var split = texture.name.Split('_');
var idx = split.Length - 1;
// Debug.Log("Postfix for texture: " + texture.name + " : " + split[idx]);
var suffix = split[idx].ToLower();
switch(suffix)
{
case "normal":
case "nrm":
{
// make sure the texture is a normal map
SetTextureForProperty("_BumpMap", texture, material);
break;
}
case "ao":
case "occlusion":
{
SetTextureForProperty("_OcclusionMap", texture, material);
break;
}
case "metallic":
case "metallicalpha":
{
SetTextureForProperty("_MetallicGlossMap", texture, material);
break;
}
case "diffuse":
case "basecolor":
{
SetTextureForProperty("_MainTex", texture, material);
break;
}
case "hdrp":
{
SetTextureForProperty("_MaskMap", texture, material);
break;
}
case "roughness":
{
SetTextureForProperty("_SpecGlossMap", texture, material);
break;
}
case "specular":
{
SetTextureForProperty("_SpecGlossMap", texture, material);
break;
}
default:
{
Debug.Log("Unknown texture for material: " + texture.name + " suffix: " + suffix);
break;
}
}
//var textPropNames = material.GetTexturePropertyNames();
//foreach (var textProp in textPropNames)
//{
// Log("Found texture property: " + textProp);
//}
}
/// <summary>
/// For the specified material, set the given property with the provided texture
/// </summary>
/// <param name="textProp">property you want to map</param>
/// <param name="texture">the texture you want to assign</param>
/// <param name="material">the material you are working with</param>
private static void SetTextureForProperty(string textProp, Texture2D texture, Material material)
{
Log("property: " + textProp + " texture: " + texture.name);
material.SetTexture(textProp, texture);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment