editor_preview_plugins.cpp 31 KB

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