editor_preview_plugins.cpp 26 KB

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