1. Dungeons & Kingdoms
  2. News
  3. Developer's Blog - Behind the scenes with Rock Formations

Developer's Blog - Behind the scenes with Rock Formations

[p]This post is for anyone interested in what goes on from a developer's perspective.[/p][p][/p][p]In this post I'm going through some of the steps involved taking a single rock model and processing it into a fragmented and harvestable resource the player can smash or cut to generate a variety of smaller resources. I wanted to share some of the behind-the-scenes work that goes into game mechanics like this.[/p][p][/p][p]There are multiple ways this could be done, but my main goal was to create my own editor tools and processes to be able to easily repeat this on several different rock models, as well as be able to re-run the process if I need to make adjustments (different fragment shapes/sizes, different resource spawn points etc). My preference is to stay within the Unity editor when possible.[/p][p][/p][p]What I start with - any one of a variety of rock shapes and sizes. These are single mesh models. The only difference the model makes is how long it takes the automated processes to run, otherwise all steps are the same.[/p][p][/p][p][/p][p]What I end up with - a rock formation players can smash into smaller resources, which can then be broken up into even smaller resources.[/p][p][/p][p][/p][p][/p][h3]Fragment the model into shards[/h3][p]I'm using Rayfire for the fragmentation and connectivity features. While Rayfire has built-in fragmentation patterns for square and random shapes, I needed more control than what it offers so I use a custom point cloud with my own custom settings. The point cloud is generated from a starting point that covers a number of rows and columns with variable randomized spacing.[/p][p][/p][p]Square fragments are for mining stone blocks from a quarry. That is for advanced stonemason jobs where they have the knowledge and tools to cut square blocks from stone.[/p][p][/p][p][/p][p][/p][p]The starter unskilled job is Stonebreaker. For that I need randomized shapes as the tools and skills don't allow for precision stone cutting, only rock smashing into smaller pieces.[/p][p]Both fragment patterns also allow the player to cut rooms or tunnels out of rock formations creating cozy caves and protected areas.[/p][p][/p][p][/p][p][/p][h3]Fragment Each Shard Into Smaller Pieces[/h3][p]Each rock shard created by the previous point cloud also needs to be fragmented so I can control things like the visual effect of showing cracks when a shard is hit, and creating a nice explosion effect when a shard is demolished revealing new resources inside.[/p][p]I wrote routines to iterate over each shard, add my own custom components and automate fragmenting them with Rayfire into standard voronoi patterns.[/p][p][/p][p][/p][h3]Saving Generated Mesh[/h3][p]Smaller rock formations end up with 10-20 shards, larger formations end up with over 400.[/p][p]Each of those shards are then fragmented into 20-40 pieces each. That's thousands of new mesh files, but they only exist inside the scene view in the editor.[/p][p]I wrote custom routines that export the mesh data into asset files on disk. There are tools to do it manually for each rock shard, but I don't want to sit there clicking buttons and finding folders for thousands of shards. Now, it's one button press.[/p][p][/p][p][/p][h3]Generating Resource Spawn Points[/h3][p]Each rock shard will reveal a variety of potential new resources when it is demolished. These new resources spawn at specific points within the demolished shard.[/p][p]I setup my first rock formation by manually placing where the spawns should go. Seeing how time consuming and tedious that was, I then wrote new routines to fully automate the processes.[/p][p]Now, all I need to do is add the prefabs for the resources I want to spawn into a list and click a single button... and wait :)[/p][p]Larger rock formations with 100's of shards can take 5-10 hours to processes. I could optimize this further but it will probably always be an overnight job.[/p][p][/p][p]The automated process...[/p][p]A point cloud is generated that covers the entire shard based on its bounding box and my custom settings for things like spawn point spacing/density.[/p][p][/p][p][/p][p]Each potential spawn point is then tested by raycasting on all axis towards a small generated collider at each cloud point position. If all raycasts don't hit the collider, it is tracked as a valid potential spawn point for prefabs.[/p][p][/p][p][/p][p]Each type of spawn prefab is then instantiated and positioned at each valid potential spawn position. I use what I call boxcasting to confirm the spawn is entirely inside the mesh shard. Raycast origin points are generated for each axis (x, y, z) offset on both positive and negative. This creates a 6 sided cube of raycast origin points that all target the instantiated spawn object. If any raycasts hit the prefab object then it fails at that position. This works for both convex and non-convex shards.[/p][p]Each spawn prefab that fits inside is then tracked and checked for bounds intersection with other tracked prefabs. Overlaps are discarded.[/p][p][/p][p]This shows what boxcasting looks like. There is a small stone prefab partially inside the shard. Raycasts are made from each point shown, but only failed raycasts show a red line. I would normally end the test on the first failed raycast but I let it run to demonstrate the full box.[/p][p][/p][p][/p][p]This shows each valid point cloud position being tested for each spawn prefab type.[/p][p][/p][p][/p][h3]The Result[/h3][p]I end up with a highly configurable and easily repeatable process to create or tweak rock formations to allow for player destruction that can spawn new resources for harvesting.[/p][p]This effort also leads up to other game mechanics coming during early access such as building destruction and allowing for the player to clear collapsed tunnels and break through walls on adventures, if they have the right tools and skills of course.[/p][p][/p][previewyoutube][/previewyoutube][p][/p][h3]Feedback[/h3][p]I'm not sure how many of these developer-focused blogs I'll make. Let me know in the comments if these types of posts interest you.[/p][p][/p]