editor_preview_plugins.cpp 28 KB

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