Perlin Noise - HELP Translating Pseudo code to Lua

Can someone help me translating this pseudo code to pure Lua?

I’m having difficulty in some parts.

Origin: http://freespace.virgin.net/hugo.elias/models/m_perlin.htm

function Noise1(integer x, integer y) n = x + y \* 57 n = (n\<\<13) ^ n; return ( 1.0 - ( (n \* (n \* n \* 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0); end function function SmoothNoise\_1(float x, float y) corners = ( Noise(x-1, y-1)+Noise(x+1, y-1)+Noise(x-1, y+1)+Noise(x+1, y+1) ) / 16 sides = ( Noise(x-1, y) +Noise(x+1, y) +Noise(x, y-1) +Noise(x, y+1) ) / 8 center = Noise(x, y) / 4 return corners + sides + center end function function InterpolatedNoise\_1(float x, float y) integer\_X = int(x) fractional\_X = x - integer\_X integer\_Y = int(y) fractional\_Y = y - integer\_Y v1 = SmoothedNoise1(integer\_X, integer\_Y) v2 = SmoothedNoise1(integer\_X + 1, integer\_Y) v3 = SmoothedNoise1(integer\_X, integer\_Y + 1) v4 = SmoothedNoise1(integer\_X + 1, integer\_Y + 1) i1 = Interpolate(v1 , v2 , fractional\_X) i2 = Interpolate(v3 , v4 , fractional\_X) return Interpolate(i1 , i2 , fractional\_Y) end function function PerlinNoise\_2D(float x, float y) total = 0 p = persistence n = Number\_Of\_Octaves - 1 loop i from 0 to n frequency = 2i amplitude = pi total = total + InterpolatedNoisei(x \* frequency, y \* frequency) \* amplitude end of i loop return total end function

What is your end goal here? If it’s to produce a 2d image that has the noise results, we have a generator for that. If you’re needing it for some other use, hopefully some of our more mathematically inclined people can chime in.

Hi.

You can basically strip off anything that says “float” or “integer”.

Switch “loop” to “for”, 2i to 2 * i and pi to math.pi.

int() will be math.floor()… though I’d have to putter around with it to see if you need to handle negative numbers the other way around.

There are a couple ways you can do the bits in Noise1(), which I’m guessing are a trouble spot because of the alien syntax.

You could either do this:

local bit = require("plugin.bit") -- make sure to add to build.settings too local function Noise (x, y) n = x + y \* 57 n = bit.lshift(n, 13) ^ n return ( 1.0 - bit.band( (n \* (n \* n \* 15731 + 789221) + 1376312589), 0x7fffffff) / 1073741824.0 end

or

local function Noise (x, y) n = x + y \* 57 n = (n \* 2^12) ^ n return ( 1.0 - ( (n \* (n \* n \* 15731 + 789221) + 1376312589) % 0x8000000) / 1073741824.0) end

When the mask for the binary and is one less than a power-of-2, you can either do the band() or just mod with the power-of-2 itself.

@Rob Miracle

Serious? How can I use it?

@StarCrunch

Thank you! :slight_smile:

Here: https://docs.coronalabs.com/guide/graphics/effects.html#generator

Scroll down to the Perlin Noise section.

Rob

Also.  Perlin noise generation has been asked and solved before:

https://forums.coronalabs.com/topic/14101-perlin-noise-module-need-help-to-convert-to-lua/

Still, I’d use a generator (as Rob mentions) if you want to fill with noise.

What is your end goal here? If it’s to produce a 2d image that has the noise results, we have a generator for that. If you’re needing it for some other use, hopefully some of our more mathematically inclined people can chime in.

Hi.

You can basically strip off anything that says “float” or “integer”.

Switch “loop” to “for”, 2i to 2 * i and pi to math.pi.

int() will be math.floor()… though I’d have to putter around with it to see if you need to handle negative numbers the other way around.

There are a couple ways you can do the bits in Noise1(), which I’m guessing are a trouble spot because of the alien syntax.

You could either do this:

local bit = require("plugin.bit") -- make sure to add to build.settings too local function Noise (x, y) n = x + y \* 57 n = bit.lshift(n, 13) ^ n return ( 1.0 - bit.band( (n \* (n \* n \* 15731 + 789221) + 1376312589), 0x7fffffff) / 1073741824.0 end

or

local function Noise (x, y) n = x + y \* 57 n = (n \* 2^12) ^ n return ( 1.0 - ( (n \* (n \* n \* 15731 + 789221) + 1376312589) % 0x8000000) / 1073741824.0) end

When the mask for the binary and is one less than a power-of-2, you can either do the band() or just mod with the power-of-2 itself.

@Rob Miracle

Serious? How can I use it?

@StarCrunch

Thank you! :slight_smile:

Here: https://docs.coronalabs.com/guide/graphics/effects.html#generator

Scroll down to the Perlin Noise section.

Rob

Also.  Perlin noise generation has been asked and solved before:

https://forums.coronalabs.com/topic/14101-perlin-noise-module-need-help-to-convert-to-lua/

Still, I’d use a generator (as Rob mentions) if you want to fill with noise.