tile surface from code c#

im aiming to do a tile ground where each tile consists of separate vertices, after too many hours of thinking and trying and not getting my problem solved i decided to come here for help. this is what i have for generating vertices now:

	public int size_x = 50;
	public int size_z = 50;
	public int tileSize = 1;

		int numTiles = size_x * size_z;
		int numTris = numTiles * 2;
		
		int vSize_x = size_x * 2;
		int vSize_z = size_z * 2;
		int numVerts = numTiles * 4;
		
		Vector3[] vertices = new Vector3[numVerts];

		int[] triangles = new int[numTris * 3];
        int tileStartPoint_x;
		int tileStartPoint_z;

		for(tileStartPoint_z = 0; tileStartPoint_z < size_z; tileStartPoint_z++)
		{
			for(tileStartPoint_x = 0; tileStartPoint_x < size_x; tileStartPoint_x++)
			{
				for(int b = 0; b <= 0; b++)
				{
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 0] = new Vector3(tileStartPoint_x, 0, tileStartPoint_z);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 1] = new Vector3(tileStartPoint_x + 1, 0, tileStartPoint_z);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 2] = new Vector3(tileStartPoint_x, 0, tileStartPoint_z + 1);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 3] = new Vector3(tileStartPoint_x + 1, 0, tileStartPoint_z + 1);
				}
			}
		}

i myself at least dont see a problem there, for me it seems like this should generate 50x50 tiles with separate vertices

this is what i have for triangles(this is definitely not complete, i was trying some stuff which is why it is how it is here):

		for(int z = 0; z < size_z; z++)
		{
			for(int x = 0; x < size_x; x++)
			{
				int squareIndex = z * size_x + x;
				int triOffset = squareIndex * 6;
				int s = numVerts/size_x;

				triangles[triOffset + 0] = 0 + x * 4 + z * s;
				triangles[triOffset + 1] = 2 + x * 4 + z * s;
				triangles[triOffset + 2] = 1 + x * 4 + z * s;

				triangles[triOffset + 3] = 1 + x * 4 + z * s;
				triangles[triOffset + 4] = 2 + x * 4 + z * s;
				triangles[triOffset + 5] = 3 + x * 4 + z * s;
			}
		}

this is what happens when i try to make for example 12x12 tiles:

this is the whole script:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]

public class tileMap : MonoBehaviour
{
	public int size_x = 50;
	public int size_z = 50;
	public int tileSize = 1;
	
	void Start()
	{
		buildMesh ();
	}
	
	public void buildMesh()
	{
		int numTiles = size_x * size_z;
		int numTris = numTiles * 2;
		
		int vSize_x = size_x * 2;
		int vSize_z = size_z * 2;
		int numVerts = numTiles * 4;
		
		Vector3[] vertices = new Vector3[numVerts];
		Vector3[] normals = new Vector3[numVerts];
		Vector2[] uv = new Vector2[numVerts];
		
		int[] triangles = new int[numTris * 3];
		
		int tileStartPoint_x;
		int tileStartPoint_z;

		for(tileStartPoint_z = 0; tileStartPoint_z < size_z; tileStartPoint_z++)
		{
			for(tileStartPoint_x = 0; tileStartPoint_x < size_x; tileStartPoint_x++)
			{
				for(int b = 0; b <= 0; b++)
				{
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 0] = new Vector3(tileStartPoint_x, 0, tileStartPoint_z);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 1] = new Vector3(tileStartPoint_x + 1, 0, tileStartPoint_z);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 2] = new Vector3(tileStartPoint_x, 0, tileStartPoint_z + 1);
					vertices[tileStartPoint_z * vSize_x + tileStartPoint_x * 4 + 3] = new Vector3(tileStartPoint_x + 1, 0, tileStartPoint_z + 1);
				}
			}
		}
		
		for(int z = 0; z < size_z; z++)
		{
			for(int x = 0; x < size_x; x++)
			{
				int squareIndex = z * size_x + x;
				int triOffset = squareIndex * 6;
				int s = numVerts/size_x;

				triangles[triOffset + 0] = 0 + x * 4 + z * s;
				triangles[triOffset + 1] = 2 + x * 4 + z * s;
				triangles[triOffset + 2] = 1 + x * 4 + z * s;

				triangles[triOffset + 3] = 1 + x * 4 + z * s;
				triangles[triOffset + 4] = 2 + x * 4 + z * s;
				triangles[triOffset + 5] = 3 + x * 4 + z * s;
			}
		}
		
		Mesh mesh = new Mesh();
		mesh.vertices = vertices;
		mesh.triangles = triangles;

		MeshFilter mesh_filter = GetComponent<MeshFilter>();
		MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
		MeshCollider mesh_collider = GetComponent<MeshCollider>();
		
		mesh_filter.mesh = mesh;
	}
}

If you set your size_x to 3 and size_z to 2 you’ll see how your generation code is incorrect.

for vertices too? because i dont see how it’s incorrect for that. but i do see that triangles are totally wrong right now, and im not sure how to do em

Your B-loop is completely unecessary.

I personally do not like lots of temporary variables when coding, makes the code harder to read. A tip is to skip them at first and then create them as an optimization later when you see code that you repeat a lot and know that everything is working like expected.

vSize looks completely wrong, would make more sense if it was “size * 4”… this is why I recommend working with raw equations.

If you also get the position in vertices right, then it is possible to reuse that after the equal sign for triangles.

I will leave you to think on the code a bit but I at least did not get the math to add up… and just a pro tip, draw and calculate on paper to test your work before you implement it :wink:

Jaasso,

the quill18 tutorial, which this code looks like being derived from, created shared vertices. Thats why he had two iterations: one for each single vertex, normal and uv which iterated over the size +1 on both axis and a second fot the triangles which counted exactly the number of tiles.

Your code reads as if you want unique vertices, hence you create 4 vertices per iteration on both axis. So you can easily combine both loops (and get rid of the additional pseudo loop).

You also don’t need to those cryptic pointers to array indexes since: creating each v, n, u and t group should reference the same tile index.

I’d ease and clean it up:

  1. create a method for each group of vertexes, normals and uvs (returning an array of 4 each) and a method for triangles (returning 6).

  2. just do a plain iteration over z and x (or vice versa) that increases per tilesize (that way you’ll have it generic) - and create an aditional counter increasing in the inner iteration by 1.

  3. Feed that counter to the v, n, u and t methods, so they know what coordinates etc to create for the current position of the counter.

  4. Since you are probably going to need eachs tiles unique info(v, n, u and t) in the future you should create a tile and a map class that hold those arrays permanently.

  5. when feeding each tiles arrays to the class you also feed them to the main vertices, normals, uv and triangles arrays you then assign to the mesh and later the filter and collider.

  6. Steps 4 and 5 may seem a bit of overhead when creating the mesh, but you’ll be thankful to be able to read and write to each tiles data in the development of yout tilemap.

edit: got your name wrong

unfortunately i wasnt able to pull it off in the way perplex suggested, i tried and tried but im too much of a noob at this point. but i modified my script a little and it seems to do its job well enough and works for me for now, it creates what i said i want in the first post. it still doesnt work correctly with a tileSize of something else than 1 and the reason should be pretty obvious. this is what i ended up with:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]

public class tileMap : MonoBehaviour
{
	public int size_x = 50;
	public int size_z = 50;
	public int tileSize = 1;

	void Start ()
	{
		buildMesh ();
	}

	public void buildMesh()
	{
		int numTiles = size_x * size_z;
		int numTris = numTiles * 2;

		int numVerts = numTiles * 4;

		Vector3[] vertices = new Vector3[numVerts];

		int[] triangles = new int[numTris * 3];

		int counter = 0;
		int counter2 = 0;

		for(int z = 0; z < size_z; z++)
		{
			for(int x = 0; x < size_x; x++)
			{
				vertices[counter * 4]	  = new Vector3(x			 , 0 , z);
				vertices[counter * 4 + 1] = new Vector3(x + tileSize , 0 , z);
				vertices[counter * 4 + 2] = new Vector3(x			 , 0 , z + tileSize);
				vertices[counter * 4 + 3] = new Vector3(x + tileSize , 0 , z + tileSize);

				int tileIndex = z * size_x + x;
				int triOffset = tileIndex * 6;
				
				triangles[triOffset + 0] = 0 + counter2;
				triangles[triOffset + 1] = 2 + counter2;
				triangles[triOffset + 2] = 1 + counter2;
				
				triangles[triOffset + 3] = 2 + counter2;
				triangles[triOffset + 4] = 3 + counter2;
				triangles[triOffset + 5] = 1 + counter2;

				counter++;
				counter2 += 4;
			}
		}

		Mesh mesh = new Mesh();
		mesh.vertices = vertices;
		mesh.triangles = triangles;

		MeshFilter mf = GetComponent<MeshFilter>();
		mf.mesh = mesh;
	}
}