Skip to content

Commit

Permalink
Merge pull request #50294 from Calinou/distance-fade-use-circular-fade
Browse files Browse the repository at this point in the history
Use circular fade instead of linear fade for distance fade
  • Loading branch information
akien-mga committed Dec 6, 2022
2 parents 6fd5162 + d926be7 commit e19ead8
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1252,15 +1252,16 @@ void BaseMaterial3D::_update_shader() {
}

if (distance_fade != DISTANCE_FADE_DISABLED) {
// Use the slightly more expensive circular fade (distance to the object) instead of linear
// (Z distance), so that the fade is always the same regardless of the camera angle.
if ((distance_fade == DISTANCE_FADE_OBJECT_DITHER || distance_fade == DISTANCE_FADE_PIXEL_DITHER)) {
if (!RenderingServer::get_singleton()->is_low_end()) {
code += " {\n";

if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) {
code += " float fade_distance = abs((VIEW_MATRIX * MODEL_MATRIX[3]).z);\n";

code += " float fade_distance = length((VIEW_MATRIX * MODEL_MATRIX[3]));\n";
} else {
code += " float fade_distance = -VERTEX.z;\n";
code += " float fade_distance = length(VERTEX);\n";
}
// Use interleaved gradient noise, which is fast but still looks good.
code += " const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);";
Expand All @@ -1274,7 +1275,7 @@ void BaseMaterial3D::_update_shader() {
}

} else {
code += " ALPHA*=clamp(smoothstep(distance_fade_min,distance_fade_max,-VERTEX.z),0.0,1.0);\n";
code += " ALPHA *= clamp(smoothstep(distance_fade_min, distance_fade_max, length(VERTEX)), 0.0, 1.0);\n";
}
}

Expand Down

0 comments on commit e19ead8

Please sign in to comment.