2
0

editor_preview_plugins.cpp 28 KB

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