visual_shader_editor_plugin.cpp 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474
  1. /*************************************************************************/
  2. /* visual_shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_shader_editor_plugin.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_properties.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/panel.h"
  42. #include "scene/main/viewport.h"
  43. #include "scene/resources/visual_shader_nodes.h"
  44. #include "servers/visual/shader_types.h"
  45. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  46. if (get_script_instance()) {
  47. return get_script_instance()->call("create_editor", p_parent_resource, p_node);
  48. }
  49. return NULL;
  50. }
  51. void VisualShaderNodePlugin::_bind_methods() {
  52. BIND_VMETHOD(MethodInfo(Variant::OBJECT, "create_editor", PropertyInfo(Variant::OBJECT, "parent_resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::OBJECT, "for_node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode")));
  53. }
  54. ///////////////////
  55. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  56. bool changed = false;
  57. if (p_visual_shader) {
  58. if (visual_shader.is_null()) {
  59. changed = true;
  60. } else {
  61. if (visual_shader.ptr() != p_visual_shader) {
  62. changed = true;
  63. }
  64. }
  65. visual_shader = Ref<VisualShader>(p_visual_shader);
  66. if (!visual_shader->is_connected("changed", this, "_update_preview")) {
  67. visual_shader->connect("changed", this, "_update_preview");
  68. }
  69. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  70. } else {
  71. if (visual_shader.is_valid()) {
  72. if (visual_shader->is_connected("changed", this, "")) {
  73. visual_shader->disconnect("changed", this, "_update_preview");
  74. }
  75. }
  76. visual_shader.unref();
  77. }
  78. if (visual_shader.is_null()) {
  79. hide();
  80. } else {
  81. if (changed) { // to avoid tree collapse
  82. _clear_buffer();
  83. _update_options_menu();
  84. _update_preview();
  85. }
  86. _update_graph();
  87. }
  88. }
  89. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  90. if (plugins.find(p_plugin) != -1)
  91. return;
  92. plugins.push_back(p_plugin);
  93. }
  94. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  95. plugins.erase(p_plugin);
  96. }
  97. void VisualShaderEditor::clear_custom_types() {
  98. for (int i = 0; i < add_options.size(); i++) {
  99. if (add_options[i].is_custom) {
  100. add_options.remove(i);
  101. i--;
  102. }
  103. }
  104. }
  105. void VisualShaderEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, const String &p_subcategory) {
  106. ERR_FAIL_COND(!p_name.is_valid_identifier());
  107. ERR_FAIL_COND(!p_script.is_valid());
  108. for (int i = 0; i < add_options.size(); i++) {
  109. if (add_options[i].is_custom) {
  110. if (add_options[i].script == p_script)
  111. return;
  112. }
  113. }
  114. AddOption ao;
  115. ao.name = p_name;
  116. ao.script = p_script;
  117. ao.return_type = p_return_icon_type;
  118. ao.description = p_description;
  119. ao.category = p_category;
  120. ao.sub_category = p_subcategory;
  121. ao.is_custom = true;
  122. bool begin = false;
  123. for (int i = 0; i < add_options.size(); i++) {
  124. if (add_options[i].is_custom) {
  125. if (add_options[i].category == p_category) {
  126. if (!begin) {
  127. begin = true;
  128. }
  129. } else {
  130. if (begin) {
  131. add_options.insert(i, ao);
  132. return;
  133. }
  134. }
  135. }
  136. }
  137. add_options.push_back(ao);
  138. }
  139. bool VisualShaderEditor::_is_available(int p_mode) {
  140. int current_mode = edit_type->get_selected();
  141. if (p_mode != -1) {
  142. switch (current_mode) {
  143. case VisualShader::TYPE_VERTEX:
  144. current_mode = 1;
  145. break;
  146. case VisualShader::TYPE_FRAGMENT:
  147. current_mode = 2;
  148. break;
  149. case VisualShader::TYPE_LIGHT:
  150. current_mode = 4;
  151. break;
  152. default:
  153. break;
  154. }
  155. int temp_mode = 0;
  156. if (p_mode & VisualShader::TYPE_FRAGMENT) {
  157. temp_mode |= 2;
  158. }
  159. if (p_mode & VisualShader::TYPE_LIGHT) {
  160. temp_mode |= 4;
  161. }
  162. if (temp_mode == 0) {
  163. temp_mode |= 1;
  164. }
  165. p_mode = temp_mode;
  166. }
  167. return (p_mode == -1 || (p_mode & current_mode) != 0);
  168. }
  169. void VisualShaderEditor::update_custom_nodes() {
  170. if (members_dialog->is_visible()) {
  171. return;
  172. }
  173. clear_custom_types();
  174. List<StringName> class_list;
  175. ScriptServer::get_global_class_list(&class_list);
  176. Dictionary added;
  177. for (int i = 0; i < class_list.size(); i++) {
  178. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  179. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  180. Ref<Resource> res = ResourceLoader::load(script_path);
  181. ERR_FAIL_COND(res.is_null());
  182. ERR_FAIL_COND(!res->is_class("Script"));
  183. Ref<Script> script = Ref<Script>(res);
  184. Ref<VisualShaderNodeCustom> ref;
  185. ref.instance();
  186. ref->set_script(script.get_ref_ptr());
  187. String name;
  188. if (ref->has_method("_get_name")) {
  189. name = (String)ref->call("_get_name");
  190. } else {
  191. name = "Unnamed";
  192. }
  193. String description = "";
  194. if (ref->has_method("_get_description")) {
  195. description = (String)ref->call("_get_description");
  196. }
  197. int return_icon_type = -1;
  198. if (ref->has_method("_get_return_icon_type")) {
  199. return_icon_type = (int)ref->call("_get_return_icon_type");
  200. }
  201. String category = "";
  202. if (ref->has_method("_get_category")) {
  203. category = (String)ref->call("_get_category");
  204. }
  205. if (category == "") {
  206. category = "Custom";
  207. }
  208. String subcategory = "";
  209. if (ref->has_method("_get_subcategory")) {
  210. subcategory = (String)ref->call("_get_subcategory");
  211. }
  212. Dictionary dict;
  213. dict["name"] = name;
  214. dict["script"] = script;
  215. dict["description"] = description;
  216. dict["return_icon_type"] = return_icon_type;
  217. dict["category"] = category;
  218. dict["subcategory"] = subcategory;
  219. String key;
  220. key = category;
  221. key += "/";
  222. if (subcategory != "") {
  223. key += subcategory;
  224. key += "/";
  225. }
  226. key += name;
  227. added[key] = dict;
  228. }
  229. }
  230. Array keys = added.keys();
  231. keys.sort();
  232. for (int i = 0; i < keys.size(); i++) {
  233. const Variant &key = keys.get(i);
  234. const Dictionary &value = (Dictionary)added[key];
  235. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["subcategory"]);
  236. }
  237. _update_options_menu();
  238. }
  239. String VisualShaderEditor::_get_description(int p_idx) {
  240. if (add_options[p_idx].highend) {
  241. return TTR("(GLES3 only)") + " " + add_options[p_idx].description; // TODO: change it to (Vulkan Only) when its ready
  242. } else {
  243. return add_options[p_idx].description;
  244. }
  245. }
  246. void VisualShaderEditor::_update_options_menu() {
  247. node_desc->set_text("");
  248. members_dialog->get_ok()->set_disabled(true);
  249. String prev_category;
  250. String prev_sub_category;
  251. members->clear();
  252. TreeItem *root = members->create_item();
  253. TreeItem *category = NULL;
  254. TreeItem *sub_category = NULL;
  255. String filter = node_filter->get_text().strip_edges();
  256. bool use_filter = !filter.empty();
  257. Vector<String> categories;
  258. Vector<String> sub_categories;
  259. int item_count = 0;
  260. int item_count2 = 0;
  261. bool is_first_item = true;
  262. Color unsupported_color = get_color("error_color", "Editor");
  263. Color supported_color = get_color("warning_color", "Editor");
  264. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES2";
  265. int current_func = -1;
  266. if (!visual_shader.is_null()) {
  267. current_func = visual_shader->get_mode();
  268. }
  269. for (int i = 0; i < add_options.size() + 1; i++) {
  270. if (i == add_options.size()) {
  271. if (sub_category != NULL && item_count2 == 0) {
  272. memdelete(sub_category);
  273. --item_count;
  274. }
  275. if (category != NULL && item_count == 0) {
  276. memdelete(category);
  277. }
  278. break;
  279. }
  280. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  281. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode))
  282. continue;
  283. if (prev_category != add_options[i].category) {
  284. if (category != NULL && item_count == 0) {
  285. memdelete(category);
  286. }
  287. item_count = 0;
  288. prev_sub_category = "";
  289. category = members->create_item(root);
  290. category->set_text(0, add_options[i].category);
  291. category->set_selectable(0, false);
  292. if (!use_filter)
  293. category->set_collapsed(true);
  294. }
  295. if (add_options[i].sub_category != "") {
  296. if (prev_sub_category != add_options[i].sub_category) {
  297. if (category != NULL) {
  298. if (sub_category != NULL && item_count2 == 0) {
  299. memdelete(sub_category);
  300. --item_count;
  301. }
  302. ++item_count;
  303. item_count2 = 0;
  304. sub_category = members->create_item(category);
  305. sub_category->set_text(0, add_options[i].sub_category);
  306. sub_category->set_selectable(0, false);
  307. if (!use_filter)
  308. sub_category->set_collapsed(true);
  309. }
  310. }
  311. } else {
  312. sub_category = NULL;
  313. }
  314. TreeItem *p_category = NULL;
  315. if (sub_category != NULL) {
  316. p_category = sub_category;
  317. ++item_count2;
  318. } else if (category != NULL) {
  319. p_category = category;
  320. ++item_count;
  321. }
  322. if (p_category != NULL) {
  323. TreeItem *item = members->create_item(p_category);
  324. if (add_options[i].highend && low_driver)
  325. item->set_custom_color(0, unsupported_color);
  326. else if (add_options[i].highend)
  327. item->set_custom_color(0, supported_color);
  328. item->set_text(0, add_options[i].name);
  329. if (is_first_item && use_filter) {
  330. item->select(0);
  331. node_desc->set_text(_get_description(i));
  332. is_first_item = false;
  333. }
  334. switch (add_options[i].return_type) {
  335. case VisualShaderNode::PORT_TYPE_SCALAR:
  336. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"));
  337. break;
  338. case VisualShaderNode::PORT_TYPE_VECTOR:
  339. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"));
  340. break;
  341. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  342. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"));
  343. break;
  344. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  345. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"));
  346. break;
  347. case VisualShaderNode::PORT_TYPE_SAMPLER:
  348. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"));
  349. break;
  350. default:
  351. break;
  352. }
  353. item->set_meta("id", i);
  354. }
  355. prev_sub_category = add_options[i].sub_category;
  356. prev_category = add_options[i].category;
  357. }
  358. }
  359. }
  360. Size2 VisualShaderEditor::get_minimum_size() const {
  361. return Size2(10, 200);
  362. }
  363. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  364. Button *button = Object::cast_to<Button>(obj);
  365. if (!button)
  366. return;
  367. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  368. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  369. }
  370. static Ref<StyleBoxEmpty> make_empty_stylebox(float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1) {
  371. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  372. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  373. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  374. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  375. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  376. return style;
  377. }
  378. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  379. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  380. Ref<StyleBoxFlat> sb = node->get_stylebox("frame", "GraphNode");
  381. Color c = sb->get_border_color();
  382. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  383. mono_color.a = 0.85;
  384. c = mono_color;
  385. node->add_color_override("title_color", c);
  386. c.a = 0.7;
  387. node->add_color_override("close_color", c);
  388. node->add_color_override("resizer_color", c);
  389. }
  390. }
  391. void VisualShaderEditor::_update_graph() {
  392. if (updating)
  393. return;
  394. if (visual_shader.is_null())
  395. return;
  396. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  397. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  398. graph->clear_connections();
  399. //erase all nodes
  400. for (int i = 0; i < graph->get_child_count(); i++) {
  401. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  402. Node *node = graph->get_child(i);
  403. graph->remove_child(node);
  404. memdelete(node);
  405. i--;
  406. }
  407. }
  408. static const Color type_color[5] = {
  409. Color(0.38, 0.85, 0.96), // scalar
  410. Color(0.84, 0.49, 0.93), // vector
  411. Color(0.55, 0.65, 0.94), // boolean
  412. Color(0.96, 0.66, 0.43), // transform
  413. Color(1.0, 1.0, 0.0) // sampler
  414. };
  415. List<VisualShader::Connection> connections;
  416. visual_shader->get_node_connections(type, &connections);
  417. Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  418. Vector<int> nodes = visual_shader->get_node_list(type);
  419. VisualShaderNodeUniformRef::clear_uniforms();
  420. // scan for all uniforms
  421. for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
  422. Vector<int> tnodes = visual_shader->get_node_list((VisualShader::Type)t);
  423. for (int i = 0; i < tnodes.size(); i++) {
  424. Ref<VisualShaderNode> vsnode = visual_shader->get_node((VisualShader::Type)t, tnodes[i]);
  425. Ref<VisualShaderNodeUniform> uniform = vsnode;
  426. if (uniform.is_valid()) {
  427. Ref<VisualShaderNodeScalarUniform> scalar_uniform = vsnode;
  428. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = vsnode;
  429. Ref<VisualShaderNodeColorUniform> color_uniform = vsnode;
  430. Ref<VisualShaderNodeBooleanUniform> bool_uniform = vsnode;
  431. Ref<VisualShaderNodeTransformUniform> transform_uniform = vsnode;
  432. VisualShaderNodeUniformRef::UniformType uniform_type;
  433. if (scalar_uniform.is_valid()) {
  434. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SCALAR;
  435. } else if (bool_uniform.is_valid()) {
  436. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_BOOLEAN;
  437. } else if (vec3_uniform.is_valid()) {
  438. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_VECTOR;
  439. } else if (transform_uniform.is_valid()) {
  440. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_TRANSFORM;
  441. } else if (color_uniform.is_valid()) {
  442. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_COLOR;
  443. } else {
  444. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SAMPLER;
  445. }
  446. VisualShaderNodeUniformRef::add_uniform(uniform->get_uniform_name(), uniform_type);
  447. }
  448. }
  449. }
  450. Control *offset;
  451. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  452. Vector2 position = visual_shader->get_node_position(type, nodes[n_i]);
  453. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, nodes[n_i]);
  454. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  455. bool is_group = !group_node.is_null();
  456. Size2 size = Size2(0, 0);
  457. VisualShaderNodeCustom *custom = Object::cast_to<VisualShaderNodeCustom>(vsnode.ptr());
  458. if (custom) {
  459. custom->_set_initialized(true);
  460. }
  461. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  462. bool is_expression = !expression_node.is_null();
  463. String expression = "";
  464. GraphNode *node = memnew(GraphNode);
  465. if (is_group) {
  466. size = group_node->get_size();
  467. node->set_resizable(true);
  468. node->connect("resize_request", this, "_node_resized", varray((int)type, nodes[n_i]));
  469. }
  470. if (is_expression) {
  471. expression = expression_node->get_expression();
  472. }
  473. /*if (!vsnode->is_connected("changed", this, "_node_changed")) {
  474. vsnode->connect("changed", this, "_node_changed", varray(vsnode->get_instance_id()), CONNECT_DEFERRED);
  475. }*/
  476. node->set_offset(position);
  477. node->set_title(vsnode->get_caption());
  478. node->set_name(itos(nodes[n_i]));
  479. if (nodes[n_i] >= 2) {
  480. node->set_show_close_button(true);
  481. node->connect("close_request", this, "_delete_request", varray(nodes[n_i]), CONNECT_DEFERRED);
  482. }
  483. node->connect("dragged", this, "_node_dragged", varray(nodes[n_i]));
  484. Control *custom_editor = NULL;
  485. int port_offset = 0;
  486. if (is_group) {
  487. port_offset += 2;
  488. }
  489. Ref<VisualShaderNodeUniform> uniform = vsnode;
  490. if (uniform.is_valid()) {
  491. graph->add_child(node);
  492. _update_created_node(node);
  493. LineEdit *uniform_name = memnew(LineEdit);
  494. uniform_name->set_text(uniform->get_uniform_name());
  495. node->add_child(uniform_name);
  496. uniform_name->connect("text_entered", this, "_line_edit_changed", varray(uniform_name, nodes[n_i]));
  497. uniform_name->connect("focus_exited", this, "_line_edit_focus_out", varray(uniform_name, nodes[n_i]));
  498. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  499. //shortcut
  500. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  501. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  502. continue;
  503. }
  504. port_offset++;
  505. }
  506. for (int i = 0; i < plugins.size(); i++) {
  507. custom_editor = plugins.write[i]->create_editor(visual_shader, vsnode);
  508. if (custom_editor) {
  509. break;
  510. }
  511. }
  512. if (custom_editor && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  513. //will be embedded in first port
  514. } else if (custom_editor) {
  515. port_offset++;
  516. node->add_child(custom_editor);
  517. custom_editor = NULL;
  518. }
  519. if (is_group) {
  520. offset = memnew(Control);
  521. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  522. node->add_child(offset);
  523. if (group_node->is_editable()) {
  524. HBoxContainer *hb2 = memnew(HBoxContainer);
  525. Button *add_input_btn = memnew(Button);
  526. add_input_btn->set_text(TTR("Add Input"));
  527. add_input_btn->connect("pressed", this, "_add_input_port", varray(nodes[n_i], group_node->get_free_input_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "input" + itos(group_node->get_free_input_port_id())), CONNECT_DEFERRED);
  528. hb2->add_child(add_input_btn);
  529. hb2->add_spacer();
  530. Button *add_output_btn = memnew(Button);
  531. add_output_btn->set_text(TTR("Add Output"));
  532. add_output_btn->connect("pressed", this, "_add_output_port", varray(nodes[n_i], group_node->get_free_output_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "output" + itos(group_node->get_free_output_port_id())), CONNECT_DEFERRED);
  533. hb2->add_child(add_output_btn);
  534. node->add_child(hb2);
  535. }
  536. }
  537. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  538. if (vsnode->is_port_separator(i)) {
  539. node->add_child(memnew(HSeparator));
  540. port_offset++;
  541. }
  542. bool valid_left = i < vsnode->get_input_port_count();
  543. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  544. bool port_left_used = false;
  545. String name_left;
  546. if (valid_left) {
  547. name_left = vsnode->get_input_port_name(i);
  548. port_left = vsnode->get_input_port_type(i);
  549. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  550. if (E->get().to_node == nodes[n_i] && E->get().to_port == i) {
  551. port_left_used = true;
  552. }
  553. }
  554. }
  555. bool valid_right = i < vsnode->get_output_port_count();
  556. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  557. String name_right;
  558. if (valid_right) {
  559. name_right = vsnode->get_output_port_name(i);
  560. port_right = vsnode->get_output_port_type(i);
  561. }
  562. HBoxContainer *hb = memnew(HBoxContainer);
  563. hb->add_constant_override("separation", 7 * EDSCALE);
  564. Variant default_value;
  565. if (valid_left && !port_left_used) {
  566. default_value = vsnode->get_input_port_default_value(i);
  567. }
  568. if (default_value.get_type() != Variant::NIL) { // only a label
  569. Button *button = memnew(Button);
  570. hb->add_child(button);
  571. button->connect("pressed", this, "_edit_port_default_input", varray(button, nodes[n_i], i));
  572. switch (default_value.get_type()) {
  573. case Variant::COLOR: {
  574. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  575. button->connect("draw", this, "_draw_color_over_button", varray(button, default_value));
  576. } break;
  577. case Variant::BOOL: {
  578. button->set_text(((bool)default_value) ? "true" : "false");
  579. } break;
  580. case Variant::INT:
  581. case Variant::REAL: {
  582. button->set_text(String::num(default_value, 4));
  583. } break;
  584. case Variant::VECTOR3: {
  585. Vector3 v = default_value;
  586. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  587. } break;
  588. default: {
  589. }
  590. }
  591. }
  592. if (i == 0 && custom_editor) {
  593. hb->add_child(custom_editor);
  594. custom_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  595. } else {
  596. if (valid_left) {
  597. if (is_group) {
  598. OptionButton *type_box = memnew(OptionButton);
  599. hb->add_child(type_box);
  600. type_box->add_item(TTR("Scalar"));
  601. type_box->add_item(TTR("Vector"));
  602. type_box->add_item(TTR("Boolean"));
  603. type_box->add_item(TTR("Transform"));
  604. type_box->add_item(TTR("Sampler"));
  605. type_box->select(group_node->get_input_port_type(i));
  606. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  607. type_box->connect("item_selected", this, "_change_input_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  608. LineEdit *name_box = memnew(LineEdit);
  609. hb->add_child(name_box);
  610. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  611. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  612. name_box->set_text(name_left);
  613. name_box->connect("text_entered", this, "_change_input_port_name", varray(name_box, nodes[n_i], i));
  614. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, false));
  615. Button *remove_btn = memnew(Button);
  616. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  617. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  618. remove_btn->connect("pressed", this, "_remove_input_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  619. hb->add_child(remove_btn);
  620. } else {
  621. Label *label = memnew(Label);
  622. label->set_text(name_left);
  623. label->add_style_override("normal", label_style); //more compact
  624. hb->add_child(label);
  625. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  626. Label *hint_label = memnew(Label);
  627. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  628. hint_label->add_color_override("font_color", get_color("font_color_readonly", "TextEdit"));
  629. hint_label->add_style_override("normal", label_style);
  630. hb->add_child(hint_label);
  631. }
  632. }
  633. }
  634. if (!is_group) {
  635. hb->add_spacer();
  636. }
  637. if (valid_right) {
  638. if (is_group) {
  639. Button *remove_btn = memnew(Button);
  640. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  641. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  642. remove_btn->connect("pressed", this, "_remove_output_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  643. hb->add_child(remove_btn);
  644. LineEdit *name_box = memnew(LineEdit);
  645. hb->add_child(name_box);
  646. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  647. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  648. name_box->set_text(name_right);
  649. name_box->connect("text_entered", this, "_change_output_port_name", varray(name_box, nodes[n_i], i));
  650. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, true));
  651. OptionButton *type_box = memnew(OptionButton);
  652. hb->add_child(type_box);
  653. type_box->add_item(TTR("Scalar"));
  654. type_box->add_item(TTR("Vector"));
  655. type_box->add_item(TTR("Boolean"));
  656. type_box->add_item(TTR("Transform"));
  657. type_box->select(group_node->get_output_port_type(i));
  658. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  659. type_box->connect("item_selected", this, "_change_output_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  660. } else {
  661. Label *label = memnew(Label);
  662. label->set_text(name_right);
  663. label->add_style_override("normal", label_style); //more compact
  664. hb->add_child(label);
  665. }
  666. }
  667. }
  668. if (valid_right && edit_type->get_selected() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  669. TextureButton *preview = memnew(TextureButton);
  670. preview->set_toggle_mode(true);
  671. preview->set_normal_texture(get_icon("GuiVisibilityHidden", "EditorIcons"));
  672. preview->set_pressed_texture(get_icon("GuiVisibilityVisible", "EditorIcons"));
  673. preview->set_v_size_flags(SIZE_SHRINK_CENTER);
  674. if (vsnode->get_output_port_for_preview() == i) {
  675. preview->set_pressed(true);
  676. }
  677. preview->connect("pressed", this, "_preview_select_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  678. hb->add_child(preview);
  679. }
  680. if (is_group) {
  681. offset = memnew(Control);
  682. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  683. node->add_child(offset);
  684. port_offset++;
  685. }
  686. node->add_child(hb);
  687. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  688. }
  689. if (vsnode->get_output_port_for_preview() >= 0) {
  690. int port_type = vsnode->get_output_port_type(vsnode->get_output_port_for_preview());
  691. if (port_type != VisualShaderNode::PORT_TYPE_TRANSFORM && port_type != VisualShaderNode::PORT_TYPE_SAMPLER) {
  692. offset = memnew(Control);
  693. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  694. node->add_child(offset);
  695. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  696. port_preview->setup(visual_shader, type, nodes[n_i], vsnode->get_output_port_for_preview());
  697. port_preview->set_h_size_flags(SIZE_SHRINK_CENTER);
  698. node->add_child(port_preview);
  699. }
  700. }
  701. offset = memnew(Control);
  702. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  703. node->add_child(offset);
  704. String error = vsnode->get_warning(visual_shader->get_mode(), type);
  705. if (error != String()) {
  706. Label *error_label = memnew(Label);
  707. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  708. error_label->set_text(error);
  709. node->add_child(error_label);
  710. }
  711. if (is_expression) {
  712. TextEdit *expression_box = memnew(TextEdit);
  713. expression_node->set_control(expression_box, 0);
  714. node->add_child(expression_box);
  715. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  716. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  717. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  718. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  719. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  720. expression_box->set_syntax_coloring(true);
  721. expression_box->add_color_override("background_color", background_color);
  722. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  723. expression_box->add_keyword_color(E->get(), keyword_color);
  724. }
  725. expression_box->add_font_override("font", get_font("expression", "EditorFonts"));
  726. expression_box->add_color_override("font_color", text_color);
  727. expression_box->add_color_override("symbol_color", symbol_color);
  728. expression_box->add_color_region("/*", "*/", comment_color, false);
  729. expression_box->add_color_region("//", "", comment_color, false);
  730. expression_box->set_text(expression);
  731. expression_box->set_context_menu_enabled(false);
  732. expression_box->set_show_line_numbers(true);
  733. expression_box->connect("focus_exited", this, "_expression_focus_out", varray(expression_box, nodes[n_i]));
  734. }
  735. if (!uniform.is_valid()) {
  736. graph->add_child(node);
  737. _update_created_node(node);
  738. if (is_group)
  739. call_deferred("_set_node_size", (int)type, nodes[n_i], size);
  740. }
  741. }
  742. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  743. int from = E->get().from_node;
  744. int from_idx = E->get().from_port;
  745. int to = E->get().to_node;
  746. int to_idx = E->get().to_port;
  747. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  748. }
  749. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  750. graph->set_minimap_opacity(graph_minimap_opacity);
  751. }
  752. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  753. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  754. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  755. if (node.is_null()) {
  756. return;
  757. }
  758. undo_redo->create_action(TTR("Add input port"));
  759. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  760. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  761. undo_redo->add_do_method(this, "_update_graph");
  762. undo_redo->add_undo_method(this, "_update_graph");
  763. undo_redo->add_do_method(this, "_rebuild");
  764. undo_redo->add_undo_method(this, "_rebuild");
  765. undo_redo->commit_action();
  766. }
  767. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  768. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  769. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  770. if (node.is_null()) {
  771. return;
  772. }
  773. undo_redo->create_action(TTR("Add output port"));
  774. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  775. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  776. undo_redo->add_do_method(this, "_update_graph");
  777. undo_redo->add_undo_method(this, "_update_graph");
  778. undo_redo->add_do_method(this, "_rebuild");
  779. undo_redo->add_undo_method(this, "_rebuild");
  780. undo_redo->commit_action();
  781. }
  782. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  783. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  784. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  785. if (node.is_null()) {
  786. return;
  787. }
  788. undo_redo->create_action(TTR("Change input port type"));
  789. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  790. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  791. undo_redo->add_do_method(this, "_update_graph");
  792. undo_redo->add_undo_method(this, "_update_graph");
  793. undo_redo->add_do_method(this, "_rebuild");
  794. undo_redo->add_undo_method(this, "_rebuild");
  795. undo_redo->commit_action();
  796. }
  797. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  798. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  799. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  800. if (node.is_null()) {
  801. return;
  802. }
  803. undo_redo->create_action(TTR("Change output port type"));
  804. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  805. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  806. undo_redo->add_do_method(this, "_update_graph");
  807. undo_redo->add_undo_method(this, "_update_graph");
  808. undo_redo->add_do_method(this, "_rebuild");
  809. undo_redo->add_undo_method(this, "_rebuild");
  810. undo_redo->commit_action();
  811. }
  812. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  813. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  814. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  815. ERR_FAIL_COND(!node.is_valid());
  816. undo_redo->create_action(TTR("Change input port name"));
  817. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, p_text);
  818. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  819. undo_redo->add_do_method(this, "_rebuild");
  820. undo_redo->add_undo_method(this, "_rebuild");
  821. undo_redo->commit_action();
  822. }
  823. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  824. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  825. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  826. ERR_FAIL_COND(!node.is_valid());
  827. undo_redo->create_action(TTR("Change output port name"));
  828. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, p_text);
  829. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, node->get_output_port_name(p_port_id));
  830. undo_redo->add_do_method(this, "_rebuild");
  831. undo_redo->add_undo_method(this, "_rebuild");
  832. undo_redo->commit_action();
  833. }
  834. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  835. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  836. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  837. if (node.is_null()) {
  838. return;
  839. }
  840. undo_redo->create_action(TTR("Remove input port"));
  841. List<VisualShader::Connection> conns;
  842. visual_shader->get_node_connections(type, &conns);
  843. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  844. int from_node = E->get().from_node;
  845. int from_port = E->get().from_port;
  846. int to_node = E->get().to_node;
  847. int to_port = E->get().to_port;
  848. if (to_node == p_node) {
  849. if (to_port == p_port) {
  850. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  851. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  852. } else if (to_port > p_port) {
  853. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  854. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  855. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  856. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  857. }
  858. }
  859. }
  860. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  861. undo_redo->add_undo_method(node.ptr(), "add_input_port", p_port, (int)node->get_input_port_type(p_port), node->get_input_port_name(p_port));
  862. undo_redo->add_do_method(this, "_update_graph");
  863. undo_redo->add_undo_method(this, "_update_graph");
  864. undo_redo->add_do_method(this, "_rebuild");
  865. undo_redo->add_undo_method(this, "_rebuild");
  866. undo_redo->commit_action();
  867. }
  868. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  869. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  870. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  871. if (node.is_null()) {
  872. return;
  873. }
  874. undo_redo->create_action(TTR("Remove output port"));
  875. List<VisualShader::Connection> conns;
  876. visual_shader->get_node_connections(type, &conns);
  877. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  878. int from_node = E->get().from_node;
  879. int from_port = E->get().from_port;
  880. int to_node = E->get().to_node;
  881. int to_port = E->get().to_port;
  882. if (from_node == p_node) {
  883. if (from_port == p_port) {
  884. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  885. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  886. } else if (from_port > p_port) {
  887. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  888. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  889. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  890. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  891. }
  892. }
  893. }
  894. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  895. undo_redo->add_undo_method(node.ptr(), "add_output_port", p_port, (int)node->get_output_port_type(p_port), node->get_output_port_name(p_port));
  896. undo_redo->add_do_method(this, "_update_graph");
  897. undo_redo->add_undo_method(this, "_update_graph");
  898. undo_redo->add_do_method(this, "_rebuild");
  899. undo_redo->add_undo_method(this, "_rebuild");
  900. undo_redo->commit_action();
  901. }
  902. void VisualShaderEditor::_expression_focus_out(Object *text_edit, int p_node) {
  903. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  904. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  905. if (node.is_null()) {
  906. return;
  907. }
  908. TextEdit *expression_box = Object::cast_to<TextEdit>(text_edit);
  909. if (node->get_expression() == expression_box->get_text())
  910. return;
  911. undo_redo->create_action(TTR("Set expression"));
  912. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  913. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  914. undo_redo->add_do_method(this, "_rebuild");
  915. undo_redo->add_undo_method(this, "_rebuild");
  916. undo_redo->commit_action();
  917. }
  918. void VisualShaderEditor::_rebuild() {
  919. if (visual_shader != NULL) {
  920. EditorNode::get_singleton()->get_log()->clear();
  921. visual_shader->rebuild();
  922. }
  923. }
  924. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  925. VisualShader::Type type = VisualShader::Type(p_type);
  926. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  927. if (node.is_null()) {
  928. return;
  929. }
  930. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  931. if (group_node.is_null()) {
  932. return;
  933. }
  934. Vector2 size = p_size;
  935. group_node->set_size(size);
  936. GraphNode *gn = NULL;
  937. if (edit_type->get_selected() == p_type) { // check - otherwise the error will be emitted
  938. Node *node2 = graph->get_node(itos(p_node));
  939. gn = Object::cast_to<GraphNode>(node2);
  940. if (!gn)
  941. return;
  942. gn->set_custom_minimum_size(size);
  943. gn->set_size(Size2(1, 1));
  944. }
  945. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  946. if (!expression_node.is_null()) {
  947. Control *text_box = expression_node->get_control(0);
  948. Size2 box_size = size;
  949. if (gn != NULL) {
  950. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  951. box_size.x = gn->get_size().x;
  952. }
  953. }
  954. box_size.x -= text_box->get_margin(MARGIN_LEFT);
  955. box_size.x -= 28 * EDSCALE;
  956. box_size.y -= text_box->get_margin(MARGIN_TOP);
  957. box_size.y -= 28 * EDSCALE;
  958. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  959. text_box->set_size(Size2(1, 1));
  960. }
  961. }
  962. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  963. VisualShader::Type type = VisualShader::Type(p_type);
  964. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  965. if (node.is_null()) {
  966. return;
  967. }
  968. undo_redo->create_action(TTR("Resize VisualShader node"), UndoRedo::MERGE_ENDS);
  969. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  970. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  971. undo_redo->commit_action();
  972. }
  973. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  974. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  975. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  976. if (node.is_null()) {
  977. return;
  978. }
  979. if (node->get_output_port_for_preview() == p_port) {
  980. p_port = -1; //toggle it
  981. }
  982. undo_redo->create_action(TTR("Set Uniform Name"));
  983. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  984. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", node->get_output_port_for_preview());
  985. undo_redo->add_do_method(this, "_update_graph");
  986. undo_redo->add_undo_method(this, "_update_graph");
  987. undo_redo->commit_action();
  988. }
  989. void VisualShaderEditor::_line_edit_changed(const String &p_text, Object *line_edit, int p_node_id) {
  990. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  991. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  992. ERR_FAIL_COND(!node.is_valid());
  993. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  994. updating = true;
  995. undo_redo->create_action(TTR("Set Uniform Name"));
  996. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  997. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  998. undo_redo->add_do_method(this, "_update_graph");
  999. undo_redo->add_undo_method(this, "_update_graph");
  1000. undo_redo->commit_action();
  1001. updating = false;
  1002. Object::cast_to<LineEdit>(line_edit)->set_text(validated_name);
  1003. }
  1004. void VisualShaderEditor::_line_edit_focus_out(Object *line_edit, int p_node_id) {
  1005. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1006. _line_edit_changed(text, line_edit, p_node_id);
  1007. }
  1008. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  1009. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1010. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1011. ERR_FAIL_COND(!node.is_valid());
  1012. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1013. if (!p_output) {
  1014. if (node->get_input_port_name(p_port_id) == text)
  1015. return;
  1016. } else {
  1017. if (node->get_output_port_name(p_port_id) == text)
  1018. return;
  1019. }
  1020. List<String> input_names;
  1021. List<String> output_names;
  1022. for (int i = 0; i < node->get_input_port_count(); i++) {
  1023. if (!p_output && i == p_port_id)
  1024. continue;
  1025. input_names.push_back(node->get_input_port_name(i));
  1026. }
  1027. for (int i = 0; i < node->get_output_port_count(); i++) {
  1028. if (p_output && i == p_port_id)
  1029. continue;
  1030. output_names.push_back(node->get_output_port_name(i));
  1031. }
  1032. String validated_name = visual_shader->validate_port_name(text, input_names, output_names);
  1033. if (validated_name == "") {
  1034. if (!p_output) {
  1035. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_input_port_name(p_port_id));
  1036. } else {
  1037. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_output_port_name(p_port_id));
  1038. }
  1039. return;
  1040. }
  1041. if (!p_output) {
  1042. _change_input_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1043. } else {
  1044. _change_output_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1045. }
  1046. }
  1047. void VisualShaderEditor::_port_edited() {
  1048. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1049. Variant value = property_editor->get_variant();
  1050. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1051. ERR_FAIL_COND(!vsn.is_valid());
  1052. undo_redo->create_action(TTR("Set Input Default Port"));
  1053. Ref<VisualShaderNodeCustom> custom = Object::cast_to<VisualShaderNodeCustom>(vsn.ptr());
  1054. if (custom.is_valid()) {
  1055. undo_redo->add_do_method(custom.ptr(), "_set_input_port_default_value", editing_port, value);
  1056. undo_redo->add_undo_method(custom.ptr(), "_set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1057. } else {
  1058. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1059. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1060. }
  1061. undo_redo->add_do_method(this, "_update_graph");
  1062. undo_redo->add_undo_method(this, "_update_graph");
  1063. undo_redo->commit_action();
  1064. property_editor->hide();
  1065. }
  1066. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1067. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1068. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1069. Button *button = Object::cast_to<Button>(p_button);
  1070. ERR_FAIL_COND(!button);
  1071. Variant value = vsn->get_input_port_default_value(p_port);
  1072. property_editor->set_global_position(button->get_global_position() + Vector2(0, button->get_size().height));
  1073. property_editor->edit(NULL, "", value.get_type(), value, 0, "");
  1074. property_editor->popup();
  1075. editing_node = p_node;
  1076. editing_port = p_port;
  1077. }
  1078. void VisualShaderEditor::_add_custom_node(const String &p_path) {
  1079. int idx = -1;
  1080. for (int i = custom_node_option_idx; i < add_options.size(); i++) {
  1081. if (add_options[i].script.is_valid()) {
  1082. if (add_options[i].script->get_path() == p_path) {
  1083. idx = i;
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. if (idx != -1) {
  1089. _add_node(idx);
  1090. }
  1091. }
  1092. void VisualShaderEditor::_add_texture_node(const String &p_path) {
  1093. VisualShaderNodeTexture *texture = (VisualShaderNodeTexture *)_add_node(texture_node_option_idx, -1);
  1094. texture->set_texture(ResourceLoader::load(p_path));
  1095. }
  1096. VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) {
  1097. ERR_FAIL_INDEX_V(p_idx, add_options.size(), NULL);
  1098. Ref<VisualShaderNode> vsnode;
  1099. bool is_custom = add_options[p_idx].is_custom;
  1100. if (!is_custom && add_options[p_idx].type != String()) {
  1101. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  1102. ERR_FAIL_COND_V(!vsn, NULL);
  1103. VisualShaderNodeScalarConstant *constant = Object::cast_to<VisualShaderNodeScalarConstant>(vsn);
  1104. if (constant) {
  1105. if ((int)add_options[p_idx].value != -1)
  1106. constant->set_constant(add_options[p_idx].value);
  1107. }
  1108. if (p_op_idx != -1) {
  1109. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1110. if (input) {
  1111. input->set_input_name(add_options[p_idx].sub_func_str);
  1112. }
  1113. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(vsn);
  1114. if (is) {
  1115. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1116. }
  1117. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(vsn);
  1118. if (cmp) {
  1119. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1120. }
  1121. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(vsn);
  1122. if (colorOp) {
  1123. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1124. }
  1125. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(vsn);
  1126. if (colorFunc) {
  1127. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1128. }
  1129. VisualShaderNodeScalarOp *scalarOp = Object::cast_to<VisualShaderNodeScalarOp>(vsn);
  1130. if (scalarOp) {
  1131. scalarOp->set_operator((VisualShaderNodeScalarOp::Operator)p_op_idx);
  1132. }
  1133. VisualShaderNodeScalarFunc *scalarFunc = Object::cast_to<VisualShaderNodeScalarFunc>(vsn);
  1134. if (scalarFunc) {
  1135. scalarFunc->set_function((VisualShaderNodeScalarFunc::Function)p_op_idx);
  1136. }
  1137. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(vsn);
  1138. if (vecOp) {
  1139. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1140. }
  1141. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(vsn);
  1142. if (vecFunc) {
  1143. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1144. }
  1145. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(vsn);
  1146. if (matFunc) {
  1147. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1148. }
  1149. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(vsn);
  1150. if (sderFunc) {
  1151. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1152. }
  1153. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(vsn);
  1154. if (vderFunc) {
  1155. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1156. }
  1157. }
  1158. vsnode = Ref<VisualShaderNode>(vsn);
  1159. } else {
  1160. ERR_FAIL_COND_V(add_options[p_idx].script.is_null(), NULL);
  1161. String base_type = add_options[p_idx].script->get_instance_base_type();
  1162. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  1163. ERR_FAIL_COND_V(!vsn, NULL);
  1164. vsnode = Ref<VisualShaderNode>(vsn);
  1165. vsnode->set_script(add_options[p_idx].script.get_ref_ptr());
  1166. }
  1167. Point2 position = graph->get_scroll_ofs();
  1168. if (saved_node_pos_dirty) {
  1169. position += saved_node_pos;
  1170. } else {
  1171. position += graph->get_size() * 0.5;
  1172. position /= EDSCALE;
  1173. }
  1174. saved_node_pos_dirty = false;
  1175. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1176. int id_to_use = visual_shader->get_valid_node_id(type);
  1177. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1178. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1179. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1180. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1181. if (expr) {
  1182. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1183. }
  1184. if (to_node != -1 && to_slot != -1) {
  1185. if (vsnode->get_output_port_count() > 0) {
  1186. int _from_node = id_to_use;
  1187. int _from_slot = 0;
  1188. if (visual_shader->is_port_types_compatible(vsnode->get_output_port_type(_from_slot), visual_shader->get_node(type, to_node)->get_input_port_type(to_slot))) {
  1189. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1190. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1191. }
  1192. }
  1193. } else if (from_node != -1 && from_slot != -1) {
  1194. if (vsnode->get_input_port_count() > 0) {
  1195. int _to_node = id_to_use;
  1196. int _to_slot = 0;
  1197. if (visual_shader->is_port_types_compatible(visual_shader->get_node(type, from_node)->get_output_port_type(from_slot), vsnode->get_input_port_type(_to_slot))) {
  1198. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1199. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1200. }
  1201. }
  1202. }
  1203. undo_redo->add_do_method(this, "_update_graph");
  1204. undo_redo->add_undo_method(this, "_update_graph");
  1205. undo_redo->commit_action();
  1206. return vsnode.ptr();
  1207. }
  1208. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  1209. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1210. drag_buffer.push_back({ type, p_node, p_from, p_to });
  1211. if (!drag_dirty) {
  1212. call_deferred("_nodes_dragged");
  1213. }
  1214. drag_dirty = true;
  1215. }
  1216. void VisualShaderEditor::_nodes_dragged() {
  1217. drag_dirty = false;
  1218. undo_redo->create_action(TTR("Node(s) Moved"));
  1219. for (List<DragOp>::Element *E = drag_buffer.front(); E; E = E->next()) {
  1220. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1221. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1222. }
  1223. updating = true;
  1224. undo_redo->add_do_method(this, "_update_graph");
  1225. undo_redo->add_undo_method(this, "_update_graph");
  1226. drag_buffer.clear();
  1227. undo_redo->commit_action();
  1228. updating = false;
  1229. }
  1230. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1231. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1232. int from = p_from.to_int();
  1233. int to = p_to.to_int();
  1234. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  1235. return;
  1236. }
  1237. undo_redo->create_action(TTR("Nodes Connected"));
  1238. List<VisualShader::Connection> conns;
  1239. visual_shader->get_node_connections(type, &conns);
  1240. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1241. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  1242. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1243. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1244. }
  1245. }
  1246. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1247. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1248. undo_redo->add_do_method(this, "_update_graph");
  1249. undo_redo->add_undo_method(this, "_update_graph");
  1250. undo_redo->commit_action();
  1251. }
  1252. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1253. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  1254. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1255. int from = p_from.to_int();
  1256. int to = p_to.to_int();
  1257. //updating = true; seems graph edit can handle this, no need to protect
  1258. undo_redo->create_action(TTR("Nodes Disconnected"));
  1259. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1260. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1261. undo_redo->add_do_method(this, "_update_graph");
  1262. undo_redo->add_undo_method(this, "_update_graph");
  1263. undo_redo->commit_action();
  1264. //updating = false;
  1265. }
  1266. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  1267. from_node = p_from.to_int();
  1268. from_slot = p_from_slot;
  1269. _show_members_dialog(true);
  1270. }
  1271. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  1272. to_node = p_to.to_int();
  1273. to_slot = p_to_slot;
  1274. _show_members_dialog(true);
  1275. }
  1276. void VisualShaderEditor::_delete_request(int which) {
  1277. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1278. Ref<VisualShaderNode> node = Ref<VisualShaderNode>(visual_shader->get_node(type, which));
  1279. undo_redo->create_action(TTR("Delete Node"));
  1280. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  1281. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, which), which);
  1282. undo_redo->add_do_method(this, "_clear_buffer");
  1283. undo_redo->add_undo_method(this, "_clear_buffer");
  1284. // restore size, inputs and outputs if node is group
  1285. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1286. if (group) {
  1287. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1288. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1289. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1290. }
  1291. // restore expression text if node is expression
  1292. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1293. if (expression) {
  1294. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1295. }
  1296. List<VisualShader::Connection> conns;
  1297. visual_shader->get_node_connections(type, &conns);
  1298. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1299. if (E->get().from_node == which || E->get().to_node == which) {
  1300. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1301. }
  1302. }
  1303. undo_redo->add_do_method(this, "_update_graph");
  1304. undo_redo->add_undo_method(this, "_update_graph");
  1305. undo_redo->commit_action();
  1306. }
  1307. void VisualShaderEditor::_node_selected(Object *p_node) {
  1308. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1309. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  1310. ERR_FAIL_COND(!gn);
  1311. int id = String(gn->get_name()).to_int();
  1312. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  1313. ERR_FAIL_COND(!vsnode.is_valid());
  1314. //do not rely on this, makes editor more complex
  1315. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  1316. }
  1317. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1318. Ref<InputEventMouseButton> mb = p_event;
  1319. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT)
  1320. _show_members_dialog(true);
  1321. }
  1322. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  1323. if (at_mouse_pos) {
  1324. saved_node_pos_dirty = true;
  1325. saved_node_pos = graph->get_local_mouse_position();
  1326. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1327. members_dialog->popup();
  1328. members_dialog->set_position(gpos);
  1329. } else {
  1330. members_dialog->popup();
  1331. saved_node_pos_dirty = false;
  1332. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  1333. }
  1334. // keep dialog within window bounds
  1335. Size2 window_size = OS::get_singleton()->get_window_size();
  1336. Rect2 dialog_rect = members_dialog->get_global_rect();
  1337. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  1338. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  1339. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  1340. }
  1341. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  1342. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  1343. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  1344. }
  1345. node_filter->call_deferred("grab_focus"); // still not visible
  1346. node_filter->select_all();
  1347. }
  1348. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  1349. Ref<InputEventKey> ie = p_ie;
  1350. if (ie.is_valid() && (ie->get_scancode() == KEY_UP ||
  1351. ie->get_scancode() == KEY_DOWN ||
  1352. ie->get_scancode() == KEY_ENTER ||
  1353. ie->get_scancode() == KEY_KP_ENTER)) {
  1354. members->call("_gui_input", ie);
  1355. node_filter->accept_event();
  1356. }
  1357. }
  1358. void VisualShaderEditor::_notification(int p_what) {
  1359. if (p_what == NOTIFICATION_ENTER_TREE) {
  1360. node_filter->set_clear_button_enabled(true);
  1361. // collapse tree by default
  1362. TreeItem *category = members->get_root()->get_children();
  1363. while (category) {
  1364. category->set_collapsed(true);
  1365. TreeItem *sub_category = category->get_children();
  1366. while (sub_category) {
  1367. sub_category->set_collapsed(true);
  1368. sub_category = sub_category->get_next();
  1369. }
  1370. category = category->get_next();
  1371. }
  1372. }
  1373. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1374. Dictionary dd = get_viewport()->gui_get_drag_data();
  1375. if (members->is_visible_in_tree() && dd.has("id")) {
  1376. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1377. }
  1378. } else if (p_what == NOTIFICATION_DRAG_END) {
  1379. members->set_drop_mode_flags(0);
  1380. }
  1381. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1382. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  1383. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  1384. node_filter->set_right_icon(Control::get_icon("Search", "EditorIcons"));
  1385. preview_shader->set_icon(Control::get_icon("Shader", "EditorIcons"));
  1386. {
  1387. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  1388. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  1389. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  1390. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  1391. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  1392. preview_text->add_color_override("background_color", background_color);
  1393. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  1394. preview_text->add_keyword_color(E->get(), keyword_color);
  1395. }
  1396. preview_text->add_font_override("font", get_font("expression", "EditorFonts"));
  1397. preview_text->add_color_override("font_color", text_color);
  1398. preview_text->add_color_override("symbol_color", symbol_color);
  1399. preview_text->add_color_region("/*", "*/", comment_color, false);
  1400. preview_text->add_color_region("//", "", comment_color, false);
  1401. error_text->add_font_override("font", get_font("status_source", "EditorFonts"));
  1402. error_text->add_color_override("font_color", get_color("error_color", "Editor"));
  1403. }
  1404. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Tools", "EditorIcons"));
  1405. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())
  1406. _update_graph();
  1407. }
  1408. }
  1409. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  1410. if (updating)
  1411. return;
  1412. updating = true;
  1413. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  1414. updating = false;
  1415. }
  1416. void VisualShaderEditor::_node_changed(int p_id) {
  1417. if (updating)
  1418. return;
  1419. if (is_visible_in_tree()) {
  1420. _update_graph();
  1421. }
  1422. }
  1423. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  1424. r_excluded.clear();
  1425. VisualShader::Type type = (VisualShader::Type)p_type;
  1426. for (int i = 0; i < graph->get_child_count(); i++) {
  1427. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1428. if (gn) {
  1429. int id = String(gn->get_name()).to_int();
  1430. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1431. Ref<VisualShaderNodeOutput> output = node;
  1432. if (output.is_valid()) {
  1433. r_excluded.insert(id);
  1434. continue;
  1435. }
  1436. r_excluded.insert(id);
  1437. }
  1438. }
  1439. }
  1440. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  1441. VisualShader::Type type = (VisualShader::Type)p_type;
  1442. selection_center.x = 0.0f;
  1443. selection_center.y = 0.0f;
  1444. for (int i = 0; i < graph->get_child_count(); i++) {
  1445. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1446. if (gn) {
  1447. int id = String(gn->get_name()).to_int();
  1448. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1449. Ref<VisualShaderNodeOutput> output = node;
  1450. if (output.is_valid()) { // can't duplicate output
  1451. r_excluded.insert(id);
  1452. continue;
  1453. }
  1454. if (node.is_valid() && gn->is_selected()) {
  1455. Vector2 pos = visual_shader->get_node_position(type, id);
  1456. selection_center += pos;
  1457. r_nodes.push_back(id);
  1458. }
  1459. r_excluded.insert(id);
  1460. }
  1461. }
  1462. selection_center /= (float)r_nodes.size();
  1463. }
  1464. void VisualShaderEditor::_dup_paste_nodes(int p_type, int p_pasted_type, List<int> &r_nodes, Set<int> &r_excluded, const Vector2 &p_offset, bool p_select) {
  1465. VisualShader::Type type = (VisualShader::Type)p_type;
  1466. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  1467. int base_id = visual_shader->get_valid_node_id(type);
  1468. int id_from = base_id;
  1469. Map<int, int> connection_remap;
  1470. Set<int> unsupported_set;
  1471. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1472. connection_remap[E->get()] = id_from;
  1473. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  1474. bool unsupported = false;
  1475. for (int i = 0; i < add_options.size(); i++) {
  1476. if (add_options[i].type == node->get_class_name()) {
  1477. if (!_is_available(add_options[i].mode)) {
  1478. unsupported = true;
  1479. }
  1480. break;
  1481. }
  1482. }
  1483. if (unsupported) {
  1484. unsupported_set.insert(E->get());
  1485. continue;
  1486. }
  1487. Ref<VisualShaderNode> dupli = node->duplicate();
  1488. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, dupli, visual_shader->get_node_position(pasted_type, E->get()) + p_offset, id_from);
  1489. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  1490. // duplicate size, inputs and outputs if node is group
  1491. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1492. if (!group.is_null()) {
  1493. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  1494. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  1495. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  1496. }
  1497. // duplicate expression text if node is expression
  1498. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1499. if (!expression.is_null()) {
  1500. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  1501. }
  1502. id_from++;
  1503. }
  1504. List<VisualShader::Connection> conns;
  1505. visual_shader->get_node_connections(pasted_type, &conns);
  1506. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1507. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  1508. continue;
  1509. }
  1510. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  1511. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1512. }
  1513. }
  1514. undo_redo->add_do_method(this, "_update_graph");
  1515. undo_redo->add_undo_method(this, "_update_graph");
  1516. undo_redo->commit_action();
  1517. if (p_select) {
  1518. // reselect duplicated nodes by excluding the other ones
  1519. for (int i = 0; i < graph->get_child_count(); i++) {
  1520. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1521. if (gn) {
  1522. int id = String(gn->get_name()).to_int();
  1523. if (!r_excluded.has(id)) {
  1524. gn->set_selected(true);
  1525. } else {
  1526. gn->set_selected(false);
  1527. }
  1528. }
  1529. }
  1530. }
  1531. }
  1532. void VisualShaderEditor::_clear_buffer() {
  1533. copy_nodes_buffer.clear();
  1534. copy_nodes_excluded_buffer.clear();
  1535. }
  1536. void VisualShaderEditor::_duplicate_nodes() {
  1537. int type = edit_type->get_selected();
  1538. List<int> nodes;
  1539. Set<int> excluded;
  1540. _dup_copy_nodes(type, nodes, excluded);
  1541. if (nodes.empty())
  1542. return;
  1543. undo_redo->create_action(TTR("Duplicate Nodes"));
  1544. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  1545. }
  1546. void VisualShaderEditor::_copy_nodes() {
  1547. copy_type = edit_type->get_selected();
  1548. _clear_buffer();
  1549. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  1550. }
  1551. void VisualShaderEditor::_paste_nodes() {
  1552. if (copy_nodes_buffer.empty())
  1553. return;
  1554. int type = edit_type->get_selected();
  1555. undo_redo->create_action(TTR("Paste Nodes"));
  1556. float scale = graph->get_zoom();
  1557. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + graph->get_local_mouse_position() / scale - selection_center), false);
  1558. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  1559. }
  1560. void VisualShaderEditor::_on_nodes_delete() {
  1561. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1562. List<int> to_erase;
  1563. for (int i = 0; i < graph->get_child_count(); i++) {
  1564. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1565. if (gn) {
  1566. if (gn->is_selected() && gn->is_close_button_visible()) {
  1567. to_erase.push_back(gn->get_name().operator String().to_int());
  1568. }
  1569. }
  1570. }
  1571. if (to_erase.empty())
  1572. return;
  1573. undo_redo->create_action(TTR("Delete Nodes"));
  1574. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1575. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  1576. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  1577. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  1578. undo_redo->add_do_method(this, "_clear_buffer");
  1579. undo_redo->add_undo_method(this, "_clear_buffer");
  1580. // restore size, inputs and outputs if node is group
  1581. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1582. if (group) {
  1583. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1584. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1585. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1586. }
  1587. // restore expression text if node is expression
  1588. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1589. if (expression) {
  1590. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1591. }
  1592. }
  1593. List<VisualShader::Connection> conns;
  1594. visual_shader->get_node_connections(type, &conns);
  1595. List<VisualShader::Connection> used_conns;
  1596. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1597. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1598. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1599. bool cancel = false;
  1600. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  1601. if (R->get().from_node == E->get().from_node && R->get().from_port == E->get().from_port && R->get().to_node == E->get().to_node && R->get().to_port == E->get().to_port) {
  1602. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  1603. break;
  1604. }
  1605. }
  1606. if (!cancel) {
  1607. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1608. used_conns.push_back(E->get());
  1609. }
  1610. }
  1611. }
  1612. }
  1613. undo_redo->add_do_method(this, "_update_graph");
  1614. undo_redo->add_undo_method(this, "_update_graph");
  1615. undo_redo->commit_action();
  1616. }
  1617. void VisualShaderEditor::_mode_selected(int p_id) {
  1618. _update_options_menu();
  1619. _update_graph();
  1620. }
  1621. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> input, String name) {
  1622. String prev_name = input->get_input_name();
  1623. if (name == prev_name)
  1624. return;
  1625. bool type_changed = input->get_input_type_by_name(name) != input->get_input_type_by_name(prev_name);
  1626. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1627. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  1628. undo_redo->add_do_method(input.ptr(), "set_input_name", name);
  1629. undo_redo->add_undo_method(input.ptr(), "set_input_name", prev_name);
  1630. if (type_changed) {
  1631. //restore connections if type changed
  1632. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1633. int id = visual_shader->find_node_id(type, input);
  1634. List<VisualShader::Connection> conns;
  1635. visual_shader->get_node_connections(type, &conns);
  1636. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1637. if (E->get().from_node == id) {
  1638. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1639. }
  1640. }
  1641. }
  1642. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1643. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1644. undo_redo->commit_action();
  1645. }
  1646. void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform_ref, String p_name) {
  1647. String prev_name = p_uniform_ref->get_uniform_name();
  1648. if (p_name == prev_name) {
  1649. return;
  1650. }
  1651. bool type_changed = p_uniform_ref->get_uniform_type_by_name(p_name) != p_uniform_ref->get_uniform_type_by_name(prev_name);
  1652. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1653. undo_redo->create_action(TTR("UniformRef Name Changed"));
  1654. undo_redo->add_do_method(p_uniform_ref.ptr(), "set_uniform_name", p_name);
  1655. undo_redo->add_undo_method(p_uniform_ref.ptr(), "set_uniform_name", prev_name);
  1656. if (type_changed) {
  1657. //restore connections if type changed
  1658. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1659. int id = visual_shader->find_node_id(type, p_uniform_ref);
  1660. List<VisualShader::Connection> conns;
  1661. visual_shader->get_node_connections(type, &conns);
  1662. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1663. if (E->get().from_node == id) {
  1664. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1665. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1666. }
  1667. }
  1668. }
  1669. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1670. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1671. undo_redo->commit_action();
  1672. }
  1673. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  1674. _update_options_menu();
  1675. }
  1676. void VisualShaderEditor::_member_selected() {
  1677. TreeItem *item = members->get_selected();
  1678. if (item != NULL && item->has_meta("id")) {
  1679. members_dialog->get_ok()->set_disabled(false);
  1680. node_desc->set_text(_get_description(item->get_meta("id")));
  1681. } else {
  1682. members_dialog->get_ok()->set_disabled(true);
  1683. node_desc->set_text("");
  1684. }
  1685. }
  1686. void VisualShaderEditor::_member_unselected() {
  1687. }
  1688. void VisualShaderEditor::_member_create() {
  1689. TreeItem *item = members->get_selected();
  1690. if (item != NULL && item->has_meta("id")) {
  1691. int idx = members->get_selected()->get_meta("id");
  1692. _add_node(idx, add_options[idx].sub_func);
  1693. members_dialog->hide();
  1694. }
  1695. }
  1696. void VisualShaderEditor::_member_cancel() {
  1697. to_node = -1;
  1698. to_slot = -1;
  1699. from_node = -1;
  1700. from_slot = -1;
  1701. }
  1702. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  1703. TreeItem *category = members->get_root()->get_children();
  1704. switch (p_idx) {
  1705. case EXPAND_ALL:
  1706. while (category) {
  1707. category->set_collapsed(false);
  1708. TreeItem *sub_category = category->get_children();
  1709. while (sub_category) {
  1710. sub_category->set_collapsed(false);
  1711. sub_category = sub_category->get_next();
  1712. }
  1713. category = category->get_next();
  1714. }
  1715. break;
  1716. case COLLAPSE_ALL:
  1717. while (category) {
  1718. category->set_collapsed(true);
  1719. TreeItem *sub_category = category->get_children();
  1720. while (sub_category) {
  1721. sub_category->set_collapsed(true);
  1722. sub_category = sub_category->get_next();
  1723. }
  1724. category = category->get_next();
  1725. }
  1726. break;
  1727. default:
  1728. break;
  1729. }
  1730. }
  1731. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1732. if (p_from == members) {
  1733. TreeItem *it = members->get_item_at_position(p_point);
  1734. if (!it)
  1735. return Variant();
  1736. if (!it->has_meta("id"))
  1737. return Variant();
  1738. int id = it->get_meta("id");
  1739. AddOption op = add_options[id];
  1740. Dictionary d;
  1741. d["id"] = id;
  1742. if (op.sub_func == -1) {
  1743. d["sub_func"] = op.sub_func_str;
  1744. } else {
  1745. d["sub_func"] = op.sub_func;
  1746. }
  1747. Label *label = memnew(Label);
  1748. label->set_text(it->get_text(0));
  1749. set_drag_preview(label);
  1750. return d;
  1751. }
  1752. return Variant();
  1753. }
  1754. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1755. if (p_from == graph) {
  1756. Dictionary d = p_data;
  1757. if (d.has("id")) {
  1758. return true;
  1759. }
  1760. if (d.has("files")) {
  1761. return true;
  1762. }
  1763. }
  1764. return false;
  1765. }
  1766. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1767. if (p_from == graph) {
  1768. Dictionary d = p_data;
  1769. if (d.has("id")) {
  1770. int idx = d["id"];
  1771. saved_node_pos = p_point;
  1772. saved_node_pos_dirty = true;
  1773. _add_node(idx, add_options[idx].sub_func);
  1774. } else if (d.has("files")) {
  1775. if (d["files"].get_type() == Variant::POOL_STRING_ARRAY) {
  1776. int j = 0;
  1777. PoolStringArray arr = d["files"];
  1778. for (int i = 0; i < arr.size(); i++) {
  1779. String type = ResourceLoader::get_resource_type(arr[i]);
  1780. if (type == "GDScript") {
  1781. Ref<Script> script = ResourceLoader::load(arr[i]);
  1782. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  1783. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1784. saved_node_pos_dirty = true;
  1785. _add_custom_node(arr[i]);
  1786. j++;
  1787. }
  1788. } else if (ClassDB::get_parent_class(type) == "Texture") {
  1789. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1790. saved_node_pos_dirty = true;
  1791. _add_texture_node(arr[i]);
  1792. j++;
  1793. }
  1794. }
  1795. }
  1796. }
  1797. }
  1798. }
  1799. void VisualShaderEditor::_show_preview_text() {
  1800. preview_showed = !preview_showed;
  1801. preview_vbox->set_visible(preview_showed);
  1802. if (preview_showed) {
  1803. if (pending_update_preview) {
  1804. _update_preview();
  1805. pending_update_preview = false;
  1806. }
  1807. }
  1808. }
  1809. void VisualShaderEditor::_update_preview() {
  1810. if (!preview_showed) {
  1811. pending_update_preview = true;
  1812. return;
  1813. }
  1814. String code = visual_shader->get_code();
  1815. preview_text->set_text(code);
  1816. ShaderLanguage sl;
  1817. Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_types());
  1818. for (int i = 0; i < preview_text->get_line_count(); i++) {
  1819. preview_text->set_line_as_marked(i, false);
  1820. }
  1821. if (err != OK) {
  1822. preview_text->set_line_as_marked(sl.get_error_line() - 1, true);
  1823. error_text->set_visible(true);
  1824. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  1825. error_text->set_text(text);
  1826. shader_error = true;
  1827. } else {
  1828. error_text->set_visible(false);
  1829. shader_error = false;
  1830. }
  1831. }
  1832. void VisualShaderEditor::_bind_methods() {
  1833. ClassDB::bind_method("_rebuild", &VisualShaderEditor::_rebuild);
  1834. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  1835. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  1836. ClassDB::bind_method("_expression_focus_out", &VisualShaderEditor::_expression_focus_out);
  1837. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  1838. ClassDB::bind_method("_node_dragged", &VisualShaderEditor::_node_dragged);
  1839. ClassDB::bind_method("_connection_request", &VisualShaderEditor::_connection_request);
  1840. ClassDB::bind_method("_disconnection_request", &VisualShaderEditor::_disconnection_request);
  1841. ClassDB::bind_method("_node_selected", &VisualShaderEditor::_node_selected);
  1842. ClassDB::bind_method("_scroll_changed", &VisualShaderEditor::_scroll_changed);
  1843. ClassDB::bind_method("_delete_request", &VisualShaderEditor::_delete_request);
  1844. ClassDB::bind_method("_on_nodes_delete", &VisualShaderEditor::_on_nodes_delete);
  1845. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  1846. ClassDB::bind_method("_edit_port_default_input", &VisualShaderEditor::_edit_port_default_input);
  1847. ClassDB::bind_method("_port_edited", &VisualShaderEditor::_port_edited);
  1848. ClassDB::bind_method("_connection_to_empty", &VisualShaderEditor::_connection_to_empty);
  1849. ClassDB::bind_method("_connection_from_empty", &VisualShaderEditor::_connection_from_empty);
  1850. ClassDB::bind_method("_line_edit_focus_out", &VisualShaderEditor::_line_edit_focus_out);
  1851. ClassDB::bind_method("_line_edit_changed", &VisualShaderEditor::_line_edit_changed);
  1852. ClassDB::bind_method("_port_name_focus_out", &VisualShaderEditor::_port_name_focus_out);
  1853. ClassDB::bind_method("_duplicate_nodes", &VisualShaderEditor::_duplicate_nodes);
  1854. ClassDB::bind_method("_copy_nodes", &VisualShaderEditor::_copy_nodes);
  1855. ClassDB::bind_method("_paste_nodes", &VisualShaderEditor::_paste_nodes);
  1856. ClassDB::bind_method("_mode_selected", &VisualShaderEditor::_mode_selected);
  1857. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  1858. ClassDB::bind_method("_uniform_select_item", &VisualShaderEditor::_uniform_select_item);
  1859. ClassDB::bind_method("_preview_select_port", &VisualShaderEditor::_preview_select_port);
  1860. ClassDB::bind_method("_graph_gui_input", &VisualShaderEditor::_graph_gui_input);
  1861. ClassDB::bind_method("_add_input_port", &VisualShaderEditor::_add_input_port);
  1862. ClassDB::bind_method("_change_input_port_type", &VisualShaderEditor::_change_input_port_type);
  1863. ClassDB::bind_method("_change_input_port_name", &VisualShaderEditor::_change_input_port_name);
  1864. ClassDB::bind_method("_remove_input_port", &VisualShaderEditor::_remove_input_port);
  1865. ClassDB::bind_method("_add_output_port", &VisualShaderEditor::_add_output_port);
  1866. ClassDB::bind_method("_change_output_port_type", &VisualShaderEditor::_change_output_port_type);
  1867. ClassDB::bind_method("_change_output_port_name", &VisualShaderEditor::_change_output_port_name);
  1868. ClassDB::bind_method("_remove_output_port", &VisualShaderEditor::_remove_output_port);
  1869. ClassDB::bind_method("_node_resized", &VisualShaderEditor::_node_resized);
  1870. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  1871. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  1872. ClassDB::bind_method("_show_preview_text", &VisualShaderEditor::_show_preview_text);
  1873. ClassDB::bind_method("_update_preview", &VisualShaderEditor::_update_preview);
  1874. ClassDB::bind_method("_nodes_dragged", &VisualShaderEditor::_nodes_dragged);
  1875. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  1876. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  1877. ClassDB::bind_method(D_METHOD("drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  1878. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  1879. ClassDB::bind_method("_tools_menu_option", &VisualShaderEditor::_tools_menu_option);
  1880. ClassDB::bind_method("_show_members_dialog", &VisualShaderEditor::_show_members_dialog);
  1881. ClassDB::bind_method("_sbox_input", &VisualShaderEditor::_sbox_input);
  1882. ClassDB::bind_method("_member_filter_changed", &VisualShaderEditor::_member_filter_changed);
  1883. ClassDB::bind_method("_member_selected", &VisualShaderEditor::_member_selected);
  1884. ClassDB::bind_method("_member_unselected", &VisualShaderEditor::_member_unselected);
  1885. ClassDB::bind_method("_member_create", &VisualShaderEditor::_member_create);
  1886. ClassDB::bind_method("_member_cancel", &VisualShaderEditor::_member_cancel);
  1887. }
  1888. VisualShaderEditor *VisualShaderEditor::singleton = NULL;
  1889. VisualShaderEditor::VisualShaderEditor() {
  1890. singleton = this;
  1891. updating = false;
  1892. saved_node_pos_dirty = false;
  1893. saved_node_pos = Point2(0, 0);
  1894. ShaderLanguage::get_keyword_list(&keyword_list);
  1895. preview_showed = false;
  1896. pending_update_preview = false;
  1897. shader_error = false;
  1898. to_node = -1;
  1899. to_slot = -1;
  1900. from_node = -1;
  1901. from_slot = -1;
  1902. main_box = memnew(HSplitContainer);
  1903. main_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1904. main_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1905. add_child(main_box);
  1906. graph = memnew(GraphEdit);
  1907. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  1908. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1909. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  1910. main_box->add_child(graph);
  1911. graph->set_drag_forwarding(this);
  1912. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  1913. graph->set_minimap_opacity(graph_minimap_opacity);
  1914. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  1915. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  1916. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  1917. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  1918. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  1919. //graph->add_valid_left_disconnect_type(0);
  1920. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1921. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  1922. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  1923. graph->connect("node_selected", this, "_node_selected");
  1924. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  1925. graph->connect("duplicate_nodes_request", this, "_duplicate_nodes");
  1926. graph->connect("copy_nodes_request", this, "_copy_nodes");
  1927. graph->connect("paste_nodes_request", this, "_paste_nodes");
  1928. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  1929. graph->connect("gui_input", this, "_graph_gui_input");
  1930. graph->connect("connection_to_empty", this, "_connection_to_empty");
  1931. graph->connect("connection_from_empty", this, "_connection_from_empty");
  1932. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  1933. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  1934. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1935. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  1936. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  1937. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1938. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  1939. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  1940. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1941. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  1942. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  1943. VSeparator *vs = memnew(VSeparator);
  1944. graph->get_zoom_hbox()->add_child(vs);
  1945. graph->get_zoom_hbox()->move_child(vs, 0);
  1946. edit_type = memnew(OptionButton);
  1947. edit_type->add_item(TTR("Vertex"));
  1948. edit_type->add_item(TTR("Fragment"));
  1949. edit_type->add_item(TTR("Light"));
  1950. edit_type->select(1);
  1951. edit_type->connect("item_selected", this, "_mode_selected");
  1952. graph->get_zoom_hbox()->add_child(edit_type);
  1953. graph->get_zoom_hbox()->move_child(edit_type, 0);
  1954. add_node = memnew(ToolButton);
  1955. graph->get_zoom_hbox()->add_child(add_node);
  1956. add_node->set_text(TTR("Add Node..."));
  1957. graph->get_zoom_hbox()->move_child(add_node, 0);
  1958. add_node->connect("pressed", this, "_show_members_dialog", varray(false));
  1959. preview_shader = memnew(ToolButton);
  1960. preview_shader->set_toggle_mode(true);
  1961. preview_shader->set_tooltip(TTR("Show resulted shader code."));
  1962. graph->get_zoom_hbox()->add_child(preview_shader);
  1963. preview_shader->connect("pressed", this, "_show_preview_text");
  1964. ///////////////////////////////////////
  1965. // PREVIEW PANEL
  1966. ///////////////////////////////////////
  1967. preview_vbox = memnew(VBoxContainer);
  1968. preview_vbox->set_visible(preview_showed);
  1969. main_box->add_child(preview_vbox);
  1970. preview_text = memnew(TextEdit);
  1971. preview_vbox->add_child(preview_text);
  1972. preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
  1973. preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
  1974. preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  1975. preview_text->set_syntax_coloring(true);
  1976. preview_text->set_show_line_numbers(true);
  1977. preview_text->set_readonly(true);
  1978. error_text = memnew(Label);
  1979. preview_vbox->add_child(error_text);
  1980. error_text->set_visible(false);
  1981. ///////////////////////////////////////
  1982. // SHADER NODES TREE
  1983. ///////////////////////////////////////
  1984. VBoxContainer *members_vb = memnew(VBoxContainer);
  1985. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  1986. HBoxContainer *filter_hb = memnew(HBoxContainer);
  1987. members_vb->add_child(filter_hb);
  1988. node_filter = memnew(LineEdit);
  1989. filter_hb->add_child(node_filter);
  1990. node_filter->connect("text_changed", this, "_member_filter_changed");
  1991. node_filter->connect("gui_input", this, "_sbox_input");
  1992. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  1993. node_filter->set_placeholder(TTR("Search"));
  1994. tools = memnew(MenuButton);
  1995. filter_hb->add_child(tools);
  1996. tools->set_tooltip(TTR("Options"));
  1997. tools->get_popup()->connect("id_pressed", this, "_tools_menu_option");
  1998. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  1999. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  2000. members = memnew(Tree);
  2001. members_vb->add_child(members);
  2002. members->set_drag_forwarding(this);
  2003. members->set_h_size_flags(SIZE_EXPAND_FILL);
  2004. members->set_v_size_flags(SIZE_EXPAND_FILL);
  2005. members->set_hide_root(true);
  2006. members->set_allow_reselect(true);
  2007. members->set_hide_folding(false);
  2008. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  2009. members->connect("item_activated", this, "_member_create");
  2010. members->connect("item_selected", this, "_member_selected");
  2011. members->connect("nothing_selected", this, "_member_unselected");
  2012. Label *desc_label = memnew(Label);
  2013. members_vb->add_child(desc_label);
  2014. desc_label->set_text(TTR("Description:"));
  2015. node_desc = memnew(RichTextLabel);
  2016. members_vb->add_child(node_desc);
  2017. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  2018. node_desc->set_v_size_flags(SIZE_FILL);
  2019. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  2020. members_dialog = memnew(ConfirmationDialog);
  2021. members_dialog->set_title(TTR("Create Shader Node"));
  2022. members_dialog->add_child(members_vb);
  2023. members_dialog->get_ok()->set_text(TTR("Create"));
  2024. members_dialog->get_ok()->connect("pressed", this, "_member_create");
  2025. members_dialog->get_ok()->set_disabled(true);
  2026. members_dialog->set_resizable(true);
  2027. members_dialog->set_as_minsize();
  2028. members_dialog->connect("hide", this, "_member_cancel");
  2029. add_child(members_dialog);
  2030. alert = memnew(AcceptDialog);
  2031. alert->set_as_minsize();
  2032. alert->get_label()->set_autowrap(true);
  2033. alert->get_label()->set_align(Label::ALIGN_CENTER);
  2034. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  2035. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  2036. add_child(alert);
  2037. ///////////////////////////////////////
  2038. // SHADER NODES TREE OPTIONS
  2039. ///////////////////////////////////////
  2040. // COLOR
  2041. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2042. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2043. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  2044. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  2045. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  2046. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  2047. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  2048. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2049. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  2050. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  2051. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2052. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2053. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  2054. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2055. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2056. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  2057. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  2058. // CONDITIONAL
  2059. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  2060. add_options.push_back(AddOption("Equal", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2061. add_options.push_back(AddOption("GreaterThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2062. add_options.push_back(AddOption("GreaterThanEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than or Equal (>=)")), VisualShaderNodeCompare::FUNC_GREATER_THAN_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2063. add_options.push_back(AddOption("If", "Conditional", "Functions", "VisualShaderNodeIf", TTR("Returns an associated vector if the provided scalars are equal, greater or less."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2064. add_options.push_back(AddOption("IsInf", "Conditional", "Functions", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between INF and a scalar parameter."), VisualShaderNodeIs::FUNC_IS_INF, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2065. add_options.push_back(AddOption("IsNaN", "Conditional", "Functions", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between NaN and a scalar parameter."), VisualShaderNodeIs::FUNC_IS_NAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2066. add_options.push_back(AddOption("LessThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2067. add_options.push_back(AddOption("LessThanEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than or Equal (<=)")), VisualShaderNodeCompare::FUNC_LESS_THAN_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2068. add_options.push_back(AddOption("NotEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2069. add_options.push_back(AddOption("Switch", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated vector if the provided boolean value is true or false."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2070. add_options.push_back(AddOption("SwitchS", "Conditional", "Functions", "VisualShaderNodeScalarSwitch", TTR("Returns an associated scalar if the provided boolean value is true or false."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2071. add_options.push_back(AddOption("Compare", "Conditional", "Common", "VisualShaderNodeCompare", TTR("Returns the boolean result of the comparison between two parameters."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2072. add_options.push_back(AddOption("Is", "Conditional", "Common", "VisualShaderNodeIs", TTR("Returns the boolean result of the comparison between INF (or NaN) and a scalar parameter."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2073. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2074. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2075. // INPUT
  2076. // SPATIAL-FOR-ALL
  2077. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  2078. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2079. add_options.push_back(AddOption("InvCamera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "inv_camera"), "inv_camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2080. add_options.push_back(AddOption("InvProjection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "inv_projection"), "inv_projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2081. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2082. add_options.push_back(AddOption("OutputIsSRGB", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "output_is_srgb"), "output_is_srgb", VisualShaderNode::PORT_TYPE_BOOLEAN, -1, Shader::MODE_SPATIAL));
  2083. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2084. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  2085. add_options.push_back(AddOption("ViewportSize", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "viewport_size"), "viewport_size", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2086. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2087. // CANVASITEM-FOR-ALL
  2088. add_options.push_back(AddOption("Alpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2089. add_options.push_back(AddOption("Color", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2090. add_options.push_back(AddOption("ModulateAlpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "modulate_alpha"), "modulate_alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2091. add_options.push_back(AddOption("ModulateColor", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "modulate_color"), "modulate_color", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2092. add_options.push_back(AddOption("TexturePixelSize", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "texture_pixel_size"), "texture_pixel_size", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2093. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2094. add_options.push_back(AddOption("UV", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2095. /////////////////
  2096. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  2097. // SPATIAL INPUTS
  2098. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  2099. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  2100. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  2101. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  2102. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  2103. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader mode.");
  2104. add_options.push_back(AddOption("Alpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2105. add_options.push_back(AddOption("Binormal", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2106. add_options.push_back(AddOption("Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2107. add_options.push_back(AddOption("DepthTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "depth_texture"), "depth_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2108. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2109. add_options.push_back(AddOption("FrontFacing", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "front_facing"), "front_facing", VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2110. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2111. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2112. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2113. add_options.push_back(AddOption("Side", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "side"), "side", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2114. add_options.push_back(AddOption("Tangent", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2115. add_options.push_back(AddOption("UV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2116. add_options.push_back(AddOption("UV2", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2117. add_options.push_back(AddOption("Vertex", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2118. add_options.push_back(AddOption("View", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2119. add_options.push_back(AddOption("Albedo", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "albedo"), "albedo", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2120. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2121. add_options.push_back(AddOption("Diffuse", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "diffuse"), "diffuse", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2122. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2123. add_options.push_back(AddOption("Light", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light"), "light", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2124. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2125. add_options.push_back(AddOption("Metallic", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "metallic"), "metallic", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2126. add_options.push_back(AddOption("Roughness", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "roughness"), "roughness", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2127. add_options.push_back(AddOption("Specular", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "specular"), "specular", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2128. add_options.push_back(AddOption("Transmission", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "transmission"), "transmission", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2129. add_options.push_back(AddOption("View", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2130. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2131. add_options.push_back(AddOption("Binormal", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2132. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2133. add_options.push_back(AddOption("ModelView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "modelview"), "modelview", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2134. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2135. add_options.push_back(AddOption("Tangent", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_mode, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2136. add_options.push_back(AddOption("UV", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2137. add_options.push_back(AddOption("UV2", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2138. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2139. // CANVASITEM INPUTS
  2140. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2141. add_options.push_back(AddOption("LightPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "light_pass"), "light_pass", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2142. add_options.push_back(AddOption("NormalTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "normal_texture"), "normal_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2143. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2144. add_options.push_back(AddOption("ScreenPixelSize", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_pixel_size"), "screen_pixel_size", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2145. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2146. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2147. add_options.push_back(AddOption("Texture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2148. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2149. add_options.push_back(AddOption("LightAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_alpha"), "light_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2150. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2151. add_options.push_back(AddOption("LightHeight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_height"), "light_height", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2152. add_options.push_back(AddOption("LightUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_uv"), "light_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2153. add_options.push_back(AddOption("LightVector", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vec"), "light_vec", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2154. add_options.push_back(AddOption("Normal", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2155. add_options.push_back(AddOption("PointCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2156. add_options.push_back(AddOption("ScreenUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2157. add_options.push_back(AddOption("ShadowAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_alpha"), "shadow_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2158. add_options.push_back(AddOption("ShadowColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_color"), "shadow_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2159. add_options.push_back(AddOption("ShadowVec", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_vec"), "shadow_vec", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2160. add_options.push_back(AddOption("Texture", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2161. add_options.push_back(AddOption("Extra", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "extra"), "extra", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2162. add_options.push_back(AddOption("LightPass", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "light_pass"), "light_pass", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2163. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2164. add_options.push_back(AddOption("Projection", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2165. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2166. add_options.push_back(AddOption("World", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2167. // PARTICLES INPUTS
  2168. add_options.push_back(AddOption("Active", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2169. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2170. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2171. add_options.push_back(AddOption("Custom", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2172. add_options.push_back(AddOption("CustomAlpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2173. add_options.push_back(AddOption("Delta", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2174. add_options.push_back(AddOption("EmissionTransform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2175. add_options.push_back(AddOption("Index", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2176. add_options.push_back(AddOption("LifeTime", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2177. add_options.push_back(AddOption("Restart", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2178. add_options.push_back(AddOption("Time", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2179. add_options.push_back(AddOption("Transform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2180. add_options.push_back(AddOption("Velocity", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2181. // SCALAR
  2182. add_options.push_back(AddOption("ScalarFunc", "Scalar", "Common", "VisualShaderNodeScalarFunc", TTR("Scalar function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2183. add_options.push_back(AddOption("ScalarOp", "Scalar", "Common", "VisualShaderNodeScalarOp", TTR("Scalar operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2184. //CONSTANTS
  2185. add_options.push_back(AddOption("E", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("E constant (2.718282). Represents the base of the natural logarithm."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_E));
  2186. add_options.push_back(AddOption("Epsilon", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Epsilon constant (0.00001). Smallest possible scalar number."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, CMP_EPSILON));
  2187. add_options.push_back(AddOption("Phi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Phi constant (1.618034). Golden ratio."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, 1.618034f));
  2188. add_options.push_back(AddOption("Pi/4", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/4 constant (0.785398) or 45 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 4));
  2189. add_options.push_back(AddOption("Pi/2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/2 constant (1.570796) or 90 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 2));
  2190. add_options.push_back(AddOption("Pi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi constant (3.141593) or 180 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI));
  2191. add_options.push_back(AddOption("Tau", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Tau constant (6.283185) or 360 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_TAU));
  2192. add_options.push_back(AddOption("Sqrt2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Sqrt2 constant (1.414214). Square root of 2."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_SQRT2));
  2193. // FUNCTIONS
  2194. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeScalarFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  2195. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  2196. add_options.push_back(AddOption("ACosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2197. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2198. add_options.push_back(AddOption("ASinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2199. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2200. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeScalarOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  2201. add_options.push_back(AddOption("ATanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2202. add_options.push_back(AddOption("Ceil", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_SCALAR));
  2203. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeScalarClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2204. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  2205. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2206. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeScalarFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  2207. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-e Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  2208. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  2209. add_options.push_back(AddOption("Floor", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_SCALAR));
  2210. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeScalarFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  2211. add_options.push_back(AddOption("InverseSqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2212. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Natural logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  2213. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  2214. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the greater of two values."), VisualShaderNodeScalarOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  2215. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the lesser of two values."), VisualShaderNodeScalarOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2216. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeScalarInterp", TTR("Linear interpolation between two scalars."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2217. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeScalarFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2218. add_options.push_back(AddOption("OneMinus", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 - scalar"), VisualShaderNodeScalarFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  2219. add_options.push_back(AddOption("Pow", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeScalarOp::OP_POW, VisualShaderNode::PORT_TYPE_SCALAR));
  2220. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeScalarFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  2221. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 / scalar"), VisualShaderNodeScalarFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  2222. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  2223. add_options.push_back(AddOption("RoundEven", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_SCALAR));
  2224. add_options.push_back(AddOption("Saturate", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeScalarFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2225. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  2226. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2227. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2228. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2229. add_options.push_back(AddOption("SmoothStep", "Scalar", "Functions", "VisualShaderNodeScalarSmoothStep", TTR("SmoothStep function( scalar(edge0), scalar(edge1), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if x is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2230. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeScalarOp::OP_STEP, VisualShaderNode::PORT_TYPE_SCALAR));
  2231. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2232. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2233. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeScalarFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  2234. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Adds scalar to scalar."), VisualShaderNodeScalarOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  2235. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Divides scalar by scalar."), VisualShaderNodeScalarOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  2236. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Multiplies scalar by scalar."), VisualShaderNodeScalarOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  2237. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Returns the remainder of the two scalars."), VisualShaderNodeScalarOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR));
  2238. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Subtracts scalar from scalar."), VisualShaderNodeScalarOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  2239. add_options.push_back(AddOption("ScalarConstant", "Scalar", "Variables", "VisualShaderNodeScalarConstant", TTR("Scalar constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2240. add_options.push_back(AddOption("ScalarUniform", "Scalar", "Variables", "VisualShaderNodeScalarUniform", TTR("Scalar uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2241. // TEXTURES
  2242. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubeMap", TTR("Perform the cubic texture lookup."), -1, -1));
  2243. texture_node_option_idx = add_options.size();
  2244. add_options.push_back(AddOption("Texture", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the texture lookup."), -1, -1));
  2245. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubeMapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  2246. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  2247. add_options.push_back(AddOption("TextureUniformTriplanar", "Textures", "Variables", "VisualShaderNodeTextureUniformTriplanar", TTR("2D texture uniform lookup with triplanar."), -1, -1, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2248. // TRANSFORM
  2249. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2250. add_options.push_back(AddOption("OuterProduct", "Transform", "Composition", "VisualShaderNodeOuterProduct", TTR("Calculate the outer product of a pair of vectors.\n\nOuterProduct treats the first parameter 'c' as a column vector (matrix with one column) and the second parameter 'r' as a row vector (matrix with one row) and does a linear algebraic matrix multiply 'c * r', yielding a matrix whose number of rows is the number of components in 'c' and whose number of columns is the number of components in 'r'."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2251. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2252. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  2253. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2254. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2255. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2256. add_options.push_back(AddOption("TransformMult", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2257. add_options.push_back(AddOption("TransformVectorMult", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2258. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2259. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2260. // VECTOR
  2261. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2262. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2263. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2264. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  2265. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  2266. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  2267. add_options.push_back(AddOption("ACosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2268. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2269. add_options.push_back(AddOption("ASinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2270. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2271. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  2272. add_options.push_back(AddOption("ATanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2273. add_options.push_back(AddOption("Ceil", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeVectorFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_VECTOR));
  2274. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeVectorClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2275. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  2276. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2277. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  2278. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  2279. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2280. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2281. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  2282. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  2283. add_options.push_back(AddOption("FaceForward", "Vector", "Functions", "VisualShaderNodeFaceForward", TTR("Returns the vector that points in the same direction as a reference vector. The function has three vector parameters : N, the vector to orient, I, the incident vector, and Nref, the reference vector. If the dot product of I and Nref is smaller than zero the return value is N. Otherwise -N is returned."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2284. add_options.push_back(AddOption("Floor", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeVectorFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_VECTOR));
  2285. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  2286. add_options.push_back(AddOption("InverseSqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2287. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2288. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  2289. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  2290. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  2291. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2292. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2293. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2294. add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2295. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  2296. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  2297. add_options.push_back(AddOption("Pow", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeVectorOp::OP_POW, VisualShaderNode::PORT_TYPE_VECTOR));
  2298. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  2299. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  2300. add_options.push_back(AddOption("Reflect", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the vector that points in the direction of reflection ( a : incident vector, b : normal vector )."), VisualShaderNodeVectorOp::OP_REFLECT, VisualShaderNode::PORT_TYPE_VECTOR));
  2301. add_options.push_back(AddOption("Refract", "Vector", "Functions", "VisualShaderNodeVectorRefract", TTR("Returns the vector that points in the direction of refraction."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2302. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  2303. add_options.push_back(AddOption("RoundEven", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2304. add_options.push_back(AddOption("Saturate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeVectorFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2305. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  2306. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2307. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2308. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2309. add_options.push_back(AddOption("SmoothStep", "Vector", "Functions", "VisualShaderNodeVectorSmoothStep", TTR("SmoothStep function( vector(edge0), vector(edge1), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if 'x' is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2310. add_options.push_back(AddOption("SmoothStepS", "Vector", "Functions", "VisualShaderNodeVectorScalarSmoothStep", TTR("SmoothStep function( scalar(edge0), scalar(edge1), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge0' and 1.0 if 'x' is larger than 'edge1'. Otherwise the return value is interpolated between 0.0 and 1.0 using Hermite polynomials."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2311. add_options.push_back(AddOption("Step", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Step function( vector(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeVectorOp::OP_STEP, VisualShaderNode::PORT_TYPE_VECTOR));
  2312. add_options.push_back(AddOption("StepS", "Vector", "Functions", "VisualShaderNodeVectorScalarStep", TTR("Step function( scalar(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2313. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2314. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2315. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  2316. add_options.push_back(AddOption("Add", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  2317. add_options.push_back(AddOption("Divide", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  2318. add_options.push_back(AddOption("Multiply", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  2319. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  2320. add_options.push_back(AddOption("Subtract", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  2321. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2322. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2323. // SPECIAL
  2324. add_options.push_back(AddOption("Expression", "Special", "", "VisualShaderNodeExpression", TTR("Custom Godot Shader Language expression, with custom amount of input and output ports. This is a direct injection of code into the vertex/fragment/light function, do not use it to write the function declarations inside.")));
  2325. add_options.push_back(AddOption("Fresnel", "Special", "", "VisualShaderNodeFresnel", TTR("Returns falloff based on the dot product of surface normal and view direction of camera (pass associated inputs to it)."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2326. add_options.push_back(AddOption("GlobalExpression", "Special", "", "VisualShaderNodeGlobalExpression", TTR("Custom Godot Shader Language expression, which is placed on top of the resulted shader. You can place various function definitions inside and call it later in the Expressions. You can also declare varyings, uniforms and constants.")));
  2327. add_options.push_back(AddOption("UniformRef", "Special", "", "VisualShaderNodeUniformRef", TTR("A reference to an existing uniform.")));
  2328. add_options.push_back(AddOption("ScalarDerivativeFunc", "Special", "Common", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) Scalar derivative function."), -1, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2329. add_options.push_back(AddOption("VectorDerivativeFunc", "Special", "Common", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) Vector derivative function."), -1, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2330. add_options.push_back(AddOption("DdX", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Derivative in 'x' using local differencing."), VisualShaderNodeVectorDerivativeFunc::FUNC_X, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2331. add_options.push_back(AddOption("DdXS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'x' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_X, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2332. add_options.push_back(AddOption("DdY", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Derivative in 'y' using local differencing."), VisualShaderNodeVectorDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2333. add_options.push_back(AddOption("DdYS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Derivative in 'y' using local differencing."), VisualShaderNodeScalarDerivativeFunc::FUNC_Y, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2334. add_options.push_back(AddOption("Sum", "Special", "Derivative", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) (Vector) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeVectorDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2335. add_options.push_back(AddOption("SumS", "Special", "Derivative", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) (Scalar) Sum of absolute derivative in 'x' and 'y'."), VisualShaderNodeScalarDerivativeFunc::FUNC_SUM, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2336. custom_node_option_idx = add_options.size();
  2337. /////////////////////////////////////////////////////////////////////
  2338. _update_options_menu();
  2339. error_panel = memnew(PanelContainer);
  2340. add_child(error_panel);
  2341. error_label = memnew(Label);
  2342. error_panel->add_child(error_label);
  2343. error_label->set_text("eh");
  2344. error_panel->hide();
  2345. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2346. Ref<VisualShaderNodePluginDefault> default_plugin;
  2347. default_plugin.instance();
  2348. add_plugin(default_plugin);
  2349. property_editor = memnew(CustomPropertyEditor);
  2350. add_child(property_editor);
  2351. property_editor->connect("variant_changed", this, "_port_edited");
  2352. }
  2353. void VisualShaderEditorPlugin::edit(Object *p_object) {
  2354. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  2355. }
  2356. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  2357. return p_object->is_class("VisualShader");
  2358. }
  2359. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  2360. if (p_visible) {
  2361. //editor->hide_animation_player_editors();
  2362. //editor->animation_panel_make_visible(true);
  2363. button->show();
  2364. editor->make_bottom_panel_item_visible(visual_shader_editor);
  2365. visual_shader_editor->update_custom_nodes();
  2366. visual_shader_editor->set_process_input(true);
  2367. //visual_shader_editor->set_process(true);
  2368. } else {
  2369. if (visual_shader_editor->is_visible_in_tree())
  2370. editor->hide_bottom_panel();
  2371. button->hide();
  2372. visual_shader_editor->set_process_input(false);
  2373. //visual_shader_editor->set_process(false);
  2374. }
  2375. }
  2376. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  2377. editor = p_node;
  2378. visual_shader_editor = memnew(VisualShaderEditor);
  2379. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2380. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  2381. button->hide();
  2382. }
  2383. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  2384. }
  2385. ////////////////
  2386. class VisualShaderNodePluginInputEditor : public OptionButton {
  2387. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  2388. Ref<VisualShaderNodeInput> input;
  2389. protected:
  2390. static void _bind_methods() {
  2391. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginInputEditor::_item_selected);
  2392. }
  2393. public:
  2394. void _notification(int p_what) {
  2395. if (p_what == NOTIFICATION_READY) {
  2396. connect("item_selected", this, "_item_selected");
  2397. }
  2398. }
  2399. void _item_selected(int p_item) {
  2400. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  2401. }
  2402. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  2403. input = p_input;
  2404. Ref<Texture> type_icon[5] = {
  2405. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  2406. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  2407. EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"),
  2408. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  2409. EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"),
  2410. };
  2411. add_item("[None]");
  2412. int to_select = -1;
  2413. for (int i = 0; i < input->get_input_index_count(); i++) {
  2414. if (input->get_input_name() == input->get_input_index_name(i)) {
  2415. to_select = i + 1;
  2416. }
  2417. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  2418. }
  2419. if (to_select >= 0) {
  2420. select(to_select);
  2421. }
  2422. }
  2423. };
  2424. ////////////////
  2425. class VisualShaderNodePluginUniformRefEditor : public OptionButton {
  2426. GDCLASS(VisualShaderNodePluginUniformRefEditor, OptionButton);
  2427. Ref<VisualShaderNodeUniformRef> uniform_ref;
  2428. protected:
  2429. static void _bind_methods() {
  2430. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginUniformRefEditor::_item_selected);
  2431. }
  2432. public:
  2433. void _notification(int p_what) {
  2434. if (p_what == NOTIFICATION_READY) {
  2435. connect("item_selected", this, "_item_selected");
  2436. }
  2437. }
  2438. void _item_selected(int p_item) {
  2439. VisualShaderEditor::get_singleton()->call_deferred("_uniform_select_item", uniform_ref, get_item_text(p_item));
  2440. }
  2441. void setup(const Ref<VisualShaderNodeUniformRef> &p_uniform_ref) {
  2442. uniform_ref = p_uniform_ref;
  2443. Ref<Texture> type_icon[6] = {
  2444. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  2445. EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"),
  2446. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  2447. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  2448. EditorNode::get_singleton()->get_gui_base()->get_icon("Color", "EditorIcons"),
  2449. EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"),
  2450. };
  2451. add_item("[None]");
  2452. int to_select = -1;
  2453. for (int i = 0; i < p_uniform_ref->get_uniforms_count(); i++) {
  2454. if (p_uniform_ref->get_uniform_name() == p_uniform_ref->get_uniform_name_by_index(i)) {
  2455. to_select = i + 1;
  2456. }
  2457. add_icon_item(type_icon[p_uniform_ref->get_uniform_type_by_index(i)], p_uniform_ref->get_uniform_name_by_index(i));
  2458. }
  2459. if (to_select >= 0) {
  2460. select(to_select);
  2461. }
  2462. }
  2463. };
  2464. ////////////////
  2465. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  2466. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  2467. Ref<Resource> parent_resource;
  2468. public:
  2469. void _property_changed(const String &prop, const Variant &p_value, const String &p_field, bool p_changing = false) {
  2470. if (p_changing)
  2471. return;
  2472. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2473. updating = true;
  2474. undo_redo->create_action(TTR("Edit Visual Property") + ": " + prop, UndoRedo::MERGE_ENDS);
  2475. undo_redo->add_do_property(node.ptr(), prop, p_value);
  2476. undo_redo->add_undo_property(node.ptr(), prop, node->get(prop));
  2477. if (p_value.get_type() == Variant::OBJECT) {
  2478. RES prev_res = node->get(prop);
  2479. RES curr_res = p_value;
  2480. if (curr_res.is_null()) {
  2481. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2482. } else {
  2483. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  2484. }
  2485. if (!prev_res.is_null()) {
  2486. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  2487. } else {
  2488. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2489. }
  2490. undo_redo->add_do_method(this, "_refresh_request");
  2491. undo_redo->add_undo_method(this, "_refresh_request");
  2492. }
  2493. undo_redo->commit_action();
  2494. updating = false;
  2495. }
  2496. void _node_changed() {
  2497. if (updating)
  2498. return;
  2499. for (int i = 0; i < properties.size(); i++) {
  2500. properties[i]->update_property();
  2501. }
  2502. }
  2503. void _refresh_request() {
  2504. VisualShaderEditor::get_singleton()->call_deferred("_update_graph");
  2505. }
  2506. void _resource_selected(const String &p_path, RES p_resource) {
  2507. _open_inspector(p_resource);
  2508. }
  2509. void _open_inspector(RES p_resource) {
  2510. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  2511. }
  2512. bool updating;
  2513. Ref<VisualShaderNode> node;
  2514. Vector<EditorProperty *> properties;
  2515. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  2516. parent_resource = p_parent_resource;
  2517. updating = false;
  2518. node = p_node;
  2519. properties = p_properties;
  2520. for (int i = 0; i < p_properties.size(); i++) {
  2521. add_child(p_properties[i]);
  2522. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  2523. if (res_prop) {
  2524. p_properties[i]->connect("resource_selected", this, "_resource_selected");
  2525. }
  2526. properties[i]->connect("property_changed", this, "_property_changed");
  2527. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  2528. properties[i]->update_property();
  2529. properties[i]->set_name_split_ratio(0);
  2530. }
  2531. node->connect("changed", this, "_node_changed");
  2532. node->connect("editor_refresh_request", this, "_refresh_request", varray(), CONNECT_DEFERRED);
  2533. }
  2534. static void _bind_methods() {
  2535. ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(String()), DEFVAL(false));
  2536. ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed);
  2537. ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request);
  2538. ClassDB::bind_method("_resource_selected", &VisualShaderNodePluginDefaultEditor::_resource_selected);
  2539. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector);
  2540. }
  2541. };
  2542. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  2543. if (p_node->is_class("VisualShaderNodeUniformRef")) {
  2544. //create input
  2545. VisualShaderNodePluginUniformRefEditor *uniform_editor = memnew(VisualShaderNodePluginUniformRefEditor);
  2546. uniform_editor->setup(p_node);
  2547. return uniform_editor;
  2548. }
  2549. if (p_node->is_class("VisualShaderNodeInput")) {
  2550. //create input
  2551. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  2552. input_editor->setup(p_node);
  2553. return input_editor;
  2554. }
  2555. Vector<StringName> properties = p_node->get_editable_properties();
  2556. if (properties.size() == 0) {
  2557. return NULL;
  2558. }
  2559. List<PropertyInfo> props;
  2560. p_node->get_property_list(&props);
  2561. Vector<PropertyInfo> pinfo;
  2562. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2563. for (int i = 0; i < properties.size(); i++) {
  2564. if (E->get().name == String(properties[i])) {
  2565. pinfo.push_back(E->get());
  2566. }
  2567. }
  2568. }
  2569. if (pinfo.size() == 0)
  2570. return NULL;
  2571. properties.clear();
  2572. Ref<VisualShaderNode> node = p_node;
  2573. Vector<EditorProperty *> editors;
  2574. for (int i = 0; i < pinfo.size(); i++) {
  2575. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  2576. if (!prop)
  2577. return NULL;
  2578. if (Object::cast_to<EditorPropertyResource>(prop)) {
  2579. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  2580. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2581. } else if (Object::cast_to<EditorPropertyTransform>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  2582. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  2583. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  2584. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2585. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  2586. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2587. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  2588. }
  2589. editors.push_back(prop);
  2590. properties.push_back(pinfo[i].name);
  2591. }
  2592. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  2593. editor->setup(p_parent_resource, editors, properties, p_node);
  2594. return editor;
  2595. }
  2596. void EditorPropertyShaderMode::_option_selected(int p_which) {
  2597. //will not use this, instead will do all the logic setting manually
  2598. //emit_signal("property_changed", get_edited_property(), p_which);
  2599. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  2600. if (visual_shader->get_mode() == p_which)
  2601. return;
  2602. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2603. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  2604. //do is easy
  2605. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  2606. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  2607. //now undo is hell
  2608. //1. restore connections to output
  2609. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2610. VisualShader::Type type = VisualShader::Type(i);
  2611. List<VisualShader::Connection> conns;
  2612. visual_shader->get_node_connections(type, &conns);
  2613. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2614. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  2615. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2616. }
  2617. }
  2618. }
  2619. //2. restore input indices
  2620. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2621. VisualShader::Type type = VisualShader::Type(i);
  2622. Vector<int> nodes = visual_shader->get_node_list(type);
  2623. for (int j = 0; j < nodes.size(); j++) {
  2624. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  2625. if (!input.is_valid()) {
  2626. continue;
  2627. }
  2628. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  2629. }
  2630. }
  2631. //3. restore enums and flags
  2632. List<PropertyInfo> props;
  2633. visual_shader->get_property_list(&props);
  2634. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2635. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  2636. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  2637. }
  2638. }
  2639. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2640. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2641. //update graph
  2642. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2643. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2644. undo_redo->commit_action();
  2645. }
  2646. void EditorPropertyShaderMode::update_property() {
  2647. int which = get_edited_object()->get(get_edited_property());
  2648. options->select(which);
  2649. }
  2650. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  2651. for (int i = 0; i < p_options.size(); i++) {
  2652. options->add_item(p_options[i], i);
  2653. }
  2654. }
  2655. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  2656. options->set_clip_text(p_enable);
  2657. }
  2658. void EditorPropertyShaderMode::_bind_methods() {
  2659. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyShaderMode::_option_selected);
  2660. }
  2661. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  2662. options = memnew(OptionButton);
  2663. options->set_clip_text(true);
  2664. add_child(options);
  2665. add_focusable(options);
  2666. options->connect("item_selected", this, "_option_selected");
  2667. }
  2668. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  2669. return true; //can handle everything
  2670. }
  2671. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  2672. //do none
  2673. }
  2674. bool EditorInspectorShaderModePlugin::parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage) {
  2675. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  2676. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  2677. Vector<String> options = p_hint_text.split(",");
  2678. editor->setup(options);
  2679. add_property_editor(p_path, editor);
  2680. return true;
  2681. }
  2682. return false; //can be overridden, although it will most likely be last anyway
  2683. }
  2684. void EditorInspectorShaderModePlugin::parse_end() {
  2685. //do none
  2686. }
  2687. //////////////////////////////////
  2688. void VisualShaderNodePortPreview::_shader_changed() {
  2689. if (shader.is_null()) {
  2690. return;
  2691. }
  2692. Vector<VisualShader::DefaultTextureParam> default_textures;
  2693. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  2694. Ref<Shader> preview_shader;
  2695. preview_shader.instance();
  2696. preview_shader->set_code(shader_code);
  2697. for (int i = 0; i < default_textures.size(); i++) {
  2698. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  2699. }
  2700. Ref<ShaderMaterial> material;
  2701. material.instance();
  2702. material->set_shader(preview_shader);
  2703. //find if a material is also being edited and copy parameters to this one
  2704. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  2705. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  2706. ShaderMaterial *src_mat;
  2707. if (!object)
  2708. continue;
  2709. if (object->has_method("get_material_override")) { // trying getting material from MeshInstance
  2710. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material_override"));
  2711. } else if (object->has_method("get_material")) { // from CanvasItem/Node2D
  2712. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material"));
  2713. } else {
  2714. src_mat = Object::cast_to<ShaderMaterial>(object);
  2715. }
  2716. if (src_mat && src_mat->get_shader().is_valid()) {
  2717. List<PropertyInfo> params;
  2718. src_mat->get_shader()->get_param_list(&params);
  2719. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  2720. material->set(E->get().name, src_mat->get(E->get().name));
  2721. }
  2722. }
  2723. }
  2724. set_material(material);
  2725. }
  2726. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  2727. shader = p_shader;
  2728. shader->connect("changed", this, "_shader_changed");
  2729. type = p_type;
  2730. port = p_port;
  2731. node = p_node;
  2732. update();
  2733. _shader_changed();
  2734. }
  2735. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  2736. return Size2(100, 100) * EDSCALE;
  2737. }
  2738. void VisualShaderNodePortPreview::_notification(int p_what) {
  2739. if (p_what == NOTIFICATION_DRAW) {
  2740. Vector<Vector2> points;
  2741. Vector<Vector2> uvs;
  2742. Vector<Color> colors;
  2743. points.push_back(Vector2());
  2744. uvs.push_back(Vector2(0, 0));
  2745. colors.push_back(Color(1, 1, 1, 1));
  2746. points.push_back(Vector2(get_size().width, 0));
  2747. uvs.push_back(Vector2(1, 0));
  2748. colors.push_back(Color(1, 1, 1, 1));
  2749. points.push_back(get_size());
  2750. uvs.push_back(Vector2(1, 1));
  2751. colors.push_back(Color(1, 1, 1, 1));
  2752. points.push_back(Vector2(0, get_size().height));
  2753. uvs.push_back(Vector2(0, 1));
  2754. colors.push_back(Color(1, 1, 1, 1));
  2755. draw_primitive(points, colors, uvs);
  2756. }
  2757. }
  2758. void VisualShaderNodePortPreview::_bind_methods() {
  2759. ClassDB::bind_method("_shader_changed", &VisualShaderNodePortPreview::_shader_changed);
  2760. }
  2761. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  2762. }
  2763. //////////////////////////////////
  2764. String VisualShaderConversionPlugin::converts_to() const {
  2765. return "Shader";
  2766. }
  2767. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  2768. Ref<VisualShader> vshader = p_resource;
  2769. return vshader.is_valid();
  2770. }
  2771. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  2772. Ref<VisualShader> vshader = p_resource;
  2773. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  2774. Ref<Shader> shader;
  2775. shader.instance();
  2776. String code = vshader->get_code();
  2777. shader->set_code(code);
  2778. return shader;
  2779. }