Believe it or not, I never used the MicroSplat Shader that I bought for $5 - I ended up writing my own Digging Script. First thing it did, was destroy everything, so I quickly gave up lol… Anyway, I’ll leave a copy of the early prototype here, if anyone wants to use it or modify it as they wish:
using UnityEngine;
public class TerrainDigger : MonoBehaviour
{
public Terrain terrain;
public float digDepth = 1f;
public float digRadius = 2f;
private float[,] initialHeights;
void Start()
{
SaveInitialHeights();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
DigHole(hit.point);
}
}
}
void OnApplicationQuit()
{
RestoreInitialHeights();
}
void SaveInitialHeights()
{
TerrainData terrainData = terrain.terrainData;
initialHeights = terrainData.GetHeights(0, 0, terrainData.heightmapResolution, terrainData.heightmapResolution);
}
void RestoreInitialHeights()
{
TerrainData terrainData = terrain.terrainData;
terrainData.SetHeights(0, 0, initialHeights);
}
void DigHole(Vector3 position)
{
TerrainData terrainData = terrain.terrainData;
Vector3 terrainPosition = position - terrain.transform.position;
int x = (int)((terrainPosition.x / terrainData.size.x) * terrainData.heightmapResolution);
int z = (int)((terrainPosition.z / terrainData.size.z) * terrainData.heightmapResolution);
int radius = Mathf.RoundToInt(digRadius / terrainData.size.x * terrainData.heightmapResolution);
float[,] heights = terrainData.GetHeights(x - radius, z - radius, radius * 2, radius * 2);
for (int i = 0; i < heights.GetLength(0); i++)
{
for (int j = 0; j < heights.GetLength(1); j++)
{
heights[i, j] -= digDepth / terrainData.size.y;
}
}
terrainData.SetHeights(x - radius, z - radius, heights);
}
}
To use it, create an empty gameObject in your scene, place the script on it, adjust the values and assign it your terrain, and enjoy. It’s an early script though, so you’ll have to modify it on your own to your own needs
Because whatever you do to the terrain in the game remains in the scene as well (BE CAREFUL WITH THAT ONE), I also integrated a mini-saving script, so the Terrain returns to its original form when you return to edit mode. If you want to save and restore it in the game, you’ll have to use Brian’s JSON Saving System (or the Binary Saving System, whichever you’re using) for that
And now I got a mini taste of voxel-based work in the engine
Anyway, I will go back to finding ways to integrate this script into my workflow. Enjoy the little gift (whoever reads this, that is)