- Home /
Adding Verticies to Mesh
Hello I am looking for some help on mesh generation. Here is my code using UnityEngine; using System.Collections; using System.Collections.Generic; [RequireComponent (typeof (MeshFilter))] [RequireComponent (typeof (MeshRenderer))] [RequireComponent (typeof (MeshCollider))] public class ChunkControll : MonoBehaviour {
public int chunkSize = 20;
public float tileSize = 1f;
//classes used by chunk generator
class Tile {
public Vector3[] v = new Vector3[4];
public Vector3[] n = new Vector3[4];
public int[] t = new int[6];
public byte transformation;
};
// Function returns one quad
Tile MakeTile (int x, int y){
Tile newTile = new Tile();
newTile.v[0] = new Vector3 (x*tileSize,y*tileSize);
newTile.v[1] = new Vector3 ((x+1)*tileSize,y*tileSize);
newTile.v[2] = new Vector3 (x*tileSize,(y+1)*tileSize);
newTile.v[3] = new Vector3 ((x+1)*tileSize,(y+1)*tileSize);
newTile.n[0] = Vector3.up;
newTile.n[1] = Vector3.up;
newTile.n[2] = Vector3.up;
newTile.n[3] = Vector3.up;
newTile.t[0]= 0;
newTile.t[1]= 3;
newTile.t[2]= 1;
newTile.t[3]= 0;
newTile.t[4]= 2;
newTile.t[5]= 3;
newTile.transformation = 0;
return (newTile);
}
// function populates a chunk with quads
Tile[,] MakeNewChunk (int offset_x, int offset_y){
int i;
int j;
int x = offset_x * chunkSize;
int y = offset_y * chunkSize;
Tile[,] chunk = new Tile [chunkSize,chunkSize];
for(i=0;i<chunkSize;i++){
for(j=0;j<chunkSize;j++){
chunk [j+x,i+y] = MakeTile(j+x,i+y);
}
}
return chunk;
}
void Start () {
List<Tile[,]> list = new List<Tile[,]>(); // list of chunks
list.Add (MakeNewChunk (0,0)); // adds chunk to the list of chunks
MeshFilter mf = GetComponent<MeshFilter> ();
Mesh mesh = new Mesh ();
mf.mesh = mesh;
mesh.vertices = list [0] [0, 0].v; //list [0] [0, 0].v acces to the Verticies table in Tile struct
}
}
I am looking ot feed the mesh.verticies variable verticies from a loop but I do not know if that is a valid way of going about things.(personally I think I will just override previous verticies becouse it just can't be that simple right?) This code does not generate any errors at least not any that I can see.
From the example above I want to Generate chunks that are about 20x20 in size each quad is created separately with its own separate normals and verticies.
Function that applies texture is separate from this script. I was trying to get game logic and graphics to be separate from each other.
Later on in the script I will want to generate more chunks depending on where the camera location is that is why I need the list there. The game will be 2D top down grid based economy-sim.
I think that the proper awnser wold be with combining meshes using mesh.CombineMeshes that way I could have mesh1 and mesh2 add both to mesh1 and then override mesh2 and add them again. tell me what you think. I know it is not optimal solution but I'm a begginer and just downloaded Unity 3 days ago.
Answer by Jahith · Feb 17, 2014 at 10:07 PM
Okay I managed to find a satisfying awnser.
Mesh ConsolidateMesh (Tile[,] MeshTable){
Mesh mesh1 = new Mesh ();
Mesh mesh2 = new Mesh ();
int i, j;
CombineInstance[] TBC = new CombineInstance[chunkSize*chunkSize];
for (i=0; i<chunkSize; i++) {
for (j=0; j<chunkSize; j++) {
mesh2.vertices = MeshTable[j,i].v;
mesh2.normals = MeshTable[j,i].n;
mesh2.triangles = MeshTable[j,i].t;
TBC[j+(i*chunkSize)].mesh = mesh2;
}
}
mesh1.CombineMeshes (TBC);
return mesh1;
}
I wrote this function to handle the mesh consolidation. I couldn't find any easy to follow examples of the use of mesh.CombineMeshes It actually looked very scary but all it needed was an array of structures that held meshes (CombineInstance[] TBC in my example) using that function causes Tile.transformation to become useless and I need that for my graphic scripts so I will call ConsolidateMesh after I apply graphics to my tiles.
I still could use an opinion on wether or not I should worry about optimization. I will not be running many chunks as defined above I'm happy with 20x20 for my game and I doubt that the player will generate more than 20 chunks so about 16000triangles.
Your answer
Follow this Question
Related Questions
Create 3D mesh from PNG 0 Answers
Creating a mesh and assigning a material during edit time. 1 Answer
Tile-based approach in Unity 1 Answer
Mesh not being generated at all [Solved] 1 Answer
Draw/generate shape as 2D sprite? 0 Answers