- Home /
Splitting vertices in a mesh specifically a plane
So this is my first time using the Mesh class. I goal was to make a tilemap so that I can take a crack at procedural dungeon creation. I have data structures set up to create a blue print the challenge I am having is drawing it.
The idea I was going for was to create the dungeon out of a single plane made up of several vertices as shown in the code below to form tiles. I was hoping to raise and lower these vertices to create walls and chasms.
In the code I wrote a little test to see how raising vertices would affect the created mesh and what I noticed is that instead of a cube like structure rising out of the plane it was creating more of hill as shown in the picture. I think the issue is that vertices are shared between tiles.
I know I would need more vertices I was wondering if anyone could point me in the right direction of how to make it form a wall instead of a hill.
![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/38625-question.png
Your answer
Follow this Question
Related Questions
How to manipulate(flatten) the Mesh of an object 0 Answers
Procedural Mesh problem from Editor Window 1 Answer
Do i have to use shared vertices if i want my proceduraly generated mesh to look smooth? 1 Answer
Cannot using two script to modify the same mesh at the same time 1 Answer
Procedural mesh with texture atlas, how to get rid of material artifacts? 0 Answers