Terrain3D icon indicating copy to clipboard operation
Terrain3D copied to clipboard

Create a lowpoly shader

Open CsloudX opened this issue 1 year ago • 4 comments

Description

maybe there was a way to create lowpoly style terrain in current state. but i don't know to do it. if it can: can make a toturial in docs how to do this? if not: can implement this feature?

CsloudX avatar Jul 18 '24 04:07 CsloudX

Certainly possible. You have to write a shader to do it.

image

  • Set mesh_vertex_density to 10+
  • Set mesh_size to 64 (to push an artifact on the LOD border out further. You can see a black line in the top right)
  • Sculpt
  • Enable the override shader and use minimum.gdshader
  • Make these changes, thanks to @Xtarsia:
--- a/project/addons/terrain_3d/extras/minimum.gdshader
+++ b/project/addons/terrain_3d/extras/minimum.gdshader
@@ -25,6 +25,7 @@ uniform uint _mouse_layer = 0x80000000u; // Layer 32

 varying flat vec2 v_uv_offset;
 varying flat vec2 v_uv2_offset;
+varying vec3 v_world_vertex;

 ////////////////////////
 // Vertex
@@ -96,6 +97,7 @@ void vertex() {

        // Convert model space to view space w/ skip_vertex_transform render mode
        VERTEX = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
+       v_world_vertex = VERTEX; // Save vertex in world space
        VERTEX = (VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
        NORMAL = normalize((MODELVIEW_MATRIX * vec4(NORMAL, 0.0)).xyz);
        BINORMAL = normalize((MODELVIEW_MATRIX * vec4(BINORMAL, 0.0)).xyz);
@@ -140,12 +142,13 @@ void fragment() {
        vec2 uv = UV + v_uv_offset;
        vec2 uv2 = UV2 + v_uv2_offset;

-       // Calculate Terrain Normals. 4 lookups
-       vec3 w_tangent, w_binormal;
-       vec3 w_normal = get_normal(uv2, w_tangent, w_binormal);
-       NORMAL = mat3(VIEW_MATRIX) * w_normal;
-       TANGENT = mat3(VIEW_MATRIX) * w_tangent;
-       BINORMAL = mat3(VIEW_MATRIX) * w_binormal;
+       // Calculate Terrain Normals
+       vec3 w_tangent = normalize(dFdx(v_world_vertex));
+       vec3 w_binormal = normalize(dFdy(v_world_vertex));
+       vec3 w_normal = normalize(cross(w_tangent, w_binormal));
+       NORMAL = mat3(VIEW_MATRIX) * w_normal * -1.0;
+       TANGENT = mat3(VIEW_MATRIX) * w_tangent;
+       BINORMAL = mat3(VIEW_MATRIX) * w_binormal;

        // Apply PBR
        ALBEDO=vec3(.2);

In the picture above, I also enabled the heightmap debug view. The shader code is in our repo. Customize the shader to texture the terrain as you like.

Hacking on the main shader:

image

Here it is with texture UVs at 0.001, though this is sloppy. You see large squares due to nearest texture filtering, and triangle panels due to flat normals. Between vertices you see the shader blending textures and the texture blocks don't line up with the mesh triangles. This isn't the true low poly look, and is more costly than a low poly shader. It should be like the grey image above.

image

If using the main shader, what should be done is:

  • Use simple textures intended for low poly, even 1px x 1px single color, or no textures and use your own colors defined by your shader.
  • Dual scaling is useless and should be removed
  • The auto shader works if you keep the original NORMAL/TANGENT/BINORMAL, and overwrite them only at the end. But it's overkill as is.
  • Hand painting works, but the weighting should be removed so it stops blending between vertices. Have one material per poly.
  • Many of the other shader features are overkill and it should be cut out for a low poly terrain.

TokisanGames avatar Jul 18 '24 07:07 TokisanGames

@TokisanGames Oh, Thanks. The terrain look well. and maybe should improve with textures.

CsloudX avatar Jul 18 '24 08:07 CsloudX

HTerrain has this feature, wish has some help: https://hterrain-plugin.readthedocs.io/en/latest/#lowpoly image

CsloudX avatar Aug 29 '24 07:08 CsloudX

What is on that documentation page is how to get flat normals. We've already discussed how to show flat normals on our terrain above.

All that is remaining is painting with simple textures if you want the look in the picture.

Also see https://github.com/TokisanGames/Terrain3D/discussions/435

This issue and #435 already show how to do low poly shaders. It will remain open until someone makes a premade one for our extras directory.

TokisanGames avatar Aug 29 '24 08:08 TokisanGames

I will happily tackle this and open a change request later today, using this sample. Thank you for the starting point!

CrowhopTech avatar Jan 30 '25 00:01 CrowhopTech

Low poly shaders added in #609

TokisanGames avatar Feb 03 '25 20:02 TokisanGames