- Home /
Issue with OnAssignMaterialModel and meshes split by Unity
Hello all,
I'm having an issue where implementing my own AssetPostprocessor.OnAssignMaterialModel() method is causing an issue on models with > 64k verts. I have searched high and low with no luck, as well as posted a thread at the Unity forums, but it hasn't been responded to after a few days. The issue that I'm seeing is that when the model is split by Unity, and I use my AssetPostprocessor.OnAssignMaterialModel() method, only the last sub-mesh has the material applied. The reason I need to support models that may be split by Unity on import is because this I'm building an automated process, and mesh size upon import is not guaranteed.
If my model has less than 64k verts this code works. If there is something else I can do, short of recursing through and manually setting a shared material on all split meshes after import to account for this, I would love to hear about it. Apparently the default functionality of Unity accounts for this, so I'm not sure what I'm doing differently.
It is very possible that I'm doing something very wrong, but unless I'm missing something the documentation on this functionality is quite lacking. Any help would be greatly appreciated!
This is what my code looks like:
Material OnAssignMaterialModel(Material material, Renderer renderer)
{
string materialPath = "Assets/Materials" + material.name + ".mat";
//if the materials folder does not exist let's create it
string pathToCheck = Application.dataPath + "/Materials";
if(System.IO.Directory.Exists(pathToCheck) == false)
{
AssetDatabase.CreateFolder("Assets", "Materials");
}
DebugLogger.WriteToLog(DebugLogger.EDebugLevel.DEBUG_INFO, "Material.Name = " + material.name);
// Find if there is a material at the material path
// Turn this off to always regeneration materials
if (AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)))
{
DebugLogger.WriteToLog(DebugLogger.EDebugLevel.DEBUG_INFO, material.name + " already exists");
return AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
}
// assign the proper shader based on textures found for the material.
string mainTexture = System.IO.Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(material.mainTexture));
//Get the name of the main texture without the suffix
string[] mainTextureSplit = mainTexture.Split('_');
string mainTextureSansSuffix = "";
if(mainTextureSplit[mainTextureSplit.Length - 1] == "d")
{
//loop through adding everything except the first split back
for( int i = 0; i < mainTextureSplit.Length - 1; ++i)
{
// add underscores back where needed
if(i != 0)
{
mainTextureSansSuffix += "_";
}
mainTextureSansSuffix += mainTextureSplit[i];
}
}
else
{
DebugLogger.WriteToLog(DebugLogger.EDebugLevel.DEBUG_WARN,
"ImporterOverride.OnAssignMaterialModel() : mainTexture is missing or wrong naming convention!");
}
// Default the shader to Diffuse
material.shader = Shader.Find("Diffuse");
//do we have a properly named main texture
if(mainTextureSansSuffix != "")
{
string bumpMap = mainTextureSansSuffix + "_n" + System.IO.Path.GetExtension(AssetDatabase.GetAssetPath(material.mainTexture));
bumpMap = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(material.mainTexture)), bumpMap);
string specMap = mainTextureSansSuffix + "_s" + System.IO.Path.GetExtension(AssetDatabase.GetAssetPath(material.mainTexture));
specMap = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(material.mainTexture)), specMap);
Texture2D textureBump = AssetDatabase.LoadAssetAtPath(bumpMap, (typeof(Texture2D))) as Texture2D;
Texture2D textureSpec = AssetDatabase.LoadAssetAtPath(specMap, (typeof(Texture2D))) as Texture2D;
// if we have both a bumpmap and specmap then we need the appropriate shader
if(textureBump != null && textureSpec != null)
{
material.shader = Shader.Find("Bumped Color Specular");
material.SetTexture("_BumpMap", textureBump);
material.SetTexture("_SpecMap", textureSpec);
}
else if(textureBump != null) // just bump diffuse
{
material.shader = Shader.Find("Bumped Diffuse");
material.SetTexture("_BumpMap", textureBump);
}
else if(textureSpec != null) // just specular
{
material.shader = Shader.Find("Specular");
//material.SetTexture("_SpecMap", textureSpec);
}
}
//Add material to the AssetDatabase and return the material
AssetDatabase.CreateAsset(material, "Assets/Materials/" + material.name + ".mat");
return material;
}
Hi, I was wondering of you ever found a solution to this issue. I'm having the same problem when trying to import a model with multiple submeshes, only the last mesh gets the material assigned. $$anonymous$$y code is pretty similar.
Your answer
Follow this Question
Related Questions
Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer
Vertex colours not blending across triangle face 1 Answer
Awarding 500 points if we manage to do it. How to import mesh over 400 000 verts into unity 3 Answers
minimize vertex artifacting for procedural mesh manipulations - vertex collision same object? 0 Answers
Spherical Terraing LOD 0 Answers