/*************************************************************************/ /* editor_preview_plugins.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "editor_preview_plugins.h" #include "core/config/project_settings.h" #include "core/io/file_access_memory.h" #include "core/io/resource_loader.h" #include "core/os/os.h" #include "editor/editor_paths.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "scene/resources/bit_map.h" #include "scene/resources/font.h" #include "scene/resources/material.h" #include "scene/resources/mesh.h" #include "servers/audio/audio_stream.h" void post_process_preview(Ref p_image) { if (p_image->get_format() != Image::FORMAT_RGBA8) { p_image->convert(Image::FORMAT_RGBA8); } 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; } } } } bool EditorTexturePreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Texture2D"); } bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const { return true; } Ref EditorTexturePreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { Ref img; Ref atex = p_from; if (atex.is_valid()) { Ref tex = atex->get_atlas(); if (!tex.is_valid()) { return Ref(); } Ref atlas = tex->get_image(); if (!atlas.is_valid()) { return Ref(); } img = atlas->get_rect(atex->get_region()); } else { Ref tex = p_from; if (tex.is_valid()) { img = tex->get_image(); if (img.is_valid()) { img = img->duplicate(); } } } if (img.is_null() || img->is_empty()) { return Ref(); } img->clear_mipmaps(); if (img->is_compressed()) { if (img->decompress() != OK) { return Ref(); } } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) { img->convert(Image::FORMAT_RGBA8); } Vector2 new_size = img->get_size(); if (new_size.x > p_size.x) { new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x); } if (new_size.y > p_size.y) { new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y); } Vector2i new_size_i(MAX(1, (int)new_size.x), MAX(1, (int)new_size.y)); img->resize(new_size_i.x, new_size_i.y, Image::INTERPOLATE_CUBIC); post_process_preview(img); return ImageTexture::create_from_image(img); } EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() { } //////////////////////////////////////////////////////////////////////////// bool EditorImagePreviewPlugin::handles(const String &p_type) const { return p_type == "Image"; } Ref EditorImagePreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { Ref img = p_from; if (img.is_null() || img->is_empty()) { return Ref(); } img = img->duplicate(); img->clear_mipmaps(); if (img->is_compressed()) { if (img->decompress() != OK) { return Ref(); } } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) { img->convert(Image::FORMAT_RGBA8); } Vector2 new_size = img->get_size(); if (new_size.x > p_size.x) { new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x); } if (new_size.y > p_size.y) { new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y); } img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC); post_process_preview(img); return ImageTexture::create_from_image(img); } EditorImagePreviewPlugin::EditorImagePreviewPlugin() { } bool EditorImagePreviewPlugin::generate_small_preview_automatically() const { return true; } //////////////////////////////////////////////////////////////////////////// bool EditorBitmapPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "BitMap"); } Ref EditorBitmapPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { Ref bm = p_from; if (bm->get_size() == Size2()) { return Ref(); } Vector data; data.resize(bm->get_size().width * bm->get_size().height); { uint8_t *w = data.ptrw(); for (int i = 0; i < bm->get_size().width; i++) { for (int j = 0; j < bm->get_size().height; j++) { if (bm->get_bit(i, j)) { w[j * (int)bm->get_size().width + i] = 255; } else { w[j * (int)bm->get_size().width + i] = 0; } } } } Ref img = Image::create_from_data(bm->get_size().width, bm->get_size().height, false, Image::FORMAT_L8, data); if (img->is_compressed()) { if (img->decompress() != OK) { return Ref(); } } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) { img->convert(Image::FORMAT_RGBA8); } Vector2 new_size = img->get_size(); if (new_size.x > p_size.x) { new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x); } if (new_size.y > p_size.y) { new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y); } img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC); post_process_preview(img); return ImageTexture::create_from_image(img); } bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const { return true; } EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() { } /////////////////////////////////////////////////////////////////////////// bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "PackedScene"); } Ref EditorPackedScenePreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { return generate_from_path(p_from->get_path(), p_size); } Ref EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const { String temp_path = EditorPaths::get_singleton()->get_cache_dir(); String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text(); cache_base = temp_path.path_join("resthumb-" + cache_base); //does not have it, try to load a cached thumbnail String path = cache_base + ".png"; if (!FileAccess::exists(path)) { return Ref(); } Ref img; img.instantiate(); Error err = img->load(path); if (err == OK) { post_process_preview(img); return ImageTexture::create_from_image(img); } else { return Ref(); } } EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() { } ////////////////////////////////////////////////////////////////// void EditorMaterialPreviewPlugin::_generate_frame_started() { RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_ONCE); //once used for capture RS::get_singleton()->request_frame_drawn_callback(callable_mp(const_cast(this), &EditorMaterialPreviewPlugin::_preview_done)); } void EditorMaterialPreviewPlugin::_preview_done() { preview_done.post(); } bool EditorMaterialPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Material"); // Any material. } bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const { return true; } Ref EditorMaterialPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { Ref material = p_from; ERR_FAIL_COND_V(material.is_null(), Ref()); if (material->get_shader_mode() == Shader::MODE_SPATIAL) { RS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid()); RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast(this), &EditorMaterialPreviewPlugin::_generate_frame_started), Object::CONNECT_ONE_SHOT); preview_done.wait(); Ref img = RS::get_singleton()->texture_2d_get(viewport_texture); RS::get_singleton()->mesh_surface_set_material(sphere, 0, RID()); ERR_FAIL_COND_V(!img.is_valid(), Ref()); img->convert(Image::FORMAT_RGBA8); int thumbnail_size = MAX(p_size.x, p_size.y); img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC); post_process_preview(img); return ImageTexture::create_from_image(img); } return Ref(); } EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() { scenario = RS::get_singleton()->scenario_create(); viewport = RS::get_singleton()->viewport_create(); RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED); RS::get_singleton()->viewport_set_scenario(viewport, scenario); RS::get_singleton()->viewport_set_size(viewport, 128, 128); RS::get_singleton()->viewport_set_transparent_background(viewport, true); RS::get_singleton()->viewport_set_active(viewport, true); viewport_texture = RS::get_singleton()->viewport_get_texture(viewport); camera = RS::get_singleton()->camera_create(); RS::get_singleton()->viewport_attach_camera(viewport, camera); RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3))); RS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10); if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) { camera_attributes = RS::get_singleton()->camera_attributes_create(); RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds. RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes); } light = RS::get_singleton()->directional_light_create(); light_instance = RS::get_singleton()->instance_create2(light, scenario); RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0))); light2 = RS::get_singleton()->directional_light_create(); RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); //RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); light_instance2 = RS::get_singleton()->instance_create2(light2, scenario); RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1))); sphere = RS::get_singleton()->mesh_create(); sphere_instance = RS::get_singleton()->instance_create2(sphere, scenario); int lats = 32; int lons = 32; const double lat_step = Math_TAU / lats; const double lon_step = Math_TAU / lons; real_t radius = 1.0; Vector vertices; Vector normals; Vector uvs; Vector tangents; Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5); for (int i = 1; i <= lats; i++) { double lat0 = lat_step * (i - 1) - Math_TAU / 4; double z0 = Math::sin(lat0); double zr0 = Math::cos(lat0); double lat1 = lat_step * i - Math_TAU / 4; double z1 = Math::sin(lat1); double zr1 = Math::cos(lat1); for (int j = lons; j >= 1; j--) { double lng0 = lon_step * (j - 1); double x0 = Math::cos(lng0); double y0 = Math::sin(lng0); double lng1 = lon_step * j; double x1 = Math::cos(lng1); double y1 = Math::sin(lng1); Vector3 v[4] = { Vector3(x1 * zr0, z0, y1 * zr0), Vector3(x1 * zr1, z1, y1 * zr1), Vector3(x0 * zr1, z1, y0 * zr1), Vector3(x0 * zr0, z0, y0 * zr0) }; #define ADD_POINT(m_idx) \ normals.push_back(v[m_idx]); \ vertices.push_back(v[m_idx] * radius); \ { \ Vector2 uv(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \ uv /= Math_PI; \ uv *= 4.0; \ uv = uv * 0.5 + Vector2(0.5, 0.5); \ uvs.push_back(uv); \ } \ { \ Vector3 t = tt.xform(v[m_idx]); \ tangents.push_back(t.x); \ tangents.push_back(t.y); \ tangents.push_back(t.z); \ tangents.push_back(1.0); \ } ADD_POINT(0); ADD_POINT(1); ADD_POINT(2); ADD_POINT(2); ADD_POINT(3); ADD_POINT(0); } } Array arr; arr.resize(RS::ARRAY_MAX); arr[RS::ARRAY_VERTEX] = vertices; arr[RS::ARRAY_NORMAL] = normals; arr[RS::ARRAY_TANGENT] = tangents; arr[RS::ARRAY_TEX_UV] = uvs; RS::get_singleton()->mesh_add_surface_from_arrays(sphere, RS::PRIMITIVE_TRIANGLES, arr); } EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() { RS::get_singleton()->free(sphere); RS::get_singleton()->free(sphere_instance); RS::get_singleton()->free(viewport); RS::get_singleton()->free(light); RS::get_singleton()->free(light_instance); RS::get_singleton()->free(light2); RS::get_singleton()->free(light_instance2); RS::get_singleton()->free(camera); RS::get_singleton()->free(camera_attributes); RS::get_singleton()->free(scenario); } /////////////////////////////////////////////////////////////////////////// bool EditorScriptPreviewPlugin::handles(const String &p_type) const { return ClassDB::is_parent_class(p_type, "Script"); } Ref EditorScriptPreviewPlugin::generate(const Ref &p_from, const Size2 &p_size) const { Ref