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;
}
}