editor_preview_plugins.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/io/file_access_memory.h"
  32. #include "core/io/resource_loader.h"
  33. #include "core/os/os.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "scene/resources/bit_map.h"
  38. #include "scene/resources/dynamic_font.h"
  39. #include "scene/resources/material.h"
  40. #include "scene/resources/mesh.h"
  41. #include "servers/audio/audio_stream.h"
  42. void post_process_preview(Ref<Image> p_image) {
  43. if (p_image->get_format() != Image::FORMAT_RGBA8)
  44. p_image->convert(Image::FORMAT_RGBA8);
  45. p_image->lock();
  46. const int w = p_image->get_width();
  47. const int h = p_image->get_height();
  48. const int r = MIN(w, h) / 32;
  49. const int r2 = r * r;
  50. Color transparent = Color(0, 0, 0, 0);
  51. for (int i = 0; i < r; i++) {
  52. for (int j = 0; j < r; j++) {
  53. int dx = i - r;
  54. int dy = j - r;
  55. if (dx * dx + dy * dy > r2) {
  56. p_image->set_pixel(i, j, transparent);
  57. p_image->set_pixel(w - 1 - i, j, transparent);
  58. p_image->set_pixel(w - 1 - i, h - 1 - j, transparent);
  59. p_image->set_pixel(i, h - 1 - j, transparent);
  60. } else {
  61. break;
  62. }
  63. }
  64. }
  65. p_image->unlock();
  66. }
  67. bool EditorTexturePreviewPlugin::handles(const String &p_type) const {
  68. return ClassDB::is_parent_class(p_type, "Texture");
  69. }
  70. bool EditorTexturePreviewPlugin::generate_small_preview_automatically() const {
  71. return true;
  72. }
  73. Ref<Texture> EditorTexturePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  74. Ref<Image> img;
  75. Ref<AtlasTexture> atex = p_from;
  76. Ref<LargeTexture> ltex = p_from;
  77. if (atex.is_valid()) {
  78. Ref<Texture> tex = atex->get_atlas();
  79. if (!tex.is_valid()) {
  80. return Ref<Texture>();
  81. }
  82. Ref<Image> atlas = tex->get_data();
  83. if (!atlas.is_valid()) {
  84. return Ref<Texture>();
  85. }
  86. img = atlas->get_rect(atex->get_region());
  87. } else if (ltex.is_valid()) {
  88. img = ltex->to_image();
  89. } else {
  90. Ref<Texture> tex = p_from;
  91. if (tex.is_valid()) {
  92. img = tex->get_data();
  93. if (img.is_valid()) {
  94. img = img->duplicate();
  95. }
  96. }
  97. }
  98. if (img.is_null() || img->empty())
  99. return Ref<Texture>();
  100. img->clear_mipmaps();
  101. if (img->is_compressed()) {
  102. if (img->decompress() != OK)
  103. return Ref<Texture>();
  104. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  105. img->convert(Image::FORMAT_RGBA8);
  106. }
  107. Vector2 new_size = img->get_size();
  108. if (new_size.x > p_size.x) {
  109. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  110. }
  111. if (new_size.y > p_size.y) {
  112. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  113. }
  114. Vector2i new_size_i(MAX(1, (int)new_size.x), MAX(1, (int)new_size.y));
  115. img->resize(new_size_i.x, new_size_i.y, Image::INTERPOLATE_CUBIC);
  116. post_process_preview(img);
  117. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  118. ptex->create_from_image(img, 0);
  119. return ptex;
  120. }
  121. EditorTexturePreviewPlugin::EditorTexturePreviewPlugin() {
  122. }
  123. ////////////////////////////////////////////////////////////////////////////
  124. bool EditorImagePreviewPlugin::handles(const String &p_type) const {
  125. return p_type == "Image";
  126. }
  127. Ref<Texture> EditorImagePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  128. Ref<Image> img = p_from;
  129. if (img.is_null() || img->empty())
  130. return Ref<Image>();
  131. img = img->duplicate();
  132. img->clear_mipmaps();
  133. if (img->is_compressed()) {
  134. if (img->decompress() != OK)
  135. return Ref<Image>();
  136. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  137. img->convert(Image::FORMAT_RGBA8);
  138. }
  139. Vector2 new_size = img->get_size();
  140. if (new_size.x > p_size.x) {
  141. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  142. }
  143. if (new_size.y > p_size.y) {
  144. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  145. }
  146. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  147. post_process_preview(img);
  148. Ref<ImageTexture> ptex;
  149. ptex.instance();
  150. ptex->create_from_image(img, 0);
  151. return ptex;
  152. }
  153. EditorImagePreviewPlugin::EditorImagePreviewPlugin() {
  154. }
  155. bool EditorImagePreviewPlugin::generate_small_preview_automatically() const {
  156. return true;
  157. }
  158. ////////////////////////////////////////////////////////////////////////////
  159. /////////////////////////////////////////////////
  160. bool EditorBitmapPreviewPlugin::handles(const String &p_type) const {
  161. return ClassDB::is_parent_class(p_type, "BitMap");
  162. }
  163. Ref<Texture> EditorBitmapPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  164. Ref<BitMap> bm = p_from;
  165. if (bm->get_size() == Size2()) {
  166. return Ref<Texture>();
  167. }
  168. PoolVector<uint8_t> data;
  169. data.resize(bm->get_size().width * bm->get_size().height);
  170. {
  171. PoolVector<uint8_t>::Write w = data.write();
  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 * bm->get_size().width + i] = 255;
  176. } else {
  177. w[j * bm->get_size().width + i] = 0;
  178. }
  179. }
  180. }
  181. }
  182. Ref<Image> img;
  183. img.instance();
  184. img->create(bm->get_size().width, bm->get_size().height, 0, Image::FORMAT_L8, data);
  185. if (img->is_compressed()) {
  186. if (img->decompress() != OK)
  187. return Ref<Texture>();
  188. } else if (img->get_format() != Image::FORMAT_RGB8 && img->get_format() != Image::FORMAT_RGBA8) {
  189. img->convert(Image::FORMAT_RGBA8);
  190. }
  191. Vector2 new_size = img->get_size();
  192. if (new_size.x > p_size.x) {
  193. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  194. }
  195. if (new_size.y > p_size.y) {
  196. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  197. }
  198. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  199. post_process_preview(img);
  200. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  201. ptex->create_from_image(img, 0);
  202. return ptex;
  203. }
  204. bool EditorBitmapPreviewPlugin::generate_small_preview_automatically() const {
  205. return true;
  206. }
  207. EditorBitmapPreviewPlugin::EditorBitmapPreviewPlugin() {
  208. }
  209. ///////////////////////////////////////////////////////////////////////////
  210. bool EditorPackedScenePreviewPlugin::handles(const String &p_type) const {
  211. return ClassDB::is_parent_class(p_type, "PackedScene");
  212. }
  213. Ref<Texture> EditorPackedScenePreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  214. return generate_from_path(p_from->get_path(), p_size);
  215. }
  216. Ref<Texture> EditorPackedScenePreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const {
  217. String temp_path = EditorSettings::get_singleton()->get_cache_dir();
  218. String cache_base = ProjectSettings::get_singleton()->globalize_path(p_path).md5_text();
  219. cache_base = temp_path.plus_file("resthumb-" + cache_base);
  220. //does not have it, try to load a cached thumbnail
  221. String path = cache_base + ".png";
  222. if (!FileAccess::exists(path))
  223. return Ref<Texture>();
  224. Ref<Image> img;
  225. img.instance();
  226. Error err = img->load(path);
  227. if (err == OK) {
  228. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  229. post_process_preview(img);
  230. ptex->create_from_image(img, 0);
  231. return ptex;
  232. } else {
  233. return Ref<Texture>();
  234. }
  235. }
  236. EditorPackedScenePreviewPlugin::EditorPackedScenePreviewPlugin() {
  237. }
  238. //////////////////////////////////////////////////////////////////
  239. void EditorMaterialPreviewPlugin::_preview_done(const Variant &p_udata) {
  240. preview_done.set();
  241. }
  242. void EditorMaterialPreviewPlugin::_bind_methods() {
  243. ClassDB::bind_method("_preview_done", &EditorMaterialPreviewPlugin::_preview_done);
  244. }
  245. bool EditorMaterialPreviewPlugin::handles(const String &p_type) const {
  246. return ClassDB::is_parent_class(p_type, "Material"); //any material
  247. }
  248. bool EditorMaterialPreviewPlugin::generate_small_preview_automatically() const {
  249. return true;
  250. }
  251. Ref<Texture> EditorMaterialPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  252. Ref<Material> material = p_from;
  253. ERR_FAIL_COND_V(material.is_null(), Ref<Texture>());
  254. if (material->get_shader_mode() == Shader::MODE_SPATIAL) {
  255. VS::get_singleton()->mesh_surface_set_material(sphere, 0, material->get_rid());
  256. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  257. preview_done.clear();
  258. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMaterialPreviewPlugin *>(this), "_preview_done", Variant());
  259. while (!preview_done.is_set()) {
  260. OS::get_singleton()->delay_usec(10);
  261. }
  262. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  263. VS::get_singleton()->mesh_surface_set_material(sphere, 0, RID());
  264. ERR_FAIL_COND_V(!img.is_valid(), Ref<ImageTexture>());
  265. img->convert(Image::FORMAT_RGBA8);
  266. int thumbnail_size = MAX(p_size.x, p_size.y);
  267. img->resize(thumbnail_size, thumbnail_size, Image::INTERPOLATE_CUBIC);
  268. post_process_preview(img);
  269. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  270. ptex->create_from_image(img, 0);
  271. return ptex;
  272. }
  273. return Ref<Texture>();
  274. }
  275. EditorMaterialPreviewPlugin::EditorMaterialPreviewPlugin() {
  276. scenario = VS::get_singleton()->scenario_create();
  277. viewport = VS::get_singleton()->viewport_create();
  278. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  279. VS::get_singleton()->viewport_set_scenario(viewport, scenario);
  280. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  281. VS::get_singleton()->viewport_set_transparent_background(viewport, true);
  282. VS::get_singleton()->viewport_set_active(viewport, true);
  283. VS::get_singleton()->viewport_set_vflip(viewport, true);
  284. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  285. camera = VS::get_singleton()->camera_create();
  286. VS::get_singleton()->viewport_attach_camera(viewport, camera);
  287. VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3)));
  288. VS::get_singleton()->camera_set_perspective(camera, 45, 0.1, 10);
  289. light = VS::get_singleton()->directional_light_create();
  290. light_instance = VS::get_singleton()->instance_create2(light, scenario);
  291. VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  292. light2 = VS::get_singleton()->directional_light_create();
  293. VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  294. //VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  295. light_instance2 = VS::get_singleton()->instance_create2(light2, scenario);
  296. VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  297. sphere = VS::get_singleton()->mesh_create();
  298. sphere_instance = VS::get_singleton()->instance_create2(sphere, scenario);
  299. int lats = 32;
  300. int lons = 32;
  301. float radius = 1.0;
  302. PoolVector<Vector3> vertices;
  303. PoolVector<Vector3> normals;
  304. PoolVector<Vector2> uvs;
  305. PoolVector<float> tangents;
  306. Basis tt = Basis(Vector3(0, 1, 0), Math_PI * 0.5);
  307. for (int i = 1; i <= lats; i++) {
  308. double lat0 = Math_PI * (-0.5 + (double)(i - 1) / lats);
  309. double z0 = Math::sin(lat0);
  310. double zr0 = Math::cos(lat0);
  311. double lat1 = Math_PI * (-0.5 + (double)i / lats);
  312. double z1 = Math::sin(lat1);
  313. double zr1 = Math::cos(lat1);
  314. for (int j = lons; j >= 1; j--) {
  315. double lng0 = 2 * Math_PI * (double)(j - 1) / lons;
  316. double x0 = Math::cos(lng0);
  317. double y0 = Math::sin(lng0);
  318. double lng1 = 2 * Math_PI * (double)(j) / lons;
  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(VS::ARRAY_MAX);
  354. arr[VS::ARRAY_VERTEX] = vertices;
  355. arr[VS::ARRAY_NORMAL] = normals;
  356. arr[VS::ARRAY_TANGENT] = tangents;
  357. arr[VS::ARRAY_TEX_UV] = uvs;
  358. VS::get_singleton()->mesh_add_surface_from_arrays(sphere, VS::PRIMITIVE_TRIANGLES, arr);
  359. }
  360. EditorMaterialPreviewPlugin::~EditorMaterialPreviewPlugin() {
  361. VS::get_singleton()->free(sphere);
  362. VS::get_singleton()->free(sphere_instance);
  363. VS::get_singleton()->free(viewport);
  364. VS::get_singleton()->free(light);
  365. VS::get_singleton()->free(light_instance);
  366. VS::get_singleton()->free(light2);
  367. VS::get_singleton()->free(light_instance2);
  368. VS::get_singleton()->free(camera);
  369. VS::get_singleton()->free(scenario);
  370. }
  371. ///////////////////////////////////////////////////////////////////////////
  372. static bool _is_text_char(CharType c) {
  373. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  374. }
  375. bool EditorScriptPreviewPlugin::handles(const String &p_type) const {
  376. return ClassDB::is_parent_class(p_type, "Script");
  377. }
  378. Ref<Texture> EditorScriptPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  379. Ref<Script> scr = p_from;
  380. if (scr.is_null())
  381. return Ref<Texture>();
  382. String code = scr->get_source_code().strip_edges();
  383. if (code == "")
  384. return Ref<Texture>();
  385. List<String> kwors;
  386. scr->get_language()->get_reserved_words(&kwors);
  387. Set<String> keywords;
  388. for (List<String>::Element *E = kwors.front(); E; E = E->next()) {
  389. keywords.insert(E->get());
  390. }
  391. int line = 0;
  392. int col = 0;
  393. Ref<Image> img;
  394. img.instance();
  395. int thumbnail_size = MAX(p_size.x, p_size.y);
  396. img->create(thumbnail_size, thumbnail_size, 0, Image::FORMAT_RGBA8);
  397. Color bg_color = EditorSettings::get_singleton()->get("text_editor/highlighting/background_color");
  398. Color keyword_color = EditorSettings::get_singleton()->get("text_editor/highlighting/keyword_color");
  399. Color text_color = EditorSettings::get_singleton()->get("text_editor/highlighting/text_color");
  400. Color symbol_color = EditorSettings::get_singleton()->get("text_editor/highlighting/symbol_color");
  401. img->lock();
  402. if (bg_color.a == 0)
  403. bg_color = Color(0, 0, 0, 0);
  404. bg_color.a = MAX(bg_color.a, 0.2); // some background
  405. for (int i = 0; i < thumbnail_size; i++) {
  406. for (int j = 0; j < thumbnail_size; j++) {
  407. img->set_pixel(i, j, bg_color);
  408. }
  409. }
  410. const int x0 = thumbnail_size / 8;
  411. const int y0 = thumbnail_size / 8;
  412. const int available_height = thumbnail_size - 2 * y0;
  413. col = x0;
  414. bool prev_is_text = false;
  415. bool in_keyword = false;
  416. for (int i = 0; i < code.length(); i++) {
  417. CharType c = code[i];
  418. if (c > 32) {
  419. if (col < thumbnail_size) {
  420. Color color = text_color;
  421. if (c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t')) {
  422. //make symbol a little visible
  423. color = symbol_color;
  424. in_keyword = false;
  425. } else if (!prev_is_text && _is_text_char(c)) {
  426. int pos = i;
  427. while (_is_text_char(code[pos])) {
  428. pos++;
  429. }
  430. String word = code.substr(i, pos - i);
  431. if (keywords.has(word))
  432. in_keyword = true;
  433. } else if (!_is_text_char(c)) {
  434. in_keyword = false;
  435. }
  436. if (in_keyword)
  437. color = keyword_color;
  438. Color ul = color;
  439. ul.a *= 0.5;
  440. img->set_pixel(col, y0 + line * 2, bg_color.blend(ul));
  441. img->set_pixel(col, y0 + line * 2 + 1, color);
  442. prev_is_text = _is_text_char(c);
  443. }
  444. } else {
  445. prev_is_text = false;
  446. in_keyword = false;
  447. if (c == '\n') {
  448. col = x0;
  449. line++;
  450. if (line >= available_height / 2)
  451. break;
  452. } else if (c == '\t') {
  453. col += 3;
  454. }
  455. }
  456. col++;
  457. }
  458. img->unlock();
  459. post_process_preview(img);
  460. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  461. ptex->create_from_image(img, 0);
  462. return ptex;
  463. }
  464. EditorScriptPreviewPlugin::EditorScriptPreviewPlugin() {
  465. }
  466. ///////////////////////////////////////////////////////////////////
  467. bool EditorAudioStreamPreviewPlugin::handles(const String &p_type) const {
  468. return ClassDB::is_parent_class(p_type, "AudioStream");
  469. }
  470. Ref<Texture> EditorAudioStreamPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  471. Ref<AudioStream> stream = p_from;
  472. ERR_FAIL_COND_V(stream.is_null(), Ref<Texture>());
  473. PoolVector<uint8_t> img;
  474. int w = p_size.x;
  475. int h = p_size.y;
  476. img.resize(w * h * 3);
  477. PoolVector<uint8_t>::Write imgdata = img.write();
  478. uint8_t *imgw = imgdata.ptr();
  479. Ref<AudioStreamPlayback> playback = stream->instance_playback();
  480. ERR_FAIL_COND_V(playback.is_null(), Ref<Texture>());
  481. float len_s = stream->get_length();
  482. if (len_s == 0) {
  483. len_s = 60; //one minute audio if no length specified
  484. }
  485. int frame_length = AudioServer::get_singleton()->get_mix_rate() * len_s;
  486. Vector<AudioFrame> frames;
  487. frames.resize(frame_length);
  488. playback->start();
  489. playback->mix(frames.ptrw(), 1, frames.size());
  490. playback->stop();
  491. for (int i = 0; i < w; i++) {
  492. float max = -1000;
  493. float min = 1000;
  494. int from = uint64_t(i) * frame_length / w;
  495. int to = (uint64_t(i) + 1) * frame_length / w;
  496. to = MIN(to, frame_length);
  497. from = MIN(from, frame_length - 1);
  498. if (to == from) {
  499. to = from + 1;
  500. }
  501. for (int j = from; j < to; j++) {
  502. max = MAX(max, frames[j].l);
  503. max = MAX(max, frames[j].r);
  504. min = MIN(min, frames[j].l);
  505. min = MIN(min, frames[j].r);
  506. }
  507. int pfrom = CLAMP((min * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  508. int pto = CLAMP((max * 0.5 + 0.5) * h / 2, 0, h / 2) + h / 4;
  509. for (int j = 0; j < h; j++) {
  510. uint8_t *p = &imgw[(j * w + i) * 3];
  511. if (j < pfrom || j > pto) {
  512. p[0] = 100;
  513. p[1] = 100;
  514. p[2] = 100;
  515. } else {
  516. p[0] = 180;
  517. p[1] = 180;
  518. p[2] = 180;
  519. }
  520. }
  521. }
  522. imgdata.release();
  523. //post_process_preview(img);
  524. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  525. Ref<Image> image;
  526. image.instance();
  527. image->create(w, h, false, Image::FORMAT_RGB8, img);
  528. ptex->create_from_image(image, 0);
  529. return ptex;
  530. }
  531. EditorAudioStreamPreviewPlugin::EditorAudioStreamPreviewPlugin() {
  532. }
  533. ///////////////////////////////////////////////////////////////////////////
  534. void EditorMeshPreviewPlugin::_preview_done(const Variant &p_udata) {
  535. preview_done.set();
  536. }
  537. void EditorMeshPreviewPlugin::_bind_methods() {
  538. ClassDB::bind_method("_preview_done", &EditorMeshPreviewPlugin::_preview_done);
  539. }
  540. bool EditorMeshPreviewPlugin::handles(const String &p_type) const {
  541. return ClassDB::is_parent_class(p_type, "Mesh"); //any Mesh
  542. }
  543. Ref<Texture> EditorMeshPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  544. Ref<Mesh> mesh = p_from;
  545. ERR_FAIL_COND_V(mesh.is_null(), Ref<Texture>());
  546. VS::get_singleton()->instance_set_base(mesh_instance, mesh->get_rid());
  547. AABB aabb = mesh->get_aabb();
  548. Vector3 ofs = aabb.position + aabb.size * 0.5;
  549. aabb.position -= ofs;
  550. Transform xform;
  551. xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI * 0.125);
  552. xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI * 0.125) * xform.basis;
  553. AABB rot_aabb = xform.xform(aabb);
  554. float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5;
  555. if (m == 0)
  556. return Ref<Texture>();
  557. m = 1.0 / m;
  558. m *= 0.5;
  559. xform.basis.scale(Vector3(m, m, m));
  560. xform.origin = -xform.basis.xform(ofs); //-ofs*m;
  561. xform.origin.z -= rot_aabb.size.z * 2;
  562. VS::get_singleton()->instance_set_transform(mesh_instance, xform);
  563. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  564. preview_done.clear();
  565. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorMeshPreviewPlugin *>(this), "_preview_done", Variant());
  566. while (!preview_done.is_set()) {
  567. OS::get_singleton()->delay_usec(10);
  568. }
  569. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  570. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  571. VS::get_singleton()->instance_set_base(mesh_instance, RID());
  572. img->convert(Image::FORMAT_RGBA8);
  573. Vector2 new_size = img->get_size();
  574. if (new_size.x > p_size.x) {
  575. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  576. }
  577. if (new_size.y > p_size.y) {
  578. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  579. }
  580. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  581. post_process_preview(img);
  582. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  583. ptex->create_from_image(img, 0);
  584. return ptex;
  585. }
  586. EditorMeshPreviewPlugin::EditorMeshPreviewPlugin() {
  587. scenario = VS::get_singleton()->scenario_create();
  588. viewport = VS::get_singleton()->viewport_create();
  589. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  590. VS::get_singleton()->viewport_set_vflip(viewport, true);
  591. VS::get_singleton()->viewport_set_scenario(viewport, scenario);
  592. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  593. VS::get_singleton()->viewport_set_transparent_background(viewport, true);
  594. VS::get_singleton()->viewport_set_active(viewport, true);
  595. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  596. camera = VS::get_singleton()->camera_create();
  597. VS::get_singleton()->viewport_attach_camera(viewport, camera);
  598. VS::get_singleton()->camera_set_transform(camera, Transform(Basis(), Vector3(0, 0, 3)));
  599. //VS::get_singleton()->camera_set_perspective(camera,45,0.1,10);
  600. VS::get_singleton()->camera_set_orthogonal(camera, 1.0, 0.01, 1000.0);
  601. light = VS::get_singleton()->directional_light_create();
  602. light_instance = VS::get_singleton()->instance_create2(light, scenario);
  603. VS::get_singleton()->instance_set_transform(light_instance, Transform().looking_at(Vector3(-1, -1, -1), Vector3(0, 1, 0)));
  604. light2 = VS::get_singleton()->directional_light_create();
  605. VS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7));
  606. //VS::get_singleton()->light_set_color(light2, VS::LIGHT_COLOR_SPECULAR, Color(0.0, 0.0, 0.0));
  607. light_instance2 = VS::get_singleton()->instance_create2(light2, scenario);
  608. VS::get_singleton()->instance_set_transform(light_instance2, Transform().looking_at(Vector3(0, 1, 0), Vector3(0, 0, 1)));
  609. //sphere = VS::get_singleton()->mesh_create();
  610. mesh_instance = VS::get_singleton()->instance_create();
  611. VS::get_singleton()->instance_set_scenario(mesh_instance, scenario);
  612. }
  613. EditorMeshPreviewPlugin::~EditorMeshPreviewPlugin() {
  614. //VS::get_singleton()->free(sphere);
  615. VS::get_singleton()->free(mesh_instance);
  616. VS::get_singleton()->free(viewport);
  617. VS::get_singleton()->free(light);
  618. VS::get_singleton()->free(light_instance);
  619. VS::get_singleton()->free(light2);
  620. VS::get_singleton()->free(light_instance2);
  621. VS::get_singleton()->free(camera);
  622. VS::get_singleton()->free(scenario);
  623. }
  624. ///////////////////////////////////////////////////////////////////////////
  625. void EditorFontPreviewPlugin::_preview_done(const Variant &p_udata) {
  626. preview_done.set();
  627. }
  628. void EditorFontPreviewPlugin::_bind_methods() {
  629. ClassDB::bind_method("_preview_done", &EditorFontPreviewPlugin::_preview_done);
  630. }
  631. bool EditorFontPreviewPlugin::handles(const String &p_type) const {
  632. return ClassDB::is_parent_class(p_type, "DynamicFontData") || ClassDB::is_parent_class(p_type, "DynamicFont");
  633. }
  634. Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path, const Size2 &p_size) const {
  635. Ref<ResourceInteractiveLoader> ril = ResourceLoader::load_interactive(p_path);
  636. ril.ptr()->wait();
  637. RES res = ril.ptr()->get_resource();
  638. Ref<DynamicFont> sampled_font;
  639. sampled_font.instance();
  640. if (res->is_class("DynamicFont")) {
  641. Ref<DynamicFont> font = res;
  642. if (font->get_font_data().is_valid()) {
  643. sampled_font->set_font_data(font->get_font_data()->duplicate());
  644. }
  645. for (int i = 0; i < font->get_fallback_count(); i++) {
  646. sampled_font->add_fallback(font->get_fallback(i)->duplicate());
  647. }
  648. } else if (res->is_class("DynamicFontData")) {
  649. sampled_font->set_font_data(res->duplicate());
  650. }
  651. sampled_font->set_size(50);
  652. String sampled_text = "Abg";
  653. Vector2 size = sampled_font->get_string_size(sampled_text);
  654. Vector2 pos;
  655. pos.x = 64 - size.x / 2;
  656. pos.y = 80;
  657. Ref<Font> font = sampled_font;
  658. font->draw(canvas_item, pos, sampled_text);
  659. preview_done.clear();
  660. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture
  661. VS::get_singleton()->request_frame_drawn_callback(const_cast<EditorFontPreviewPlugin *>(this), "_preview_done", Variant());
  662. while (!preview_done.is_set()) {
  663. OS::get_singleton()->delay_usec(10);
  664. }
  665. VS::get_singleton()->canvas_item_clear(canvas_item);
  666. Ref<Image> img = VS::get_singleton()->texture_get_data(viewport_texture);
  667. ERR_FAIL_COND_V(img.is_null(), Ref<ImageTexture>());
  668. img->convert(Image::FORMAT_RGBA8);
  669. Vector2 new_size = img->get_size();
  670. if (new_size.x > p_size.x) {
  671. new_size = Vector2(p_size.x, new_size.y * p_size.x / new_size.x);
  672. }
  673. if (new_size.y > p_size.y) {
  674. new_size = Vector2(new_size.x * p_size.y / new_size.y, p_size.y);
  675. }
  676. img->resize(new_size.x, new_size.y, Image::INTERPOLATE_CUBIC);
  677. post_process_preview(img);
  678. Ref<ImageTexture> ptex = Ref<ImageTexture>(memnew(ImageTexture));
  679. ptex->create_from_image(img, 0);
  680. return ptex;
  681. }
  682. Ref<Texture> EditorFontPreviewPlugin::generate(const RES &p_from, const Size2 &p_size) const {
  683. String path = p_from->get_path();
  684. if (!FileAccess::exists(path)) {
  685. return Ref<Texture>();
  686. }
  687. return generate_from_path(path, p_size);
  688. }
  689. EditorFontPreviewPlugin::EditorFontPreviewPlugin() {
  690. viewport = VS::get_singleton()->viewport_create();
  691. VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_DISABLED);
  692. VS::get_singleton()->viewport_set_vflip(viewport, true);
  693. VS::get_singleton()->viewport_set_size(viewport, 128, 128);
  694. VS::get_singleton()->viewport_set_active(viewport, true);
  695. viewport_texture = VS::get_singleton()->viewport_get_texture(viewport);
  696. canvas = VS::get_singleton()->canvas_create();
  697. canvas_item = VS::get_singleton()->canvas_item_create();
  698. VS::get_singleton()->viewport_attach_canvas(viewport, canvas);
  699. VS::get_singleton()->canvas_item_set_parent(canvas_item, canvas);
  700. }
  701. EditorFontPreviewPlugin::~EditorFontPreviewPlugin() {
  702. VS::get_singleton()->free(canvas_item);
  703. VS::get_singleton()->free(canvas);
  704. VS::get_singleton()->free(viewport);
  705. }