Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

MEGATON's Procedural Terrain Generator [ DOWNLOAD ]

Discussion in 'Scripting' started by MEGATON, Jan 7, 2011.

  1. MEGATON

    MEGATON

    Guest

    Joined:
    Mar 3, 2008
    Posts:
    28


    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'
     
    Last edited: Jan 7, 2011
  2. duke

    duke

    Joined:
    Jan 10, 2007
    Posts:
    763
    Very generous! Thanks!
     
  3. PAX

    PAX

    Joined:
    Oct 12, 2010
    Posts:
    54
    Bug:


    What about multitexturing?
     
  4. Taigo

    Taigo

    Joined:
    Feb 18, 2010
    Posts:
    128
    Very awesome, thanks buddy!
    The small seams are no big deal.
     
  5. MEGATON

    MEGATON

    Guest

    Joined:
    Mar 3, 2008
    Posts:
    28
    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. :) )

    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.
     
    Last edited: Jan 7, 2011
  6. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    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...
     
  7. windexglow

    windexglow

    Joined:
    Jun 18, 2010
    Posts:
    378
    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.
     
  8. robert_d

    robert_d

    Joined:
    Dec 6, 2011
    Posts:
    16
    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.
     
  9. online|offworld

    online|offworld

    Joined:
    Nov 17, 2009
    Posts:
    85
    some of us don't want to learn c#?
     
  10. robert_d

    robert_d

    Joined:
    Dec 6, 2011
    Posts:
    16
    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?
     
    Last edited: Dec 13, 2011
  11. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,285
    Looks like your camera is under the ground...
     
  12. robert_d

    robert_d

    Joined:
    Dec 6, 2011
    Posts:
    16
    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.
     
    Last edited: Dec 14, 2011
  13. ludiares

    ludiares

    Joined:
    Jul 14, 2011
    Posts:
    242
    Does the terrain generator create a collider?
     
  14. robert_d

    robert_d

    Joined:
    Dec 6, 2011
    Posts:
    16
    OK I am moron, all I had to do is to increase y position of First Person Controller
     
  15. DEADDUCKGAMES

    DEADDUCKGAMES

    Joined:
    Nov 11, 2011
    Posts:
    72
    this terrain looks really good
     
  16. Po0ka

    Po0ka

    Joined:
    Dec 19, 2012
    Posts:
    3
    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?
     
  17. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    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
     
  18. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    251
    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 :( Anyone thats done the conversion able to help out ?

    Never mind fixed it, rewrtoe the thermal erosion in plainer syntax and it worked fine.
     
    Last edited: Dec 21, 2012
  19. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    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-
    http://www.youtube.com/watch?v=2W3Vm1_QGf0
    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:)
     
    Last edited: Feb 2, 2013
  20. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    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!
     

    Attached Files:

    Last edited: Feb 3, 2013
  21. Deadphyre

    Deadphyre

    Joined:
    Sep 26, 2013
    Posts:
    21
    I know this thread is a few years old but I ran into a ton of problems with the javascript not able to import the other scripts. I think it would just be easier to rewrite it into c#. For me anyways, I'm not well versed in JS...not well versed in programming in general but I know a lot more about c# than js. Does anyone else know of any other ProcT generators with source out there other than this one?
     
  22. Sca

    Sca

    Joined:
    Jan 10, 2013
    Posts:
    16
    I, as Deadphyre, am also very interested in this :)
     
  23. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,770
    Me three. So much so, that I'd be willing to roll up my sleeves and write this if there isn't one available... I did procedural cube-based terrain generation for BlockHaven (a now mothballed project, alas), and it wouldn't be too hard to adapt it to generate non-cube meshes instead.

    However, searching the forums and Unity Answers, I keep finding projects like this where somebody has clearly already done it, but the code was never quite finished or no longer appears to be available. I'd prefer not to reinvent the wheel if there's a good wheel already... I've even looked in the Asset Store, but haven't had any luck.

    So. Does anyone know of a procedural, on-the-fly terrain generator still available? And if not, anybody want to collaborate (or at least cheer me on) in creating one?
     
  24. james-scaife

    james-scaife

    Joined:
    Jan 25, 2014
    Posts:
    1
    cheer cheer
     
  25. pinchmass

    pinchmass

    Joined:
    Jul 2, 2012
    Posts:
    156
unityunity