Zylann / godot_heightmap_plugin

HeightMap terrain for Godot implemented in GDScript
Other
1.72k stars 159 forks source link

How can I use Generator values in a script? #208

Open elliotrpage opened 3 years ago

elliotrpage commented 3 years ago

Hello!

Thanks again for writing this.

I now have a terrain generation script using this plugin working, so I can quickly generate multiple terrain maps and then pick the one I would like to use.

I have worked out a set of variables using the Terrain Generation tool bundled with the addon I would like to use with the script (Roughness, curve, octaves, erosion, etc) How do I invoke/set these generation variables in my generation script? Are there parameters in hterrain.gd or hterrain_data.gd I need to invoke?

Many Thanks, Elliot

Zylann commented 3 years ago

If you went generating your terrain using GDScript, invoking functions from the built-in generator is not possible, because it uses shaders and an offscreen viewport (with some shaders running in feedback loop such as erosion). There is no straightforward way to just "invoke" it, its logic runs on the GPU over multiple frames. It still does generate an image in the end (which is what the heightmap ultimately is) so its final operations look very much like shown in the doc, but the generation process is very different.

You can either port the shader functions to GDScript, or look into shaders and viewports usage. The shaders used are here: https://github.com/Zylann/godot_heightmap_plugin/tree/master/addons/zylann.hterrain/tools/generator/shaders

If you only want to invoke the generator, sorry it's not yet possible to use it standalone, though it's probably not much work to do it. The main thing to know about it, is that it runs over multiple frames, so the result comes from signals after some time rather than the result of a function, and on top of that, it produces tiles, not the entire terrain at once. I would like to make a proper API for it and started to switch it to a resource, but I haven't taken the time to work more on it yet.

elliotrpage commented 3 years ago

Thanks for getting back to me so quickly. I had started to suspect this was the case from poking around in the .gd files.

The main reason I was using the scripted generation was to automatically apply some basic texturing and to "curl up" the edges of the map to stop the player walking off it.

To get the same effect i will instead generate a heap of terrains using the generator dialog and then write a modification script (like the one mentioned in the docs) to apply the shaders and map edges. This should have the same effect just with a few extra mouseclicks (unless I am missing something).