2
0

visual_shader_editor_plugin.cpp 209 KB

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