• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D Drawing 3D terrain in realtime while sculpting it

Kentae

Member
Hey guys.

I want to create my own 3D world creator and a part of this creator should be terrain sculpting.
I know how I should do all my sculpting tools and so on but the thing that stumps me a little is, as the title suggests, how to draw the terrain in real time.
More specificly, how to draw it in real time without it lagging like hell.
I've seen several examples of this being done before but never seen any code that I can learn from.

So my question becomes this:
How do I draw realtime sculptable 3D terrain in an efficient way?

I figure that redrawing the whole terrain every frame, even just when a sculpting tool is being used, will lag like hell, espesially if the terrain is fairly large.
Same goes for rebuilding a terrain model every frame.
I have been fiddeling around some with the idea of having the terrain be parted up in several smaller models and only rebuilding the ones that are in range of the sculpting tools. This however can become tricky to code.

I use GMS 1.4 btw.

Any help would be greatly appreciated :)
 

lolslayer

Member
Summary of a few approaches:
1. Store the heightmap (and possibly other data) in a texture(/textures). Create a flat grid model to draw the world with and use a vertex shader to change the height (and possibly other data) based on the texture data.
Pros: Fast, interpolates very easily between values, can very easily be combined with a LOD system
Cons: Map is limited to the maximum texture size, you could fix this with a chunck system
Note: you need a DLL to get texture data in the vertex shader

2. Create a chunck system to represent the terrain data. Create 3D models of each chunck. When changing the data of the terrain only update the models of the chuncks that get altered
Pros: Very straight-forward, can easily be extended to any size you want it to be, very customizable
Cons: A bit slower

3. Create a huge 2D array storing all the data of the whole terrain, only use the terrain data close to the camera for terrain model creation every frame
Pros: Very easy to set-up, very straight-forward
Cons: Very slow, you can't unload any terrain data

Can't really think up of another good system right now, I might edit this later on
 
Summary of a few approaches:
1. Store the heightmap (and possibly other data) in a texture(/textures). Create a flat grid model to draw the world with and use a vertex shader to change the height (and possibly other data) based on the texture data.
Pros: Fast, interpolates very easily between values, can very easily be combined with a LOD system
Cons: Map is limited to the maximum texture size, you could fix this with a chunck system
Note: you need a DLL to get texture data in the vertex shader
Are there already DLLs out there that do this? Also, why isn't it a part of gamemaker by default?
 

lolslayer

Member
Are there already DLLs out there that do this? Also, why isn't it a part of gamemaker by default?
I don't know why it isn't part of gamemaker by default, but with D3D at least you have to tell what shader stages can see sampler inputs, and apparently yoyogames checked it off with the vertex shader. I know that SweetCelestia has a .DLL for that so I'll try to contact him about it

You would need to code that using GML.

Not part of GM by default because things like vertex format, texturing, etc are highly project dependant
You can't do it without DLL's, samplers are just not visible on the vertex shader stage whatever you do
 

Kentae

Member
Thanks for the replies guys :)

@lolslayer I considered the chunk system and so far I think I'm set on using that. However I'm intregued by the first approach you explained too. I'm not too fond of having to use a dll for this but I'd really like to know how you would do the LOD system for it.

I also have a question about it. How Will the terrains normals act in such a system? I ask because I have ligthing and specular systems that require the normals to be correct. :)
 

lolslayer

Member
Thanks for the replies guys :)

@lolslayer I considered the chunk system and so far I think I'm set on using that. However I'm intregued by the first approach you explained too. I'm not too fond of having to use a dll for this but I'd really like to know how you would do the LOD system for it.

I also have a question about it. How Will the terrains normals act in such a system? I ask because I have ligthing and specular systems that require the normals to be correct. :)
Yeah, I also don't like using .DLL's, but DLL only makes a very minimal change and Game Maker should just have had it by default, so in this case I can live with it

The LOD system can be very easy, the grid that visualizes the heightmap will just have bigger grid cells the further away they are from the camera. This way you can reduce the poly count very easily without really noticing it. You just need to make sure that the border between two grid sizes don't have seems, but when creating the grid you can make transition grid cells at the borders to fix this.

Normals can be created by getting the cross product of vectors calculated using terrain adjacent to the position to calculate the normal from. I can give a more detailed explenation but a stackoverflow link should be enough:
https://stackoverflow.com/questions/5281261/generating-a-normal-map-from-a-height-map
 

Kentae

Member
Yeah, I also don't like using .DLL's, but DLL only makes a very minimal change and Game Maker should just have had it by default, so in this case I can live with it

The LOD system can be very easy, the grid that visualizes the heightmap will just have bigger grid cells the further away they are from the camera. This way you can reduce the poly count very easily without really noticing it. You just need to make sure that the border between two grid sizes don't have seems, but when creating the grid you can make transition grid cells at the borders to fix this.

Normals can be created by getting the cross product of vectors calculated using terrain adjacent to the position to calculate the normal from. I can give a more detailed explenation but a stackoverflow link should be enough:
https://stackoverflow.com/questions/5281261/generating-a-normal-map-from-a-height-map
That's Just the way I figured the terrain LOD would work but I was affraid of just that, getting seems. I'll have to experiment a bit with this :)

I already have a script to calculate the normals for my terrain. I just wondered how they would be affected when the terrain drawing was done with a flat plane manipulated by a vertex shader to get the hight. If that makes any sense? xD
 

lolslayer

Member
That's Just the way I figured the terrain LOD would work but I was affraid of just that, getting seems. I'll have to experiment a bit with this :)

I already have a script to calculate the normals for my terrain. I just wondered how they would be affected when the terrain drawing was done with a flat plane manipulated by a vertex shader to get the hight. If that makes any sense? xD
I can show you a model how I would fix the seems if you want

And I get what you mean, you just have to do the same calculations you did to calculate the normals in GML, but then in the vertex shader to get the correct normals of the terrain
 
Top