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
1
Question by Reinhold · May 07, 2014 at 02:35 PM · meshimportblendertriangles

How do i avoid shared vertices?

Hello,

i made a subdivided Icosahedron in Blender and split it at all edges so that i was left with zero shared vertices. I triple checked that.

Now, i imported that into Unity and am creating "continents" by assigning UV-coordinates to the vertices. I do that by going through each triangle in the triangles-array, find the three corresponding vertices, and assign three equal UV-values to them. That combined with some Perlin-Noise leaves me with the desired effect.

However, i noticed some strange behaviour at the "edges" of the continents, resulting in gradients that should not be there. When checking the UV-coordinates and vertex-indices of the triangles at runtime, i got the following result:

alt text

At the top, the selected triangle (with the white border) consists out of the vertex-indices

P0(4210)

P1(4211)

P2(1251)

The one at the bottom out of

P0(1250)

P1(4210)

P2(1251)

with two vertices (4210 and 1251)and thus their UV-coordinates (0.5, 0.8) being shared, resulting in the intended water-triangle having two vertices with green UV-coordinates, which is not what i want.

So my question is, how do i avoid my icosphere having shared vertices? I checked again in Blender, but it is all just how i want it there, with no vertices shared. Is this a problem with Unity altering (optimizing) the mesh when importing, or what did i do wrong to not have the mesh imported with no shared vertices?

Thanks in advance

shared.png (54.2 kB)
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

3 Replies

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

Answer by DMGregory · May 07, 2014 at 03:08 PM

I think the answer may be Mesh.Optimize:

For imported models you should never call this as the import pipeline already does it for you.

I suspect that Unity is automatically merging identical vertices when you import the mesh.

Here's one way to hack around it...

 int[] sourceIndices = sourceMesh.GetTriangles(0);
 Vector3[] sourceVerts = sourceMesh.vertices;
 Vector2[] sourceUVs = sourceMesh.uv;
 
 int[] newIndices = new int[sourceIndices.Length];
 Vector3[] newVertices = new Vector3[sourceIndices.Length];
 Vector2[] newUVs = new Vector2[sourceIndices.Length];
 
 // Create a unique vertex for every index in the original Mesh:
 for(int i = 0; i < sourceIndices.Length; i++)
 {
    newIndices[i] = i;
    newVertices[i] = sourceVertices[sourceIndices[i]];
    newUVs[i] = sourceUVs[sourceIndices[i]];
 }
 
 Mesh unsharedVertexMesh = new Mesh();
 unsharedVertexMesh.vertices = newVertices;
 usharedVertexMesh.uv = newUVs;
 
 unsharedVertexMesh.SetTriangles(newIndices, 0);

What this does is create a new mesh using the same triangle indices as the original, but with unique copies of every vertex. As long as you don't call Optimize() on it, Unity should leave this the way you want it.

(Note that the code above assumes the mesh contains only one submesh)

Comment
Add comment · Show 1 · 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 Reinhold · May 07, 2014 at 03:31 PM 0
Share

Thanks a lot man.

This worked perfectly. Interesting to see that you see$$anonymous$$gly cant stop Unity from optimizing the mesh on import though. (even if you uncheck "Optimize $$anonymous$$esh")

Cheers!

avatar image
1

Answer by HsinChien · Apr 02, 2016 at 06:11 PM

enhanced code that supports sub-meshes

         mesh=GetComponent<MeshFilter>().mesh;
 
         // unshare verts
         int subMeshCnt=mesh.subMeshCount;
         int[] tris=mesh.triangles;
         int triCnt=mesh.triangles.Length;
         int[] newTris=new int[triCnt];
         Vector3[] sourceVerts = mesh.vertices;
         Vector3[] sourceNorms = mesh.normals;
         Vector2[] sourceUVs = mesh.uv;
 
         Vector3[] newVertices = new Vector3[triCnt];
         Vector3[] newNorms = new Vector3[triCnt];
         Vector2[] newUVs = new Vector2[triCnt];
 
         int offsetVal=0;
 
         for (int k=0; k<subMeshCnt; k++)
         {
             int[] sourceIndices = mesh.GetTriangles(k);
 
             int[] newIndices = new int[sourceIndices.Length];
 
             // Create a unique vertex for every index in the original Mesh:
             for(int i = 0; i < sourceIndices.Length; i++)
             {
                 int newIndex=sourceIndices[i];
                 int iOffset=i+offsetVal;
                 newIndices[i] = iOffset;
                 newVertices[iOffset] = sourceVerts[newIndex];
                 newNorms[iOffset]=sourceNorms[newIndex];
                 newUVs[iOffset] = sourceUVs[newIndex];
             }
             offsetVal+=sourceIndices.Length;
 
             mesh.vertices = newVertices;
             mesh.normals=newNorms;
             mesh.uv = newUVs;
 
             mesh.SetTriangles(newIndices, k);
         }
 
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.Optimize(); 
 
Comment
Add comment · Show 1 · 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 ed7k · Sep 05, 2016 at 02:52 PM 0
Share

Thanks for sharing, I found this extremely useful :)

avatar image
0

Answer by _dns_ · May 07, 2014 at 03:13 PM

Hi, I think you need to have submeshes to have different UV for the same vertex. Unity mesh has 1 UV for each vertex (and UV2 but that's something different). Check the documentation about submeshes, GetTopology etc... This is basically a mesh composed of multiple meshes. The interesting part is that each submesh can have its own material and then it's own set of UV, but some vertices will be duplicated (shared vertices won't be anymore between multiple submeshes)

In Blender, you have to assign multiple materials to your mesh so that Unity imports multiple materials and creates submeshes. This involve marking "seam" in the UV editing, I did this long time ago and don't remember exactly how, you can find tutorials about assigning materials on youtube.

Comment
Add comment · Show 3 · 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 Reinhold · May 07, 2014 at 03:34 PM 0
Share

Yep, i wanted to avoid submeshes though for a few reasons, mainly because i did not want additional draw calls and also because changing UVs is quick and easy.

avatar image Owen-Reynolds · May 07, 2014 at 03:54 PM 0
Share

You're confusing $$anonymous$$aterials and texCoords. $$anonymous$$ultiple materials require submeshes. $$anonymous$$aking every single face into a submesh would work, but would cost a lot of draw calls.

As you note, when two faces share a vert, they can't each assign a different tex-coord to it. Verts get UVs, not faces, and they only get one per unwrap. So, the trick is, you split the vertex (either click "Split" or add a seam. A seam says "split these later.") Now each face can assign a different UV coord to "its" vert.

avatar image _dns_ · May 07, 2014 at 04:05 PM 0
Share

hummm, ok, I should have read the question one more time, submeshes is not what you needed, my mistake.

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh Import Transform Problems 0 Answers

mesh from blender twinkle on unity 0 Answers

Object Colliders 2 Answers

Importing .blend file - Missing Animation 1 Answer

Issue in blender mesh composition import 1 Answer


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