Skip to content

Commit

Permalink
Fix sprite crunch at half-pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
adamscott committed Jan 4, 2024
1 parent 179dfdc commit 1ddb85e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
17 changes: 16 additions & 1 deletion drivers/gles3/rasterizer_canvas_gles3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,22 @@ void RasterizerCanvasGLES3::_record_item_commands(const Item *p_item, RID p_rend

if (c->type != Item::Command::TYPE_MESH) {
// For Meshes, this gets updated below.
_update_transform_2d_to_mat2x3(base_transform * draw_transform, state.instance_data_array[r_index].world);
Transform2D final_transform = base_transform * draw_transform;

// Make sure to round nearest filtered textures to avoid sprite "crunching".
// We don't round linear filtered textures because it would look identical to nearest filtering.
switch (p_item->texture_filter) {
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
final_transform.set_origin(final_transform.get_origin().round());
} break;
default: {
// Do nothing.
}
}

_update_transform_2d_to_mat2x3(final_transform, state.instance_data_array[r_index].world);
}

// Zero out most fields.
Expand Down
14 changes: 14 additions & 0 deletions servers/rendering/renderer_rd/renderer_canvas_render_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,20 @@ void RendererCanvasRenderRD::_render_item(RD::DrawListID p_draw_list, RID p_rend

PushConstant push_constant;
Transform2D base_transform = p_canvas_transform_inverse * p_item->final_transform;

// Make sure to round nearest filtered textures to avoid sprite "crunching".
// We don't round linear filtered textures because it would look identical to nearest filtering.
switch (p_item->texture_filter) {
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST:
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS:
case RS::CanvasItemTextureFilter::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
base_transform.set_origin(base_transform.get_origin().round());
} break;
default: {
// Do nothing.
}
}

Transform2D draw_transform;
_update_transform_2d_to_mat2x3(base_transform, push_constant.world);

Expand Down

0 comments on commit 1ddb85e

Please sign in to comment.