Difference between revisions of "love.math.noise"

m (Changed color values)
(Examples)
Line 60: Line 60:
  
 
function love.draw()
 
function love.draw()
     for x = 1, #grid do
+
    local gridSize = 8
         for y = 1, #grid[x] do
+
     for y = 1, #grid do
             local f = 1 * grid[x][y]
+
         for x = 1, #grid[y] do
             love.graphics.setColor( f, f, f, 1 )
+
             local value = grid[y][x]
             love.graphics.rectangle( 'fill', x * 8, y * 8, 7, 7 )
+
             love.graphics.setColor( value, value, value )
            love.graphics.setColor( 1, 1, 1, 1 )
+
             love.graphics.rectangle( 'fill', x * gridSize, y * gridSize, gridSize-1, gridSize-1 )
 
         end
 
         end
 
     end
 
     end
Line 72: Line 72:
 
-- Fill each pixel in our grid with simplex noise.
 
-- Fill each pixel in our grid with simplex noise.
 
local function noise()
 
local function noise()
     for x = 1, 60 do
+
    local randomX = love.math.random(1024)
         for y = 1, 60 do
+
    local randomY = love.math.random(1024)
             grid[x] = grid[x] or {}
+
     for y = 1, 60 do
            grid[x][y] = love.math.noise( x + love.math.random(), y + love.math.random() )
+
    grod[y] = {}
 +
         for x = 1, 60 do
 +
             grid[y][x] = love.math.noise(0.1*x + randomX, 0.2*y + randomY)
 
         end
 
         end
 
     end
 
     end

Revision as of 13:38, 26 September 2022

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Generates a Simplex or Perlin noise value in 1-4 dimensions. The return value will always be the same, given the same arguments.

Simplex noise is closely related to Perlin noise. It is widely used for procedural content generation.

There are many webpages which discuss Perlin and Simplex noise in detail.

O.png The return value might be constant if only integer arguments are used. Avoid solely passing in integers, to get varying return values.  


Function

Generates Simplex noise from 1 dimension.

Synopsis

value = love.math.noise( x )

Arguments

number x
The number used to generate the noise value.

Returns

number value
The noise value in the range of [0, 1].

Function

Generates Simplex noise from 2 dimensions.

Synopsis

value = love.math.noise( x, y )

Arguments

number x
The first value of the 2-dimensional vector used to generate the noise value.
number y
The second value of the 2-dimensional vector used to generate the noise value.

Returns

number value
The noise value in the range of [0, 1].

Function

Generates Perlin noise (Simplex noise in version 0.9.2 and older) from 3 dimensions.

Synopsis

value = love.math.noise( x, y, z )

Arguments

number x
The first value of the 3-dimensional vector used to generate the noise value.
number y
The second value of the 3-dimensional vector used to generate the noise value.
number z
The third value of the 3-dimensional vector used to generate the noise value.

Returns

number value
The noise value in the range of [0, 1].

Function

Generates Perlin noise (Simplex noise in version 0.9.2 and older) from 4 dimensions.

Synopsis

value = love.math.noise( x, y, z, w )

Arguments

number x
The first value of the 4-dimensional vector used to generate the noise value.
number y
The second value of the 4-dimensional vector used to generate the noise value.
number z
The third value of the 4-dimensional vector used to generate the noise value.
number w
The fourth value of the 4-dimensional vector used to generate the noise value.

Returns

number value
The noise value in the range of [0, 1].

Examples

Fills a two-dimensional grid with simplex noise each time a key is pressed.

local grid = {}

function love.draw()
    local gridSize = 8
    for y = 1, #grid do
        for x = 1, #grid[y] do
            local value = grid[y][x]
            love.graphics.setColor( value, value, value )
            love.graphics.rectangle( 'fill', x * gridSize, y * gridSize, gridSize-1, gridSize-1 )
        end
    end
end

-- Fill each pixel in our grid with simplex noise.
local function noise()
    local randomX = love.math.random(1024)
    local randomY = love.math.random(1024)
    for y = 1, 60 do
    grod[y] = {}
        for x = 1, 60 do
            grid[y][x] = love.math.noise(0.1*x + randomX, 0.2*y + randomY)
        end
    end
end

function love.keypressed()
    noise()
end

See Also

Other Languages