Normal map not working with triplanar shader

UnknownUserUnknownUser Posts: 1,392


Before is just diffuse and after is with the normal map applied. I am using GLES2 (although I get the same results in 3), and Zylann's voxel tools, with a modified script from one of the examples. What am I doing wrong?

shader_type spatial;

uniform sampler2D u_texture_top : hint_albedo;
uniform sampler2D u_texture_top_n : hint_normal;
uniform sampler2D u_texture_side : hint_albedo;
uniform sampler2D u_texture_side_n : hint_normal;
uniform sampler2D u_texture_bot : hint_albedo;
uniform sampler2D u_texture_bot_n : hint_normal;

varying vec3 v_world_pos;
varying vec3 v_world_normal;

vec3 get_triplanar_blend(vec3 world_normal) {
    vec3 blending = abs(world_normal);
    blending = normalize(max(blending, vec3(0.00001)));
    float b = blending.x + blending.y + blending.z;
    return blending / vec3(b, b, b);
}

vec4 texture_triplanar(sampler2D tex, vec3 world_pos, vec3 blend) {
    vec4 xaxis = texture(tex, world_pos.yz);
    vec4 yaxis = texture(tex, world_pos.xz);
    vec4 zaxis = texture(tex, world_pos.xy);
    return xaxis * blend.x + yaxis * blend.y + zaxis * blend.z;
}

void vertex() {
    v_world_pos = VERTEX;
    v_world_normal = NORMAL;
}

void fragment() {
    vec3 normal = normalize(v_world_normal);
    vec3 wpos = v_world_pos * 0.2;
    vec3 blending = get_triplanar_blend(normal);
    vec3 top_col = texture_triplanar(u_texture_top, wpos, blending).rgb;
    vec3 side_col = texture_triplanar(u_texture_side, wpos, blending).rgb;
    vec3 bot_col = texture_triplanar(u_texture_bot, wpos, blending).rgb;
    vec3 top_n = texture_triplanar(u_texture_top_n, wpos, blending).rgb;
    vec3 side_n = texture_triplanar(u_texture_side_n, wpos, blending).rgb;
    vec3 bot_n = texture_triplanar(u_texture_bot_n, wpos, blending).rgb;
    float r = top_col.r;
    vec3 mix_col = mix(top_col, side_col, clamp(normal.y * -4.0 + 2.5 + 4.0*r, 0.0, 1.0));
    vec3 mix_n = mix(side_n, top_n, clamp(normal.y * -4.0 + 2.5 + 4.0*r, 0.0, 1.0));
    ALBEDO = mix(mix_col, bot_col, clamp(normal.y * -4.0 - 1.5 + 4.0*r, 0.0, 1.0));
    //vec3 mixed_n = 
    NORMALMAP = mix(mix_n, bot_n, clamp(normal.y * -4.0 - 1.5 + 4.0*r, 0.0, 1.0));
    NORMALMAP_DEPTH = 1.0;
    SPECULAR = 0.5;
}

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file