local tmp ={{1,1,0},{-1,1,0},{1,-1,0},{-1,-1,0},
{1,0,1},{-1,0,1},{1,0,-1},{-1,0,-1},
{0,1,1},{0,-1,1},{0,1,-1},{0,-1,-1}}local grad3 ={}-- make 0 based indicesfor i,t inipairs(tmp)dolocal gr ={}for j =1, 3do
gr[j-1]=t[j]end
grad3[i-1]=gr
end
tmp =nillocal p ={151,160,137,91,90,15,
131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180}local perm ={}for i=0, 511do perm[i]= p[(i &255) + 1]endlocalfunction dot(g, x, y, z)return g[0]*x + g[1]*y + g[2]*z;endlocalfunction mix(a, b, t)return(1-t)*a + t*b
endlocalfunction fade(t)return t*t*t*(t*(t*6-15)+10);endlocalfunction noise(x, y, z)-- Find unit grid cell containing pointlocal X =(x//1)local Y =(y//1)local Z =(z//1)-- Get relative xyz coordinates of point within that cell
x -= X
y -= Y
z -= Z
-- Wrap the integer cells at 255 (smaller integer period can be introduced here)
X = X &255
Y = Y &255
Z = Z &255-- Calculate a set of eight hashed gradient indiceslocal gi000 = perm[X+perm[Y+perm[Z]]]%12local gi001 = perm[X+perm[Y+perm[Z+1]]]%12local gi010 = perm[X+perm[Y+1+perm[Z]]]%12local gi011 = perm[X+perm[Y+1+perm[Z+1]]]%12local gi100 = perm[X+1+perm[Y+perm[Z]]]%12local gi101 = perm[X+1+perm[Y+perm[Z+1]]]%12local gi110 = perm[X+1+perm[Y+1+perm[Z]]]%12local gi111 = perm[X+1+perm[Y+1+perm[Z+1]]]%12-- Calculate noise contributions from each of the eight cornerslocal n000= dot(grad3[gi000], x, y, z)local n100= dot(grad3[gi100], x-1, y, z)local n010= dot(grad3[gi010], x, y-1, z)local n110= dot(grad3[gi110], x-1, y-1, z)local n001= dot(grad3[gi001], x, y, z-1)local n101= dot(grad3[gi101], x-1, y, z-1)local n011= dot(grad3[gi011], x, y-1, z-1)local n111= dot(grad3[gi111], x-1, y-1, z-1)-- Compute the fade curve value for each of x, y, zlocal u = fade(x);local v = fade(y);local w = fade(z);-- Interpolate along x the contributions from each of the cornerslocal nx00 = mix(n000, n100, u);local nx01 = mix(n001, n101, u);local nx10 = mix(n010, n110, u);local nx11 = mix(n011, n111, u);-- Interpolate the four results along ylocal nxy0 = mix(nx00, nx10, v);local nxy1 = mix(nx01, nx11, v);-- Interpolate the two last results along zlocal nxyz = mix(nxy0, nxy1, w);return nxyz;endreturn noise
And thats how you can use it with code above:
-- add -- self.x = 0-- self.y = 0-- to init function of FollowSpritefunction FollowSprite:update3(dt)local x, y = self.x,self.y--self:getPosition()local distSq = distance(x, y, self.tx, self.ty)local n1 =0local n2 =0local power =20--self.speed = math.sqrt(distSq) * 0.75 --< dynamic speed based on distanceif(distSq > self.dist^2)thenlocal a = angle(x, y, self.tx, self.ty)local dx =cos(a)* self.speed * dt
local dy =sin(a)* self.speed * dt
x += dx
y += dy
--self:setRotation(^>a)
n1 = noise(y/100, 0.1, self.zoff)* power /2
n2 = noise(0.1, x/100, self.zoff)* power /2else
n1 = noise(y/100, 0.1, self.zoff)* power
n2 = noise(0.1, x/100, self.zoff)* power
end
self:setPosition(x + n1, y + n2)
self.zoff += dt
self.x = x
self.y = y
end
For some reason I decided to create a noise plugin
And after a few hours, its working, and its x5 times faster than lua version. But it only works for windows, cuz idk how to build for other platforms ¯\_(ツ)_/¯
if you make it public, I can add it to gideros distributon as well as port it to other platforms
The question is, does someone need it? Maybe it should have more than just simplex and perlin noises Maybe something like this: https://github.com/Auburns/FastNoise If so, I can also create a few examples
> Newcomers roadmap: from where to start learning Gideros "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
static void g_initializePlugin(lua_State* L){
noise = FastNoise();// rest of the code
}
And the binding functions like that:
static int GetNoise(lua_State* L){
FN_DECIMAL x = lua_tonumber(L, -1);
FN_DECIMAL y = lua_tonumber(L, -2);
FN_DECIMAL z = lua_tonumber(L, -3);
FN_DECIMAL v = noise.GetNoise(x, y, z);
lua_pushnumber(L, v);return1;}
g_deinitializePlugin is empty, cuz idk how to destroy static instance, google says that it will be automatically destroyed. Also, should I to use lua_pop every time Im getting/pushing values from/to stack?
if You don’t need to instanciate your generator, you won’t need to create classes. For basic lua C interface, take a look at how it is done for other plugins in gideros
Comments
EDIT:
You can add noise to make it move "randomly" around given point
Noise code (refernce):
And thats how you can use it with code above:
Likes: Apollo14, antix, MoKaLux, plicatibu
And after a few hours, its working, and its x5 times faster than lua version.
But it only works for windows, cuz idk how to build for other platforms ¯\_(ツ)_/¯
Likes: MoKaLux, talis
Likes: MoKaLux, SinisterSoft
If so, I can also create a few examples
Likes: SinisterSoft
Likes: plicatibu
"What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
“The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
Thank you for sharing.
And please, let your code be added to Gideros.
Regards.
Likes: MoKaLux
Likes: Apollo14
So, how to properly create a binding?
The way I did it is:
Define noise variable
static FastNoise noise;
Also, should I to use lua_pop every time Im getting/pushing values from/to stack?
Or I need to use g_createClass ?