Jump to content


Photo

Easy Heightmap Scripts


  • Please log in to reply
42 replies to this topic

#1 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 15 March 2009 - 04:20 AM

  • Title: Easy Heightmap
  • Description: Create a working heightmap in 3 scripts
  • GM Version: Game Maker 7
  • Registered: Yes
  • File Type: .Zip (contains 2 files, an example and the scripts)
  • File Size: Approx 90KB
  • File Link: Download (Host-a-net)
  • You may also be interested in: GameDev Studio
Additional Info
Credit is always appreciated, but not required.


This is a very easy to use heightmap engine. It contains three scripts
terrain_create(); - creates the heightmap with the wanted settings
terrain_draw(); - Draw the terrain
terrain_get_z(); - Find the terrains Z at position [X,Y]


Screenshot:
Posted Image

Enjoy :P


Like this example? Then visit GM3D, You'll find many 3D things there :)

~~Brett14;

[EDIT]
After several requests for a script that stretches one texture over the whole terrain, I've decided instead of PM'ming them all the code just put it here. So if you want a stretched texture, then replace the create code with this. Thanks.

PLEASE NOTE: You'll need to remove the spaces between some lines that split the function names in half.
/*
****---- ~ Credit goes to: brett14 ~ ----****

THE TERRAIN SHOULD HAVE THE HIGHEST DEPTH (10000 etc. - it gets set to -1000 after initing this though)
loads the terrain from a heightmap sprite - it does not matter what the size of the sprite is (32x32, 64x64 or 128x128 is recommended)



Argument0 = terrain heightmap sprite
Argument1 = the size of the terrain squares (X,Y)
Argument2 = the height value of the terrain (Z)

*/
var counter,counter2,xx,yy;
global.swidth=sprite_get_width(argument0);
global.sheight=sprite_get_height(argument0);
global.terrainsize = argument1;
global.terraingrid=ds_grid_create(global.swidth,global.sheight);
global.terrainmodel=d3d_model_create();
draw_sprite_stretched(argument0,0,0,0,global.swidth+2,global.sheight
+2)
for(w=0;w<global.swidth+2;w+=1){
	for(h=0;h<global.sheight+2;h+=1){
		val=draw_getpixel(w,h)
		valex=color_get_value(val)
		ds_grid_set(global.terraingrid,w,h,valex/240*argument2)
		}
	}
counter2=0;
for(xx=0;xx<global.swidth-1;xx+=1){
d3d_model_primitive_begin(global.terrainmodel,pr_trianglestrip);
counter=0;
for (yy=0;yy<global.sheight;yy+=1){
  z2=ds_grid_get(global.terraingrid,yy,xx+1);
  d3d_model_vertex_normal_texture(global.terrainmodel,yy*global.terrainsize,(xx+1)*global.terrainsize,z2,0,0,1,counter,counter2+
1/global.swidth);
  z1=ds_grid_get(global.terraingrid,yy,xx);
  d3d_model_vertex_normal_texture(global.terrainmodel,yy*global.terrainsize,xx*g
lobal.terrainsize,z1,0,0,1,counter,counter2);  
  counter+=1/global.swidth;
  }
counter2+=1/global.swidth;
d3d_model_primitive_end(global.terrainmodel);
}
depth=-1000;
[/EDIT]

Edited by brett14, 14 March 2012 - 03:30 AM.

  • 3

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#2 JohnnyDarkness

JohnnyDarkness

    GMC Member

  • New Member
  • 19 posts

Posted 25 March 2009 - 01:43 PM

Exellent Job Brett14, It does what it is supposed to. I hope this will work for my game. 5/5
  • 0

#3 §-«»-§

§-«»-§

    GMC Member

  • New Member
  • 83 posts

Posted 07 April 2009 - 07:06 AM

Very nice. Your demo seems to work perfectly, though since the camera starts pointing straight up it took me a while to figure out where the terrain actually was.

The simplicity of your system is impressive, it hides the nuts and bolts of terrain generation quite well. One question: Will lighting "just work" with this or will it take some modification of your scripts?
  • 0

#4 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 08 April 2009 - 04:00 AM

Maybe, I wrote these a long time ago, but if i remember correctly, lighting works fine :medieval: , if not it will only take a little modification.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#5 the sivart

the sivart

    GMC Member

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

Posted 20 April 2009 - 03:59 PM

looks like you did a very good job :snitch:
  • 0

I am "The Sivart" and I like to make tutorials on youtube >> Posted Image << I am "The Sivart" and I like to make tutorials on youtube


#6 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 21 April 2009 - 12:34 AM

looks like you did a very good job :snitch:

Yeah, I try to do my best on everything I give out on the gmc. All things are made to be extremely easy to use.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#7 allewille

allewille

    GMC Member

  • New Member
  • 30 posts

Posted 29 November 2009 - 07:54 PM

how do you change the max height of the map ?? because ow the higest point isnt really high at all...
  • 0
-Alexander
Blackmoon Studios

#8 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 07 January 2010 - 10:58 PM

Thank you for such an amazing set of easy to use scripts.
You did a very good job on this!

What is the easiest way to create heightmap sprites?

how do you change the max height of the map ?? because ow the higest point isnt really high at all...

in the terrain sprite you can make the highest point pure white, or when you call terrain_create make the third argument bigger. Right now it's:
terrain_create(heightmap,256,2500);
you can change it to:
terrain_create(heightmap,256,4000);
to make the max height bigger.

Edited by Hockeyflyers, 08 January 2010 - 12:41 AM.

  • 0

#9 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 08 January 2010 - 12:55 AM

Glad you like them :(
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#10 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 08 January 2010 - 03:50 AM

I have two quick questions.
How would I get multiple terrains on the ground? Would the best way to be in the for loops in the terrain_create() script check for a terrain object and if it's there add it to the primitive?

and

What is the easiest way to create heightmap sprites?

Edited by Hockeyflyers, 08 January 2010 - 03:51 AM.

  • 0

#11 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 09 January 2010 - 01:01 AM

to create a heightmap: create a sprite with the desired dimensions, make it all black. Draw white lines where you want it to be higher at. Blur the sprite 3-5. Keep repeating these two steps until you get a result you like. If it is too high then do the same thing with black.

To create multiple terrains, what do you mean by that? Visible at the same time OR just create them all at the same time?
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#12 Hockeyflyers

Hockeyflyers

    Hockeyplayer Games

  • New Member
  • 1108 posts
  • Version:Unknown

Posted 09 January 2010 - 03:21 AM

to create a heightmap: create a sprite with the desired dimensions, make it all black. Draw white lines where you want it to be higher at. Blur the sprite 3-5. Keep repeating these two steps until you get a result you like. If it is too high then do the same thing with black.

thanks

To create multiple terrains, what do you mean by that? Visible at the same time OR just create them all at the same time?

Oh sorry, I meant multiple textures in one terrain.
  • 0

#13 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 09 January 2010 - 07:18 AM

multiple textures is not supported by this engine. I could make it possible, but I'm not going too, as it would take too much time (a couple of hours probably). Sorry.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#14 anatheron

anatheron

    GMC Member

  • GMC Member
  • 32 posts

Posted 21 January 2010 - 11:24 PM

Wow! I must addmit... This is quite awsome :whistle: And it will sure come in handy when it comes to getting the games to look better.
only prob is that I can't see the hightmap in the room editor wich makes it a bit difficoult to place other objects correctly. But else good work, It truly is awsome! ;P
  • 0

#15 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 02 March 2010 - 05:00 AM

I've added the script (in the top post) to create a terrain with one texture stretched over it, due to many requests via PM.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#16 GMkizzle

GMkizzle

    GMC Member

  • GMC Member
  • 384 posts

Posted 09 May 2010 - 07:30 AM

These scripts are great!
Multiple textures would be great, but i know you said you wouldnt do it, but i think you should reconsider :)
  • 0

Posted Image


#17 michael pw

michael pw

    GMC Member

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

Posted 28 June 2010 - 11:00 PM

great engine, used it a few times. i have a problem though, basically i have a heightmap sprite but the terrain drawn is flat???

i had this problem before but managed to fix it. but i forgot what i did D:

pls help thanks. actually ill test get z to see if the terrain model array data list has been created first.

EDIT: the z value is consistently at 0

EDIT: soz i always do this, i accidently had another object calling the script but to a plain heightmap, sorry for fussing, it works now.

Edited by michael pw, 28 June 2010 - 11:05 PM.

  • 0

Known as MishMash on the internet, If only my username could be changed :D?

Posted Image

Working on Sharp Edge, a 3D Level designer which exports to GMMOD and textures, with a vast toolset, and fancy GUI. ( So far, software is only going to be distributed with my game, in the future, it will be up for public use and with the ability to customise the software using the Plugin System to suite your games needs.)
Also working on 3D Multiplayer FPS Global Anarchy Denied Operations.


#18 nottud

nottud

    GMC Member

  • GMC Member
  • 130 posts

Posted 01 July 2010 - 06:26 AM

This is really good - I am impressed by the VERY high FPS count. I was getting like 1300!
  • 0

#19 solano

solano

    GMC Member

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

Posted 01 July 2010 - 07:25 AM

How did you get it to look so good.
Is it because of the culling?
d3d_set_culling(1);
d3d_model_draw(global.terrainmodel,0,0,0,background_get_texture(argument0))
d3d_set_culling(0);

Or is it the culling and something else?
  • 0

#20 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 01 July 2010 - 03:18 PM

@nottud @ solano
Nah, the culling doesn't make it look good. The culling just makes it run quite a bit faster.

@solano
To get it to look so good... Um? Culling just makes it so that faces that are not seen don't get drawn. I just used a nice texture, and I wrote the scripts to be efficient.

@nottud
GM8 or GM7? I have a faster version for GM8, its about 30% faster but it's not out to the public - think I should release it?

Edited by brett14, 01 July 2010 - 03:19 PM.

  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#21 KurtBlissZ

KurtBlissZ

    GMC noob

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

Posted 07 July 2010 - 11:01 PM

Im not getting the terrain to popup at all. just the sprite on the top left corner. Can anyone help me?
  • 0

Get YYC 


#22 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 10 July 2010 - 10:27 PM

My guess is it's a depth issue. Can't help you without you providing an example, sorry.

~~Brett14;
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#23 KurtBlissZ

KurtBlissZ

    GMC noob

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

Posted 10 July 2010 - 10:54 PM

opps I had to wait for a little bit. sorry I'm a retard.

Edited by kurtbliss, 10 July 2010 - 11:08 PM.

  • 0

Get YYC 


#24 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 11 July 2010 - 09:10 PM

You can save the grid with ds_grid_write(); and write that string to a file, and then read it from a file to generate the heightmap. It's much faster at loading then turning the heightmap into a grid and then turning the grid into a model (Almost instant vs a couple seconds)
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#25 whiteshadow

whiteshadow

    GMC Member

  • New Member
  • 29 posts
  • Version:GM8

Posted 02 November 2010 - 11:07 AM

this is an awsome engine. im going to use it in my game and credit will be given. there is a problem sometimes the player is lower than it should and sometimes it is higher. i dont klnow if it is from my programing or this. thanks anyway
  • 0
Error 43
It's Coming

#26 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 02 November 2010 - 11:10 PM

there is a problem sometimes the player is lower than it should and sometimes it is higher

This is probably your programming because it mathematically returns the correct Z values on the terrain based on the X,Y positions
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#27 whiteshadow

whiteshadow

    GMC Member

  • New Member
  • 29 posts
  • Version:GM8

Posted 03 November 2010 - 08:28 AM

there is a problem sometimes the player is lower than it should and sometimes it is higher

This is probably your programming because it mathematically returns the correct Z values on the terrain based on the X,Y positions


i have an object that has the get z script in the create event an is sometimes drawn below the terrain or its sometimes floating. do you know why and could you possibly run me throught the ds_grid_write. thanks

Edited by whiteshadow, 03 November 2010 - 08:38 AM.

  • 0
Error 43
It's Coming

#28 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 03 November 2010 - 11:02 PM

I can't help you without some code. Either upload the buggy code, or PM me the .gmk. HOWEVER - I'll be gone for 3 days so it may be a while for you to get the result.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#29 programmmer5

programmmer5

    GMC Member

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

Posted 06 November 2010 - 05:04 PM

this is great
and it would be better if you added shadows and multiple textures
this is awsome
and hope you can update it ::lmao::
  • 0

Kidnapped Treasure [Jam 13]

xulg.png

WIP: Space Chase (3D)GMC jam 9 

Space Chase 3D for Android < Click it!

 


#30 programmmer5

programmmer5

    GMC Member

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

Posted 30 November 2010 - 12:50 PM

hey i cant place any of my models in this example
they all end up near the position (0;0) :P

first i thought that the terrain was way bigger than the room (witch it was)
and corected it by shrinking down the size of the terrain to the room size, but it still
ends up in the same position.

and when i try to make my models appear on the higher parts of the terrain by using your code
z=terrain_get_z(x,y);
it creates at the moast random x values. :wacko:

and lastly how do you change the z value of the player to the terrain? :blink:
  • 0

Kidnapped Treasure [Jam 13]

xulg.png

WIP: Space Chase (3D)GMC jam 9 

Space Chase 3D for Android < Click it!

 


#31 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 01 December 2010 - 12:51 AM

same problem as the last person, I can't help you without you supplying code. Chances are (judging by the random X values) you acidently are creating it at
instance_create(terrain_get_z(x,y),y,instance);
however that is a complete guess.

and lastly how do you change the z value of the player to the terrain? :blink:

z=terrain_get_z(x,y);
and in you're projection code put the zfrom variable to either z or z+(how tall the player is). Put the zto variable to the same thing as zfrom +/- your zdirection


hey i cant place any of my models in this example

You should really use an editor MADE for 3D to do this, not the built in 2D editor for gamemaker. There have been many made (just search for some) and I too have made a couple (Editor14, Dark Region)

Edited by brett14, 01 December 2010 - 12:54 AM.

  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#32 monstrous123

monstrous123

    GMC Member

  • New Member
  • 1 posts

Posted 14 December 2010 - 06:18 PM

Cheers it was really quick and easy to use :) - is there any way i can stretch one large texture over the whole map though? I've seen it done before and it would be perfect for my game... Any ideas?
  • 0

#33 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 15 December 2010 - 01:46 AM

read the first post.
  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#34 dohyung97022

dohyung97022

    GMC Member

  • New Member
  • 2 posts

Posted 26 January 2011 - 11:52 PM

When this game goes to another room, it makes all the stuff in variable y upsied down.
What is the variable in one of your script that makes it happen??
(When it goes to a 2d demention room);

Edited by dohyung97022, 27 January 2011 - 12:00 AM.

  • 0

#35 Venomous

Venomous

    GMC Member

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

Posted 27 January 2011 - 12:05 AM

When this game goes to another room, it makes all the stuff in variable y upsied down.
What is the variable in one of your script that makes it happen??
(When it goes to a 2d demention room);

Call d3d_end().
As long as 3d mode is on, 2d rooms will be drawn upside down.

Edited by XtraButter, 27 January 2011 - 12:44 AM.

  • 0

#36 brett14

brett14

    GMC Member

  • GMC Member
  • 1151 posts
  • Version:GM8

Posted 27 January 2011 - 01:00 AM

It is not my scripts, its due to the fact the 3D mode is turned on. When 3D is turned of the view gets flipped. Call d3d_end.

Edited by brett14, 27 January 2011 - 01:01 AM.

  • 0

P3DC V6.00 | Editor14 | Large 3D Terrain

GML programmer since 2005, C/C++ programmer since 2009, Java programmer since 2012


#37 dohyung97022

dohyung97022

    GMC Member

  • New Member
  • 2 posts

Posted 27 January 2011 - 09:10 AM

WOW
this was simple...
I thought this happened because of the scripts...

Have my Thanks!
  • 0

#38 GS-games

GS-games

    Creative

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

Posted 17 February 2012 - 05:58 PM

Works nice, but how do I change the texture repeats within the model?Posted Image

Edited by GS-games, 10 February 2013 - 11:51 AM.

  • 0

Hi there!

Twitter: https://twitter.com/GabrielSandsted

Website (under development) gsgames.net: http://gsgames.net/ 

Facebook page: https://www.facebook.com/gsgames.net 

tx5PmL9.png


#39 aburgin

aburgin

    GMC Member

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

Posted 23 February 2012 - 05:49 PM

Greetings !!

to Brett14....

I love the work here, but there is an issue. It appears that for some reason, when changing rooms, the terrain is not fully regenerated. It appears the height map array is inverted or changed with random numbers.

As an example I used your demo (which is great)... but if you add a second room that is a duplicate of the first room (except use a different texture map) and program the "F" and "B" keys to from room to room, the array is not re-generated, as you would expect. It appears corrupted (?!?).

I've noted, in GM8x, you can use the "persistence" check box to keep the terrain the same, except then the global terrain array is kept the same through all rooms. The texture doesn't change as it should.

Any Ideas ?????

nope... not using d3d_end()

I tried playing with d3d_start() in the terrain object, but that yields a flat texture map.

Maybe the question is....... How do I make the room re-load the terrain as it does on first load. Do I need to destroy the array in memory ?? ... load the height map and texture from file ??

What am I missing ????

yes, I have created a GM8 file, if needed.

Edited by aburgin, 23 February 2012 - 11:12 PM.

  • 0

#40 denton

denton

    GMC Member

  • GMC Member
  • 50 posts
  • Version:Unknown

Posted 04 November 2012 - 11:23 AM

Greetings !!

to Brett14....

I love the work here, but there is an issue. It appears that for some reason, when changing rooms, the terrain is not fully regenerated. It appears the height map array is inverted or changed with random numbers.

As an example I used your demo (which is great)... but if you add a second room that is a duplicate of the first room (except use a different texture map) and program the "F" and "B" keys to from room to room, the array is not re-generated, as you would expect. It appears corrupted (?!?).

I've noted, in GM8x, you can use the "persistence" check box to keep the terrain the same, except then the global terrain array is kept the same through all rooms. The texture doesn't change as it should.

Any Ideas ?????

nope... not using d3d_end()

I tried playing with d3d_start() in the terrain object, but that yields a flat texture map.

Maybe the question is....... How do I make the room re-load the terrain as it does on first load. Do I need to destroy the array in memory ?? ... load the height map and texture from file ??

What am I missing ????

yes, I have created a GM8 file, if needed.



Yeah I'm having the same problem, I got around it by just making the heightmap massive and just keeping it persistent. but now i need to do something that requires me to have more terrains, Did anyone figure this out?
  • 0

#41 _171863

_171863

    GMC Member

  • GMC Member
  • 14 posts

Posted 28 January 2013 - 10:15 AM

sorry for reviving this thread, but is there a possible reason the drawn model is flat

pm me if you want the gmk
  • 0

#42 justmiff

justmiff

    GMC Member

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

Posted 10 February 2013 - 10:49 AM

sorry for reviving this thread, but is there a possible reason the drawn model is flat

pm me if you want the gmk


Heightmaps are measured usually by their colour values. If your heightmap is completely black, then that will register as the minimal height for the entire model. Try drawing grey or white shades on your heightmap and see if there's any results. (Black=0 White=255) If you are already aware of this then try and make sure there is no transparent pixels, I'm not entirely sure what would be up if you are doing it correctly.
  • 0

I am an apprentice software developer in the UK. Mainly focusing on ASP.net development within Sharepoint using Visual C#. My web applications often consist of GMHTML5 elements. Game Maker brought me into the realm of programming from the age of 9, and I have been making games using DnD since I was only 6.


#43 derptomi

derptomi

    GMC Member

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

Posted 08 March 2013 - 02:27 PM

It doesn't seem to work with GM: Studio. It just draws a flat surface... Any thoughts?
  • 1




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users