abhinavrastogi / simple-terrain

Vanilla JS implementation of Diamond-Square algorithm for generating realistic terrain heightmaps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Diamond Square Terrain Generation

This is a simple implementation of the Diamond Square Algorithm in vanilla JS ES5. This algo is used to generate semi-realistic terrain heightmaps.

Quoting Wiki for definition:

The diamond-square algorithm is a method for generating heightmaps for computer graphics. It is a slightly better algorithm than the three-dimensional implementation of the midpoint displacement algorithm which produces two-dimensional landscapes. It is also known as the random midpoint displacement fractal, the cloud fractal or the plasma fractal, because of the plasma effect produced when applied.

Demo

http://abhinavrastogi.github.io/simple-terrain

Usage

The module exports a single function, which accepts the size n of the terrain you want to generate and returns a 2D array of size 2^n x 2^n. The value of each point in the terrain can range from 0 to 1.

var terrain = diamondSquareTerrain(<size in order of 2>);

Example:

var diamondSquareTerrain = require('diamond-square-terrain');
var terrain = diamondSquareTerrain(8); // Returns a 2D array of size 256x256
var terrain = diamondSquareTerrain(5); // Returns a 2D array of size 32x32

Sample output

Array mapped to an image

About

Vanilla JS implementation of Diamond-Square algorithm for generating realistic terrain heightmaps


Languages

Language:JavaScript 81.9%Language:HTML 18.1%