• 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 8NeonBitGuy · Jul 10, 2014 at 06:22 PM · c#meshvector3random

Modify mesh problems

Hello, I've recently asked myself how could I make a random planet and my quick answer was modify the verts of a sphere randomly, but now, it is taking a lot of pain.

after some research this is my code:

     Mesh mesh;
     Vector3[] verts;
     Vector3[] normals;
     Vector3 WorldPosition;
     void Awake(){
         mesh = GetComponent<MeshFilter>().mesh;
         verts = mesh.vertices;
         normals = mesh.normals;
         for (int i=0; i<verts.Length; i++) {
             verts[i] += normals[i] * Random.Range(1f,1.2f);
         }
         mesh.vertices = verts;
         mesh.normals = normals;
         mesh.RecalculateNormals ();
         mesh.RecalculateBounds ();
         mesh.Optimize ();
     }

I use it on a sphere and a plane to check if its correct, in the plane it seems that there is no problem. Plane before and after code:

alt text alt text

But in the sphere something goes very wrong:

alt text alt text

it seems like the verts are duplicating or something weird, I tried Mesh.Clear() but that doesn't work.

Anyone have an idea how I can fix it?

Thanks

plane guay.jpg (191.4 kB)
plane modificado.jpg (194.8 kB)
Comment
Add comment · Show 4
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 robertbu · Jul 10, 2014 at 06:29 PM 0
Share

It appears that Unity's sphere does not always share vertices. I cannot think of a simple solution. Some ideas:

  • Detect all vertices that are equal or nearly equal, then redo the triangle indices so that only one of the equal vertices is used.

  • Detect when vertices are equal or nearly equal and do the same modification you are doing above to both.

  • Construct (or have some construct) a sphere that shares all vertices.

avatar image lukas_balaz · Jul 10, 2014 at 06:39 PM 0
Share

just go to 3ds max, c4d, blender or whatever, create new sphere, export it to .fbx and load in unity

avatar image 8NeonBitGuy · Jul 10, 2014 at 07:07 PM 0
Share

Thanks a lot guys, I tought it was the double vertices and I found a solution to it, first I modify a vert and then I check if there is the same vert.

avatar image lukas_balaz · Jul 11, 2014 at 10:26 AM 0
Share

but I think it is still better to export sphere from c4d, because double vertices can cause more troubles (if you want to do something with mesh)

1 Reply

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

Answer by 8NeonBitGuy · Jul 10, 2014 at 07:56 PM

After thinkin a few hours more this is my solution.

 for (int i=0; i<verts.Length; i++) {
             tempVert=verts[i];
             if(verts[i]!=initVerts[i]){ 
                 Debug.Log ("this vert has been already modified");
                 continue;
             }
             verts[i] += normals[i] * Random.Range(1f,1.2f);
             for(int j=0; j<verts.Length;j++){ 
                 if(i==j){ 
                     Debug.Log ("its the same vert");
                     continue;
                 }else{
                     if(verts[j] == (tempVert)){ 
                         Debug.Log ("Double vert");
                         verts[j]=verts[i];
                     }
                 }
             }
         }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Instantiate object in C# 1 Answer

Procedural array of meshes problem / solved 1 Answer

Few problems with arrays 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges