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.