random terrain generation in BGE- with blend

I’ve finally integrated the random texture generator with the terrain deformer.


This is just an early test, I have to clean up the code and include the tree placer, but it is nearly ready for my modest needs.

Try the blend file, tell me if it works.

the properties “mix” and “mix2” set the roughness of the terrain. If “mix2” is set to 0.0
it will only use the value of “mix”.

Values of around 8 will give very rough terrain.
Values of around 36 or 64 will give very smooth.

“slopes” sets the slope detection angle. I could do with re-writing this bit to be more discerning. 1 detects a lot of slopes, 3 detects not many. 0 would give you an all slope map (no grass and trees).

“scale” sets the height of the mountains.

“high” sets the lowest point that mountains are formed.
“low” sets the highest point that pits start from.

Set “high” to a value near 255 to have no mountains, set “low” to near 1 to have no pits.

I plan to add some more details later, such as a random texture picker to pick textures other than the rock and grass ones (allowing for deserts and others) and also a tree placer which will place tree objects based on a random texture avoiding slopes.

credit goes to this person for creating the perlin noise script:

oops, forgot that you need to add
own.reinstancePhysicsMesh(own, mesh)
to the end of the script to make the changes physically effective.

New version which uses nodes to give a nicer texture.
try it here:


That’s pretty cool… This would be great for RTS games. It would be so much epic this way, to generate a random map (of course with a few edits, you can make a map where 2 - 4 bases could be created, with a decent distance apart). That’s really cool :slight_smile:

Great job :wink:

Hey Smoking Mirror you are working hard! Congratulations. This is freaking awesome! I guess you plan is to integrate this onto your Mech game? Is the “generate” script the only one running? Or the “text” file is also in action in this demo?

Works pretty nice. Don’t forget that now we can attach .blend files directly into BlenderArtists, maybe, in the long run, it’s i good idea to have your file attached to the initial post :wink:



This will come handy.
Regards dude!

Wow! Very nice! I agree with Linkxgl great for RTS games, good work.

Thanks everyone. :slight_smile:
I plan to improve the script soon.

> make more modular, so you can use just the parts you want.
> make a random texture chooser or at least a terrain type property. To generate swamps, islands, grassland and deserts.
> add support for a tree spawner
> add support for a buliding spawner, road generator and small town generator.

Yes, it’s for the mech game.

The Text file was an early version without the random factor.
I wanted to attach the blend, but it is a little too big because of the textures.

Everything should be modular, so you only use the parts you need for your project.
I considered using nodes for the textures, but I’ve found that I can’t get lights to affect a node texture in game.
In usual circumstances I could bake the shadows and light and use it as a multiplier, but in this case the map will change every time you regenerate, so nodes won’t work. :frowning:

If you know a way around this please tell me, as I got nodes working pretty well, before finding out about the light problem.

oh, my mistake, the Text file was an early version without the random factor.
I wanted to attach the blend, but it is a little too big because of the textures.

Oh, I had some progress with nodes after all. Keep an eye open for a special nodes version coming soon.

Looking forward to that one! This project is a brilliant addition to the resources! Thanks for sharing it.

I added my new version which uses nodes for the textures.
I’m still getting a little bit of a messy result at the very edges of the map…
I’ve tried a lot to deal with it but I can’t seen to get rid of the the problem.

It is related to the order of the verts in the mesh. There seems no good way of sorting them to get the result I want.

Anyway, it came out nice in the end. :slight_smile: (even with the problems).

I was wondering, smoking mirror. This could be a good Vertex displacement shader replacement! Can it use an object material’s textures for heighmap, color, and even maybe normals too?! This way I can maybe implement in my planetary generator!

I ran in to some troubles when trying to run this in a blender player stand alone runtime.
The Blender Image Module is not included in the Blender player file.
It looks like the PIL (Python image library) may have to be packaged and the script re-written to get it to work.

On the other hand I’ve been working on a non-realtime version so I can generate some terrain quickly for use in games.

I think the generation is too slow for it to work in a shader.

Well that’s unfortunate! I’ll have to resort to pre-made terrain patches! Vertex displacement, doesn’t seem to respond to reinstance mesh physiscs! At least it’s a straight forward method!

Hey Smoking, could you update this for 2.5? Someone’s asking for it on the Blender.org forum.

I’ve not upgraded to 2.5 myself yet. So I’m not sure how the code needs to be changed…
Also I’m not sure if the code I used from “import Blender” still works with the new version, it sounded at the time if some of it was going to be re-written or dumped completely.
The prinicple is not too difficult, and I think someone else was working on a similar thing around here somewhere…

I love this demo, i started working to update it to 2.5x over the weekend, but ran out of time, i’ll post it if i finish it…

This script is not easy to change about the shuffle and the setPixel methods.
The old Blender had some getPixel/setPixel methods, but they were brutally slow (no surprise there!) for image manipulation.
The new bpy.ops.image has no getPixel/setPixel methods. I don’t know if the put it back to the API.

Using the current 1.1.7 version of the Python Imaging Library is not possible, it does not yet support Python 3.
So we must wait for the new version which support Python 3.

One possibility is to use NumPy, which has some fast array types and methods, for image manipulation. NumPy currently builds with Python 3. We may need to build some adapter classes to make NumPy and Blender play nicely together.

You know, the VideoTexture / bge.texture modules can handle changing images to arrays. If you don’t need to save them back, you can use the array and check and change the colors manually, then set the image’s source to use the array. I checked the color of an (external) image in my Shadow maps example.

Yes, working with the Video texture module may work…
In the end I decided that I didn’t quite need the high level of control I was getting in this technique for small random levels, so I’m just going to be using random numbers to adjust the height and basic color of the verts on the mesh. Not as nice a result, but quicker and possible even in exported EXE files.