Path: blob/master/editor/inspector/editor_preview_plugins.cpp
20841 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<Texture2D>();195}196197img = img->duplicate();198img->clear_mipmaps();199200if (img->is_compressed()) {201if (img->decompress() != OK) {202return Ref<Texture2D>();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;233if (bm.is_null()) {234return Ref<Texture2D>();235}236237if (bm->get_size() == Size2()) {238return Ref<Texture2D>();239}240241Vector<uint8_t> data;242243data.resize(bm->get_size().width * bm->get_size().height);244245{246uint8_t *w = data.ptrw();247248for (int i = 0; i < bm->get_size().width; i++) {249for (int j = 0; j < bm->get_size().height; j++) {250if (bm->get_bit(i, j)) {251w[j * (int)bm->get_size().width + i] = 255;252} else {253w[j * (int)bm->get_size().width + i] = 0;254}255}256}257}258259Ref<Image> img = Image::create_from_data(bm->get_size().width, bm->get_size().height, false, Image::FORMAT_L8, data);260261if (img->is_compressed()) {262if (img->decompress() != OK) {263return Ref<Texture2D>();264}265} else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {266img->convert(Image::FORMAT_RGBA8);267}268269Vector2 new_size = img->get_size();270if (new_size.x > p_size.x) {271new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);272}273if (new_size.y > p_size.y) {274new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);275}276img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);277post_process_preview(img);278279return ImageTexture::create_from_image(img);280}281282bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {283return true;284}285286///////////////////////////////////////////////////////////////////////////287288bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {289return ClassDB::is_parent_class(p_type, "PackedScene");290}291292Ref<Texture2D> EditorPackedScenePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {293return generate_from_path(p_from->get_path(), p_size, p_metadata);294}295296Ref<Texture2D> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {297String temp_path = EditorPaths::get_singleton()->get_cache_dir();298String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();299cache_base = temp_path.path_join("resthumb-" + cache_base);300301//does not have it, try to load a cached thumbnail302303String path = cache_base + ".png";304305if (!FileAccess::exists(path)) {306return Ref<Texture2D>();307}308309Ref<Image> img;310img.instantiate();311Error err = img->load(path);312if (err == OK) {313post_process_preview(img);314return ImageTexture::create_from_image(img);315316} else {317return Ref<Texture2D>();318}319}320321//////////////////////////////////////////////////////////////////322323void EditorMaterialPreviewPlugin::abort() {324draw_requester.abort();325}326327bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {328return ClassDB::is_parent_class(p_type, "Material"); // Any material.329}330331bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const {332return true;333}334335Ref<Texture2D> EditorMaterialPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {336Ref<Material> material = p_from;337ERR_FAIL_COND_V(material.is_null(), Ref<Texture2D>());338339if (material->get_shader_mode() == Shader::MODE_SPATIAL) {340RS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());341342draw_requester.request_and_wait(viewport);343344Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);345RS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());346347ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());348349img->convert(Image::FORMAT_RGBA8);350int thumbnail_size = MAX(p_size.x, p_size.y);351img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC);352post_process_preview(img);353return ImageTexture::create_from_image(img);354}355356return Ref<Texture2D>();357}358359EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {360scenario = RS::get_singleton()->scenario_create();361362viewport = RS::get_singleton()->viewport_create();363RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);364RS::get_singleton()->viewport_set_scenario(viewport, scenario);365RS::get_singleton()->viewport_set_size(viewport, 128, 128);366RS::get_singleton()->viewport_set_transparent_background(viewport, true);367RS::get_singleton()->viewport_set_active(viewport, true);368viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);369370camera = RS::get_singleton()->camera_create();371RS::get_singleton()->viewport_attach_camera(viewport, camera);372RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));373RS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);374375if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {376camera_attributes = RS::get_singleton()->camera_attributes_create();377RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.378RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);379}380381light = RS::get_singleton()->directional_light_create();382light_instance = RS::get_singleton()->instance_create2(light, scenario);383RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));384385light2 = RS::get_singleton()->directional_light_create();386RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));387//RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));388389light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);390391RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));392393sphere = RS::get_singleton()->mesh_create();394sphere_instance = RS::get_singleton()->instance_create2(sphere, scenario);395396int lats = 32;397int lons = 32;398const double lat_step = Math::PI / lats;399const double lon_step = Math::TAU / lons;400real_t radius = 1.0;401402Vector<Vector3> vertices;403Vector<Vector3> normals;404Vector<Vector2> uvs;405Vector<real_t> tangents;406Basis tt = Basis(Vector3(0, 1, 0), Math::PI * 0.5);407408for (int i = 1; i <= lats; i++) {409double lat0 = lat_step * (i - 1) - Math::TAU / 4;410double z0 = Math::sin(lat0);411double zr0 = Math::cos(lat0);412413double lat1 = lat_step * i - Math::TAU / 4;414double z1 = Math::sin(lat1);415double zr1 = Math::cos(lat1);416417for (int j = lons; j >= 1; j--) {418double lng0 = lon_step * (j - 1);419double x0 = Math::cos(lng0);420double y0 = Math::sin(lng0);421422double lng1 = lon_step * j;423double x1 = Math::cos(lng1);424double y1 = Math::sin(lng1);425426Vector3 v[4] = {427Vector3(x1 * zr0, z0, y1 * zr0),428Vector3(x1 * zr1, z1, y1 * zr1),429Vector3(x0 * zr1, z1, y0 * zr1),430Vector3(x0 * zr0, z0, y0 * zr0)431};432433#define ADD_POINT(m_idx) \434normals.push_back(v[m_idx]); \435vertices.push_back(v[m_idx] * radius); \436{ \437Vector2 uv; \438if (j >= lons / 2) { \439uv = Vector2(Math::atan2(-v[m_idx].x, -v[m_idx].z), Math::atan2(v[m_idx].y, -v[m_idx].z)); \440} else { \441uv = Vector2(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \442} \443uv /= Math::PI; \444uv *= 4.0; \445uv = uv * 0.5 + Vector2(0.5, 0.5); \446uvs.push_back(uv); \447} \448{ \449Vector3 t = tt.xform(v[m_idx]); \450tangents.push_back(t.x); \451tangents.push_back(t.y); \452tangents.push_back(t.z); \453tangents.push_back(1.0); \454}455456ADD_POINT(0);457ADD_POINT(1);458ADD_POINT(2);459460ADD_POINT(2);461ADD_POINT(3);462ADD_POINT(0);463}464}465466Array arr;467arr.resize(RS::ARRAY_MAX);468arr[RS::ARRAY_VERTEX] = vertices;469arr[RS::ARRAY_NORMAL] = normals;470arr[RS::ARRAY_TANGENT] = tangents;471arr[RS::ARRAY_TEX_UV] = uvs;472RS::get_singleton()->mesh_add_surface_from_arrays(sphere, RS::PRIMITIVE_TRIANGLES, arr);473}474475EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {476ERR_FAIL_NULL(RenderingServer::get_singleton());477RS::get_singleton()->free_rid(sphere);478RS::get_singleton()->free_rid(sphere_instance);479RS::get_singleton()->free_rid(viewport);480RS::get_singleton()->free_rid(light);481RS::get_singleton()->free_rid(light_instance);482RS::get_singleton()->free_rid(light2);483RS::get_singleton()->free_rid(light_instance2);484RS::get_singleton()->free_rid(camera);485RS::get_singleton()->free_rid(camera_attributes);486RS::get_singleton()->free_rid(scenario);487}488489///////////////////////////////////////////////////////////////////////////490491bool EditorScriptPreviewPlugin::handles(const String &p_type) const {492return ClassDB::is_parent_class(p_type, "Script");493}494495Ref<Texture2D> EditorScriptPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {496Error err;497String code = FileAccess::get_file_as_string(p_path, &err);498if (err != OK) {499return Ref<Texture2D>();500}501502ScriptLanguage *lang = ScriptServer::get_language_for_extension(p_path.get_extension());503return _generate_from_source_code(lang, code, p_size, p_metadata);504}505506Ref<Texture2D> EditorScriptPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {507Ref<Script> scr = p_from;508if (scr.is_null()) {509return Ref<Texture2D>();510}511512String code = scr->get_source_code().strip_edges();513return _generate_from_source_code(scr->get_language(), code, p_size, p_metadata);514}515516Ref<Texture2D> EditorScriptPreviewPlugin::_generate_from_source_code(const ScriptLanguage *p_language, const String &p_source_code, const Size2 &p_size, Dictionary &p_metadata) const {517if (p_source_code.is_empty()) {518return Ref<Texture2D>();519}520521HashSet<String> control_flow_keywords;522HashSet<String> keywords;523524if (p_language) {525for (const String &keyword : p_language->get_reserved_words()) {526if (p_language->is_control_flow_keyword(keyword)) {527control_flow_keywords.insert(keyword);528} else {529keywords.insert(keyword);530}531}532}533534int line = 0;535int col = 0;536int thumbnail_size = MAX(p_size.x, p_size.y);537Ref<Image> img = Image::create_empty(thumbnail_size, thumbnail_size, false, Image::FORMAT_RGBA8);538539Color bg_color = EDITOR_GET("text_editor/theme/highlighting/background_color");540Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");541Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");542Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");543Color symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");544Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");545Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");546547if (bg_color.a == 0) {548bg_color = Color(0, 0, 0, 0);549}550bg_color.a = MAX(bg_color.a, 0.2); // Ensure we have some background, regardless of the text editor setting.551552img->fill(bg_color);553554const int x0 = thumbnail_size / 8;555const int y0 = thumbnail_size / 8;556const int available_height = thumbnail_size - 2 * y0;557col = x0;558559bool prev_is_text = false;560bool in_control_flow_keyword = false;561bool in_keyword = false;562bool in_comment = false;563bool in_doc_comment = false;564for (int i = 0; i < p_source_code.length(); i++) {565char32_t c = p_source_code[i];566if (c > 32) {567if (col < thumbnail_size) {568Color color = text_color;569570if (c == '#') {571if (i < p_source_code.length() - 1 && p_source_code[i + 1] == '#') {572in_doc_comment = true;573} else {574in_comment = true;575}576}577578if (in_comment) {579color = comment_color;580} else if (in_doc_comment) {581color = doc_comment_color;582} else {583if (is_symbol(c)) {584// Make symbol a little visible.585color = symbol_color;586in_control_flow_keyword = false;587in_keyword = false;588} else if (!prev_is_text && is_ascii_identifier_char(c)) {589int pos = i;590591while (is_ascii_identifier_char(p_source_code[pos])) {592pos++;593}594String word = p_source_code.substr(i, pos - i);595if (control_flow_keywords.has(word)) {596in_control_flow_keyword = true;597} else if (keywords.has(word)) {598in_keyword = true;599}600601} else if (!is_ascii_identifier_char(c)) {602in_keyword = false;603}604605if (in_control_flow_keyword) {606color = control_flow_keyword_color;607} else if (in_keyword) {608color = keyword_color;609}610}611Color ul = color;612ul.a *= 0.5;613img->set_pixel(col, y0 + line * 2, bg_color.blend(ul));614img->set_pixel(col, y0 + line * 2 + 1, color);615616prev_is_text = is_ascii_identifier_char(c);617}618col++;619} else {620prev_is_text = false;621in_control_flow_keyword = false;622in_keyword = false;623624if (c == '\n') {625in_comment = false;626in_doc_comment = false;627628col = x0;629line++;630if (line >= available_height / 2) {631break;632}633} else if (c == '\t') {634col += 3;635} else {636col++;637}638}639}640post_process_preview(img);641return ImageTexture::create_from_image(img);642}643644///////////////////////////////////////////////////////////////////645646bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {647return ClassDB::is_parent_class(p_type, "AudioStream");648}649650Ref<Texture2D> EditorAudioStreamPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {651Ref<AudioStream> stream = p_from;652ERR_FAIL_COND_V(stream.is_null(), Ref<Texture2D>());653654Vector<uint8_t> img;655656int w = p_size.x;657int h = p_size.y;658img.resize(w * h * 3);659660uint8_t *imgdata = img.ptrw();661uint8_t *imgw = imgdata;662663Ref<AudioStreamPlayback> playback = stream->instantiate_playback();664ERR_FAIL_COND_V(playback.is_null(), Ref<Texture2D>());665666real_t len_s = stream->get_length();667if (len_s == 0) {668len_s = 60; //one minute audio if no length specified669}670int frame_length = AudioServer::get_singleton()->get_mix_rate() * len_s;671672Vector<AudioFrame> frames;673frames.resize(frame_length);674675playback->start();676playback->mix(frames.ptrw(), 1, frames.size());677playback->stop();678679for (int i = 0; i < w; i++) {680real_t max = -1000;681real_t min = 1000;682int from = uint64_t(i) * frame_length / w;683int to = (uint64_t(i) + 1) * frame_length / w;684to = MIN(to, frame_length);685from = MIN(from, frame_length - 1);686if (to == from) {687to = from + 1;688}689690for (int j = from; j < to; j++) {691max = MAX(max, frames[j].left);692max = MAX(max, frames[j].right);693694min = MIN(min, frames[j].left);695min = MIN(min, frames[j].right);696}697698int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;699int pto = CLAMP((max * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;700701for (int j = 0; j < h; j++) {702uint8_t *p = &imgw[(j * w + i) * 3];703if (j < pfrom || j > pto) {704p[0] = 100;705p[1] = 100;706p[2] = 100;707} else {708p[0] = 180;709p[1] = 180;710p[2] = 180;711}712}713}714715p_metadata["length"] = stream->get_length();716717//post_process_preview(img);718719Ref<Image> image = Image::create_from_data(w, h, false, Image::FORMAT_RGB8, img);720return ImageTexture::create_from_image(image);721}722723///////////////////////////////////////////////////////////////////////////724725void EditorMeshPreviewPlugin::abort() {726draw_requester.abort();727}728729bool EditorMeshPreviewPlugin::handles(const String &p_type) const {730return ClassDB::is_parent_class(p_type, "Mesh"); // Any mesh.731}732733Ref<Texture2D> EditorMeshPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {734Ref<Mesh> mesh = p_from;735ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture2D>());736737RS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());738739AABB aabb = mesh->get_aabb();740Vector3 ofs = aabb.get_center();741aabb.position -= ofs;742Transform3D xform;743xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math::PI * 0.125);744xform.basis = Basis().rotated(Vector3(1, 0, 0), Math::PI * 0.125) * xform.basis;745AABB rot_aabb = xform.xform(aabb);746real_t m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;747if (m == 0) {748return Ref<Texture2D>();749}750m = 1.0 / m;751m *= 0.5;752xform.basis.scale(Vector3(m, m, m));753xform.origin = -xform.basis.xform(ofs); //-ofs*m;754xform.origin.z -= rot_aabb.size.z * 2;755RS::get_singleton()->instance_set_transform(mesh_instance, xform);756757draw_requester.request_and_wait(viewport);758759Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);760ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());761762RS::get_singleton()->instance_set_base(mesh_instance, RID());763764img->convert(Image::FORMAT_RGBA8);765766Vector2 new_size = img->get_size();767if (new_size.x > p_size.x) {768new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);769}770if (new_size.y > p_size.y) {771new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);772}773img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);774post_process_preview(img);775776return ImageTexture::create_from_image(img);777}778779EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {780scenario = RS::get_singleton()->scenario_create();781782viewport = RS::get_singleton()->viewport_create();783RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);784RS::get_singleton()->viewport_set_scenario(viewport, scenario);785RS::get_singleton()->viewport_set_size(viewport, 128, 128);786RS::get_singleton()->viewport_set_transparent_background(viewport, true);787RS::get_singleton()->viewport_set_active(viewport, true);788viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);789790camera = RS::get_singleton()->camera_create();791RS::get_singleton()->viewport_attach_camera(viewport, camera);792RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));793//RS::get_singleton()->camera_set_perspective(camera,45,0.1,10);794RS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);795796if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {797camera_attributes = RS::get_singleton()->camera_attributes_create();798RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.799RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);800}801802light = RS::get_singleton()->directional_light_create();803light_instance = RS::get_singleton()->instance_create2(light, scenario);804RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));805806light2 = RS::get_singleton()->directional_light_create();807RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));808//RS::get_singleton()->light_set_color(light2, RS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0));809light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);810811RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));812813//sphere = RS::get_singleton()->mesh_create();814mesh_instance = RS::get_singleton()->instance_create();815RS::get_singleton()->instance_set_scenario(mesh_instance, scenario);816}817818EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {819ERR_FAIL_NULL(RenderingServer::get_singleton());820//RS::get_singleton()->free(sphere);821RS::get_singleton()->free_rid(mesh_instance);822RS::get_singleton()->free_rid(viewport);823RS::get_singleton()->free_rid(light);824RS::get_singleton()->free_rid(light_instance);825RS::get_singleton()->free_rid(light2);826RS::get_singleton()->free_rid(light_instance2);827RS::get_singleton()->free_rid(camera);828RS::get_singleton()->free_rid(camera_attributes);829RS::get_singleton()->free_rid(scenario);830}831832///////////////////////////////////////////////////////////////////////////833834void EditorFontPreviewPlugin::abort() {835draw_requester.abort();836}837838bool EditorFontPreviewPlugin::handles(const String &p_type) const {839return ClassDB::is_parent_class(p_type, "Font");840}841842Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {843Ref<Font> sampled_font = ResourceLoader::load(p_path);844ERR_FAIL_COND_V(sampled_font.is_null(), Ref<Texture2D>());845846String sample;847static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ሀᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";848for (int i = 0; i < sample_base.length(); i++) {849if (sampled_font->has_char(sample_base[i])) {850sample += sample_base[i];851}852}853if (sample.is_empty()) {854sample = sampled_font->get_supported_chars().substr(0, 6);855}856Vector2 size = sampled_font->get_string_size(sample, HORIZONTAL_ALIGNMENT_LEFT, -1, 50);857858Vector2 pos;859860pos.x = 64 - size.x / 2;861pos.y = 80;862863const Color c = GLOBAL_GET("rendering/environment/defaults/default_clear_color");864const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;865sampled_font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(fg, fg, fg));866867draw_requester.request_and_wait(viewport);868869RS::get_singleton()->canvas_item_clear(canvas_item);870871Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);872ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());873874img->convert(Image::FORMAT_RGBA8);875876Vector2 new_size = img->get_size();877if (new_size.x > p_size.x) {878new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);879}880if (new_size.y > p_size.y) {881new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);882}883img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);884post_process_preview(img);885886return ImageTexture::create_from_image(img);887}888889Ref<Texture2D> EditorFontPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {890String path = p_from->get_path();891if (!FileAccess::exists(path)) {892return Ref<Texture2D>();893}894return generate_from_path(path, p_size, p_metadata);895}896897EditorFontPreviewPlugin::EditorFontPreviewPlugin() {898viewport = RS::get_singleton()->viewport_create();899RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);900RS::get_singleton()->viewport_set_size(viewport, 128, 128);901RS::get_singleton()->viewport_set_active(viewport, true);902viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);903904canvas = RS::get_singleton()->canvas_create();905canvas_item = RS::get_singleton()->canvas_item_create();906907RS::get_singleton()->viewport_attach_canvas(viewport, canvas);908RS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);909}910911EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {912ERR_FAIL_NULL(RenderingServer::get_singleton());913RS::get_singleton()->free_rid(canvas_item);914RS::get_singleton()->free_rid(canvas);915RS::get_singleton()->free_rid(viewport);916}917918////////////////////////////////////////////////////////////////////////////919920static const real_t GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR = 4.0;921922bool EditorGradientPreviewPlugin::handles(const String &p_type) const {923return ClassDB::is_parent_class(p_type, "Gradient");924}925926bool EditorGradientPreviewPlugin::generate_small_preview_automatically() const {927return true;928}929930Ref<Texture2D> EditorGradientPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {931Ref<Gradient> gradient = p_from;932if (gradient.is_valid()) {933Ref<GradientTexture1D> ptex;934ptex.instantiate();935ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE);936ptex->set_gradient(gradient);937return ImageTexture::create_from_image(ptex->get_image());938}939return Ref<Texture2D>();940}941942943