Jump to content


Photo

Randomly Generated Heightmaps


  • Please log in to reply
15 replies to this topic

#1 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 28 April 2011 - 04:54 PM

So I messed around trying to make a procedural map creator when I accidentally made this.
These scripts generate random heightmaps for use as textures or terrain models (or both).
The procedure is quite simple, start with a randomly filled screen and smoothen it out. This will generate a sprite with smooth "hills and valleys".

Download
You may save the output of the script and use that freely, but please give credit if you're using the script to generate heightmaps during runtime.

In the full scripts there is the option to add noise or make a looping texture. As they're quite long, I'll only post the really compact version of the script here for you to look at:
/*
create_heightmap_fast(size,detail)
Creates a random heightmap.
Size can be anything, as long as it's smaller than the game window.
Detail is the ammount of detail. Higher detail means more hills and valleys.
Detail is maximum 5, the minimum varies (but is mostly less than 0)

Script created by TheSnidr
www.thesnidr.com
*/
var s,g,pg,xx,yy;
argument0=log2(argument0)
argument1=median(argument0-5+round(argument1),3+frac(argument0),argument0)
s=power(2,argument1)
g=ds_grid_create(s,s)
for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
    ds_grid_set(g,xx,yy,choose(0,1))}}
for (argument1+=1;argument1<=argument0;argument1+=1){
    s=power(2,argument1)
    pg=g
    g=ds_grid_create(s,s)
    for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
        ds_grid_set(g,xx,yy,ds_grid_get_disk_mean(pg,xx/2,yy/2,1.5))}}
    ds_grid_destroy(pg)}
for (xx=0;xx<s;xx+=1){for (yy=0;yy<s;yy+=1){
    draw_point_color(xx,yy,make_color_hsv(0,0,240*ds_grid_get_disk_mean(g,xx,yy,1)))}}
ds_grid_destroy(g)
return sprite_create_from_screen(0,0,s,s,false,false,0,0);
Please note, the full script (which you'll find a link to at the beginning of this post) is better commented.
This script (the compact and faster version) will create a 512*512 heightmap in less than 2 seconds (on my computer at least).
The full script with all options turned on takes about twice as long (it is still not completely optimized), but it may in some cases look better.

Both scripts can return sprites of any size, but as the rendering time increases exponentially, you'd want to not make them too big. Also, the fast script draws and creates its sprite directly from the screen, meaning it will have to be smaller than the game window. Otherwise it won't render correctly. The full script draws its sprite onto a surface first, thus avoiding this problem.
The detail argument affects how random the terrain is. It can be maximum 5, meaning you'll only get random rubble. A detail of 2 or 3 looks good. The minimum detail varies proportionally with the size, but this isn't important.
Sprites smaller than 16*16 will look bad.

Here are some I made while playing around with the hue, saturation and value:
Posted Image

The fast script is the only one I have optimized to the max so far, but feel free to comment if you see any other ways to improve them!
Also, please don't remove the "made by TheSnidr" part!

Edited by TheSnidr, 08 May 2011 - 08:03 PM.

  • 0

#2 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 02 May 2011 - 09:17 PM

Even though nobody cares I've continued working on this ^^
I've made it into some kind of 3D terrain maker that generates a random terrain. It's pretty cool, here are some pictures:
Posted Image
Posted Image
I added a "terrain_create_from_heightmap" script that will create terrain from a supplied heightmap by using draw_getpixel. But this approach is slow...
So I made it create the terrain directly from the grids used in the texture generation system, and it now generates a 256*256 texture AND terrain model in about a second!

The 3D scripts are NOT finished yet, but they may be downloaded from the link in first post anyway.
Please note, I haven't done much 3D before, so please tell me if I'm doing any silly mistakes.
  • 0

#3 Schyler

Schyler

    Noskcirderf Derf

  • GMC Member
  • 2445 posts
  • Version:GM8.1

Posted 02 May 2011 - 10:45 PM

You could probably throw this into my 3D RTS engine :P

http://gmc.yoyogames...howtopic=494846

Anyway, neat script. I've always needed a ready supply of heightmaps for testing. This makes it all too easy ;D

Edited by Schyler, 02 May 2011 - 10:45 PM.

  • 0

#4 Venomous

Venomous

    GMC Member

  • GMC Member
  • 1467 posts
  • Version:GM:Studio

Posted 02 May 2011 - 11:30 PM

Really cool.
This will come in handy.
Do you require any credit if used?

Edited by XtraButter, 02 May 2011 - 11:30 PM.

  • 0

#5 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 03 May 2011 - 07:27 AM

Sweet, comments!
I'm glad you like the script ^^
You may save the output of the script and use that freely, but please give credit if you're using the script to generate heightmaps during runtime.

Edited by TheSnidr, 08 May 2011 - 08:03 PM.

  • 0

#6 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1018 posts
  • Version:GM8

Posted 27 January 2012 - 07:50 PM

How on earth do you get 311 FPS with all of those polygons?!?!?!?!

Please, if there are any 'magic' things you do, tell me!

It looks really nice, I might use it in my game - better than my terrain generation that seems to always make hills lines at 45 degrees...

Also, can you upload a .gmk version please?

Thanks fo your time,

Zesterer

Edited by Zesterer, 27 January 2012 - 07:51 PM.

  • 0

#7 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 27 January 2012 - 08:01 PM

How on earth do you get 311 FPS with all of those polygons?!?!?!?!

Please, if there are any 'magic' things you do, tell me!

It looks really nice, I might use it in my game - better than my terrain generation that seems to always make hills lines at 45 degrees...

Also, can you upload a .gmk version please?

Thanks fo your time,

Zesterer

There is no magic, but your flaw is evident from your post - you're using game maker 8.0.
The 3D system is vastly improved in 8.1, in some cases being hundreds of times faster than the previous versions. I made this post when I first tried 8.1: http://gmc.yoyogames...dpost&p=3754240
Now that's impressive speed!

I don't have any other files than the one in the first post, dunno what happened to the rest. There was no interest in this topic, so I let it die

Edited by TheSnidr, 27 January 2012 - 08:46 PM.

  • 0

#8 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1018 posts
  • Version:GM8

Posted 27 January 2012 - 08:40 PM

Thanks for the reply! I was wondering why my 3D games seemed loads slower than anyone else's! I will get GM8.1 as soon as I get the chance! :D

Thanks for helping,

Zesterer
  • 0

#9 brac37

brac37

    GMC Member

  • GMC Member
  • 765 posts
  • Version:GM7

Posted 27 January 2012 - 09:56 PM

Cool. I did some similar things, but not with this performance, I think. See

Download smooth_terrain_fast2.gmk from Host-A

or the ocean texture generator

Download ocean.gm6 from Host-A

With the ocean texture generator, I load an entirely random background which I blur with "drawing arithmetic". "Drawing arithmetic" might be faster since all pixels are done in parallel. See the first link of my signature for more "drawing arithmetic".
  • 0

#10 Redyoshi

Redyoshi

    GMC Member

  • New Member
  • 31 posts

Posted 31 January 2012 - 12:55 PM

How on earth do you get 311 FPS with all of those polygons?!?!?!?!
Zesterer


He stole NASA's computer.
  • 0

#11 Zesterer

Zesterer

    Professor of Articul

  • GMC Member
  • 1018 posts
  • Version:GM8

Posted 31 January 2012 - 01:33 PM

:D

Gm8.1 has really advanced, I see!
  • 0

#12 Follomania

Follomania

    Unum Mirabile

  • GMC Member
  • 445 posts
  • Version:GM:Studio

Posted 09 February 2012 - 04:35 AM

That looks pretty cool! Can it generate textures to match the terrain?
I made a program a while back that does close to the same thing and have pretty much fallen in love with random terrain generation. I'm currently working on a random planet generator, but Jam #5 distracted me. :(
Here's a screenshot of my terrain generator's results (your generation time is a LOT faster than mine, though.)
Spoiler


Awesome work!
  • 0

#13 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 16 February 2012 - 09:51 PM

:D

Gm8.1 has really advanced, I see!

I didn't really test my example myself after this topic sunk down the depths of the GMC, but I just did.
You thought 300 fps was a lot? Think again:
Posted Image
I honestly don't know what's different from previous time I tested it, but it now runs even faster than it did then - actually about 7 times faster. Perhaps the speed of D3D has been improved even more since gm8.1 first was released.

Follomania, your terrain looks good, I like the variety. The engine as it is now won't generate terrain-aware textures, but it'd be a nice feature indeed.
By "planet", do you mean you're gonna wrap it around a sphere? I've done that already ^^
  • 0

#14 Follomania

Follomania

    Unum Mirabile

  • GMC Member
  • 445 posts
  • Version:GM:Studio

Posted 16 February 2012 - 11:18 PM

Follomania, your terrain looks good, I like the variety. The engine as it is now won't generate terrain-aware textures, but it'd be a nice feature indeed.
By "planet", do you mean you're gonna wrap it around a sphere? I've done that already ^^


I currently wrap it around a sphere. I've been wanting to rewrite it so I use an icosahedron rather than my current sphere method to avoid the stretching at the poles.
Spoiler

I figured you probably already have done it based on where you are in 3D programming.

I was wondering, how much work would it take to make your heightmaps "tile-able", so I could make one model and tile it so it appears to be infinite? I've been going through the code, and am still trying to figure it all out. :(
  • 0

#15 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2437 posts
  • Version:GM:Studio

Posted 17 February 2012 - 12:02 AM

Wow, impressive! I remember it took me weeks of headcrunching before I had a so-so working heightmapped sphere. If you want to base it on an icosahedron (or even better, a geodesic dome), check out my topic on the matter.
BUT using this kind of sphere won't fix the moprhing at the poles. Actually it will only make it worse, unless you alter the heightmap itself. You need to use an equirectangular heightmap, a heightmap specifically designed for a sphere. When I needed that, I modified the RanGenHei engine to make equirectangular maps instead. I've updated the link in the first post with the newest version of the engine containing these scripts.

Here's an image showing the difference between a normal and an equirectangular heightmap:
Posted Image
The poles are the obvious, long lines at the top and bottom, like the poles on a map of earth.
The equirectangular map has a very diffuse seam on the middle as a result of the method I use to create it (it's more visible with a higher level of detail). It's not perfect, but it gives a good "estimate" of an equirectangular map. As you can see, it also loops.

Edited by TheSnidr, 17 February 2012 - 12:04 AM.

  • 0

#16 Follomania

Follomania

    Unum Mirabile

  • GMC Member
  • 445 posts
  • Version:GM:Studio

Posted 17 February 2012 - 01:37 AM

I tweaked my generation code according the the information you gave me, and it's working pretty well. I just need to smooth the poles a little more, but it's looking pretty good. Thanks for the link to your topic! It was very helpful.
Spoiler

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users