2
0

editor_preview_plugins.cpp 29 KB

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