Ok so I’ve been doing little projects in unity like making a fully working car with suspension, headlights, etc. I’m now onto this project where I try to tackle 2D procedural generation. I’ve been using some different tutorials seeing how the code works then writing my own version. I have a plane that is divided into voxels. (The look I’m going for is sorta like terraria). So right now I just have a square plane and I’m trying to figure out how to texture each voxel but its 1 mesh. Basically if I had a plane with 5x5 voxels how would I put a texture on each voxel not one texture on the whole mesh.
My code currently:
public class CreateMesh : MonoBehaviour {
public List<Vector3> newVertices = new List<Vector3> ();
public List<Vector3> newNormals = new List<Vector3> ();
public List<Vector2> newUV = new List<Vector2> ();
public List<int> newTris = new List<int> ();
public int Width;
public int Height;
private int SquareCount = 0;
void GenerateMeshValues() {
for (int y = 0; y < Height; y++) {
for (int x = 0; x < Width; x++) {
newVertices.Add (new Vector3 (x, y, 0));
newVertices.Add (new Vector3 (x + 1, y, 0));
newVertices.Add(new Vector3 (x+1,y-1,0));
newVertices.Add(new Vector3 (x,y-1,0));
newUV.Add (new Vector2 (x / Width, y / Height));
newUV.Add (new Vector2 ((x+1) /Width, y / Height));
newUV.Add (new Vector2 ((x+1) / Width, (y-1) / Height));
newUV.Add (new Vector2 (x / Width, (y-1) / Height));
newTris.Add (0 + (SquareCount * 4));
newTris.Add (1 + (SquareCount * 4));
newTris.Add (2 + (SquareCount * 4));
newTris.Add (0 + (SquareCount * 4));
newTris.Add (2 + (SquareCount * 4));
newTris.Add (3 + (SquareCount * 4));
newNormals.Add (new Vector3 (0, 0, 1));
newNormals.Add (new Vector3 (0, 0, 1));
newNormals.Add (new Vector3 (0, 0, 1));
newNormals.Add (new Vector3 (0, 0, 1));
SquareCount++;
}
}
}
// Use this for initialization
void Start () {
GenerateMeshValues ();
Mesh mesh = new Mesh ();
GetComponent<MeshFilter> ().mesh = mesh;
mesh.vertices = newVertices.ToArray();
mesh.uv = newUV.ToArray();
mesh.triangles = newTris.ToArray();
mesh.normals = newNormals.ToArray();
}
}