Aug
11
2013

Scorched Earth Terrain (wip)

scorched_earth_terrain_wip_unity_1

This is another thing that i’ve always wanted to try in Unity, Scorched Earth deformable collapsing terrain (youtube link).

Ideas:
1 – Using SetPixels() with texture would most likely be easiest, just slow on bigger resolutions (but no need high resolution for this anyways?)
2 – 2D marching squares (unifywiki), but how to do falling ground..?
3 – High resolution mesh, then adjust vertices for making holes (and create extra falling mesh slices..?)
4 – 2D cellular automata thing, again its using SetPixels(), but would be nice to have 2d water on the terrain actually..
5 – Bitmap2Mesh, also using SetPixels() with texture, but could be made to use just array only..?

Idea#3: High resolution quad mesh
– Testing this first.. main screenshot is starting point (mesh, screenwidth, resolution = 1)
– Not sure yet how to handle collisions
– Making holes: Sphere(s) defines holes, all vertices inside sphere are moved down, until not inside sphere
– Collapsing: Create new mesh slices, same amount as those vertices that were moved down. Slice height = amount the vertice was moved down.. Then somehow animate them falling down.
– Weld terrain back to 1 mesh: Go trhough each slice, move the vertices below them, up to slice top Y, destroy slices.
– Problems: What is there are multiple explosions in different places..will get lots of slices on top of each other?
– Added webplayer (v1): Can destroy ground, slices are created and for now they fall with rigidbody (but wont stop), could use iTween for moving slices?
– Could probably decrease vertex count by 50%, since now each terrain slice/column is 4 vertices (not shared).
– Added webplayer (v2): Main thing missing is spherical holes from explosions..
– Added webplayer (v3):
– Spherical holes on ground using : [charp]… float angle = Mathf.Asin(counter/radius);  float height = Mathf.Cos(angle)*radius; …[/charp]
– Ground plane has now half the vertices (and its smoother, since top  vertices connect to previous one)
– Exploded ground slices now fall with gravity (code from http://stackoverflow.com/questions/3966188/c-sharp-xna-simulate-gravity )
– Falling slices are shown red, then after every slice has stopped, terrain vertices is set to those positions (and slices are destoyed)

Sources: (not finished or cleaned up..)
https://github.com/unitycoder/ScorchedEarthTerrainFail

Webplayer:
http://unitycoder.com/upload/demos/ScorchedEarthTerrain1/ (v1)
http://unitycoder.com/upload/demos/ScorchedEarthTerrain2/ (v2)
http://unitycoder.com/upload/demos/ScorchedEarthTerrain3/ (v3)

Image#2: Basic terrain shape (perlin noise), Vertices are moved down when clicking the mountain

scorched_earth_terrain_wip_unity_2

Image#3: Problems if using UV maps on terrain, since if we cut part of terrain at middle, we should add more vertices there to keep UV values.. (now we have vertices only in top of the terrain and bottom, not middle). Problem creating circular holes at the moment.. Slices are now welded to main terrain after movement stops.

scorched_earth_terrain_wip_unity_3

Image#4: Explosion shader (see source below) test with quad mesh primitive. Smooth edges, compared to using mesh circle. Idea is from here: Planetary rings shader

shader_explosion_circle_gradient_unity


Shader "unityCoder/explocircleTest1" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Radius ("Radius", float) = 0.5
}

SubShader {
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
}
LOD 200
Lighting Off

CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
float _Radius;

struct Input {
float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o)
{
float dist = distance(IN.uv_MainTex, float2(0.5,0.5));
if ( abs(dist) > _Radius)     clip (-1.0);
float val = dist*128;
float c1 = 1-sin(val+_Time.x)+0.5;
float c2 = cos(val+_Time.y)+0.5;
float c3 = 0;
o.Albedo = float3(c1,c2,c3)*3;
o.Alpha = 1;
}
ENDCG
}
FallBack "Diffuse"
}

Image#5: Almost.. (see webplayer v3), but wont be able to do those small 1 pixel dirt pieces falling down like in original..not with meshes, texture with SetPixels() would be the only way i’m afraid..
scorched_earth_terrain_wip_unity_4


9 Comments + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.