Real-time Terrain Deformation in Unity3D
June 10th, 2010 by Devin ReimerA few weeks back I got thinking. Would it be possible to have a terrain within Unity deform in real-time? For example if an explosion went off, could it blow a hole in the terrain? I decided this would be a good project to learn the more ‘advanced’ features of Terrains within Unity.
It ended up being more challenging than I predicted, as it was difficult to get the framerate to remain stable while performing these operation. Using a few tricks I ended up getting it to work exactly as I had hoped.
The demo works like this. A script randomly spawns ‘shells’ that fall quickly to the ground. Each shell has an onTrigger collider attached to it. If this collides with a terrain that has a TerrainDeformer component, the shell tells that component its location and the force of the explosion. The shell then instantiate an explosion (supplied by Ben Throop great Detonator framework), and removes itself.
The TerrainDeformer script then translates that position to the correct location relative to it’s terrain and modifies both the heightmap (terrain height) and the alphamap (terrain texture). A bit of math is used to find all the heightmap and alphamap position within the area of the impact circle.
The texture which is used to re-texture the impact area is chosen from the list of terrain textures based on the numerical index value passed into the script (Terrain Deformation Texture Num). In this example it is set to one, so it will re-texture with the second texture in the list.
When creating your own terrains it is necessary for the height of the terrain to be higher than 0 meters so craters can be formed. I recommend a depth of at least 3 meters. This can done by setting the terrain height to something higher than 3 meters, then clicking Terrain->Flatten Heightmap and entering 3 meters.
For performance reasons it is important to keep your terrain size small and more importantly keep your Heightmap Resolution low. In this example it is set to 33.
The current version does have limitations like lack of terrain edge detection and support for multiple terrains. This example is more of proof of concept to show that not only can it be done, but done without much of a performance hit.
Thanks to Calin for creating the dirt texture.
To check out the demo click here.
To get the source (unitypackage) click here.
Source Requirements: Unity 2.6 and the Detonator Framework.
Tags: Unity3D



June 15th, 2010 at 8:26 PM
This will be very helpful and time saving to look at. I want to have buildings and other things break and deform too in an open world game idea I have. Keep up the good work!
July 2nd, 2010 at 1:16 PM
[...] continuer sur le thème de la déformation de mesh, voici un projet gratuit du site almostlogical. Cette fois-ci les scripts fournis permettent de déformer un terrain à des endroits indiqués ou [...]
August 1st, 2010 at 4:22 AM
nice one. thanx!
I am looking for a way to crumble terrain. do you know a way for this, too?
August 15th, 2010 at 1:49 PM
@headkit I don’t know exactly what you mean by crumble. If you mean make ‘crater’ slowly, well yes you could using the same technique. That being said editing the terrain at runtime is performance hog. So lots of small changes can impact performance.
August 18th, 2010 at 9:22 PM
Hi Devin. This post is pretty nice. I have a question here:
I tried your demo and I feel that the depth of the crate depends on the ‘terrain height’ of the terrain. For example, if the height is 6, the crate is fleet and if the height is 60, then the crate will be very deep. I wonder if it is possible that when the height is 60 and I can make some shallow crates just like when the height is 6?
I modified the codes and nothing changes and the properties of terrain in Unity 3d really confused me. Can you offer some help here? Thanks.
August 19th, 2010 at 12:24 PM
Hi Devin, it was my mistake. Please ignore my last post.