- Home /
Meshes flipping on procedural generation
Hi,
Im trying to procedural recycle some game objects that i created in a pool of objects. When the camera position goes some distance ahead of the first object it will reconfigure their mesh/position so it can be rendered and positioned ahead of the last object.
I use this method to Set the mesh to its new values:
public void SetMesh(GameObject go,float center,float width,float height,float distance, float floor){
Mesh meshset = go.GetComponent<MeshFilter>().mesh;
meshset.vertices = new Vector3[] {
new Vector3(center-width, floor, 0),
new Vector3(center+width, floor, 0),
new Vector3(center+width, floor+height, 0),
new Vector3(center-width, floor+height, 0)
};
meshset.RecalculateBounds ();
meshset.RecalculateNormals ();
meshset.Optimize ();
go.GetComponent<MeshFilter> ().mesh = meshset;
}
and the method to make the verification of the position of the camera vs position of object with this on update:
void Update () {
float posz = GameObject.Find ("Main Camera").transform.position.z;
if (listab[count2].transform.position.z < posz - 3.0F) {
float lastdistance = listab[count2].transform.position.z;
float x = Random.Range(-5.0F, 5.0F) * 0.1F;
float y = Random.Range(5.0F, 10.0F) * 0.1F;
Barrier novo = new BlueBarrier();
novo.SetMesh(listab[count2],0,x,y,lastdistance + 10.0F, floor);
listab[count2].transform.position += new Vector3(0,0,10.0F);
//listab[count2].transform.Rotate(0, 90, 0);
if(count2==listab.Count-1){ count2 =0;
}
else{ count2++;}
}
What happens and i dont understand why is that as seen in the following images some of them are set right and some are set flipped :
As you can see on the code i have a commented rotation and what it does is that it sets some meshs right, some meshs get flipped and some get rotated. Tried to change from Update to LateUpdate to FixedUpdate and the results are the same. Anyone know how to solve this?
Thank you
Just for clarification: when you generate these meshes, do they always have exactly 4 vertices? When generating meshes procedurally you usually set the vertices, set the triangles and then set the uvs/colors if any.
the code to generate the meshes at first is this:
public $$anonymous$$esh Create$$anonymous$$esh(float center,float width, float height, float distance, float floor)
{
$$anonymous$$esh m = new $$anonymous$$esh();
m.name = name + "$$anonymous$$esh";
m.vertices = new Vector3[] {
new Vector3(center+width, floor, distance),
new Vector3(center-width, floor, distance),
new Vector3(center-width, floor+height, distance),
new Vector3(center+width, floor+height, distance)
};
m.uv = new Vector2[] {
new Vector2 (0, 0),
new Vector2 (0, 1),
new Vector2(1, 1),
new Vector2 (1, 0)
};
m.triangles = new int[] { 0, 1, 2, 0, 2, 3};
m.RecalculateNormals();
m.RecalculateBounds ();
return m;
}
and :
public GameObject Gen(float center,float width, float height, float distance, float floor, int count){
GameObject quad = new GameObject(name + count);
$$anonymous$$eshFilter meshFilter = ($$anonymous$$eshFilter)quad.AddComponent(typeof($$anonymous$$eshFilter));
meshFilter.mesh = Create$$anonymous$$esh(center,width, height, distance, floor);
$$anonymous$$eshRenderer renderer = quad.AddComponent(typeof($$anonymous$$eshRenderer)) as $$anonymous$$eshRenderer;
renderer.material.shader = Shader.Find ("Standard");
Texture2D tex = new Texture2D(1, 1);
tex.SetPixel(0, 0, color);
tex.Apply();
renderer.material.mainTexture = tex;
renderer.material.color = color;
//BoxCollider collider = quad.AddComponent (typeof(BoxCollider)) as BoxCollider;
quad.transform.position = new Vector3 (center, floor, distance);
//count++; // needs to be set outside
return quad;
}
Do you need to regenerate them? Or can you just move the original object?
What i do is use the Gen() method to generate them and when i have to reposition them i use the method Set$$anonymous$$esh() which changes the vertices and transforms its position. What happens is that as you see in the images, some faces are facing the camera and some are flipped or reversed, and they should all be facing the camera
I see that you set the normals with RecalculateNormals(), maybe you should set them manually?
Your answer
Follow this Question
Related Questions
Seam on Procedural Mesh 1 Answer
Procedural Mesh, Strange Clipping 1 Answer
[SOLVED]Procedural generated side by side cubes without 'seams'? 2 Answers