learning note – procedure cave generation


this is cool. generate cave 0-1 map by smooth random map, then create mesh, then find the wall(edge) and draw them, then find the size of room,delete too small size room and connect rooms together.



1. [Range(0,100)] public int randomFillPercent;
This can create a slide in inspector to select the int value
2.int[,] map;
3.seed = Time.time.ToString(); System.Random random = new System.Random(seed.GetHashCode());
//Get psudo random value each time
4.void OnDrawGizmos()
Can be used to draw some cube.
Gizmos .color = (map[x,y]==1)?Color.black:Color.white;
Gizmos.DrawCube (pos, Vector3.one);
5 int[,] map,
map.GetLength(0) map.GetLength(1) to know the length of multi dimension array.
6 align with view: align camera with view
Align view to selected: align scene view to camera
7 Mesh mesh = new Mesh ();
mesh.vertices = vertices.ToArray ();
mesh.triangles = triangles.ToArray ();
mesh.RecalculateNormals ();
GetComponent<MeshFilter> ().mesh = mesh;
This way can manually set mesh infos.
Vertices saves all the position of each vertice.
Triangles has 3*number of triangle value, each is an index, use that index can get position from vertices.
8 this way to draw mesh is interesting. Use four control node and four non-control node. If a control node is selected, its neighbour non-control node will be selected.
9 define of outline edge: two vertics which share one triange.
10 get intersect of two list
List<Triangle> trianglesContainingVertexA = triangleDictionary [vertexA];
List<Triangle> trianglesContainingVertexB = triangleDictionary [vertexB];
IEnumerable<Triangle> intersect = trianglesContainingVertexA.Intersect (trianglesContainingVertexB);
11 access a struct like an array:
struct Triangle {
public int[] vertices = new int[3];
public Triangle(params int[] v) {
for(int i = 0;i<v.Count;i++){
vertices[i] = v[i];
public int this[int i]{
get {
return vertices[i]
12 when set mesh filter, set vertices first, then triangle.
Triangles has to be counter clock wise
13 use queue:
Queue<Coord> queue = new Queue<Coord> ();
Coord tile = queue.Dequeue ();
queue.Enqueue (new Coord (x, y));
14 int max value: int.MaxValue
15 if not set color for drawline, it wont draw anything
Debug.DrawLine (CoordToWorldPoint (tileA), CoordToWorldPoint (tileB), Color.green, 100);
16 inherite from Icomparable and implement compareTo
public int CompareTo(Room otherRoom){
return otherRoom.roomSize.CompareTo (roomSize);
Then can use rooms.sort() to sort them
Notice you have to write like this:
class Room : IComparable<Room>
IComparable defines an int CompareTo(object) while the generic IComparable<T> defines an int CompareTo(T)
17 set default value for method
void ConnectClosestRooms(List<Room> allRooms, bool forceAccessibilityFromMainRoom = false)
18 generate collider:
In either one, remember to delete the former collider before add new.
MeshCollider wallCollider = walls.gameObject.GetComponent<MeshCollider> ();
Destroy (wallCollider);
Create meshCollider and set the sharedMesh as mesh in meshFilter.
wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
wallCollider.sharedMesh = wallMesh;
Create edgeCollider2D and set the points as vector2.
EdgeCollider2D edgeCollider = gameObject.AddComponent<EdgeCollider2D> ();
Vector2[] edgePoints = new Vector2[outline.Count];
for (int i = 0; i < outline.Count; i++) {
edgePoints [i] = new Vector2( vertices [outline [i]].x,vertices [outline [i]].z);
edgeCollider.points = edgePoints;

This entry was posted in 未分类. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s