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

[3.x] Allow Distance Fade Pixel Dither and Object Dither in the GLES2 backend #50295

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
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: 14 additions & 17 deletions scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,25 +914,22 @@ void SpatialMaterial::_update_shader() {
// 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 (!VisualServer::get_singleton()->is_low_end()) {
code += "\t{\n";

if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) {
code += "\t\tfloat fade_distance = length((INV_CAMERA_MATRIX * WORLD_MATRIX[3]));\n";
} else {
code += "\t\tfloat fade_distance = length(VERTEX);\n";
}
// Use interleaved gradient noise, which is fast but still looks good.
code += "\t\tconst vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);";
code += "\t\tfloat fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0);\n";
// Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance.
code += "\t\tif (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) {\n";
code += "\t\t\tdiscard;\n";
code += "\t\t}\n";

code += "\t}\n\n";
code += "\t{\n";

if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) {
code += "\t\tfloat fade_distance = length((INV_CAMERA_MATRIX * WORLD_MATRIX[3]));\n";
} else {
code += "\t\tfloat fade_distance = length(VERTEX);\n";
}
// Use interleaved gradient noise, which is fast but still looks good.
code += "\t\tconst vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);";
code += "\t\tfloat fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0);\n";
// Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance.
code += "\t\tif (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) {\n";
code += "\t\t\tdiscard;\n";
code += "\t\t}\n";

code += "\t}\n\n";
} else {
code += "\tALPHA *= clamp(smoothstep(distance_fade_min, distance_fade_max, length(VERTEX)), 0.0, 1.0);\n";
}
Expand Down