Jump to content

Terrain in Leadwerks 5


Josh

5,047 views

 Share

I wanted to work on something a bit easier before going back into voxel ray tracing, which is another difficult problem. "Something easier" was terrain, and it ended up consuming the entire month of August, but I think you will agree it was worthwhile.

In Leadwerks Game Engine, I used clipmaps to pre-render the terrain around the camera to a series of cascading textures. You can read about the implementation here:

This worked very well with the hardware we had available at the time, but did result in some blurriness in the terrain surface at far distances. At the time this was invented, we had some really severe hardware restrictions, so this was the best solution then. I also did some experiments with tessellation, but a finished version was never released.

New Terrain System

Vulkan gives us a lot more freedom to follow our dreams. When designing a new system, I find it useful to come up with a list of attributes I care about, and then look for the engineering solution that best meets those needs.

Here's what we want:

  • Unlimited number of texture layers
  • Pixel-perfect resolution at any distance
  • Support for tessellation, including physics that match the tessellated surface.
  • Fast performance independent from the number of texture layers (more layers should not slow down the renderer)

Hardware tessellation is easy to make a basic demo for, but it is hard to turn it into a usable feature, so I decided to attack this first. You can read my articles about the implementation below. Once I got the system worked out for models, it was pretty easy to carry that over to terrain.

So then I turned my attention to the basic terrain system. In the new engine, terrain is a regular old entity. This means you can move it, rotate it, and even flip it upside down to make a cave level. Ever wonder what a rotated terrain looks like?

Untitled.thumb.jpg.cc0950f13ffcf999d8bb9d5468b7d062.jpg

Now you know.

You can create multiple terrains, instead of just having one terrain per world like in Leadwerks. If you just need a little patch of terrain in a mostly indoor scene, you can create one with exactly the dimensions you want and place it wherever you like. And because terrain is running through the exact same rendering path as models, shadows work exactly the same.

Here is some of the terrain API, which will be documented in the new engine:

shared_ptr<Terrain> CreateTerrain(shared_ptr<World> world, const int tilesx, const int tiles, const int patchsize = 32, const int LODLevels = 4)
shared_ptr<Material> Terrain::GetMaterial(const int x, const int y, const int index = 0)
float Terrain::GetHeight(const int x, const int y, const bool global = true)
void Terrain::SetHeight(const int x, const int y, const float height)
void Terrain::SetSlopeConstraints(const float minimum, const float maximum, const float range, const int layer)
void Terrain::SetHeightConstraints(const float minimum, const float maximum, const float range, const int layer)
int Terrain::AddLayer(shared_ptr<Material> material)
void Terrain::SetMaterial(const int index, const int x, const int y, const float strength = 1.0, const int threadindex = 0)
Vec3 Terrain::GetNormal(const int x, const int y)
float Terrain::GetSlope(const int x, const int y)
void Terrain::UpdateNormals()
void Terrain::UpdateNormals(const int x, const int y, const int width, const int height)
float Terrain::GetMaterialStrength(const int x, const int y, const int index)

What I came up with is flexible it can be used in three ways.

  • Create one big terrain split up into segments (like Leadwerks Engine does, except non-square terrains are now supported).
  • Create small patches of terrain to fit in a specific area.
  • Create many terrains and tile them to simulate very large areas.

Updating Normals

I spent almost a full day trying to calculate terrain normal in local space. When they were scaled up in a non-linear scale, the PN Quads started to produce waves. I finally realized that normal cannot really be scaled. The scaled vector, even if normalized, is not the correct normal. I searched for some information on this issue, but the only thing I could find is a few mentions of an article called "Abnormal Normals" by someone named Eric Haines, but it seems the original article has gone down the memory hole. In retrospect it makes sense if I picture the normal vectors rotating instead of shifting each axis. So bottom line is that normal for any surface have to be recalculated if a non-uniform scale is used.

I'm doing more things on the CPU in this design because the terrain system is more complex, and because it's a lot harder to get Vulkan to do anything. I might move it over to the GPU in the future but for right now I will stick with the CPU. I used multithreading to improve performance by a lot:

Physics

Newton Dynamics provides a way to dynamically calculate triangles for collision. This will be used to calculate a high-res collision mesh on-the-fly for physics. (For future development.) Something similar could probably be done for the picking system, but that might not be a great idea to do.

Materials

At first I thought I would implement a system where one terrain vertex just has one material, but it quickly became apparent that this would result in very "square" patterns, and that per-vertex blending between multiple materials would be needed. You can see below the transitions between materials form a blocky pattern.

splat1.thumb.jpg.c2326e1a9fe8d375b850ef97c82674e0.jpg

So I came up with a more advanced system that gives nice smooth transitions between multiple materials, but is still very fast:

splat2.thumb.jpg.6744367ea0b04321f59d16c79b81ad3f.jpg

The new terrain system supports up to 256 different materials per terrain. I've worked out a system that runs fast no matter how many material layers you use, so you don't have to be concerned at all about using too many layers. You will run out of video memory before you run out of options.

Each layer uses a PBR material with full support for metalness, roughness, and reflections. This allows a wider range of materials, like slick shiny obsidian rocks and reflective ice. When combined with tessellation, it is possible to make snow that actually looks like snow.

snow.jpg.b1c5d0bfc614743aaa34b10bdc80b722.jpg

 Instancing

Like any other entity, terrain can be copied or instantiated. If you make an instance of a terrain, it will use the same height, material, normal, and alpha data as the original. When the new editor arrives, I expect that will allow you to modify one terrain and see the results appear on the other instance immediately. A lot of "capture the flag" maps have two identical sides facing each other, so this could be good for that.

untitled.png.a054b45aef9263a561fdf9fa874b0f2f.png

Final Shots

Loading up "The Zone" with a single displacement map added to one material produced some very nice results.

t1.thumb.jpg.c7ab92b604fa089e8b51b1765d4661f1.jpg

t2.thumb.jpg.5efa1b4ebe5b16c159be8b15d4599d7f.jpg

t3.thumb.jpg.c398bf00d33a3c2e120b77afb26cfb64.jpg

The new terrain system will be very flexible, it looks great, and it runs fast. (Tessellation requires a high-end GPU, but can be disabled.) I think this is one of the features that will make people very excited about using the new Turbo Game Engine when it comes out.

  • Like 9
 Share

8 Comments


Recommended Comments

Wonderful blog post!

When can we get a new "beta" for this?

Vegetation is the only feature missing to complete everything I need for where I am going with my game. I will be hiring a full time content creator on Oct 1st, so this is exciting.
 

Link to comment

@martyj Currently there is a bug that randomly makes the whole terrain black. Probably texture data not being sent correctly to the GPU. I need to fix that, and I might implement directional light shadows before an update, if they don't take too much time.

Link to comment
54 minutes ago, havenphillip said:

Would be cool if we could add a specular texture on the new terrain so as to get that after rain look or add oil spots etc.

Metalness / roughness will do this.

Link to comment
2 hours ago, Josh said:

@martyj Currently there is a bug that randomly makes the whole terrain black. Probably texture data not being sent correctly to the GPU. I need to fix that, and I might implement directional light shadows before an update, if they don't take too much time.

Try running it on an AMD card.

Back when I was working on OpenGL I had a bug where a uniform was set to zero by default on Nvidia, yet it was randomized on AMD.

If the terrain flashes random colors, you know that's your issue.

  • Like 1
Link to comment

This is very sweet.  Curious if we'll get oceans back and possibly rivers but I know you're not there yet.  Edit: come to think of it, do you think it would be easier to implement with your new renderer and Vulkan?

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...