Merge pull request #44690 from lawnjelly/transform_snap2

[3.2] Improve 2d snapping
This commit is contained in:
Rémi Verschelde 2021-01-05 17:19:44 +01:00 committed by GitHub
commit 3032b38b52
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 4 deletions

View file

@ -60,6 +60,7 @@ private:
float _time_scale; float _time_scale;
bool _pixel_snap; bool _pixel_snap;
bool _snap_2d_transforms; bool _snap_2d_transforms;
bool _snap_2d_viewports;
uint64_t _physics_frames; uint64_t _physics_frames;
float _physics_interpolation_fraction; float _physics_interpolation_fraction;
@ -109,6 +110,7 @@ public:
_FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; } _FORCE_INLINE_ bool get_use_pixel_snap() const { return _pixel_snap; }
bool get_snap_2d_transforms() const { return _snap_2d_transforms; } bool get_snap_2d_transforms() const { return _snap_2d_transforms; }
bool get_snap_2d_viewports() const { return _snap_2d_viewports; }
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
_FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; } _FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; }

View file

@ -1119,6 +1119,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()->_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); Engine::get_singleton()->_snap_2d_transforms = GLOBAL_DEF("rendering/quality/2d/use_transform_snap", false);
Engine::get_singleton()->_snap_2d_viewports = GLOBAL_DEF("rendering/quality/2d/use_camera_snap", false);
OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true); OS::get_singleton()->_keep_screen_on = GLOBAL_DEF("display/window/energy_saving/keep_screen_on", true);
if (rtm == -1) { if (rtm == -1) {
rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE); rtm = GLOBAL_DEF("rendering/threads/thread_model", OS::RENDER_THREAD_SAFE);

View file

@ -99,7 +99,7 @@ void Sprite::_get_rects(Rect2 &r_src_rect, Rect2 &r_dst_rect, bool &r_filter_cli
Point2 dest_offset = offset; Point2 dest_offset = offset;
if (centered) if (centered)
dest_offset -= frame_size / 2; dest_offset -= frame_size / 2;
if (Engine::get_singleton()->get_snap_2d_transforms()) { if (Engine::get_singleton()->get_use_pixel_snap()) {
dest_offset = dest_offset.floor(); dest_offset = dest_offset.floor();
} }
@ -378,7 +378,7 @@ Rect2 Sprite::get_rect() const {
Point2 ofs = offset; Point2 ofs = offset;
if (centered) if (centered)
ofs -= Size2(s) / 2; ofs -= Size2(s) / 2;
if (Engine::get_singleton()->get_snap_2d_transforms()) { if (Engine::get_singleton()->get_use_pixel_snap()) {
ofs = ofs.floor(); ofs = ofs.floor();
} }

View file

@ -40,12 +40,24 @@ static Transform2D _canvas_get_transform(VisualServerViewport::Viewport *p_viewp
Transform2D xf = p_viewport->global_transform; Transform2D xf = p_viewport->global_transform;
float scale = 1.0; float scale = 1.0;
bool snap = Engine::get_singleton()->get_snap_2d_viewports();
if (p_viewport->canvas_map.has(p_canvas->parent)) { if (p_viewport->canvas_map.has(p_canvas->parent)) {
xf = xf * p_viewport->canvas_map[p_canvas->parent].transform;
Transform2D c_xform = p_viewport->canvas_map[p_canvas->parent].transform;
if (snap) {
c_xform.elements[2] = c_xform.elements[2].floor();
}
xf = xf * c_xform;
scale = p_canvas->parent_scale; scale = p_canvas->parent_scale;
} }
xf = xf * p_canvas_data->transform; Transform2D c_xform = p_canvas_data->transform;
if (snap) {
c_xform.elements[2] = c_xform.elements[2].floor();
}
xf = xf * c_xform;
if (scale != 1.0 && !VSG::canvas->disable_scale) { if (scale != 1.0 && !VSG::canvas->disable_scale) {
Vector2 pivot = p_vp_size * 0.5; Vector2 pivot = p_vp_size * 0.5;