Slow 3D terrain generation

I’ve recently been working on 3D infinite terrain generation, but when it generates the new layer of blocks, it runs awkwardly slow (0.5 fps). I’m not expecting it to run fast, but I think id should be able to get around 10 fps when I’m rendering. Just a quick rundown of how my code works, I have 5x 5 grid of meshes, each composed of a 10 x 10 grid of noise height values (vertices within the mesh). when i reach the point where it is triggered to regenerate, it moves all the meshes in the 5 x 5 grid down one and generates a new layer. Any help on how to speed this up would be appreciated. Also, thanks @Ignatz for the lighting shader.

It is tough to make infinite terrain work in real time. One option is to reduce the workload to using mist or dark to reduce visibility.

This code from @tnlogy may help, if he doesn’t mind your using it. It seems to work reasonably well.

Due to changes in Codea recently, you need to make one change to make the shader defined as S run without error.

It is currently defined as

S = shader(Perlin .. [[ //lots of code]], [[ //more code]])
--then you say in Codea
self.m.shader = S

Instead, write it like this

S ={v=Perlin .. [[ //lots of code]], f=[[ //more code]]}
--then you say in Codea
self.m.shader = shader(S.v,S.f)

Why does @tnlogy 's terrain run so fast compared to mine? How is his code generating the terrain?

I don’t know, I haven’t compared it - have a look at the code or ask him

Feel free to use it :slight_smile:

Havent had time to check your code, but I’ll try to take a look.

We have some similarities in the code, but the main difference is that I make the noise-calculation in the vertex shader rather than in Lua. Then I use a 3x3 grid of meshes which I move according to the camera with some messy code so that the camera always is above the middle grid.

Each vertex height position is then calculated with the function h() in the shader taking the offset information from Lua that specifies where the grid-item is positioned in the world to generate correct perlin noise values that match neighbour grid meshes.

Tell me if you need some more explanations. It would be interesting to see improvements in speed and artistic value with some fog and such. You should be able to remove some draw calls to meshes depending on the camera direction, but I have ignored that for now,

I’m planning to play with it, and I’ll include fog for effect

Looking forward to it :slight_smile:

I’ve done one as well here:

It’s not generating it, but working off a height map, with some mojo for precooking lighting. The lightmap generation is slow, and the heightfield is fixed (I’ve done some work, but it’s parked at the mo on heightfield generation), but the rendering is fairly efficient.

It uses a quad tree for culling unseen areas, and a single fixed mesh with vertex processing for heights for the drawing with differing resolution depending how close to the camera it is.

Code is dirty, but it might provide some ideas. (all free as I got most of it off the web anyway)