Could you add parameters for frequency and lacunarity?
EDIT: Some other feedback:
The interface doesn’t make a lot of sense. The ModuleScript returns a table with a method called “new”, which suggests that there’s some OOP stuff going on and that PerlinNoiseAPI.new
is a constructor that would return some kind of object. But actually calling the function just samples the noise function at the given coordinates and other parameters. It would make more sense to not call it an “API” and just return a function from the ModuleScript.
E.g.:
Instead of this:
local PerlinNoiseAPI = {}
function PerlinNoiseAPI.new(coords,amplitude,octaves,persistence)
end
return PerlinNoiseAPI
Just do
local PerlinNoise = function(coords,amplitude,octaves,persistence)
end
return PerlinNoiseAPI
The amplitude
parameter should be a multiplier on the output of the Perlin noise function, but the way you’re using it makes it behave more like a period
or (the inverse of) frequency
parameter should.
E.g.:
local perlinvalue = math.noise(X/amplitude,Y/amplitude,Z/amplitude)
should actually be
local perlinvalue = math.noise(X/period,Y/period, Z/period) * amplitude
and again in the loop where you sample each of the octaves.
When checking if the arguments are valid, you can do
assert(condition, error_message)
--rest of the code
instead of
if not condition then
error(error_message)
else
--rest of the code
end
Having fewer if statements, and especially fewer deeply nested if statements, just makes your code more readable and easy to understand. It also lets you have the main part of your code be at the lowest level of indentation possible, which can sometimes be helpful if it allows you to scroll sideways to see everything.
Hope this is helpful