Zylann / godot_heightmap_plugin

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

Tangent error in shader #447

Open agung098 opened 5 months ago

agung098 commented 5 months ago

If i add triplanar tangent script in shader its error in godot 4.2

Zylann commented 5 months ago

What are you talking about? Which script? Which code? What are you doing? Which plugin version? You're not providing enough information. Please don't delete the issue template if it's to write a single line with no details. Also if all you did is to modify one of the plugin shaders, this is likely not an issue with the plugin but more a Godot shaders question, which you can ask in different places.

agung098 commented 5 months ago

I put this code in vertex, for make triplanar : TANGENT = cross(NORMAL, vec3(0,0,1)); BINORMAL = cross(NORMAL, TANGENT);

This is error message: servers/rendering/renderer_rd/forward_clustered/render_forward_clustered.cpp:3725 - Attempting to use a shader that requires tangents with a mesh that doesn't contain tangents. Ensure that meshes are imported with the 'ensure_tangents' option. If creating your own meshes, add an ARRAY_TANGENTarray (when using ArrayMesh) or callgenerate_tangents() (when using SurfaceTool). (User)

Zylann commented 5 months ago

Well, first, this warning is bogus because the mesh used for rendering does not need tangents, especially if you make them up in the shader anyways. So at the very least, it looks like an oversight from Godot.

But one thing you could do is to not put this code in the vertex shader, or create a varying to pass it to the vertex shader instead of using Godot's built-in TANGENT.

agung098 commented 5 months ago

It's issues in godot but, issues closed:https://github.com/godotengine/godot/issues/84875

Zylann commented 5 months ago

It's been closed without the present case being raised. So maybe it should be re-opened, but if having this warning is desired to handle the other cases, I'm not sure how the devs should make Godot realize when tangents are generated in the shader...

agung098 commented 5 months ago

I try in different terrain plugin but its ok, no error message

agung098 commented 5 months ago

This is simple terrain for try: https://github.com/majikayogames/SimpleTerrain/tree/main/addons/SimpleTerrain

Zylann commented 5 months ago

Why are you linking this? It's not even using this plugin. But should be noted that this plugin does not assign TANGENT anywhere in its shader, from what I can see. It's also working differently so obviously some things dont happen the same way.

agung098 commented 5 months ago

https://github.com/TokisanGames/Terrain3D/blob/main/src/shaders/main.glsl#L10

agung098 commented 5 months ago

vec3 get_normal(vec2 uv, out vec3 tangent, out vec3 binormal) { float u, v, height; vec3 normal; // Use vertex normals within radius of vertex_normals_distance, and along region borders. if (v_region_border_mask > 0.5 || v_vertex_xz_dist < vertex_normals_distance) { normal = normalize(v_normal); } else { height = get_height(uv); u = height - get_height(uv + vec2(_region_texel_size, 0)); v = height - get_height(uv + vec2(0, _region_texel_size)); normal = normalize(vec3(u, _mesh_vertex_spacing, v)); } tangent = cross(normal, vec3(0, 0, 1)); binormal = cross(normal, tangent); return normal; }

Zylann commented 5 months ago

That's yet another plugin where they don't even precalculate normals. I think they also added tangents to their mesh (which is also different, it uses clipmap instead of quad tree) just to silence the warning, even though they are not even used by the shader.

As I said, you can still make your tangent in the shader if you want to, by not using Godot's builtin variable in the vertex shader or doing it in the vertex shader, no need to refactor how this plugin works

Zylann commented 5 months ago

Also, one of the shaders (Classic4) already has a triplanar slot (which also isn't using TANGENT): image

The reason it works without TANGENT is because for a very long time this plugin just factored all normalmapping (ground and textures) into NORMAL, and doesn't use NORMALMAP. I don't exactly recall why (the plugin has been around since Godot 2) It's not how it's normally done nowadays I guess, but if the latter is desired, shaders need to be modified a bit more extensively. I have no plan to do this, but PRs are welcome.

agung098 commented 5 months ago

Same error message if i input NORMAL_MAP in multisplat16 fragment shader

agung098 commented 5 months ago
    NORMAL_MAP = /*u_terrain_normal_basis **/ (
        w.r * normal0 +
        w.g * normal1 +
        w.b * normal2 +
        w.a * normal3) / w_sum;
Zylann commented 5 months ago

Well, that's even more annoying... again, this warning makes no sense here.

So here are your options: