Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by chase cobb · Jan 30, 2014 at 03:04 PM · meshvertexlimit

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;
     }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jose_KokonutDeveloper · Jan 19, 2017 at 08:03 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges