Path: blob/master/editor/inspector/editor_preview_plugins.cpp
9902 views
/**************************************************************************/1/* editor_preview_plugins.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_preview_plugins.h"3132#include "core/config/project_settings.h"33#include "core/io/image.h"34#include "core/io/resource_loader.h"35#include "core/object/script_language.h"36#include "editor/file_system/editor_paths.h"37#include "editor/settings/editor_settings.h"38#include "editor/themes/editor_scale.h"39#include "scene/resources/atlas_texture.h"40#include "scene/resources/bit_map.h"41#include "scene/resources/font.h"42#include "scene/resources/gradient_texture.h"43#include "scene/resources/image_texture.h"44#include "scene/resources/material.h"45#include "scene/resources/mesh.h"46#include "servers/audio/audio_stream.h"4748void post_process_preview(Ref<Image> p_image) {49if (p_image->get_format() != Image::FORMAT_RGBA8) {50p_image->convert(Image::FORMAT_RGBA8);51}5253const int w = p_image->get_width();54const int h = p_image->get_height();5556const int r = MIN(w, h) / 32;57const int r2 = r * r;58Color transparent = Color(0, 0, 0, 0);5960for (int i = 0; i < r; i++) {61for (int j = 0; j < r; j++) {62int dx = i - r;63int dy = j - r;64if (dx * dx + dy * dy > r2) {65p_image->set_pixel(i, j, transparent);66p_image->set_pixel(w - 1 - i, j, transparent);67p_image->set_pixel(w - 1 - i, h - 1 - j, transparent);68p_image->set_pixel(i, h - 1 - j, transparent);69} else {70break;71}72}73}74}7576bool EditorTexturePreviewPlugin::handles(const String &p_type) const {77return ClassDB::is_parent_class(p_type, "Texture");78}7980bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const {81return true;82}8384Ref<Texture2D> EditorTexturePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {85Ref<Image> img;8687Ref<AtlasTexture> tex_atlas = p_from;88Ref<Texture3D> tex_3d = p_from;89Ref<TextureLayered> tex_lyr = p_from;9091if (tex_atlas.is_valid()) {92Ref<Texture2D> tex = tex_atlas->get_atlas();93if (tex.is_null()) {94return Ref<Texture2D>();95}9697Ref<Image> atlas = tex->get_image();98if (atlas.is_null()) {99return Ref<Texture2D>();100}101102if (atlas->is_compressed()) {103atlas = atlas->duplicate();104if (atlas->decompress() != OK) {105return Ref<Texture2D>();106}107}108109if (!tex_atlas->get_region().has_area()) {110return Ref<Texture2D>();111}112113img = atlas->get_region(tex_atlas->get_region());114115} else if (tex_3d.is_valid()) {116if (tex_3d->get_depth() == 0) {117return Ref<Texture2D>();118}119120Vector<Ref<Image>> data = tex_3d->get_data();121if (data.size() != tex_3d->get_depth()) {122return Ref<Texture2D>();123}124125// Use the middle slice for the thumbnail.126const int mid_depth = (tex_3d->get_depth() - 1) / 2;127if (!data.is_empty() && data[mid_depth].is_valid()) {128img = data[mid_depth]->duplicate();129}130131} else if (tex_lyr.is_valid()) {132if (tex_lyr->get_layers() == 0) {133return Ref<Texture2D>();134}135136// Use the middle slice for the thumbnail.137const int mid_layer = (tex_lyr->get_layers() - 1) / 2;138139Ref<Image> data = tex_lyr->get_layer_data(mid_layer);140if (data.is_valid()) {141img = data->duplicate();142}143144} else {145Ref<Texture2D> tex = p_from;146if (tex.is_valid()) {147img = tex->get_image();148if (img.is_valid()) {149img = img->duplicate();150}151}152}153154if (img.is_null() || img->is_empty()) {155return Ref<Texture2D>();156}157158p_metadata["dimensions"] = img->get_size();159160img->clear_mipmaps();161162if (img->is_compressed()) {163if (img->decompress() != OK) {164return Ref<Texture2D>();165}166} else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {167img->convert(Image::FORMAT_RGBA8);168}169170Vector2 new_size = img->get_size();171if (new_size.x > p_size.x) {172new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);173}174if (new_size.y > p_size.y) {175new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);176}177Vector2i new_size_i = Vector2i(new_size).maxi(1);178img->resize(new_size_i.x, new_size_i.y, Image::INTERPOLATE_CUBIC);179post_process_preview(img);180181return ImageTexture::create_from_image(img);182}183184////////////////////////////////////////////////////////////////////////////185186bool EditorImagePreviewPlugin::handles(const String &p_type) const {187return p_type == "Image";188}189190Ref<Texture2D> EditorImagePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {191Ref<Image> img = p_from;192193if (img.is_null() || img->is_empty()) {194return Ref<Image>();195}196197img = img->duplicate();198img->clear_mipmaps();199200if (img->is_compressed()) {201if (img->decompress() != OK) {202return Ref<Image>();203}204} else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {205img->convert(Image::FORMAT_RGBA8);206}207208Vector2 new_size = img->get_size();209if (new_size.x > p_size.x) {210new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);211}212if (new_size.y > p_size.y) {213new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);214}215img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);216post_process_preview(img);217218return ImageTexture::create_from_image(img);219}220221bool EditorImagePreviewPlugin::generate_small_preview_automatically() const {222return true;223}224225////////////////////////////////////////////////////////////////////////////226227bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {228return ClassDB::is_parent_class(p_type, "BitMap");229}230231Ref<Texture2D> EditorBitmapPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {232Ref<BitMap> bm = p_from;233234if (bm->get_size() == Size2()) {235return Ref<Texture2D>();236}237238Vector<uint8_t> data;239240data.resize(bm->get_size().width * bm->get_size().height);241242{243uint8_t *w = data.ptrw();244245for (int i = 0; i < bm->get_size().width; i++) {246for (int j = 0; j < bm->get_size().height; j++) {247if (bm->get_bit(i, j)) {248w[j * (int)bm->get_size().width + i] = 255;249} else {250w[j * (int)bm->get_size().width + i] = 0;251}252}253}254}255256Ref<Image> img = Image::create_from_data(bm->get_size().width, bm->get_size().height, false, Image::FORMAT_L8, data);257258if (img->is_compressed()) {259if (img->decompress() != OK) {260return Ref<Texture2D>();261}262} else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {263img->convert(Image::FORMAT_RGBA8);264}265266Vector2 new_size = img->get_size();267if (new_size.x > p_size.x) {268new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);269}270if (new_size.y > p_size.y) {271new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);272}273img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);274post_process_preview(img);275276return ImageTexture::create_from_image(img);277}278279bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {280return true;281}282283///////////////////////////////////////////////////////////////////////////284285bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {286return ClassDB::is_parent_class(p_type, "PackedScene");287}288289Ref<Texture2D> EditorPackedScenePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {290return generate_from_path(p_from->get_path(), p_size, p_metadata);291}292293Ref<Texture2D> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {294String temp_path = EditorPaths::get_singleton()->get_cache_dir();295String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();296cache_base = temp_path.path_join("resthumb-" + cache_base);297298//does not have it, try to load a cached thumbnail299300String path = cache_base + ".png";301302if (!FileAccess::exists(path)) {303return Ref<Texture2D>();304}305306Ref<Image> img;307img.instantiate();308Error err = img->load(path);309if (err == OK) {310post_process_preview(img);311return ImageTexture::create_from_image(img);312313} else {314return Ref<Texture2D>();315}316}317318//////////////////////////////////////////////////////////////////319320void EditorMaterialPreviewPlugin::abort() {321draw_requester.abort();322}323324bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {325return ClassDB::is_parent_class(p_type, "Material"); // Any material.326}327328bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const {329return true;330}331332Ref<Texture2D> EditorMaterialPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {333Ref<Material> material = p_from;334ERR_FAIL_COND_V(material.is_null(), Ref<Texture2D>());335336if (material->get_shader_mode() == Shader::MODE_SPATIAL) {337RS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());338339draw_requester.request_and_wait(viewport);340341Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);342RS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());343344ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());345346img->convert(Image::FORMAT_RGBA8);347int thumbnail_size = MAX(p_size.x, p_size.y);348img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC);349post_process_preview(img);350return ImageTexture::create_from_image(img);351}352353return Ref<Texture2D>();354}355356EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {357scenario = RS::get_singleton()->scenario_create();358359viewport = RS::get_singleton()->viewport_create();360RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);361RS::get_singleton()->viewport_set_scenario(viewport, scenario);362RS::get_singleton()->viewport_set_size(viewport, 128, 128);363RS::get_singleton()->viewport_set_transparent_background(viewport, true);364RS::get_singleton()->viewport_set_active(viewport, true);365viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);366367camera = RS::get_singleton()->camera_create();368RS::get_singleton()->viewport_attach_camera(viewport, camera);369RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));370RS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);371372if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {373camera_attributes = RS::get_singleton()->camera_attributes_create();374RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.375RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);376}377378light = RS::get_singleton()->directional_light_create();379light_instance = RS::get_singleton()->instance_create2(light, scenario);380RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));381382light2 = RS::get_singleton()->directional_light_create();383RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));384//RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));385386light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);387388RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));389390sphere = RS::get_singleton()->mesh_create();391sphere_instance = RS::get_singleton()->instance_create2(sphere, scenario);392393int lats = 32;394int lons = 32;395const double lat_step = Math::TAU / lats;396const double lon_step = Math::TAU / lons;397real_t radius = 1.0;398399Vector<Vector3> vertices;400Vector<Vector3> normals;401Vector<Vector2> uvs;402Vector<real_t> tangents;403Basis tt = Basis(Vector3(0, 1, 0), Math::PI * 0.5);404405for (int i = 1; i <= lats; i++) {406double lat0 = lat_step * (i - 1) - Math::TAU / 4;407double z0 = Math::sin(lat0);408double zr0 = Math::cos(lat0);409410double lat1 = lat_step * i - Math::TAU / 4;411double z1 = Math::sin(lat1);412double zr1 = Math::cos(lat1);413414for (int j = lons; j >= 1; j--) {415double lng0 = lon_step * (j - 1);416double x0 = Math::cos(lng0);417double y0 = Math::sin(lng0);418419double lng1 = lon_step * j;420double x1 = Math::cos(lng1);421double y1 = Math::sin(lng1);422423Vector3 v[4] = {424Vector3(x1 * zr0, z0, y1 * zr0),425Vector3(x1 * zr1, z1, y1 * zr1),426Vector3(x0 * zr1, z1, y0 * zr1),427Vector3(x0 * zr0, z0, y0 * zr0)428};429430#define ADD_POINT(m_idx) \431normals.push_back(v[m_idx]); \432vertices.push_back(v[m_idx] * radius); \433{ \434Vector2 uv(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \435uv /= Math::PI; \436uv *= 4.0; \437uv = uv * 0.5 + Vector2(0.5, 0.5); \438uvs.push_back(uv); \439} \440{ \441Vector3 t = tt.xform(v[m_idx]); \442tangents.push_back(t.x); \443tangents.push_back(t.y); \444tangents.push_back(t.z); \445tangents.push_back(1.0); \446}447448ADD_POINT(0);449ADD_POINT(1);450ADD_POINT(2);451452ADD_POINT(2);453ADD_POINT(3);454ADD_POINT(0);455}456}457458Array arr;459arr.resize(RS::ARRAY_MAX);460arr[RS::ARRAY_VERTEX] = vertices;461arr[RS::ARRAY_NORMAL] = normals;462arr[RS::ARRAY_TANGENT] = tangents;463arr[RS::ARRAY_TEX_UV] = uvs;464RS::get_singleton()->mesh_add_surface_from_arrays(sphere, RS::PRIMITIVE_TRIANGLES, arr);465}466467EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {468ERR_FAIL_NULL(RenderingServer::get_singleton());469RS::get_singleton()->free(sphere);470RS::get_singleton()->free(sphere_instance);471RS::get_singleton()->free(viewport);472RS::get_singleton()->free(light);473RS::get_singleton()->free(light_instance);474RS::get_singleton()->free(light2);475RS::get_singleton()->free(light_instance2);476RS::get_singleton()->free(camera);477RS::get_singleton()->free(camera_attributes);478RS::get_singleton()->free(scenario);479}480481///////////////////////////////////////////////////////////////////////////482483bool EditorScriptPreviewPlugin::handles(const String &p_type) const {484return ClassDB::is_parent_class(p_type, "Script");485}486487Ref<Texture2D> EditorScriptPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {488Error err;489String code = FileAccess::get_file_as_string(p_path, &err);490if (err != OK) {491return Ref<Texture2D>();492}493494ScriptLanguage *lang = ScriptServer::get_language_for_extension(p_path.get_extension());495return _generate_from_source_code(lang, code, p_size, p_metadata);496}497498Ref<Texture2D> EditorScriptPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {499Ref<Script> scr = p_from;500if (scr.is_null()) {501return Ref<Texture2D>();502}503504String code = scr->get_source_code().strip_edges();505return _generate_from_source_code(scr->get_language(), code, p_size, p_metadata);506}507508Ref<Texture2D> EditorScriptPreviewPlugin::_generate_from_source_code(const ScriptLanguage *p_language, const String &p_source_code, const Size2 &p_size, Dictionary &p_metadata) const {509if (p_source_code.is_empty()) {510return Ref<Texture2D>();511}512513HashSet<String> control_flow_keywords;514HashSet<String> keywords;515516if (p_language) {517for (const String &keyword : p_language->get_reserved_words()) {518if (p_language->is_control_flow_keyword(keyword)) {519control_flow_keywords.insert(keyword);520} else {521keywords.insert(keyword);522}523}524}525526int line = 0;527int col = 0;528int thumbnail_size = MAX(p_size.x, p_size.y);529Ref<Image> img = Image::create_empty(thumbnail_size, thumbnail_size, false, Image::FORMAT_RGBA8);530531Color bg_color = EDITOR_GET("text_editor/theme/highlighting/background_color");532Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");533Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");534Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");535Color symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");536Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");537Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");538539if (bg_color.a == 0) {540bg_color = Color(0, 0, 0, 0);541}542bg_color.a = MAX(bg_color.a, 0.2); // Ensure we have some background, regardless of the text editor setting.543544img->fill(bg_color);545546const int x0 = thumbnail_size / 8;547const int y0 = thumbnail_size / 8;548const int available_height = thumbnail_size - 2 * y0;549col = x0;550551bool prev_is_text = false;552bool in_control_flow_keyword = false;553bool in_keyword = false;554bool in_comment = false;555bool in_doc_comment = false;556for (int i = 0; i < p_source_code.length(); i++) {557char32_t c = p_source_code[i];558if (c > 32) {559if (col < thumbnail_size) {560Color color = text_color;561562if (c == '#') {563if (i < p_source_code.length() - 1 && p_source_code[i + 1] == '#') {564in_doc_comment = true;565} else {566in_comment = true;567}568}569570if (in_comment) {571color = comment_color;572} else if (in_doc_comment) {573color = doc_comment_color;574} else {575if (is_symbol(c)) {576// Make symbol a little visible.577color = symbol_color;578in_control_flow_keyword = false;579in_keyword = false;580} else if (!prev_is_text && is_ascii_identifier_char(c)) {581int pos = i;582583while (is_ascii_identifier_char(p_source_code[pos])) {584pos++;585}586String word = p_source_code.substr(i, pos - i);587if (control_flow_keywords.has(word)) {588in_control_flow_keyword = true;589} else if (keywords.has(word)) {590in_keyword = true;591}592593} else if (!is_ascii_identifier_char(c)) {594in_keyword = false;595}596597if (in_control_flow_keyword) {598color = control_flow_keyword_color;599} else if (in_keyword) {600color = keyword_color;601}602}603Color ul = color;604ul.a *= 0.5;605img->set_pixel(col, y0 + line * 2, bg_color.blend(ul));606img->set_pixel(col, y0 + line * 2 + 1, color);607608prev_is_text = is_ascii_identifier_char(c);609}610col++;611} else {612prev_is_text = false;613in_control_flow_keyword = false;614in_keyword = false;615616if (c == '\n') {617in_comment = false;618in_doc_comment = false;619620col = x0;621line++;622if (line >= available_height / 2) {623break;624}625} else if (c == '\t') {626col += 3;627} else {628col++;629}630}631}632post_process_preview(img);633return ImageTexture::create_from_image(img);634}635636///////////////////////////////////////////////////////////////////637638bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {639return ClassDB::is_parent_class(p_type, "AudioStream");640}641642Ref<Texture2D> EditorAudioStreamPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {643Ref<AudioStream> stream = p_from;644ERR_FAIL_COND_V(stream.is_null(), Ref<Texture2D>());645646Vector<uint8_t> img;647648int w = p_size.x;649int h = p_size.y;650img.resize(w * h * 3);651652uint8_t *imgdata = img.ptrw();653uint8_t *imgw = imgdata;654655Ref<AudioStreamPlayback> playback = stream->instantiate_playback();656ERR_FAIL_COND_V(playback.is_null(), Ref<Texture2D>());657658real_t len_s = stream->get_length();659if (len_s == 0) {660len_s = 60; //one minute audio if no length specified661}662int frame_length = AudioServer::get_singleton()->get_mix_rate() * len_s;663664Vector<AudioFrame> frames;665frames.resize(frame_length);666667playback->start();668playback->mix(frames.ptrw(), 1, frames.size());669playback->stop();670671for (int i = 0; i < w; i++) {672real_t max = -1000;673real_t min = 1000;674int from = uint64_t(i) * frame_length / w;675int to = (uint64_t(i) + 1) * frame_length / w;676to = MIN(to, frame_length);677from = MIN(from, frame_length - 1);678if (to == from) {679to = from + 1;680}681682for (int j = from; j < to; j++) {683max = MAX(max, frames[j].left);684max = MAX(max, frames[j].right);685686min = MIN(min, frames[j].left);687min = MIN(min, frames[j].right);688}689690int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;691int pto = CLAMP((max * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;692693for (int j = 0; j < h; j++) {694uint8_t *p = &imgw[(j * w + i) * 3];695if (j < pfrom || j > pto) {696p[0] = 100;697p[1] = 100;698p[2] = 100;699} else {700p[0] = 180;701p[1] = 180;702p[2] = 180;703}704}705}706707p_metadata["length"] = stream->get_length();708709//post_process_preview(img);710711Ref<Image> image = Image::create_from_data(w, h, false, Image::FORMAT_RGB8, img);712return ImageTexture::create_from_image(image);713}714715///////////////////////////////////////////////////////////////////////////716717void EditorMeshPreviewPlugin::abort() {718draw_requester.abort();719}720721bool EditorMeshPreviewPlugin::handles(const String &p_type) const {722return ClassDB::is_parent_class(p_type, "Mesh"); // Any mesh.723}724725Ref<Texture2D> EditorMeshPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {726Ref<Mesh> mesh = p_from;727ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture2D>());728729RS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());730731AABB aabb = mesh->get_aabb();732Vector3 ofs = aabb.get_center();733aabb.position -= ofs;734Transform3D xform;735xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math::PI * 0.125);736xform.basis = Basis().rotated(Vector3(1, 0, 0), Math::PI * 0.125) * xform.basis;737AABB rot_aabb = xform.xform(aabb);738real_t m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;739if (m == 0) {740return Ref<Texture2D>();741}742m = 1.0 / m;743m *= 0.5;744xform.basis.scale(Vector3(m, m, m));745xform.origin = -xform.basis.xform(ofs); //-ofs*m;746xform.origin.z -= rot_aabb.size.z * 2;747RS::get_singleton()->instance_set_transform(mesh_instance, xform);748749draw_requester.request_and_wait(viewport);750751Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);752ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());753754RS::get_singleton()->instance_set_base(mesh_instance, RID());755756img->convert(Image::FORMAT_RGBA8);757758Vector2 new_size = img->get_size();759if (new_size.x > p_size.x) {760new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);761}762if (new_size.y > p_size.y) {763new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);764}765img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);766post_process_preview(img);767768return ImageTexture::create_from_image(img);769}770771EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {772scenario = RS::get_singleton()->scenario_create();773774viewport = RS::get_singleton()->viewport_create();775RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);776RS::get_singleton()->viewport_set_scenario(viewport, scenario);777RS::get_singleton()->viewport_set_size(viewport, 128, 128);778RS::get_singleton()->viewport_set_transparent_background(viewport, true);779RS::get_singleton()->viewport_set_active(viewport, true);780viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);781782camera = RS::get_singleton()->camera_create();783RS::get_singleton()->viewport_attach_camera(viewport, camera);784RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));785//RS::get_singleton()->camera_set_perspective(camera,45,0.1,10);786RS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);787788if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {789camera_attributes = RS::get_singleton()->camera_attributes_create();790RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.791RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);792}793794light = RS::get_singleton()->directional_light_create();795light_instance = RS::get_singleton()->instance_create2(light, scenario);796RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));797798light2 = RS::get_singleton()->directional_light_create();799RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));800//RS::get_singleton()->light_set_color(light2, RS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0));801light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);802803RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));804805//sphere = RS::get_singleton()->mesh_create();806mesh_instance = RS::get_singleton()->instance_create();807RS::get_singleton()->instance_set_scenario(mesh_instance, scenario);808}809810EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {811ERR_FAIL_NULL(RenderingServer::get_singleton());812//RS::get_singleton()->free(sphere);813RS::get_singleton()->free(mesh_instance);814RS::get_singleton()->free(viewport);815RS::get_singleton()->free(light);816RS::get_singleton()->free(light_instance);817RS::get_singleton()->free(light2);818RS::get_singleton()->free(light_instance2);819RS::get_singleton()->free(camera);820RS::get_singleton()->free(camera_attributes);821RS::get_singleton()->free(scenario);822}823824///////////////////////////////////////////////////////////////////////////825826void EditorFontPreviewPlugin::abort() {827draw_requester.abort();828}829830bool EditorFontPreviewPlugin::handles(const String &p_type) const {831return ClassDB::is_parent_class(p_type, "Font");832}833834Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {835Ref<Font> sampled_font = ResourceLoader::load(p_path);836ERR_FAIL_COND_V(sampled_font.is_null(), Ref<Texture2D>());837838String sample;839static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ሀᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";840for (int i = 0; i < sample_base.length(); i++) {841if (sampled_font->has_char(sample_base[i])) {842sample += sample_base[i];843}844}845if (sample.is_empty()) {846sample = sampled_font->get_supported_chars().substr(0, 6);847}848Vector2 size = sampled_font->get_string_size(sample, HORIZONTAL_ALIGNMENT_LEFT, -1, 50);849850Vector2 pos;851852pos.x = 64 - size.x / 2;853pos.y = 80;854855const Color c = GLOBAL_GET("rendering/environment/defaults/default_clear_color");856const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;857sampled_font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(fg, fg, fg));858859draw_requester.request_and_wait(viewport);860861RS::get_singleton()->canvas_item_clear(canvas_item);862863Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);864ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());865866img->convert(Image::FORMAT_RGBA8);867868Vector2 new_size = img->get_size();869if (new_size.x > p_size.x) {870new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);871}872if (new_size.y > p_size.y) {873new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);874}875img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);876post_process_preview(img);877878return ImageTexture::create_from_image(img);879}880881Ref<Texture2D> EditorFontPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {882String path = p_from->get_path();883if (!FileAccess::exists(path)) {884return Ref<Texture2D>();885}886return generate_from_path(path, p_size, p_metadata);887}888889EditorFontPreviewPlugin::EditorFontPreviewPlugin() {890viewport = RS::get_singleton()->viewport_create();891RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);892RS::get_singleton()->viewport_set_size(viewport, 128, 128);893RS::get_singleton()->viewport_set_active(viewport, true);894viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);895896canvas = RS::get_singleton()->canvas_create();897canvas_item = RS::get_singleton()->canvas_item_create();898899RS::get_singleton()->viewport_attach_canvas(viewport, canvas);900RS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);901}902903EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {904ERR_FAIL_NULL(RenderingServer::get_singleton());905RS::get_singleton()->free(canvas_item);906RS::get_singleton()->free(canvas);907RS::get_singleton()->free(viewport);908}909910////////////////////////////////////////////////////////////////////////////911912static const real_t GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR = 4.0;913914bool EditorGradientPreviewPlugin::handles(const String &p_type) const {915return ClassDB::is_parent_class(p_type, "Gradient");916}917918bool EditorGradientPreviewPlugin::generate_small_preview_automatically() const {919return true;920}921922Ref<Texture2D> EditorGradientPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {923Ref<Gradient> gradient = p_from;924if (gradient.is_valid()) {925Ref<GradientTexture1D> ptex;926ptex.instantiate();927ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE);928ptex->set_gradient(gradient);929return ImageTexture::create_from_image(ptex->get_image());930}931return Ref<Texture2D>();932}933934935