Hey guys. First post.
I’ve been following along with this tutorial, parts one and two specifically (I didn’t go into the 3D stuff because I’m not making a voxel-based game).
I need some help with “GenTerrain” and “BuildMesh”, because in his tutorial, Alexandros is generating based on math formulas and I wish to generate from 4D array or vector data. For example, an XYAB array where X and Y are the two-dimensional coordinates and A and B are tile coordinates on the texture. In the tutorial, he selects what tile it is from a Vector2.
I’m not trying to ask anyone to write me code here, I just need a push in the right direction and maybe a link or two to some tutorials or videos, or any advice you think would be useful.
Here is Alexandros’s final code, and my code so far, it differs a little bit from the tutorial because, again, I wasn’t planning on building a voxel game:
My Code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TileEngine : MonoBehaviour {
public List<Vector3> newVertices = new List<Vector3>();
public List<Vector3> colVertices = new List<Vector3>();
public List<Vector2> newUV = new List<Vector2>();
public List<int> newTriangles = new List<int>();
public List<int> colTriangles = new List<int>();
public byte[,] blocks;
private Mesh mesh;
private MeshCollider col;
private int squareCount;
private int colCount;
private float tUnit = 0.50f;
private Vector2 tGrassland = new Vector2 (0, 1);
private Vector2 tForest = new Vector2 (1, 1);
private Vector2 tMountain = new Vector2 (0, 0);
private Vector2 tHills = new Vector2 (1, 0);
void Start () {
mesh = GetComponent<MeshFilter> ().mesh;
col = GetComponent<MeshCollider> ();
GenTerrain ();
BuildMesh ();
UpdateMesh ();
}
void Update () {
}
void GenTerrain () {
blocks = new byte [10, 10];
}
void BuildMesh () {
}
void ColliderTriangles () {
colTriangles.Add (colCount * 4);
colTriangles.Add ((colCount * 4) + 1);
colTriangles.Add ((colCount * 4) + 3);
colTriangles.Add ((colCount * 4) + 1);
colTriangles.Add ((colCount * 4) + 2);
colTriangles.Add ((colCount * 4) + 3);
}
void GenCollider(int x, int y){
colVertices.Add (new Vector3 (x, y, 1));
colVertices.Add (new Vector3 (x + 1, y, 1));
colVertices.Add (new Vector3 (x + 1, y, 0));
colVertices.Add (new Vector3 (x, y, 0));
ColliderTriangles ();
colCount++;
colVertices.Add (new Vector3 (x, y - 1, 0));
colVertices.Add (new Vector3 (x + 1, y - 1, 0));
colVertices.Add (new Vector3 (x + 1, y - 1, 1));
colVertices.Add (new Vector3 (x, y - 1, 1));
ColliderTriangles ();
colCount++;
colVertices.Add (new Vector3 (x, y - 1, 1));
colVertices.Add (new Vector3 (x, y, 1));
colVertices.Add (new Vector3 (x, y, 0));
colVertices.Add (new Vector3 (x, y - 1, 0));
ColliderTriangles ();
colCount++;
colVertices.Add (new Vector3 (x + 1, y, 1));
colVertices.Add (new Vector3 (x + 1, y - 1, 1));
colVertices.Add (new Vector3 (x + 1, y - 1, 0));
colVertices.Add (new Vector3 (x + 1, y, 0));
ColliderTriangles ();
colCount++;
}
void GenSquare(int x, int y, Vector2 texture){
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));
newTriangles.Add (squareCount*4);
newTriangles.Add ((squareCount*4)+1);
newTriangles.Add ((squareCount*4)+3);
newTriangles.Add ((squareCount*4)+1);
newTriangles.Add ((squareCount*4)+2);
newTriangles.Add ((squareCount*4)+3);
newUV.Add (new Vector2 (tUnit * texture.x, tUnit * texture.y + tUnit));
newUV.Add (new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y + tUnit));
newUV.Add (new Vector2 (tUnit * texture.x + tUnit, tUnit * texture.y));
newUV.Add (new Vector2 (tUnit * texture.x, tUnit * texture.y));
squareCount++;
}
void UpdateMesh () {
mesh.Clear ();
mesh.vertices = newVertices.ToArray ();
mesh.triangles = newTriangles.ToArray ();
mesh.uv = newUV.ToArray ();
mesh.Optimize ();
mesh.RecalculateNormals ();
newVertices.Clear ();
newTriangles.Clear ();
newUV.Clear ();
squareCount = 0;
Mesh newMesh = new Mesh ();
newMesh.vertices = colVertices.ToArray ();
newMesh.triangles = colTriangles.ToArray ();
col.sharedMesh = newMesh;
colVertices.Clear ();
colTriangles.Clear ();
colCount = 0;
}
}