Things that I don't get in Procedural Cave generation Tutorial
Things that I don't get:
Why do we write this: Vector3 pos = new Vector3(-width/2 + x + .5f,0, -height/2 + y+.5f);
How did Sebastian Lague come up with these coordinates?
The IComparable interface, CompareTo function, ArrayList.sort(). How did Sebastian sort the greatest to least in the surviving rooms size.
The whole thing about Uvs. what is the variable tile amount for?
Here is the code which contains parts that I don't get: Edit: I am narrowing down the code. i hope i dont delete anything important.
List<List<Coord>> roomRegions = GetRegions (0);
int roomThresholdSize = 50;
List<Room> survivingRooms = new List<Room> ();
foreach (List<Coord> roomRegion in roomRegions) {
if (roomRegion.Count < roomThresholdSize) {
foreach (Coord tile in roomRegion) {
map[tile.tileX,tile.tileY] = 1;
}
}
else {
survivingRooms.Add(new Room(roomRegion, map));
}
}
survivingRooms.Sort ();
survivingRooms [0].isMainRoom = true;
survivingRooms [0].isAccessibleFromMainRoom = true;
ConnectClosestRooms (survivingRooms);
}
Debug.DrawLine (CoordToWorldPoint (tileA), CoordToWorldPoint (tileB), Color.green, 100);
}
Vector3 CoordToWorldPoint(Coord tile) {
return new Vector3 (-width / 2 + .5f + tile.tileX, 2, -height / 2 + .5f + tile.tileY);
}
class Room : IComparable<Room> {
public List<Coord> tiles;
public List<Coord> edgeTiles;
public List<Room> connectedRooms;
public int roomSize;
public bool isAccessibleFromMainRoom;
public bool isMainRoom;
public Room() {
}
public Room(List<Coord> roomTiles, int[,] map) {
tiles = roomTiles;
roomSize = tiles.Count;
connectedRooms = new List<Room>();
edgeTiles = new List<Coord>();
foreach (Coord tile in tiles) {
for (int x = tile.tileX-1; x <= tile.tileX+1; x++) {
for (int y = tile.tileY-1; y <= tile.tileY+1; y++) {
if (x == tile.tileX || y == tile.tileY) {
if (map[x,y] == 1) {
edgeTiles.Add(tile);
}
}
}
}
}
}
public void SetAccessibleFromMainRoom() {
if (!isAccessibleFromMainRoom) {
isAccessibleFromMainRoom = true;
foreach (Room connectedRoom in connectedRooms) {
connectedRoom.SetAccessibleFromMainRoom();
}
}
}
public static void ConnectRooms(Room roomA, Room roomB) {
if (roomA.isAccessibleFromMainRoom) {
roomB.SetAccessibleFromMainRoom ();
} else if (roomB.isAccessibleFromMainRoom) {
roomA.SetAccessibleFromMainRoom();
}
roomA.connectedRooms.Add (roomB);
roomB.connectedRooms.Add (roomA);
}
public bool IsConnected(Room otherRoom) {
return connectedRooms.Contains(otherRoom);
}
public int CompareTo(Room otherRoom) {
return otherRoom.roomSize.CompareTo (roomSize);
}
}
}
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class MeshGenerator : MonoBehaviour {
public SquareGrid squareGrid;
public MeshFilter walls;
public MeshFilter cave;
public bool is2D;
List<Vector3> vertices;
List<int> triangles;
Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>> ();
List<List<int>> outlines = new List<List<int>> ();
HashSet<int> checkedVertices = new HashSet<int>();
public void GenerateMesh(int[,] map, float squareSize) {
triangleDictionary.Clear ();
outlines.Clear ();
checkedVertices.Clear ();
squareGrid = new SquareGrid(map, squareSize);
vertices = new List<Vector3>();
triangles = new List<int>();
for (int x = 0; x < squareGrid.squares.GetLength(0); x ++) {
for (int y = 0; y < squareGrid.squares.GetLength(1); y ++) {
TriangulateSquare(squareGrid.squares[x,y]);
}
}
Mesh mesh = new Mesh();
cave.mesh = mesh;
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
int tileAmount = 10;
Vector2[] uvs = new Vector2[vertices.Count];
for (int i =0; i < vertices.Count; i ++) {
float percentX = Mathf.InverseLerp(-map.GetLength(0)/2*squareSize,map.GetLength(0)/2*squareSize,vertices[i].x) * tileAmount;
float percentY = Mathf.InverseLerp(-map.GetLength(0)/2*squareSize,map.GetLength(0)/2*squareSize,vertices[i].z) * tileAmount;
uvs[i] = new Vector2(percentX,percentY);
}
mesh.uv = uvs;
You should really try specifying what the issue is for people who haven't watched the tutorial and narrowing down the problem to a lot less than 700 lines of code.
Answer by Bunny83 · Jun 15, 2016 at 08:51 AM
This line:
new Vector3(-width/2 + x + .5f,0, -height/2 + y+.5f);
will create a position that references the center of each tile. Each tile has a size of 1f units. The center is at 0.5f, 0.5f of each tile position. Since "x" and "y" most likely go from 0 to width and 0 to height he also offsets the overall position of each tile by half the width and half the height of the whole map.
So the final map goes from "-half width" to "+half width" and "-half height" to "+half height". So the position 0,0 is in the center of the map.
About IComparable:
First of all nowhere is used an "ArrayList", only a generic "List". Don't confuse those two things, they are two very different classes. You still have your other question on that topic. If you still need more help with that one, post a comment on that question or edit your question to clarify what's still unclear.
About UVs and tile amount:
The whole code you posted (where most of the code is unrelated to any of your questions) doesn't contain any UV coordinates and doesn't contain any "tile amount" variable. So what are you actually talking about?
You clearly included too many unrelated questions into one question. As i said in the comment on your other question, please read the FAQs and the user guide.
Never $$anonymous$$d about my other comment. I finally understand the concept :).