Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by greatness · Jun 15, 2016 at 12:53 AM · vector3projectproceduraluvgeneration

Things that I don't get in Procedural Cave generation Tutorial

Things that I don't get:

  1. 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?

  1. The IComparable interface, CompareTo function, ArrayList.sort(). How did Sebastian sort the greatest to least in the surviving rooms size.

  2. 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;
 
























  
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cjdev · Jun 15, 2016 at 04:11 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greatness · Jun 16, 2016 at 12:56 AM 0
Share

Never $$anonymous$$d about my other comment. I finally understand the concept :).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calculate a points distance from a vector 3 2 Answers

How to generate endless 2d terrain 0 Answers

Example of Procedural Planet generation? 3 Answers

Fractals. Procedural generation in 3D games. 0 Answers

Vector3.Project, why do none of the examples I see work; why am I not getting this!? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges