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

Add an option to jitter BaseMaterial3D dithering pattern across frames #79522

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions doc/classes/BaseMaterial3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,10 @@
Smoothly fades the object out based on each pixel's distance from the camera using the alpha channel.
</constant>
<constant name="DISTANCE_FADE_PIXEL_DITHER" value="2" enum="DistanceFadeMode">
Smoothly fades the object out based on each pixel's distance from the camera using a dithering approach. Dithering discards pixels based on a set pattern to smoothly fade without enabling transparency. On certain hardware, this can be faster than [constant DISTANCE_FADE_PIXEL_ALPHA].
Smoothly fades the object out based on each pixel's distance from the camera using a dithering approach. Dithering discards pixels based on a set pattern to smoothly fade without enabling transparency. On certain hardware, this can be faster than [constant DISTANCE_FADE_PIXEL_ALPHA]. If [member ProjectSettings.rendering/anti_aliasing/quality/use_taa] is [code]true[/code] when the project starts, the dithering pattern is jittered every frame to improve quality.
</constant>
<constant name="DISTANCE_FADE_OBJECT_DITHER" value="3" enum="DistanceFadeMode">
Smoothly fades the object out based on the object's distance from the camera using a dithering approach. Dithering discards pixels based on a set pattern to smoothly fade without enabling transparency. On certain hardware, this can be faster than [constant DISTANCE_FADE_PIXEL_ALPHA] and [constant DISTANCE_FADE_PIXEL_DITHER].
Smoothly fades the object out based on the object's distance from the camera using a dithering approach. Dithering discards pixels based on a set pattern to smoothly fade without enabling transparency. On certain hardware, this can be faster than [constant DISTANCE_FADE_PIXEL_ALPHA] and [constant DISTANCE_FADE_PIXEL_DITHER]. If [member ProjectSettings.rendering/anti_aliasing/quality/use_taa] is [code]true[/code] when the project starts, the dithering pattern is jittered every frame to improve quality.
</constant>
</constants>
</class>
2 changes: 1 addition & 1 deletion doc/classes/ProjectSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2371,7 +2371,7 @@
[b]Note:[/b] This property is only read when the project starts. To set debanding at run-time, set [member Viewport.use_debanding] on the root [Viewport] instead.
</member>
<member name="rendering/anti_aliasing/quality/use_taa" type="bool" setter="" getter="" default="false">
Enables Temporal Anti-Aliasing for the default screen [Viewport]. TAA works by jittering the camera and accumulating the images of the last rendered frames, motion vector rendering is used to account for camera and object motion. Enabling TAA can make the image blurrier, which is partially counteracted by automatically using a negative mipmap LOD bias (see [member rendering/textures/default_filters/texture_mipmap_bias]).
Enables Temporal Anti-Aliasing for the default screen [Viewport]. TAA works by jittering the camera and accumulating the images of the last rendered frames, motion vector rendering is used to account for camera and object motion. Enabling TAA can make the image blurrier, which is partially counteracted by automatically using a negative mipmap LOD bias (see [member rendering/textures/default_filters/texture_mipmap_bias]). If TAA is enabled when the project starts, the dithering pattern for the [constant BaseMaterial3D.DISTANCE_FADE_PIXEL_DITHER] and [constant BaseMaterial3D.DISTANCE_FADE_OBJECT_DITHER] distance fade modes is jittered every frame to improve quality.
[b]Note:[/b] The implementation is not complete yet. Some visual instances such as particles and skinned meshes may show ghosting artifacts in motion.
[b]Note:[/b] TAA is only supported in the Forward+ rendering method, not Mobile or Compatibility.
</member>
Expand Down
9 changes: 9 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4293,6 +4293,15 @@
<constant name="MATERIAL_RENDER_PRIORITY_MAX" value="127">
The maximum renderpriority of all materials.
</constant>
<constant name="BASE_MATERIAL_3D_DITHERING_JITTER_AUTO" value="0" enum="BaseMaterial3DDitheringJitter">
Jitter the [BaseMaterial3D] dithered transparency modes' pattern if temporal antialiasing (TAA) is enabled.
</constant>
<constant name="BASE_MATERIAL_3D_DITHERING_JITTER_NEVER" value="1" enum="BaseMaterial3DDitheringJitter">
Never jitter the [BaseMaterial3D] dithered transparency modes' pattern, even if temporal antialiasing (TAA) is enabled.
</constant>
<constant name="BASE_MATERIAL_3D_DITHERING_JITTER_ALWAYS" value="2" enum="BaseMaterial3DDitheringJitter">
Always jitter the [BaseMaterial3D] dithered transparency modes' pattern, even if temporal antialiasing (TAA) is disabled.
</constant>
<constant name="ARRAY_VERTEX" value="0" enum="ArrayType">
Array is a vertex position array.
</constant>
Expand Down
12 changes: 11 additions & 1 deletion scene/resources/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/error/error_macros.h"
#include "core/version.h"
#include "scene/main/scene_tree.h"
#include "scene/main/viewport.h"

void Material::set_next_pass(const Ref<Material> &p_pass) {
for (Ref<Material> pass_child = p_pass; pass_child != nullptr; pass_child = pass_child->get_next_pass()) {
Expand Down Expand Up @@ -1669,8 +1670,17 @@ void fragment() {)";
// Use interleaved gradient noise, which is fast but still looks good.
const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189);
float fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0);
)";

if (GLOBAL_GET("rendering/anti_aliasing/quality/use_taa") || int(GLOBAL_GET("rendering/scaling_3d/mode")) == Viewport::SCALING_3D_MODE_FSR2) {
code += " float jitter = fract(sin(dot(vec2(TIME), vec2(12.9898, 78.233))) * 43758.5453);";
} else {
code += " float jitter = 0.0;";
}

code += R"(
// Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance.
if (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) {
if (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy + vec2(jitter), magic.xy)))) {
discard;
}
}
Expand Down
4 changes: 4 additions & 0 deletions servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,10 @@ void RenderingServer::_bind_methods() {
BIND_CONSTANT(MATERIAL_RENDER_PRIORITY_MIN);
BIND_CONSTANT(MATERIAL_RENDER_PRIORITY_MAX);

BIND_ENUM_CONSTANT(BASE_MATERIAL_3D_DITHERING_JITTER_AUTO);
BIND_ENUM_CONSTANT(BASE_MATERIAL_3D_DITHERING_JITTER_NEVER);
BIND_ENUM_CONSTANT(BASE_MATERIAL_3D_DITHERING_JITTER_ALWAYS);

/* MESH API */

ClassDB::bind_method(D_METHOD("mesh_create_from_surfaces", "surfaces", "blend_shape_count"), &RenderingServer::_mesh_create_from_surfaces, DEFVAL(0));
Expand Down
7 changes: 7 additions & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ class RenderingServer : public Object {
MATERIAL_RENDER_PRIORITY_MAX = 127,
};

enum BaseMaterial3DDitheringJitter {
BASE_MATERIAL_3D_DITHERING_JITTER_AUTO,
BASE_MATERIAL_3D_DITHERING_JITTER_NEVER,
BASE_MATERIAL_3D_DITHERING_JITTER_ALWAYS,
};

virtual RID material_create() = 0;

virtual void material_set_shader(RID p_shader_material, RID p_shader) = 0;
Expand Down Expand Up @@ -1779,6 +1785,7 @@ class RenderingServer : public Object {
VARIANT_ENUM_CAST(RenderingServer::TextureLayeredType);
VARIANT_ENUM_CAST(RenderingServer::CubeMapLayer);
VARIANT_ENUM_CAST(RenderingServer::ShaderMode);
VARIANT_ENUM_CAST(RenderingServer::BaseMaterial3DDitheringJitter);
VARIANT_ENUM_CAST(RenderingServer::ArrayType);
VARIANT_BITFIELD_CAST(RenderingServer::ArrayFormat);
VARIANT_ENUM_CAST(RenderingServer::ArrayCustomFormat);
Expand Down
Loading