visual_shader_editor_plugin.cpp 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_shader_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/math_defs.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/version.h"
  37. #include "editor/editor_log.h"
  38. #include "editor/editor_properties.h"
  39. #include "editor/editor_scale.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/main/window.h"
  44. #include "scene/resources/visual_shader_nodes.h"
  45. #include "servers/display_server.h"
  46. #include "servers/rendering/shader_types.h"
  47. struct FloatConstantDef {
  48. String name;
  49. float value;
  50. String desc;
  51. };
  52. static FloatConstantDef float_constant_defs[] = {
  53. { "E", Math_E, TTR("E constant (2.718282). Represents the base of the natural logarithm.") },
  54. { "Epsilon", CMP_EPSILON, TTR("Epsilon constant (0.00001). Smallest possible scalar number.") },
  55. { "Phi", 1.618034f, TTR("Phi constant (1.618034). Golden ratio.") },
  56. { "Pi/4", Math_PI / 4, TTR("Pi/4 constant (0.785398) or 45 degrees.") },
  57. { "Pi/2", Math_PI / 2, TTR("Pi/2 constant (1.570796) or 90 degrees.") },
  58. { "Pi", Math_PI, TTR("Pi constant (3.141593) or 180 degrees.") },
  59. { "Tau", Math_TAU, TTR("Tau constant (6.283185) or 360 degrees.") },
  60. { "Sqrt2", Math_SQRT2, TTR("Sqrt2 constant (1.414214). Square root of 2.") }
  61. };
  62. const int MAX_FLOAT_CONST_DEFS = sizeof(float_constant_defs) / sizeof(FloatConstantDef);
  63. ///////////////////
  64. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  65. if (get_script_instance()) {
  66. return get_script_instance()->call("create_editor", p_parent_resource, p_node);
  67. }
  68. return nullptr;
  69. }
  70. void VisualShaderNodePlugin::_bind_methods() {
  71. 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")));
  72. }
  73. ///////////////////
  74. 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) {
  75. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  76. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  77. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  78. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  79. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  80. return style;
  81. }
  82. ///////////////////
  83. VisualShaderGraphPlugin::VisualShaderGraphPlugin() {
  84. }
  85. void VisualShaderGraphPlugin::_bind_methods() {
  86. ClassDB::bind_method("add_node", &VisualShaderGraphPlugin::add_node);
  87. ClassDB::bind_method("remove_node", &VisualShaderGraphPlugin::remove_node);
  88. ClassDB::bind_method("connect_nodes", &VisualShaderGraphPlugin::connect_nodes);
  89. ClassDB::bind_method("disconnect_nodes", &VisualShaderGraphPlugin::disconnect_nodes);
  90. ClassDB::bind_method("set_node_position", &VisualShaderGraphPlugin::set_node_position);
  91. ClassDB::bind_method("set_node_size", &VisualShaderGraphPlugin::set_node_size);
  92. ClassDB::bind_method("update_node", &VisualShaderGraphPlugin::update_node);
  93. ClassDB::bind_method("update_node_deferred", &VisualShaderGraphPlugin::update_node_deferred);
  94. ClassDB::bind_method("set_input_port_default_value", &VisualShaderGraphPlugin::set_input_port_default_value);
  95. ClassDB::bind_method("set_uniform_name", &VisualShaderGraphPlugin::set_uniform_name);
  96. ClassDB::bind_method("set_expression", &VisualShaderGraphPlugin::set_expression);
  97. ClassDB::bind_method("update_curve", &VisualShaderGraphPlugin::update_curve);
  98. ClassDB::bind_method("update_constant", &VisualShaderGraphPlugin::update_constant);
  99. }
  100. void VisualShaderGraphPlugin::register_shader(VisualShader *p_shader) {
  101. visual_shader = Ref<VisualShader>(p_shader);
  102. }
  103. void VisualShaderGraphPlugin::set_connections(List<VisualShader::Connection> &p_connections) {
  104. connections = p_connections;
  105. }
  106. void VisualShaderGraphPlugin::show_port_preview(VisualShader::Type p_type, int p_node_id, int p_port_id) {
  107. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id)) {
  108. for (Map<int, Port>::Element *E = links[p_node_id].output_ports.front(); E; E = E->next()) {
  109. E->value().preview_button->set_pressed(false);
  110. }
  111. if (links[p_node_id].preview_visible && !is_dirty() && links[p_node_id].preview_box != nullptr) {
  112. links[p_node_id].graph_node->remove_child(links[p_node_id].preview_box);
  113. memdelete(links[p_node_id].preview_box);
  114. links[p_node_id].graph_node->set_size(Vector2(-1, -1));
  115. links[p_node_id].preview_visible = false;
  116. }
  117. if (p_port_id != -1) {
  118. if (is_dirty()) {
  119. links[p_node_id].preview_pos = links[p_node_id].graph_node->get_child_count();
  120. }
  121. VBoxContainer *vbox = memnew(VBoxContainer);
  122. links[p_node_id].graph_node->add_child(vbox);
  123. if (links[p_node_id].preview_pos != -1) {
  124. links[p_node_id].graph_node->move_child(vbox, links[p_node_id].preview_pos);
  125. }
  126. Control *offset = memnew(Control);
  127. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  128. vbox->add_child(offset);
  129. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  130. port_preview->setup(visual_shader, visual_shader->get_shader_type(), p_node_id, p_port_id);
  131. port_preview->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  132. vbox->add_child(port_preview);
  133. links[p_node_id].preview_visible = true;
  134. links[p_node_id].preview_box = vbox;
  135. links[p_node_id].output_ports[p_port_id].preview_button->set_pressed(true);
  136. }
  137. }
  138. }
  139. void VisualShaderGraphPlugin::update_node_deferred(VisualShader::Type p_type, int p_node_id) {
  140. call_deferred("update_node", p_type, p_node_id);
  141. }
  142. void VisualShaderGraphPlugin::update_node(VisualShader::Type p_type, int p_node_id) {
  143. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  144. return;
  145. }
  146. remove_node(p_type, p_node_id);
  147. add_node(p_type, p_node_id);
  148. }
  149. void VisualShaderGraphPlugin::set_input_port_default_value(VisualShader::Type p_type, int p_node_id, int p_port_id, Variant p_value) {
  150. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  151. return;
  152. }
  153. Button *button = links[p_node_id].input_ports[p_port_id].default_input_button;
  154. switch (p_value.get_type()) {
  155. case Variant::COLOR: {
  156. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  157. if (!button->is_connected("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button))) {
  158. button->connect("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button), varray(button, p_value));
  159. }
  160. } break;
  161. case Variant::BOOL: {
  162. button->set_text(((bool)p_value) ? "true" : "false");
  163. } break;
  164. case Variant::INT:
  165. case Variant::FLOAT: {
  166. button->set_text(String::num(p_value, 4));
  167. } break;
  168. case Variant::VECTOR3: {
  169. Vector3 v = p_value;
  170. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  171. } break;
  172. default: {
  173. }
  174. }
  175. }
  176. void VisualShaderGraphPlugin::set_uniform_name(VisualShader::Type p_type, int p_node_id, const String &p_name) {
  177. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].uniform_name != nullptr) {
  178. links[p_node_id].uniform_name->set_text(p_name);
  179. }
  180. }
  181. void VisualShaderGraphPlugin::update_curve(int p_node_id) {
  182. if (links.has(p_node_id) && links[p_node_id].curve_editor) {
  183. if (((VisualShaderNodeCurveTexture *)links[p_node_id].visual_node)->get_texture().is_valid()) {
  184. links[p_node_id].curve_editor->set_curve(((VisualShaderNodeCurveTexture *)links[p_node_id].visual_node)->get_texture()->get_curve());
  185. }
  186. }
  187. }
  188. int VisualShaderGraphPlugin::get_constant_index(float p_constant) const {
  189. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  190. if (Math::is_equal_approx(p_constant, float_constant_defs[i].value)) {
  191. return i + 1;
  192. }
  193. }
  194. return 0;
  195. }
  196. void VisualShaderGraphPlugin::update_constant(VisualShader::Type p_type, int p_node_id) {
  197. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].const_op) {
  198. return;
  199. }
  200. VisualShaderNodeFloatConstant *float_const = Object::cast_to<VisualShaderNodeFloatConstant>(links[p_node_id].visual_node);
  201. if (!float_const) {
  202. return;
  203. }
  204. links[p_node_id].const_op->select(get_constant_index(float_const->get_constant()));
  205. links[p_node_id].graph_node->set_size(Size2(-1, -1));
  206. }
  207. void VisualShaderGraphPlugin::set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression) {
  208. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].expression_edit) {
  209. return;
  210. }
  211. links[p_node_id].expression_edit->set_text(p_expression);
  212. }
  213. void VisualShaderGraphPlugin::update_node_size(int p_node_id) {
  214. if (!links.has(p_node_id)) {
  215. return;
  216. }
  217. links[p_node_id].graph_node->set_size(Size2(-1, -1));
  218. }
  219. void VisualShaderGraphPlugin::register_default_input_button(int p_node_id, int p_port_id, Button *p_button) {
  220. links[p_node_id].input_ports.insert(p_port_id, { p_button });
  221. }
  222. void VisualShaderGraphPlugin::register_constant_option_btn(int p_node_id, OptionButton *p_button) {
  223. links[p_node_id].const_op = p_button;
  224. }
  225. void VisualShaderGraphPlugin::register_expression_edit(int p_node_id, CodeEdit *p_expression_edit) {
  226. links[p_node_id].expression_edit = p_expression_edit;
  227. }
  228. void VisualShaderGraphPlugin::register_curve_editor(int p_node_id, CurveEditor *p_curve_editor) {
  229. links[p_node_id].curve_editor = p_curve_editor;
  230. }
  231. void VisualShaderGraphPlugin::update_uniform_refs() {
  232. for (Map<int, Link>::Element *E = links.front(); E; E = E->next()) {
  233. VisualShaderNodeUniformRef *ref = Object::cast_to<VisualShaderNodeUniformRef>(E->get().visual_node);
  234. if (ref) {
  235. remove_node(E->get().type, E->key());
  236. add_node(E->get().type, E->key());
  237. }
  238. }
  239. }
  240. VisualShader::Type VisualShaderGraphPlugin::get_shader_type() const {
  241. return visual_shader->get_shader_type();
  242. }
  243. void VisualShaderGraphPlugin::set_node_position(VisualShader::Type p_type, int p_id, const Vector2 &p_position) {
  244. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  245. links[p_id].graph_node->set_offset(p_position);
  246. }
  247. }
  248. void VisualShaderGraphPlugin::set_node_size(VisualShader::Type p_type, int p_id, const Vector2 &p_size) {
  249. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  250. links[p_id].graph_node->set_size(p_size);
  251. }
  252. }
  253. bool VisualShaderGraphPlugin::is_preview_visible(int p_id) const {
  254. return links[p_id].preview_visible;
  255. }
  256. void VisualShaderGraphPlugin::clear_links() {
  257. links.clear();
  258. }
  259. bool VisualShaderGraphPlugin::is_dirty() const {
  260. return dirty;
  261. }
  262. void VisualShaderGraphPlugin::make_dirty(bool p_enabled) {
  263. dirty = p_enabled;
  264. }
  265. void VisualShaderGraphPlugin::register_link(VisualShader::Type p_type, int p_id, VisualShaderNode *p_visual_node, GraphNode *p_graph_node) {
  266. links.insert(p_id, { p_type, p_visual_node, p_graph_node, p_visual_node->get_output_port_for_preview() != -1, -1, Map<int, InputPort>(), Map<int, Port>(), nullptr, nullptr, nullptr, nullptr, nullptr });
  267. }
  268. void VisualShaderGraphPlugin::register_output_port(int p_node_id, int p_port, TextureButton *p_button) {
  269. links[p_node_id].output_ports.insert(p_port, { p_button });
  270. }
  271. void VisualShaderGraphPlugin::register_uniform_name(int p_node_id, LineEdit *p_uniform_name) {
  272. links[p_node_id].uniform_name = p_uniform_name;
  273. }
  274. void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id) {
  275. if (p_type != visual_shader->get_shader_type()) {
  276. return;
  277. }
  278. Control *offset;
  279. static Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  280. static const Color type_color[6] = {
  281. Color(0.38, 0.85, 0.96), // scalar (float)
  282. Color(0.49, 0.78, 0.94), // scalar (int)
  283. Color(0.84, 0.49, 0.93), // vector
  284. Color(0.55, 0.65, 0.94), // boolean
  285. Color(0.96, 0.66, 0.43), // transform
  286. Color(1.0, 1.0, 0.0), // sampler
  287. };
  288. Ref<VisualShaderNode> vsnode = visual_shader->get_node(p_type, p_id);
  289. Ref<VisualShaderNodeResizableBase> resizable_node = Object::cast_to<VisualShaderNodeResizableBase>(vsnode.ptr());
  290. bool is_resizable = !resizable_node.is_null();
  291. Size2 size = Size2(0, 0);
  292. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  293. bool is_group = !group_node.is_null();
  294. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  295. bool is_expression = !expression_node.is_null();
  296. String expression = "";
  297. GraphNode *node = memnew(GraphNode);
  298. register_link(p_type, p_id, vsnode.ptr(), node);
  299. if (is_resizable) {
  300. size = resizable_node->get_size();
  301. node->set_resizable(true);
  302. node->connect("resize_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_resized), varray((int)p_type, p_id));
  303. }
  304. if (is_expression) {
  305. expression = expression_node->get_expression();
  306. }
  307. node->set_offset(visual_shader->get_node_position(p_type, p_id));
  308. node->set_title(vsnode->get_caption());
  309. node->set_name(itos(p_id));
  310. if (p_id >= 2) {
  311. node->set_show_close_button(true);
  312. node->connect("close_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_delete_node_request), varray(p_type, p_id), CONNECT_DEFERRED);
  313. }
  314. node->connect("dragged", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_dragged), varray(p_id));
  315. Control *custom_editor = nullptr;
  316. int port_offset = 0;
  317. if (is_group) {
  318. port_offset += 2;
  319. }
  320. Ref<VisualShaderNodeUniform> uniform = vsnode;
  321. if (uniform.is_valid()) {
  322. VisualShaderEditor::get_singleton()->graph->add_child(node);
  323. VisualShaderEditor::get_singleton()->_update_created_node(node);
  324. LineEdit *uniform_name = memnew(LineEdit);
  325. register_uniform_name(p_id, uniform_name);
  326. uniform_name->set_text(uniform->get_uniform_name());
  327. node->add_child(uniform_name);
  328. uniform_name->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_changed), varray(p_id));
  329. uniform_name->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_focus_out), varray(uniform_name, p_id));
  330. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  331. //shortcut
  332. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  333. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  334. if (!vsnode->is_use_prop_slots()) {
  335. return;
  336. }
  337. }
  338. port_offset++;
  339. }
  340. for (int i = 0; i < VisualShaderEditor::get_singleton()->plugins.size(); i++) {
  341. vsnode->set_meta("id", p_id);
  342. vsnode->set_meta("shader_type", (int)p_type);
  343. custom_editor = VisualShaderEditor::get_singleton()->plugins.write[i]->create_editor(visual_shader, vsnode);
  344. vsnode->remove_meta("id");
  345. vsnode->remove_meta("shader_type");
  346. if (custom_editor) {
  347. if (vsnode->is_show_prop_names()) {
  348. custom_editor->call_deferred("_show_prop_names", true);
  349. }
  350. break;
  351. }
  352. }
  353. Ref<VisualShaderNodeCurveTexture> curve = vsnode;
  354. if (curve.is_valid()) {
  355. if (curve->get_texture().is_valid() && !curve->get_texture()->is_connected("changed", callable_mp(VisualShaderEditor::get_singleton()->get_graph_plugin(), &VisualShaderGraphPlugin::update_curve))) {
  356. curve->get_texture()->connect("changed", callable_mp(VisualShaderEditor::get_singleton()->get_graph_plugin(), &VisualShaderGraphPlugin::update_curve), varray(p_id));
  357. }
  358. HBoxContainer *hbox = memnew(HBoxContainer);
  359. custom_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  360. hbox->add_child(custom_editor);
  361. custom_editor = hbox;
  362. }
  363. Ref<VisualShaderNodeFloatConstant> float_const = vsnode;
  364. if (float_const.is_valid()) {
  365. HBoxContainer *hbox = memnew(HBoxContainer);
  366. hbox->add_child(custom_editor);
  367. OptionButton *btn = memnew(OptionButton);
  368. hbox->add_child(btn);
  369. register_constant_option_btn(p_id, btn);
  370. btn->add_item("");
  371. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  372. btn->add_item(float_constant_defs[i].name);
  373. }
  374. btn->select(get_constant_index(float_const->get_constant()));
  375. btn->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_float_constant_selected), varray(p_id));
  376. custom_editor = hbox;
  377. }
  378. if (custom_editor && !vsnode->is_use_prop_slots() && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  379. //will be embedded in first port
  380. } else if (custom_editor) {
  381. port_offset++;
  382. node->add_child(custom_editor);
  383. if (curve.is_valid()) {
  384. VisualShaderEditor::get_singleton()->graph->add_child(node);
  385. VisualShaderEditor::get_singleton()->_update_created_node(node);
  386. CurveEditor *curve_editor = memnew(CurveEditor);
  387. node->add_child(curve_editor);
  388. register_curve_editor(p_id, curve_editor);
  389. curve_editor->set_custom_minimum_size(Size2(300, 0));
  390. curve_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  391. if (curve->get_texture().is_valid()) {
  392. curve_editor->set_curve(curve->get_texture()->get_curve());
  393. }
  394. TextureButton *preview = memnew(TextureButton);
  395. preview->set_toggle_mode(true);
  396. preview->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityHidden", "EditorIcons"));
  397. preview->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityVisible", "EditorIcons"));
  398. preview->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  399. register_output_port(p_id, 0, preview);
  400. preview->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_preview_select_port), varray(p_id, 0), CONNECT_DEFERRED);
  401. custom_editor->add_child(preview);
  402. VisualShaderNode::PortType port_left = vsnode->get_input_port_type(0);
  403. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  404. node->set_slot(0, true, port_left, type_color[port_left], true, port_right, type_color[port_right]);
  405. VisualShaderEditor::get_singleton()->call_deferred("_set_node_size", (int)p_type, p_id, size);
  406. }
  407. if (vsnode->is_use_prop_slots()) {
  408. return;
  409. }
  410. custom_editor = nullptr;
  411. }
  412. if (is_group) {
  413. offset = memnew(Control);
  414. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  415. node->add_child(offset);
  416. if (group_node->is_editable()) {
  417. HBoxContainer *hb2 = memnew(HBoxContainer);
  418. Button *add_input_btn = memnew(Button);
  419. add_input_btn->set_text(TTR("Add Input"));
  420. add_input_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_add_input_port), varray(p_id, group_node->get_free_input_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "input" + itos(group_node->get_free_input_port_id())), CONNECT_DEFERRED);
  421. hb2->add_child(add_input_btn);
  422. hb2->add_spacer();
  423. Button *add_output_btn = memnew(Button);
  424. add_output_btn->set_text(TTR("Add Output"));
  425. add_output_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_add_output_port), varray(p_id, group_node->get_free_output_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "output" + itos(group_node->get_free_output_port_id())), CONNECT_DEFERRED);
  426. hb2->add_child(add_output_btn);
  427. node->add_child(hb2);
  428. }
  429. }
  430. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  431. if (vsnode->is_port_separator(i)) {
  432. node->add_child(memnew(HSeparator));
  433. port_offset++;
  434. }
  435. bool valid_left = i < vsnode->get_input_port_count();
  436. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  437. bool port_left_used = false;
  438. String name_left;
  439. if (valid_left) {
  440. name_left = vsnode->get_input_port_name(i);
  441. port_left = vsnode->get_input_port_type(i);
  442. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  443. if (E->get().to_node == p_id && E->get().to_port == i) {
  444. port_left_used = true;
  445. }
  446. }
  447. }
  448. bool valid_right = i < vsnode->get_output_port_count();
  449. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  450. String name_right;
  451. if (valid_right) {
  452. name_right = vsnode->get_output_port_name(i);
  453. port_right = vsnode->get_output_port_type(i);
  454. }
  455. HBoxContainer *hb = memnew(HBoxContainer);
  456. hb->add_theme_constant_override("separation", 7 * EDSCALE);
  457. Variant default_value;
  458. if (valid_left && !port_left_used) {
  459. default_value = vsnode->get_input_port_default_value(i);
  460. }
  461. Button *button = memnew(Button);
  462. hb->add_child(button);
  463. register_default_input_button(p_id, i, button);
  464. button->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_edit_port_default_input), varray(button, p_id, i));
  465. if (default_value.get_type() != Variant::NIL) { // only a label
  466. set_input_port_default_value(p_type, p_id, i, default_value);
  467. } else {
  468. button->hide();
  469. }
  470. if (i == 0 && custom_editor) {
  471. hb->add_child(custom_editor);
  472. custom_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  473. } else {
  474. if (valid_left) {
  475. if (is_group) {
  476. OptionButton *type_box = memnew(OptionButton);
  477. hb->add_child(type_box);
  478. type_box->add_item(TTR("Float"));
  479. type_box->add_item(TTR("Int"));
  480. type_box->add_item(TTR("Vector"));
  481. type_box->add_item(TTR("Boolean"));
  482. type_box->add_item(TTR("Transform"));
  483. type_box->add_item(TTR("Sampler"));
  484. type_box->select(group_node->get_input_port_type(i));
  485. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  486. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_type), varray(p_id, i), CONNECT_DEFERRED);
  487. LineEdit *name_box = memnew(LineEdit);
  488. hb->add_child(name_box);
  489. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  490. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  491. name_box->set_text(name_left);
  492. name_box->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_name), varray(name_box, p_id, i));
  493. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, false));
  494. Button *remove_btn = memnew(Button);
  495. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  496. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  497. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_input_port), varray(p_id, i), CONNECT_DEFERRED);
  498. hb->add_child(remove_btn);
  499. } else {
  500. Label *label = memnew(Label);
  501. label->set_text(name_left);
  502. label->add_theme_style_override("normal", label_style); //more compact
  503. hb->add_child(label);
  504. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  505. Label *hint_label = memnew(Label);
  506. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  507. hint_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("font_color_readonly", "TextEdit"));
  508. hint_label->add_theme_style_override("normal", label_style);
  509. hb->add_child(hint_label);
  510. }
  511. }
  512. }
  513. if (!is_group) {
  514. hb->add_spacer();
  515. }
  516. if (valid_right) {
  517. if (is_group) {
  518. Button *remove_btn = memnew(Button);
  519. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  520. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  521. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_output_port), varray(p_id, i), CONNECT_DEFERRED);
  522. hb->add_child(remove_btn);
  523. LineEdit *name_box = memnew(LineEdit);
  524. hb->add_child(name_box);
  525. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  526. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  527. name_box->set_text(name_right);
  528. name_box->connect("text_entered", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_name), varray(name_box, p_id, i));
  529. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, true));
  530. OptionButton *type_box = memnew(OptionButton);
  531. hb->add_child(type_box);
  532. type_box->add_item(TTR("Float"));
  533. type_box->add_item(TTR("Int"));
  534. type_box->add_item(TTR("Vector"));
  535. type_box->add_item(TTR("Boolean"));
  536. type_box->add_item(TTR("Transform"));
  537. type_box->select(group_node->get_output_port_type(i));
  538. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  539. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_type), varray(p_id, i), CONNECT_DEFERRED);
  540. } else {
  541. Label *label = memnew(Label);
  542. label->set_text(name_right);
  543. label->add_theme_style_override("normal", label_style); //more compact
  544. hb->add_child(label);
  545. }
  546. }
  547. }
  548. if (valid_right && visual_shader->get_shader_type() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  549. TextureButton *preview = memnew(TextureButton);
  550. preview->set_toggle_mode(true);
  551. preview->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityHidden", "EditorIcons"));
  552. preview->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityVisible", "EditorIcons"));
  553. preview->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  554. register_output_port(p_id, i, preview);
  555. preview->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_preview_select_port), varray(p_id, i), CONNECT_DEFERRED);
  556. hb->add_child(preview);
  557. }
  558. if (is_group) {
  559. offset = memnew(Control);
  560. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  561. node->add_child(offset);
  562. port_offset++;
  563. }
  564. node->add_child(hb);
  565. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  566. }
  567. if (vsnode->get_output_port_for_preview() >= 0) {
  568. show_port_preview(p_type, p_id, vsnode->get_output_port_for_preview());
  569. }
  570. offset = memnew(Control);
  571. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  572. node->add_child(offset);
  573. String error = vsnode->get_warning(visual_shader->get_mode(), p_type);
  574. if (error != String()) {
  575. Label *error_label = memnew(Label);
  576. error_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("error_color", "Editor"));
  577. error_label->set_text(error);
  578. node->add_child(error_label);
  579. }
  580. if (is_expression) {
  581. CodeEdit *expression_box = memnew(CodeEdit);
  582. Ref<CodeHighlighter> expression_syntax_highlighter;
  583. expression_syntax_highlighter.instance();
  584. expression_node->set_control(expression_box, 0);
  585. node->add_child(expression_box);
  586. register_expression_edit(p_id, expression_box);
  587. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  588. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  589. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  590. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  591. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  592. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  593. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  594. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  595. expression_box->set_syntax_highlighter(expression_syntax_highlighter);
  596. expression_box->add_theme_color_override("background_color", background_color);
  597. for (List<String>::Element *E = VisualShaderEditor::get_singleton()->keyword_list.front(); E; E = E->next()) {
  598. expression_syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  599. }
  600. expression_box->add_theme_font_override("font", VisualShaderEditor::get_singleton()->get_theme_font("expression", "EditorFonts"));
  601. expression_box->add_theme_color_override("font_color", text_color);
  602. expression_syntax_highlighter->set_number_color(number_color);
  603. expression_syntax_highlighter->set_symbol_color(symbol_color);
  604. expression_syntax_highlighter->set_function_color(function_color);
  605. expression_syntax_highlighter->set_member_variable_color(members_color);
  606. expression_syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  607. expression_syntax_highlighter->add_color_region("//", "", comment_color, true);
  608. expression_box->set_text(expression);
  609. expression_box->set_context_menu_enabled(false);
  610. expression_box->set_draw_line_numbers(true);
  611. expression_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_expression_focus_out), varray(expression_box, p_id));
  612. }
  613. if (!uniform.is_valid()) {
  614. VisualShaderEditor::get_singleton()->graph->add_child(node);
  615. VisualShaderEditor::get_singleton()->_update_created_node(node);
  616. if (is_resizable) {
  617. VisualShaderEditor::get_singleton()->call_deferred("_set_node_size", (int)p_type, p_id, size);
  618. }
  619. }
  620. }
  621. void VisualShaderGraphPlugin::remove_node(VisualShader::Type p_type, int p_id) {
  622. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  623. links[p_id].graph_node->get_parent()->remove_child(links[p_id].graph_node);
  624. memdelete(links[p_id].graph_node);
  625. links.erase(p_id);
  626. }
  627. }
  628. void VisualShaderGraphPlugin::connect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  629. if (visual_shader->get_shader_type() == p_type) {
  630. VisualShaderEditor::get_singleton()->graph->connect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  631. if (links[p_to_node].input_ports.has(p_to_port) && links[p_to_node].input_ports[p_to_port].default_input_button != nullptr) {
  632. links[p_to_node].input_ports[p_to_port].default_input_button->hide();
  633. }
  634. }
  635. }
  636. void VisualShaderGraphPlugin::disconnect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  637. if (visual_shader->get_shader_type() == p_type) {
  638. VisualShaderEditor::get_singleton()->graph->disconnect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  639. if (links[p_to_node].input_ports.has(p_to_port) && links[p_to_node].input_ports[p_to_port].default_input_button != nullptr && links[p_to_node].visual_node->get_input_port_default_value(p_to_port).get_type() != Variant::NIL) {
  640. links[p_to_node].input_ports[p_to_port].default_input_button->show();
  641. set_input_port_default_value(p_type, p_to_node, p_to_port, links[p_to_node].visual_node->get_input_port_default_value(p_to_port));
  642. }
  643. }
  644. }
  645. VisualShaderGraphPlugin::~VisualShaderGraphPlugin() {
  646. }
  647. /////////////////
  648. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  649. bool changed = false;
  650. if (p_visual_shader) {
  651. if (visual_shader.is_null()) {
  652. changed = true;
  653. } else {
  654. if (visual_shader.ptr() != p_visual_shader) {
  655. changed = true;
  656. }
  657. }
  658. visual_shader = Ref<VisualShader>(p_visual_shader);
  659. graph_plugin->register_shader(visual_shader.ptr());
  660. if (!visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  661. visual_shader->connect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  662. }
  663. #ifndef DISABLE_DEPRECATED
  664. String version = VERSION_BRANCH;
  665. if (visual_shader->get_version() != version) {
  666. visual_shader->update_version(version);
  667. }
  668. #endif
  669. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  670. _set_mode(visual_shader->get_mode());
  671. } else {
  672. if (visual_shader.is_valid()) {
  673. if (visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  674. visual_shader->disconnect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  675. }
  676. }
  677. visual_shader.unref();
  678. }
  679. if (visual_shader.is_null()) {
  680. hide();
  681. } else {
  682. if (changed) { // to avoid tree collapse
  683. _clear_buffer();
  684. _update_options_menu();
  685. _update_preview();
  686. _update_graph();
  687. }
  688. }
  689. }
  690. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  691. if (plugins.find(p_plugin) != -1) {
  692. return;
  693. }
  694. plugins.push_back(p_plugin);
  695. }
  696. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  697. plugins.erase(p_plugin);
  698. }
  699. void VisualShaderEditor::clear_custom_types() {
  700. for (int i = 0; i < add_options.size(); i++) {
  701. if (add_options[i].is_custom) {
  702. add_options.remove(i);
  703. i--;
  704. }
  705. }
  706. }
  707. 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, bool p_highend) {
  708. ERR_FAIL_COND(!p_name.is_valid_identifier());
  709. ERR_FAIL_COND(!p_script.is_valid());
  710. for (int i = 0; i < add_options.size(); i++) {
  711. if (add_options[i].is_custom) {
  712. if (add_options[i].script == p_script) {
  713. return;
  714. }
  715. }
  716. }
  717. AddOption ao;
  718. ao.name = p_name;
  719. ao.script = p_script;
  720. ao.return_type = p_return_icon_type;
  721. ao.description = p_description;
  722. ao.category = p_category;
  723. ao.highend = p_highend;
  724. ao.is_custom = true;
  725. bool begin = false;
  726. String root = p_category.split("/")[0];
  727. for (int i = 0; i < add_options.size(); i++) {
  728. if (add_options[i].is_custom) {
  729. if (add_options[i].category == root) {
  730. if (!begin) {
  731. begin = true;
  732. }
  733. } else {
  734. if (begin) {
  735. add_options.insert(i, ao);
  736. return;
  737. }
  738. }
  739. }
  740. }
  741. add_options.push_back(ao);
  742. }
  743. bool VisualShaderEditor::_is_available(int p_mode) {
  744. int current_mode = edit_type->get_selected();
  745. if (p_mode != -1) {
  746. switch (current_mode) {
  747. case 0: // Vertex or Emit
  748. current_mode = 1;
  749. break;
  750. case 1: // Fragment or Process
  751. current_mode = 2;
  752. break;
  753. case 2: // Light or End
  754. current_mode = 4;
  755. break;
  756. default:
  757. break;
  758. }
  759. }
  760. return (p_mode == -1 || (p_mode & current_mode) != 0);
  761. }
  762. void VisualShaderEditor::update_custom_nodes() {
  763. if (members_dialog->is_visible()) {
  764. return;
  765. }
  766. clear_custom_types();
  767. List<StringName> class_list;
  768. ScriptServer::get_global_class_list(&class_list);
  769. Dictionary added;
  770. for (int i = 0; i < class_list.size(); i++) {
  771. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  772. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  773. Ref<Resource> res = ResourceLoader::load(script_path);
  774. ERR_FAIL_COND(res.is_null());
  775. ERR_FAIL_COND(!res->is_class("Script"));
  776. Ref<Script> script = Ref<Script>(res);
  777. Ref<VisualShaderNodeCustom> ref;
  778. ref.instance();
  779. ref->set_script(script);
  780. String name;
  781. if (ref->has_method("_get_name")) {
  782. name = (String)ref->call("_get_name");
  783. } else {
  784. name = "Unnamed";
  785. }
  786. String description = "";
  787. if (ref->has_method("_get_description")) {
  788. description = (String)ref->call("_get_description");
  789. }
  790. int return_icon_type = -1;
  791. if (ref->has_method("_get_return_icon_type")) {
  792. return_icon_type = (int)ref->call("_get_return_icon_type");
  793. }
  794. String category = "";
  795. if (ref->has_method("_get_category")) {
  796. category = (String)ref->call("_get_category");
  797. }
  798. String subcategory = "";
  799. if (ref->has_method("_get_subcategory")) {
  800. subcategory = (String)ref->call("_get_subcategory");
  801. }
  802. bool highend = false;
  803. if (ref->has_method("_is_highend")) {
  804. highend = (bool)ref->call("_is_highend");
  805. }
  806. Dictionary dict;
  807. dict["name"] = name;
  808. dict["script"] = script;
  809. dict["description"] = description;
  810. dict["return_icon_type"] = return_icon_type;
  811. category = category.rstrip("/");
  812. category = category.lstrip("/");
  813. category = "Addons/" + category;
  814. if (subcategory != "") {
  815. category += "/" + subcategory;
  816. }
  817. dict["category"] = category;
  818. dict["highend"] = highend;
  819. String key;
  820. key = category + "/" + name;
  821. added[key] = dict;
  822. }
  823. }
  824. Array keys = added.keys();
  825. keys.sort();
  826. for (int i = 0; i < keys.size(); i++) {
  827. const Variant &key = keys.get(i);
  828. const Dictionary &value = (Dictionary)added[key];
  829. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["highend"]);
  830. }
  831. _update_options_menu();
  832. }
  833. String VisualShaderEditor::_get_description(int p_idx) {
  834. return add_options[p_idx].description;
  835. }
  836. void VisualShaderEditor::_update_options_menu() {
  837. node_desc->set_text("");
  838. members_dialog->get_ok()->set_disabled(true);
  839. members->clear();
  840. TreeItem *root = members->create_item();
  841. String filter = node_filter->get_text().strip_edges();
  842. bool use_filter = !filter.empty();
  843. bool is_first_item = true;
  844. Color unsupported_color = get_theme_color("error_color", "Editor");
  845. Color supported_color = get_theme_color("warning_color", "Editor");
  846. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES2";
  847. Map<String, TreeItem *> folders;
  848. int current_func = -1;
  849. if (!visual_shader.is_null()) {
  850. current_func = visual_shader->get_mode();
  851. }
  852. Vector<AddOption> custom_options;
  853. Vector<AddOption> embedded_options;
  854. for (int i = 0; i < add_options.size(); i++) {
  855. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  856. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode)) {
  857. continue;
  858. }
  859. const_cast<AddOption &>(add_options[i]).temp_idx = i; // save valid id
  860. if (add_options[i].is_custom) {
  861. custom_options.push_back(add_options[i]);
  862. } else {
  863. embedded_options.push_back(add_options[i]);
  864. }
  865. }
  866. }
  867. Vector<AddOption> options;
  868. SortArray<AddOption, _OptionComparator> sorter;
  869. sorter.sort(custom_options.ptrw(), custom_options.size());
  870. options.append_array(custom_options);
  871. options.append_array(embedded_options);
  872. for (int i = 0; i < options.size(); i++) {
  873. String path = options[i].category;
  874. Vector<String> subfolders = path.split("/");
  875. TreeItem *category = nullptr;
  876. if (!folders.has(path)) {
  877. category = root;
  878. String path_temp = "";
  879. for (int j = 0; j < subfolders.size(); j++) {
  880. path_temp += subfolders[j];
  881. if (!folders.has(path_temp)) {
  882. category = members->create_item(category);
  883. category->set_selectable(0, false);
  884. category->set_collapsed(!use_filter);
  885. category->set_text(0, subfolders[j]);
  886. folders.insert(path_temp, category);
  887. } else {
  888. category = folders[path_temp];
  889. }
  890. }
  891. } else {
  892. category = folders[path];
  893. }
  894. TreeItem *item = members->create_item(category);
  895. if (options[i].highend && low_driver) {
  896. item->set_custom_color(0, unsupported_color);
  897. } else if (options[i].highend) {
  898. item->set_custom_color(0, supported_color);
  899. }
  900. item->set_text(0, options[i].name);
  901. if (is_first_item && use_filter) {
  902. item->select(0);
  903. node_desc->set_text(options[i].description);
  904. is_first_item = false;
  905. }
  906. switch (options[i].return_type) {
  907. case VisualShaderNode::PORT_TYPE_SCALAR:
  908. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"));
  909. break;
  910. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  911. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"));
  912. break;
  913. case VisualShaderNode::PORT_TYPE_VECTOR:
  914. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"));
  915. break;
  916. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  917. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"));
  918. break;
  919. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  920. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"));
  921. break;
  922. case VisualShaderNode::PORT_TYPE_SAMPLER:
  923. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"));
  924. break;
  925. default:
  926. break;
  927. }
  928. item->set_meta("id", options[i].temp_idx);
  929. }
  930. }
  931. void VisualShaderEditor::_set_mode(int p_which) {
  932. if (p_which == VisualShader::MODE_PARTICLES) {
  933. edit_type_standart->set_visible(false);
  934. edit_type_particles->set_visible(true);
  935. edit_type = edit_type_particles;
  936. particles_mode = true;
  937. } else {
  938. edit_type_particles->set_visible(false);
  939. edit_type_standart->set_visible(true);
  940. edit_type = edit_type_standart;
  941. particles_mode = false;
  942. }
  943. visual_shader->set_shader_type(get_current_shader_type());
  944. }
  945. Size2 VisualShaderEditor::get_minimum_size() const {
  946. return Size2(10, 200);
  947. }
  948. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  949. Button *button = Object::cast_to<Button>(obj);
  950. if (!button) {
  951. return;
  952. }
  953. Ref<StyleBox> normal = get_theme_stylebox("normal", "Button");
  954. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  955. }
  956. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  957. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  958. Ref<StyleBoxFlat> sb = node->get_theme_stylebox("frame", "GraphNode");
  959. Color c = sb->get_border_color();
  960. Color ic;
  961. Color mono_color;
  962. if (((c.r + c.g + c.b) / 3) < 0.7) {
  963. mono_color = Color(1.0, 1.0, 1.0);
  964. ic = Color(0.0, 0.0, 0.0, 0.7);
  965. } else {
  966. mono_color = Color(0.0, 0.0, 0.0);
  967. ic = Color(1.0, 1.0, 1.0, 0.7);
  968. }
  969. mono_color.a = 0.85;
  970. c = mono_color;
  971. node->add_theme_color_override("title_color", c);
  972. c.a = 0.7;
  973. node->add_theme_color_override("close_color", c);
  974. node->add_theme_color_override("resizer_color", ic);
  975. }
  976. }
  977. void VisualShaderEditor::_update_uniforms(bool p_update_refs) {
  978. VisualShaderNodeUniformRef::clear_uniforms();
  979. for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
  980. Vector<int> tnodes = visual_shader->get_node_list((VisualShader::Type)t);
  981. for (int i = 0; i < tnodes.size(); i++) {
  982. Ref<VisualShaderNode> vsnode = visual_shader->get_node((VisualShader::Type)t, tnodes[i]);
  983. Ref<VisualShaderNodeUniform> uniform = vsnode;
  984. if (uniform.is_valid()) {
  985. Ref<VisualShaderNodeFloatUniform> float_uniform = vsnode;
  986. Ref<VisualShaderNodeIntUniform> int_uniform = vsnode;
  987. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = vsnode;
  988. Ref<VisualShaderNodeColorUniform> color_uniform = vsnode;
  989. Ref<VisualShaderNodeBooleanUniform> bool_uniform = vsnode;
  990. Ref<VisualShaderNodeTransformUniform> transform_uniform = vsnode;
  991. VisualShaderNodeUniformRef::UniformType uniform_type;
  992. if (float_uniform.is_valid()) {
  993. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_FLOAT;
  994. } else if (int_uniform.is_valid()) {
  995. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_INT;
  996. } else if (bool_uniform.is_valid()) {
  997. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_BOOLEAN;
  998. } else if (vec3_uniform.is_valid()) {
  999. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_VECTOR;
  1000. } else if (transform_uniform.is_valid()) {
  1001. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_TRANSFORM;
  1002. } else if (color_uniform.is_valid()) {
  1003. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_COLOR;
  1004. } else {
  1005. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SAMPLER;
  1006. }
  1007. VisualShaderNodeUniformRef::add_uniform(uniform->get_uniform_name(), uniform_type);
  1008. }
  1009. }
  1010. }
  1011. if (p_update_refs) {
  1012. graph_plugin->update_uniform_refs();
  1013. }
  1014. }
  1015. void VisualShaderEditor::_update_uniform_refs(Set<String> &p_deleted_names) {
  1016. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  1017. VisualShader::Type type = VisualShader::Type(i);
  1018. Vector<int> nodes = visual_shader->get_node_list(type);
  1019. for (int j = 0; j < nodes.size(); j++) {
  1020. if (j > 0) {
  1021. Ref<VisualShaderNodeUniformRef> ref = visual_shader->get_node(type, nodes[j]);
  1022. if (ref.is_valid()) {
  1023. if (p_deleted_names.has(ref->get_uniform_name())) {
  1024. undo_redo->add_do_method(ref.ptr(), "set_uniform_name", "[None]");
  1025. undo_redo->add_undo_method(ref.ptr(), "set_uniform_name", ref->get_uniform_name());
  1026. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  1027. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  1028. }
  1029. }
  1030. }
  1031. }
  1032. }
  1033. }
  1034. void VisualShaderEditor::_update_graph() {
  1035. if (updating) {
  1036. return;
  1037. }
  1038. if (visual_shader.is_null()) {
  1039. return;
  1040. }
  1041. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  1042. VisualShader::Type type = get_current_shader_type();
  1043. graph->clear_connections();
  1044. //erase all nodes
  1045. for (int i = 0; i < graph->get_child_count(); i++) {
  1046. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  1047. Node *node = graph->get_child(i);
  1048. graph->remove_child(node);
  1049. memdelete(node);
  1050. i--;
  1051. }
  1052. }
  1053. List<VisualShader::Connection> connections;
  1054. visual_shader->get_node_connections(type, &connections);
  1055. graph_plugin->set_connections(connections);
  1056. Vector<int> nodes = visual_shader->get_node_list(type);
  1057. _update_uniforms(false);
  1058. graph_plugin->clear_links();
  1059. graph_plugin->make_dirty(true);
  1060. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  1061. graph_plugin->add_node(type, nodes[n_i]);
  1062. }
  1063. graph_plugin->make_dirty(false);
  1064. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  1065. int from = E->get().from_node;
  1066. int from_idx = E->get().from_port;
  1067. int to = E->get().to_node;
  1068. int to_idx = E->get().to_port;
  1069. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  1070. }
  1071. }
  1072. VisualShader::Type VisualShaderEditor::get_current_shader_type() const {
  1073. VisualShader::Type type;
  1074. if (particles_mode) {
  1075. type = VisualShader::Type(edit_type->get_selected() + 3);
  1076. } else {
  1077. type = VisualShader::Type(edit_type->get_selected());
  1078. }
  1079. return type;
  1080. }
  1081. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  1082. VisualShader::Type type = get_current_shader_type();
  1083. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  1084. if (node.is_null()) {
  1085. return;
  1086. }
  1087. undo_redo->create_action(TTR("Add Input Port"));
  1088. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  1089. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  1090. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1091. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1092. undo_redo->commit_action();
  1093. }
  1094. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  1095. VisualShader::Type type = get_current_shader_type();
  1096. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1097. if (node.is_null()) {
  1098. return;
  1099. }
  1100. undo_redo->create_action(TTR("Add Output Port"));
  1101. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  1102. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  1103. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1104. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1105. undo_redo->commit_action();
  1106. }
  1107. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  1108. VisualShader::Type type = get_current_shader_type();
  1109. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1110. if (node.is_null()) {
  1111. return;
  1112. }
  1113. undo_redo->create_action(TTR("Change Input Port Type"));
  1114. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  1115. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  1116. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1117. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1118. undo_redo->commit_action();
  1119. }
  1120. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  1121. VisualShader::Type type = get_current_shader_type();
  1122. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1123. if (node.is_null()) {
  1124. return;
  1125. }
  1126. undo_redo->create_action(TTR("Change Output Port Type"));
  1127. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  1128. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  1129. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1130. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1131. undo_redo->commit_action();
  1132. }
  1133. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  1134. VisualShader::Type type = get_current_shader_type();
  1135. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1136. ERR_FAIL_COND(!node.is_valid());
  1137. undo_redo->create_action(TTR("Change Input Port Name"));
  1138. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, p_text);
  1139. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  1140. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1141. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1142. undo_redo->commit_action();
  1143. }
  1144. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  1145. VisualShader::Type type = get_current_shader_type();
  1146. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1147. ERR_FAIL_COND(!node.is_valid());
  1148. undo_redo->create_action(TTR("Change Output Port Name"));
  1149. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, p_text);
  1150. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, node->get_output_port_name(p_port_id));
  1151. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1152. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1153. undo_redo->commit_action();
  1154. }
  1155. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  1156. VisualShader::Type type = get_current_shader_type();
  1157. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1158. if (node.is_null()) {
  1159. return;
  1160. }
  1161. undo_redo->create_action(TTR("Remove Input Port"));
  1162. List<VisualShader::Connection> conns;
  1163. visual_shader->get_node_connections(type, &conns);
  1164. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1165. int from_node = E->get().from_node;
  1166. int from_port = E->get().from_port;
  1167. int to_node = E->get().to_node;
  1168. int to_port = E->get().to_port;
  1169. if (to_node == p_node) {
  1170. if (to_port == p_port) {
  1171. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1172. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1173. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1174. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1175. } else if (to_port > p_port) {
  1176. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1177. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1178. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1179. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1180. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  1181. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1182. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1183. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1184. }
  1185. }
  1186. }
  1187. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  1188. 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));
  1189. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1190. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1191. undo_redo->commit_action();
  1192. }
  1193. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  1194. VisualShader::Type type = get_current_shader_type();
  1195. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1196. if (node.is_null()) {
  1197. return;
  1198. }
  1199. undo_redo->create_action(TTR("Remove Output Port"));
  1200. List<VisualShader::Connection> conns;
  1201. visual_shader->get_node_connections(type, &conns);
  1202. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1203. int from_node = E->get().from_node;
  1204. int from_port = E->get().from_port;
  1205. int to_node = E->get().to_node;
  1206. int to_port = E->get().to_port;
  1207. if (from_node == p_node) {
  1208. if (from_port == p_port) {
  1209. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1210. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1211. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1212. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1213. } else if (from_port > p_port) {
  1214. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1215. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1216. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1217. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1218. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  1219. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1220. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1221. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1222. }
  1223. }
  1224. }
  1225. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  1226. 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));
  1227. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1228. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1229. undo_redo->commit_action();
  1230. }
  1231. void VisualShaderEditor::_expression_focus_out(Object *code_edit, int p_node) {
  1232. VisualShader::Type type = get_current_shader_type();
  1233. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  1234. if (node.is_null()) {
  1235. return;
  1236. }
  1237. CodeEdit *expression_box = Object::cast_to<CodeEdit>(code_edit);
  1238. if (node->get_expression() == expression_box->get_text()) {
  1239. return;
  1240. }
  1241. undo_redo->create_action(TTR("Set VisualShader Expression"));
  1242. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  1243. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  1244. undo_redo->add_do_method(graph_plugin.ptr(), "set_expression", type, p_node, expression_box->get_text());
  1245. undo_redo->add_undo_method(graph_plugin.ptr(), "set_expression", type, p_node, node->get_expression());
  1246. undo_redo->commit_action();
  1247. }
  1248. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  1249. VisualShader::Type type = VisualShader::Type(p_type);
  1250. Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(type, p_node);
  1251. if (node.is_null()) {
  1252. return;
  1253. }
  1254. Size2 size = p_size;
  1255. if (!node->is_allow_v_resize()) {
  1256. size.y = 0;
  1257. }
  1258. node->set_size(size);
  1259. if (get_current_shader_type() == type) {
  1260. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1261. Control *text_box = nullptr;
  1262. if (!expression_node.is_null()) {
  1263. text_box = expression_node->get_control(0);
  1264. if (text_box) {
  1265. text_box->set_custom_minimum_size(Size2(0, 0));
  1266. }
  1267. }
  1268. GraphNode *gn = nullptr;
  1269. Node *node2 = graph->get_node(itos(p_node));
  1270. gn = Object::cast_to<GraphNode>(node2);
  1271. if (!gn) {
  1272. return;
  1273. }
  1274. gn->set_custom_minimum_size(size);
  1275. gn->set_size(Size2(1, 1));
  1276. if (!expression_node.is_null() && text_box) {
  1277. Size2 box_size = size;
  1278. if (gn != nullptr) {
  1279. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  1280. box_size.x = gn->get_size().x;
  1281. }
  1282. }
  1283. box_size.x -= text_box->get_margin(MARGIN_LEFT);
  1284. box_size.x -= 28 * EDSCALE;
  1285. box_size.y -= text_box->get_margin(MARGIN_TOP);
  1286. box_size.y -= 28 * EDSCALE;
  1287. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  1288. text_box->set_size(Size2(1, 1));
  1289. }
  1290. }
  1291. }
  1292. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  1293. Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(VisualShader::Type(p_type), p_node);
  1294. if (node.is_null()) {
  1295. return;
  1296. }
  1297. undo_redo->create_action(TTR("Resize VisualShader Node"), UndoRedo::MERGE_ENDS);
  1298. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  1299. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  1300. undo_redo->commit_action();
  1301. }
  1302. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  1303. VisualShader::Type type = get_current_shader_type();
  1304. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1305. if (node.is_null()) {
  1306. return;
  1307. }
  1308. int prev_port = node->get_output_port_for_preview();
  1309. if (node->get_output_port_for_preview() == p_port) {
  1310. p_port = -1; //toggle it
  1311. }
  1312. undo_redo->create_action(p_port == -1 ? TTR("Hide Port Preview") : TTR("Show Port Preview"));
  1313. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  1314. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", prev_port);
  1315. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, p_node);
  1316. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, p_node);
  1317. undo_redo->commit_action();
  1318. }
  1319. void VisualShaderEditor::_uniform_line_edit_changed(const String &p_text, int p_node_id) {
  1320. VisualShader::Type type = get_current_shader_type();
  1321. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  1322. ERR_FAIL_COND(!node.is_valid());
  1323. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  1324. if (validated_name == node->get_uniform_name()) {
  1325. return;
  1326. }
  1327. undo_redo->create_action(TTR("Set Uniform Name"));
  1328. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  1329. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  1330. undo_redo->add_do_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, validated_name);
  1331. undo_redo->add_undo_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, node->get_uniform_name());
  1332. undo_redo->add_do_method(this, "_update_uniforms", true);
  1333. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1334. Set<String> changed_names;
  1335. changed_names.insert(node->get_uniform_name());
  1336. _update_uniform_refs(changed_names);
  1337. undo_redo->commit_action();
  1338. }
  1339. void VisualShaderEditor::_uniform_line_edit_focus_out(Object *line_edit, int p_node_id) {
  1340. _uniform_line_edit_changed(Object::cast_to<LineEdit>(line_edit)->get_text(), p_node_id);
  1341. }
  1342. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  1343. VisualShader::Type type = get_current_shader_type();
  1344. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1345. ERR_FAIL_COND(!node.is_valid());
  1346. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1347. if (!p_output) {
  1348. if (node->get_input_port_name(p_port_id) == text) {
  1349. return;
  1350. }
  1351. } else {
  1352. if (node->get_output_port_name(p_port_id) == text) {
  1353. return;
  1354. }
  1355. }
  1356. List<String> input_names;
  1357. List<String> output_names;
  1358. for (int i = 0; i < node->get_input_port_count(); i++) {
  1359. if (!p_output && i == p_port_id) {
  1360. continue;
  1361. }
  1362. input_names.push_back(node->get_input_port_name(i));
  1363. }
  1364. for (int i = 0; i < node->get_output_port_count(); i++) {
  1365. if (p_output && i == p_port_id) {
  1366. continue;
  1367. }
  1368. output_names.push_back(node->get_output_port_name(i));
  1369. }
  1370. String validated_name = visual_shader->validate_port_name(text, input_names, output_names);
  1371. if (validated_name == "") {
  1372. if (!p_output) {
  1373. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_input_port_name(p_port_id));
  1374. } else {
  1375. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_output_port_name(p_port_id));
  1376. }
  1377. return;
  1378. }
  1379. if (!p_output) {
  1380. _change_input_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1381. } else {
  1382. _change_output_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1383. }
  1384. }
  1385. void VisualShaderEditor::_port_edited() {
  1386. VisualShader::Type type = get_current_shader_type();
  1387. Variant value = property_editor->get_variant();
  1388. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1389. ERR_FAIL_COND(!vsn.is_valid());
  1390. undo_redo->create_action(TTR("Set Input Default Port"));
  1391. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1392. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1393. undo_redo->add_do_method(graph_plugin.ptr(), "set_input_port_default_value", type, editing_node, editing_port, value);
  1394. undo_redo->add_undo_method(graph_plugin.ptr(), "set_input_port_default_value", type, editing_node, editing_port, vsn->get_input_port_default_value(editing_port));
  1395. undo_redo->commit_action();
  1396. property_editor->hide();
  1397. }
  1398. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1399. VisualShader::Type type = get_current_shader_type();
  1400. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1401. Button *button = Object::cast_to<Button>(p_button);
  1402. ERR_FAIL_COND(!button);
  1403. Variant value = vsn->get_input_port_default_value(p_port);
  1404. property_editor->set_position(button->get_screen_position() + Vector2(0, button->get_size().height));
  1405. property_editor->edit(nullptr, "", value.get_type(), value, 0, "");
  1406. property_editor->popup();
  1407. editing_node = p_node;
  1408. editing_port = p_port;
  1409. }
  1410. void VisualShaderEditor::_add_custom_node(const String &p_path) {
  1411. int idx = -1;
  1412. for (int i = custom_node_option_idx; i < add_options.size(); i++) {
  1413. if (add_options[i].script.is_valid()) {
  1414. if (add_options[i].script->get_path() == p_path) {
  1415. idx = i;
  1416. break;
  1417. }
  1418. }
  1419. }
  1420. if (idx != -1) {
  1421. _add_node(idx);
  1422. }
  1423. }
  1424. void VisualShaderEditor::_add_cubemap_node(const String &p_path) {
  1425. VisualShaderNodeCubemap *cubemap = (VisualShaderNodeCubemap *)_add_node(cubemap_node_option_idx, -1);
  1426. cubemap->set_cube_map(ResourceLoader::load(p_path));
  1427. }
  1428. void VisualShaderEditor::_add_texture2d_node(const String &p_path) {
  1429. VisualShaderNodeTexture *texture2d = (VisualShaderNodeTexture *)_add_node(texture2d_node_option_idx, -1);
  1430. texture2d->set_texture(ResourceLoader::load(p_path));
  1431. }
  1432. void VisualShaderEditor::_add_texture2d_array_node(const String &p_path) {
  1433. VisualShaderNodeTexture2DArray *texture2d_array = (VisualShaderNodeTexture2DArray *)_add_node(texture2d_array_node_option_idx, -1);
  1434. texture2d_array->set_texture_array(ResourceLoader::load(p_path));
  1435. }
  1436. void VisualShaderEditor::_add_texture3d_node(const String &p_path) {
  1437. VisualShaderNodeTexture3D *texture3d = (VisualShaderNodeTexture3D *)_add_node(texture3d_node_option_idx, -1);
  1438. texture3d->set_texture(ResourceLoader::load(p_path));
  1439. }
  1440. void VisualShaderEditor::_add_curve_node(const String &p_path) {
  1441. VisualShaderNodeCurveTexture *curve = (VisualShaderNodeCurveTexture *)_add_node(curve_node_option_idx, -1);
  1442. curve->set_texture(ResourceLoader::load(p_path));
  1443. }
  1444. VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) {
  1445. ERR_FAIL_INDEX_V(p_idx, add_options.size(), nullptr);
  1446. Ref<VisualShaderNode> vsnode;
  1447. bool is_custom = add_options[p_idx].is_custom;
  1448. if (!is_custom && add_options[p_idx].type != String()) {
  1449. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  1450. ERR_FAIL_COND_V(!vsn, nullptr);
  1451. VisualShaderNodeFloatConstant *constant = Object::cast_to<VisualShaderNodeFloatConstant>(vsn);
  1452. if (constant) {
  1453. if ((int)add_options[p_idx].value != -1) {
  1454. constant->set_constant(add_options[p_idx].value);
  1455. }
  1456. }
  1457. if (p_op_idx != -1) {
  1458. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1459. if (input) {
  1460. input->set_input_name(add_options[p_idx].sub_func_str);
  1461. }
  1462. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(vsn);
  1463. if (is) {
  1464. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1465. }
  1466. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(vsn);
  1467. if (cmp) {
  1468. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1469. }
  1470. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(vsn);
  1471. if (colorOp) {
  1472. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1473. }
  1474. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(vsn);
  1475. if (colorFunc) {
  1476. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1477. }
  1478. VisualShaderNodeFloatOp *floatOp = Object::cast_to<VisualShaderNodeFloatOp>(vsn);
  1479. if (floatOp) {
  1480. floatOp->set_operator((VisualShaderNodeFloatOp::Operator)p_op_idx);
  1481. }
  1482. VisualShaderNodeIntOp *intOp = Object::cast_to<VisualShaderNodeIntOp>(vsn);
  1483. if (intOp) {
  1484. intOp->set_operator((VisualShaderNodeIntOp::Operator)p_op_idx);
  1485. }
  1486. VisualShaderNodeFloatFunc *floatFunc = Object::cast_to<VisualShaderNodeFloatFunc>(vsn);
  1487. if (floatFunc) {
  1488. floatFunc->set_function((VisualShaderNodeFloatFunc::Function)p_op_idx);
  1489. }
  1490. VisualShaderNodeIntFunc *intFunc = Object::cast_to<VisualShaderNodeIntFunc>(vsn);
  1491. if (intFunc) {
  1492. intFunc->set_function((VisualShaderNodeIntFunc::Function)p_op_idx);
  1493. }
  1494. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(vsn);
  1495. if (vecOp) {
  1496. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1497. }
  1498. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(vsn);
  1499. if (vecFunc) {
  1500. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1501. }
  1502. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(vsn);
  1503. if (matFunc) {
  1504. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1505. }
  1506. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(vsn);
  1507. if (sderFunc) {
  1508. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1509. }
  1510. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(vsn);
  1511. if (vderFunc) {
  1512. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1513. }
  1514. VisualShaderNodeMultiplyAdd *fmaFunc = Object::cast_to<VisualShaderNodeMultiplyAdd>(vsn);
  1515. if (fmaFunc) {
  1516. fmaFunc->set_op_type((VisualShaderNodeMultiplyAdd::OpType)p_op_idx);
  1517. }
  1518. }
  1519. vsnode = Ref<VisualShaderNode>(vsn);
  1520. } else {
  1521. ERR_FAIL_COND_V(add_options[p_idx].script.is_null(), nullptr);
  1522. String base_type = add_options[p_idx].script->get_instance_base_type();
  1523. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  1524. ERR_FAIL_COND_V(!vsn, nullptr);
  1525. vsnode = Ref<VisualShaderNode>(vsn);
  1526. vsnode->set_script(add_options[p_idx].script);
  1527. }
  1528. Point2 position = graph->get_scroll_ofs();
  1529. if (saved_node_pos_dirty) {
  1530. position += saved_node_pos;
  1531. } else {
  1532. position += graph->get_size() * 0.5;
  1533. position /= EDSCALE;
  1534. }
  1535. saved_node_pos_dirty = false;
  1536. VisualShader::Type type = get_current_shader_type();
  1537. int id_to_use = visual_shader->get_valid_node_id(type);
  1538. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1539. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1540. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1541. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_to_use);
  1542. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_to_use);
  1543. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1544. if (expr) {
  1545. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1546. }
  1547. if (to_node != -1 && to_slot != -1) {
  1548. if (vsnode->get_output_port_count() > 0) {
  1549. int _from_node = id_to_use;
  1550. int _from_slot = 0;
  1551. 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))) {
  1552. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1553. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1554. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1555. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1556. }
  1557. }
  1558. } else if (from_node != -1 && from_slot != -1) {
  1559. if (vsnode->get_input_port_count() > 0) {
  1560. int _to_node = id_to_use;
  1561. int _to_slot = 0;
  1562. 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))) {
  1563. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1564. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1565. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1566. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1567. }
  1568. }
  1569. }
  1570. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(vsnode.ptr());
  1571. if (uniform) {
  1572. undo_redo->add_do_method(this, "_update_uniforms", true);
  1573. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1574. }
  1575. VisualShaderNodeCurveTexture *curve = Object::cast_to<VisualShaderNodeCurveTexture>(vsnode.ptr());
  1576. if (curve) {
  1577. graph_plugin->call_deferred("update_curve", id_to_use);
  1578. }
  1579. undo_redo->commit_action();
  1580. return vsnode.ptr();
  1581. }
  1582. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  1583. VisualShader::Type type = get_current_shader_type();
  1584. drag_buffer.push_back({ type, p_node, p_from, p_to });
  1585. if (!drag_dirty) {
  1586. call_deferred("_nodes_dragged");
  1587. }
  1588. drag_dirty = true;
  1589. }
  1590. void VisualShaderEditor::_nodes_dragged() {
  1591. drag_dirty = false;
  1592. undo_redo->create_action(TTR("Node(s) Moved"));
  1593. for (List<DragOp>::Element *E = drag_buffer.front(); E; E = E->next()) {
  1594. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1595. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1596. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1597. undo_redo->add_undo_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1598. }
  1599. drag_buffer.clear();
  1600. undo_redo->commit_action();
  1601. }
  1602. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1603. VisualShader::Type type = get_current_shader_type();
  1604. int from = p_from.to_int();
  1605. int to = p_to.to_int();
  1606. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  1607. return;
  1608. }
  1609. undo_redo->create_action(TTR("Nodes Connected"));
  1610. List<VisualShader::Connection> conns;
  1611. visual_shader->get_node_connections(type, &conns);
  1612. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1613. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  1614. 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);
  1615. 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);
  1616. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1617. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1618. }
  1619. }
  1620. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1621. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1622. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1623. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1624. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, to);
  1625. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, to);
  1626. undo_redo->commit_action();
  1627. }
  1628. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1629. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  1630. VisualShader::Type type = get_current_shader_type();
  1631. int from = p_from.to_int();
  1632. int to = p_to.to_int();
  1633. undo_redo->create_action(TTR("Nodes Disconnected"));
  1634. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1635. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1636. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1637. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1638. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, to);
  1639. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, to);
  1640. undo_redo->commit_action();
  1641. }
  1642. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  1643. from_node = p_from.to_int();
  1644. from_slot = p_from_slot;
  1645. _show_members_dialog(true);
  1646. }
  1647. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  1648. to_node = p_to.to_int();
  1649. to_slot = p_to_slot;
  1650. _show_members_dialog(true);
  1651. }
  1652. void VisualShaderEditor::_delete_nodes(int p_type, const List<int> &p_nodes) {
  1653. VisualShader::Type type = VisualShader::Type(p_type);
  1654. List<VisualShader::Connection> conns;
  1655. visual_shader->get_node_connections(type, &conns);
  1656. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  1657. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1658. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1659. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1660. }
  1661. }
  1662. }
  1663. Set<String> uniform_names;
  1664. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  1665. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  1666. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  1667. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  1668. undo_redo->add_undo_method(graph_plugin.ptr(), "add_node", type, F->get());
  1669. undo_redo->add_do_method(this, "_clear_buffer");
  1670. undo_redo->add_undo_method(this, "_clear_buffer");
  1671. // restore size, inputs and outputs if node is group
  1672. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1673. if (group) {
  1674. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1675. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1676. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1677. }
  1678. // restore expression text if node is expression
  1679. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1680. if (expression) {
  1681. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1682. }
  1683. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  1684. if (uniform) {
  1685. uniform_names.insert(uniform->get_uniform_name());
  1686. }
  1687. }
  1688. List<VisualShader::Connection> used_conns;
  1689. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  1690. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1691. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1692. bool cancel = false;
  1693. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  1694. 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) {
  1695. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  1696. break;
  1697. }
  1698. }
  1699. if (!cancel) {
  1700. 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);
  1701. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  1702. used_conns.push_back(E->get());
  1703. }
  1704. }
  1705. }
  1706. }
  1707. // delete nodes from the graph
  1708. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  1709. undo_redo->add_do_method(graph_plugin.ptr(), "remove_node", type, F->get());
  1710. }
  1711. // update uniform refs if any uniform has been deleted
  1712. if (uniform_names.size() > 0) {
  1713. undo_redo->add_do_method(this, "_update_uniforms", true);
  1714. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1715. _update_uniform_refs(uniform_names);
  1716. }
  1717. }
  1718. void VisualShaderEditor::_delete_node_request(int p_type, int p_node) {
  1719. List<int> to_erase;
  1720. to_erase.push_back(p_node);
  1721. undo_redo->create_action(TTR("Delete VisualShader Node"));
  1722. _delete_nodes(p_type, to_erase);
  1723. undo_redo->commit_action();
  1724. }
  1725. void VisualShaderEditor::_delete_nodes_request() {
  1726. List<int> to_erase;
  1727. for (int i = 0; i < graph->get_child_count(); i++) {
  1728. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1729. if (gn) {
  1730. if (gn->is_selected() && gn->is_close_button_visible()) {
  1731. to_erase.push_back(gn->get_name().operator String().to_int());
  1732. }
  1733. }
  1734. }
  1735. if (to_erase.empty()) {
  1736. return;
  1737. }
  1738. undo_redo->create_action(TTR("Delete VisualShader Node(s)"));
  1739. _delete_nodes(get_current_shader_type(), to_erase);
  1740. undo_redo->commit_action();
  1741. }
  1742. void VisualShaderEditor::_node_selected(Object *p_node) {
  1743. VisualShader::Type type = get_current_shader_type();
  1744. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  1745. ERR_FAIL_COND(!gn);
  1746. int id = String(gn->get_name()).to_int();
  1747. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  1748. ERR_FAIL_COND(!vsnode.is_valid());
  1749. //do not rely on this, makes editor more complex
  1750. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  1751. }
  1752. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1753. Ref<InputEventMouseButton> mb = p_event;
  1754. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1755. List<int> to_change;
  1756. for (int i = 0; i < graph->get_child_count(); i++) {
  1757. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1758. if (gn) {
  1759. if (gn->is_selected() && gn->is_close_button_visible()) {
  1760. to_change.push_back(gn->get_name().operator String().to_int());
  1761. }
  1762. }
  1763. }
  1764. if (to_change.empty() && copy_nodes_buffer.empty()) {
  1765. _show_members_dialog(true);
  1766. } else {
  1767. popup_menu->set_item_disabled(NodeMenuOptions::COPY, to_change.empty());
  1768. popup_menu->set_item_disabled(NodeMenuOptions::PASTE, copy_nodes_buffer.empty());
  1769. popup_menu->set_item_disabled(NodeMenuOptions::DELETE, to_change.empty());
  1770. popup_menu->set_item_disabled(NodeMenuOptions::DUPLICATE, to_change.empty());
  1771. menu_point = graph->get_local_mouse_position();
  1772. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1773. popup_menu->set_position(gpos);
  1774. popup_menu->popup();
  1775. }
  1776. }
  1777. }
  1778. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  1779. if (at_mouse_pos) {
  1780. saved_node_pos_dirty = true;
  1781. saved_node_pos = graph->get_local_mouse_position();
  1782. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1783. members_dialog->popup();
  1784. members_dialog->set_position(gpos);
  1785. } else {
  1786. members_dialog->popup();
  1787. saved_node_pos_dirty = false;
  1788. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  1789. }
  1790. // keep dialog within window bounds
  1791. Size2 window_size = DisplayServer::get_singleton()->window_get_size();
  1792. Rect2 dialog_rect = Rect2(members_dialog->get_position(), members_dialog->get_size());
  1793. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  1794. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  1795. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  1796. }
  1797. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  1798. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  1799. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  1800. }
  1801. node_filter->call_deferred("grab_focus"); // still not visible
  1802. node_filter->select_all();
  1803. }
  1804. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  1805. Ref<InputEventKey> ie = p_ie;
  1806. if (ie.is_valid() && (ie->get_keycode() == KEY_UP ||
  1807. ie->get_keycode() == KEY_DOWN ||
  1808. ie->get_keycode() == KEY_ENTER ||
  1809. ie->get_keycode() == KEY_KP_ENTER)) {
  1810. members->call("_gui_input", ie);
  1811. node_filter->accept_event();
  1812. }
  1813. }
  1814. void VisualShaderEditor::_notification(int p_what) {
  1815. if (p_what == NOTIFICATION_ENTER_TREE) {
  1816. node_filter->set_clear_button_enabled(true);
  1817. // collapse tree by default
  1818. TreeItem *category = members->get_root()->get_children();
  1819. while (category) {
  1820. category->set_collapsed(true);
  1821. TreeItem *sub_category = category->get_children();
  1822. while (sub_category) {
  1823. sub_category->set_collapsed(true);
  1824. sub_category = sub_category->get_next();
  1825. }
  1826. category = category->get_next();
  1827. }
  1828. }
  1829. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1830. Dictionary dd = get_viewport()->gui_get_drag_data();
  1831. if (members->is_visible_in_tree() && dd.has("id")) {
  1832. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1833. }
  1834. } else if (p_what == NOTIFICATION_DRAG_END) {
  1835. members->set_drop_mode_flags(0);
  1836. }
  1837. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1838. highend_label->set_modulate(get_theme_color("vulkan_color", "Editor"));
  1839. error_panel->add_theme_style_override("panel", get_theme_stylebox("bg", "Tree"));
  1840. error_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1841. node_filter->set_right_icon(Control::get_theme_icon("Search", "EditorIcons"));
  1842. preview_shader->set_icon(Control::get_theme_icon("Shader", "EditorIcons"));
  1843. {
  1844. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  1845. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  1846. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  1847. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  1848. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  1849. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  1850. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  1851. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  1852. preview_text->add_theme_color_override("background_color", background_color);
  1853. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  1854. syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  1855. }
  1856. preview_text->add_theme_font_override("font", get_theme_font("expression", "EditorFonts"));
  1857. preview_text->add_theme_color_override("font_color", text_color);
  1858. syntax_highlighter->set_number_color(number_color);
  1859. syntax_highlighter->set_symbol_color(symbol_color);
  1860. syntax_highlighter->set_function_color(function_color);
  1861. syntax_highlighter->set_member_variable_color(members_color);
  1862. syntax_highlighter->clear_color_regions();
  1863. syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  1864. syntax_highlighter->add_color_region("//", "", comment_color, true);
  1865. error_text->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
  1866. error_text->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  1867. }
  1868. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Tools", "EditorIcons"));
  1869. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree()) {
  1870. _update_graph();
  1871. }
  1872. }
  1873. }
  1874. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  1875. if (updating) {
  1876. return;
  1877. }
  1878. updating = true;
  1879. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  1880. updating = false;
  1881. }
  1882. void VisualShaderEditor::_node_changed(int p_id) {
  1883. if (updating) {
  1884. return;
  1885. }
  1886. if (is_visible_in_tree()) {
  1887. _update_graph();
  1888. }
  1889. }
  1890. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  1891. r_excluded.clear();
  1892. VisualShader::Type type = (VisualShader::Type)p_type;
  1893. for (int i = 0; i < graph->get_child_count(); i++) {
  1894. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1895. if (gn) {
  1896. int id = String(gn->get_name()).to_int();
  1897. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1898. Ref<VisualShaderNodeOutput> output = node;
  1899. if (output.is_valid()) {
  1900. r_excluded.insert(id);
  1901. continue;
  1902. }
  1903. r_excluded.insert(id);
  1904. }
  1905. }
  1906. }
  1907. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  1908. VisualShader::Type type = (VisualShader::Type)p_type;
  1909. selection_center.x = 0.0f;
  1910. selection_center.y = 0.0f;
  1911. for (int i = 0; i < graph->get_child_count(); i++) {
  1912. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1913. if (gn) {
  1914. int id = String(gn->get_name()).to_int();
  1915. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1916. Ref<VisualShaderNodeOutput> output = node;
  1917. if (output.is_valid()) { // can't duplicate output
  1918. r_excluded.insert(id);
  1919. continue;
  1920. }
  1921. if (node.is_valid() && gn->is_selected()) {
  1922. Vector2 pos = visual_shader->get_node_position(type, id);
  1923. selection_center += pos;
  1924. r_nodes.push_back(id);
  1925. }
  1926. r_excluded.insert(id);
  1927. }
  1928. }
  1929. selection_center /= (float)r_nodes.size();
  1930. }
  1931. 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) {
  1932. VisualShader::Type type = (VisualShader::Type)p_type;
  1933. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  1934. int base_id = visual_shader->get_valid_node_id(type);
  1935. int id_from = base_id;
  1936. Map<int, int> connection_remap;
  1937. Set<int> unsupported_set;
  1938. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1939. connection_remap[E->get()] = id_from;
  1940. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  1941. bool unsupported = false;
  1942. for (int i = 0; i < add_options.size(); i++) {
  1943. if (add_options[i].type == node->get_class_name()) {
  1944. if (!_is_available(add_options[i].mode)) {
  1945. unsupported = true;
  1946. }
  1947. break;
  1948. }
  1949. }
  1950. if (unsupported) {
  1951. unsupported_set.insert(E->get());
  1952. continue;
  1953. }
  1954. Ref<VisualShaderNode> dupli = node->duplicate();
  1955. 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);
  1956. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_from);
  1957. // duplicate size, inputs and outputs if node is group
  1958. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1959. if (!group.is_null()) {
  1960. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  1961. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_size", type, id_from, group->get_size());
  1962. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  1963. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  1964. }
  1965. // duplicate expression text if node is expression
  1966. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1967. if (!expression.is_null()) {
  1968. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  1969. }
  1970. id_from++;
  1971. }
  1972. List<VisualShader::Connection> conns;
  1973. visual_shader->get_node_connections(pasted_type, &conns);
  1974. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1975. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  1976. continue;
  1977. }
  1978. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  1979. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1980. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1981. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1982. }
  1983. }
  1984. id_from = base_id;
  1985. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1986. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  1987. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_from);
  1988. id_from++;
  1989. }
  1990. undo_redo->commit_action();
  1991. if (p_select) {
  1992. // reselect duplicated nodes by excluding the other ones
  1993. for (int i = 0; i < graph->get_child_count(); i++) {
  1994. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1995. if (gn) {
  1996. int id = String(gn->get_name()).to_int();
  1997. if (!r_excluded.has(id)) {
  1998. gn->set_selected(true);
  1999. } else {
  2000. gn->set_selected(false);
  2001. }
  2002. }
  2003. }
  2004. }
  2005. }
  2006. void VisualShaderEditor::_clear_buffer() {
  2007. copy_nodes_buffer.clear();
  2008. copy_nodes_excluded_buffer.clear();
  2009. }
  2010. void VisualShaderEditor::_duplicate_nodes() {
  2011. int type = get_current_shader_type();
  2012. List<int> nodes;
  2013. Set<int> excluded;
  2014. _dup_copy_nodes(type, nodes, excluded);
  2015. if (nodes.empty()) {
  2016. return;
  2017. }
  2018. undo_redo->create_action(TTR("Duplicate VisualShader Node(s)"));
  2019. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  2020. }
  2021. void VisualShaderEditor::_copy_nodes() {
  2022. copy_type = get_current_shader_type();
  2023. _clear_buffer();
  2024. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  2025. }
  2026. void VisualShaderEditor::_paste_nodes(bool p_use_custom_position, const Vector2 &p_custom_position) {
  2027. if (copy_nodes_buffer.empty()) {
  2028. return;
  2029. }
  2030. int type = get_current_shader_type();
  2031. float scale = graph->get_zoom();
  2032. Vector2 mpos;
  2033. if (p_use_custom_position) {
  2034. mpos = p_custom_position;
  2035. } else {
  2036. mpos = graph->get_local_mouse_position();
  2037. }
  2038. undo_redo->create_action(TTR("Paste VisualShader Node(s)"));
  2039. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + mpos / scale - selection_center), false);
  2040. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  2041. }
  2042. void VisualShaderEditor::_mode_selected(int p_id) {
  2043. visual_shader->set_shader_type(particles_mode ? VisualShader::Type(p_id + 3) : VisualShader::Type(p_id));
  2044. _update_options_menu();
  2045. _update_graph();
  2046. }
  2047. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> p_input, String p_name) {
  2048. String prev_name = p_input->get_input_name();
  2049. if (p_name == prev_name) {
  2050. return;
  2051. }
  2052. bool type_changed = p_input->get_input_type_by_name(p_name) != p_input->get_input_type_by_name(prev_name);
  2053. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2054. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  2055. undo_redo->add_do_method(p_input.ptr(), "set_input_name", p_name);
  2056. undo_redo->add_undo_method(p_input.ptr(), "set_input_name", prev_name);
  2057. // update output port
  2058. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  2059. VisualShader::Type type = VisualShader::Type(type_id);
  2060. int id = visual_shader->find_node_id(type, p_input);
  2061. if (id != VisualShader::NODE_ID_INVALID) {
  2062. if (type_changed) {
  2063. List<VisualShader::Connection> conns;
  2064. visual_shader->get_node_connections(type, &conns);
  2065. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2066. if (E->get().from_node == id) {
  2067. if (visual_shader->is_port_types_compatible(p_input->get_input_type_by_name(p_name), visual_shader->get_node(type, E->get().to_node)->get_input_port_type(E->get().to_port))) {
  2068. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2069. 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);
  2070. continue;
  2071. }
  2072. 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);
  2073. 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);
  2074. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2075. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2076. }
  2077. }
  2078. }
  2079. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  2080. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  2081. break;
  2082. }
  2083. }
  2084. undo_redo->commit_action();
  2085. }
  2086. void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform_ref, String p_name) {
  2087. String prev_name = p_uniform_ref->get_uniform_name();
  2088. if (p_name == prev_name) {
  2089. return;
  2090. }
  2091. bool type_changed = p_uniform_ref->get_uniform_type_by_name(p_name) != p_uniform_ref->get_uniform_type_by_name(prev_name);
  2092. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2093. undo_redo->create_action(TTR("UniformRef Name Changed"));
  2094. undo_redo->add_do_method(p_uniform_ref.ptr(), "set_uniform_name", p_name);
  2095. undo_redo->add_undo_method(p_uniform_ref.ptr(), "set_uniform_name", prev_name);
  2096. // update output port
  2097. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  2098. VisualShader::Type type = VisualShader::Type(type_id);
  2099. int id = visual_shader->find_node_id(type, p_uniform_ref);
  2100. if (id != VisualShader::NODE_ID_INVALID) {
  2101. if (type_changed) {
  2102. List<VisualShader::Connection> conns;
  2103. visual_shader->get_node_connections(type, &conns);
  2104. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2105. if (E->get().from_node == id) {
  2106. if (visual_shader->is_port_types_compatible(p_uniform_ref->get_uniform_type_by_name(p_name), visual_shader->get_node(type, E->get().to_node)->get_input_port_type(E->get().to_port))) {
  2107. continue;
  2108. }
  2109. 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);
  2110. 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);
  2111. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2112. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, E->get().from_node, E->get().from_port, E->get().to_node, E->get().to_port);
  2113. }
  2114. }
  2115. }
  2116. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  2117. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  2118. break;
  2119. }
  2120. }
  2121. undo_redo->commit_action();
  2122. }
  2123. void VisualShaderEditor::_float_constant_selected(int p_index, int p_node) {
  2124. if (p_index == 0) {
  2125. graph_plugin->update_node_size(p_node);
  2126. return;
  2127. }
  2128. --p_index;
  2129. ERR_FAIL_INDEX(p_index, MAX_FLOAT_CONST_DEFS);
  2130. VisualShader::Type type = get_current_shader_type();
  2131. Ref<VisualShaderNodeFloatConstant> node = visual_shader->get_node(type, p_node);
  2132. if (!node.is_valid()) {
  2133. return;
  2134. }
  2135. undo_redo->create_action(TTR("Set constant"));
  2136. undo_redo->add_do_method(node.ptr(), "set_constant", float_constant_defs[p_index].value);
  2137. undo_redo->add_undo_method(node.ptr(), "set_constant", node->get_constant());
  2138. undo_redo->add_do_method(graph_plugin.ptr(), "update_constant", type, p_node);
  2139. undo_redo->add_undo_method(graph_plugin.ptr(), "update_constant", type, p_node);
  2140. undo_redo->commit_action();
  2141. }
  2142. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  2143. _update_options_menu();
  2144. }
  2145. void VisualShaderEditor::_member_selected() {
  2146. TreeItem *item = members->get_selected();
  2147. if (item != nullptr && item->has_meta("id")) {
  2148. members_dialog->get_ok()->set_disabled(false);
  2149. highend_label->set_visible(add_options[item->get_meta("id")].highend);
  2150. node_desc->set_text(_get_description(item->get_meta("id")));
  2151. } else {
  2152. highend_label->set_visible(false);
  2153. members_dialog->get_ok()->set_disabled(true);
  2154. node_desc->set_text("");
  2155. }
  2156. }
  2157. void VisualShaderEditor::_member_unselected() {
  2158. }
  2159. void VisualShaderEditor::_member_create() {
  2160. TreeItem *item = members->get_selected();
  2161. if (item != nullptr && item->has_meta("id")) {
  2162. int idx = members->get_selected()->get_meta("id");
  2163. _add_node(idx, add_options[idx].sub_func);
  2164. members_dialog->hide();
  2165. }
  2166. }
  2167. void VisualShaderEditor::_member_cancel() {
  2168. to_node = -1;
  2169. to_slot = -1;
  2170. from_node = -1;
  2171. from_slot = -1;
  2172. }
  2173. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  2174. TreeItem *category = members->get_root()->get_children();
  2175. switch (p_idx) {
  2176. case EXPAND_ALL:
  2177. while (category) {
  2178. category->set_collapsed(false);
  2179. TreeItem *sub_category = category->get_children();
  2180. while (sub_category) {
  2181. sub_category->set_collapsed(false);
  2182. sub_category = sub_category->get_next();
  2183. }
  2184. category = category->get_next();
  2185. }
  2186. break;
  2187. case COLLAPSE_ALL:
  2188. while (category) {
  2189. category->set_collapsed(true);
  2190. TreeItem *sub_category = category->get_children();
  2191. while (sub_category) {
  2192. sub_category->set_collapsed(true);
  2193. sub_category = sub_category->get_next();
  2194. }
  2195. category = category->get_next();
  2196. }
  2197. break;
  2198. default:
  2199. break;
  2200. }
  2201. }
  2202. void VisualShaderEditor::_node_menu_id_pressed(int p_idx) {
  2203. switch (p_idx) {
  2204. case NodeMenuOptions::ADD:
  2205. _show_members_dialog(true);
  2206. break;
  2207. case NodeMenuOptions::COPY:
  2208. _copy_nodes();
  2209. break;
  2210. case NodeMenuOptions::PASTE:
  2211. _paste_nodes(true, menu_point);
  2212. break;
  2213. case NodeMenuOptions::DELETE:
  2214. _delete_nodes_request();
  2215. break;
  2216. case NodeMenuOptions::DUPLICATE:
  2217. _duplicate_nodes();
  2218. break;
  2219. }
  2220. }
  2221. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2222. if (p_from == members) {
  2223. TreeItem *it = members->get_item_at_position(p_point);
  2224. if (!it) {
  2225. return Variant();
  2226. }
  2227. if (!it->has_meta("id")) {
  2228. return Variant();
  2229. }
  2230. int id = it->get_meta("id");
  2231. AddOption op = add_options[id];
  2232. Dictionary d;
  2233. d["id"] = id;
  2234. if (op.sub_func == -1) {
  2235. d["sub_func"] = op.sub_func_str;
  2236. } else {
  2237. d["sub_func"] = op.sub_func;
  2238. }
  2239. Label *label = memnew(Label);
  2240. label->set_text(it->get_text(0));
  2241. set_drag_preview(label);
  2242. return d;
  2243. }
  2244. return Variant();
  2245. }
  2246. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2247. if (p_from == graph) {
  2248. Dictionary d = p_data;
  2249. if (d.has("id")) {
  2250. return true;
  2251. }
  2252. if (d.has("files")) {
  2253. return true;
  2254. }
  2255. }
  2256. return false;
  2257. }
  2258. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2259. if (p_from == graph) {
  2260. Dictionary d = p_data;
  2261. if (d.has("id")) {
  2262. int idx = d["id"];
  2263. saved_node_pos = p_point;
  2264. saved_node_pos_dirty = true;
  2265. _add_node(idx, add_options[idx].sub_func);
  2266. } else if (d.has("files")) {
  2267. if (d["files"].get_type() == Variant::PACKED_STRING_ARRAY) {
  2268. int j = 0;
  2269. PackedStringArray arr = d["files"];
  2270. for (int i = 0; i < arr.size(); i++) {
  2271. String type = ResourceLoader::get_resource_type(arr[i]);
  2272. if (type == "GDScript") {
  2273. Ref<Script> script = ResourceLoader::load(arr[i]);
  2274. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  2275. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2276. saved_node_pos_dirty = true;
  2277. _add_custom_node(arr[i]);
  2278. j++;
  2279. }
  2280. } else if (type == "CurveTexture") {
  2281. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2282. saved_node_pos_dirty = true;
  2283. _add_curve_node(arr[i]);
  2284. j++;
  2285. } else if (ClassDB::get_parent_class(type) == "Texture2D") {
  2286. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2287. saved_node_pos_dirty = true;
  2288. _add_texture2d_node(arr[i]);
  2289. j++;
  2290. } else if (type == "Texture2DArray") {
  2291. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2292. saved_node_pos_dirty = true;
  2293. _add_texture2d_array_node(arr[i]);
  2294. j++;
  2295. } else if (ClassDB::get_parent_class(type) == "Texture3D") {
  2296. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2297. saved_node_pos_dirty = true;
  2298. _add_texture3d_node(arr[i]);
  2299. j++;
  2300. } else if (type == "Cubemap") {
  2301. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  2302. saved_node_pos_dirty = true;
  2303. _add_cubemap_node(arr[i]);
  2304. j++;
  2305. }
  2306. }
  2307. }
  2308. }
  2309. }
  2310. }
  2311. void VisualShaderEditor::_show_preview_text() {
  2312. preview_showed = !preview_showed;
  2313. preview_vbox->set_visible(preview_showed);
  2314. if (preview_showed) {
  2315. if (pending_update_preview) {
  2316. _update_preview();
  2317. pending_update_preview = false;
  2318. }
  2319. }
  2320. }
  2321. static ShaderLanguage::DataType _get_global_variable_type(const StringName &p_variable) {
  2322. RS::GlobalVariableType gvt = RS::get_singleton()->global_variable_get_type(p_variable);
  2323. return RS::global_variable_type_get_shader_datatype(gvt);
  2324. }
  2325. void VisualShaderEditor::_update_preview() {
  2326. if (!preview_showed) {
  2327. pending_update_preview = true;
  2328. return;
  2329. }
  2330. String code = visual_shader->get_code();
  2331. preview_text->set_text(code);
  2332. ShaderLanguage sl;
  2333. Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type);
  2334. for (int i = 0; i < preview_text->get_line_count(); i++) {
  2335. preview_text->set_line_as_marked(i, false);
  2336. }
  2337. if (err != OK) {
  2338. preview_text->set_line_as_marked(sl.get_error_line() - 1, true);
  2339. error_text->set_visible(true);
  2340. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  2341. error_text->set_text(text);
  2342. shader_error = true;
  2343. } else {
  2344. error_text->set_visible(false);
  2345. shader_error = false;
  2346. }
  2347. }
  2348. void VisualShaderEditor::_bind_methods() {
  2349. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  2350. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  2351. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  2352. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  2353. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  2354. ClassDB::bind_method("_uniform_select_item", &VisualShaderEditor::_uniform_select_item);
  2355. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  2356. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  2357. ClassDB::bind_method("_update_uniforms", &VisualShaderEditor::_update_uniforms);
  2358. ClassDB::bind_method("_set_mode", &VisualShaderEditor::_set_mode);
  2359. ClassDB::bind_method("_nodes_dragged", &VisualShaderEditor::_nodes_dragged);
  2360. ClassDB::bind_method("_float_constant_selected", &VisualShaderEditor::_float_constant_selected);
  2361. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  2362. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  2363. ClassDB::bind_method(D_METHOD("drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  2364. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  2365. }
  2366. VisualShaderEditor *VisualShaderEditor::singleton = nullptr;
  2367. VisualShaderEditor::VisualShaderEditor() {
  2368. singleton = this;
  2369. updating = false;
  2370. saved_node_pos_dirty = false;
  2371. saved_node_pos = Point2(0, 0);
  2372. ShaderLanguage::get_keyword_list(&keyword_list);
  2373. preview_showed = false;
  2374. pending_update_preview = false;
  2375. shader_error = false;
  2376. to_node = -1;
  2377. to_slot = -1;
  2378. from_node = -1;
  2379. from_slot = -1;
  2380. main_box = memnew(HSplitContainer);
  2381. main_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2382. main_box->set_h_size_flags(SIZE_EXPAND_FILL);
  2383. add_child(main_box);
  2384. graph = memnew(GraphEdit);
  2385. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  2386. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  2387. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  2388. main_box->add_child(graph);
  2389. graph->set_drag_forwarding(this);
  2390. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  2391. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2392. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  2393. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  2394. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  2395. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  2396. //graph->add_valid_left_disconnect_type(0);
  2397. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  2398. graph->connect("connection_request", callable_mp(this, &VisualShaderEditor::_connection_request), varray(), CONNECT_DEFERRED);
  2399. graph->connect("disconnection_request", callable_mp(this, &VisualShaderEditor::_disconnection_request), varray(), CONNECT_DEFERRED);
  2400. graph->connect("node_selected", callable_mp(this, &VisualShaderEditor::_node_selected));
  2401. graph->connect("scroll_offset_changed", callable_mp(this, &VisualShaderEditor::_scroll_changed));
  2402. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualShaderEditor::_duplicate_nodes));
  2403. graph->connect("copy_nodes_request", callable_mp(this, &VisualShaderEditor::_copy_nodes));
  2404. graph->connect("paste_nodes_request", callable_mp(this, &VisualShaderEditor::_paste_nodes), varray(false, Point2()));
  2405. graph->connect("delete_nodes_request", callable_mp(this, &VisualShaderEditor::_delete_nodes_request));
  2406. graph->connect("gui_input", callable_mp(this, &VisualShaderEditor::_graph_gui_input));
  2407. graph->connect("connection_to_empty", callable_mp(this, &VisualShaderEditor::_connection_to_empty));
  2408. graph->connect("connection_from_empty", callable_mp(this, &VisualShaderEditor::_connection_from_empty));
  2409. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  2410. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2411. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  2412. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2413. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2414. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR);
  2415. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_VECTOR);
  2416. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2417. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  2418. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2419. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  2420. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2421. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  2422. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  2423. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  2424. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2425. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  2426. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  2427. VSeparator *vs = memnew(VSeparator);
  2428. graph->get_zoom_hbox()->add_child(vs);
  2429. graph->get_zoom_hbox()->move_child(vs, 0);
  2430. edit_type_standart = memnew(OptionButton);
  2431. edit_type_standart->add_item(TTR("Vertex"));
  2432. edit_type_standart->add_item(TTR("Fragment"));
  2433. edit_type_standart->add_item(TTR("Light"));
  2434. edit_type_standart->select(1);
  2435. edit_type_standart->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  2436. edit_type_particles = memnew(OptionButton);
  2437. edit_type_particles->add_item(TTR("Emit"));
  2438. edit_type_particles->add_item(TTR("Process"));
  2439. edit_type_particles->add_item(TTR("End"));
  2440. edit_type_particles->select(0);
  2441. edit_type_particles->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  2442. edit_type = edit_type_standart;
  2443. graph->get_zoom_hbox()->add_child(edit_type_particles);
  2444. graph->get_zoom_hbox()->move_child(edit_type_particles, 0);
  2445. graph->get_zoom_hbox()->add_child(edit_type_standart);
  2446. graph->get_zoom_hbox()->move_child(edit_type_standart, 0);
  2447. add_node = memnew(Button);
  2448. add_node->set_flat(true);
  2449. graph->get_zoom_hbox()->add_child(add_node);
  2450. add_node->set_text(TTR("Add Node..."));
  2451. graph->get_zoom_hbox()->move_child(add_node, 0);
  2452. add_node->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_members_dialog), varray(false));
  2453. preview_shader = memnew(Button);
  2454. preview_shader->set_flat(true);
  2455. preview_shader->set_toggle_mode(true);
  2456. preview_shader->set_tooltip(TTR("Show resulted shader code."));
  2457. graph->get_zoom_hbox()->add_child(preview_shader);
  2458. preview_shader->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_preview_text));
  2459. ///////////////////////////////////////
  2460. // PREVIEW PANEL
  2461. ///////////////////////////////////////
  2462. preview_vbox = memnew(VBoxContainer);
  2463. preview_vbox->set_visible(preview_showed);
  2464. main_box->add_child(preview_vbox);
  2465. preview_text = memnew(CodeEdit);
  2466. syntax_highlighter.instance();
  2467. preview_vbox->add_child(preview_text);
  2468. preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2469. preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
  2470. preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  2471. preview_text->set_syntax_highlighter(syntax_highlighter);
  2472. preview_text->set_draw_line_numbers(true);
  2473. preview_text->set_readonly(true);
  2474. error_text = memnew(Label);
  2475. preview_vbox->add_child(error_text);
  2476. error_text->set_visible(false);
  2477. ///////////////////////////////////////
  2478. // POPUP MENU
  2479. ///////////////////////////////////////
  2480. popup_menu = memnew(PopupMenu);
  2481. add_child(popup_menu);
  2482. popup_menu->add_item("Add Node", NodeMenuOptions::ADD);
  2483. popup_menu->add_separator();
  2484. popup_menu->add_item("Copy", NodeMenuOptions::COPY);
  2485. popup_menu->add_item("Paste", NodeMenuOptions::PASTE);
  2486. popup_menu->add_item("Delete", NodeMenuOptions::DELETE);
  2487. popup_menu->add_item("Duplicate", NodeMenuOptions::DUPLICATE);
  2488. popup_menu->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_node_menu_id_pressed));
  2489. ///////////////////////////////////////
  2490. // SHADER NODES TREE
  2491. ///////////////////////////////////////
  2492. VBoxContainer *members_vb = memnew(VBoxContainer);
  2493. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2494. HBoxContainer *filter_hb = memnew(HBoxContainer);
  2495. members_vb->add_child(filter_hb);
  2496. node_filter = memnew(LineEdit);
  2497. filter_hb->add_child(node_filter);
  2498. node_filter->connect("text_changed", callable_mp(this, &VisualShaderEditor::_member_filter_changed));
  2499. node_filter->connect("gui_input", callable_mp(this, &VisualShaderEditor::_sbox_input));
  2500. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2501. node_filter->set_placeholder(TTR("Search"));
  2502. tools = memnew(MenuButton);
  2503. filter_hb->add_child(tools);
  2504. tools->set_tooltip(TTR("Options"));
  2505. tools->get_popup()->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_tools_menu_option));
  2506. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  2507. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  2508. members = memnew(Tree);
  2509. members_vb->add_child(members);
  2510. members->set_drag_forwarding(this);
  2511. members->set_h_size_flags(SIZE_EXPAND_FILL);
  2512. members->set_v_size_flags(SIZE_EXPAND_FILL);
  2513. members->set_hide_root(true);
  2514. members->set_allow_reselect(true);
  2515. members->set_hide_folding(false);
  2516. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  2517. members->connect("item_activated", callable_mp(this, &VisualShaderEditor::_member_create));
  2518. members->connect("item_selected", callable_mp(this, &VisualShaderEditor::_member_selected));
  2519. members->connect("nothing_selected", callable_mp(this, &VisualShaderEditor::_member_unselected));
  2520. HBoxContainer *desc_hbox = memnew(HBoxContainer);
  2521. members_vb->add_child(desc_hbox);
  2522. Label *desc_label = memnew(Label);
  2523. desc_hbox->add_child(desc_label);
  2524. desc_label->set_text(TTR("Description:"));
  2525. desc_hbox->add_spacer();
  2526. highend_label = memnew(Label);
  2527. desc_hbox->add_child(highend_label);
  2528. highend_label->set_visible(false);
  2529. highend_label->set_text("Vulkan");
  2530. highend_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  2531. highend_label->set_tooltip(TTR("High-end node"));
  2532. node_desc = memnew(RichTextLabel);
  2533. members_vb->add_child(node_desc);
  2534. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  2535. node_desc->set_v_size_flags(SIZE_FILL);
  2536. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  2537. members_dialog = memnew(ConfirmationDialog);
  2538. members_dialog->set_title(TTR("Create Shader Node"));
  2539. members_dialog->set_exclusive(false);
  2540. members_dialog->add_child(members_vb);
  2541. members_dialog->get_ok()->set_text(TTR("Create"));
  2542. members_dialog->get_ok()->connect("pressed", callable_mp(this, &VisualShaderEditor::_member_create));
  2543. members_dialog->get_ok()->set_disabled(true);
  2544. members_dialog->connect("cancelled", callable_mp(this, &VisualShaderEditor::_member_cancel));
  2545. add_child(members_dialog);
  2546. alert = memnew(AcceptDialog);
  2547. alert->get_label()->set_autowrap(true);
  2548. alert->get_label()->set_align(Label::ALIGN_CENTER);
  2549. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  2550. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  2551. add_child(alert);
  2552. ///////////////////////////////////////
  2553. // SHADER NODES TREE OPTIONS
  2554. ///////////////////////////////////////
  2555. // COLOR
  2556. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2557. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2558. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  2559. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  2560. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  2561. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  2562. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  2563. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2564. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  2565. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  2566. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2567. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2568. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  2569. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2570. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2571. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  2572. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  2573. // CONDITIONAL
  2574. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  2575. add_options.push_back(AddOption("Equal", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2576. add_options.push_back(AddOption("GreaterThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2577. 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));
  2578. 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));
  2579. 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));
  2580. 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));
  2581. add_options.push_back(AddOption("LessThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2582. 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));
  2583. add_options.push_back(AddOption("NotEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2584. 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));
  2585. 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));
  2586. 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));
  2587. 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));
  2588. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2589. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2590. // INPUT
  2591. // SPATIAL-FOR-ALL
  2592. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  2593. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2594. 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));
  2595. 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));
  2596. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2597. 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));
  2598. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2599. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  2600. 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));
  2601. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2602. // CANVASITEM-FOR-ALL
  2603. 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));
  2604. 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));
  2605. 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));
  2606. 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));
  2607. 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));
  2608. /////////////////
  2609. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  2610. // SPATIAL INPUTS
  2611. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  2612. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  2613. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  2614. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  2615. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  2616. const String input_param_for_emit_shader_mode = TTR("'%s' input parameter for emit shader mode.");
  2617. const String input_param_for_process_shader_mode = TTR("'%s' input parameter for process shader mode.");
  2618. const String input_param_for_end_shader_mode = TTR("'%s' input parameter for end shader mode.");
  2619. const String input_param_for_emit_and_process_shader_mode = TTR("'%s' input parameter for emit and process shader mode.");
  2620. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader mode.");
  2621. add_options.push_back(AddOption("Alpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2622. add_options.push_back(AddOption("Binormal", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2623. add_options.push_back(AddOption("Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2624. add_options.push_back(AddOption("DepthTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "depth_texture"), "depth_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2625. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2626. add_options.push_back(AddOption("FrontFacing", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "front_facing"), "front_facing", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2627. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2628. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2629. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2630. add_options.push_back(AddOption("Side", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "side"), "side", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2631. add_options.push_back(AddOption("Tangent", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2632. add_options.push_back(AddOption("UV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2633. add_options.push_back(AddOption("UV2", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2634. add_options.push_back(AddOption("Vertex", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2635. add_options.push_back(AddOption("View", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SPATIAL));
  2636. add_options.push_back(AddOption("Albedo", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "albedo"), "albedo", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2637. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2638. add_options.push_back(AddOption("Diffuse", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "diffuse"), "diffuse", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2639. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2640. add_options.push_back(AddOption("Light", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light"), "light", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2641. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2642. add_options.push_back(AddOption("Metallic", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "metallic"), "metallic", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2643. add_options.push_back(AddOption("Roughness", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "roughness"), "roughness", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2644. add_options.push_back(AddOption("Specular", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "specular"), "specular", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2645. add_options.push_back(AddOption("Transmission", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "transmission"), "transmission", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2646. add_options.push_back(AddOption("View", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2647. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2648. add_options.push_back(AddOption("Binormal", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2649. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2650. add_options.push_back(AddOption("ModelView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "modelview"), "modelview", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2651. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2652. add_options.push_back(AddOption("Tangent", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_mode, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2653. add_options.push_back(AddOption("UV", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2654. add_options.push_back(AddOption("UV2", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2655. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  2656. // CANVASITEM INPUTS
  2657. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2658. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2659. add_options.push_back(AddOption("NormalTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "normal_texture"), "normal_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2660. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2661. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2662. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2663. 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, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2664. add_options.push_back(AddOption("Texture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2665. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2666. add_options.push_back(AddOption("LightAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_alpha"), "light_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2667. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2668. add_options.push_back(AddOption("LightHeight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_height"), "light_height", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2669. add_options.push_back(AddOption("LightUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_uv"), "light_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2670. add_options.push_back(AddOption("LightVector", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vec"), "light_vec", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2671. add_options.push_back(AddOption("Normal", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2672. 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, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2673. 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, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2674. add_options.push_back(AddOption("ShadowAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_alpha"), "shadow_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2675. add_options.push_back(AddOption("ShadowColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_color"), "shadow_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2676. add_options.push_back(AddOption("ShadowVec", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_vec"), "shadow_vec", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2677. add_options.push_back(AddOption("Texture", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  2678. add_options.push_back(AddOption("Extra", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "extra"), "extra", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2679. 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, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2680. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2681. add_options.push_back(AddOption("Projection", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2682. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2683. add_options.push_back(AddOption("World", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  2684. // PARTICLES INPUTS
  2685. add_options.push_back(AddOption("Active", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2686. add_options.push_back(AddOption("Alpha", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2687. add_options.push_back(AddOption("Color", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2688. add_options.push_back(AddOption("Custom", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2689. add_options.push_back(AddOption("CustomAlpha", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2690. add_options.push_back(AddOption("Delta", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2691. add_options.push_back(AddOption("EmissionTransform", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2692. add_options.push_back(AddOption("Index", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2693. add_options.push_back(AddOption("LifeTime", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2694. add_options.push_back(AddOption("Restart", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2695. add_options.push_back(AddOption("Time", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2696. add_options.push_back(AddOption("Transform", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2697. add_options.push_back(AddOption("Velocity", "Input", "Emit", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  2698. add_options.push_back(AddOption("Active", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2699. add_options.push_back(AddOption("Alpha", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2700. add_options.push_back(AddOption("Color", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2701. add_options.push_back(AddOption("Custom", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2702. add_options.push_back(AddOption("CustomAlpha", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2703. add_options.push_back(AddOption("Delta", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2704. add_options.push_back(AddOption("EmissionTransform", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2705. add_options.push_back(AddOption("Index", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2706. add_options.push_back(AddOption("LifeTime", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2707. add_options.push_back(AddOption("Restart", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2708. add_options.push_back(AddOption("Time", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2709. add_options.push_back(AddOption("Transform", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2710. add_options.push_back(AddOption("Velocity", "Input", "Process", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  2711. add_options.push_back(AddOption("Active", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2712. add_options.push_back(AddOption("Alpha", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2713. add_options.push_back(AddOption("Color", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2714. add_options.push_back(AddOption("Custom", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2715. add_options.push_back(AddOption("CustomAlpha", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2716. add_options.push_back(AddOption("Delta", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2717. add_options.push_back(AddOption("EmissionTransform", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2718. add_options.push_back(AddOption("Index", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2719. add_options.push_back(AddOption("LifeTime", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2720. add_options.push_back(AddOption("Restart", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2721. add_options.push_back(AddOption("Time", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2722. add_options.push_back(AddOption("Transform", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2723. add_options.push_back(AddOption("Velocity", "Input", "End", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_END, Shader::MODE_PARTICLES));
  2724. // SKY INPUTS
  2725. add_options.push_back(AddOption("AtCubeMapPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_cubemap_pass"), "at_cubemap_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2726. add_options.push_back(AddOption("AtHalfResPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_half_res_pass"), "at_half_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2727. add_options.push_back(AddOption("AtQuarterResPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "at_quarter_res_pass"), "at_quarter_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2728. add_options.push_back(AddOption("EyeDir", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "eyedir"), "eyedir", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2729. add_options.push_back(AddOption("HalfResColor", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "half_res_color"), "half_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2730. add_options.push_back(AddOption("HalfResAlpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "half_res_alpha"), "half_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2731. add_options.push_back(AddOption("Light0Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_color"), "light0_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2732. add_options.push_back(AddOption("Light0Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_direction"), "light0_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2733. add_options.push_back(AddOption("Light0Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_enabled"), "light0_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2734. add_options.push_back(AddOption("Light0Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light0_energy"), "light0_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2735. add_options.push_back(AddOption("Light1Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_color"), "light1_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2736. add_options.push_back(AddOption("Light1Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_direction"), "light1_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2737. add_options.push_back(AddOption("Light1Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_enabled"), "light1_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2738. add_options.push_back(AddOption("Light1Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light1_energy"), "light1_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2739. add_options.push_back(AddOption("Light2Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_color"), "light2_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2740. add_options.push_back(AddOption("Light2Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_direction"), "light2_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2741. add_options.push_back(AddOption("Light2Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_enabled"), "light2_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2742. add_options.push_back(AddOption("Light2Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light2_energy"), "light2_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2743. add_options.push_back(AddOption("Light3Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_color"), "light3_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2744. add_options.push_back(AddOption("Light3Direction", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_direction"), "light3_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2745. add_options.push_back(AddOption("Light3Enabled", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_enabled"), "light3_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2746. add_options.push_back(AddOption("Light3Energy", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "light3_energy"), "light3_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2747. add_options.push_back(AddOption("Position", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "position"), "position", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2748. add_options.push_back(AddOption("QuarterResColor", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "quarter_res_color"), "quarter_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2749. add_options.push_back(AddOption("QuarterResAlpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "quarter_res_alpha"), "quarter_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2750. add_options.push_back(AddOption("Radiance", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "radiance"), "radiance", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2751. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2752. add_options.push_back(AddOption("SkyCoords", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "sky_coords"), "sky_coords", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2753. add_options.push_back(AddOption("Time", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_SKY));
  2754. // SCALAR
  2755. add_options.push_back(AddOption("FloatFunc", "Scalar", "Common", "VisualShaderNodeFloatFunc", TTR("Float function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2756. add_options.push_back(AddOption("IntFunc", "Scalar", "Common", "VisualShaderNodeIntFunc", TTR("Integer function."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2757. add_options.push_back(AddOption("FloatOp", "Scalar", "Common", "VisualShaderNodeFloatOp", TTR("Float operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2758. add_options.push_back(AddOption("IntOp", "Scalar", "Common", "VisualShaderNodeIntOp", TTR("Integer operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2759. //CONSTANTS
  2760. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  2761. add_options.push_back(AddOption(float_constant_defs[i].name, "Scalar", "Constants", "VisualShaderNodeFloatConstant", float_constant_defs[i].desc, -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, float_constant_defs[i].value));
  2762. }
  2763. // FUNCTIONS
  2764. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeFloatFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  2765. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeIntFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2766. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  2767. add_options.push_back(AddOption("ACosH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2768. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2769. add_options.push_back(AddOption("ASinH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2770. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2771. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeFloatOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  2772. add_options.push_back(AddOption("ATanH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2773. add_options.push_back(AddOption("Ceil", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeFloatFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_SCALAR));
  2774. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeScalarClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2775. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Constrains a value to lie between two further values."), VisualShaderNodeIntFunc::FUNC_CLAMP, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2776. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  2777. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2778. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeFloatFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  2779. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-e Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  2780. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  2781. add_options.push_back(AddOption("Floor", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeFloatFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_SCALAR));
  2782. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeFloatFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  2783. add_options.push_back(AddOption("InverseSqrt", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeFloatFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2784. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Natural logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  2785. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  2786. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the greater of two values."), VisualShaderNodeFloatOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  2787. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the lesser of two values."), VisualShaderNodeFloatOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2788. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeScalarInterp", TTR("Linear interpolation between two scalars."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2789. add_options.push_back(AddOption("MultiplyAdd", "Scalar", "Functions", "VisualShaderNodeMultiplyAdd", TTR("Performs a fused multiply-add operation (a * b + c) on scalars."), VisualShaderNodeMultiplyAdd::OP_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR));
  2790. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeFloatFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2791. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeIntFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2792. add_options.push_back(AddOption("OneMinus", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 - scalar"), VisualShaderNodeFloatFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  2793. add_options.push_back(AddOption("Pow", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeFloatOp::OP_POW, VisualShaderNode::PORT_TYPE_SCALAR));
  2794. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeFloatFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  2795. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 / scalar"), VisualShaderNodeFloatFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  2796. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeFloatFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  2797. add_options.push_back(AddOption("RoundEven", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeFloatFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_SCALAR));
  2798. add_options.push_back(AddOption("Saturate", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeFloatFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2799. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  2800. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeIntFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2801. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2802. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2803. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeFloatFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2804. 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));
  2805. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeFloatOp::OP_STEP, VisualShaderNode::PORT_TYPE_SCALAR));
  2806. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2807. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2808. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeFloatFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  2809. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Sums two floating-point scalars."), VisualShaderNodeFloatOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  2810. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Sums two integer scalars."), VisualShaderNodeIntOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2811. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Divides two floating-point scalars."), VisualShaderNodeFloatOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  2812. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Divides two integer scalars."), VisualShaderNodeIntOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2813. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Multiplies two floating-point scalars."), VisualShaderNodeFloatOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  2814. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Multiplies two integer scalars."), VisualShaderNodeIntOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2815. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Returns the remainder of the two floating-point scalars."), VisualShaderNodeFloatOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR));
  2816. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Returns the remainder of the two integer scalars."), VisualShaderNodeIntOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2817. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Subtracts two floating-point scalars."), VisualShaderNodeFloatOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  2818. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Subtracts two integer scalars."), VisualShaderNodeIntOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2819. add_options.push_back(AddOption("FloatConstant", "Scalar", "Variables", "VisualShaderNodeFloatConstant", TTR("Scalar floating-point constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2820. add_options.push_back(AddOption("IntConstant", "Scalar", "Variables", "VisualShaderNodeIntConstant", TTR("Scalar integer constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2821. add_options.push_back(AddOption("FloatUniform", "Scalar", "Variables", "VisualShaderNodeFloatUniform", TTR("Scalar floating-point uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2822. add_options.push_back(AddOption("IntUniform", "Scalar", "Variables", "VisualShaderNodeIntUniform", TTR("Scalar integer uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  2823. // TEXTURES
  2824. cubemap_node_option_idx = add_options.size();
  2825. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubemap", TTR("Perform the cubic texture lookup."), -1, -1));
  2826. curve_node_option_idx = add_options.size();
  2827. add_options.push_back(AddOption("CurveTexture", "Textures", "Functions", "VisualShaderNodeCurveTexture", TTR("Perform the curve texture lookup."), -1, -1));
  2828. texture2d_node_option_idx = add_options.size();
  2829. add_options.push_back(AddOption("Texture2D", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the 2D texture lookup."), -1, -1));
  2830. texture2d_array_node_option_idx = add_options.size();
  2831. add_options.push_back(AddOption("Texture2DArray", "Textures", "Functions", "VisualShaderNodeTexture2DArray", TTR("Perform the 2D-array texture lookup."), -1, -1, -1, -1, -1));
  2832. texture3d_node_option_idx = add_options.size();
  2833. add_options.push_back(AddOption("Texture3D", "Textures", "Functions", "VisualShaderNodeTexture3D", TTR("Perform the 3D texture lookup."), -1, -1));
  2834. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubemapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  2835. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  2836. add_options.push_back(AddOption("TextureUniformTriplanar", "Textures", "Variables", "VisualShaderNodeTextureUniformTriplanar", TTR("2D texture uniform lookup with triplanar."), -1, -1, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  2837. add_options.push_back(AddOption("Texture2DArrayUniform", "Textures", "Variables", "VisualShaderNodeTexture2DArrayUniform", TTR("2D array of textures uniform lookup."), -1, -1, -1, -1, -1));
  2838. add_options.push_back(AddOption("Texture3DUniform", "Textures", "Variables", "VisualShaderNodeTexture3DUniform", TTR("3D texture uniform lookup."), -1, -1, -1, -1, -1));
  2839. // TRANSFORM
  2840. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2841. 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));
  2842. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2843. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  2844. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2845. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2846. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2847. add_options.push_back(AddOption("TransformMult", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2848. add_options.push_back(AddOption("TransformVectorMult", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2849. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2850. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2851. // VECTOR
  2852. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2853. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2854. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2855. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  2856. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  2857. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  2858. 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));
  2859. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2860. 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));
  2861. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2862. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  2863. 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));
  2864. 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));
  2865. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeVectorClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2866. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  2867. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2868. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  2869. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  2870. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2871. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2872. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  2873. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  2874. 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));
  2875. 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));
  2876. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  2877. 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));
  2878. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2879. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  2880. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  2881. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  2882. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2883. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2884. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2885. add_options.push_back(AddOption("MultiplyAdd", "Vector", "Functions", "VisualShaderNodeMultiplyAdd", TTR("Performs a fused multiply-add operation (a * b + c) on vectors."), VisualShaderNodeMultiplyAdd::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  2886. add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2887. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  2888. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  2889. 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));
  2890. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  2891. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  2892. 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));
  2893. 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));
  2894. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  2895. 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));
  2896. 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));
  2897. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  2898. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2899. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2900. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2901. 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));
  2902. 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));
  2903. 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));
  2904. 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));
  2905. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2906. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2907. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  2908. add_options.push_back(AddOption("Add", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  2909. add_options.push_back(AddOption("Divide", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  2910. add_options.push_back(AddOption("Multiply", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  2911. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  2912. add_options.push_back(AddOption("Subtract", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  2913. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2914. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2915. // SPECIAL
  2916. 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.")));
  2917. 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));
  2918. 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.")));
  2919. add_options.push_back(AddOption("UniformRef", "Special", "", "VisualShaderNodeUniformRef", TTR("A reference to an existing uniform.")));
  2920. add_options.push_back(AddOption("ScalarDerivativeFunc", "Special", "Common", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) Scalar derivative function."), -1, VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2921. add_options.push_back(AddOption("VectorDerivativeFunc", "Special", "Common", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) Vector derivative function."), -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2922. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2923. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2924. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2925. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2926. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2927. 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, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, -1, -1, true));
  2928. custom_node_option_idx = add_options.size();
  2929. /////////////////////////////////////////////////////////////////////
  2930. _update_options_menu();
  2931. error_panel = memnew(PanelContainer);
  2932. add_child(error_panel);
  2933. error_label = memnew(Label);
  2934. error_panel->add_child(error_label);
  2935. error_label->set_text("eh");
  2936. error_panel->hide();
  2937. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2938. Ref<VisualShaderNodePluginDefault> default_plugin;
  2939. default_plugin.instance();
  2940. add_plugin(default_plugin);
  2941. graph_plugin.instance();
  2942. property_editor = memnew(CustomPropertyEditor);
  2943. add_child(property_editor);
  2944. property_editor->connect("variant_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
  2945. }
  2946. /////////////////
  2947. void VisualShaderEditorPlugin::edit(Object *p_object) {
  2948. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  2949. }
  2950. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  2951. return p_object->is_class("VisualShader");
  2952. }
  2953. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  2954. if (p_visible) {
  2955. //editor->hide_animation_player_editors();
  2956. //editor->animation_panel_make_visible(true);
  2957. button->show();
  2958. editor->make_bottom_panel_item_visible(visual_shader_editor);
  2959. visual_shader_editor->update_custom_nodes();
  2960. visual_shader_editor->set_process_input(true);
  2961. //visual_shader_editor->set_process(true);
  2962. } else {
  2963. if (visual_shader_editor->is_visible_in_tree()) {
  2964. editor->hide_bottom_panel();
  2965. }
  2966. button->hide();
  2967. visual_shader_editor->set_process_input(false);
  2968. //visual_shader_editor->set_process(false);
  2969. }
  2970. }
  2971. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  2972. editor = p_node;
  2973. visual_shader_editor = memnew(VisualShaderEditor);
  2974. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2975. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  2976. button->hide();
  2977. }
  2978. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  2979. }
  2980. ////////////////
  2981. class VisualShaderNodePluginInputEditor : public OptionButton {
  2982. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  2983. Ref<VisualShaderNodeInput> input;
  2984. public:
  2985. void _notification(int p_what) {
  2986. if (p_what == NOTIFICATION_READY) {
  2987. connect("item_selected", callable_mp(this, &VisualShaderNodePluginInputEditor::_item_selected));
  2988. }
  2989. }
  2990. void _item_selected(int p_item) {
  2991. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  2992. }
  2993. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  2994. input = p_input;
  2995. Ref<Texture2D> type_icon[6] = {
  2996. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  2997. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  2998. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  2999. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  3000. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"),
  3001. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  3002. };
  3003. add_item("[None]");
  3004. int to_select = -1;
  3005. for (int i = 0; i < input->get_input_index_count(); i++) {
  3006. if (input->get_input_name() == input->get_input_index_name(i)) {
  3007. to_select = i + 1;
  3008. }
  3009. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  3010. }
  3011. if (to_select >= 0) {
  3012. select(to_select);
  3013. }
  3014. }
  3015. };
  3016. ////////////////
  3017. class VisualShaderNodePluginUniformRefEditor : public OptionButton {
  3018. GDCLASS(VisualShaderNodePluginUniformRefEditor, OptionButton);
  3019. Ref<VisualShaderNodeUniformRef> uniform_ref;
  3020. public:
  3021. void _notification(int p_what) {
  3022. if (p_what == NOTIFICATION_READY) {
  3023. connect("item_selected", callable_mp(this, &VisualShaderNodePluginUniformRefEditor::_item_selected));
  3024. }
  3025. }
  3026. void _item_selected(int p_item) {
  3027. VisualShaderEditor::get_singleton()->call_deferred("_uniform_select_item", uniform_ref, get_item_text(p_item));
  3028. }
  3029. void setup(const Ref<VisualShaderNodeUniformRef> &p_uniform_ref) {
  3030. uniform_ref = p_uniform_ref;
  3031. Ref<Texture2D> type_icon[7] = {
  3032. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  3033. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  3034. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  3035. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  3036. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform", "EditorIcons"),
  3037. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Color", "EditorIcons"),
  3038. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  3039. };
  3040. add_item("[None]");
  3041. int to_select = -1;
  3042. for (int i = 0; i < p_uniform_ref->get_uniforms_count(); i++) {
  3043. if (p_uniform_ref->get_uniform_name() == p_uniform_ref->get_uniform_name_by_index(i)) {
  3044. to_select = i + 1;
  3045. }
  3046. add_icon_item(type_icon[p_uniform_ref->get_uniform_type_by_index(i)], p_uniform_ref->get_uniform_name_by_index(i));
  3047. }
  3048. if (to_select >= 0) {
  3049. select(to_select);
  3050. }
  3051. }
  3052. };
  3053. ////////////////
  3054. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  3055. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  3056. Ref<Resource> parent_resource;
  3057. int node_id;
  3058. VisualShader::Type shader_type;
  3059. public:
  3060. void _property_changed(const String &p_property, const Variant &p_value, const String &p_field = "", bool p_changing = false) {
  3061. if (p_changing) {
  3062. return;
  3063. }
  3064. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3065. updating = true;
  3066. undo_redo->create_action(TTR("Edit Visual Property") + ": " + p_property, UndoRedo::MERGE_ENDS);
  3067. undo_redo->add_do_property(node.ptr(), p_property, p_value);
  3068. undo_redo->add_undo_property(node.ptr(), p_property, node->get(p_property));
  3069. if (p_value.get_type() == Variant::OBJECT) {
  3070. RES prev_res = node->get(p_property);
  3071. RES curr_res = p_value;
  3072. if (curr_res.is_null()) {
  3073. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  3074. } else {
  3075. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  3076. }
  3077. if (!prev_res.is_null()) {
  3078. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  3079. } else {
  3080. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  3081. }
  3082. }
  3083. if (p_property != "constant") {
  3084. undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  3085. undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  3086. } else {
  3087. undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id);
  3088. undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id);
  3089. }
  3090. undo_redo->commit_action();
  3091. updating = false;
  3092. }
  3093. void _node_changed() {
  3094. if (updating) {
  3095. return;
  3096. }
  3097. for (int i = 0; i < properties.size(); i++) {
  3098. properties[i]->update_property();
  3099. }
  3100. }
  3101. void _resource_selected(const String &p_path, RES p_resource) {
  3102. _open_inspector(p_resource);
  3103. }
  3104. void _open_inspector(RES p_resource) {
  3105. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  3106. }
  3107. bool updating;
  3108. Ref<VisualShaderNode> node;
  3109. Vector<EditorProperty *> properties;
  3110. Vector<Label *> prop_names;
  3111. void _show_prop_names(bool p_show) {
  3112. for (int i = 0; i < prop_names.size(); i++) {
  3113. prop_names[i]->set_visible(p_show);
  3114. }
  3115. }
  3116. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  3117. parent_resource = p_parent_resource;
  3118. updating = false;
  3119. node = p_node;
  3120. properties = p_properties;
  3121. node_id = (int)p_node->get_meta("id");
  3122. shader_type = VisualShader::Type((int)p_node->get_meta("shader_type"));
  3123. for (int i = 0; i < p_properties.size(); i++) {
  3124. HBoxContainer *hbox = memnew(HBoxContainer);
  3125. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3126. add_child(hbox);
  3127. Label *prop_name = memnew(Label);
  3128. String prop_name_str = p_names[i];
  3129. prop_name_str = prop_name_str.capitalize() + ":";
  3130. prop_name->set_text(prop_name_str);
  3131. prop_name->set_visible(false);
  3132. hbox->add_child(prop_name);
  3133. prop_names.push_back(prop_name);
  3134. p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  3135. hbox->add_child(p_properties[i]);
  3136. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  3137. if (res_prop) {
  3138. p_properties[i]->connect("resource_selected", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_resource_selected));
  3139. }
  3140. properties[i]->connect("property_changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_property_changed));
  3141. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  3142. properties[i]->update_property();
  3143. properties[i]->set_name_split_ratio(0);
  3144. }
  3145. node->connect("changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_node_changed));
  3146. }
  3147. static void _bind_methods() {
  3148. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector); // Used by UndoRedo.
  3149. ClassDB::bind_method("_show_prop_names", &VisualShaderNodePluginDefaultEditor::_show_prop_names); // Used with call_deferred.
  3150. }
  3151. };
  3152. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  3153. if (p_node->is_class("VisualShaderNodeUniformRef")) {
  3154. //create input
  3155. VisualShaderNodePluginUniformRefEditor *uniform_editor = memnew(VisualShaderNodePluginUniformRefEditor);
  3156. uniform_editor->setup(p_node);
  3157. return uniform_editor;
  3158. }
  3159. if (p_node->is_class("VisualShaderNodeInput")) {
  3160. //create input
  3161. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  3162. input_editor->setup(p_node);
  3163. return input_editor;
  3164. }
  3165. Vector<StringName> properties = p_node->get_editable_properties();
  3166. if (properties.size() == 0) {
  3167. return nullptr;
  3168. }
  3169. List<PropertyInfo> props;
  3170. p_node->get_property_list(&props);
  3171. Vector<PropertyInfo> pinfo;
  3172. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3173. for (int i = 0; i < properties.size(); i++) {
  3174. if (E->get().name == String(properties[i])) {
  3175. pinfo.push_back(E->get());
  3176. }
  3177. }
  3178. }
  3179. if (pinfo.size() == 0) {
  3180. return nullptr;
  3181. }
  3182. properties.clear();
  3183. Ref<VisualShaderNode> node = p_node;
  3184. Vector<EditorProperty *> editors;
  3185. for (int i = 0; i < pinfo.size(); i++) {
  3186. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  3187. if (!prop) {
  3188. return nullptr;
  3189. }
  3190. if (Object::cast_to<EditorPropertyResource>(prop)) {
  3191. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  3192. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3193. } else if (Object::cast_to<EditorPropertyTransform>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  3194. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  3195. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  3196. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3197. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  3198. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3199. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  3200. }
  3201. editors.push_back(prop);
  3202. properties.push_back(pinfo[i].name);
  3203. }
  3204. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  3205. editor->setup(p_parent_resource, editors, properties, p_node);
  3206. return editor;
  3207. }
  3208. void EditorPropertyShaderMode::_option_selected(int p_which) {
  3209. //will not use this, instead will do all the logic setting manually
  3210. //emit_signal("property_changed", get_edited_property(), p_which);
  3211. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  3212. if (visual_shader->get_mode() == p_which) {
  3213. return;
  3214. }
  3215. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3216. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  3217. //do is easy
  3218. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  3219. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  3220. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_set_mode", p_which);
  3221. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_set_mode", visual_shader->get_mode());
  3222. //now undo is hell
  3223. //1. restore connections to output
  3224. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  3225. VisualShader::Type type = VisualShader::Type(i);
  3226. List<VisualShader::Connection> conns;
  3227. visual_shader->get_node_connections(type, &conns);
  3228. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  3229. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  3230. 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);
  3231. }
  3232. }
  3233. }
  3234. //2. restore input indices
  3235. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  3236. VisualShader::Type type = VisualShader::Type(i);
  3237. Vector<int> nodes = visual_shader->get_node_list(type);
  3238. for (int j = 0; j < nodes.size(); j++) {
  3239. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  3240. if (!input.is_valid()) {
  3241. continue;
  3242. }
  3243. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  3244. }
  3245. }
  3246. //3. restore enums and flags
  3247. List<PropertyInfo> props;
  3248. visual_shader->get_property_list(&props);
  3249. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3250. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  3251. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  3252. }
  3253. }
  3254. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  3255. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  3256. //update graph
  3257. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  3258. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  3259. undo_redo->commit_action();
  3260. }
  3261. void EditorPropertyShaderMode::update_property() {
  3262. int which = get_edited_object()->get(get_edited_property());
  3263. options->select(which);
  3264. }
  3265. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  3266. for (int i = 0; i < p_options.size(); i++) {
  3267. options->add_item(p_options[i], i);
  3268. }
  3269. }
  3270. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  3271. options->set_clip_text(p_enable);
  3272. }
  3273. void EditorPropertyShaderMode::_bind_methods() {
  3274. }
  3275. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  3276. options = memnew(OptionButton);
  3277. options->set_clip_text(true);
  3278. add_child(options);
  3279. add_focusable(options);
  3280. options->connect("item_selected", callable_mp(this, &EditorPropertyShaderMode::_option_selected));
  3281. }
  3282. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  3283. return true; //can handle everything
  3284. }
  3285. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  3286. //do none
  3287. }
  3288. 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, bool p_wide) {
  3289. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  3290. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  3291. Vector<String> options = p_hint_text.split(",");
  3292. editor->setup(options);
  3293. add_property_editor(p_path, editor);
  3294. return true;
  3295. }
  3296. return false; //can be overridden, although it will most likely be last anyway
  3297. }
  3298. void EditorInspectorShaderModePlugin::parse_end() {
  3299. //do none
  3300. }
  3301. //////////////////////////////////
  3302. void VisualShaderNodePortPreview::_shader_changed() {
  3303. if (shader.is_null()) {
  3304. return;
  3305. }
  3306. Vector<VisualShader::DefaultTextureParam> default_textures;
  3307. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  3308. Ref<Shader> preview_shader;
  3309. preview_shader.instance();
  3310. preview_shader->set_code(shader_code);
  3311. for (int i = 0; i < default_textures.size(); i++) {
  3312. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  3313. }
  3314. Ref<ShaderMaterial> material;
  3315. material.instance();
  3316. material->set_shader(preview_shader);
  3317. //find if a material is also being edited and copy parameters to this one
  3318. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  3319. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  3320. if (!object) {
  3321. continue;
  3322. }
  3323. ShaderMaterial *src_mat = Object::cast_to<ShaderMaterial>(object);
  3324. if (src_mat && src_mat->get_shader().is_valid()) {
  3325. List<PropertyInfo> params;
  3326. src_mat->get_shader()->get_param_list(&params);
  3327. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  3328. material->set(E->get().name, src_mat->get(E->get().name));
  3329. }
  3330. }
  3331. }
  3332. set_material(material);
  3333. }
  3334. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  3335. shader = p_shader;
  3336. shader->connect("changed", callable_mp(this, &VisualShaderNodePortPreview::_shader_changed));
  3337. type = p_type;
  3338. port = p_port;
  3339. node = p_node;
  3340. update();
  3341. _shader_changed();
  3342. }
  3343. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  3344. return Size2(100, 100) * EDSCALE;
  3345. }
  3346. void VisualShaderNodePortPreview::_notification(int p_what) {
  3347. if (p_what == NOTIFICATION_DRAW) {
  3348. Vector<Vector2> points;
  3349. Vector<Vector2> uvs;
  3350. Vector<Color> colors;
  3351. points.push_back(Vector2());
  3352. uvs.push_back(Vector2(0, 0));
  3353. colors.push_back(Color(1, 1, 1, 1));
  3354. points.push_back(Vector2(get_size().width, 0));
  3355. uvs.push_back(Vector2(1, 0));
  3356. colors.push_back(Color(1, 1, 1, 1));
  3357. points.push_back(get_size());
  3358. uvs.push_back(Vector2(1, 1));
  3359. colors.push_back(Color(1, 1, 1, 1));
  3360. points.push_back(Vector2(0, get_size().height));
  3361. uvs.push_back(Vector2(0, 1));
  3362. colors.push_back(Color(1, 1, 1, 1));
  3363. draw_primitive(points, colors, uvs);
  3364. }
  3365. }
  3366. void VisualShaderNodePortPreview::_bind_methods() {
  3367. }
  3368. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  3369. }
  3370. //////////////////////////////////
  3371. String VisualShaderConversionPlugin::converts_to() const {
  3372. return "Shader";
  3373. }
  3374. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  3375. Ref<VisualShader> vshader = p_resource;
  3376. return vshader.is_valid();
  3377. }
  3378. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  3379. Ref<VisualShader> vshader = p_resource;
  3380. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  3381. Ref<Shader> shader;
  3382. shader.instance();
  3383. String code = vshader->get_code();
  3384. shader->set_code(code);
  3385. return shader;
  3386. }