godot_heightmap_plugin icon indicating copy to clipboard operation
godot_heightmap_plugin copied to clipboard

How to get Multisplat16 working in procedural generation?

Open Critsium-xy opened this issue 1 year ago • 3 comments

If I create a new HTerrainData using code like this:

var terrain_data = HTerrainData.new()

And in generating procedure, I can get only one splatmap like this

var splatmap0: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT)

How can I get and edit other splatmaps? If I tried like this, it will raise an error

var splatmap1: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT,1)
var splatmap2: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT,2)
var splatmap3: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT,3)

And finally, could you help describe how to edit this four splatmaps to assign 16 different materials? Thank you!

Critsium-xy avatar Jun 02 '24 16:06 Critsium-xy

var splatmap1: Image = terrain_data.get_image(HTerrainData.CHANNEL_SPLAT,1)

This is how you should be able to get them though. Maybe the reason you could not is because by the time you run this, the terrain doesn't even know that they are needed, so they aren't there, so it might cause an error.

The issue is that the API isn't really thought for setting up these maps from scratch by code. If you change the terrain to use Multisplat16 in the editor and paint a bit on it, then save, it will have the images when you get them from script. If you don't want to do that, then you'd have to do what the painting tool does to add them, something like:

	var data = terrain.data
	while data.get_map_count(HTerrainData.CHANNEL_SPLAT) < 4:
		data._edit_add_map(HTerrainData.CHANNEL_SPLAT)

could you help describe how to edit this four splatmaps to assign 16 different materials? Thank you!

Not sure exactly what part you mean here. If you mean how to edit the splatmaps, then it's like a regular splatmap, except there are 16 weights across 4 images instead of just 4 weights in 1. You have to make sure they sum up to 1 across all images. Then it's just math. In GDScript, the code for normalizing would be something like:

var d := splat0.r + splat0.g + splat0.b + splat0.a \
       + splat1.r + splat1.g + splat1.b + splat1.a \
       + splat2.r + splat2.g + splat2.b + splat2.a \
       + splat3.r + splat3.g + splat3.b + splat3.a
splat0 /= d
splat1 /= d
splat2 /= d
splat3 /= d

The editor uses shaders on a viewport instead of editing the images with GDScript: https://github.com/Zylann/godot_heightmap_plugin/blob/b08f6c2a20e97d46d49ff5a9b4578696e7792739/addons/zylann.hterrain/tools/brush/shaders/splat16.gdshader#L75

Zylann avatar Jun 02 '24 16:06 Zylann

@Zylann Thank you! And I can do Globalmap baking in code?

Critsium-xy avatar Jun 02 '24 17:06 Critsium-xy

A globalmap is just like any other map, so you can do it in code too, however I don't think you can use the baker used in the editor. Or you'll have to do some digging to make it usable in-game.

Zylann avatar Jun 02 '24 17:06 Zylann