0 votes

Hello there,
So I want to make use of Zylann's great plugin for heightmap generation to procedurally create levels. The thing is that I want my terrains to be constrained to certain limits (max height, max slope, roughness and erosion). The generator tool lets me create exactly what I need with some specific parameters for erosion, height range, scale, curve etc. plus some variation by changing the offset and seed.
The problem is that on the plugin documentation on how to create a map from code I can't seem to wrap my head around on how to include all the params available on the generator tool.
I tried to port the plugin's generator script and make use of it but I can't find at what point the script is writting to the terrain node and/or how to fully port it.

Any tips would be greatly appreciated as I've been stuck on this the whole weekend without any result :/

asked Mar 8 in Projects by omomthings (17 points)

1 Answer

+1 vote
Best answer

The generator tool in the plugin uses viewports and a series of shaders to produce an Image, which is then applied to the terrain textures in the same way as the end of the documentation example. It's quite advanced and took a lot of iterative work to be the way it is now, so explaining how everything works in detail would take a while.

The doc example shows the basics of how to set pixels of the terrain and update the node in a single pass (i.e only one XY for loop). If you start from there you should be able to tinker with a result pretty quickly, and have a better understanding of the basics.
On the other hand, each feature of the plugin's generator is a subject on its own, you may have to learn how each of them is done.
The main approaches used are:

  • Fractal noise: Perlin, OpenSimplex etc. This can be enough and is used by anyone generating their own terrain.
  • Curving: the heightmap can be deformed by applying a function to the height. For example if a quadratic curve is applied (slow start, fast end), it will further flatten the lower heights and make the highest peaks even sharper. If a "stepify" function is applied, it will create a mesa-looking environment. I was considering using a Curve resource later on.
  • Multiple passes: each pass takes result from the preceding pass. First generate noise, then run erosion algorithm 30 times in a row, then a final pass to calculate normals from the result
  • Fragment shaders: they are used instead of GDScript to speed up the process, but forces to wait one frame per pass so the generator usually takes a few seconds to complete.
  • Viewports: required for shader rendering. They are never cleared to allow using results from previous passes.
  • Tiling: this isn't strictly necessary but this generator runs on sub-sections of the terrain, because some graphics cards might not support largest viewport sizes. It also means the generator uses a bit of padding for tiles to be seamless, which is cropped later on.
  • At the end, viewport contents are downloaded as Image and copied into the terrain, just like the end of the documentation example shows.
    It is copied here: https://github.com/Zylann/godot_heightmap_plugin/blob/0f52300afb39c8f6b5ea48b4f879f76667cee18e/addons/zylann.hterrain/tools/generator/generator_dialog.gd#L317
    And uploaded here: See https://github.com/Zylann/godot_heightmap_plugin/blob/0f52300afb39c8f6b5ea48b4f879f76667cee18e/addons/zylann.hterrain/tools/generator/generator_dialog.gd#L340 (the generator only works on the heightmap at the moment).

And this is my own "formula" which I made in a somewhat messy way, there are plenty other possible tweaks.
This is all doable in GDScript without the extra bells and whistles, but it will be slower. If you want to go for the viewport shader approach, you may have to learn them first.

answered Mar 8 by Zylann (26,153 points)
selected Mar 8 by omomthings

This is awesome! Thank you for the detailed explanation this is totally a precious answer! I will surely have to work on this a lot to achieve what I'm looking for but now at least I can see where I'm going and what I need to focus on!
P.S : you are doing some awesome work with this comunity through the plugin's and the QA support! So thank you for the awesome job

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.