Giter Site home page Giter Site logo

techdojo / heightmap Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mlepage/heightmap

0.0 2.0 0.0 102 KB

Simple heightmap module for Lua. Uses the diamond-square algorithm to generate realistic terrain. MIT license.

License: MIT License

Lua 100.00%

heightmap's Introduction

Heightmap

Overview

A Lua module by Marc Lepage for producing heightmaps.

The heightmap module uses the diamond-square algorithm to generate cloud or plasma fractal heightmaps which can be used for terrain.

Usage

-- import module
require "heightmap"

-- create 32x32 heightmap
map = heightmap.create(32, 32)

-- examine each height value
for x = 0, map.w do
    for y = 0, map.h do
        print(map[x][y])
    end
end

-- define a custom height function
-- (reusing the default but scaling it)
function f(map, x, y, d, h)
    return 2 * heightmap.defaultf(map, x, y, d, h)
end

-- use it to create a larger non-square heightmap
map = heightmap.create(100, 200, f)

How it Works

The heightmap must be a square the size of a power of two, plus one, so that it can be evenly divided. For example, 4x4 cells will require 5x5 vertices. If another size is specified, a sufficiently large power of two square will be used, and the result clipped to the desired size.

First the four corners are seeded with a random value (C).

Then each square is used to set the value of its center (S) based on the average of its four corners (plus some randomness).

Then each diamond is used to set the value of its center (D) based on the average of its four points (plus some randomness).

The square and diamond steps continue until all values have been set:

  4     S 2    D 2    S 1    D 1
C...C  c...c  c.D.c  c.d.c  cDdDc
.....  .....  .....  .S.S.  DsDsD
.....  ..S..  D.s.D  d.s.d  dDsDd
.....  .....  .....  .S.S.  DsDsD
C...C  c...c  c.D.c  c.d.c  cDdDc

The default height function randomly displaces values by up to +/- 0.5 of the step size. So above, the corners will be from -2 to +2, the center will be the mean of the corners randomly displaced from -1 to +1, and so on.

Resources

heightmap's People

Contributors

mlepage avatar oddity avatar

Watchers

Jon Howard avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.