Zylann / godot_voxel

Voxel module for Godot Engine
MIT License
2.59k stars 244 forks source link

Setting material parameters at runtime (VoxelLodTerrain material duplication) #212

Open ChristianB84 opened 3 years ago

ChristianB84 commented 3 years ago

I tried to modify the material properties of the LOD terrain when already some terrain had been generated, but noticed that my changes only worked for the new terrain (and even in that case only partially). Because issue #104 is still open, I expected there to be only one material for the whole terrain (and only one is exposed in the editor as far as I can tell).

Then I looked at the code and noticed that the material is actually duplicated in voxel_lod_terrain.cpp (currently starting at line 1073). There is a code comment that points to https://github.com/godotengine/godot/issues/34741. I don't really understand why this one material needs to be "pooled", but if that's actually necessary, is there any way to globally change the material properties?

I locally commented out those lines and it seems to work as I'd expect it.

Zylann commented 3 years ago

The reason is that to make Transvoxel work best, some bits have to be done in shader to stitch LODs together. To do this, each block needs to know seam configurations in the shader, and it's different on each block. So while every block has the same shader, they have to get different parameter values (i.e different ShaderMaterial). This makes hundreds of ShaderMaterials reference the same Shader, which led me to pool them as well, because Godot was adding/removing connections to the shader but because there was hundreds of them it was affecting FPS.

In Godot 3 there is no way to specify per-instance uniform parameters, so that's what I ended up with. Godot 4 has that feature so the material could be made single again.

If you want to change parameters at runtime we could add methods to do it, although since it's going to run over hundreds of materials it won't be as fast.

You can also deactivate the duplication code, but bear in mind you will see LOD cracks more often.

Because issue #104 is still open, I expected there to be only one material for the whole terrain (and only one is exposed in the editor as far as I can tell).

There is a single material for smooth terrain. Though 104 is not really the same issue.

ChristianB84 commented 3 years ago

Oh, okay, so it's essentially a workaround. That makes sense. I didn't see any issues so far in my local build where I removed the duplication. Which kind of subtle issues should I expect? Currently I'm changing the material only for debugging purposes (I have a button for reloading my shaders to make debugging them easier).

Zylann commented 3 years ago

Essentially, LOD cracks. When two chunks of different LOD touch, geometry might not perfectly align, so you can see tiny openings in some places where the sky can be seen through. Depending on your settings they might not be noticeable, but they will be there. It's not a show stopper, since this is only a visual issue. But I know some people really want to hide them as much as possible.