Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Procedural Cave Generation

Discussion in 'Community Learning & Teaching' started by SebastianLague, Feb 9, 2015.

  1. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Last edited: Jul 15, 2015
    OzDave, konsic, Farelle and 17 others like this.
  2. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Very well done! Excellent format, execution and clarity. Looking forward to seeing further episodes!
     
    landon912 likes this.
  3. Zachary Van Kleeck

    Zachary Van Kleeck

    Joined:
    Oct 31, 2013
    Posts:
    40
    love it, and you sound like Stewie from family guy!
     
  4. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    I've uploaded episode 2, covering the marching squares algorithm (to create a mesh out of the generated cave shapes).
     
    TheGrimDerp likes this.
  5. gl33mer

    gl33mer

    Joined:
    May 24, 2010
    Posts:
    281
    Thank you Sebastian for this great set of videos.
     
    Last edited: Mar 25, 2015
  6. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    This is really impressive. I'm actually trying to do something much simpler (I think) and am having all sorts of troubles. Nothing like a random generator, just a custom Unity editor that would allow a game designer to click points in 3D space (currently against a plane) and connect the points together with meshes (edges), and then creating the polygon that the edges make when the designer clicks a point that they already made (points = sphere with a collider on it used during the creation process).

    These tutorials give me some hope that it is possible for me to figure it out. :) Thanks!
     
    Last edited: Mar 7, 2015
  7. Mikeedee

    Mikeedee

    Joined:
    Jan 5, 2015
    Posts:
    42
    Thanks a lot for this, this comes in a handy as I've been looking into learn about procedural creation for a while :)

    edit: seems like the last tutorial(3) source code isn't working correctly, no mesh is visible despite the small hickup everytime I hit the left mouse button or at the start due the cave generation code doing its magic, so I assume something is wrong with the meshgenerator code.

    I added the mesh filter, tweaked values (width and height from 5 to 1000, random fill percent from 5 to 60) but still no mesh. :(
    The mesh filter component in the editor does show it isn't empty, but the mesh name that is added is just blank space (though I think this is on purpose) and selecting doesn't seem to be doing anything.
    If you need more info let me know :)
     
    Last edited: Mar 10, 2015
  8. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Hey, I'm not sure what could be going wrong. If you send me your Unity project though I'm sure I will be able to figure it out! ;)
     
  9. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
  10. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Been working on an algorithm for intelligently joining up the caverns with passages. Will start on the tutorial soon.

     
    Paulohmm likes this.
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  12. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Thanks Adam :)

    I've uploaded episode 05 - it covers the detection of the different regions of the map so that we can easily remove those we don't want (e.g tiny rooms / walls).
     
  13. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Episode 06 now out, covering the connection of isolated rooms.
     
  14. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Uploaded episode 07, which shows how we can ensure that all rooms in the map are accessible.
     
  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oh man. I don't watch Breaking Bad or Game of Thrones, but I'm now getting to feel like the friends I know who do! Now I'm behind on my viewing and have to catch up!
     
    TheGrimDerp and keenanwoodall like this.
  16. kuchaku

    kuchaku

    Joined:
    Oct 14, 2014
    Posts:
    37
    This is an extremely useful algorithm video series. Thank you.

    As a long term goal I'm hoping to mix elements of accidental noise, fractals, and such with cellular automata. I'm thinking different algorithms will produce different biome kind of effects.

    But this video series has given me a jump start in comprehending things, where as these other approaches are quite difficult:

    http://accidentalnoise.sourceforge.net/
     
    TheGrimDerp likes this.
  17. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Glad to hear it kuchaku, good luck with your project! I'm currently planning a series on generating land masses with perlin noise as a follow-up to this series, but your idea of combining different procedural approaches sounds intriguing. I'll do some experimenting of my own :)
     
    kuchaku likes this.
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Is this series done? (I've not reached the end of the videos yet...)
     
  19. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    I've still got one more episode to make before this series could be considered complete. I do have a few ideas for things I'd like to add to it after that (e.g. choosing good spawn points for doors/treasure chests/enemies and the like), but I might take a break from this series after the next episode to work on some other stuff, not sure yet!
     
    TheGrimDerp likes this.
  20. Eldh

    Eldh

    Joined:
    Nov 22, 2010
    Posts:
    22
    Great séries super usefull ! Just a question thought, have you tried generating big map ? The detecting region code(episode 5) is super slow. When i disable it, it creates the map 100 times faster. How would you go to speed thing up ?
     
  21. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Hi Eldh, how big a map are you talking about? The region detection code runs pretty fast on my end, though the region connection (e06 and e07) is quite heavy. I'll look into making some optimizations.

    Episode 08 is now out:
     
  22. Eldh

    Eldh

    Joined:
    Nov 22, 2010
    Posts:
    22
    It starts to slow down on a 512*512 map. And after that it increase even more. I tried a 4048 map it took about 5 minutes of freezing to create, without the detection region it took about 5 seconds. But maybe it's not suppose to be used with such map size.
     
  23. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Wow,looks interesting topic + with videos its a definitely pearl.Thanks much, will start to watch it soon.

    I am not sure but I think this guy has done pretty much same thing + he has added remove geometry when user touches on screen something like we see in "Where's my Mickey/Water?" game.Check in, http://catlikecoding.com/unity/tutorials/marching-squares-5/


    Have you added this functionality too?
     
    TheGrimDerp likes this.
  24. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Hi idurvesh, I don't currently plan to add that feature, but I will certainly consider it.

    Here's episode 09: collisions and textures
     
    kuchaku, idurvesh and chelnok like this.
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  26. BornGodsGame

    BornGodsGame

    Joined:
    Jun 28, 2014
    Posts:
    587
    Amazing from the ones I´ve seen. Just a small question. Is it possible to run this, then export the created meshes to use in a different project?
     
  27. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    There are 3rd party mesh exporters. I'd check either the wiki or the asset store.
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    TheGrimDerp likes this.
  29. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    That's fantastic Adam, thanks!

    To Carve_Online: That functionality isn't provided in the series, but as Adam suggests you can definitely find something suitable online. If you want to write something quick yourself, you'd probably want to use AssetDatabase.CreateAsset() to save the procedural meshes. CreateAsset would take in a mesh and a string for the path + filename with '.asset' extension.
     
    chelnok likes this.
  30. michaelcapone

    michaelcapone

    Joined:
    Aug 16, 2012
    Posts:
    12
    Is this tutorial series compatible with Unity 4.6? I know I should make the move to 5, but I have been slow to migrate. Please let me know. This tutorial looks amazing.
     
  31. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Thanks michaelcapone. I have not tested it myself, but it should be fully compatible with 4.6. If there are any alterations that need to be made to the code, they should be very minor. And of course you're always welcome to post here if you hit a stumbling block and I'll help you get it sorted out.
     
  32. michaelcapone

    michaelcapone

    Joined:
    Aug 16, 2012
    Posts:
    12
    I have been following along in Unity 4.6. I have not run in to any programming issues so far. However, my maps display with a bunch of unintentional vertical and horizontal lines running through it. Any idea if this is a setting I can change to remove them? Have you seen this before?

     
  33. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    That is because you're in perspective mode, just switch into ortho and it should look good!
     
    michaelcapone likes this.
  34. michaelcapone

    michaelcapone

    Joined:
    Aug 16, 2012
    Posts:
    12
    Thanks! And keep up the terrific work.
     
  35. Korze

    Korze

    Joined:
    Feb 18, 2014
    Posts:
    2
    RESOLVED: PEBCAK issue. Utilized form ID10-T to resolve. Capitalize "Start" to start. Hah.

    Code does nothing at 9:57 of Episode 1, using the most recent unity (5.1.1). When you click Play, nothing happens. Have checked and rechecked code. Want to move forward, but afraid of not comprehending these steps and missing something critical

    Will continue forward in the meantime.
     
    Last edited: Jul 18, 2015
  36. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    This is the most useful (pretty much only useful for me anyway) tutorial on the Unity website and i have done them all. Useful for beginners and pros alike! Unity Should get you to do all their tutorials from now on, I found the in-depth way you explain things far more useful and informative than the other tutorials hosts.

    Getting a few bugs on 5.1.2 so would be great if you try following the tutorial yourself using the latest build of unity to find and rectify any small bugs that exist :)

    EDIT: Actually im thinking the bugs im getting are actually because im using some freaky patch version of unity that is both NOT 5.1.2 and also a version i didnt need. Just updated to actual 5.1.2 and everything works fine!
     
  37. TheGrimDerp

    TheGrimDerp

    Joined:
    Dec 8, 2012
    Posts:
    6
    aha! i was having this as well!
     
  38. SomeSlacker

    SomeSlacker

    Joined:
    Jul 1, 2015
    Posts:
    1
    I'm speechless, what an incredible learn project!

    I was trying to extend the automata to 3d, but am having trouble visualizing the map generated, the gizmo's not gonna cut it and even a noob like me knows instantiate is gonna blow up my puter.(after blowing up my puter). any suggestions? I need to visualize the original map so's I can come up with some smoothing rules, I've nixed the outer walls so I have some small chance of seein inside if I can come up with some way to see a large enough map to make sense of the smoothing iterations. And just so's I'm not crazy, are marching cubes even possible? hehe....16^3? (answered this last only 2^8 so feasible)
     
    Last edited: Jul 21, 2015
  39. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Thanks Daemonhahn and SomeSlacker, I'm glad you both enjoyed it!

    SomeSlacker, I'm not entirely sure what to suggest short of coding the marching cubes in advance (using a simple test example) and just using that for your visualization? Or would the gizmos not perhaps work at least for visualizing the outer shell of the map? I'll let you know if I think of anything helpful... Please do post pictures if you get any interesting-looking results by the way! :)
     
  40. Increment

    Increment

    Joined:
    Jul 21, 2015
    Posts:
    1
    Sebastian thank you so much for this series. I found it through a Google search on PCG in Unity(I didn't even realize it was on unity learn as a project). Anyway it is teaching me so much and I really appreciate what you are doing. Also subscribed to your channel and looking forward to anything else you may be doing.
     
    SebastianLague likes this.
  41. r_del71

    r_del71

    Joined:
    Jul 22, 2015
    Posts:
    3
    Great tutorial Sebastian! One problem I have is when I create a new map in 3D mode, the old MeshColliders seem to remain. Did I miss something on how to clear those out? In the 2D mode there is code to clear out the EdgeColliders, but I didn't see anything for the 3D mode.
     
  42. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    My apologies r_del71, I overlooked that. If you could add these two lines to the top of the CreateWallMesh method:
    Code (CSharp):
    1. MeshCollider currentCollider = GetComponent<MeshCollider> ();
    2. Destroy(currentCollider);
    And change this line: MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
    to simply: MeshCollider wallCollider = gameObject.AddComponent<MeshCollider> ();
    it should all work fine : )
     
    r_del71 likes this.
  43. Snownebula

    Snownebula

    Joined:
    Nov 29, 2013
    Posts:
    174
    How can I create a 3d cave with this?
     
  44. r_del71

    r_del71

    Joined:
    Jul 22, 2015
    Posts:
    3
    Thanks for the quick response, and yes it does work! I should have just applied the info you gave for the 2D stuff to the 3D stuff, but I am a bit new to the Unity environment and wanted to make sure I wasn't over simplifying it.

    Thanks again!
     
  45. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    Hello Sebastian!
    I just came across this tutorial and love it! Now, I am wondering...
    how can I optimize the mesh / edge collider?
    I am using Ferr2d toolkit to make a poly shape out of it and my cave has way to many vertices.
    How can I make sure that for example on a straight-line just two vertices are inserted instead for every unity one.
    ( one line for example is 5 units long before it makes a small hill and there for 5 vertices are inserted. )
    Regards! Tom
     
  46. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Hey stationx. Optimizing the edge collider should be pretty straightforward - you could use the outline code I showed for generating the 3D walls and step through the outline list comparing directions between vertices and eliminating the unnecessary ones. I'd be happy to help you with the code for that if you're struggling. Optimizing the mesh however is going to be much trickier because you'll need to figure out how to connect up the triangles. You might want to look into delaunay triangulation there, but I haven't experimented with that myself. Hope that helps!
     
  47. HaakonXCI

    HaakonXCI

    Joined:
    Dec 14, 2014
    Posts:
    3
    Hi, I'm having the same issue as michaelcapone, except when I switch to orthographic the issue is still there. What am I doing wrong? I attached a screenshot so you can see.
     

    Attached Files:

  48. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    Heey Sebastian!
    Thnx for the fast reply! Would you mind showing me that code part then? It is indeed about the edge collider. Not the mesh itself.
    Regards! Tom
     
  49. SebastianLague

    SebastianLague

    Joined:
    Aug 31, 2014
    Posts:
    111
    Hi Haakon, you're switching your camera projection to ortho, but the gizmos are being rendered in scene view so that has no effect. You can switch scene view to ortho by pressing the little square in the centre of the view widget (top right scene window).

    @stationx I'll get back to you later with some code.
     
    stationx likes this.
  50. Deleted User

    Deleted User

    Guest

    Hi!

    Really cool tutorial to a cool subject, but do you have the whole finished project available for download?

    I would like to play with it and look at some parts without having to go through all videos and re-create all steps etc and the github only contains the C# source code and not the project files?