mirror of
https://github.com/TangentFoxy/heightmap.git
synced 2025-07-27 18:32:16 +00:00
remove deprecated module(), better README
This commit is contained in:
36
README.md
36
README.md
@@ -11,33 +11,35 @@ The heightmap module uses the diamond-square algorithm to generate cloud or plas
|
|||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
|
|
||||||
-- import module
|
```lua
|
||||||
require "heightmap"
|
-- import module
|
||||||
|
local heightmap = require "heightmap"
|
||||||
|
|
||||||
-- create 32x32 heightmap
|
-- create 32x32 heightmap
|
||||||
map = heightmap.create(32, 32)
|
map = heightmap.create(32, 32)
|
||||||
|
|
||||||
-- examine each height value
|
-- examine each height value
|
||||||
for x = 0, map.w do
|
for x = 0, map.w do
|
||||||
for y = 0, map.h do
|
for y = 0, map.h do
|
||||||
print(map[x][y])
|
print(map[x][y])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- define a custom height function
|
-- define a custom height function
|
||||||
-- (reusing the default but scaling it)
|
-- (reusing the default but scaling it)
|
||||||
function f(map, x, y, d, h)
|
function f(map, x, y, d, h)
|
||||||
return 2 * heightmap.defaultf(map, x, y, d, h)
|
return 2 * heightmap.defaultf(map, x, y, d, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- use it to create a larger non-square heightmap
|
-- use it to create a larger non-square heightmap
|
||||||
map = heightmap.create(100, 200, f)
|
map = heightmap.create(100, 200, f)
|
||||||
|
|
||||||
-- create a map with normalized range of -1 to 1
|
-- create a map with normalized range of -1 to 1
|
||||||
map = heightmap.create(150, 300, -1, 1)
|
map = heightmap.create(150, 300, -1, 1)
|
||||||
|
|
||||||
-- use the custom height function and normalize with a range of 10 to 20
|
-- use the custom height function and normalize with a range of 10 to 20
|
||||||
map = heightmap.create(200, 200, f, 10, 20)
|
map = heightmap.create(200, 200, f, 10, 20)
|
||||||
|
```
|
||||||
|
|
||||||
How it Works
|
How it Works
|
||||||
------------
|
------------
|
||||||
|
@@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
local max, random = math.max, math.random
|
local max, random = math.max, math.random
|
||||||
|
|
||||||
module(...)
|
|
||||||
|
|
||||||
-- Find power of two sufficient for size
|
-- Find power of two sufficient for size
|
||||||
local function pot(size)
|
local function pot(size)
|
||||||
local pot = 2
|
local pot = 2
|
||||||
@@ -88,13 +86,13 @@ end
|
|||||||
-- d is depth (from size to 1 by powers of two)
|
-- d is depth (from size to 1 by powers of two)
|
||||||
-- h is mean height at map[x][y] (from square/diamond of radius d)
|
-- h is mean height at map[x][y] (from square/diamond of radius d)
|
||||||
-- returns h' which is used to set map[x][y]
|
-- returns h' which is used to set map[x][y]
|
||||||
function defaultf(map, x, y, d, h)
|
local function defaultf(map, x, y, d, h)
|
||||||
return h + (random()-0.5)*d
|
return h + (random()-0.5)*d
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Create a heightmap using the specified height function (or default)
|
-- Create a heightmap using the specified height function (or default)
|
||||||
-- map[x][y] where x from 0 to map.w and y from 0 to map.h
|
-- map[x][y] where x from 0 to map.w and y from 0 to map.h
|
||||||
function create(width, height, f)
|
local function create(width, height, f)
|
||||||
f = f and f or defaultf
|
f = f and f or defaultf
|
||||||
-- make heightmap
|
-- make heightmap
|
||||||
local map = diamondsquare(pot(max(width, height)), f)
|
local map = diamondsquare(pot(max(width, height)), f)
|
||||||
@@ -104,3 +102,8 @@ function create(width, height, f)
|
|||||||
map.w, map.h = width, height
|
map.w, map.h = width, height
|
||||||
return map
|
return map
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
defaultf = defaultf,
|
||||||
|
create = create
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user