Add love.math.perlinNoise and love.math.simplexNoise.

This commit is contained in:
Miku AuahDark
2022-09-24 23:51:12 +08:00
parent da27af1eda
commit b285c2c866
3 changed files with 150 additions and 30 deletions
+83 -12
View File
@@ -329,16 +329,76 @@ int w_noise(lua_State *L)
switch (nargs)
{
case 1:
val = noise1(args[0]);
val = simplexNoise1(args[0]);
break;
case 2:
val = noise2(args[0], args[1]);
val = simplexNoise2(args[0], args[1]);
break;
case 3:
val = noise3(args[0], args[1], args[2]);
val = perlinNoise3(args[0], args[1], args[2]);
break;
case 4:
val = noise4(args[0], args[1], args[2], args[3]);
val = perlinNoise4(args[0], args[1], args[2], args[3]);
break;
}
lua_pushnumber(L, (lua_Number) val);
return 1;
}
int w_perlinNoise(lua_State* L)
{
int nargs = std::min(std::max(lua_gettop(L), 1), 4);
double args[4];
for (int i = 0; i < nargs; i++)
args[i] = luaL_checknumber(L, i + 1);
float val = 0.0f;
switch (nargs)
{
case 1:
val = perlinNoise1(args[0]);
break;
case 2:
val = perlinNoise2(args[0], args[1]);
break;
case 3:
val = perlinNoise3(args[0], args[1], args[2]);
break;
case 4:
val = perlinNoise4(args[0], args[1], args[2], args[3]);
break;
}
lua_pushnumber(L, (lua_Number) val);
return 1;
}
int w_simplexNoise(lua_State* L)
{
int nargs = std::min(std::max(lua_gettop(L), 1), 4);
double args[4];
for (int i = 0; i < nargs; i++)
args[i] = luaL_checknumber(L, i + 1);
float val = 0.0f;
switch (nargs)
{
case 1:
val = simplexNoise1(args[0]);
break;
case 2:
val = simplexNoise2(args[0], args[1]);
break;
case 3:
val = simplexNoise3(args[0], args[1], args[2]);
break;
case 4:
val = simplexNoise4(args[0], args[1], args[2], args[3]);
break;
}
@@ -349,10 +409,14 @@ int w_noise(lua_State *L)
// C functions in a struct, necessary for the FFI versions of math functions.
struct FFI_Math
{
float (*noise1)(double x);
float (*noise2)(double x, double y);
float (*noise3)(double x, double y, double z);
float (*noise4)(double x, double y, double z, double w);
float (*snoise1)(double x);
float (*snoise2)(double x, double y);
float (*snoise3)(double x, double y, double z);
float (*snoise4)(double x, double y, double z, double w);
float (*pnoise1)(double x);
float (*pnoise2)(double x, double y);
float (*pnoise3)(double x, double y, double z);
float (*pnoise4)(double x, double y, double z, double w);
float (*gammaToLinear)(float c);
float (*linearToGamma)(float c);
@@ -360,10 +424,15 @@ struct FFI_Math
static FFI_Math ffifuncs =
{
noise1,
noise2,
noise3,
noise4,
simplexNoise1,
simplexNoise2,
simplexNoise3,
simplexNoise4,
perlinNoise1,
perlinNoise2,
perlinNoise3,
perlinNoise4,
gammaToLinear,
linearToGamma,
@@ -383,6 +452,8 @@ static const luaL_Reg functions[] =
{ "gammaToLinear", w_gammaToLinear },
{ "linearToGamma", w_linearToGamma },
{ "noise", w_noise },
{ "perlinNoise", w_perlinNoise },
{ "simplexNoise", w_simplexNoise },
{ 0, 0 }
};