- Home /
Splitting shared vertices in a plane
So this is my first time working with the mesh class in unity. I am trying to create a tile map so I can take a stab at procedural dungeon making. I have data structures made to create a blue print of a dungeon the challenge I am trying to tackle is drawing it.
My idea was to use a single procedural plane made up of many vertices to create sub planes this is shown in the code below. My hope was was to raise and lower these vertices to create walls and chasms. I wrote a little test to see how raising certain vertices affected the mesh the issue I am having is that there vertices shared between sub planes so instead a vertical cube rising out of it its forming more of a hill. as shown in the image.
I know I would have to increase the number of vertices but I was hoping for a nudge in the right direction of how to go about making sure there are no shared vertices .
![using UnityEngine;
using System.Collections;
[RequireComponent(typeof (MeshFilter))]
[RequireComponent(typeof (MeshRenderer))]
[RequireComponent(typeof (MeshCollider))]
public class TileMap : MonoBehaviour {
//number of tiles in the x direction
public int size_x = 7*35;
//number of tiles in the z direction
public int size_z = 7*35;
//size of each tile
public float tileSize = 10f;
// Use this for initialization
void Start () {
buildMesh();
}
public void buildMesh(){
//number of tiles total on the map
int numTiles = size_x*size_z;
//number of triangles is double the number of tiles needed as 2 triangles make one square on the map.
int numTris = numTiles*2;
//number of vertices in the x dirextion
int vsize_x = size_x + 1;
//number of vertices in the z direction
int vsize_z = size_z + 1;
// number of vertices total in the map needed to make the plane.
int numVerts = vsize_x * vsize_z;
//Create mesh data.
//array of vector 3's to store vertex data
Vector3[] vertices = new Vector3[numVerts];
//array of vector 3's to store normal data
Vector3[] normals = new Vector3[numVerts];
//array of vector 2's to store uv data.
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris*3];
//One nested for loop to set up the vertices normals, and uv
int x,z;
for(z=0; z < vsize_z; z++){
for(x=0; x < vsize_x; x++){
vertices[z * vsize_x + x] = new Vector3(x*tileSize,0,z*tileSize);
normals [z * vsize_x + x] = Vector3.up;
uv[z * vsize_x + x] = new Vector2((float)x/size_x,(float)z/size_z);
}
}
// a little test I wrote to see how elevating certain vetices affect the mesh.
vertices[0] += new Vector3(0,10,0);
vertices[1] += new Vector3(0,10,0);
vertices[vsize_x] += new Vector3(0,10,0);
vertices[vsize_x+1] += new Vector3(0,10,0);
//end of test
//one nested for loop to set up the triangle data for the mesh.
for(z=0; z < size_z; z++){
for(x=0; x < size_x; x++){
int squareIndex = z * size_x + x;
int triOffset = squareIndex*6;
triangles[triOffset +0] = z * vsize_x + x + 0;
triangles[triOffset +2] = z * vsize_x + x + vsize_x + 1;
triangles[triOffset +1] = z * vsize_x + x + vsize_x + 0;
triangles[triOffset +3] = z * vsize_x + x + 0;
triangles[triOffset +5] = z * vsize_x + x + 1;
triangles[triOffset +4] = z * vsize_x + x + vsize_x + 1;
}
}
//Create a new mesh
Mesh mesh = new Mesh();
//Populate mesh with data
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
//Assign our mesh to our filter/renderer/collider.
MeshFilter mesh_filter = GetComponent<MeshFilter>();
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();
MeshCollider mesh_collider = GetComponent<MeshCollider>();
mesh_filter.mesh = mesh;
}
}][1]
[1]: /storage/temp/38604-question.png
Your answer
Follow this Question
Related Questions
How to manipulate(flatten) the Mesh of an object 0 Answers
Problem with setting vertices positions and smoothing groups. 1 Answer
Create runtime 3D mesh and Fill area like Paper.io 2 1 Answer
Get plain coordinates for another object 2 Answers
Accessing mesh vertices is extremely inefficient; any workarounds? 0 Answers