- Home /
Having trouble with triangles in mesh gen
So I'm currently using a script to generate a mesh for a 2d grid-based game I'm planning to make, but for some reason, when I apply a random color to the material during generation,it shows a peculiar occurrence...
Here's the code for it: using UnityEngine; using System.Collections;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshRenderer))]
public class GridScript : MonoBehaviour {
// Grid and tile size variables.
public int size_x;
public int size_y;
public int tileSize;
// Tile types and colliders
string[,] tileType;
// Use this for initialization
void Start() {
// Initialize the Grid Data variables.
tileType = new string[size_x, size_y];
// Run init functions.
InitGrid();
}
void Update() {
if(Input.GetMouseButtonDown(0)) {
processGridClick();
}
}
void genGridData() {
for (int x = 0; x < size_x; x++) {
for (int y = 0; y < size_y; y++) {
tileType[x, y] = "empty";
}
}
tileType[5, 5] = "wall";
}
public void processGridClick() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (collider.Raycast(ray, out hitInfo, Mathf.Infinity)) {
int x = Mathf.FloorToInt(hitInfo.point.x / tileSize);
int y = Mathf.FloorToInt(hitInfo.point.y / tileSize);
Debug.Log(tileType[x,y]);
}
}
public void buildTexture() {
int texWidth = 10;
int texHeight = 10;
Texture2D texture = new Texture2D(texWidth, texHeight);
for (int y = 0; y < texHeight; y++) {
for (int x = 0; x < texWidth; x++) {
Color c = new Color(Random.Range (0f, 1f), Random.Range (0f, 1f) , Random.Range (0f, 1f) );
texture.SetPixel(x, y, c);
}
}
texture.filterMode = FilterMode.Point;
texture.Apply();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
mesh_renderer.sharedMaterials[0].mainTexture = texture;
}
public void InitGrid() {
// Initiate the required mesh variables.
int numTiles = size_x * size_y;
int vsize_x = size_x + 1;
int vsize_y = size_y + 1;
int numVerts = vsize_x * vsize_y;
int numTris = numTiles * 2;
// Initiate the mesh data.
Vector3[] vertices = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris * 3];
// For loops to full the vertices, normals, uvs, and triangles of the mesh.
int x, y;
for (y = 0; y < vsize_y; y++) {
for (x = 0; x < vsize_x; x++) {
vertices[y * vsize_x + x] = new Vector3(x * tileSize, y * tileSize, 0);
normals[y * vsize_x + x] = Vector3.forward;
uv[y * size_x + x] = new Vector2((float)x / size_x, (float)y / size_y);
}
}
for (y = 0; y < size_y; y++) {
for (x = 0; x < size_x; x++) {
int squareIndex = y * size_x + x;
int triOffset = squareIndex * 6;
triangles[triOffset + 0] = y * vsize_x + x + 0;
triangles[triOffset + 1] = y * vsize_x + x + vsize_x + 0;
triangles[triOffset + 2] = y * vsize_x + x + vsize_x + 1;
triangles[triOffset + 3] = y * vsize_x + x + 0;
triangles[triOffset + 4] = y * vsize_x + x + vsize_x + 1;
triangles[triOffset + 5] = y * vsize_x + x + 1;
}
}
// Create the mesh itself, and assign the variables used to manipulate it.
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh_filter.mesh = mesh;
mesh_collider.sharedMesh = mesh;
buildTexture();
} // End of initGrid
} // End of script
it's quite a bit, but something tells me the trouble is in initGrid... When it generates, the top triangles and bottom ones don't lie within the same quad, but I just can't seem to find what's causing it...
Do you have an image of what exactly happens? would help towards helping you what to do to fix it.
Sure, here ya go. If you need anything else, just shoot
I think that its this : uv[y * size_x + x] = new Vector2((float)x / size_x, (float)y / size_y);
which is causing the issue, You need to give the UVs in a specific order. Try making you're code loop per face rather than per vertex, then add all the data for the face to the arrays at once.
Here is an example of how I add my mesh data for my voxel game : http://puu.sh/7tc23.png ( full code : http://pastebin.com/zxw1zB7e )and the UV calculation : http://puu.sh/7tc5h.png . The code in the first image is called in a for loop so it does that for how many faces it needs to draw.
Hmm... That code definitely looks a lot cleaner, though being a newbie to c#(only a little experience in Java to help) most of it is a tad confusing, and I've personally never seen a file that large, so a rundown might help.(Sorry for being a complete newbie to all of this, as well)
np :) basically, to build my voxel meshes, The meshbuilder class is used, this recieves the chunk data and loops through every block in the chunk and checks every side of each block, it then compares whether each block is solid or air, if its air, it continues, if its solid, it checks if the blocks next to it are solid and if they are not solid, render a face at that position and direction. You can see how I used switches dependent on the side it checks, you will not need to do a switch based system, ins$$anonymous$$d you will just loop through every grid section and add a square at the position, this way you can easily group all you're meshing code into the same loop, if you get what I mean?
Answer by wibble82 · Mar 13, 2014 at 07:43 AM
That looks like a perfectly reasonable approach. Filling out the vertices first, then hooking up the triangles is the standard way of doin this.
I think the issue is the uv line. When indexing into the array you currently use size_x instead of vsize_x like the lines above it. That causing your row accesses to be off by 1 and thus giving you that zig zag effect.
Main lesson here - name variables nice and differently - typos like that are always tricky to spot if your key variable names are only different by 1 letter!