MEGATON's Procedural Terrain Generator [ DOWNLOAD ]

Many have asked for the code - so HERE it is.

Try a build of what you get HERE fixing link

Few Instructions:
The package extracts to a folder named ‘Procedural Terrain Generator’
The package also contains the U3 FPS walker in Standard Assets
The terrain heightmap is rendered to a 256x256 meta-texture.
This heightmap dimension and other parameters can be changed in the file named ‘cgame.js’

Very generous! Thanks!

Bug:

What about multitexturing?

Very awesome, thanks buddy!
The small seams are no big deal.

I never bothered to figure out precisely why the seams appear like that. I’m guessing its an issue with mesh normals.
The code uses a Unity function for redirecting the normals after modifying height - “mesh.RecalculateNormals()”

I used a custom shader and did a real-time 3D planar projection mapping for uvs. See this link
Using projection mapping you won’t get texture stretching on vertical polygons.

That particular shader was linked to a “texture selector function” regulated by generated “planet parameters”.
I will try to untangle the projection mapping shader and post it on the forum. ( without the multi-texturing )
One can then make a simpler version blending four textures using a color mask bitmap or vertex color. My code got too hairy. ( I’m not judging anyones skills here. The code was pretty bad. :slight_smile: )

The custom terrain shader I built blended eight textures using values from the terrain-heightmap and using the altitude of the pixel in worldspace.

Here is what the final shader looked like.

Nice.

Any chance we could get the custom shader, too? That planar projection trick would be useful for solving all sorts of problems having to do with procedural content…

When I did procedural terrain, I also ran into that normals problem. Using the heightmap-to-mesh procedural example unity provides, I modified it a bit.
Normals who had their neighbors worked fine. The solution would be to add 2 to each side of the terrain-block. Instead of 128x128, it would be 132x132. This allowed the calculation of normals to work fine.
When the mesh data was finally sent to triangulate, I would exclude the 2 on each border thus returning to 128x128.

Edit: Stumpy’s Shader Editor (free, indie&pro) has a tri-planar example that works great for procedural terrain. Vertex colors also help with texturing.

Why is this generator written JavaScript? It would be more useful if it was written in c# so I could use Visual Studio to figure out how it works.

some of us don’t want to learn c#?

Hi guys
I’ve translated this generator to C#, but I messed something up in the example project, please look at this screen video of me playing example project
http://screenr.com/Pcvs
As you can see first person controller instead of stopping on the generated terrain (as it is in original example project), it falls through it.
Can you tell me where did I make a mistake, please?

Looks like your camera is under the ground…

It is underground because it fell through terrain.
It didn’t stop on the terrain as it does in original project.
BTW I only replaced JS generator scripts with my c# scripts.

Does the terrain generator create a collider?

OK I am moron, all I had to do is to increase y position of First Person Controller

this terrain looks really good

I know it’s an old thread that must not be dig over again and again, but i’ve been searching for around a complete year about these types of terrain generation. All what i faced was either Laggy ones, code-fail ones, or like this one; The terrain just copies itself over and over and it is not actually re-creating meshes.
The only one that as a “working one” was a minecraft clone thing that created cubes, but what i want is a terrain one with heightmaps, so i was happy when finally being able to touch this, but i got very surprised when i saw the same mountains three times…
Why is it not 100% dynamic map creating? Can’t it be this way?

I am not posting here because i’m a mad kid or something like that, i’m very frustrated of never finding the good paths for making an actual good terrain to make my game on, and i was searching for this for ages, and now i have to search again…


Is it possible to change the seed ingame, by keeping actual terrains, and keeping the corners seamless with the new seed? Or is it impossible thing?

Its possible and not that hard to do and has been made a few times on this forum already.
Basics are that you create some noise (simplex noise as an example) in a background thread and apply it to a piece of terrain as you move around.
Using this method in my own game currently, also have a look at http://forum.unity3d.com/threads/153548-ProWorld-Procedural-World-Generation

Currently converting this to c# and its … almost working.

If i switch the erosion1 function for erosion0 it works (but looks awful).

Other wise it crashes in the x, y loop in erosionThermal( float center,float val1,float val2,float val3,float val4 )

The problem is it hangs Unity completely so im having real trouble debugging it. It seems like the values coming back from channel.GetPixel might be the problem but again i cant debug it as everything hangs :frowning: Anyone thats done the conversion able to help out ?

Never mind fixed it, rewrtoe the thermal erosion in plainer syntax and it worked fine.

really imperssed with that MEGATON. esp 3d proj mapping.

Okay so I edited out the import maths lines in each of the files and it runs really well! I also think I did a hi score, I travelled 15 km, but I did make the controller limit faster. Because I want to check if it is infinite because it’s not mentioned above-it is infinite as far as the CPU can handle it. I think I would have to travel at least to the moon and back before the CPU started to go buggy.

so the shadows are buggy-the textures lineup fine, but the recalculate normals is not working. recalculate normals is wrong, it is simply flipping a bit on one of the axes. I’m trying to think of a workaround, rotating the tiles beforehand doesn’t make any difference, you can cover it bit by using a texture normal map. I will make a forum thread on it perhaps.

I didn’t look at the code very much yet, but it’s a very long! I was hoping to make my version of this with about 100 lines. I didn’t understand why 3d projection Mapping of the textures would require a different shader? I thought it would just change the UVs. Blending terrain textures apparently isn’t very documented in unity, I’ve read you have to change the alpha channels onto textures set them 1, 0/0, 1 and blend in between, at least that’s one of the options.

this is an awesome video destructible terrain-

in unity

anyways, at the moment it is using a standard diffuse texture in my version, which shouldn’t interfere with any normals. In fact nothing logically should,

… I can’t believe I found this a year ago and I didn’t know the code was available! Thanks very much! Maybe I will contact you one day for question about using it in a game!

by the way, if the earth is 1mm, saturn is 9 mm, the sun is the size of an orange(11cm), the sun supplies 1^9 megatons . second. but wait a moment. beltegeuse is 60m and antares is 87m. how many megatons is beltegeuse:)

SOLUTION!
quoting someone:
The problem is that at the edges, Unity does not have sufficient information about the neighboring triangles to calculate the correct normal. You will need to manually average the normals along the edges to make sure that each pair of connected edge vertices has identical normal values (this also applies to position and uv’s, of course).

Another thing…

your implementation is pretty fast, i noticed the terrain tiles are 16x16 rows, i wonder what other speed optimisations and processsing jams you encountered while you programmed.

and a different mesh didn’t solve the normals, but I did notice that it was optimised for the 16x one:
I like this actually!

1154468--44016--$funny mesh.jpg