- Home /
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:
 
 
But in the sphere something goes very wrong:
 
 
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
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. 
just go to 3ds max, c4d, blender or whatever, create new sphere, export it to .fbx and load in unity
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.
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)
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];
                     }
                 }
             }
         }
Your answer
 
 
             Follow this Question
Related Questions
How would you go about shaping terrain with scripting? (C#) 1 Answer
Bullet bounces off the wall 2 Answers
Trouble with Null Reference in Script 2 Answers
Having Trouble with Vector3.Angle 1 Answer
Array index is out of range 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                