Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a lowpoly shader #422

Open
CsloudX opened this issue Jul 18, 2024 · 4 comments
Open

Create a lowpoly shader #422

CsloudX opened this issue Jul 18, 2024 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@CsloudX
Copy link

CsloudX commented Jul 18, 2024

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?

@TokisanGames TokisanGames changed the title Create lowpoly terrain material Create a lowpoly shader Jul 18, 2024
@TokisanGames TokisanGames added the enhancement New feature or request label Jul 18, 2024
@TokisanGames TokisanGames added this to the Beta 0.9.x milestone Jul 18, 2024
@TokisanGames
Copy link
Owner

TokisanGames commented Jul 18, 2024

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.

@CsloudX
Copy link
Author

CsloudX commented Jul 18, 2024

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

@CsloudX
Copy link
Author

CsloudX commented Aug 29, 2024

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

@TokisanGames
Copy link
Owner

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 #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 TokisanGames added the good first issue Good for newcomers label Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: 0.9.4
Development

No branches or pull requests

2 participants