Building a world is no easy task—the one we live in took somewhere between six days and 13 billion years, depending on who you ask. If you ask self-described JavaScript junkie Hunter Loftis, though, he might have a different opinion. Loftis developed a startlingly simple method for generating beautiful, realistic terrain, and he’s sharing it with the whole Internet.In a process that uses only 120 lines of code, Loftis’ directions turn a flat grid into one of nearly infinite complexity. The code relies on fractal imaging, a geometric concept in which each section of the shape has the same statistical character as the whole shape. This is one of the best ways to mathematically model seemingly chaotic patterns, such as crystal growth or coastal erosion. In this case, Loftis is using it to model entire chunks of terrain. Here is a demo of the randomized terrain generation. Hit the refresh button to generated new combinations of enchanting hills, mountains, canyons, and valleys.
Advertisement
Modeling realistic terrain in and of itself isn’t the most momentous thing about this project, though. The real kicker is the simplicity of the code. It works by taking a simple grid and continually dividing it into smaller and smaller grids. As each smaller grid is created, the randomizing function splits the grids into alternating squares and diamonds. Then it creates the new randomized terrain for the next set of grids based on data points from the previous square or diamond. You can read Loftis’ in-depth explanation here.
The result is truly random terrain, but with graduated areas of randomization. This algorithm keeps randomly generated terrain from erecting giant towers one pixel wide next to a plane pockmarked with deep, pixelated pits. In short, it makes the terrain look natural.Loftis closes his explanation with a challenge: “If you're anything like me, the results of this simple algorithm leave you itching to go build an online Terragen, a jetpack-based first person shooter, fishing simulator, MMORPG, etc. This singlecube, canvasprojected demo practically begs for extension.”And the script-slinging population of the web is responding:
ORIGINAL REPORTING ON EVERYTHING THAT MATTERS IN YOUR INBOX.
By signing up, you agree to the Terms of Use and Privacy Policy & to receive electronic communications from Vice Media Group, which may include marketing promotions, advertisements and sponsored content.