editor_preview_plugins.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /**************************************************************************/
  2. /* editor_preview_plugins.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_preview_plugins.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/file_access_memory.h"
  33. #include "core/io/image.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/object/script_language.h"
  36. #include "core/os/os.h"
  37. #include "editor/editor_paths.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/themes/editor_scale.h"
  40. #include "scene/resources/atlas_texture.h"
  41. #include "scene/resources/bit_map.h"
  42. #include "scene/resources/font.h"
  43. #include "scene/resources/gradient_texture.h"
  44. #include "scene/resources/image_texture.h"
  45. #include "scene/resources/material.h"
  46. #include "scene/resources/mesh.h"
  47. #include "servers/audio/audio_stream.h"
  48. void post_process_preview(Ref<Image> p_image) {
  49. if (p_image->get_format() != Image::FORMAT_RGBA8) {
  50. p_image->convert(Image::FORMAT_RGBA8);
  51. }
  52. const int w = p_image->get_width();
  53. const int h = p_image->get_height();
  54. const int r = MIN(w, h) / 32;
  55. const int r2 = r * r;
  56. Color transparent = Color(0, 0, 0, 0);
  57. for (int i = 0; i < r; i++) {
  58. for (int j = 0; j < r; j++) {
  59. int dx = i - r;
  60. int dy = j - r;
  61. if (dx * dx + dy * dy > r2) {
  62. p_image->set_pixel(i, j, transparent);
  63. p_image->set_pixel(w - 1 - i, j, transparent);
  64. p_image->set_pixel(w - 1 - i, h - 1 - j, transparent);
  65. p_image->set_pixel(i, h - 1 - j, transparent);
  66. } else {
  67. break;
  68. }
  69. }
  70. }
  71. }
  72. bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
  73. return ClassDB::is_parent_class(p_type, "Texture2D") || ClassDB::is_parent_class(p_type, "Texture3D") || ClassDB::is_parent_class(p_type, "TextureLayered");
  74. }
  75. bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const {
  76. return true;
  77. }
  78. Ref<Texture2D> EditorTexturePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  79. Ref<Image> img;
  80. Ref<AtlasTexture> tex_atlas = p_from;
  81. Ref<Texture3D> tex_3d = p_from;
  82. Ref<TextureLayered> tex_lyr = p_from;
  83. if (tex_atlas.is_valid()) {
  84. Ref<Texture2D> tex = tex_atlas->get_atlas();
  85. if (!tex.is_valid()) {
  86. return Ref<Texture2D>();
  87. }
  88. Ref<Image> atlas = tex->get_image();
  89. if (!atlas.is_valid()) {
  90. return Ref<Texture2D>();
  91. }
  92. if (!tex_atlas->get_region().has_area()) {
  93. return Ref<Texture2D>();
  94. }
  95. img = atlas->get_region(tex_atlas->get_region());
  96. } else if (tex_3d.is_valid()) {
  97. if (tex_3d->get_depth() == 0) {
  98. return Ref<Texture2D>();
  99. }
  100. Vector<Ref<Image>> data = tex_3d->get_data();
  101. if (data.size() != tex_3d->get_depth()) {
  102. return Ref<Texture2D>();
  103. }
  104. // Use the middle slice for the thumbnail.
  105. const int mid_depth = (tex_3d->get_depth() - 1) / 2;
  106. if (!data.is_empty() && data[mid_depth].is_valid()) {
  107. img = data[mid_depth]->duplicate();
  108. }
  109. } else if (tex_lyr.is_valid()) {
  110. if (tex_lyr->get_layers() == 0) {
  111. return Ref<Texture2D>();
  112. }
  113. // Use the middle slice for the thumbnail.
  114. const int mid_layer = (tex_lyr->get_layers() - 1) / 2;
  115. Ref<Image> data = tex_lyr->get_layer_data(mid_layer);
  116. if (data.is_valid()) {
  117. img = data->duplicate();
  118. }
  119. } else {
  120. Ref<Texture2D> tex = p_from;
  121. if (tex.is_valid()) {
  122. img = tex->get_image();
  123. if (img.is_valid()) {
  124. img = img->duplicate();
  125. }
  126. }
  127. }
  128. if (img.is_null() || img->is_empty()) {
  129. return Ref<Texture2D>();
  130. }
  131. p_metadata["dimensions"] = img->get_size();
  132. img->clear_mipmaps();
  133. if (img->is_compressed()) {
  134. if (img->decompress() != OK) {
  135. return Ref<Texture2D>();
  136. }
  137. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  138. img->convert(Image::FORMAT_RGBA8);
  139. }
  140. Vector2 new_size = img->get_size();
  141. if (new_size.x > p_size.x) {
  142. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  143. }
  144. if (new_size.y > p_size.y) {
  145. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  146. }
  147. Vector2i new_size_i = Vector2i(new_size).maxi(1);
  148. img->resize(new_size_i.x, new_size_i.y, Image::INTERPOLATE_CUBIC);
  149. post_process_preview(img);
  150. return ImageTexture::create_from_image(img);
  151. }
  152. EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() {
  153. }
  154. ////////////////////////////////////////////////////////////////////////////
  155. bool EditorImagePreviewPlugin::handles(const String &p_type) const {
  156. return p_type == "Image";
  157. }
  158. Ref<Texture2D> EditorImagePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  159. Ref<Image> img = p_from;
  160. if (img.is_null() || img->is_empty()) {
  161. return Ref<Image>();
  162. }
  163. img = img->duplicate();
  164. img->clear_mipmaps();
  165. if (img->is_compressed()) {
  166. if (img->decompress() != OK) {
  167. return Ref<Image>();
  168. }
  169. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  170. img->convert(Image::FORMAT_RGBA8);
  171. }
  172. Vector2 new_size = img->get_size();
  173. if (new_size.x > p_size.x) {
  174. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  175. }
  176. if (new_size.y > p_size.y) {
  177. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  178. }
  179. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  180. post_process_preview(img);
  181. return ImageTexture::create_from_image(img);
  182. }
  183. EditorImagePreviewPlugin::EditorImagePreviewPlugin() {
  184. }
  185. bool EditorImagePreviewPlugin::generate_small_preview_automatically() const {
  186. return true;
  187. }
  188. ////////////////////////////////////////////////////////////////////////////
  189. bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {
  190. return ClassDB::is_parent_class(p_type, "BitMap");
  191. }
  192. Ref<Texture2D> EditorBitmapPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  193. Ref<BitMap> bm = p_from;
  194. if (bm->get_size() == Size2()) {
  195. return Ref<Texture2D>();
  196. }
  197. Vector<uint8_t> data;
  198. data.resize(bm->get_size().width * bm->get_size().height);
  199. {
  200. uint8_t *w = data.ptrw();
  201. for (int i = 0; i < bm->get_size().width; i++) {
  202. for (int j = 0; j < bm->get_size().height; j++) {
  203. if (bm->get_bit(i, j)) {
  204. w[j * (int)bm->get_size().width + i] = 255;
  205. } else {
  206. w[j * (int)bm->get_size().width + i] = 0;
  207. }
  208. }
  209. }
  210. }
  211. Ref<Image> img = Image::create_from_data(bm->get_size().width, bm->get_size().height, false, Image::FORMAT_L8, data);
  212. if (img->is_compressed()) {
  213. if (img->decompress() != OK) {
  214. return Ref<Texture2D>();
  215. }
  216. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  217. img->convert(Image::FORMAT_RGBA8);
  218. }
  219. Vector2 new_size = img->get_size();
  220. if (new_size.x > p_size.x) {
  221. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  222. }
  223. if (new_size.y > p_size.y) {
  224. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  225. }
  226. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  227. post_process_preview(img);
  228. return ImageTexture::create_from_image(img);
  229. }
  230. bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {
  231. return true;
  232. }
  233. EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() {
  234. }
  235. ///////////////////////////////////////////////////////////////////////////
  236. bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {
  237. return ClassDB::is_parent_class(p_type, "PackedScene");
  238. }
  239. Ref<Texture2D> EditorPackedScenePreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  240. return generate_from_path(p_from->get_path(), p_size, p_metadata);
  241. }
  242. Ref<Texture2D> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {
  243. String temp_path = EditorPaths::get_singleton()->get_cache_dir();
  244. String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();
  245. cache_base = temp_path.path_join("resthumb-" + cache_base);
  246. //does not have it, try to load a cached thumbnail
  247. String path = cache_base + ".png";
  248. if (!FileAccess::exists(path)) {
  249. return Ref<Texture2D>();
  250. }
  251. Ref<Image> img;
  252. img.instantiate();
  253. Error err = img->load(path);
  254. if (err == OK) {
  255. post_process_preview(img);
  256. return ImageTexture::create_from_image(img);
  257. } else {
  258. return Ref<Texture2D>();
  259. }
  260. }
  261. EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() {
  262. }
  263. //////////////////////////////////////////////////////////////////
  264. void EditorMaterialPreviewPlugin::abort() {
  265. draw_requester.abort();
  266. }
  267. bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
  268. return ClassDB::is_parent_class(p_type, "Material"); // Any material.
  269. }
  270. bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const {
  271. return true;
  272. }
  273. Ref<Texture2D> EditorMaterialPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  274. Ref<Material> material = p_from;
  275. ERR_FAIL_COND_V(material.is_null(), Ref<Texture2D>());
  276. if (material->get_shader_mode() == Shader::MODE_SPATIAL) {
  277. RS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());
  278. draw_requester.request_and_wait(viewport);
  279. Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
  280. RS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());
  281. ERR_FAIL_COND_V(!img.is_valid(), Ref<ImageTexture>());
  282. img->convert(Image::FORMAT_RGBA8);
  283. int thumbnail_size = MAX(p_size.x, p_size.y);
  284. img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC);
  285. post_process_preview(img);
  286. return ImageTexture::create_from_image(img);
  287. }
  288. return Ref<Texture2D>();
  289. }
  290. EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
  291. scenario = RS::get_singleton()->scenario_create();
  292. viewport = RS::get_singleton()->viewport_create();
  293. RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);
  294. RS::get_singleton()->viewport_set_scenario(viewport, scenario);
  295. RS::get_singleton()->viewport_set_size(viewport, 128, 128);
  296. RS::get_singleton()->viewport_set_transparent_background(viewport, true);
  297. RS::get_singleton()->viewport_set_active(viewport, true);
  298. viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
  299. camera = RS::get_singleton()->camera_create();
  300. RS::get_singleton()->viewport_attach_camera(viewport, camera);
  301. RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));
  302. RS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);
  303. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  304. camera_attributes = RS::get_singleton()->camera_attributes_create();
  305. RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.
  306. RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);
  307. }
  308. light = RS::get_singleton()->directional_light_create();
  309. light_instance = RS::get_singleton()->instance_create2(light, scenario);
  310. RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  311. light2 = RS::get_singleton()->directional_light_create();
  312. RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  313. //RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  314. light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);
  315. RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  316. sphere = RS::get_singleton()->mesh_create();
  317. sphere_instance = RS::get_singleton()->instance_create2(sphere, scenario);
  318. int lats = 32;
  319. int lons = 32;
  320. const double lat_step = Math_TAU / lats;
  321. const double lon_step = Math_TAU / lons;
  322. real_t radius = 1.0;
  323. Vector<Vector3> vertices;
  324. Vector<Vector3> normals;
  325. Vector<Vector2> uvs;
  326. Vector<real_t> tangents;
  327. Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5);
  328. for (int i = 1; i <= lats; i++) {
  329. double lat0 = lat_step * (i - 1) - Math_TAU / 4;
  330. double z0 = Math::sin(lat0);
  331. double zr0 = Math::cos(lat0);
  332. double lat1 = lat_step * i - Math_TAU / 4;
  333. double z1 = Math::sin(lat1);
  334. double zr1 = Math::cos(lat1);
  335. for (int j = lons; j >= 1; j--) {
  336. double lng0 = lon_step * (j - 1);
  337. double x0 = Math::cos(lng0);
  338. double y0 = Math::sin(lng0);
  339. double lng1 = lon_step * j;
  340. double x1 = Math::cos(lng1);
  341. double y1 = Math::sin(lng1);
  342. Vector3 v[4] = {
  343. Vector3(x1 * zr0, z0, y1 * zr0),
  344. Vector3(x1 * zr1, z1, y1 * zr1),
  345. Vector3(x0 * zr1, z1, y0 * zr1),
  346. Vector3(x0 * zr0, z0, y0 * zr0)
  347. };
  348. #define ADD_POINT(m_idx) \
  349. normals.push_back(v[m_idx]); \
  350. vertices.push_back(v[m_idx] * radius); \
  351. { \
  352. Vector2 uv(Math::atan2(v[m_idx].x, v[m_idx].z), Math::atan2(-v[m_idx].y, v[m_idx].z)); \
  353. uv /= Math_PI; \
  354. uv *= 4.0; \
  355. uv = uv * 0.5 + Vector2(0.5, 0.5); \
  356. uvs.push_back(uv); \
  357. } \
  358. { \
  359. Vector3 t = tt.xform(v[m_idx]); \
  360. tangents.push_back(t.x); \
  361. tangents.push_back(t.y); \
  362. tangents.push_back(t.z); \
  363. tangents.push_back(1.0); \
  364. }
  365. ADD_POINT(0);
  366. ADD_POINT(1);
  367. ADD_POINT(2);
  368. ADD_POINT(2);
  369. ADD_POINT(3);
  370. ADD_POINT(0);
  371. }
  372. }
  373. Array arr;
  374. arr.resize(RS::ARRAY_MAX);
  375. arr[RS::ARRAY_VERTEX] = vertices;
  376. arr[RS::ARRAY_NORMAL] = normals;
  377. arr[RS::ARRAY_TANGENT] = tangents;
  378. arr[RS::ARRAY_TEX_UV] = uvs;
  379. RS::get_singleton()->mesh_add_surface_from_arrays(sphere, RS::PRIMITIVE_TRIANGLES, arr);
  380. }
  381. EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {
  382. ERR_FAIL_NULL(RenderingServer::get_singleton());
  383. RS::get_singleton()->free(sphere);
  384. RS::get_singleton()->free(sphere_instance);
  385. RS::get_singleton()->free(viewport);
  386. RS::get_singleton()->free(light);
  387. RS::get_singleton()->free(light_instance);
  388. RS::get_singleton()->free(light2);
  389. RS::get_singleton()->free(light_instance2);
  390. RS::get_singleton()->free(camera);
  391. RS::get_singleton()->free(camera_attributes);
  392. RS::get_singleton()->free(scenario);
  393. }
  394. ///////////////////////////////////////////////////////////////////////////
  395. bool EditorScriptPreviewPlugin::handles(const String &p_type) const {
  396. return ClassDB::is_parent_class(p_type, "Script");
  397. }
  398. Ref<Texture2D> EditorScriptPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {
  399. Error err;
  400. String code = FileAccess::get_file_as_string(p_path, &err);
  401. if (err != OK) {
  402. return Ref<Texture2D>();
  403. }
  404. ScriptLanguage *lang = ScriptServer::get_language_for_extension(p_path.get_extension());
  405. return _generate_from_source_code(lang, code, p_size, p_metadata);
  406. }
  407. Ref<Texture2D> EditorScriptPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  408. Ref<Script> scr = p_from;
  409. if (scr.is_null()) {
  410. return Ref<Texture2D>();
  411. }
  412. String code = scr->get_source_code().strip_edges();
  413. return _generate_from_source_code(scr->get_language(), code, p_size, p_metadata);
  414. }
  415. Ref<Texture2D> EditorScriptPreviewPlugin::_generate_from_source_code(const ScriptLanguage *p_language, const String &p_source_code, const Size2 &p_size, Dictionary &p_metadata) const {
  416. if (p_source_code.is_empty()) {
  417. return Ref<Texture2D>();
  418. }
  419. List<String> kwors;
  420. if (p_language) {
  421. p_language->get_reserved_words(&kwors);
  422. }
  423. HashSet<String> control_flow_keywords;
  424. HashSet<String> keywords;
  425. for (const String &E : kwors) {
  426. if (p_language && p_language->is_control_flow_keyword(E)) {
  427. control_flow_keywords.insert(E);
  428. } else {
  429. keywords.insert(E);
  430. }
  431. }
  432. int line = 0;
  433. int col = 0;
  434. int thumbnail_size = MAX(p_size.x, p_size.y);
  435. Ref<Image> img = Image::create_empty(thumbnail_size, thumbnail_size, false, Image::FORMAT_RGBA8);
  436. Color bg_color = EDITOR_GET("text_editor/theme/highlighting/background_color");
  437. Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  438. Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  439. Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");
  440. Color symbol_color = EDITOR_GET("text_editor/theme/highlighting/symbol_color");
  441. Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  442. Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  443. if (bg_color.a == 0) {
  444. bg_color = Color(0, 0, 0, 0);
  445. }
  446. bg_color.a = MAX(bg_color.a, 0.2); // Ensure we have some background, regardless of the text editor setting.
  447. img->fill(bg_color);
  448. const int x0 = thumbnail_size / 8;
  449. const int y0 = thumbnail_size / 8;
  450. const int available_height = thumbnail_size - 2 * y0;
  451. col = x0;
  452. bool prev_is_text = false;
  453. bool in_control_flow_keyword = false;
  454. bool in_keyword = false;
  455. bool in_comment = false;
  456. bool in_doc_comment = false;
  457. for (int i = 0; i < p_source_code.length(); i++) {
  458. char32_t c = p_source_code[i];
  459. if (c > 32) {
  460. if (col < thumbnail_size) {
  461. Color color = text_color;
  462. if (c == '#') {
  463. if (i < p_source_code.length() - 1 && p_source_code[i + 1] == '#') {
  464. in_doc_comment = true;
  465. } else {
  466. in_comment = true;
  467. }
  468. }
  469. if (in_comment) {
  470. color = comment_color;
  471. } else if (in_doc_comment) {
  472. color = doc_comment_color;
  473. } else {
  474. if (is_symbol(c)) {
  475. // Make symbol a little visible.
  476. color = symbol_color;
  477. in_control_flow_keyword = false;
  478. in_keyword = false;
  479. } else if (!prev_is_text && is_ascii_identifier_char(c)) {
  480. int pos = i;
  481. while (is_ascii_identifier_char(p_source_code[pos])) {
  482. pos++;
  483. }
  484. String word = p_source_code.substr(i, pos - i);
  485. if (control_flow_keywords.has(word)) {
  486. in_control_flow_keyword = true;
  487. } else if (keywords.has(word)) {
  488. in_keyword = true;
  489. }
  490. } else if (!is_ascii_identifier_char(c)) {
  491. in_keyword = false;
  492. }
  493. if (in_control_flow_keyword) {
  494. color = control_flow_keyword_color;
  495. } else if (in_keyword) {
  496. color = keyword_color;
  497. }
  498. }
  499. Color ul = color;
  500. ul.a *= 0.5;
  501. img->set_pixel(col, y0 + line * 2, bg_color.blend(ul));
  502. img->set_pixel(col, y0 + line * 2 + 1, color);
  503. prev_is_text = is_ascii_identifier_char(c);
  504. }
  505. col++;
  506. } else {
  507. prev_is_text = false;
  508. in_control_flow_keyword = false;
  509. in_keyword = false;
  510. if (c == '\n') {
  511. in_comment = false;
  512. in_doc_comment = false;
  513. col = x0;
  514. line++;
  515. if (line >= available_height / 2) {
  516. break;
  517. }
  518. } else if (c == '\t') {
  519. col += 3;
  520. } else {
  521. col++;
  522. }
  523. }
  524. }
  525. post_process_preview(img);
  526. return ImageTexture::create_from_image(img);
  527. }
  528. EditorScriptPreviewPlugin::EditorScriptPreviewPlugin() {
  529. }
  530. ///////////////////////////////////////////////////////////////////
  531. bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {
  532. return ClassDB::is_parent_class(p_type, "AudioStream");
  533. }
  534. Ref<Texture2D> EditorAudioStreamPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  535. Ref<AudioStream> stream = p_from;
  536. ERR_FAIL_COND_V(stream.is_null(), Ref<Texture2D>());
  537. Vector<uint8_t> img;
  538. int w = p_size.x;
  539. int h = p_size.y;
  540. img.resize(w * h * 3);
  541. uint8_t *imgdata = img.ptrw();
  542. uint8_t *imgw = imgdata;
  543. Ref<AudioStreamPlayback> playback = stream->instantiate_playback();
  544. ERR_FAIL_COND_V(playback.is_null(), Ref<Texture2D>());
  545. real_t len_s = stream->get_length();
  546. if (len_s == 0) {
  547. len_s = 60; //one minute audio if no length specified
  548. }
  549. int frame_length = AudioServer::get_singleton()->get_mix_rate() * len_s;
  550. Vector<AudioFrame> frames;
  551. frames.resize(frame_length);
  552. playback->start();
  553. playback->mix(frames.ptrw(), 1, frames.size());
  554. playback->stop();
  555. for (int i = 0; i < w; i++) {
  556. real_t max = -1000;
  557. real_t min = 1000;
  558. int from = uint64_t(i) * frame_length / w;
  559. int to = (uint64_t(i) + 1) * frame_length / w;
  560. to = MIN(to, frame_length);
  561. from = MIN(from, frame_length - 1);
  562. if (to == from) {
  563. to = from + 1;
  564. }
  565. for (int j = from; j < to; j++) {
  566. max = MAX(max, frames[j].left);
  567. max = MAX(max, frames[j].right);
  568. min = MIN(min, frames[j].left);
  569. min = MIN(min, frames[j].right);
  570. }
  571. int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  572. int pto = CLAMP((max * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  573. for (int j = 0; j < h; j++) {
  574. uint8_t *p = &imgw[(j * w + i) * 3];
  575. if (j < pfrom || j > pto) {
  576. p[0] = 100;
  577. p[1] = 100;
  578. p[2] = 100;
  579. } else {
  580. p[0] = 180;
  581. p[1] = 180;
  582. p[2] = 180;
  583. }
  584. }
  585. }
  586. p_metadata["length"] = stream->get_length();
  587. //post_process_preview(img);
  588. Ref<Image> image = Image::create_from_data(w, h, false, Image::FORMAT_RGB8, img);
  589. return ImageTexture::create_from_image(image);
  590. }
  591. EditorAudioStreamPreviewPlugin::EditorAudioStreamPreviewPlugin() {
  592. }
  593. ///////////////////////////////////////////////////////////////////////////
  594. void EditorMeshPreviewPlugin::abort() {
  595. draw_requester.abort();
  596. }
  597. bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
  598. return ClassDB::is_parent_class(p_type, "Mesh"); // Any mesh.
  599. }
  600. Ref<Texture2D> EditorMeshPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  601. Ref<Mesh> mesh = p_from;
  602. ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture2D>());
  603. RS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());
  604. AABB aabb = mesh->get_aabb();
  605. Vector3 ofs = aabb.get_center();
  606. aabb.position -= ofs;
  607. Transform3D xform;
  608. xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.125);
  609. xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.125) * xform.basis;
  610. AABB rot_aabb = xform.xform(aabb);
  611. real_t m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
  612. if (m == 0) {
  613. return Ref<Texture2D>();
  614. }
  615. m = 1.0 / m;
  616. m *= 0.5;
  617. xform.basis.scale(Vector3(m, m, m));
  618. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  619. xform.origin.z -= rot_aabb.size.z * 2;
  620. RS::get_singleton()->instance_set_transform(mesh_instance, xform);
  621. draw_requester.request_and_wait(viewport);
  622. Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
  623. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  624. RS::get_singleton()->instance_set_base(mesh_instance, RID());
  625. img->convert(Image::FORMAT_RGBA8);
  626. Vector2 new_size = img->get_size();
  627. if (new_size.x > p_size.x) {
  628. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  629. }
  630. if (new_size.y > p_size.y) {
  631. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  632. }
  633. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  634. post_process_preview(img);
  635. return ImageTexture::create_from_image(img);
  636. }
  637. EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {
  638. scenario = RS::get_singleton()->scenario_create();
  639. viewport = RS::get_singleton()->viewport_create();
  640. RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);
  641. RS::get_singleton()->viewport_set_scenario(viewport, scenario);
  642. RS::get_singleton()->viewport_set_size(viewport, 128, 128);
  643. RS::get_singleton()->viewport_set_transparent_background(viewport, true);
  644. RS::get_singleton()->viewport_set_active(viewport, true);
  645. viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
  646. camera = RS::get_singleton()->camera_create();
  647. RS::get_singleton()->viewport_attach_camera(viewport, camera);
  648. RS::get_singleton()->camera_set_transform(camera, Transform3D(Basis(), Vector3(0, 0, 3)));
  649. //RS::get_singleton()->camera_set_perspective(camera,45,0.1,10);
  650. RS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);
  651. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  652. camera_attributes = RS::get_singleton()->camera_attributes_create();
  653. RS::get_singleton()->camera_attributes_set_exposure(camera_attributes, 1.0, 0.000032552); // Matches default CameraAttributesPhysical to work well with default DirectionalLight3Ds.
  654. RS::get_singleton()->camera_set_camera_attributes(camera, camera_attributes);
  655. }
  656. light = RS::get_singleton()->directional_light_create();
  657. light_instance = RS::get_singleton()->instance_create2(light, scenario);
  658. RS::get_singleton()->instance_set_transform(light_instance, Transform3D().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  659. light2 = RS::get_singleton()->directional_light_create();
  660. RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  661. //RS::get_singleton()->light_set_color(light2, RS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0));
  662. light_instance2 = RS::get_singleton()->instance_create2(light2, scenario);
  663. RS::get_singleton()->instance_set_transform(light_instance2, Transform3D().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  664. //sphere = RS::get_singleton()->mesh_create();
  665. mesh_instance = RS::get_singleton()->instance_create();
  666. RS::get_singleton()->instance_set_scenario(mesh_instance, scenario);
  667. }
  668. EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {
  669. ERR_FAIL_NULL(RenderingServer::get_singleton());
  670. //RS::get_singleton()->free(sphere);
  671. RS::get_singleton()->free(mesh_instance);
  672. RS::get_singleton()->free(viewport);
  673. RS::get_singleton()->free(light);
  674. RS::get_singleton()->free(light_instance);
  675. RS::get_singleton()->free(light2);
  676. RS::get_singleton()->free(light_instance2);
  677. RS::get_singleton()->free(camera);
  678. RS::get_singleton()->free(camera_attributes);
  679. RS::get_singleton()->free(scenario);
  680. }
  681. ///////////////////////////////////////////////////////////////////////////
  682. void EditorFontPreviewPlugin::abort() {
  683. draw_requester.abort();
  684. }
  685. bool EditorFontPreviewPlugin::handles(const String &p_type) const {
  686. return ClassDB::is_parent_class(p_type, "Font");
  687. }
  688. Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size, Dictionary &p_metadata) const {
  689. Ref<Font> sampled_font = ResourceLoader::load(p_path);
  690. ERR_FAIL_COND_V(sampled_font.is_null(), Ref<Texture2D>());
  691. String sample;
  692. static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ሀᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
  693. for (int i = 0; i < sample_base.length(); i++) {
  694. if (sampled_font->has_char(sample_base[i])) {
  695. sample += sample_base[i];
  696. }
  697. }
  698. if (sample.is_empty()) {
  699. sample = sampled_font->get_supported_chars().substr(0, 6);
  700. }
  701. Vector2 size = sampled_font->get_string_size(sample, HORIZONTAL_ALIGNMENT_LEFT, -1, 50);
  702. Vector2 pos;
  703. pos.x = 64 - size.x / 2;
  704. pos.y = 80;
  705. const Color c = GLOBAL_GET("rendering/environment/defaults/default_clear_color");
  706. const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;
  707. sampled_font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(fg, fg, fg));
  708. draw_requester.request_and_wait(viewport);
  709. RS::get_singleton()->canvas_item_clear(canvas_item);
  710. Ref<Image> img = RS::get_singleton()->texture_2d_get(viewport_texture);
  711. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  712. img->convert(Image::FORMAT_RGBA8);
  713. Vector2 new_size = img->get_size();
  714. if (new_size.x > p_size.x) {
  715. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  716. }
  717. if (new_size.y > p_size.y) {
  718. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  719. }
  720. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  721. post_process_preview(img);
  722. return ImageTexture::create_from_image(img);
  723. }
  724. Ref<Texture2D> EditorFontPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  725. String path = p_from->get_path();
  726. if (!FileAccess::exists(path)) {
  727. return Ref<Texture2D>();
  728. }
  729. return generate_from_path(path, p_size, p_metadata);
  730. }
  731. EditorFontPreviewPlugin::EditorFontPreviewPlugin() {
  732. viewport = RS::get_singleton()->viewport_create();
  733. RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_DISABLED);
  734. RS::get_singleton()->viewport_set_size(viewport, 128, 128);
  735. RS::get_singleton()->viewport_set_active(viewport, true);
  736. viewport_texture = RS::get_singleton()->viewport_get_texture(viewport);
  737. canvas = RS::get_singleton()->canvas_create();
  738. canvas_item = RS::get_singleton()->canvas_item_create();
  739. RS::get_singleton()->viewport_attach_canvas(viewport, canvas);
  740. RS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  741. }
  742. EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {
  743. ERR_FAIL_NULL(RenderingServer::get_singleton());
  744. RS::get_singleton()->free(canvas_item);
  745. RS::get_singleton()->free(canvas);
  746. RS::get_singleton()->free(viewport);
  747. }
  748. ////////////////////////////////////////////////////////////////////////////
  749. static const real_t GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR = 4.0;
  750. bool EditorGradientPreviewPlugin::handles(const String &p_type) const {
  751. return ClassDB::is_parent_class(p_type, "Gradient");
  752. }
  753. bool EditorGradientPreviewPlugin::generate_small_preview_automatically() const {
  754. return true;
  755. }
  756. Ref<Texture2D> EditorGradientPreviewPlugin::generate(const Ref<Resource> &p_from, const Size2 &p_size, Dictionary &p_metadata) const {
  757. Ref<Gradient> gradient = p_from;
  758. if (gradient.is_valid()) {
  759. Ref<GradientTexture1D> ptex;
  760. ptex.instantiate();
  761. ptex->set_width(p_size.width * GRADIENT_PREVIEW_TEXTURE_SCALE_FACTOR * EDSCALE);
  762. ptex->set_gradient(gradient);
  763. return ImageTexture::create_from_image(ptex->get_image());
  764. }
  765. return Ref<Texture2D>();
  766. }
  767. EditorGradientPreviewPlugin::EditorGradientPreviewPlugin() {
  768. }