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 2d snap transforms option #43554

Merged
merged 1 commit into from
Nov 16, 2020
Merged
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
1 change: 1 addition & 0 deletions core/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ Engine::Engine() {
_target_fps = 0;
_time_scale = 1.0;
_pixel_snap = false;
_snap_2d_transforms = false;
_physics_frames = 0;
_idle_frames = 0;
_in_physics = false;
Expand Down
2 changes: 2 additions & 0 deletions core/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Engine {
int _target_fps;
float _time_scale;
bool _pixel_snap;
bool _snap_2d_transforms;
uint64_t _physics_frames;
float _physics_interpolation_fraction;

Expand Down Expand Up @@ -110,6 +111,7 @@ class Engine {
Object *get_singleton_object(const String &p_name) const;

_FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; }
bool get_snap_2d_transforms() const { return _snap_2d_transforms; }

#ifdef TOOLS_ENABLED
_FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; }
Expand Down
1 change: 1 addition & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
}

Engine::get_singleton()->_pixel_snap = GLOBAL_DEF("rendering/quality/2d/use_pixel_snap", false);
Engine::get_singleton()->_snap_2d_transforms = GLOBAL_DEF("rendering/quality/2d/use_transform_snap", false);
OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
if (rtm == -1) {
rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE);
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/animated_sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ void AnimatedSprite::_notification(int p_what) {
if (centered)
ofs -= s / 2;

if (Engine::get_singleton()->get_use_pixel_snap()) {
if (Engine::get_singleton()->get_snap_2d_transforms()) {
ofs = ofs.floor();
}
Rect2 dst_rect(ofs, s);
Expand Down
4 changes: 2 additions & 2 deletions scene/2d/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void Sprite::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_cli
Point2 dest_offset = offset;
if (centered)
dest_offset -= frame_size / 2;
if (Engine::get_singleton()->get_use_pixel_snap()) {
if (Engine::get_singleton()->get_snap_2d_transforms()) {
dest_offset = dest_offset.floor();
}

Expand Down Expand Up @@ -378,7 +378,7 @@ Rect2 Sprite::get_rect() const {
Point2 ofs = offset;
if (centered)
ofs -= Size2(s) / 2;
if (Engine::get_singleton()->get_use_pixel_snap()) {
if (Engine::get_singleton()->get_snap_2d_transforms()) {
ofs = ofs.floor();
}

Expand Down
8 changes: 7 additions & 1 deletion servers/visual/visual_server_canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ void VisualServerCanvas::_render_canvas_item(Item *p_canvas_item, const Transfor
}

Rect2 rect = ci->get_rect();
Transform2D xform = p_transform * ci->xform;
Transform2D xform = ci->xform;
if (snap_2d_transforms) {
xform.elements[2] = xform.elements[2].floor();
}
xform = p_transform * xform;

Rect2 global_rect = xform.xform(rect);
global_rect.position += p_clip_rect.position;

Expand Down Expand Up @@ -1477,6 +1482,7 @@ VisualServerCanvas::VisualServerCanvas() {
z_last_list = (RasterizerCanvas::Item **)memalloc(z_range * sizeof(RasterizerCanvas::Item *));

disable_scale = false;
snap_2d_transforms = Engine::get_singleton()->get_snap_2d_transforms();
}

VisualServerCanvas::~VisualServerCanvas() {
Expand Down
1 change: 1 addition & 0 deletions servers/visual/visual_server_canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ class VisualServerCanvas {
RID_Owner<RasterizerCanvas::Light> canvas_light_owner;

bool disable_scale;
bool snap_2d_transforms;

private:
void _render_canvas_item_tree(Item *p_canvas_item, const Transform2D &p_transform, const Rect2 &p_clip_rect, const Color &p_modulate, RasterizerCanvas::Light *p_lights);
Expand Down