Why I'm Using Wave Function Collapse for Procedural Terrain | Unity Devlog

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone i'm working on a procedural modular terrain system for my projects in unity when i say projects of course i mean games but i'm a behavioral scientist so i wanted something that would work for behavior simulations too in unity you can simulate all sorts of things like boys utility ai neural networks and kinesis keep an eye out on my channel for more of this kind of thing and let me know in the comments what kind of simulations you think would be fun in the past i spent a lot of time working on procedural voxel terrain i've done minecraft style boxes marching cubes and dual contouring there were challenges sometimes they were interesting like this was one of my first attempts at marching cubes to rain no idea what happened here looks kind of neat though other times well okay i know i know what it looks like i promise i promise it's not what i was going for i knew i wanted to move away from the kind of voxel complexity i've spent far too much time on and then i learned about wave function collapse no not this wave function collapse this wave function collapsed hey don't get mad at me i didn't name it so this method can generate 2d textures from input or generate 3d worlds from modules the texture version looks something like this you saw the 3d version at the beginning of the video let me tell you how i got here i was starting a new side project as one often does and at the time i was really into pixel art and classic top-down games like legend of zelda i like the fun little perspective tricks they use like look at how the walls are angled to get better visibility they even angle the models in some of the top down 3d games i wanted to make a 3d procedural mesh that had the same top-down pixel art vibe and angled walls and i did and i like the overall look at the first attempt it helps that i'm borrowing a lot from textures like castlevania zelda and metroid i'm also using a pixel art shader on the characters while i was working on this i got really distracted by a heart style and maybe i'll talk about that in another video my next task was to remove gaps between adjacent blocks at first i tried a custom extruded version of marching squares and it worked really well until it didn't then i tried marching cubes remember how i said i didn't want to use voxels well here i am again using voxels it did exactly what i wanted but it was so much work let me tell you a little bit about marching cubes and you can see where i went wrong here i have a 3d grid of voxels the dark voxels represent solid material while the light voxels represent open space you can kind of imagine what the overall shape will be especially if i don't show the open voxels they're still there they're just not being shown you might be tempted to place a cube everywhere there's a solid voxel this is the voxel approach used by minecraft it's nice but you can really only get cubic shapes the marching cubes method uses a cell of eight voxels one in each corner remember the open voxels are still there too a mesh is built inside the cell to separate the solid and open voxels here a flat ground mesh is created since there are four voxels at the bottom of the cell in the next cell an angled ramp is needed to cover the solid voxels this cell has all solid voxels so it's completely underground and doesn't need a mesh now we create another flat ground mesh to cover the four bottom voxels and this process continues as the cell marches through the voxel grid rating a mesh anywhere one is needed that's the basic process of marching cubes there are a lot of ways to build the individual cell meshes but generally there are 256 possible meshes that could be built normally do the smart thing and find some pre-calculated marching cubes mesh tables online to help out but i'm not always that smart i coded all of this manually and i didn't even get to use my old marching cubes code it was a lot of work and i don't recommend this at all i even have way more than 256 cases for all these special rules i just wanted more control over the types of shapes that i could get than the standard marching cubes rules would provide i wanted both blocky shapes and beveled shapes eventually with my version of the algorithm i was able to get pretty much what i wanted but i still didn't like it it was weeks of work and i regret all of it i just didn't like it what really got me was the texturing i was trying a lot of different styles but texturing is one area where voxel algorithms are weak since the mesh is built procedurally you have much less artistic control than if you built and textured a mesh in the 3d modeling program so looking back at this it's not all bad but i just didn't like it it wasn't really what i wanted and i was frustrated but i did find an interesting solution i was looking at twitter and you can follow me there too and i saw a post from oscar stallberg the creator of bad north and townscaper he was sharing a devlog from the creators of phantom brigade where they discussed their procedural terrain system so first check out this game i've played some other games but not this one does look really cool though they used a modular system for their buildings that i really like here's their post on modular buildings link in the description they create 3d tiles based on marching cube's rules and assemble them using a modified marching cubes algorithm but because they create the meshes by hand in a 3d modeling program they have full artistic control of the shapes and materials they use and it really works great i really like the idea of making 3d tiles instead of using the standard marching cube shapes but i also didn't want to be restricted by marching cube's rules at all i dug a little deeper and i started reading more of oscar stahlberg's posts he uses a method called wave function collapse yes we finally get to it in oscar stahlberg's bad north wave function collapse is used to generate these cute little islands they look great and more importantly they don't look anything like you would get from marching cubes in townscaper you build these little towns that's it everything's assembled with wave function collapse and it looks nothing like marching cubes it's fun and super cheap so check it out here's how wave function collapse works first to create modules here i have a simple 2d grass tile the grass tile connects to the little trees tile and vice versa i'm creating matches for each tile based on adjacency the big trees tile connects to the little trees tile but not to grass sand connects to grass but not to trees and finally water connects to sand let's also assume that every tile can connect to itself now that the modules are ready we need a grid of cells initially each cell can contain any of the modules we can say that the domain of each cell initially includes all possible modules now we pick a random cell and randomly assign a module the cell has now been collapsed we also know how to reduce the domains of the neighboring cells we know that the little trees module can only be placed near other little trees big trees and grass reducing the domains of the neighboring cells might also reduce the domains of the neighbor's neighbors when we collapse one cell we need to propagate the information to all cells that should change now we pick a new cell to collapse it should probably be a cell with a small domain each time we collapse the cell we propagate the new constraints to adjacent cells watch how the domain sizes decrease each time we collapse a new cell i'll let this play out a bit so you can see the process [Music] and i'll also speed it up a bit too [Music] and that's it that's way function collapse [Music] here's the same tile set on a much bigger map the process is nearly instant when it isn't being animated but i also really like watching the algorithm work one of the nice things about this method is that you can set whatever connectivity rules you want and the algorithm will figure out how to put things together for the most part okay so that was a simple set of tiles for this demonstration here's a more serious set of pixel art tiles i was really into pixel art for a bit while i was working on this i'm not great at it but it's a style i'd like to get better at the little worn down pathways in this one are kind of interesting and i like how the flowers turned out i have some rules here that clump the flowers and weeds together but don't group those with the trees it's all about setting rules for how the tiles can connect you have a lot of creative freedom here are some 3d sets i've been working with the algorithm works pretty much the same in 3d i'm following general marching cube's design guidelines for creating my modules but i can also ignore a lot of it or add my own rules to it's really nice to be unrestricted by voxel meshing rules for this one i was playing around with small river and waterfall modules that always flow south that's the kind of thing you can't do with marching cubes [Music] and here the trees might look a little weird during generation but that's because the tree mesh is randomly picked from a few different options when the mesh is built during animation the mesh has built each update randomly picking a mesh to use means i can have one module that can have multiple looks would be much less performant to have one module per tree type okay that's about it for this video i hope you enjoyed it in the next video i'll go into technical details about how wavefunction collapse works and about how my own code works please do all the normal youtube things like subscribe hit the bell swipe right tell your mom you love her reach out to an old friend but also let me know what you're thinking in the comments it would be nice to talk to people with similar interests all right that's all for now see you next time
Info
Channel: DV Gen
Views: 1,077,136
Rating: undefined out of 5
Keywords:
Id: 20KHNA9jTsE
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Sun Apr 10 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.