From 2f53325beda6531eff42460bd95881bd6f8e392b Mon Sep 17 00:00:00 2001 From: poke1024 Date: Wed, 28 Feb 2018 20:23:40 +0100 Subject: [PATCH] Fix round preview getting square on "run scene" (issue 16734) (cherry picked from commit 899f7b125e843d7187ad7c614588d635ce989f80) --- editor/editor_node.cpp | 1 + editor/plugins/editor_preview_plugins.cpp | 32 +++++++++++++++++++++++ editor/plugins/editor_preview_plugins.h | 2 ++ 3 files changed, 35 insertions(+) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 7441e322be8..dcd6952539b 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -975,6 +975,7 @@ void EditorNode::_save_scene_with_preview(String p_file, int p_idx) { String file = cache_base + ".png"; + post_process_preview(img); img->save_png(file); } diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp index 215964235e1..e4557776640 100644 --- a/editor/plugins/editor_preview_plugins.cpp +++ b/editor/plugins/editor_preview_plugins.cpp @@ -39,6 +39,38 @@ #include "scene/resources/material.h" #include "scene/resources/mesh.h" +void post_process_preview(Ref p_image) { + + if (p_image->get_format() != Image::FORMAT_RGBA8) + p_image->convert(Image::FORMAT_RGBA8); + + p_image->lock(); + + const int w = p_image->get_width(); + const int h = p_image->get_height(); + + const int r = MIN(w, h) / 32; + const int r2 = r * r; + Color transparent = Color(0, 0, 0, 0); + + for (int i = 0; i < r; i++) { + for (int j = 0; j < r; j++) { + int dx = i - r; + int dy = j - r; + if (dx * dx + dy * dy > r2) { + p_image->set_pixel(i, j, transparent); + p_image->set_pixel(w - 1 - i, j, transparent); + p_image->set_pixel(w - 1 - i, h - 1 - j, transparent); + p_image->set_pixel(i, h - 1 - j, transparent); + } else { + break; + } + } + } + + p_image->unlock(); +} + bool EditorTexturePreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Texture"); diff --git a/editor/plugins/editor_preview_plugins.h b/editor/plugins/editor_preview_plugins.h index 2e12515e30a..35b5c3a5f01 100644 --- a/editor/plugins/editor_preview_plugins.h +++ b/editor/plugins/editor_preview_plugins.h @@ -33,6 +33,8 @@ #include "editor/editor_resource_preview.h" +void post_process_preview(Ref p_image); + class EditorTexturePreviewPlugin : public EditorResourcePreviewGenerator { GDCLASS(EditorTexturePreviewPlugin, EditorResourcePreviewGenerator) public: