Loise

Build Status LFE Versions Erlang Versions Tags

A noise library for LFE/Erlang

Loise project logo

Contents

Introduction

This is a library, written in LFE, useful for generating Perlin and Simplex noise. Perlin noise is a computer-generated visual effect developed by Ken Perlin, who won an Academy Award for Technical Achievement for inventing it. It can be used to simulate elements from nature, and is especially useful in circumstances where computer memory is limited. (See the complete Perlin Wikipedia article.)

Simplex noise, on the other hand, is a method for constructing an n-dimensional noise function comparable to Perlin noise ("classic" noise) but with a lower computational overhead, especially in larger dimensions. Ken Perlin designed the algorithm in 2001 to address the limitations of his classic noise function, especially in higher dimensions. (See the complete Simplex Wikipedia article for more.)

Background

The loise project stated life as a port of the Racket noise-generator by jpverkamp to LFE. However, it has undergone some seriosu refactoring since then, as well as the inclusion of many new features.

Dependencies

This project requires that you have Erlang installed (tested with R15B03, R16B03, 17.5, 18.0, and 18.3). It also assumes that you have rebar3 installed somwhere in your $PATH.

Usage

The data generated with the perlin and simplex functions can be used to create images. Erlang is not a good language for image generation, however this library does provide some convenience functions for generating images.

Preliminary steps:

$ rebar3 compile
$ rebar3 lfe repl

Starting

The loise library maintains state and as such must be run in order to use:

lfe> (loise:start)
#(ok (loise))

Once this is done, the examples below will be ready to run.

Perlin

Below are 5 perlin noise images generated at 1x, 2x, 4x, 8x, and 16x respectively.

These were generated with the following from the REPL:

lfe> (set opts #m(noise perlin multiplier 1))
#M(multiplier 1 noise perlin)
lfe> (loise:image "perlin-1.png")
ok
lfe> (loise:image "perlin-2.png" (mupd opts 'multiplier 2))
ok
lfe> (loise:image "perlin-4.png" (mupd opts 'multiplier 4))
ok
lfe> (loise:image "perlin-8.png" (mupd opts 'multiplier 8))
ok
lfe> (loise:image "perlin-16.png" (mupd opts 'multiplier 16))
ok

You can also limit the number of gradations for the shades of grey, giving the images a more "layered" or "topographical" look:

lfe> (set opts `#m(noise perlin
multiplier 4
graded? true
grades-count 8))
lfe> (loise:image "perlin-8-shades.png" opts)
ok

Which will create the following:

You may also change the permutation table from the default, to one generated with a random seed:

lfe> (set opts (maps:merge opts #m(random? true
graded? false
seed 4)))
lfe> (loise:image "perlin-rand-1.png" opts)
ok
lfe> (loise:image "perlin-rand-2.png" (mupd opts 'seed '(4 2)))
ok
lfe> (loise:image "perlin-rand-3.png" (mupd opts 'seed (4 2 42)))
ok

Simplex

Below are 5 simplex noise images generated at 1x, 2x, 4x, 8x, and 16x respectively.

These were generated with the following from the REPL:

lfe> (set opts #m(noise simplex multiplier 1))
#M(multiplier 1 noise simplex)
lfe> (loise:image "simplex-1.png")
ok
lfe> (loise:image "simplex-2.png" (mupd opts 'multiplier 2))
ok
lfe> (loise:image "simplex-2.png" (mupd opts 'multiplier 2))
ok
lfe> (loise:image "simplex-8.png" (mupd opts 'multiplier 8))
ok
lfe> (loise:image "simplex-16.png" (mupd opts 'multiplier 16))
ok

Just as with perlin, simplex allows you to limit the number of gradations for the shades of grey:

lfe> (set opts (mset opts 'graded? 'true 'grades-count 5 'multiplier 4))
lfe> (loise:image "simplex-5-shades.png" opts)
ok

Which will create the following:

You may also change the permutation table from the default, to one generated with a random seed:

lfe> (set opts (mset opts 'random? 'true 'graded? 'false 'seed 4))
lfe> (loise:image "simplex-rand-1.png")
ok
lfe> (loise:image "simplex-rand-2.png" (mupd opts 'seed '(4 2)))
ok
lfe> (loise:image "simplex-rand-3.png" (mupd opts 'seed '(4 2 42)))
ok

You may either pass an integer or a list of 1, 2 or 3 integers as values for the seed option key. Note that without different seeds, the same image would be generated for multiple calls to loise:image.

ASCII

You can also generate ASCII "images" with loise:

lfe> (loise:format-ascii #m(noise perlin color? true))

And this:

lfe> (loise:format-ascii #m(noise simplex color? true))

The ASCII annlog to the greyscale PNG noise images is simply a gride without color:

lfe> (loise:format-ascii #m(noise simplex color? false))

You are not bound to the default ASCII represnetation nor default color scheme. In the following example, we can generate a landscape that includes alpine forests and grasslands and greatly increase the map area in the terminal:

lfe> (set opts #m(color? true
width 282
height 94
multiplier 2.5
graded? true
grades-count 9
ascii-map ("A" "^" "!" "n" "*" "-" "~" "~" "~")
colors (whiteb yellow green green greenb green
blue blue blue)))
lfe> (loise:format-ascii opts)

As with the PNG images, ASCII output may be randomized by setting different seeds:

By default, loise uses a pre-generated "permutation table" to generate patterns. You can view this table in src/loise-defaults.lfe. If you would like to generate your own for more random results, you will need to enable the random option and then generate a new table:

lfe> (set opts #m(random? true graded? true seed 4))
lfe> (loise:format-ascii opts)
lfe> (loise:format-ascii (mupd opts 'seed '(4 2)))
lfe> (loise:format-ascii (mupd opts 'seed '(4 2 42)))

From the REPL

You can now use the loise noise functions by themseves, if you so desire (without having to render output, etc.). Here is some example usage:

lfe> (loise:perlin 3.14 1.59 2.65)
-0.3772216257243449
lfe> (loise:simplex 0.1)
0.4410072765
lfe> (loise:simplex 0.1 0.2)
0.9410934374999996
lfe> (loise:simplex 0.1 0.2 0.9)
-0.07602014100000003

Note that, depending upon your random and seed settings, you may get different values than those shown above.

Or, iterating over some values in one dimension:

lfe> (set input (list-comp ((<- x (lists:seq 0 9))) (/ x 10)))
(0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9)
lfe> (list-comp ((<- x input))
(loise:round
(loise:perlin x)
2))
(0.0 0.11 0.23 0.37 0.46 0.5 0.46 0.37 0.23 0.11)
lfe> (list-comp ((<- x input))
(list-comp ((<- y input))
(loise:round
(loise:perlin x y)
2)))

In a Module

(defmodule mymodule
(export
(get-perlin-pie 0)
(get-simplex-pie 0)))
(def get-perlin-pie ()
(loise:perlin 3.14 1.59 2.65))
(def get-simplex-pie ()
(loise:simplex 3.14 1.59 2.65))

License

Copyright © 2013-2021 Duncan McGreggor
Distributed under the Apache License, Version 2.0.