editor_preview_plugins.cpp 30 KB

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