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 MediaGiant · Jan 23, 2014 at 12:00 PM · importmodelmaterialsfbxcopy

Imported mesh loses material information when assigning to triangles array

Hi all, after importing an FBX model it looks fine with six materials on the same mesh nicely positioned in the right places. But if I do anything with the triangles array the material properties change.

alt text

The statue on the left looks the same after running the following code on it:

 Mesh mesh;
 MeshFilter meshFilter;
 meshFilter = obj.GetComponent(typeof(MeshFilter)) as MeshFilter;
 mesh = meshFilter ? meshFilter.sharedMesh : null;
 Mesh mesh2 = new Mesh();
 mesh2 = mesh;
 //mesh.triangles = mesh.triangles;
 obj.GetComponent<MeshFilter>().sharedMesh = mesh2;

I can modify the vertices and uvs and any other property of the mesh and the materials stay the same but if I uncomment the seventh line then the model looks like the one on the right.

If I had to guess I think what is happening is that Unity is creating a copy of the triangles and then creating a new instance of the object. I've checked and new statue has all six materials applied all over the mesh in layers, which is normal and explained in the docs. The last material is the yellow one so that explains the final look of the model.

The questions then are, what is so different about the new mesh that Unity doesn't preserve the positions the materials? And if Unity can import a model and preserve the material placement where or in what class is this information stored? And can I play with these?

Many thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MediaGiant · Feb 06, 2014 at 03:13 AM

Just in case anybody else is dealing with a similar misunderstanding to mine,

The imported model uses submeshes with a different set of triangles and a separate material for each submesh. To work with the triangles of each submesh we need to use an array of arrays. The following is an example of copying, manipulating and writing the submeshes of a model, which works even if there is only the single mesh.

 // copy submeshes of selected game object
 Mesh mesh = Selection.activeGameObject.GetComponent<MeshFilter>().sharedMesh;
 int[][] copy_triangles = new int[mesh.subMeshCount][];
 for (int i = 0; i < mesh.subMeshCount; i++)
 {
     copy_triangles[i] = new int[mesh.GetTriangles(i).Length];
     copy_triangles[i] = mesh.GetTriangles(i);                    
 }
 
 // do something with the triangle array of each submesh
 int tempInt = 0;
 for (int i = 0; i < mesh.subMeshCount; i++)
 {
     for (int j = 0; j < copy_triangles[i].Length; j += 3)
     {
         // reverse triangle winding
         tempInt = copy_triangles[i][j];
         copy_triangles[i][j] = copy_triangles[i][j+1];
         copy_triangles[i][j+1] = tempInt;
     }
 }
 
 // create a new mesh and add submeshes
 Mesh mesh2 = new Mesh();
 mesh2.subMeshCount = mesh.subMeshCount; // important
 mesh2.vertices = mesh.vertices;
 mesh2.uv = mesh.uv;
 mesh2.normals = mesh.normals;
 for (int i = 0; i < mesh.subMeshCount; i++)
 {
     mesh2.SetTriangles(copy_triangles[i], i);
 }
 mesh2.RecalculateBounds();



Comment
Add comment · Share
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
0

Answer by Eric5h5 · Feb 06, 2014 at 02:32 AM

You need to set the submeshes properly using SetTriangles.

Comment
Add comment · Share
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

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

Imported models question 1 Answer

Replacing an FBX model - I keep having to re-apply all scripts/etc 2 Answers

Blender FBX - Material Generation on Import Weirdness 1 Answer

Getting out mesh filter from imported model 0 Answers

Unity Model Requirements 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