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

Fixing incorrect normal map when using triplanar world mapping and mesh rotation #83658

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,20 +1059,33 @@ void BaseMaterial3D::_update_shader() {
}
if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) {
//generate tangent and binormal in world space
code += " TANGENT = vec3(0.0,0.0,-1.0) * abs(NORMAL.x);\n";
code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.y);\n";
code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(NORMAL.z);\n";
code += " TANGENT = normalize(TANGENT);\n";
if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
code += " vec3 normal = MODEL_NORMAL_MATRIX * NORMAL;\n";
} else {
code += " vec3 normal = NORMAL;\n";
}
code += " TANGENT = vec3(0.0,0.0,-1.0) * abs(normal.x);\n";
code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(normal.y);\n";
code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(normal.z);\n";
if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
code += " TANGENT = inverse(MODEL_NORMAL_MATRIX) * normalize(TANGENT);\n";
} else {
code += " TANGENT = normalize(TANGENT);\n";
}
RPicster marked this conversation as resolved.
Show resolved Hide resolved

code += " BINORMAL = vec3(0.0,1.0,0.0) * abs(NORMAL.x);\n";
code += " BINORMAL+= vec3(0.0,0.0,-1.0) * abs(NORMAL.y);\n";
code += " BINORMAL+= vec3(0.0,1.0,0.0) * abs(NORMAL.z);\n";
code += " BINORMAL = normalize(BINORMAL);\n";
code += " BINORMAL = vec3(0.0,1.0,0.0) * abs(normal.x);\n";
code += " BINORMAL+= vec3(0.0,0.0,-1.0) * abs(normal.y);\n";
code += " BINORMAL+= vec3(0.0,1.0,0.0) * abs(normal.z);\n";
if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
code += " BINORMAL = inverse(MODEL_NORMAL_MATRIX) * normalize(BINORMAL);\n";
} else {
code += " BINORMAL = normalize(BINORMAL);\n";
}
}

if (flags[FLAG_UV1_USE_TRIPLANAR]) {
if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) {
code += " uv1_power_normal=pow(abs(mat3(MODEL_MATRIX) * NORMAL),vec3(uv1_blend_sharpness));\n";
code += " uv1_power_normal=pow(abs(normal),vec3(uv1_blend_sharpness));\n";
AThousandShips marked this conversation as resolved.
Show resolved Hide resolved
code += " uv1_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0f)).xyz * uv1_scale + uv1_offset;\n";
} else {
code += " uv1_power_normal=pow(abs(NORMAL),vec3(uv1_blend_sharpness));\n";
Expand Down
Loading