visual_shader_editor_plugin.cpp 250 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000
  1. /*************************************************************************/
  2. /* visual_shader_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "visual_shader_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/math_defs.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/version.h"
  37. #include "editor/editor_log.h"
  38. #include "editor/editor_properties.h"
  39. #include "editor/editor_scale.h"
  40. #include "scene/animation/animation_player.h"
  41. #include "scene/gui/menu_button.h"
  42. #include "scene/gui/panel.h"
  43. #include "scene/main/window.h"
  44. #include "scene/resources/visual_shader_nodes.h"
  45. #include "scene/resources/visual_shader_particle_nodes.h"
  46. #include "scene/resources/visual_shader_sdf_nodes.h"
  47. #include "servers/display_server.h"
  48. #include "servers/rendering/shader_types.h"
  49. struct FloatConstantDef {
  50. String name;
  51. float value = 0;
  52. String desc;
  53. };
  54. static FloatConstantDef float_constant_defs[] = {
  55. { "E", Math_E, TTR("E constant (2.718282). Represents the base of the natural logarithm.") },
  56. { "Epsilon", CMP_EPSILON, TTR("Epsilon constant (0.00001). Smallest possible scalar number.") },
  57. { "Phi", 1.618034f, TTR("Phi constant (1.618034). Golden ratio.") },
  58. { "Pi/4", Math_PI / 4, TTR("Pi/4 constant (0.785398) or 45 degrees.") },
  59. { "Pi/2", Math_PI / 2, TTR("Pi/2 constant (1.570796) or 90 degrees.") },
  60. { "Pi", Math_PI, TTR("Pi constant (3.141593) or 180 degrees.") },
  61. { "Tau", Math_TAU, TTR("Tau constant (6.283185) or 360 degrees.") },
  62. { "Sqrt2", Math_SQRT2, TTR("Sqrt2 constant (1.414214). Square root of 2.") }
  63. };
  64. const int MAX_FLOAT_CONST_DEFS = sizeof(float_constant_defs) / sizeof(FloatConstantDef);
  65. ///////////////////
  66. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  67. if (get_script_instance()) {
  68. return get_script_instance()->call("_create_editor", p_parent_resource, p_node);
  69. }
  70. return nullptr;
  71. }
  72. void VisualShaderNodePlugin::_bind_methods() {
  73. 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")));
  74. }
  75. ///////////////////
  76. 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) {
  77. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  78. style->set_default_margin(SIDE_LEFT, p_margin_left * EDSCALE);
  79. style->set_default_margin(SIDE_RIGHT, p_margin_right * EDSCALE);
  80. style->set_default_margin(SIDE_BOTTOM, p_margin_bottom * EDSCALE);
  81. style->set_default_margin(SIDE_TOP, p_margin_top * EDSCALE);
  82. return style;
  83. }
  84. ///////////////////
  85. VisualShaderGraphPlugin::VisualShaderGraphPlugin() {
  86. }
  87. void VisualShaderGraphPlugin::_bind_methods() {
  88. ClassDB::bind_method("add_node", &VisualShaderGraphPlugin::add_node);
  89. ClassDB::bind_method("remove_node", &VisualShaderGraphPlugin::remove_node);
  90. ClassDB::bind_method("connect_nodes", &VisualShaderGraphPlugin::connect_nodes);
  91. ClassDB::bind_method("disconnect_nodes", &VisualShaderGraphPlugin::disconnect_nodes);
  92. ClassDB::bind_method("set_node_position", &VisualShaderGraphPlugin::set_node_position);
  93. ClassDB::bind_method("set_node_size", &VisualShaderGraphPlugin::set_node_size);
  94. ClassDB::bind_method("update_node", &VisualShaderGraphPlugin::update_node);
  95. ClassDB::bind_method("update_node_deferred", &VisualShaderGraphPlugin::update_node_deferred);
  96. ClassDB::bind_method("set_input_port_default_value", &VisualShaderGraphPlugin::set_input_port_default_value);
  97. ClassDB::bind_method("set_uniform_name", &VisualShaderGraphPlugin::set_uniform_name);
  98. ClassDB::bind_method("set_expression", &VisualShaderGraphPlugin::set_expression);
  99. ClassDB::bind_method("update_curve", &VisualShaderGraphPlugin::update_curve);
  100. ClassDB::bind_method("update_constant", &VisualShaderGraphPlugin::update_constant);
  101. }
  102. void VisualShaderGraphPlugin::register_shader(VisualShader *p_shader) {
  103. visual_shader = Ref<VisualShader>(p_shader);
  104. }
  105. void VisualShaderGraphPlugin::set_connections(List<VisualShader::Connection> &p_connections) {
  106. connections = p_connections;
  107. }
  108. void VisualShaderGraphPlugin::show_port_preview(VisualShader::Type p_type, int p_node_id, int p_port_id) {
  109. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].output_ports.has(p_port_id)) {
  110. for (Map<int, Port>::Element *E = links[p_node_id].output_ports.front(); E; E = E->next()) {
  111. if (E->value().preview_button != nullptr) {
  112. E->value().preview_button->set_pressed(false);
  113. }
  114. }
  115. if (links[p_node_id].preview_visible && !is_dirty() && links[p_node_id].preview_box != nullptr) {
  116. links[p_node_id].graph_node->remove_child(links[p_node_id].preview_box);
  117. memdelete(links[p_node_id].preview_box);
  118. links[p_node_id].graph_node->set_size(Vector2(-1, -1));
  119. links[p_node_id].preview_visible = false;
  120. }
  121. if (p_port_id != -1 && links[p_node_id].output_ports[p_port_id].preview_button != nullptr) {
  122. if (is_dirty()) {
  123. links[p_node_id].preview_pos = links[p_node_id].graph_node->get_child_count();
  124. }
  125. VBoxContainer *vbox = memnew(VBoxContainer);
  126. links[p_node_id].graph_node->add_child(vbox);
  127. if (links[p_node_id].preview_pos != -1) {
  128. links[p_node_id].graph_node->move_child(vbox, links[p_node_id].preview_pos);
  129. }
  130. Control *offset = memnew(Control);
  131. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  132. vbox->add_child(offset);
  133. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  134. port_preview->setup(visual_shader, visual_shader->get_shader_type(), p_node_id, p_port_id);
  135. port_preview->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  136. vbox->add_child(port_preview);
  137. links[p_node_id].preview_visible = true;
  138. links[p_node_id].preview_box = vbox;
  139. links[p_node_id].output_ports[p_port_id].preview_button->set_pressed(true);
  140. }
  141. }
  142. }
  143. void VisualShaderGraphPlugin::update_node_deferred(VisualShader::Type p_type, int p_node_id) {
  144. call_deferred("update_node", p_type, p_node_id);
  145. }
  146. void VisualShaderGraphPlugin::update_node(VisualShader::Type p_type, int p_node_id) {
  147. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  148. return;
  149. }
  150. remove_node(p_type, p_node_id);
  151. add_node(p_type, p_node_id);
  152. }
  153. void VisualShaderGraphPlugin::set_input_port_default_value(VisualShader::Type p_type, int p_node_id, int p_port_id, Variant p_value) {
  154. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id)) {
  155. return;
  156. }
  157. Button *button = links[p_node_id].input_ports[p_port_id].default_input_button;
  158. switch (p_value.get_type()) {
  159. case Variant::COLOR: {
  160. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  161. if (!button->is_connected("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button))) {
  162. button->connect("draw", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_draw_color_over_button), varray(button, p_value));
  163. }
  164. } break;
  165. case Variant::BOOL: {
  166. button->set_text(((bool)p_value) ? "true" : "false");
  167. } break;
  168. case Variant::INT:
  169. case Variant::FLOAT: {
  170. button->set_text(String::num(p_value, 4));
  171. } break;
  172. case Variant::VECTOR3: {
  173. Vector3 v = p_value;
  174. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  175. } break;
  176. default: {
  177. }
  178. }
  179. }
  180. void VisualShaderGraphPlugin::set_uniform_name(VisualShader::Type p_type, int p_node_id, const String &p_name) {
  181. if (visual_shader->get_shader_type() == p_type && links.has(p_node_id) && links[p_node_id].uniform_name != nullptr) {
  182. links[p_node_id].uniform_name->set_text(p_name);
  183. }
  184. }
  185. void VisualShaderGraphPlugin::update_curve(int p_node_id) {
  186. if (links.has(p_node_id) && links[p_node_id].curve_editor) {
  187. if (((VisualShaderNodeCurveTexture *)links[p_node_id].visual_node)->get_texture().is_valid()) {
  188. links[p_node_id].curve_editor->set_curve(((VisualShaderNodeCurveTexture *)links[p_node_id].visual_node)->get_texture()->get_curve());
  189. }
  190. }
  191. }
  192. int VisualShaderGraphPlugin::get_constant_index(float p_constant) const {
  193. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  194. if (Math::is_equal_approx(p_constant, float_constant_defs[i].value)) {
  195. return i + 1;
  196. }
  197. }
  198. return 0;
  199. }
  200. void VisualShaderGraphPlugin::update_constant(VisualShader::Type p_type, int p_node_id) {
  201. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].const_op) {
  202. return;
  203. }
  204. VisualShaderNodeFloatConstant *float_const = Object::cast_to<VisualShaderNodeFloatConstant>(links[p_node_id].visual_node);
  205. if (!float_const) {
  206. return;
  207. }
  208. links[p_node_id].const_op->select(get_constant_index(float_const->get_constant()));
  209. links[p_node_id].graph_node->set_size(Size2(-1, -1));
  210. }
  211. void VisualShaderGraphPlugin::set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression) {
  212. if (p_type != visual_shader->get_shader_type() || !links.has(p_node_id) || !links[p_node_id].expression_edit) {
  213. return;
  214. }
  215. links[p_node_id].expression_edit->set_text(p_expression);
  216. }
  217. void VisualShaderGraphPlugin::update_node_size(int p_node_id) {
  218. if (!links.has(p_node_id)) {
  219. return;
  220. }
  221. links[p_node_id].graph_node->set_size(Size2(-1, -1));
  222. }
  223. void VisualShaderGraphPlugin::register_default_input_button(int p_node_id, int p_port_id, Button *p_button) {
  224. links[p_node_id].input_ports.insert(p_port_id, { p_button });
  225. }
  226. void VisualShaderGraphPlugin::register_constant_option_btn(int p_node_id, OptionButton *p_button) {
  227. links[p_node_id].const_op = p_button;
  228. }
  229. void VisualShaderGraphPlugin::register_expression_edit(int p_node_id, CodeEdit *p_expression_edit) {
  230. links[p_node_id].expression_edit = p_expression_edit;
  231. }
  232. void VisualShaderGraphPlugin::register_curve_editor(int p_node_id, CurveEditor *p_curve_editor) {
  233. links[p_node_id].curve_editor = p_curve_editor;
  234. }
  235. void VisualShaderGraphPlugin::update_uniform_refs() {
  236. for (Map<int, Link>::Element *E = links.front(); E; E = E->next()) {
  237. VisualShaderNodeUniformRef *ref = Object::cast_to<VisualShaderNodeUniformRef>(E->get().visual_node);
  238. if (ref) {
  239. remove_node(E->get().type, E->key());
  240. add_node(E->get().type, E->key());
  241. }
  242. }
  243. }
  244. VisualShader::Type VisualShaderGraphPlugin::get_shader_type() const {
  245. return visual_shader->get_shader_type();
  246. }
  247. void VisualShaderGraphPlugin::set_node_position(VisualShader::Type p_type, int p_id, const Vector2 &p_position) {
  248. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  249. links[p_id].graph_node->set_position_offset(p_position);
  250. }
  251. }
  252. void VisualShaderGraphPlugin::set_node_size(VisualShader::Type p_type, int p_id, const Vector2 &p_size) {
  253. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  254. links[p_id].graph_node->set_size(p_size);
  255. }
  256. }
  257. bool VisualShaderGraphPlugin::is_preview_visible(int p_id) const {
  258. return links[p_id].preview_visible;
  259. }
  260. void VisualShaderGraphPlugin::clear_links() {
  261. links.clear();
  262. }
  263. bool VisualShaderGraphPlugin::is_dirty() const {
  264. return dirty;
  265. }
  266. void VisualShaderGraphPlugin::make_dirty(bool p_enabled) {
  267. dirty = p_enabled;
  268. }
  269. void VisualShaderGraphPlugin::register_link(VisualShader::Type p_type, int p_id, VisualShaderNode *p_visual_node, GraphNode *p_graph_node) {
  270. links.insert(p_id, { p_type, p_visual_node, p_graph_node, p_visual_node->get_output_port_for_preview() != -1, -1, Map<int, InputPort>(), Map<int, Port>(), nullptr, nullptr, nullptr, nullptr, nullptr });
  271. }
  272. void VisualShaderGraphPlugin::register_output_port(int p_node_id, int p_port, TextureButton *p_button) {
  273. links[p_node_id].output_ports.insert(p_port, { p_button });
  274. }
  275. void VisualShaderGraphPlugin::register_uniform_name(int p_node_id, LineEdit *p_uniform_name) {
  276. links[p_node_id].uniform_name = p_uniform_name;
  277. }
  278. void VisualShaderGraphPlugin::update_theme() {
  279. vector_expanded_color[0] = VisualShaderEditor::get_singleton()->get_theme_color("axis_x_color", "Editor"); // red
  280. vector_expanded_color[1] = VisualShaderEditor::get_singleton()->get_theme_color("axis_y_color", "Editor"); // green
  281. vector_expanded_color[2] = VisualShaderEditor::get_singleton()->get_theme_color("axis_z_color", "Editor"); // blue
  282. }
  283. void VisualShaderGraphPlugin::add_node(VisualShader::Type p_type, int p_id) {
  284. if (p_type != visual_shader->get_shader_type()) {
  285. return;
  286. }
  287. Control *offset;
  288. static Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  289. static const Color type_color[6] = {
  290. Color(0.38, 0.85, 0.96), // scalar (float)
  291. Color(0.49, 0.78, 0.94), // scalar (int)
  292. Color(0.84, 0.49, 0.93), // vector
  293. Color(0.55, 0.65, 0.94), // boolean
  294. Color(0.96, 0.66, 0.43), // transform
  295. Color(1.0, 1.0, 0.0), // sampler
  296. };
  297. static const String vector_expanded_name[3] = {
  298. "red",
  299. "green",
  300. "blue"
  301. };
  302. Ref<VisualShaderNode> vsnode = visual_shader->get_node(p_type, p_id);
  303. Ref<VisualShaderNodeResizableBase> resizable_node = Object::cast_to<VisualShaderNodeResizableBase>(vsnode.ptr());
  304. bool is_resizable = !resizable_node.is_null();
  305. Size2 size = Size2(0, 0);
  306. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  307. bool is_group = !group_node.is_null();
  308. bool is_comment = false;
  309. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  310. bool is_expression = !expression_node.is_null();
  311. String expression = "";
  312. VisualShaderNodeCustom *custom_node = Object::cast_to<VisualShaderNodeCustom>(vsnode.ptr());
  313. if (custom_node) {
  314. custom_node->_set_initialized(true);
  315. }
  316. GraphNode *node = memnew(GraphNode);
  317. register_link(p_type, p_id, vsnode.ptr(), node);
  318. if (is_resizable) {
  319. size = resizable_node->get_size();
  320. node->set_resizable(true);
  321. node->connect("resize_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_resized), varray((int)p_type, p_id));
  322. }
  323. if (is_expression) {
  324. expression = expression_node->get_expression();
  325. }
  326. node->set_position_offset(visual_shader->get_node_position(p_type, p_id));
  327. node->set_title(vsnode->get_caption());
  328. node->set_name(itos(p_id));
  329. if (p_id >= 2) {
  330. node->set_show_close_button(true);
  331. node->connect("close_request", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_delete_node_request), varray(p_type, p_id), CONNECT_DEFERRED);
  332. }
  333. node->connect("dragged", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_node_dragged), varray(p_id));
  334. Control *custom_editor = nullptr;
  335. int port_offset = 0;
  336. if (is_group) {
  337. port_offset += 2;
  338. }
  339. if (is_resizable) {
  340. Ref<VisualShaderNodeComment> comment_node = Object::cast_to<VisualShaderNodeComment>(vsnode.ptr());
  341. if (comment_node.is_valid()) {
  342. is_comment = true;
  343. node->set_comment(true);
  344. Label *comment_label = memnew(Label);
  345. node->add_child(comment_label);
  346. comment_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  347. comment_label->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  348. comment_label->set_text(comment_node->get_description());
  349. }
  350. }
  351. Ref<VisualShaderNodeParticleEmit> emit = vsnode;
  352. if (emit.is_valid()) {
  353. node->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  354. }
  355. Ref<VisualShaderNodeUniform> uniform = vsnode;
  356. if (uniform.is_valid()) {
  357. VisualShaderEditor::get_singleton()->graph->add_child(node);
  358. VisualShaderEditor::get_singleton()->_update_created_node(node);
  359. LineEdit *uniform_name = memnew(LineEdit);
  360. register_uniform_name(p_id, uniform_name);
  361. uniform_name->set_text(uniform->get_uniform_name());
  362. node->add_child(uniform_name);
  363. uniform_name->connect("text_submitted", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_changed), varray(p_id));
  364. uniform_name->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_uniform_line_edit_focus_out), varray(uniform_name, p_id));
  365. if (vsnode->get_input_port_count() == 0 && vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  366. //shortcut
  367. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  368. node->set_slot(0, false, VisualShaderNode::PORT_TYPE_SCALAR, Color(), true, port_right, type_color[port_right]);
  369. if (!vsnode->is_use_prop_slots()) {
  370. return;
  371. }
  372. }
  373. port_offset++;
  374. }
  375. for (int i = 0; i < VisualShaderEditor::get_singleton()->plugins.size(); i++) {
  376. vsnode->set_meta("id", p_id);
  377. vsnode->set_meta("shader_type", (int)p_type);
  378. custom_editor = VisualShaderEditor::get_singleton()->plugins.write[i]->create_editor(visual_shader, vsnode);
  379. vsnode->remove_meta("id");
  380. vsnode->remove_meta("shader_type");
  381. if (custom_editor) {
  382. if (vsnode->is_show_prop_names()) {
  383. custom_editor->call_deferred("_show_prop_names", true);
  384. }
  385. break;
  386. }
  387. }
  388. Ref<VisualShaderNodeCurveTexture> curve = vsnode;
  389. if (curve.is_valid()) {
  390. if (curve->get_texture().is_valid() && !curve->get_texture()->is_connected("changed", callable_mp(VisualShaderEditor::get_singleton()->get_graph_plugin(), &VisualShaderGraphPlugin::update_curve))) {
  391. curve->get_texture()->connect("changed", callable_mp(VisualShaderEditor::get_singleton()->get_graph_plugin(), &VisualShaderGraphPlugin::update_curve), varray(p_id));
  392. }
  393. HBoxContainer *hbox = memnew(HBoxContainer);
  394. custom_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  395. hbox->add_child(custom_editor);
  396. custom_editor = hbox;
  397. }
  398. Ref<VisualShaderNodeFloatConstant> float_const = vsnode;
  399. if (float_const.is_valid()) {
  400. HBoxContainer *hbox = memnew(HBoxContainer);
  401. hbox->add_child(custom_editor);
  402. OptionButton *btn = memnew(OptionButton);
  403. hbox->add_child(btn);
  404. register_constant_option_btn(p_id, btn);
  405. btn->add_item("");
  406. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  407. btn->add_item(float_constant_defs[i].name);
  408. }
  409. btn->select(get_constant_index(float_const->get_constant()));
  410. btn->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_float_constant_selected), varray(p_id));
  411. custom_editor = hbox;
  412. }
  413. 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) == "")) {
  414. //will be embedded in first port
  415. } else if (custom_editor) {
  416. port_offset++;
  417. node->add_child(custom_editor);
  418. if (curve.is_valid()) {
  419. VisualShaderEditor::get_singleton()->graph->add_child(node);
  420. VisualShaderEditor::get_singleton()->_update_created_node(node);
  421. CurveEditor *curve_editor = memnew(CurveEditor);
  422. node->add_child(curve_editor);
  423. register_curve_editor(p_id, curve_editor);
  424. curve_editor->set_custom_minimum_size(Size2(300, 0));
  425. curve_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  426. if (curve->get_texture().is_valid()) {
  427. curve_editor->set_curve(curve->get_texture()->get_curve());
  428. }
  429. TextureButton *preview = memnew(TextureButton);
  430. preview->set_toggle_mode(true);
  431. preview->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityHidden", "EditorIcons"));
  432. preview->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityVisible", "EditorIcons"));
  433. preview->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  434. register_output_port(p_id, 0, preview);
  435. preview->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_preview_select_port), varray(p_id, 0), CONNECT_DEFERRED);
  436. custom_editor->add_child(preview);
  437. VisualShaderNode::PortType port_left = vsnode->get_input_port_type(0);
  438. VisualShaderNode::PortType port_right = vsnode->get_output_port_type(0);
  439. node->set_slot(0, true, port_left, type_color[port_left], true, port_right, type_color[port_right]);
  440. VisualShaderEditor::get_singleton()->call_deferred("_set_node_size", (int)p_type, p_id, size);
  441. }
  442. if (vsnode->is_use_prop_slots()) {
  443. return;
  444. }
  445. custom_editor = nullptr;
  446. }
  447. if (is_group) {
  448. offset = memnew(Control);
  449. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  450. node->add_child(offset);
  451. if (group_node->is_editable()) {
  452. HBoxContainer *hb2 = memnew(HBoxContainer);
  453. String input_port_name = "input" + itos(group_node->get_free_input_port_id());
  454. String output_port_name = "output" + itos(group_node->get_free_output_port_id());
  455. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  456. if (i < vsnode->get_input_port_count()) {
  457. if (input_port_name == vsnode->get_input_port_name(i)) {
  458. input_port_name = "_" + input_port_name;
  459. }
  460. }
  461. if (i < vsnode->get_output_port_count()) {
  462. if (output_port_name == vsnode->get_output_port_name(i)) {
  463. output_port_name = "_" + output_port_name;
  464. }
  465. }
  466. }
  467. Button *add_input_btn = memnew(Button);
  468. add_input_btn->set_text(TTR("Add Input"));
  469. 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_port_name), CONNECT_DEFERRED);
  470. hb2->add_child(add_input_btn);
  471. hb2->add_spacer();
  472. Button *add_output_btn = memnew(Button);
  473. add_output_btn->set_text(TTR("Add Output"));
  474. 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_port_name), CONNECT_DEFERRED);
  475. hb2->add_child(add_output_btn);
  476. node->add_child(hb2);
  477. }
  478. }
  479. int output_port_count = 0;
  480. for (int i = 0; i < vsnode->get_output_port_count(); i++) {
  481. if (vsnode->_is_output_port_expanded(i)) {
  482. if (vsnode->get_output_port_type(i) == VisualShaderNode::PORT_TYPE_VECTOR) {
  483. output_port_count += 3;
  484. }
  485. }
  486. output_port_count++;
  487. }
  488. int max_ports = MAX(vsnode->get_input_port_count(), output_port_count);
  489. VisualShaderNode::PortType expanded_type = VisualShaderNode::PORT_TYPE_SCALAR;
  490. int expanded_port_counter = 0;
  491. for (int i = 0, j = 0; i < max_ports; i++, j++) {
  492. if (expanded_type == VisualShaderNode::PORT_TYPE_VECTOR && expanded_port_counter >= 3) {
  493. expanded_type = VisualShaderNode::PORT_TYPE_SCALAR;
  494. expanded_port_counter = 0;
  495. i -= 3;
  496. }
  497. if (vsnode->is_port_separator(i)) {
  498. node->add_child(memnew(HSeparator));
  499. port_offset++;
  500. }
  501. bool valid_left = j < vsnode->get_input_port_count();
  502. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  503. bool port_left_used = false;
  504. String name_left;
  505. if (valid_left) {
  506. name_left = vsnode->get_input_port_name(i);
  507. port_left = vsnode->get_input_port_type(i);
  508. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  509. if (E->get().to_node == p_id && E->get().to_port == j) {
  510. port_left_used = true;
  511. }
  512. }
  513. }
  514. bool valid_right = true;
  515. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  516. String name_right;
  517. if (expanded_type == VisualShaderNode::PORT_TYPE_SCALAR) {
  518. valid_right = i < vsnode->get_output_port_count();
  519. if (valid_right) {
  520. name_right = vsnode->get_output_port_name(i);
  521. port_right = vsnode->get_output_port_type(i);
  522. }
  523. } else {
  524. name_right = vector_expanded_name[expanded_port_counter++];
  525. }
  526. HBoxContainer *hb = memnew(HBoxContainer);
  527. hb->add_theme_constant_override("separation", 7 * EDSCALE);
  528. Variant default_value;
  529. if (valid_left && !port_left_used) {
  530. default_value = vsnode->get_input_port_default_value(i);
  531. }
  532. Button *button = memnew(Button);
  533. hb->add_child(button);
  534. register_default_input_button(p_id, i, button);
  535. button->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_edit_port_default_input), varray(button, p_id, i));
  536. if (default_value.get_type() != Variant::NIL) { // only a label
  537. set_input_port_default_value(p_type, p_id, i, default_value);
  538. } else {
  539. button->hide();
  540. }
  541. if (i == 0 && custom_editor) {
  542. hb->add_child(custom_editor);
  543. custom_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  544. } else {
  545. if (valid_left) {
  546. if (is_group) {
  547. OptionButton *type_box = memnew(OptionButton);
  548. hb->add_child(type_box);
  549. type_box->add_item(TTR("Float"));
  550. type_box->add_item(TTR("Int"));
  551. type_box->add_item(TTR("Vector"));
  552. type_box->add_item(TTR("Boolean"));
  553. type_box->add_item(TTR("Transform"));
  554. type_box->add_item(TTR("Sampler"));
  555. type_box->select(group_node->get_input_port_type(i));
  556. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  557. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_type), varray(p_id, i), CONNECT_DEFERRED);
  558. LineEdit *name_box = memnew(LineEdit);
  559. hb->add_child(name_box);
  560. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  561. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  562. name_box->set_text(name_left);
  563. name_box->connect("text_submitted", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_input_port_name), varray(name_box, p_id, i), CONNECT_DEFERRED);
  564. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, false), CONNECT_DEFERRED);
  565. Button *remove_btn = memnew(Button);
  566. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  567. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  568. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_input_port), varray(p_id, i), CONNECT_DEFERRED);
  569. hb->add_child(remove_btn);
  570. } else {
  571. Label *label = memnew(Label);
  572. label->set_text(name_left);
  573. label->add_theme_style_override("normal", label_style); //more compact
  574. hb->add_child(label);
  575. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  576. Label *hint_label = memnew(Label);
  577. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  578. hint_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("font_readonly_color", "TextEdit"));
  579. hint_label->add_theme_style_override("normal", label_style);
  580. hb->add_child(hint_label);
  581. }
  582. }
  583. }
  584. if (!is_group) {
  585. hb->add_spacer();
  586. }
  587. if (valid_right) {
  588. if (is_group) {
  589. Button *remove_btn = memnew(Button);
  590. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Remove", "EditorIcons"));
  591. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  592. remove_btn->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_remove_output_port), varray(p_id, i), CONNECT_DEFERRED);
  593. hb->add_child(remove_btn);
  594. LineEdit *name_box = memnew(LineEdit);
  595. hb->add_child(name_box);
  596. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  597. name_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  598. name_box->set_text(name_right);
  599. name_box->connect("text_submitted", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_name), varray(name_box, p_id, i), CONNECT_DEFERRED);
  600. name_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_port_name_focus_out), varray(name_box, p_id, i, true), CONNECT_DEFERRED);
  601. OptionButton *type_box = memnew(OptionButton);
  602. hb->add_child(type_box);
  603. type_box->add_item(TTR("Float"));
  604. type_box->add_item(TTR("Int"));
  605. type_box->add_item(TTR("Vector"));
  606. type_box->add_item(TTR("Boolean"));
  607. type_box->add_item(TTR("Transform"));
  608. type_box->select(group_node->get_output_port_type(i));
  609. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  610. type_box->connect("item_selected", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_change_output_port_type), varray(p_id, i), CONNECT_DEFERRED);
  611. } else {
  612. Label *label = memnew(Label);
  613. label->set_text(name_right);
  614. label->add_theme_style_override("normal", label_style); //more compact
  615. hb->add_child(label);
  616. }
  617. }
  618. }
  619. if (valid_right) {
  620. if (vsnode->is_output_port_expandable(i)) {
  621. TextureButton *expand = memnew(TextureButton);
  622. expand->set_toggle_mode(true);
  623. expand->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiTreeArrowDown", "EditorIcons"));
  624. expand->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiTreeArrowRight", "EditorIcons"));
  625. expand->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  626. expand->set_pressed(vsnode->_is_output_port_expanded(i));
  627. expand->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_expand_output_port), varray(p_id, i, !vsnode->_is_output_port_expanded(i)), CONNECT_DEFERRED);
  628. hb->add_child(expand);
  629. }
  630. if (visual_shader->get_shader_type() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  631. TextureButton *preview = memnew(TextureButton);
  632. preview->set_toggle_mode(true);
  633. preview->set_normal_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityHidden", "EditorIcons"));
  634. preview->set_pressed_texture(VisualShaderEditor::get_singleton()->get_theme_icon("GuiVisibilityVisible", "EditorIcons"));
  635. preview->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  636. register_output_port(p_id, j, preview);
  637. preview->connect("pressed", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_preview_select_port), varray(p_id, j), CONNECT_DEFERRED);
  638. hb->add_child(preview);
  639. }
  640. }
  641. if (is_group) {
  642. offset = memnew(Control);
  643. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  644. node->add_child(offset);
  645. port_offset++;
  646. }
  647. node->add_child(hb);
  648. if (expanded_type != VisualShaderNode::PORT_TYPE_SCALAR) {
  649. continue;
  650. }
  651. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  652. if (vsnode->_is_output_port_expanded(i)) {
  653. if (vsnode->get_output_port_type(i) == VisualShaderNode::PORT_TYPE_VECTOR) {
  654. port_offset++;
  655. valid_left = (i + 1) < vsnode->get_input_port_count();
  656. port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  657. if (valid_left) {
  658. port_left = vsnode->get_input_port_type(i + 1);
  659. }
  660. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], true, VisualShaderNode::PORT_TYPE_SCALAR, vector_expanded_color[0]);
  661. port_offset++;
  662. valid_left = (i + 2) < vsnode->get_input_port_count();
  663. port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  664. if (valid_left) {
  665. port_left = vsnode->get_input_port_type(i + 2);
  666. }
  667. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], true, VisualShaderNode::PORT_TYPE_SCALAR, vector_expanded_color[1]);
  668. port_offset++;
  669. valid_left = (i + 3) < vsnode->get_input_port_count();
  670. port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  671. if (valid_left) {
  672. port_left = vsnode->get_input_port_type(i + 3);
  673. }
  674. node->set_slot(i + port_offset, valid_left, port_left, type_color[port_left], true, VisualShaderNode::PORT_TYPE_SCALAR, vector_expanded_color[2]);
  675. expanded_type = VisualShaderNode::PORT_TYPE_VECTOR;
  676. }
  677. }
  678. }
  679. if (vsnode->get_output_port_for_preview() >= 0) {
  680. show_port_preview(p_type, p_id, vsnode->get_output_port_for_preview());
  681. }
  682. offset = memnew(Control);
  683. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  684. node->add_child(offset);
  685. String error = vsnode->get_warning(visual_shader->get_mode(), p_type);
  686. if (error != String()) {
  687. Label *error_label = memnew(Label);
  688. error_label->add_theme_color_override("font_color", VisualShaderEditor::get_singleton()->get_theme_color("error_color", "Editor"));
  689. error_label->set_text(error);
  690. node->add_child(error_label);
  691. }
  692. if (is_expression) {
  693. CodeEdit *expression_box = memnew(CodeEdit);
  694. Ref<CodeHighlighter> expression_syntax_highlighter;
  695. expression_syntax_highlighter.instantiate();
  696. expression_node->set_ctrl_pressed(expression_box, 0);
  697. node->add_child(expression_box);
  698. register_expression_edit(p_id, expression_box);
  699. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  700. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  701. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  702. Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
  703. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  704. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  705. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  706. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  707. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  708. expression_box->set_syntax_highlighter(expression_syntax_highlighter);
  709. expression_box->add_theme_color_override("background_color", background_color);
  710. for (List<String>::Element *E = VisualShaderEditor::get_singleton()->keyword_list.front(); E; E = E->next()) {
  711. if (ShaderLanguage::is_control_flow_keyword(E->get())) {
  712. expression_syntax_highlighter->add_keyword_color(E->get(), control_flow_keyword_color);
  713. } else {
  714. expression_syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  715. }
  716. }
  717. expression_box->add_theme_font_override("font", VisualShaderEditor::get_singleton()->get_theme_font("expression", "EditorFonts"));
  718. expression_box->add_theme_font_size_override("font_size", VisualShaderEditor::get_singleton()->get_theme_font_size("expression_size", "EditorFonts"));
  719. expression_box->add_theme_color_override("font_color", text_color);
  720. expression_syntax_highlighter->set_number_color(number_color);
  721. expression_syntax_highlighter->set_symbol_color(symbol_color);
  722. expression_syntax_highlighter->set_function_color(function_color);
  723. expression_syntax_highlighter->set_member_variable_color(members_color);
  724. expression_syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  725. expression_syntax_highlighter->add_color_region("//", "", comment_color, true);
  726. expression_box->clear_comment_delimiters();
  727. expression_box->add_comment_delimiter("/*", "*/", false);
  728. expression_box->add_comment_delimiter("//", "", true);
  729. expression_box->set_text(expression);
  730. expression_box->set_context_menu_enabled(false);
  731. expression_box->set_draw_line_numbers(true);
  732. expression_box->connect("focus_exited", callable_mp(VisualShaderEditor::get_singleton(), &VisualShaderEditor::_expression_focus_out), varray(expression_box, p_id));
  733. }
  734. if (!uniform.is_valid()) {
  735. VisualShaderEditor::get_singleton()->graph->add_child(node);
  736. if (is_comment) {
  737. VisualShaderEditor::get_singleton()->graph->move_child(node, 0); // to prevents a bug where comment node overlaps its content
  738. }
  739. VisualShaderEditor::get_singleton()->_update_created_node(node);
  740. if (is_resizable) {
  741. VisualShaderEditor::get_singleton()->call_deferred("_set_node_size", (int)p_type, p_id, size);
  742. }
  743. }
  744. }
  745. void VisualShaderGraphPlugin::remove_node(VisualShader::Type p_type, int p_id) {
  746. if (visual_shader->get_shader_type() == p_type && links.has(p_id)) {
  747. links[p_id].graph_node->get_parent()->remove_child(links[p_id].graph_node);
  748. memdelete(links[p_id].graph_node);
  749. links.erase(p_id);
  750. }
  751. }
  752. void VisualShaderGraphPlugin::connect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  753. if (visual_shader->get_shader_type() == p_type) {
  754. VisualShaderEditor::get_singleton()->graph->connect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  755. connections.push_back({ p_from_node, p_from_port, p_to_node, p_to_port });
  756. if (links[p_to_node].input_ports.has(p_to_port) && links[p_to_node].input_ports[p_to_port].default_input_button != nullptr) {
  757. links[p_to_node].input_ports[p_to_port].default_input_button->hide();
  758. }
  759. }
  760. }
  761. void VisualShaderGraphPlugin::disconnect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  762. if (visual_shader->get_shader_type() == p_type) {
  763. VisualShaderEditor::get_singleton()->graph->disconnect_node(itos(p_from_node), p_from_port, itos(p_to_node), p_to_port);
  764. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  765. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  766. connections.erase(E);
  767. break;
  768. }
  769. }
  770. 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) {
  771. links[p_to_node].input_ports[p_to_port].default_input_button->show();
  772. 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));
  773. }
  774. }
  775. }
  776. VisualShaderGraphPlugin::~VisualShaderGraphPlugin() {
  777. }
  778. /////////////////
  779. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  780. bool changed = false;
  781. if (p_visual_shader) {
  782. if (visual_shader.is_null()) {
  783. changed = true;
  784. } else {
  785. if (visual_shader.ptr() != p_visual_shader) {
  786. changed = true;
  787. }
  788. }
  789. visual_shader = Ref<VisualShader>(p_visual_shader);
  790. graph_plugin->register_shader(visual_shader.ptr());
  791. if (!visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  792. visual_shader->connect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  793. }
  794. #ifndef DISABLE_DEPRECATED
  795. String version = VERSION_BRANCH;
  796. if (visual_shader->get_version() != version) {
  797. visual_shader->update_version(version);
  798. }
  799. #endif
  800. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  801. _set_mode(visual_shader->get_mode());
  802. } else {
  803. if (visual_shader.is_valid()) {
  804. if (visual_shader->is_connected("changed", callable_mp(this, &VisualShaderEditor::_update_preview))) {
  805. visual_shader->disconnect("changed", callable_mp(this, &VisualShaderEditor::_update_preview));
  806. }
  807. }
  808. visual_shader.unref();
  809. }
  810. if (visual_shader.is_null()) {
  811. hide();
  812. } else {
  813. if (changed) { // to avoid tree collapse
  814. _clear_buffer();
  815. _update_options_menu();
  816. _update_preview();
  817. _update_graph();
  818. }
  819. }
  820. }
  821. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  822. if (plugins.find(p_plugin) != -1) {
  823. return;
  824. }
  825. plugins.push_back(p_plugin);
  826. }
  827. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  828. plugins.erase(p_plugin);
  829. }
  830. void VisualShaderEditor::clear_custom_types() {
  831. for (int i = 0; i < add_options.size(); i++) {
  832. if (add_options[i].is_custom) {
  833. add_options.remove(i);
  834. i--;
  835. }
  836. }
  837. }
  838. 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) {
  839. ERR_FAIL_COND(!p_name.is_valid_identifier());
  840. ERR_FAIL_COND(!p_script.is_valid());
  841. for (int i = 0; i < add_options.size(); i++) {
  842. if (add_options[i].is_custom) {
  843. if (add_options[i].script == p_script) {
  844. return;
  845. }
  846. }
  847. }
  848. AddOption ao;
  849. ao.name = p_name;
  850. ao.script = p_script;
  851. ao.return_type = p_return_icon_type;
  852. ao.description = p_description;
  853. ao.category = p_category;
  854. ao.highend = p_highend;
  855. ao.is_custom = true;
  856. bool begin = false;
  857. String root = p_category.split("/")[0];
  858. for (int i = 0; i < add_options.size(); i++) {
  859. if (add_options[i].is_custom) {
  860. if (add_options[i].category == root) {
  861. if (!begin) {
  862. begin = true;
  863. }
  864. } else {
  865. if (begin) {
  866. add_options.insert(i, ao);
  867. return;
  868. }
  869. }
  870. }
  871. }
  872. add_options.push_back(ao);
  873. }
  874. bool VisualShaderEditor::_is_available(int p_mode) {
  875. int current_mode = edit_type->get_selected();
  876. if (p_mode != -1) {
  877. switch (current_mode) {
  878. case 0: // Vertex / Emit
  879. current_mode = 1;
  880. break;
  881. case 1: // Fragment / Process
  882. current_mode = 2;
  883. break;
  884. case 2: // Light / Collide
  885. current_mode = 4;
  886. break;
  887. default:
  888. break;
  889. }
  890. }
  891. return (p_mode == -1 || (p_mode & current_mode) != 0);
  892. }
  893. void VisualShaderEditor::update_custom_nodes() {
  894. if (members_dialog->is_visible()) {
  895. return;
  896. }
  897. clear_custom_types();
  898. List<StringName> class_list;
  899. ScriptServer::get_global_class_list(&class_list);
  900. Dictionary added;
  901. for (int i = 0; i < class_list.size(); i++) {
  902. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  903. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  904. Ref<Resource> res = ResourceLoader::load(script_path);
  905. ERR_FAIL_COND(res.is_null());
  906. ERR_FAIL_COND(!res->is_class("Script"));
  907. Ref<Script> script = Ref<Script>(res);
  908. Ref<VisualShaderNodeCustom> ref;
  909. ref.instantiate();
  910. ref->set_script(script);
  911. String name;
  912. if (ref->has_method("_get_name")) {
  913. name = (String)ref->call("_get_name");
  914. } else {
  915. name = "Unnamed";
  916. }
  917. String description = "";
  918. if (ref->has_method("_get_description")) {
  919. description = (String)ref->call("_get_description");
  920. }
  921. int return_icon_type = -1;
  922. if (ref->has_method("_get_return_icon_type")) {
  923. return_icon_type = (int)ref->call("_get_return_icon_type");
  924. }
  925. String category = "";
  926. if (ref->has_method("_get_category")) {
  927. category = (String)ref->call("_get_category");
  928. }
  929. String subcategory = "";
  930. if (ref->has_method("_get_subcategory")) {
  931. subcategory = (String)ref->call("_get_subcategory");
  932. }
  933. bool highend = false;
  934. if (ref->has_method("_is_highend")) {
  935. highend = (bool)ref->call("_is_highend");
  936. }
  937. Dictionary dict;
  938. dict["name"] = name;
  939. dict["script"] = script;
  940. dict["description"] = description;
  941. dict["return_icon_type"] = return_icon_type;
  942. category = category.rstrip("/");
  943. category = category.lstrip("/");
  944. category = "Addons/" + category;
  945. if (subcategory != "") {
  946. category += "/" + subcategory;
  947. }
  948. dict["category"] = category;
  949. dict["highend"] = highend;
  950. String key;
  951. key = category + "/" + name;
  952. added[key] = dict;
  953. }
  954. }
  955. Array keys = added.keys();
  956. keys.sort();
  957. for (int i = 0; i < keys.size(); i++) {
  958. const Variant &key = keys.get(i);
  959. const Dictionary &value = (Dictionary)added[key];
  960. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["highend"]);
  961. }
  962. _update_options_menu();
  963. }
  964. String VisualShaderEditor::_get_description(int p_idx) {
  965. return add_options[p_idx].description;
  966. }
  967. void VisualShaderEditor::_update_options_menu() {
  968. node_desc->set_text("");
  969. members_dialog->get_ok_button()->set_disabled(true);
  970. members->clear();
  971. TreeItem *root = members->create_item();
  972. String filter = node_filter->get_text().strip_edges();
  973. bool use_filter = !filter.is_empty();
  974. bool is_first_item = true;
  975. Color unsupported_color = get_theme_color("error_color", "Editor");
  976. Color supported_color = get_theme_color("warning_color", "Editor");
  977. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/driver/driver_name") == "GLES2";
  978. Map<String, TreeItem *> folders;
  979. int current_func = -1;
  980. if (!visual_shader.is_null()) {
  981. current_func = visual_shader->get_mode();
  982. }
  983. Vector<AddOption> custom_options;
  984. Vector<AddOption> embedded_options;
  985. for (int i = 0; i < add_options.size(); i++) {
  986. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  987. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode)) {
  988. continue;
  989. }
  990. const_cast<AddOption &>(add_options[i]).temp_idx = i; // save valid id
  991. if (add_options[i].is_custom) {
  992. custom_options.push_back(add_options[i]);
  993. } else {
  994. embedded_options.push_back(add_options[i]);
  995. }
  996. }
  997. }
  998. Vector<AddOption> options;
  999. SortArray<AddOption, _OptionComparator> sorter;
  1000. sorter.sort(custom_options.ptrw(), custom_options.size());
  1001. options.append_array(custom_options);
  1002. options.append_array(embedded_options);
  1003. for (int i = 0; i < options.size(); i++) {
  1004. String path = options[i].category;
  1005. Vector<String> subfolders = path.split("/");
  1006. TreeItem *category = nullptr;
  1007. if (!folders.has(path)) {
  1008. category = root;
  1009. String path_temp = "";
  1010. for (int j = 0; j < subfolders.size(); j++) {
  1011. path_temp += subfolders[j];
  1012. if (!folders.has(path_temp)) {
  1013. category = members->create_item(category);
  1014. category->set_selectable(0, false);
  1015. category->set_collapsed(!use_filter);
  1016. category->set_text(0, subfolders[j]);
  1017. folders.insert(path_temp, category);
  1018. } else {
  1019. category = folders[path_temp];
  1020. }
  1021. }
  1022. } else {
  1023. category = folders[path];
  1024. }
  1025. TreeItem *item = members->create_item(category);
  1026. if (options[i].highend && low_driver) {
  1027. item->set_custom_color(0, unsupported_color);
  1028. } else if (options[i].highend) {
  1029. item->set_custom_color(0, supported_color);
  1030. }
  1031. item->set_text(0, options[i].name);
  1032. if (is_first_item && use_filter) {
  1033. item->select(0);
  1034. node_desc->set_text(options[i].description);
  1035. is_first_item = false;
  1036. }
  1037. switch (options[i].return_type) {
  1038. case VisualShaderNode::PORT_TYPE_SCALAR:
  1039. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"));
  1040. break;
  1041. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  1042. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"));
  1043. break;
  1044. case VisualShaderNode::PORT_TYPE_VECTOR:
  1045. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"));
  1046. break;
  1047. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1048. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"));
  1049. break;
  1050. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1051. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform3D", "EditorIcons"));
  1052. break;
  1053. case VisualShaderNode::PORT_TYPE_SAMPLER:
  1054. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"));
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. item->set_meta("id", options[i].temp_idx);
  1060. }
  1061. }
  1062. void VisualShaderEditor::_set_mode(int p_which) {
  1063. if (p_which == VisualShader::MODE_SKY) {
  1064. edit_type_standard->set_visible(false);
  1065. edit_type_particles->set_visible(false);
  1066. edit_type_sky->set_visible(true);
  1067. edit_type = edit_type_sky;
  1068. custom_mode_box->set_visible(false);
  1069. mode = MODE_FLAGS_SKY;
  1070. } else if (p_which == VisualShader::MODE_PARTICLES) {
  1071. edit_type_standard->set_visible(false);
  1072. edit_type_particles->set_visible(true);
  1073. edit_type_sky->set_visible(false);
  1074. edit_type = edit_type_particles;
  1075. if ((edit_type->get_selected() + 3) > VisualShader::TYPE_PROCESS) {
  1076. custom_mode_box->set_visible(false);
  1077. } else {
  1078. custom_mode_box->set_visible(true);
  1079. }
  1080. mode = MODE_FLAGS_PARTICLES;
  1081. } else {
  1082. edit_type_particles->set_visible(false);
  1083. edit_type_standard->set_visible(true);
  1084. edit_type_sky->set_visible(false);
  1085. edit_type = edit_type_standard;
  1086. custom_mode_box->set_visible(false);
  1087. mode = MODE_FLAGS_SPATIAL_CANVASITEM;
  1088. }
  1089. visual_shader->set_shader_type(get_current_shader_type());
  1090. }
  1091. Size2 VisualShaderEditor::get_minimum_size() const {
  1092. return Size2(10, 200);
  1093. }
  1094. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  1095. Button *button = Object::cast_to<Button>(obj);
  1096. if (!button) {
  1097. return;
  1098. }
  1099. Ref<StyleBox> normal = get_theme_stylebox("normal", "Button");
  1100. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  1101. }
  1102. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  1103. const Ref<StyleBoxFlat> sb = node->get_theme_stylebox("frame", "GraphNode");
  1104. Color c = sb->get_border_color();
  1105. const Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0, 0.85) : Color(0.0, 0.0, 0.0, 0.85);
  1106. c = mono_color;
  1107. node->add_theme_color_override("title_color", c);
  1108. c.a = 0.7;
  1109. node->add_theme_color_override("close_color", c);
  1110. node->add_theme_color_override("resizer_color", c);
  1111. }
  1112. void VisualShaderEditor::_update_uniforms(bool p_update_refs) {
  1113. VisualShaderNodeUniformRef::clear_uniforms();
  1114. for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
  1115. Vector<int> tnodes = visual_shader->get_node_list((VisualShader::Type)t);
  1116. for (int i = 0; i < tnodes.size(); i++) {
  1117. Ref<VisualShaderNode> vsnode = visual_shader->get_node((VisualShader::Type)t, tnodes[i]);
  1118. Ref<VisualShaderNodeUniform> uniform = vsnode;
  1119. if (uniform.is_valid()) {
  1120. Ref<VisualShaderNodeFloatUniform> float_uniform = vsnode;
  1121. Ref<VisualShaderNodeIntUniform> int_uniform = vsnode;
  1122. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = vsnode;
  1123. Ref<VisualShaderNodeColorUniform> color_uniform = vsnode;
  1124. Ref<VisualShaderNodeBooleanUniform> bool_uniform = vsnode;
  1125. Ref<VisualShaderNodeTransformUniform> transform_uniform = vsnode;
  1126. VisualShaderNodeUniformRef::UniformType uniform_type;
  1127. if (float_uniform.is_valid()) {
  1128. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_FLOAT;
  1129. } else if (int_uniform.is_valid()) {
  1130. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_INT;
  1131. } else if (bool_uniform.is_valid()) {
  1132. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_BOOLEAN;
  1133. } else if (vec3_uniform.is_valid()) {
  1134. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_VECTOR;
  1135. } else if (transform_uniform.is_valid()) {
  1136. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_TRANSFORM;
  1137. } else if (color_uniform.is_valid()) {
  1138. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_COLOR;
  1139. } else {
  1140. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SAMPLER;
  1141. }
  1142. VisualShaderNodeUniformRef::add_uniform(uniform->get_uniform_name(), uniform_type);
  1143. }
  1144. }
  1145. }
  1146. if (p_update_refs) {
  1147. graph_plugin->update_uniform_refs();
  1148. }
  1149. }
  1150. void VisualShaderEditor::_update_uniform_refs(Set<String> &p_deleted_names) {
  1151. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  1152. VisualShader::Type type = VisualShader::Type(i);
  1153. Vector<int> nodes = visual_shader->get_node_list(type);
  1154. for (int j = 0; j < nodes.size(); j++) {
  1155. if (j > 0) {
  1156. Ref<VisualShaderNodeUniformRef> ref = visual_shader->get_node(type, nodes[j]);
  1157. if (ref.is_valid()) {
  1158. if (p_deleted_names.has(ref->get_uniform_name())) {
  1159. undo_redo->add_do_method(ref.ptr(), "set_uniform_name", "[None]");
  1160. undo_redo->add_undo_method(ref.ptr(), "set_uniform_name", ref->get_uniform_name());
  1161. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  1162. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", VisualShader::Type(i), nodes[j]);
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. void VisualShaderEditor::_update_graph() {
  1170. if (updating) {
  1171. return;
  1172. }
  1173. if (visual_shader.is_null()) {
  1174. return;
  1175. }
  1176. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  1177. VisualShader::Type type = get_current_shader_type();
  1178. graph->clear_connections();
  1179. //erase all nodes
  1180. for (int i = 0; i < graph->get_child_count(); i++) {
  1181. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  1182. Node *node = graph->get_child(i);
  1183. graph->remove_child(node);
  1184. memdelete(node);
  1185. i--;
  1186. }
  1187. }
  1188. List<VisualShader::Connection> connections;
  1189. visual_shader->get_node_connections(type, &connections);
  1190. graph_plugin->set_connections(connections);
  1191. Vector<int> nodes = visual_shader->get_node_list(type);
  1192. _update_uniforms(false);
  1193. graph_plugin->clear_links();
  1194. graph_plugin->make_dirty(true);
  1195. graph_plugin->update_theme();
  1196. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  1197. graph_plugin->add_node(type, nodes[n_i]);
  1198. }
  1199. graph_plugin->make_dirty(false);
  1200. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  1201. int from = E->get().from_node;
  1202. int from_idx = E->get().from_port;
  1203. int to = E->get().to_node;
  1204. int to_idx = E->get().to_port;
  1205. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  1206. }
  1207. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  1208. graph->set_minimap_opacity(graph_minimap_opacity);
  1209. }
  1210. VisualShader::Type VisualShaderEditor::get_current_shader_type() const {
  1211. VisualShader::Type type;
  1212. if (mode & MODE_FLAGS_PARTICLES) {
  1213. type = VisualShader::Type(edit_type->get_selected() + 3 + (custom_mode_enabled ? 3 : 0));
  1214. } else if (mode & MODE_FLAGS_SKY) {
  1215. type = VisualShader::Type(edit_type->get_selected() + 8);
  1216. } else {
  1217. type = VisualShader::Type(edit_type->get_selected());
  1218. }
  1219. return type;
  1220. }
  1221. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  1222. VisualShader::Type type = get_current_shader_type();
  1223. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  1224. if (node.is_null()) {
  1225. return;
  1226. }
  1227. undo_redo->create_action(TTR("Add Input Port"));
  1228. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  1229. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  1230. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1231. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1232. undo_redo->commit_action();
  1233. }
  1234. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  1235. VisualShader::Type type = get_current_shader_type();
  1236. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1237. if (node.is_null()) {
  1238. return;
  1239. }
  1240. undo_redo->create_action(TTR("Add Output Port"));
  1241. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  1242. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  1243. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1244. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1245. undo_redo->commit_action();
  1246. }
  1247. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  1248. VisualShader::Type type = get_current_shader_type();
  1249. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1250. if (node.is_null()) {
  1251. return;
  1252. }
  1253. undo_redo->create_action(TTR("Change Input Port Type"));
  1254. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  1255. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  1256. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1257. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1258. undo_redo->commit_action();
  1259. }
  1260. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  1261. VisualShader::Type type = get_current_shader_type();
  1262. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1263. if (node.is_null()) {
  1264. return;
  1265. }
  1266. undo_redo->create_action(TTR("Change Output Port Type"));
  1267. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  1268. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  1269. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1270. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1271. undo_redo->commit_action();
  1272. }
  1273. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *p_line_edit, int p_node_id, int p_port_id) {
  1274. VisualShader::Type type = get_current_shader_type();
  1275. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1276. ERR_FAIL_COND(!node.is_valid());
  1277. String prev_name = node->get_input_port_name(p_port_id);
  1278. if (prev_name == p_text) {
  1279. return;
  1280. }
  1281. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  1282. ERR_FAIL_COND(!line_edit);
  1283. String validated_name = visual_shader->validate_port_name(p_text, node.ptr(), p_port_id, false);
  1284. if (validated_name == String() || prev_name == validated_name) {
  1285. line_edit->set_text(node->get_input_port_name(p_port_id));
  1286. return;
  1287. }
  1288. undo_redo->create_action(TTR("Change Input Port Name"));
  1289. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, validated_name);
  1290. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  1291. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1292. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1293. undo_redo->commit_action();
  1294. }
  1295. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *p_line_edit, int p_node_id, int p_port_id) {
  1296. VisualShader::Type type = get_current_shader_type();
  1297. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1298. ERR_FAIL_COND(!node.is_valid());
  1299. String prev_name = node->get_output_port_name(p_port_id);
  1300. if (prev_name == p_text) {
  1301. return;
  1302. }
  1303. LineEdit *line_edit = Object::cast_to<LineEdit>(p_line_edit);
  1304. ERR_FAIL_COND(!line_edit);
  1305. String validated_name = visual_shader->validate_port_name(p_text, node.ptr(), p_port_id, true);
  1306. if (validated_name == String() || prev_name == validated_name) {
  1307. line_edit->set_text(node->get_output_port_name(p_port_id));
  1308. return;
  1309. }
  1310. undo_redo->create_action(TTR("Change Output Port Name"));
  1311. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, validated_name);
  1312. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, prev_name);
  1313. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1314. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node_id);
  1315. undo_redo->commit_action();
  1316. }
  1317. void VisualShaderEditor::_expand_output_port(int p_node, int p_port, bool p_expand) {
  1318. VisualShader::Type type = get_current_shader_type();
  1319. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1320. ERR_FAIL_COND(!node.is_valid());
  1321. if (p_expand) {
  1322. undo_redo->create_action(TTR("Expand Output Port"));
  1323. } else {
  1324. undo_redo->create_action(TTR("Shrink Output Port"));
  1325. }
  1326. undo_redo->add_do_method(node.ptr(), "_set_output_port_expanded", p_port, p_expand);
  1327. undo_redo->add_undo_method(node.ptr(), "_set_output_port_expanded", p_port, !p_expand);
  1328. int type_size = 0;
  1329. if (node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_VECTOR) {
  1330. type_size = 3;
  1331. }
  1332. List<VisualShader::Connection> conns;
  1333. visual_shader->get_node_connections(type, &conns);
  1334. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1335. int from_node = E->get().from_node;
  1336. int from_port = E->get().from_port;
  1337. int to_node = E->get().to_node;
  1338. int to_port = E->get().to_port;
  1339. if (from_node == p_node) {
  1340. if (p_expand) {
  1341. if (from_port > p_port) { // reconnect ports after expanded ports
  1342. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1343. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1344. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1345. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1346. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port + type_size, to_node, to_port);
  1347. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port + type_size, to_node, to_port);
  1348. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port + type_size, to_node, to_port);
  1349. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port + type_size, to_node, to_port);
  1350. }
  1351. } else {
  1352. if (from_port > p_port + type_size) { // reconnect ports after expanded ports
  1353. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1354. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1355. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1356. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1357. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_port - type_size, to_node, to_port);
  1358. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - type_size, to_node, to_port);
  1359. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port - type_size, to_node, to_port);
  1360. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port - type_size, to_node, to_port);
  1361. } else if (from_port > p_port) { // disconnect component ports
  1362. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1363. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1364. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1365. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1366. }
  1367. }
  1368. }
  1369. }
  1370. int preview_port = node->get_output_port_for_preview();
  1371. if (p_expand) {
  1372. if (preview_port > p_port) {
  1373. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", preview_port + type_size);
  1374. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", preview_port);
  1375. }
  1376. } else {
  1377. if (preview_port > p_port + type_size) {
  1378. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", preview_port - type_size);
  1379. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", preview_port);
  1380. }
  1381. }
  1382. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1383. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1384. undo_redo->commit_action();
  1385. }
  1386. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  1387. VisualShader::Type type = get_current_shader_type();
  1388. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1389. if (node.is_null()) {
  1390. return;
  1391. }
  1392. undo_redo->create_action(TTR("Remove Input Port"));
  1393. List<VisualShader::Connection> conns;
  1394. visual_shader->get_node_connections(type, &conns);
  1395. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1396. int from_node = E->get().from_node;
  1397. int from_port = E->get().from_port;
  1398. int to_node = E->get().to_node;
  1399. int to_port = E->get().to_port;
  1400. if (to_node == p_node) {
  1401. if (to_port == p_port) {
  1402. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1403. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1404. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1405. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1406. } else if (to_port > p_port) {
  1407. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1408. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1409. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1410. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1411. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  1412. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1413. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1414. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  1415. }
  1416. }
  1417. }
  1418. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  1419. 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));
  1420. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1421. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1422. undo_redo->commit_action();
  1423. }
  1424. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  1425. VisualShader::Type type = get_current_shader_type();
  1426. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1427. if (node.is_null()) {
  1428. return;
  1429. }
  1430. undo_redo->create_action(TTR("Remove Output Port"));
  1431. List<VisualShader::Connection> conns;
  1432. visual_shader->get_node_connections(type, &conns);
  1433. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1434. int from_node = E->get().from_node;
  1435. int from_port = E->get().from_port;
  1436. int to_node = E->get().to_node;
  1437. int to_port = E->get().to_port;
  1438. if (from_node == p_node) {
  1439. if (from_port == p_port) {
  1440. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1441. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1442. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1443. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1444. } else if (from_port > p_port) {
  1445. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1446. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  1447. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  1448. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port, to_node, to_port);
  1449. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  1450. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1451. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1452. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  1453. }
  1454. }
  1455. }
  1456. int preview_port = node->get_output_port_for_preview();
  1457. if (preview_port != -1) {
  1458. if (preview_port == p_port) {
  1459. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", -1);
  1460. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", preview_port);
  1461. } else if (preview_port > p_port) {
  1462. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", preview_port - 1);
  1463. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", preview_port);
  1464. }
  1465. }
  1466. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  1467. 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));
  1468. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, p_node);
  1469. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type, p_node);
  1470. undo_redo->commit_action();
  1471. }
  1472. void VisualShaderEditor::_expression_focus_out(Object *code_edit, int p_node) {
  1473. VisualShader::Type type = get_current_shader_type();
  1474. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  1475. if (node.is_null()) {
  1476. return;
  1477. }
  1478. CodeEdit *expression_box = Object::cast_to<CodeEdit>(code_edit);
  1479. if (node->get_expression() == expression_box->get_text()) {
  1480. return;
  1481. }
  1482. undo_redo->create_action(TTR("Set VisualShader Expression"));
  1483. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  1484. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  1485. undo_redo->add_do_method(graph_plugin.ptr(), "set_expression", type, p_node, expression_box->get_text());
  1486. undo_redo->add_undo_method(graph_plugin.ptr(), "set_expression", type, p_node, node->get_expression());
  1487. undo_redo->commit_action();
  1488. }
  1489. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  1490. VisualShader::Type type = VisualShader::Type(p_type);
  1491. Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(type, p_node);
  1492. if (node.is_null()) {
  1493. return;
  1494. }
  1495. Size2 size = p_size;
  1496. if (!node->is_allow_v_resize()) {
  1497. size.y = 0;
  1498. }
  1499. node->set_size(size);
  1500. if (get_current_shader_type() == type) {
  1501. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1502. Control *text_box = nullptr;
  1503. if (!expression_node.is_null()) {
  1504. text_box = expression_node->is_ctrl_pressed(0);
  1505. if (text_box) {
  1506. text_box->set_custom_minimum_size(Size2(0, 0));
  1507. }
  1508. }
  1509. GraphNode *gn = nullptr;
  1510. Node *node2 = graph->get_node(itos(p_node));
  1511. gn = Object::cast_to<GraphNode>(node2);
  1512. if (!gn) {
  1513. return;
  1514. }
  1515. gn->set_custom_minimum_size(size);
  1516. gn->set_size(Size2(1, 1));
  1517. if (!expression_node.is_null() && text_box) {
  1518. Size2 box_size = size;
  1519. if (gn != nullptr) {
  1520. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  1521. box_size.x = gn->get_size().x;
  1522. }
  1523. }
  1524. box_size.x -= text_box->get_offset(SIDE_LEFT);
  1525. box_size.x -= 28 * EDSCALE;
  1526. box_size.y -= text_box->get_offset(SIDE_TOP);
  1527. box_size.y -= 28 * EDSCALE;
  1528. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  1529. text_box->set_size(Size2(1, 1));
  1530. }
  1531. }
  1532. }
  1533. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  1534. Ref<VisualShaderNodeResizableBase> node = visual_shader->get_node(VisualShader::Type(p_type), p_node);
  1535. if (node.is_null()) {
  1536. return;
  1537. }
  1538. undo_redo->create_action(TTR("Resize VisualShader Node"), UndoRedo::MERGE_ENDS);
  1539. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  1540. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  1541. undo_redo->commit_action();
  1542. }
  1543. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  1544. VisualShader::Type type = get_current_shader_type();
  1545. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1546. if (node.is_null()) {
  1547. return;
  1548. }
  1549. int prev_port = node->get_output_port_for_preview();
  1550. if (node->get_output_port_for_preview() == p_port) {
  1551. p_port = -1; //toggle it
  1552. }
  1553. undo_redo->create_action(p_port == -1 ? TTR("Hide Port Preview") : TTR("Show Port Preview"));
  1554. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  1555. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", prev_port);
  1556. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, p_node);
  1557. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, p_node);
  1558. undo_redo->commit_action();
  1559. }
  1560. void VisualShaderEditor::_comment_title_popup_show(const Point2 &p_position, int p_node_id) {
  1561. VisualShader::Type type = get_current_shader_type();
  1562. Ref<VisualShaderNodeComment> node = visual_shader->get_node(type, p_node_id);
  1563. if (node.is_null()) {
  1564. return;
  1565. }
  1566. comment_title_change_edit->set_text(node->get_title());
  1567. comment_title_change_popup->set_meta("id", p_node_id);
  1568. comment_title_change_popup->popup();
  1569. comment_title_change_popup->set_position(p_position);
  1570. }
  1571. void VisualShaderEditor::_comment_title_text_changed(const String &p_new_text) {
  1572. comment_title_change_edit->set_size(Size2(-1, -1));
  1573. comment_title_change_popup->set_size(Size2(-1, -1));
  1574. }
  1575. void VisualShaderEditor::_comment_title_text_submitted(const String &p_new_text) {
  1576. comment_title_change_popup->hide();
  1577. }
  1578. void VisualShaderEditor::_comment_title_popup_focus_out() {
  1579. comment_title_change_popup->hide();
  1580. }
  1581. void VisualShaderEditor::_comment_title_popup_hide() {
  1582. ERR_FAIL_COND(!comment_title_change_popup->has_meta("id"));
  1583. int node_id = (int)comment_title_change_popup->get_meta("id");
  1584. VisualShader::Type type = get_current_shader_type();
  1585. Ref<VisualShaderNodeComment> node = visual_shader->get_node(type, node_id);
  1586. ERR_FAIL_COND(node.is_null());
  1587. if (node->get_title() == comment_title_change_edit->get_text()) {
  1588. return; // nothing changed - ignored
  1589. }
  1590. undo_redo->create_action(TTR("Set Comment Node Title"));
  1591. undo_redo->add_do_method(node.ptr(), "set_title", comment_title_change_edit->get_text());
  1592. undo_redo->add_undo_method(node.ptr(), "set_title", node->get_title());
  1593. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, node_id);
  1594. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, node_id);
  1595. undo_redo->commit_action();
  1596. }
  1597. void VisualShaderEditor::_comment_desc_popup_show(const Point2 &p_position, int p_node_id) {
  1598. VisualShader::Type type = get_current_shader_type();
  1599. Ref<VisualShaderNodeComment> node = visual_shader->get_node(type, p_node_id);
  1600. if (node.is_null()) {
  1601. return;
  1602. }
  1603. comment_desc_change_edit->set_text(node->get_description());
  1604. comment_desc_change_popup->set_meta("id", p_node_id);
  1605. comment_desc_change_popup->popup();
  1606. comment_desc_change_popup->set_position(p_position);
  1607. }
  1608. void VisualShaderEditor::_comment_desc_text_changed() {
  1609. comment_desc_change_edit->set_size(Size2(-1, -1));
  1610. comment_desc_change_popup->set_size(Size2(-1, -1));
  1611. }
  1612. void VisualShaderEditor::_comment_desc_confirm() {
  1613. comment_desc_change_popup->hide();
  1614. }
  1615. void VisualShaderEditor::_comment_desc_popup_hide() {
  1616. ERR_FAIL_COND(!comment_desc_change_popup->has_meta("id"));
  1617. int node_id = (int)comment_desc_change_popup->get_meta("id");
  1618. VisualShader::Type type = get_current_shader_type();
  1619. Ref<VisualShaderNodeComment> node = visual_shader->get_node(type, node_id);
  1620. ERR_FAIL_COND(node.is_null());
  1621. if (node->get_description() == comment_desc_change_edit->get_text()) {
  1622. return; // nothing changed - ignored
  1623. }
  1624. undo_redo->create_action(TTR("Set Comment Node Description"));
  1625. undo_redo->add_do_method(node.ptr(), "set_description", comment_desc_change_edit->get_text());
  1626. undo_redo->add_undo_method(node.ptr(), "set_description", node->get_title());
  1627. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, node_id);
  1628. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, node_id);
  1629. undo_redo->commit_action();
  1630. }
  1631. void VisualShaderEditor::_uniform_line_edit_changed(const String &p_text, int p_node_id) {
  1632. VisualShader::Type type = get_current_shader_type();
  1633. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  1634. ERR_FAIL_COND(!node.is_valid());
  1635. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  1636. if (validated_name == node->get_uniform_name()) {
  1637. return;
  1638. }
  1639. undo_redo->create_action(TTR("Set Uniform Name"));
  1640. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  1641. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  1642. undo_redo->add_do_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, validated_name);
  1643. undo_redo->add_undo_method(graph_plugin.ptr(), "set_uniform_name", type, p_node_id, node->get_uniform_name());
  1644. undo_redo->add_do_method(this, "_update_uniforms", true);
  1645. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1646. Set<String> changed_names;
  1647. changed_names.insert(node->get_uniform_name());
  1648. _update_uniform_refs(changed_names);
  1649. undo_redo->commit_action();
  1650. }
  1651. void VisualShaderEditor::_uniform_line_edit_focus_out(Object *line_edit, int p_node_id) {
  1652. _uniform_line_edit_changed(Object::cast_to<LineEdit>(line_edit)->get_text(), p_node_id);
  1653. }
  1654. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  1655. if (!p_output) {
  1656. _change_input_port_name(Object::cast_to<LineEdit>(line_edit)->get_text(), line_edit, p_node_id, p_port_id);
  1657. } else {
  1658. _change_output_port_name(Object::cast_to<LineEdit>(line_edit)->get_text(), line_edit, p_node_id, p_port_id);
  1659. }
  1660. }
  1661. void VisualShaderEditor::_port_edited() {
  1662. VisualShader::Type type = get_current_shader_type();
  1663. Variant value = property_editor->get_variant();
  1664. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1665. ERR_FAIL_COND(!vsn.is_valid());
  1666. undo_redo->create_action(TTR("Set Input Default Port"));
  1667. Ref<VisualShaderNodeCustom> custom = Object::cast_to<VisualShaderNodeCustom>(vsn.ptr());
  1668. if (custom.is_valid()) {
  1669. undo_redo->add_do_method(custom.ptr(), "_set_input_port_default_value", editing_port, value);
  1670. undo_redo->add_undo_method(custom.ptr(), "_set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1671. } else {
  1672. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1673. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1674. }
  1675. undo_redo->add_do_method(graph_plugin.ptr(), "set_input_port_default_value", type, editing_node, editing_port, value);
  1676. 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));
  1677. undo_redo->commit_action();
  1678. property_editor->hide();
  1679. }
  1680. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1681. VisualShader::Type type = get_current_shader_type();
  1682. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1683. Button *button = Object::cast_to<Button>(p_button);
  1684. ERR_FAIL_COND(!button);
  1685. Variant value = vsn->get_input_port_default_value(p_port);
  1686. property_editor->set_position(button->get_screen_position() + Vector2(0, button->get_size().height));
  1687. property_editor->edit(nullptr, "", value.get_type(), value, 0, "");
  1688. property_editor->popup();
  1689. editing_node = p_node;
  1690. editing_port = p_port;
  1691. }
  1692. void VisualShaderEditor::_setup_node(VisualShaderNode *p_node, int p_op_idx) {
  1693. // FLOAT_OP
  1694. {
  1695. VisualShaderNodeFloatOp *floatOp = Object::cast_to<VisualShaderNodeFloatOp>(p_node);
  1696. if (floatOp) {
  1697. floatOp->set_operator((VisualShaderNodeFloatOp::Operator)p_op_idx);
  1698. return;
  1699. }
  1700. }
  1701. // FLOAT_FUNC
  1702. {
  1703. VisualShaderNodeFloatFunc *floatFunc = Object::cast_to<VisualShaderNodeFloatFunc>(p_node);
  1704. if (floatFunc) {
  1705. floatFunc->set_function((VisualShaderNodeFloatFunc::Function)p_op_idx);
  1706. return;
  1707. }
  1708. }
  1709. // VECTOR_OP
  1710. {
  1711. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(p_node);
  1712. if (vecOp) {
  1713. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1714. return;
  1715. }
  1716. }
  1717. // VECTOR_FUNC
  1718. {
  1719. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(p_node);
  1720. if (vecFunc) {
  1721. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1722. return;
  1723. }
  1724. }
  1725. // COLOR_OP
  1726. {
  1727. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(p_node);
  1728. if (colorOp) {
  1729. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1730. return;
  1731. }
  1732. }
  1733. // COLOR_FUNC
  1734. {
  1735. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(p_node);
  1736. if (colorFunc) {
  1737. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1738. return;
  1739. }
  1740. }
  1741. // INT_OP
  1742. {
  1743. VisualShaderNodeIntOp *intOp = Object::cast_to<VisualShaderNodeIntOp>(p_node);
  1744. if (intOp) {
  1745. intOp->set_operator((VisualShaderNodeIntOp::Operator)p_op_idx);
  1746. return;
  1747. }
  1748. }
  1749. // INT_FUNC
  1750. {
  1751. VisualShaderNodeIntFunc *intFunc = Object::cast_to<VisualShaderNodeIntFunc>(p_node);
  1752. if (intFunc) {
  1753. intFunc->set_function((VisualShaderNodeIntFunc::Function)p_op_idx);
  1754. return;
  1755. }
  1756. }
  1757. // TRANSFORM_FUNC
  1758. {
  1759. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(p_node);
  1760. if (matFunc) {
  1761. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1762. return;
  1763. }
  1764. }
  1765. //UV_FUNC
  1766. {
  1767. VisualShaderNodeUVFunc *uvFunc = Object::cast_to<VisualShaderNodeUVFunc>(p_node);
  1768. if (uvFunc) {
  1769. uvFunc->set_function((VisualShaderNodeUVFunc::Function)p_op_idx);
  1770. return;
  1771. }
  1772. }
  1773. // IS
  1774. {
  1775. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(p_node);
  1776. if (is) {
  1777. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1778. return;
  1779. }
  1780. }
  1781. // COMPARE
  1782. {
  1783. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(p_node);
  1784. if (cmp) {
  1785. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1786. return;
  1787. }
  1788. }
  1789. // DERIVATIVE
  1790. {
  1791. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(p_node);
  1792. if (sderFunc) {
  1793. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1794. return;
  1795. }
  1796. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(p_node);
  1797. if (vderFunc) {
  1798. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1799. return;
  1800. }
  1801. }
  1802. // MIX
  1803. {
  1804. VisualShaderNodeMix *mix = Object::cast_to<VisualShaderNodeMix>(p_node);
  1805. if (mix) {
  1806. mix->set_op_type((VisualShaderNodeMix::OpType)p_op_idx);
  1807. return;
  1808. }
  1809. }
  1810. // CLAMP
  1811. {
  1812. VisualShaderNodeClamp *clampFunc = Object::cast_to<VisualShaderNodeClamp>(p_node);
  1813. if (clampFunc) {
  1814. clampFunc->set_op_type((VisualShaderNodeClamp::OpType)p_op_idx);
  1815. return;
  1816. }
  1817. }
  1818. // SWITCH
  1819. {
  1820. VisualShaderNodeSwitch *switchFunc = Object::cast_to<VisualShaderNodeSwitch>(p_node);
  1821. if (switchFunc) {
  1822. switchFunc->set_op_type((VisualShaderNodeSwitch::OpType)p_op_idx);
  1823. return;
  1824. }
  1825. }
  1826. // SMOOTHSTEP
  1827. {
  1828. VisualShaderNodeSmoothStep *smoothStepFunc = Object::cast_to<VisualShaderNodeSmoothStep>(p_node);
  1829. if (smoothStepFunc) {
  1830. smoothStepFunc->set_op_type((VisualShaderNodeSmoothStep::OpType)p_op_idx);
  1831. return;
  1832. }
  1833. }
  1834. // STEP
  1835. {
  1836. VisualShaderNodeStep *stepFunc = Object::cast_to<VisualShaderNodeStep>(p_node);
  1837. if (stepFunc) {
  1838. stepFunc->set_op_type((VisualShaderNodeStep::OpType)p_op_idx);
  1839. return;
  1840. }
  1841. }
  1842. // MULTIPLY_ADD
  1843. {
  1844. VisualShaderNodeMultiplyAdd *fmaFunc = Object::cast_to<VisualShaderNodeMultiplyAdd>(p_node);
  1845. if (fmaFunc) {
  1846. fmaFunc->set_op_type((VisualShaderNodeMultiplyAdd::OpType)p_op_idx);
  1847. }
  1848. }
  1849. }
  1850. void VisualShaderEditor::_add_node(int p_idx, int p_op_idx, String p_resource_path, int p_node_idx) {
  1851. ERR_FAIL_INDEX(p_idx, add_options.size());
  1852. Ref<VisualShaderNode> vsnode;
  1853. bool is_custom = add_options[p_idx].is_custom;
  1854. if (!is_custom && add_options[p_idx].type != String()) {
  1855. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instantiate(add_options[p_idx].type));
  1856. ERR_FAIL_COND(!vsn);
  1857. VisualShaderNodeFloatConstant *constant = Object::cast_to<VisualShaderNodeFloatConstant>(vsn);
  1858. if (constant) {
  1859. if ((int)add_options[p_idx].value != -1) {
  1860. constant->set_constant(add_options[p_idx].value);
  1861. }
  1862. } else {
  1863. if (p_op_idx != -1) {
  1864. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1865. if (input) {
  1866. input->set_input_name(add_options[p_idx].sub_func_str);
  1867. } else {
  1868. _setup_node(vsn, p_op_idx);
  1869. }
  1870. }
  1871. }
  1872. vsnode = Ref<VisualShaderNode>(vsn);
  1873. } else {
  1874. ERR_FAIL_COND(add_options[p_idx].script.is_null());
  1875. String base_type = add_options[p_idx].script->get_instance_base_type();
  1876. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instantiate(base_type));
  1877. ERR_FAIL_COND(!vsn);
  1878. vsnode = Ref<VisualShaderNode>(vsn);
  1879. vsnode->set_script(add_options[p_idx].script);
  1880. }
  1881. Point2 position = graph->get_scroll_ofs();
  1882. if (saved_node_pos_dirty) {
  1883. position += saved_node_pos;
  1884. } else {
  1885. position += graph->get_size() * 0.5;
  1886. position /= EDSCALE;
  1887. }
  1888. saved_node_pos_dirty = false;
  1889. VisualShader::Type type = get_current_shader_type();
  1890. int id_to_use = visual_shader->get_valid_node_id(type);
  1891. if (p_resource_path.is_empty()) {
  1892. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1893. } else {
  1894. id_to_use += p_node_idx;
  1895. }
  1896. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1897. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1898. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_to_use);
  1899. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_to_use);
  1900. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1901. if (expr) {
  1902. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1903. }
  1904. bool created_expression_port = false;
  1905. if (to_node != -1 && to_slot != -1) {
  1906. VisualShaderNode::PortType input_port_type = visual_shader->get_node(type, to_node)->get_input_port_type(to_slot);
  1907. if (expr && expr->is_editable() && input_port_type != VisualShaderNode::PORT_TYPE_SAMPLER) {
  1908. undo_redo->add_do_method(expr, "add_output_port", 0, input_port_type, "output0");
  1909. undo_redo->add_undo_method(expr, "remove_output_port", 0);
  1910. String initial_expression_code;
  1911. switch (input_port_type) {
  1912. case VisualShaderNode::PORT_TYPE_SCALAR:
  1913. initial_expression_code = "output0 = 1.0;";
  1914. break;
  1915. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  1916. initial_expression_code = "output0 = 1;";
  1917. break;
  1918. case VisualShaderNode::PORT_TYPE_VECTOR:
  1919. initial_expression_code = "output0 = vec3(1.0, 1.0, 1.0);";
  1920. break;
  1921. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1922. initial_expression_code = "output0 = true;";
  1923. break;
  1924. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1925. initial_expression_code = "output0 = mat4(1.0);";
  1926. break;
  1927. default:
  1928. break;
  1929. }
  1930. undo_redo->add_do_method(expr, "set_expression", initial_expression_code);
  1931. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, id_to_use);
  1932. created_expression_port = true;
  1933. }
  1934. if (vsnode->get_output_port_count() > 0 || created_expression_port) {
  1935. int _from_node = id_to_use;
  1936. int _from_slot = 0;
  1937. if (created_expression_port) {
  1938. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1939. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1940. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1941. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1942. } else {
  1943. // Need to setting up Input node properly before committing since `is_port_types_compatible` (calling below) is using `mode` and `shader_type`.
  1944. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsnode.ptr());
  1945. if (input) {
  1946. input->set_shader_mode(visual_shader->get_mode());
  1947. input->set_shader_type(visual_shader->get_shader_type());
  1948. }
  1949. // Attempting to connect to the first correct port.
  1950. for (int i = 0; i < vsnode->get_output_port_count(); i++) {
  1951. if (visual_shader->is_port_types_compatible(vsnode->get_output_port_type(i), input_port_type)) {
  1952. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, i, to_node, to_slot);
  1953. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, i, to_node, to_slot);
  1954. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, _from_node, i, to_node, to_slot);
  1955. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, _from_node, i, to_node, to_slot);
  1956. break;
  1957. }
  1958. }
  1959. }
  1960. }
  1961. } else if (from_node != -1 && from_slot != -1) {
  1962. VisualShaderNode::PortType output_port_type = visual_shader->get_node(type, from_node)->get_output_port_type(from_slot);
  1963. if (expr && expr->is_editable()) {
  1964. undo_redo->add_do_method(expr, "add_input_port", 0, output_port_type, "input0");
  1965. undo_redo->add_undo_method(expr, "remove_input_port", 0);
  1966. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type, id_to_use);
  1967. created_expression_port = true;
  1968. }
  1969. if (vsnode->get_input_port_count() > 0 || created_expression_port) {
  1970. int _to_node = id_to_use;
  1971. int _to_slot = 0;
  1972. if (created_expression_port) {
  1973. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1974. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1975. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1976. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1977. } else {
  1978. // Attempting to connect to the first correct port.
  1979. for (int i = 0; i < vsnode->get_input_port_count(); i++) {
  1980. if (visual_shader->is_port_types_compatible(output_port_type, vsnode->get_input_port_type(i))) {
  1981. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
  1982. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
  1983. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, i);
  1984. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, i);
  1985. break;
  1986. }
  1987. }
  1988. }
  1989. }
  1990. }
  1991. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(vsnode.ptr());
  1992. if (uniform) {
  1993. undo_redo->add_do_method(this, "_update_uniforms", true);
  1994. undo_redo->add_undo_method(this, "_update_uniforms", true);
  1995. }
  1996. VisualShaderNodeCurveTexture *curve = Object::cast_to<VisualShaderNodeCurveTexture>(vsnode.ptr());
  1997. if (curve) {
  1998. graph_plugin->call_deferred("update_curve", id_to_use);
  1999. }
  2000. if (p_resource_path.is_empty()) {
  2001. undo_redo->commit_action();
  2002. } else {
  2003. //post-initialization
  2004. VisualShaderNodeTexture *texture2d = Object::cast_to<VisualShaderNodeTexture>(vsnode.ptr());
  2005. VisualShaderNodeTexture3D *texture3d = Object::cast_to<VisualShaderNodeTexture3D>(vsnode.ptr());
  2006. if (texture2d || texture3d || curve) {
  2007. undo_redo->add_do_method(vsnode.ptr(), "set_texture", ResourceLoader::load(p_resource_path));
  2008. return;
  2009. }
  2010. VisualShaderNodeCubemap *cubemap = Object::cast_to<VisualShaderNodeCubemap>(vsnode.ptr());
  2011. if (cubemap) {
  2012. undo_redo->add_do_method(vsnode.ptr(), "set_cube_map", ResourceLoader::load(p_resource_path));
  2013. return;
  2014. }
  2015. VisualShaderNodeTexture2DArray *texture2d_array = Object::cast_to<VisualShaderNodeTexture2DArray>(vsnode.ptr());
  2016. if (texture2d_array) {
  2017. undo_redo->add_do_method(vsnode.ptr(), "set_texture_array", ResourceLoader::load(p_resource_path));
  2018. }
  2019. }
  2020. }
  2021. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  2022. VisualShader::Type type = get_current_shader_type();
  2023. drag_buffer.push_back({ type, p_node, p_from, p_to });
  2024. if (!drag_dirty) {
  2025. call_deferred("_nodes_dragged");
  2026. }
  2027. drag_dirty = true;
  2028. }
  2029. void VisualShaderEditor::_nodes_dragged() {
  2030. drag_dirty = false;
  2031. undo_redo->create_action(TTR("Node(s) Moved"));
  2032. for (List<DragOp>::Element *E = drag_buffer.front(); E; E = E->next()) {
  2033. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  2034. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  2035. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  2036. undo_redo->add_undo_method(graph_plugin.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  2037. }
  2038. drag_buffer.clear();
  2039. undo_redo->commit_action();
  2040. }
  2041. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  2042. VisualShader::Type type = get_current_shader_type();
  2043. int from = p_from.to_int();
  2044. int to = p_to.to_int();
  2045. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  2046. return;
  2047. }
  2048. undo_redo->create_action(TTR("Nodes Connected"));
  2049. List<VisualShader::Connection> conns;
  2050. visual_shader->get_node_connections(type, &conns);
  2051. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2052. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  2053. 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);
  2054. 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);
  2055. 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);
  2056. 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);
  2057. }
  2058. }
  2059. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  2060. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  2061. undo_redo->add_do_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  2062. undo_redo->add_undo_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  2063. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, to);
  2064. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, to);
  2065. undo_redo->commit_action();
  2066. }
  2067. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  2068. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  2069. VisualShader::Type type = get_current_shader_type();
  2070. int from = p_from.to_int();
  2071. int to = p_to.to_int();
  2072. undo_redo->create_action(TTR("Nodes Disconnected"));
  2073. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  2074. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  2075. undo_redo->add_do_method(graph_plugin.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  2076. undo_redo->add_undo_method(graph_plugin.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  2077. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", (int)type, to);
  2078. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", (int)type, to);
  2079. undo_redo->commit_action();
  2080. }
  2081. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  2082. from_node = p_from.to_int();
  2083. from_slot = p_from_slot;
  2084. _show_members_dialog(true);
  2085. }
  2086. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  2087. to_node = p_to.to_int();
  2088. to_slot = p_to_slot;
  2089. _show_members_dialog(true);
  2090. }
  2091. void VisualShaderEditor::_delete_nodes(int p_type, const List<int> &p_nodes) {
  2092. VisualShader::Type type = VisualShader::Type(p_type);
  2093. List<VisualShader::Connection> conns;
  2094. visual_shader->get_node_connections(type, &conns);
  2095. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  2096. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2097. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  2098. 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);
  2099. }
  2100. }
  2101. }
  2102. Set<String> uniform_names;
  2103. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  2104. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  2105. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  2106. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  2107. undo_redo->add_undo_method(graph_plugin.ptr(), "add_node", type, F->get());
  2108. undo_redo->add_do_method(this, "_clear_buffer");
  2109. undo_redo->add_undo_method(this, "_clear_buffer");
  2110. // restore size, inputs and outputs if node is group
  2111. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  2112. if (group) {
  2113. undo_redo->add_undo_method(group, "set_size", group->get_size());
  2114. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  2115. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  2116. }
  2117. // restore expression text if node is expression
  2118. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  2119. if (expression) {
  2120. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  2121. }
  2122. VisualShaderNodeUniform *uniform = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  2123. if (uniform) {
  2124. uniform_names.insert(uniform->get_uniform_name());
  2125. }
  2126. }
  2127. List<VisualShader::Connection> used_conns;
  2128. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  2129. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2130. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  2131. bool cancel = false;
  2132. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  2133. 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) {
  2134. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  2135. break;
  2136. }
  2137. }
  2138. if (!cancel) {
  2139. 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);
  2140. 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);
  2141. used_conns.push_back(E->get());
  2142. }
  2143. }
  2144. }
  2145. }
  2146. // delete nodes from the graph
  2147. for (const List<int>::Element *F = p_nodes.front(); F; F = F->next()) {
  2148. undo_redo->add_do_method(graph_plugin.ptr(), "remove_node", type, F->get());
  2149. }
  2150. // update uniform refs if any uniform has been deleted
  2151. if (uniform_names.size() > 0) {
  2152. undo_redo->add_do_method(this, "_update_uniforms", true);
  2153. undo_redo->add_undo_method(this, "_update_uniforms", true);
  2154. _update_uniform_refs(uniform_names);
  2155. }
  2156. }
  2157. void VisualShaderEditor::_replace_node(VisualShader::Type p_type_id, int p_node_id, const StringName &p_from, const StringName &p_to) {
  2158. undo_redo->add_do_method(visual_shader.ptr(), "replace_node", p_type_id, p_node_id, p_to);
  2159. undo_redo->add_undo_method(visual_shader.ptr(), "replace_node", p_type_id, p_node_id, p_from);
  2160. }
  2161. void VisualShaderEditor::_update_constant(VisualShader::Type p_type_id, int p_node_id, Variant p_var, int p_preview_port) {
  2162. Ref<VisualShaderNode> node = visual_shader->get_node(p_type_id, p_node_id);
  2163. ERR_FAIL_COND(!node.is_valid());
  2164. ERR_FAIL_COND(!node->has_method("set_constant"));
  2165. node->call("set_constant", p_var);
  2166. if (p_preview_port != -1) {
  2167. node->set_output_port_for_preview(p_preview_port);
  2168. }
  2169. }
  2170. void VisualShaderEditor::_update_uniform(VisualShader::Type p_type_id, int p_node_id, Variant p_var, int p_preview_port) {
  2171. Ref<VisualShaderNodeUniform> uniform = visual_shader->get_node(p_type_id, p_node_id);
  2172. ERR_FAIL_COND(!uniform.is_valid());
  2173. String valid_name = visual_shader->validate_uniform_name(uniform->get_uniform_name(), uniform);
  2174. uniform->set_uniform_name(valid_name);
  2175. graph_plugin->set_uniform_name(p_type_id, p_node_id, valid_name);
  2176. if (uniform->has_method("set_default_value_enabled")) {
  2177. uniform->call("set_default_value_enabled", true);
  2178. uniform->call("set_default_value", p_var);
  2179. }
  2180. if (p_preview_port != -1) {
  2181. uniform->set_output_port_for_preview(p_preview_port);
  2182. }
  2183. }
  2184. void VisualShaderEditor::_convert_constants_to_uniforms(bool p_vice_versa) {
  2185. VisualShader::Type type_id = get_current_shader_type();
  2186. if (!p_vice_versa) {
  2187. undo_redo->create_action(TTR("Convert Constant Node(s) To Uniform(s)"));
  2188. } else {
  2189. undo_redo->create_action(TTR("Convert Uniform Node(s) To Constant(s)"));
  2190. }
  2191. const Set<int> &current_set = p_vice_versa ? selected_uniforms : selected_constants;
  2192. Set<String> deleted_names;
  2193. for (Set<int>::Element *E = current_set.front(); E; E = E->next()) {
  2194. int node_id = E->get();
  2195. Ref<VisualShaderNode> node = visual_shader->get_node(type_id, node_id);
  2196. bool caught = false;
  2197. Variant var;
  2198. // float
  2199. if (!p_vice_versa) {
  2200. Ref<VisualShaderNodeFloatConstant> float_const = Object::cast_to<VisualShaderNodeFloatConstant>(node.ptr());
  2201. if (float_const.is_valid()) {
  2202. _replace_node(type_id, node_id, "VisualShaderNodeFloatConstant", "VisualShaderNodeFloatUniform");
  2203. var = float_const->get_constant();
  2204. caught = true;
  2205. }
  2206. } else {
  2207. Ref<VisualShaderNodeFloatUniform> float_uniform = Object::cast_to<VisualShaderNodeFloatUniform>(node.ptr());
  2208. if (float_uniform.is_valid()) {
  2209. _replace_node(type_id, node_id, "VisualShaderNodeFloatUniform", "VisualShaderNodeFloatConstant");
  2210. var = float_uniform->get_default_value();
  2211. caught = true;
  2212. }
  2213. }
  2214. // int
  2215. if (!caught) {
  2216. if (!p_vice_versa) {
  2217. Ref<VisualShaderNodeIntConstant> int_const = Object::cast_to<VisualShaderNodeIntConstant>(node.ptr());
  2218. if (int_const.is_valid()) {
  2219. _replace_node(type_id, node_id, "VisualShaderNodeIntConstant", "VisualShaderNodeIntUniform");
  2220. var = int_const->get_constant();
  2221. caught = true;
  2222. }
  2223. } else {
  2224. Ref<VisualShaderNodeIntUniform> int_uniform = Object::cast_to<VisualShaderNodeIntUniform>(node.ptr());
  2225. if (int_uniform.is_valid()) {
  2226. _replace_node(type_id, node_id, "VisualShaderNodeIntUniform", "VisualShaderNodeIntConstant");
  2227. var = int_uniform->get_default_value();
  2228. caught = true;
  2229. }
  2230. }
  2231. }
  2232. // boolean
  2233. if (!caught) {
  2234. if (!p_vice_versa) {
  2235. Ref<VisualShaderNodeBooleanConstant> boolean_const = Object::cast_to<VisualShaderNodeBooleanConstant>(node.ptr());
  2236. if (boolean_const.is_valid()) {
  2237. _replace_node(type_id, node_id, "VisualShaderNodeBooleanConstant", "VisualShaderNodeBooleanUniform");
  2238. var = boolean_const->get_constant();
  2239. caught = true;
  2240. }
  2241. } else {
  2242. Ref<VisualShaderNodeBooleanUniform> boolean_uniform = Object::cast_to<VisualShaderNodeBooleanUniform>(node.ptr());
  2243. if (boolean_uniform.is_valid()) {
  2244. _replace_node(type_id, node_id, "VisualShaderNodeBooleanUniform", "VisualShaderNodeBooleanConstant");
  2245. var = boolean_uniform->get_default_value();
  2246. caught = true;
  2247. }
  2248. }
  2249. }
  2250. // vec3
  2251. if (!caught) {
  2252. if (!p_vice_versa) {
  2253. Ref<VisualShaderNodeVec3Constant> vec3_const = Object::cast_to<VisualShaderNodeVec3Constant>(node.ptr());
  2254. if (vec3_const.is_valid()) {
  2255. _replace_node(type_id, node_id, "VisualShaderNodeVec3Constant", "VisualShaderNodeVec3Uniform");
  2256. var = vec3_const->get_constant();
  2257. caught = true;
  2258. }
  2259. } else {
  2260. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = Object::cast_to<VisualShaderNodeVec3Uniform>(node.ptr());
  2261. if (vec3_uniform.is_valid()) {
  2262. _replace_node(type_id, node_id, "VisualShaderNodeVec3Uniform", "VisualShaderNodeVec3Constant");
  2263. var = vec3_uniform->get_default_value();
  2264. caught = true;
  2265. }
  2266. }
  2267. }
  2268. // color
  2269. if (!caught) {
  2270. if (!p_vice_versa) {
  2271. Ref<VisualShaderNodeColorConstant> color_const = Object::cast_to<VisualShaderNodeColorConstant>(node.ptr());
  2272. if (color_const.is_valid()) {
  2273. _replace_node(type_id, node_id, "VisualShaderNodeColorConstant", "VisualShaderNodeColorUniform");
  2274. var = color_const->get_constant();
  2275. caught = true;
  2276. }
  2277. } else {
  2278. Ref<VisualShaderNodeColorUniform> color_uniform = Object::cast_to<VisualShaderNodeColorUniform>(node.ptr());
  2279. if (color_uniform.is_valid()) {
  2280. _replace_node(type_id, node_id, "VisualShaderNodeColorUniform", "VisualShaderNodeColorConstant");
  2281. var = color_uniform->get_default_value();
  2282. caught = true;
  2283. }
  2284. }
  2285. }
  2286. // transform
  2287. if (!caught) {
  2288. if (!p_vice_versa) {
  2289. Ref<VisualShaderNodeTransformConstant> transform_const = Object::cast_to<VisualShaderNodeTransformConstant>(node.ptr());
  2290. if (transform_const.is_valid()) {
  2291. _replace_node(type_id, node_id, "VisualShaderNodeTransformConstant", "VisualShaderNodeTransformUniform");
  2292. var = transform_const->get_constant();
  2293. caught = true;
  2294. }
  2295. } else {
  2296. Ref<VisualShaderNodeTransformUniform> transform_uniform = Object::cast_to<VisualShaderNodeTransformUniform>(node.ptr());
  2297. if (transform_uniform.is_valid()) {
  2298. _replace_node(type_id, node_id, "VisualShaderNodeTransformUniform", "VisualShaderNodeTransformConstant");
  2299. var = transform_uniform->get_default_value();
  2300. caught = true;
  2301. }
  2302. }
  2303. }
  2304. ERR_CONTINUE(!caught);
  2305. int preview_port = node->get_output_port_for_preview();
  2306. if (!p_vice_versa) {
  2307. undo_redo->add_do_method(this, "_update_uniform", type_id, node_id, var, preview_port);
  2308. undo_redo->add_undo_method(this, "_update_constant", type_id, node_id, var, preview_port);
  2309. } else {
  2310. undo_redo->add_do_method(this, "_update_constant", type_id, node_id, var, preview_port);
  2311. undo_redo->add_undo_method(this, "_update_uniform", type_id, node_id, var, preview_port);
  2312. Ref<VisualShaderNodeUniform> uniform = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  2313. ERR_CONTINUE(!uniform.is_valid());
  2314. deleted_names.insert(uniform->get_uniform_name());
  2315. }
  2316. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, node_id);
  2317. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, node_id);
  2318. }
  2319. undo_redo->add_do_method(this, "_update_uniforms", true);
  2320. undo_redo->add_undo_method(this, "_update_uniforms", true);
  2321. if (deleted_names.size() > 0) {
  2322. _update_uniform_refs(deleted_names);
  2323. }
  2324. undo_redo->commit_action();
  2325. }
  2326. void VisualShaderEditor::_delete_node_request(int p_type, int p_node) {
  2327. List<int> to_erase;
  2328. to_erase.push_back(p_node);
  2329. undo_redo->create_action(TTR("Delete VisualShader Node"));
  2330. _delete_nodes(p_type, to_erase);
  2331. undo_redo->commit_action();
  2332. }
  2333. void VisualShaderEditor::_delete_nodes_request() {
  2334. List<int> to_erase;
  2335. for (int i = 0; i < graph->get_child_count(); i++) {
  2336. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2337. if (gn) {
  2338. if (gn->is_selected() && gn->is_close_button_visible()) {
  2339. to_erase.push_back(gn->get_name().operator String().to_int());
  2340. }
  2341. }
  2342. }
  2343. if (to_erase.is_empty()) {
  2344. return;
  2345. }
  2346. undo_redo->create_action(TTR("Delete VisualShader Node(s)"));
  2347. _delete_nodes(get_current_shader_type(), to_erase);
  2348. undo_redo->commit_action();
  2349. }
  2350. void VisualShaderEditor::_node_selected(Object *p_node) {
  2351. VisualShader::Type type = get_current_shader_type();
  2352. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  2353. ERR_FAIL_COND(!gn);
  2354. int id = String(gn->get_name()).to_int();
  2355. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  2356. ERR_FAIL_COND(!vsnode.is_valid());
  2357. //do not rely on this, makes editor more complex
  2358. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  2359. }
  2360. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  2361. Ref<InputEventMouseButton> mb = p_event;
  2362. VisualShader::Type type = get_current_shader_type();
  2363. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MOUSE_BUTTON_RIGHT) {
  2364. selected_constants.clear();
  2365. selected_uniforms.clear();
  2366. selected_comment = -1;
  2367. List<int> to_change;
  2368. for (int i = 0; i < graph->get_child_count(); i++) {
  2369. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2370. if (gn) {
  2371. if (gn->is_selected() && gn->is_close_button_visible()) {
  2372. int id = gn->get_name().operator String().to_int();
  2373. to_change.push_back(id);
  2374. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  2375. VisualShaderNodeComment *comment_node = Object::cast_to<VisualShaderNodeComment>(node.ptr());
  2376. if (comment_node != nullptr) {
  2377. selected_comment = id;
  2378. }
  2379. VisualShaderNodeConstant *constant_node = Object::cast_to<VisualShaderNodeConstant>(node.ptr());
  2380. if (constant_node != nullptr) {
  2381. selected_constants.insert(id);
  2382. }
  2383. VisualShaderNodeUniform *uniform_node = Object::cast_to<VisualShaderNodeUniform>(node.ptr());
  2384. if (uniform_node != nullptr && uniform_node->is_convertible_to_constant()) {
  2385. selected_uniforms.insert(id);
  2386. }
  2387. }
  2388. }
  2389. }
  2390. if (to_change.size() > 1) {
  2391. selected_comment = -1;
  2392. }
  2393. if (to_change.is_empty() && copy_nodes_buffer.is_empty()) {
  2394. _show_members_dialog(true);
  2395. } else {
  2396. popup_menu->set_item_disabled(NodeMenuOptions::COPY, to_change.is_empty());
  2397. popup_menu->set_item_disabled(NodeMenuOptions::PASTE, copy_nodes_buffer.is_empty());
  2398. popup_menu->set_item_disabled(NodeMenuOptions::DELETE, to_change.is_empty());
  2399. popup_menu->set_item_disabled(NodeMenuOptions::DUPLICATE, to_change.is_empty());
  2400. int temp = popup_menu->get_item_index(NodeMenuOptions::SEPARATOR2);
  2401. if (temp != -1) {
  2402. popup_menu->remove_item(temp);
  2403. }
  2404. temp = popup_menu->get_item_index(NodeMenuOptions::CONVERT_CONSTANTS_TO_UNIFORMS);
  2405. if (temp != -1) {
  2406. popup_menu->remove_item(temp);
  2407. }
  2408. temp = popup_menu->get_item_index(NodeMenuOptions::CONVERT_UNIFORMS_TO_CONSTANTS);
  2409. if (temp != -1) {
  2410. popup_menu->remove_item(temp);
  2411. }
  2412. temp = popup_menu->get_item_index(NodeMenuOptions::SEPARATOR3);
  2413. if (temp != -1) {
  2414. popup_menu->remove_item(temp);
  2415. }
  2416. temp = popup_menu->get_item_index(NodeMenuOptions::SET_COMMENT_TITLE);
  2417. if (temp != -1) {
  2418. popup_menu->remove_item(temp);
  2419. }
  2420. temp = popup_menu->get_item_index(NodeMenuOptions::SET_COMMENT_DESCRIPTION);
  2421. if (temp != -1) {
  2422. popup_menu->remove_item(temp);
  2423. }
  2424. if (selected_comment != -1) {
  2425. popup_menu->add_separator("", NodeMenuOptions::SEPARATOR2);
  2426. popup_menu->add_item(TTR("Set Comment Title"), NodeMenuOptions::SET_COMMENT_TITLE);
  2427. popup_menu->add_item(TTR("Set Comment Description"), NodeMenuOptions::SET_COMMENT_DESCRIPTION);
  2428. }
  2429. if (selected_constants.size() > 0 || selected_uniforms.size() > 0) {
  2430. popup_menu->add_separator("", NodeMenuOptions::SEPARATOR3);
  2431. if (selected_constants.size() > 0) {
  2432. popup_menu->add_item(TTR("Convert Constant(s) to Uniform(s)"), NodeMenuOptions::CONVERT_CONSTANTS_TO_UNIFORMS);
  2433. }
  2434. if (selected_uniforms.size() > 0) {
  2435. popup_menu->add_item(TTR("Convert Uniform(s) to Constant(s)"), NodeMenuOptions::CONVERT_UNIFORMS_TO_CONSTANTS);
  2436. }
  2437. }
  2438. menu_point = graph->get_local_mouse_position();
  2439. Point2 gpos = Input::get_singleton()->get_mouse_position();
  2440. popup_menu->set_position(gpos);
  2441. popup_menu->set_size(Size2(-1, -1));
  2442. popup_menu->popup();
  2443. }
  2444. }
  2445. }
  2446. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  2447. if (at_mouse_pos) {
  2448. saved_node_pos_dirty = true;
  2449. saved_node_pos = graph->get_local_mouse_position();
  2450. Point2 gpos = Input::get_singleton()->get_mouse_position();
  2451. members_dialog->popup();
  2452. members_dialog->set_position(gpos);
  2453. } else {
  2454. members_dialog->popup();
  2455. saved_node_pos_dirty = false;
  2456. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  2457. }
  2458. // keep dialog within window bounds
  2459. Size2 window_size = DisplayServer::get_singleton()->window_get_size();
  2460. Rect2 dialog_rect = Rect2(members_dialog->get_position(), members_dialog->get_size());
  2461. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  2462. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  2463. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  2464. }
  2465. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  2466. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  2467. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  2468. }
  2469. node_filter->call_deferred("grab_focus"); // still not visible
  2470. node_filter->select_all();
  2471. }
  2472. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  2473. Ref<InputEventKey> ie = p_ie;
  2474. if (ie.is_valid() && (ie->get_keycode() == KEY_UP ||
  2475. ie->get_keycode() == KEY_DOWN ||
  2476. ie->get_keycode() == KEY_ENTER ||
  2477. ie->get_keycode() == KEY_KP_ENTER)) {
  2478. members->call("_gui_input", ie);
  2479. node_filter->accept_event();
  2480. }
  2481. }
  2482. void VisualShaderEditor::_notification(int p_what) {
  2483. if (p_what == NOTIFICATION_ENTER_TREE) {
  2484. node_filter->set_clear_button_enabled(true);
  2485. // collapse tree by default
  2486. TreeItem *category = members->get_root()->get_first_child();
  2487. while (category) {
  2488. category->set_collapsed(true);
  2489. TreeItem *sub_category = category->get_first_child();
  2490. while (sub_category) {
  2491. sub_category->set_collapsed(true);
  2492. sub_category = sub_category->get_next();
  2493. }
  2494. category = category->get_next();
  2495. }
  2496. }
  2497. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2498. Dictionary dd = get_viewport()->gui_get_drag_data();
  2499. if (members->is_visible_in_tree() && dd.has("id")) {
  2500. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  2501. }
  2502. } else if (p_what == NOTIFICATION_DRAG_END) {
  2503. members->set_drop_mode_flags(0);
  2504. }
  2505. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  2506. highend_label->set_modulate(get_theme_color("vulkan_color", "Editor"));
  2507. node_filter->set_right_icon(Control::get_theme_icon("Search", "EditorIcons"));
  2508. preview_shader->set_icon(Control::get_theme_icon("Shader", "EditorIcons"));
  2509. {
  2510. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  2511. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  2512. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  2513. Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
  2514. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  2515. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  2516. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  2517. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  2518. Color members_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  2519. preview_text->add_theme_color_override("background_color", background_color);
  2520. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  2521. if (ShaderLanguage::is_control_flow_keyword(E->get())) {
  2522. syntax_highlighter->add_keyword_color(E->get(), control_flow_keyword_color);
  2523. } else {
  2524. syntax_highlighter->add_keyword_color(E->get(), keyword_color);
  2525. }
  2526. }
  2527. preview_text->add_theme_font_override("font", get_theme_font("expression", "EditorFonts"));
  2528. preview_text->add_theme_font_size_override("font_size", get_theme_font_size("expression_size", "EditorFonts"));
  2529. preview_text->add_theme_color_override("font_color", text_color);
  2530. syntax_highlighter->set_number_color(number_color);
  2531. syntax_highlighter->set_symbol_color(symbol_color);
  2532. syntax_highlighter->set_function_color(function_color);
  2533. syntax_highlighter->set_member_variable_color(members_color);
  2534. syntax_highlighter->clear_color_regions();
  2535. syntax_highlighter->add_color_region("/*", "*/", comment_color, false);
  2536. syntax_highlighter->add_color_region("//", "", comment_color, true);
  2537. preview_text->clear_comment_delimiters();
  2538. preview_text->add_comment_delimiter("/*", "*/", false);
  2539. preview_text->add_comment_delimiter("//", "", true);
  2540. error_panel->add_theme_style_override("panel", get_theme_stylebox("panel", "Panel"));
  2541. error_label->add_theme_font_override("font", get_theme_font("status_source", "EditorFonts"));
  2542. error_label->add_theme_font_size_override("font_size", get_theme_font_size("status_source_size", "EditorFonts"));
  2543. error_label->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
  2544. }
  2545. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Tools", "EditorIcons"));
  2546. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree()) {
  2547. _update_graph();
  2548. }
  2549. }
  2550. }
  2551. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  2552. if (updating) {
  2553. return;
  2554. }
  2555. updating = true;
  2556. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  2557. updating = false;
  2558. }
  2559. void VisualShaderEditor::_node_changed(int p_id) {
  2560. if (updating) {
  2561. return;
  2562. }
  2563. if (is_visible_in_tree()) {
  2564. _update_graph();
  2565. }
  2566. }
  2567. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  2568. r_excluded.clear();
  2569. VisualShader::Type type = (VisualShader::Type)p_type;
  2570. for (int i = 0; i < graph->get_child_count(); i++) {
  2571. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2572. if (gn) {
  2573. int id = String(gn->get_name()).to_int();
  2574. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  2575. Ref<VisualShaderNodeOutput> output = node;
  2576. if (output.is_valid()) {
  2577. r_excluded.insert(id);
  2578. continue;
  2579. }
  2580. r_excluded.insert(id);
  2581. }
  2582. }
  2583. }
  2584. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  2585. VisualShader::Type type = (VisualShader::Type)p_type;
  2586. selection_center.x = 0.0f;
  2587. selection_center.y = 0.0f;
  2588. for (int i = 0; i < graph->get_child_count(); i++) {
  2589. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2590. if (gn) {
  2591. int id = String(gn->get_name()).to_int();
  2592. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  2593. Ref<VisualShaderNodeOutput> output = node;
  2594. if (output.is_valid()) { // can't duplicate output
  2595. r_excluded.insert(id);
  2596. continue;
  2597. }
  2598. if (node.is_valid() && gn->is_selected()) {
  2599. Vector2 pos = visual_shader->get_node_position(type, id);
  2600. selection_center += pos;
  2601. r_nodes.push_back(id);
  2602. }
  2603. r_excluded.insert(id);
  2604. }
  2605. }
  2606. selection_center /= (float)r_nodes.size();
  2607. }
  2608. 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) {
  2609. VisualShader::Type type = (VisualShader::Type)p_type;
  2610. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  2611. int base_id = visual_shader->get_valid_node_id(type);
  2612. int id_from = base_id;
  2613. Map<int, int> connection_remap;
  2614. Set<int> unsupported_set;
  2615. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  2616. connection_remap[E->get()] = id_from;
  2617. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  2618. bool unsupported = false;
  2619. for (int i = 0; i < add_options.size(); i++) {
  2620. if (add_options[i].type == node->get_class_name()) {
  2621. if (!_is_available(add_options[i].mode)) {
  2622. unsupported = true;
  2623. }
  2624. break;
  2625. }
  2626. }
  2627. if (unsupported) {
  2628. unsupported_set.insert(E->get());
  2629. continue;
  2630. }
  2631. Ref<VisualShaderNode> dupli = node->duplicate();
  2632. 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);
  2633. undo_redo->add_do_method(graph_plugin.ptr(), "add_node", type, id_from);
  2634. // duplicate size, inputs and outputs if node is group
  2635. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  2636. if (!group.is_null()) {
  2637. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  2638. undo_redo->add_do_method(graph_plugin.ptr(), "set_node_size", type, id_from, group->get_size());
  2639. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  2640. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  2641. }
  2642. // duplicate expression text if node is expression
  2643. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  2644. if (!expression.is_null()) {
  2645. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  2646. }
  2647. id_from++;
  2648. }
  2649. List<VisualShader::Connection> conns;
  2650. visual_shader->get_node_connections(pasted_type, &conns);
  2651. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2652. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  2653. continue;
  2654. }
  2655. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  2656. 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);
  2657. 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);
  2658. 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);
  2659. }
  2660. }
  2661. id_from = base_id;
  2662. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  2663. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  2664. undo_redo->add_undo_method(graph_plugin.ptr(), "remove_node", type, id_from);
  2665. id_from++;
  2666. }
  2667. undo_redo->commit_action();
  2668. if (p_select) {
  2669. // reselect duplicated nodes by excluding the other ones
  2670. for (int i = 0; i < graph->get_child_count(); i++) {
  2671. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  2672. if (gn) {
  2673. int id = String(gn->get_name()).to_int();
  2674. if (!r_excluded.has(id)) {
  2675. gn->set_selected(true);
  2676. } else {
  2677. gn->set_selected(false);
  2678. }
  2679. }
  2680. }
  2681. }
  2682. }
  2683. void VisualShaderEditor::_clear_buffer() {
  2684. copy_nodes_buffer.clear();
  2685. copy_nodes_excluded_buffer.clear();
  2686. }
  2687. void VisualShaderEditor::_duplicate_nodes() {
  2688. int type = get_current_shader_type();
  2689. List<int> nodes;
  2690. Set<int> excluded;
  2691. _dup_copy_nodes(type, nodes, excluded);
  2692. if (nodes.is_empty()) {
  2693. return;
  2694. }
  2695. undo_redo->create_action(TTR("Duplicate VisualShader Node(s)"));
  2696. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  2697. }
  2698. void VisualShaderEditor::_copy_nodes() {
  2699. copy_type = get_current_shader_type();
  2700. _clear_buffer();
  2701. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  2702. }
  2703. void VisualShaderEditor::_paste_nodes(bool p_use_custom_position, const Vector2 &p_custom_position) {
  2704. if (copy_nodes_buffer.is_empty()) {
  2705. return;
  2706. }
  2707. int type = get_current_shader_type();
  2708. float scale = graph->get_zoom();
  2709. Vector2 mpos;
  2710. if (p_use_custom_position) {
  2711. mpos = p_custom_position;
  2712. } else {
  2713. mpos = graph->get_local_mouse_position();
  2714. }
  2715. undo_redo->create_action(TTR("Paste VisualShader Node(s)"));
  2716. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + mpos / scale - selection_center), false);
  2717. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  2718. }
  2719. void VisualShaderEditor::_mode_selected(int p_id) {
  2720. int offset = 0;
  2721. if (mode & MODE_FLAGS_PARTICLES) {
  2722. offset = 3;
  2723. if (p_id + offset > VisualShader::TYPE_PROCESS) {
  2724. custom_mode_box->set_visible(false);
  2725. custom_mode_enabled = false;
  2726. } else {
  2727. custom_mode_box->set_visible(true);
  2728. if (custom_mode_box->is_pressed()) {
  2729. custom_mode_enabled = true;
  2730. offset += 3;
  2731. }
  2732. }
  2733. } else if (mode & MODE_FLAGS_SKY) {
  2734. offset = 8;
  2735. }
  2736. visual_shader->set_shader_type(VisualShader::Type(p_id + offset));
  2737. _update_options_menu();
  2738. _update_graph();
  2739. }
  2740. void VisualShaderEditor::_custom_mode_toggled(bool p_enabled) {
  2741. if (!(mode & MODE_FLAGS_PARTICLES)) {
  2742. return;
  2743. }
  2744. custom_mode_enabled = p_enabled;
  2745. int id = edit_type->get_selected() + 3;
  2746. if (p_enabled) {
  2747. visual_shader->set_shader_type(VisualShader::Type(id + 3));
  2748. } else {
  2749. visual_shader->set_shader_type(VisualShader::Type(id));
  2750. }
  2751. _update_options_menu();
  2752. _update_graph();
  2753. }
  2754. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> p_input, String p_name) {
  2755. String prev_name = p_input->get_input_name();
  2756. if (p_name == prev_name) {
  2757. return;
  2758. }
  2759. bool type_changed = p_input->get_input_type_by_name(p_name) != p_input->get_input_type_by_name(prev_name);
  2760. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2761. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  2762. undo_redo->add_do_method(p_input.ptr(), "set_input_name", p_name);
  2763. undo_redo->add_undo_method(p_input.ptr(), "set_input_name", prev_name);
  2764. // update output port
  2765. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  2766. VisualShader::Type type = VisualShader::Type(type_id);
  2767. int id = visual_shader->find_node_id(type, p_input);
  2768. if (id != VisualShader::NODE_ID_INVALID) {
  2769. if (type_changed) {
  2770. List<VisualShader::Connection> conns;
  2771. visual_shader->get_node_connections(type, &conns);
  2772. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2773. if (E->get().from_node == id) {
  2774. 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))) {
  2775. 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);
  2776. 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);
  2777. continue;
  2778. }
  2779. 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);
  2780. 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);
  2781. 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);
  2782. 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);
  2783. }
  2784. }
  2785. }
  2786. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  2787. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  2788. break;
  2789. }
  2790. }
  2791. undo_redo->commit_action();
  2792. }
  2793. void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform_ref, String p_name) {
  2794. String prev_name = p_uniform_ref->get_uniform_name();
  2795. if (p_name == prev_name) {
  2796. return;
  2797. }
  2798. bool type_changed = p_uniform_ref->get_uniform_type_by_name(p_name) != p_uniform_ref->get_uniform_type_by_name(prev_name);
  2799. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2800. undo_redo->create_action(TTR("UniformRef Name Changed"));
  2801. undo_redo->add_do_method(p_uniform_ref.ptr(), "set_uniform_name", p_name);
  2802. undo_redo->add_undo_method(p_uniform_ref.ptr(), "set_uniform_name", prev_name);
  2803. // update output port
  2804. for (int type_id = 0; type_id < VisualShader::TYPE_MAX; type_id++) {
  2805. VisualShader::Type type = VisualShader::Type(type_id);
  2806. int id = visual_shader->find_node_id(type, p_uniform_ref);
  2807. if (id != VisualShader::NODE_ID_INVALID) {
  2808. if (type_changed) {
  2809. List<VisualShader::Connection> conns;
  2810. visual_shader->get_node_connections(type, &conns);
  2811. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2812. if (E->get().from_node == id) {
  2813. 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))) {
  2814. continue;
  2815. }
  2816. 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);
  2817. 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);
  2818. 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);
  2819. 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);
  2820. }
  2821. }
  2822. }
  2823. undo_redo->add_do_method(graph_plugin.ptr(), "update_node", type_id, id);
  2824. undo_redo->add_undo_method(graph_plugin.ptr(), "update_node", type_id, id);
  2825. break;
  2826. }
  2827. }
  2828. undo_redo->commit_action();
  2829. }
  2830. void VisualShaderEditor::_float_constant_selected(int p_index, int p_node) {
  2831. if (p_index == 0) {
  2832. graph_plugin->update_node_size(p_node);
  2833. return;
  2834. }
  2835. --p_index;
  2836. ERR_FAIL_INDEX(p_index, MAX_FLOAT_CONST_DEFS);
  2837. VisualShader::Type type = get_current_shader_type();
  2838. Ref<VisualShaderNodeFloatConstant> node = visual_shader->get_node(type, p_node);
  2839. if (!node.is_valid()) {
  2840. return;
  2841. }
  2842. undo_redo->create_action(TTR("Set constant"));
  2843. undo_redo->add_do_method(node.ptr(), "set_constant", float_constant_defs[p_index].value);
  2844. undo_redo->add_undo_method(node.ptr(), "set_constant", node->get_constant());
  2845. undo_redo->add_do_method(graph_plugin.ptr(), "update_constant", type, p_node);
  2846. undo_redo->add_undo_method(graph_plugin.ptr(), "update_constant", type, p_node);
  2847. undo_redo->commit_action();
  2848. }
  2849. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  2850. _update_options_menu();
  2851. }
  2852. void VisualShaderEditor::_member_selected() {
  2853. TreeItem *item = members->get_selected();
  2854. if (item != nullptr && item->has_meta("id")) {
  2855. members_dialog->get_ok_button()->set_disabled(false);
  2856. highend_label->set_visible(add_options[item->get_meta("id")].highend);
  2857. node_desc->set_text(_get_description(item->get_meta("id")));
  2858. } else {
  2859. highend_label->set_visible(false);
  2860. members_dialog->get_ok_button()->set_disabled(true);
  2861. node_desc->set_text("");
  2862. }
  2863. }
  2864. void VisualShaderEditor::_member_unselected() {
  2865. }
  2866. void VisualShaderEditor::_member_create() {
  2867. TreeItem *item = members->get_selected();
  2868. if (item != nullptr && item->has_meta("id")) {
  2869. int idx = members->get_selected()->get_meta("id");
  2870. _add_node(idx, add_options[idx].sub_func);
  2871. members_dialog->hide();
  2872. }
  2873. }
  2874. void VisualShaderEditor::_member_cancel() {
  2875. to_node = -1;
  2876. to_slot = -1;
  2877. from_node = -1;
  2878. from_slot = -1;
  2879. }
  2880. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  2881. TreeItem *category = members->get_root()->get_first_child();
  2882. switch (p_idx) {
  2883. case EXPAND_ALL:
  2884. while (category) {
  2885. category->set_collapsed(false);
  2886. TreeItem *sub_category = category->get_first_child();
  2887. while (sub_category) {
  2888. sub_category->set_collapsed(false);
  2889. sub_category = sub_category->get_next();
  2890. }
  2891. category = category->get_next();
  2892. }
  2893. break;
  2894. case COLLAPSE_ALL:
  2895. while (category) {
  2896. category->set_collapsed(true);
  2897. TreeItem *sub_category = category->get_first_child();
  2898. while (sub_category) {
  2899. sub_category->set_collapsed(true);
  2900. sub_category = sub_category->get_next();
  2901. }
  2902. category = category->get_next();
  2903. }
  2904. break;
  2905. default:
  2906. break;
  2907. }
  2908. }
  2909. void VisualShaderEditor::_node_menu_id_pressed(int p_idx) {
  2910. switch (p_idx) {
  2911. case NodeMenuOptions::ADD:
  2912. _show_members_dialog(true);
  2913. break;
  2914. case NodeMenuOptions::COPY:
  2915. _copy_nodes();
  2916. break;
  2917. case NodeMenuOptions::PASTE:
  2918. _paste_nodes(true, menu_point);
  2919. break;
  2920. case NodeMenuOptions::DELETE:
  2921. _delete_nodes_request();
  2922. break;
  2923. case NodeMenuOptions::DUPLICATE:
  2924. _duplicate_nodes();
  2925. break;
  2926. case NodeMenuOptions::CONVERT_CONSTANTS_TO_UNIFORMS:
  2927. _convert_constants_to_uniforms(false);
  2928. break;
  2929. case NodeMenuOptions::CONVERT_UNIFORMS_TO_CONSTANTS:
  2930. _convert_constants_to_uniforms(true);
  2931. break;
  2932. case NodeMenuOptions::SET_COMMENT_TITLE:
  2933. _comment_title_popup_show(get_global_mouse_position(), selected_comment);
  2934. break;
  2935. case NodeMenuOptions::SET_COMMENT_DESCRIPTION:
  2936. _comment_desc_popup_show(get_global_mouse_position(), selected_comment);
  2937. break;
  2938. default:
  2939. break;
  2940. }
  2941. }
  2942. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2943. if (p_from == members) {
  2944. TreeItem *it = members->get_item_at_position(p_point);
  2945. if (!it) {
  2946. return Variant();
  2947. }
  2948. if (!it->has_meta("id")) {
  2949. return Variant();
  2950. }
  2951. int id = it->get_meta("id");
  2952. AddOption op = add_options[id];
  2953. Dictionary d;
  2954. d["id"] = id;
  2955. if (op.sub_func == -1) {
  2956. d["sub_func"] = op.sub_func_str;
  2957. } else {
  2958. d["sub_func"] = op.sub_func;
  2959. }
  2960. Label *label = memnew(Label);
  2961. label->set_text(it->get_text(0));
  2962. set_drag_preview(label);
  2963. return d;
  2964. }
  2965. return Variant();
  2966. }
  2967. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2968. if (p_from == graph) {
  2969. Dictionary d = p_data;
  2970. if (d.has("id")) {
  2971. return true;
  2972. }
  2973. if (d.has("files")) {
  2974. return true;
  2975. }
  2976. }
  2977. return false;
  2978. }
  2979. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2980. if (p_from == graph) {
  2981. Dictionary d = p_data;
  2982. if (d.has("id")) {
  2983. int idx = d["id"];
  2984. saved_node_pos = p_point;
  2985. saved_node_pos_dirty = true;
  2986. _add_node(idx, add_options[idx].sub_func);
  2987. } else if (d.has("files")) {
  2988. undo_redo->create_action(TTR("Add Node(s) to Visual Shader"));
  2989. if (d["files"].get_type() == Variant::PACKED_STRING_ARRAY) {
  2990. PackedStringArray arr = d["files"];
  2991. for (int i = 0; i < arr.size(); i++) {
  2992. String type = ResourceLoader::get_resource_type(arr[i]);
  2993. if (type == "GDScript") {
  2994. Ref<Script> script = ResourceLoader::load(arr[i]);
  2995. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  2996. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  2997. saved_node_pos_dirty = true;
  2998. int idx = -1;
  2999. for (int j = custom_node_option_idx; j < add_options.size(); j++) {
  3000. if (add_options[j].script.is_valid()) {
  3001. if (add_options[j].script->get_path() == arr[i]) {
  3002. idx = j;
  3003. break;
  3004. }
  3005. }
  3006. }
  3007. if (idx != -1) {
  3008. _add_node(idx, -1, arr[i], i);
  3009. }
  3010. }
  3011. } else if (type == "CurveTexture") {
  3012. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  3013. saved_node_pos_dirty = true;
  3014. _add_node(curve_node_option_idx, -1, arr[i], i);
  3015. } else if (ClassDB::get_parent_class(type) == "Texture2D") {
  3016. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  3017. saved_node_pos_dirty = true;
  3018. _add_node(texture2d_node_option_idx, -1, arr[i], i);
  3019. } else if (type == "Texture2DArray") {
  3020. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  3021. saved_node_pos_dirty = true;
  3022. _add_node(texture2d_array_node_option_idx, -1, arr[i], i);
  3023. } else if (ClassDB::get_parent_class(type) == "Texture3D") {
  3024. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  3025. saved_node_pos_dirty = true;
  3026. _add_node(texture3d_node_option_idx, -1, arr[i], i);
  3027. } else if (type == "Cubemap") {
  3028. saved_node_pos = p_point + Vector2(0, i * 250 * EDSCALE);
  3029. saved_node_pos_dirty = true;
  3030. _add_node(cubemap_node_option_idx, -1, arr[i], i);
  3031. }
  3032. }
  3033. }
  3034. undo_redo->commit_action();
  3035. }
  3036. }
  3037. }
  3038. void VisualShaderEditor::_show_preview_text() {
  3039. preview_showed = !preview_showed;
  3040. if (preview_showed) {
  3041. if (preview_first) {
  3042. preview_window->set_size(Size2(400 * EDSCALE, 600 * EDSCALE));
  3043. preview_window->popup_centered();
  3044. preview_first = false;
  3045. } else {
  3046. preview_window->popup();
  3047. }
  3048. _preview_size_changed();
  3049. if (pending_update_preview) {
  3050. _update_preview();
  3051. pending_update_preview = false;
  3052. }
  3053. } else {
  3054. preview_window->hide();
  3055. }
  3056. }
  3057. void VisualShaderEditor::_preview_close_requested() {
  3058. preview_showed = false;
  3059. preview_window->hide();
  3060. preview_shader->set_pressed(false);
  3061. }
  3062. void VisualShaderEditor::_preview_size_changed() {
  3063. preview_vbox->set_custom_minimum_size(preview_window->get_size());
  3064. }
  3065. static ShaderLanguage::DataType _get_global_variable_type(const StringName &p_variable) {
  3066. RS::GlobalVariableType gvt = RS::get_singleton()->global_variable_get_type(p_variable);
  3067. return RS::global_variable_type_get_shader_datatype(gvt);
  3068. }
  3069. void VisualShaderEditor::_update_preview() {
  3070. if (!preview_showed) {
  3071. pending_update_preview = true;
  3072. return;
  3073. }
  3074. String code = visual_shader->get_code();
  3075. preview_text->set_text(code);
  3076. ShaderLanguage sl;
  3077. 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())), ShaderLanguage::VaryingFunctionNames(), ShaderTypes::get_singleton()->get_types(), _get_global_variable_type);
  3078. for (int i = 0; i < preview_text->get_line_count(); i++) {
  3079. preview_text->set_line_background_color(i, Color(0, 0, 0, 0));
  3080. }
  3081. if (err != OK) {
  3082. Color error_line_color = EDITOR_GET("text_editor/highlighting/mark_color");
  3083. preview_text->set_line_background_color(sl.get_error_line() - 1, error_line_color);
  3084. error_panel->show();
  3085. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  3086. error_label->set_text(text);
  3087. shader_error = true;
  3088. } else {
  3089. error_panel->hide();
  3090. shader_error = false;
  3091. }
  3092. }
  3093. void VisualShaderEditor::_visibility_changed() {
  3094. if (!is_visible()) {
  3095. if (preview_window->is_visible()) {
  3096. preview_shader->set_pressed(false);
  3097. preview_window->hide();
  3098. preview_showed = false;
  3099. }
  3100. }
  3101. }
  3102. void VisualShaderEditor::_bind_methods() {
  3103. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  3104. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  3105. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  3106. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  3107. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  3108. ClassDB::bind_method("_uniform_select_item", &VisualShaderEditor::_uniform_select_item);
  3109. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  3110. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  3111. ClassDB::bind_method("_update_uniforms", &VisualShaderEditor::_update_uniforms);
  3112. ClassDB::bind_method("_set_mode", &VisualShaderEditor::_set_mode);
  3113. ClassDB::bind_method("_nodes_dragged", &VisualShaderEditor::_nodes_dragged);
  3114. ClassDB::bind_method("_float_constant_selected", &VisualShaderEditor::_float_constant_selected);
  3115. ClassDB::bind_method("_update_constant", &VisualShaderEditor::_update_constant);
  3116. ClassDB::bind_method("_update_uniform", &VisualShaderEditor::_update_uniform);
  3117. ClassDB::bind_method("_expand_output_port", &VisualShaderEditor::_expand_output_port);
  3118. ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  3119. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  3120. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  3121. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  3122. }
  3123. VisualShaderEditor *VisualShaderEditor::singleton = nullptr;
  3124. VisualShaderEditor::VisualShaderEditor() {
  3125. singleton = this;
  3126. updating = false;
  3127. saved_node_pos_dirty = false;
  3128. saved_node_pos = Point2(0, 0);
  3129. ShaderLanguage::get_keyword_list(&keyword_list);
  3130. pending_update_preview = false;
  3131. shader_error = false;
  3132. to_node = -1;
  3133. to_slot = -1;
  3134. from_node = -1;
  3135. from_slot = -1;
  3136. graph = memnew(GraphEdit);
  3137. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  3138. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  3139. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  3140. add_child(graph);
  3141. graph->set_drag_forwarding(this);
  3142. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  3143. graph->set_minimap_opacity(graph_minimap_opacity);
  3144. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  3145. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR_INT);
  3146. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  3147. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  3148. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  3149. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  3150. //graph->add_valid_left_disconnect_type(0);
  3151. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  3152. graph->connect("connection_request", callable_mp(this, &VisualShaderEditor::_connection_request), varray(), CONNECT_DEFERRED);
  3153. graph->connect("disconnection_request", callable_mp(this, &VisualShaderEditor::_disconnection_request), varray(), CONNECT_DEFERRED);
  3154. graph->connect("node_selected", callable_mp(this, &VisualShaderEditor::_node_selected));
  3155. graph->connect("scroll_offset_changed", callable_mp(this, &VisualShaderEditor::_scroll_changed));
  3156. graph->connect("duplicate_nodes_request", callable_mp(this, &VisualShaderEditor::_duplicate_nodes));
  3157. graph->connect("copy_nodes_request", callable_mp(this, &VisualShaderEditor::_copy_nodes));
  3158. graph->connect("paste_nodes_request", callable_mp(this, &VisualShaderEditor::_paste_nodes), varray(false, Point2()));
  3159. graph->connect("delete_nodes_request", callable_mp(this, &VisualShaderEditor::_delete_nodes_request));
  3160. graph->connect("gui_input", callable_mp(this, &VisualShaderEditor::_graph_gui_input));
  3161. graph->connect("connection_to_empty", callable_mp(this, &VisualShaderEditor::_connection_to_empty));
  3162. graph->connect("connection_from_empty", callable_mp(this, &VisualShaderEditor::_connection_from_empty));
  3163. graph->connect("visibility_changed", callable_mp(this, &VisualShaderEditor::_visibility_changed));
  3164. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  3165. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  3166. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  3167. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  3168. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  3169. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_SCALAR);
  3170. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_VECTOR);
  3171. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR_INT, VisualShaderNode::PORT_TYPE_BOOLEAN);
  3172. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  3173. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  3174. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  3175. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  3176. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  3177. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR_INT);
  3178. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  3179. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  3180. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  3181. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  3182. VSeparator *vs = memnew(VSeparator);
  3183. graph->get_zoom_hbox()->add_child(vs);
  3184. graph->get_zoom_hbox()->move_child(vs, 0);
  3185. custom_mode_box = memnew(CheckBox);
  3186. custom_mode_box->set_text(TTR("Custom"));
  3187. custom_mode_box->set_pressed(false);
  3188. custom_mode_box->set_visible(false);
  3189. custom_mode_box->connect("toggled", callable_mp(this, &VisualShaderEditor::_custom_mode_toggled));
  3190. edit_type_standard = memnew(OptionButton);
  3191. edit_type_standard->add_item(TTR("Vertex"));
  3192. edit_type_standard->add_item(TTR("Fragment"));
  3193. edit_type_standard->add_item(TTR("Light"));
  3194. edit_type_standard->select(1);
  3195. edit_type_standard->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  3196. edit_type_particles = memnew(OptionButton);
  3197. edit_type_particles->add_item(TTR("Start"));
  3198. edit_type_particles->add_item(TTR("Process"));
  3199. edit_type_particles->add_item(TTR("Collide"));
  3200. edit_type_particles->select(0);
  3201. edit_type_particles->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  3202. edit_type_sky = memnew(OptionButton);
  3203. edit_type_sky->add_item(TTR("Sky"));
  3204. edit_type_sky->select(0);
  3205. edit_type_sky->connect("item_selected", callable_mp(this, &VisualShaderEditor::_mode_selected));
  3206. edit_type = edit_type_standard;
  3207. graph->get_zoom_hbox()->add_child(custom_mode_box);
  3208. graph->get_zoom_hbox()->move_child(custom_mode_box, 0);
  3209. graph->get_zoom_hbox()->add_child(edit_type_standard);
  3210. graph->get_zoom_hbox()->move_child(edit_type_standard, 0);
  3211. graph->get_zoom_hbox()->add_child(edit_type_particles);
  3212. graph->get_zoom_hbox()->move_child(edit_type_particles, 0);
  3213. graph->get_zoom_hbox()->add_child(edit_type_sky);
  3214. graph->get_zoom_hbox()->move_child(edit_type_sky, 0);
  3215. add_node = memnew(Button);
  3216. add_node->set_flat(true);
  3217. graph->get_zoom_hbox()->add_child(add_node);
  3218. add_node->set_text(TTR("Add Node..."));
  3219. graph->get_zoom_hbox()->move_child(add_node, 0);
  3220. add_node->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_members_dialog), varray(false));
  3221. preview_shader = memnew(Button);
  3222. preview_shader->set_flat(true);
  3223. preview_shader->set_toggle_mode(true);
  3224. preview_shader->set_tooltip(TTR("Show generated shader code."));
  3225. graph->get_zoom_hbox()->add_child(preview_shader);
  3226. preview_shader->connect("pressed", callable_mp(this, &VisualShaderEditor::_show_preview_text));
  3227. ///////////////////////////////////////
  3228. // PREVIEW WINDOW
  3229. ///////////////////////////////////////
  3230. preview_window = memnew(Window);
  3231. preview_window->set_title(TTR("Generated shader code"));
  3232. preview_window->set_visible(preview_showed);
  3233. preview_window->connect("close_requested", callable_mp(this, &VisualShaderEditor::_preview_close_requested));
  3234. preview_window->connect("size_changed", callable_mp(this, &VisualShaderEditor::_preview_size_changed));
  3235. add_child(preview_window);
  3236. preview_vbox = memnew(VBoxContainer);
  3237. preview_window->add_child(preview_vbox);
  3238. preview_vbox->add_theme_constant_override("separation", 0);
  3239. preview_text = memnew(CodeEdit);
  3240. syntax_highlighter.instantiate();
  3241. preview_vbox->add_child(preview_text);
  3242. preview_text->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3243. preview_text->set_syntax_highlighter(syntax_highlighter);
  3244. preview_text->set_draw_line_numbers(true);
  3245. preview_text->set_readonly(true);
  3246. error_panel = memnew(PanelContainer);
  3247. preview_vbox->add_child(error_panel);
  3248. error_panel->set_visible(false);
  3249. error_label = memnew(Label);
  3250. error_panel->add_child(error_label);
  3251. error_label->set_autowrap(true);
  3252. ///////////////////////////////////////
  3253. // POPUP MENU
  3254. ///////////////////////////////////////
  3255. popup_menu = memnew(PopupMenu);
  3256. add_child(popup_menu);
  3257. popup_menu->add_item(TTR("Add Node"), NodeMenuOptions::ADD);
  3258. popup_menu->add_separator();
  3259. popup_menu->add_item(TTR("Copy"), NodeMenuOptions::COPY);
  3260. popup_menu->add_item(TTR("Paste"), NodeMenuOptions::PASTE);
  3261. popup_menu->add_item(TTR("Delete"), NodeMenuOptions::DELETE);
  3262. popup_menu->add_item(TTR("Duplicate"), NodeMenuOptions::DUPLICATE);
  3263. popup_menu->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_node_menu_id_pressed));
  3264. ///////////////////////////////////////
  3265. // SHADER NODES TREE
  3266. ///////////////////////////////////////
  3267. VBoxContainer *members_vb = memnew(VBoxContainer);
  3268. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  3269. HBoxContainer *filter_hb = memnew(HBoxContainer);
  3270. members_vb->add_child(filter_hb);
  3271. node_filter = memnew(LineEdit);
  3272. filter_hb->add_child(node_filter);
  3273. node_filter->connect("text_changed", callable_mp(this, &VisualShaderEditor::_member_filter_changed));
  3274. node_filter->connect("gui_input", callable_mp(this, &VisualShaderEditor::_sbox_input));
  3275. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  3276. node_filter->set_placeholder(TTR("Search"));
  3277. tools = memnew(MenuButton);
  3278. filter_hb->add_child(tools);
  3279. tools->set_tooltip(TTR("Options"));
  3280. tools->get_popup()->connect("id_pressed", callable_mp(this, &VisualShaderEditor::_tools_menu_option));
  3281. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  3282. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  3283. members = memnew(Tree);
  3284. members_vb->add_child(members);
  3285. members->set_drag_forwarding(this);
  3286. members->set_h_size_flags(SIZE_EXPAND_FILL);
  3287. members->set_v_size_flags(SIZE_EXPAND_FILL);
  3288. members->set_hide_root(true);
  3289. members->set_allow_reselect(true);
  3290. members->set_hide_folding(false);
  3291. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  3292. members->connect("item_activated", callable_mp(this, &VisualShaderEditor::_member_create));
  3293. members->connect("item_selected", callable_mp(this, &VisualShaderEditor::_member_selected));
  3294. members->connect("nothing_selected", callable_mp(this, &VisualShaderEditor::_member_unselected));
  3295. HBoxContainer *desc_hbox = memnew(HBoxContainer);
  3296. members_vb->add_child(desc_hbox);
  3297. Label *desc_label = memnew(Label);
  3298. desc_hbox->add_child(desc_label);
  3299. desc_label->set_text(TTR("Description:"));
  3300. desc_hbox->add_spacer();
  3301. highend_label = memnew(Label);
  3302. desc_hbox->add_child(highend_label);
  3303. highend_label->set_visible(false);
  3304. highend_label->set_text("Vulkan");
  3305. highend_label->set_mouse_filter(Control::MOUSE_FILTER_STOP);
  3306. highend_label->set_tooltip(TTR("High-end node"));
  3307. node_desc = memnew(RichTextLabel);
  3308. members_vb->add_child(node_desc);
  3309. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  3310. node_desc->set_v_size_flags(SIZE_FILL);
  3311. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  3312. members_dialog = memnew(ConfirmationDialog);
  3313. members_dialog->set_title(TTR("Create Shader Node"));
  3314. members_dialog->set_exclusive(false);
  3315. members_dialog->add_child(members_vb);
  3316. members_dialog->get_ok_button()->set_text(TTR("Create"));
  3317. members_dialog->get_ok_button()->connect("pressed", callable_mp(this, &VisualShaderEditor::_member_create));
  3318. members_dialog->get_ok_button()->set_disabled(true);
  3319. members_dialog->connect("cancelled", callable_mp(this, &VisualShaderEditor::_member_cancel));
  3320. add_child(members_dialog);
  3321. alert = memnew(AcceptDialog);
  3322. alert->get_label()->set_autowrap(true);
  3323. alert->get_label()->set_align(Label::ALIGN_CENTER);
  3324. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  3325. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  3326. add_child(alert);
  3327. comment_title_change_popup = memnew(PopupPanel);
  3328. comment_title_change_edit = memnew(LineEdit);
  3329. comment_title_change_edit->set_expand_to_text_length_enabled(true);
  3330. comment_title_change_edit->connect("text_changed", callable_mp(this, &VisualShaderEditor::_comment_title_text_changed));
  3331. comment_title_change_edit->connect("text_submitted", callable_mp(this, &VisualShaderEditor::_comment_title_text_submitted));
  3332. comment_title_change_popup->add_child(comment_title_change_edit);
  3333. comment_title_change_edit->set_size(Size2(-1, -1));
  3334. comment_title_change_popup->set_size(Size2(-1, -1));
  3335. comment_title_change_popup->connect("focus_exited", callable_mp(this, &VisualShaderEditor::_comment_title_popup_focus_out));
  3336. comment_title_change_popup->connect("popup_hide", callable_mp(this, &VisualShaderEditor::_comment_title_popup_hide));
  3337. add_child(comment_title_change_popup);
  3338. comment_desc_change_popup = memnew(PopupPanel);
  3339. VBoxContainer *comment_desc_vbox = memnew(VBoxContainer);
  3340. comment_desc_change_popup->add_child(comment_desc_vbox);
  3341. comment_desc_change_edit = memnew(TextEdit);
  3342. comment_desc_change_edit->connect("text_changed", callable_mp(this, &VisualShaderEditor::_comment_desc_text_changed));
  3343. comment_desc_vbox->add_child(comment_desc_change_edit);
  3344. comment_desc_change_edit->set_custom_minimum_size(Size2(300 * EDSCALE, 150 * EDSCALE));
  3345. comment_desc_change_edit->set_size(Size2(-1, -1));
  3346. comment_desc_change_popup->set_size(Size2(-1, -1));
  3347. comment_desc_change_popup->connect("focus_exited", callable_mp(this, &VisualShaderEditor::_comment_desc_confirm));
  3348. comment_desc_change_popup->connect("popup_hide", callable_mp(this, &VisualShaderEditor::_comment_desc_popup_hide));
  3349. Button *comment_desc_confirm_button = memnew(Button);
  3350. comment_desc_confirm_button->set_text(TTR("OK"));
  3351. comment_desc_vbox->add_child(comment_desc_confirm_button);
  3352. comment_desc_confirm_button->connect("pressed", callable_mp(this, &VisualShaderEditor::_comment_desc_confirm));
  3353. add_child(comment_desc_change_popup);
  3354. ///////////////////////////////////////
  3355. // SHADER NODES TREE OPTIONS
  3356. ///////////////////////////////////////
  3357. // COLOR
  3358. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3359. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3360. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  3361. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  3362. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  3363. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  3364. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  3365. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  3366. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  3367. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  3368. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  3369. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  3370. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  3371. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  3372. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  3373. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  3374. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  3375. // CONDITIONAL
  3376. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  3377. add_options.push_back(AddOption("Equal", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3378. add_options.push_back(AddOption("GreaterThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3379. 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));
  3380. 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));
  3381. 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));
  3382. 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));
  3383. add_options.push_back(AddOption("LessThan", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3384. 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));
  3385. add_options.push_back(AddOption("NotEqual", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3386. add_options.push_back(AddOption("Switch", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated vector if the provided boolean value is true or false."), VisualShaderNodeSwitch::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  3387. add_options.push_back(AddOption("SwitchBool", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated boolean if the provided boolean value is true or false."), VisualShaderNodeSwitch::OP_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3388. add_options.push_back(AddOption("SwitchFloat", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated floating-point scalar if the provided boolean value is true or false."), VisualShaderNodeSwitch::OP_TYPE_FLOAT, VisualShaderNode::PORT_TYPE_SCALAR));
  3389. add_options.push_back(AddOption("SwitchInt", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated integer scalar if the provided boolean value is true or false."), VisualShaderNodeSwitch::OP_TYPE_INT, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3390. add_options.push_back(AddOption("SwitchTransform", "Conditional", "Functions", "VisualShaderNodeSwitch", TTR("Returns an associated transform if the provided boolean value is true or false."), VisualShaderNodeSwitch::OP_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3391. 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));
  3392. 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));
  3393. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3394. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  3395. // INPUT
  3396. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  3397. // SPATIAL-FOR-ALL
  3398. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  3399. 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));
  3400. 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));
  3401. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  3402. 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));
  3403. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  3404. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  3405. 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));
  3406. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  3407. // CANVASITEM-FOR-ALL
  3408. 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));
  3409. 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));
  3410. 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));
  3411. 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));
  3412. 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));
  3413. // PARTICLES-FOR-ALL
  3414. add_options.push_back(AddOption("Active", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "active"), "active", VisualShaderNode::PORT_TYPE_BOOLEAN, -1, Shader::MODE_PARTICLES));
  3415. add_options.push_back(AddOption("Alpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_PARTICLES));
  3416. add_options.push_back(AddOption("AttractorForce", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "attractor_force"), "attractor_force", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_PARTICLES));
  3417. add_options.push_back(AddOption("Color", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_PARTICLES));
  3418. add_options.push_back(AddOption("Custom", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_PARTICLES));
  3419. add_options.push_back(AddOption("CustomAlpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_PARTICLES));
  3420. add_options.push_back(AddOption("Delta", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_PARTICLES));
  3421. add_options.push_back(AddOption("EmissionTransform", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_PARTICLES));
  3422. add_options.push_back(AddOption("Index", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR_INT, -1, Shader::MODE_PARTICLES));
  3423. add_options.push_back(AddOption("LifeTime", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_PARTICLES));
  3424. add_options.push_back(AddOption("Restart", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "restart"), "restart", VisualShaderNode::PORT_TYPE_BOOLEAN, -1, Shader::MODE_PARTICLES));
  3425. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_PARTICLES));
  3426. add_options.push_back(AddOption("Transform", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_PARTICLES));
  3427. add_options.push_back(AddOption("Velocity", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_PARTICLES));
  3428. /////////////////
  3429. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  3430. // SPATIAL INPUTS
  3431. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  3432. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  3433. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  3434. const String input_param_for_sky_shader_mode = TTR("'%s' input parameter for sky shader mode.");
  3435. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  3436. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  3437. const String input_param_for_start_shader_mode = TTR("'%s' input parameter for start shader mode.");
  3438. const String input_param_for_process_shader_mode = TTR("'%s' input parameter for process shader mode.");
  3439. const String input_param_for_collide_shader_mode = TTR("'%s' input parameter for collide shader mode.");
  3440. const String input_param_for_start_and_process_shader_mode = TTR("'%s' input parameter for start and process shader modes.");
  3441. const String input_param_for_process_and_collide_shader_mode = TTR("'%s' input parameter for process and collide shader modes.");
  3442. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader modes.");
  3443. 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));
  3444. 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));
  3445. 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));
  3446. 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));
  3447. 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));
  3448. 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));
  3449. 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));
  3450. 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));
  3451. 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));
  3452. 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));
  3453. 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));
  3454. 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));
  3455. 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));
  3456. 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));
  3457. 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));
  3458. 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));
  3459. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  3460. 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));
  3461. 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));
  3462. 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));
  3463. 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));
  3464. 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));
  3465. 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));
  3466. 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));
  3467. add_options.push_back(AddOption("Backlight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "backlight"), "backlight", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_SPATIAL));
  3468. 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));
  3469. 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));
  3470. 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));
  3471. 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));
  3472. 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));
  3473. 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));
  3474. 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));
  3475. 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));
  3476. 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));
  3477. 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));
  3478. // CANVASITEM INPUTS
  3479. add_options.push_back(AddOption("AtLightPass", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "at_light_pass"), "at_light_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  3480. 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));
  3481. 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));
  3482. 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));
  3483. 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));
  3484. 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));
  3485. 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));
  3486. add_options.push_back(AddOption("SpecularShininess", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "specular_shininess"), "specular_shininess", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  3487. add_options.push_back(AddOption("SpecularShininessAlpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "specular_shininess_alpha"), "specular_shininess_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  3488. add_options.push_back(AddOption("SpecularShininessTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "specular_shininess_texture"), "specular_shininess_texture", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  3489. 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));
  3490. 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));
  3491. 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_CANVAS_ITEM));
  3492. 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));
  3493. 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));
  3494. add_options.push_back(AddOption("LightColorAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color_alpha"), "light_color_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3495. add_options.push_back(AddOption("LightPosition", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_position"), "light_position", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3496. add_options.push_back(AddOption("LightVertex", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vertex"), "light_vertex", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3497. 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));
  3498. 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));
  3499. 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));
  3500. 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));
  3501. 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));
  3502. add_options.push_back(AddOption("SpecularShininess", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "specular_shininess"), "specular_shininess", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3503. add_options.push_back(AddOption("SpecularShininessAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "specular_shininess_alpha"), "specular_shininess_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3504. 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));
  3505. add_options.push_back(AddOption("AtLightPass", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "at_light_pass"), "at_light_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  3506. add_options.push_back(AddOption("Canvas", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "canvas"), "canvas", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  3507. 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));
  3508. add_options.push_back(AddOption("Screen", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "screen"), "screen", VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_CANVAS_ITEM));
  3509. 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));
  3510. 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));
  3511. // SKY INPUTS
  3512. add_options.push_back(AddOption("AtCubeMapPass", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "at_cubemap_pass"), "at_cubemap_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3513. add_options.push_back(AddOption("AtHalfResPass", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "at_half_res_pass"), "at_half_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3514. add_options.push_back(AddOption("AtQuarterResPass", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "at_quarter_res_pass"), "at_quarter_res_pass", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3515. add_options.push_back(AddOption("EyeDir", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "eyedir"), "eyedir", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3516. add_options.push_back(AddOption("HalfResColor", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "half_res_color"), "half_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3517. add_options.push_back(AddOption("HalfResAlpha", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "half_res_alpha"), "half_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3518. add_options.push_back(AddOption("Light0Color", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light0_color"), "light0_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3519. add_options.push_back(AddOption("Light0Direction", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light0_direction"), "light0_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3520. add_options.push_back(AddOption("Light0Enabled", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light0_enabled"), "light0_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3521. add_options.push_back(AddOption("Light0Energy", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light0_energy"), "light0_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3522. add_options.push_back(AddOption("Light1Color", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light1_color"), "light1_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3523. add_options.push_back(AddOption("Light1Direction", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light1_direction"), "light1_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3524. add_options.push_back(AddOption("Light1Enabled", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light1_enabled"), "light1_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3525. add_options.push_back(AddOption("Light1Energy", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light1_energy"), "light1_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3526. add_options.push_back(AddOption("Light2Color", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light2_color"), "light2_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3527. add_options.push_back(AddOption("Light2Direction", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light2_direction"), "light2_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3528. add_options.push_back(AddOption("Light2Enabled", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light2_enabled"), "light2_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3529. add_options.push_back(AddOption("Light2Energy", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light2_energy"), "light2_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3530. add_options.push_back(AddOption("Light3Color", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light3_color"), "light3_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3531. add_options.push_back(AddOption("Light3Direction", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light3_direction"), "light3_direction", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3532. add_options.push_back(AddOption("Light3Enabled", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light3_enabled"), "light3_enabled", VisualShaderNode::PORT_TYPE_BOOLEAN, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3533. add_options.push_back(AddOption("Light3Energy", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "light3_energy"), "light3_energy", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3534. add_options.push_back(AddOption("Position", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "position"), "position", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3535. add_options.push_back(AddOption("QuarterResColor", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "quarter_res_color"), "quarter_res_color", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3536. add_options.push_back(AddOption("QuarterResAlpha", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "quarter_res_alpha"), "quarter_res_alpha", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3537. add_options.push_back(AddOption("Radiance", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "radiance"), "radiance", VisualShaderNode::PORT_TYPE_SAMPLER, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3538. add_options.push_back(AddOption("ScreenUV", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3539. add_options.push_back(AddOption("SkyCoords", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "sky_coords"), "sky_coords", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3540. add_options.push_back(AddOption("Time", "Input", "Sky", "VisualShaderNodeInput", vformat(input_param_for_sky_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_SKY, Shader::MODE_SKY));
  3541. // PARTICLES
  3542. add_options.push_back(AddOption("CollisionDepth", "Input", "Collide", "VisualShaderNodeInput", vformat(input_param_for_collide_shader_mode, "collision_depth"), "collision_depth", VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_COLLIDE, Shader::MODE_PARTICLES));
  3543. add_options.push_back(AddOption("CollisionNormal", "Input", "Collide", "VisualShaderNodeInput", vformat(input_param_for_collide_shader_mode, "collision_normal"), "collision_normal", VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_COLLIDE, Shader::MODE_PARTICLES));
  3544. add_options.push_back(AddOption("EmitParticle", "Particles", "", "VisualShaderNodeParticleEmit", "", -1, -1, -1, Shader::MODE_PARTICLES));
  3545. add_options.push_back(AddOption("ParticleAccelerator", "Particles", "", "VisualShaderNodeParticleAccelerator", "", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_PROCESS, Shader::MODE_PARTICLES));
  3546. add_options.push_back(AddOption("ParticleRandomness", "Particles", "", "VisualShaderNodeParticleRandomness", "", -1, VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_EMIT | TYPE_FLAGS_PROCESS | TYPE_FLAGS_COLLIDE, Shader::MODE_PARTICLES));
  3547. add_options.push_back(AddOption("MultiplyByAxisAngle", "Particles", "Transform", "VisualShaderNodeParticleMultiplyByAxisAngle", "A node for help to multiply a position input vector by rotation using specific axis. Intended to work with emitters.", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT | TYPE_FLAGS_PROCESS | TYPE_FLAGS_COLLIDE, Shader::MODE_PARTICLES));
  3548. add_options.push_back(AddOption("BoxEmitter", "Particles", "Emitters", "VisualShaderNodeParticleBoxEmitter", "", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  3549. add_options.push_back(AddOption("RingEmitter", "Particles", "Emitters", "VisualShaderNodeParticleRingEmitter", "", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  3550. add_options.push_back(AddOption("SphereEmitter", "Particles", "Emitters", "VisualShaderNodeParticleSphereEmitter", "", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  3551. add_options.push_back(AddOption("ConeVelocity", "Particles", "Velocity", "VisualShaderNodeParticleConeVelocity", "", -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_EMIT, Shader::MODE_PARTICLES));
  3552. // SCALAR
  3553. add_options.push_back(AddOption("FloatFunc", "Scalar", "Common", "VisualShaderNodeFloatFunc", TTR("Float function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3554. add_options.push_back(AddOption("IntFunc", "Scalar", "Common", "VisualShaderNodeIntFunc", TTR("Integer function."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3555. add_options.push_back(AddOption("FloatOp", "Scalar", "Common", "VisualShaderNodeFloatOp", TTR("Float operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3556. add_options.push_back(AddOption("IntOp", "Scalar", "Common", "VisualShaderNodeIntOp", TTR("Integer operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3557. //CONSTANTS
  3558. for (int i = 0; i < MAX_FLOAT_CONST_DEFS; i++) {
  3559. 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));
  3560. }
  3561. // FUNCTIONS
  3562. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeFloatFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  3563. 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));
  3564. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  3565. 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));
  3566. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  3567. 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));
  3568. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  3569. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeFloatOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  3570. 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));
  3571. 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));
  3572. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeClamp", TTR("Constrains a value to lie between two further values."), VisualShaderNodeClamp::OP_TYPE_FLOAT, VisualShaderNode::PORT_TYPE_SCALAR));
  3573. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeClamp", TTR("Constrains a value to lie between two further values."), VisualShaderNodeClamp::OP_TYPE_INT, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3574. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  3575. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeFloatFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  3576. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeFloatFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  3577. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-e Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  3578. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 Exponential."), VisualShaderNodeFloatFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  3579. 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));
  3580. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeFloatFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  3581. 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));
  3582. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Natural logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  3583. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Base-2 logarithm."), VisualShaderNodeFloatFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  3584. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the greater of two values."), VisualShaderNodeFloatOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  3585. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeFloatOp", TTR("Returns the lesser of two values."), VisualShaderNodeFloatOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  3586. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeMix", TTR("Linear interpolation between two scalars."), VisualShaderNodeMix::OP_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR));
  3587. 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));
  3588. add_options.push_back(AddOption("Negate", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeFloatFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  3589. 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));
  3590. add_options.push_back(AddOption("OneMinus", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 - scalar"), VisualShaderNodeFloatFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  3591. 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));
  3592. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeFloatFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  3593. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("1.0 / scalar"), VisualShaderNodeFloatFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  3594. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeFloatFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  3595. 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));
  3596. 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));
  3597. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  3598. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeIntFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeIntFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3599. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  3600. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeFloatFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  3601. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeFloatFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  3602. add_options.push_back(AddOption("SmoothStep", "Scalar", "Functions", "VisualShaderNodeSmoothStep", 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."), VisualShaderNodeSmoothStep::OP_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR));
  3603. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeStep", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeStep::OP_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR));
  3604. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  3605. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeFloatFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  3606. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeFloatFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeFloatFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  3607. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Sums two floating-point scalars."), VisualShaderNodeFloatOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  3608. add_options.push_back(AddOption("Add", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Sums two integer scalars."), VisualShaderNodeIntOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3609. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Divides two floating-point scalars."), VisualShaderNodeFloatOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  3610. add_options.push_back(AddOption("Divide", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Divides two integer scalars."), VisualShaderNodeIntOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3611. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Multiplies two floating-point scalars."), VisualShaderNodeFloatOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  3612. add_options.push_back(AddOption("Multiply", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Multiplies two integer scalars."), VisualShaderNodeIntOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3613. 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));
  3614. 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));
  3615. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeFloatOp", TTR("Subtracts two floating-point scalars."), VisualShaderNodeFloatOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  3616. add_options.push_back(AddOption("Subtract", "Scalar", "Operators", "VisualShaderNodeIntOp", TTR("Subtracts two integer scalars."), VisualShaderNodeIntOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3617. add_options.push_back(AddOption("FloatConstant", "Scalar", "Variables", "VisualShaderNodeFloatConstant", TTR("Scalar floating-point constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3618. add_options.push_back(AddOption("IntConstant", "Scalar", "Variables", "VisualShaderNodeIntConstant", TTR("Scalar integer constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3619. add_options.push_back(AddOption("FloatUniform", "Scalar", "Variables", "VisualShaderNodeFloatUniform", TTR("Scalar floating-point uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3620. add_options.push_back(AddOption("IntUniform", "Scalar", "Variables", "VisualShaderNodeIntUniform", TTR("Scalar integer uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR_INT));
  3621. // SDF
  3622. {
  3623. add_options.push_back(AddOption("ScreenUVToSDF", "SDF", "", "VisualShaderNodeScreenUVToSDF", TTR("Converts screen UV to a SDF."), -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3624. add_options.push_back(AddOption("SDFRaymarch", "SDF", "", "VisualShaderNodeSDFRaymarch", TTR("Casts a ray against the screen SDF and returns the distance travelled."), -1, -1, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3625. add_options.push_back(AddOption("SDFToScreenUV", "SDF", "", "VisualShaderNodeSDFToScreenUV", TTR("Converts a SDF to screen UV."), -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3626. add_options.push_back(AddOption("TextureSDF", "SDF", "", "VisualShaderNodeTextureSDF", TTR("Performs a SDF texture lookup."), -1, VisualShaderNode::PORT_TYPE_SCALAR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3627. add_options.push_back(AddOption("TextureSDFNormal", "SDF", "", "VisualShaderNodeTextureSDFNormal", TTR("Performs a SDF normal texture lookup."), -1, VisualShaderNode::PORT_TYPE_VECTOR, TYPE_FLAGS_FRAGMENT | TYPE_FLAGS_LIGHT, Shader::MODE_CANVAS_ITEM));
  3628. }
  3629. // TEXTURES
  3630. add_options.push_back(AddOption("UVFunc", "Textures", "Common", "VisualShaderNodeUVFunc", TTR("Function to be applied on texture coordinates."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3631. cubemap_node_option_idx = add_options.size();
  3632. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubemap", TTR("Perform the cubic texture lookup."), -1, -1));
  3633. curve_node_option_idx = add_options.size();
  3634. add_options.push_back(AddOption("CurveTexture", "Textures", "Functions", "VisualShaderNodeCurveTexture", TTR("Perform the curve texture lookup."), -1, -1));
  3635. texture2d_node_option_idx = add_options.size();
  3636. add_options.push_back(AddOption("Texture2D", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the 2D texture lookup."), -1, -1));
  3637. texture2d_array_node_option_idx = add_options.size();
  3638. add_options.push_back(AddOption("Texture2DArray", "Textures", "Functions", "VisualShaderNodeTexture2DArray", TTR("Perform the 2D-array texture lookup."), -1, -1, -1, -1, -1));
  3639. texture3d_node_option_idx = add_options.size();
  3640. add_options.push_back(AddOption("Texture3D", "Textures", "Functions", "VisualShaderNodeTexture3D", TTR("Perform the 3D texture lookup."), -1, -1));
  3641. add_options.push_back(AddOption("UVPanning", "Textures", "Functions", "VisualShaderNodeUVFunc", TTR("Apply panning function on texture coordinates."), VisualShaderNodeUVFunc::FUNC_PANNING, VisualShaderNode::PORT_TYPE_VECTOR));
  3642. add_options.push_back(AddOption("UVScaling", "Textures", "Functions", "VisualShaderNodeUVFunc", TTR("Apply scaling function on texture coordinates."), VisualShaderNodeUVFunc::FUNC_SCALING, VisualShaderNode::PORT_TYPE_VECTOR));
  3643. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubemapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  3644. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  3645. 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));
  3646. add_options.push_back(AddOption("Texture2DArrayUniform", "Textures", "Variables", "VisualShaderNodeTexture2DArrayUniform", TTR("2D array of textures uniform lookup."), -1, -1, -1, -1, -1));
  3647. add_options.push_back(AddOption("Texture3DUniform", "Textures", "Variables", "VisualShaderNodeTexture3DUniform", TTR("3D texture uniform lookup."), -1, -1, -1, -1, -1));
  3648. // TRANSFORM
  3649. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3650. 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));
  3651. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3652. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  3653. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3654. add_options.push_back(AddOption("GetBillboardMatrix", "Transform", "Functions", "VisualShaderNodeBillboard", TTR("Calculates how the object should face the camera to be applied on Model View Matrix output port for 3D objects."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM, TYPE_FLAGS_VERTEX, Shader::MODE_SPATIAL));
  3655. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3656. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3657. add_options.push_back(AddOption("TransformMult", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3658. add_options.push_back(AddOption("TransformVectorMult", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3659. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3660. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  3661. // VECTOR
  3662. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3663. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3664. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3665. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  3666. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  3667. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  3668. 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));
  3669. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  3670. 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));
  3671. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  3672. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  3673. 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));
  3674. 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));
  3675. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeClamp", TTR("Constrains a value to lie between two further values."), VisualShaderNodeClamp::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  3676. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  3677. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  3678. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  3679. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  3680. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3681. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3682. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  3683. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  3684. 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));
  3685. 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));
  3686. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  3687. 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));
  3688. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  3689. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  3690. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  3691. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  3692. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  3693. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeMix", TTR("Linear interpolation between two vectors."), VisualShaderNodeMix::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  3694. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeMix", TTR("Linear interpolation between two vectors using scalar."), VisualShaderNodeMix::OP_TYPE_VECTOR_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR));
  3695. 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));
  3696. add_options.push_back(AddOption("Negate", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  3697. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  3698. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  3699. 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));
  3700. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  3701. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  3702. 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));
  3703. 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));
  3704. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  3705. 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));
  3706. 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));
  3707. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  3708. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  3709. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  3710. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  3711. add_options.push_back(AddOption("SmoothStep", "Vector", "Functions", "VisualShaderNodeSmoothStep", 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."), VisualShaderNodeSmoothStep::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  3712. add_options.push_back(AddOption("SmoothStepS", "Vector", "Functions", "VisualShaderNodeSmoothStep", 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."), VisualShaderNodeSmoothStep::OP_TYPE_VECTOR_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR));
  3713. add_options.push_back(AddOption("Step", "Vector", "Functions", "VisualShaderNodeStep", TTR("Step function( vector(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeStep::OP_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR));
  3714. add_options.push_back(AddOption("StepS", "Vector", "Functions", "VisualShaderNodeStep", TTR("Step function( scalar(edge), vector(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeStep::OP_TYPE_VECTOR_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR));
  3715. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  3716. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  3717. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  3718. add_options.push_back(AddOption("Add", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  3719. add_options.push_back(AddOption("Divide", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  3720. add_options.push_back(AddOption("Multiply", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  3721. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  3722. add_options.push_back(AddOption("Subtract", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  3723. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3724. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  3725. // SPECIAL
  3726. add_options.push_back(AddOption("Comment", "Special", "", "VisualShaderNodeComment", TTR("A rectangular area with a description string for better graph organization.")));
  3727. 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.")));
  3728. 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));
  3729. 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.")));
  3730. add_options.push_back(AddOption("UniformRef", "Special", "", "VisualShaderNodeUniformRef", TTR("A reference to an existing uniform.")));
  3731. 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));
  3732. 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));
  3733. 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));
  3734. 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));
  3735. 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));
  3736. 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));
  3737. 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));
  3738. 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));
  3739. custom_node_option_idx = add_options.size();
  3740. /////////////////////////////////////////////////////////////////////
  3741. _update_options_menu();
  3742. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3743. Ref<VisualShaderNodePluginDefault> default_plugin;
  3744. default_plugin.instantiate();
  3745. add_plugin(default_plugin);
  3746. graph_plugin.instantiate();
  3747. property_editor = memnew(CustomPropertyEditor);
  3748. add_child(property_editor);
  3749. property_editor->connect("variant_changed", callable_mp(this, &VisualShaderEditor::_port_edited));
  3750. }
  3751. /////////////////
  3752. void VisualShaderEditorPlugin::edit(Object *p_object) {
  3753. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  3754. }
  3755. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  3756. return p_object->is_class("VisualShader");
  3757. }
  3758. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  3759. if (p_visible) {
  3760. //editor->hide_animation_player_editors();
  3761. //editor->animation_panel_make_visible(true);
  3762. button->show();
  3763. editor->make_bottom_panel_item_visible(visual_shader_editor);
  3764. visual_shader_editor->update_custom_nodes();
  3765. visual_shader_editor->set_process_input(true);
  3766. //visual_shader_editor->set_process(true);
  3767. } else {
  3768. if (visual_shader_editor->is_visible_in_tree()) {
  3769. editor->hide_bottom_panel();
  3770. }
  3771. button->hide();
  3772. visual_shader_editor->set_process_input(false);
  3773. //visual_shader_editor->set_process(false);
  3774. }
  3775. }
  3776. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  3777. editor = p_node;
  3778. visual_shader_editor = memnew(VisualShaderEditor);
  3779. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  3780. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  3781. button->hide();
  3782. }
  3783. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  3784. }
  3785. ////////////////
  3786. class VisualShaderNodePluginInputEditor : public OptionButton {
  3787. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  3788. Ref<VisualShaderNodeInput> input;
  3789. public:
  3790. void _notification(int p_what) {
  3791. if (p_what == NOTIFICATION_READY) {
  3792. connect("item_selected", callable_mp(this, &VisualShaderNodePluginInputEditor::_item_selected));
  3793. }
  3794. }
  3795. void _item_selected(int p_item) {
  3796. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  3797. }
  3798. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  3799. input = p_input;
  3800. Ref<Texture2D> type_icon[6] = {
  3801. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  3802. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  3803. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  3804. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  3805. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform3D", "EditorIcons"),
  3806. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  3807. };
  3808. add_item("[None]");
  3809. int to_select = -1;
  3810. for (int i = 0; i < input->get_input_index_count(); i++) {
  3811. if (input->get_input_name() == input->get_input_index_name(i)) {
  3812. to_select = i + 1;
  3813. }
  3814. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  3815. }
  3816. if (to_select >= 0) {
  3817. select(to_select);
  3818. }
  3819. }
  3820. };
  3821. ////////////////
  3822. class VisualShaderNodePluginUniformRefEditor : public OptionButton {
  3823. GDCLASS(VisualShaderNodePluginUniformRefEditor, OptionButton);
  3824. Ref<VisualShaderNodeUniformRef> uniform_ref;
  3825. public:
  3826. void _notification(int p_what) {
  3827. if (p_what == NOTIFICATION_READY) {
  3828. connect("item_selected", callable_mp(this, &VisualShaderNodePluginUniformRefEditor::_item_selected));
  3829. }
  3830. }
  3831. void _item_selected(int p_item) {
  3832. VisualShaderEditor::get_singleton()->call_deferred("_uniform_select_item", uniform_ref, get_item_text(p_item));
  3833. }
  3834. void setup(const Ref<VisualShaderNodeUniformRef> &p_uniform_ref) {
  3835. uniform_ref = p_uniform_ref;
  3836. Ref<Texture2D> type_icon[7] = {
  3837. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("float", "EditorIcons"),
  3838. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("int", "EditorIcons"),
  3839. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("bool", "EditorIcons"),
  3840. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Vector3", "EditorIcons"),
  3841. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Transform3D", "EditorIcons"),
  3842. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("Color", "EditorIcons"),
  3843. EditorNode::get_singleton()->get_gui_base()->get_theme_icon("ImageTexture", "EditorIcons"),
  3844. };
  3845. add_item("[None]");
  3846. int to_select = -1;
  3847. for (int i = 0; i < p_uniform_ref->get_uniforms_count(); i++) {
  3848. if (p_uniform_ref->get_uniform_name() == p_uniform_ref->get_uniform_name_by_index(i)) {
  3849. to_select = i + 1;
  3850. }
  3851. add_icon_item(type_icon[p_uniform_ref->get_uniform_type_by_index(i)], p_uniform_ref->get_uniform_name_by_index(i));
  3852. }
  3853. if (to_select >= 0) {
  3854. select(to_select);
  3855. }
  3856. }
  3857. };
  3858. ////////////////
  3859. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  3860. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  3861. Ref<Resource> parent_resource;
  3862. int node_id;
  3863. VisualShader::Type shader_type;
  3864. public:
  3865. void _property_changed(const String &p_property, const Variant &p_value, const String &p_field = "", bool p_changing = false) {
  3866. if (p_changing) {
  3867. return;
  3868. }
  3869. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3870. updating = true;
  3871. undo_redo->create_action(TTR("Edit Visual Property") + ": " + p_property, UndoRedo::MERGE_ENDS);
  3872. undo_redo->add_do_property(node.ptr(), p_property, p_value);
  3873. undo_redo->add_undo_property(node.ptr(), p_property, node->get(p_property));
  3874. if (p_value.get_type() == Variant::OBJECT) {
  3875. RES prev_res = node->get(p_property);
  3876. RES curr_res = p_value;
  3877. if (curr_res.is_null()) {
  3878. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  3879. } else {
  3880. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  3881. }
  3882. if (!prev_res.is_null()) {
  3883. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  3884. } else {
  3885. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  3886. }
  3887. }
  3888. if (p_property != "constant") {
  3889. undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  3890. undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_node_deferred", shader_type, node_id);
  3891. } else {
  3892. undo_redo->add_do_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id);
  3893. undo_redo->add_undo_method(VisualShaderEditor::get_singleton()->get_graph_plugin(), "update_constant", shader_type, node_id);
  3894. }
  3895. undo_redo->commit_action();
  3896. updating = false;
  3897. }
  3898. void _node_changed() {
  3899. if (updating) {
  3900. return;
  3901. }
  3902. for (int i = 0; i < properties.size(); i++) {
  3903. properties[i]->update_property();
  3904. }
  3905. }
  3906. void _resource_selected(const String &p_path, RES p_resource) {
  3907. _open_inspector(p_resource);
  3908. }
  3909. void _open_inspector(RES p_resource) {
  3910. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  3911. }
  3912. bool updating;
  3913. Ref<VisualShaderNode> node;
  3914. Vector<EditorProperty *> properties;
  3915. Vector<Label *> prop_names;
  3916. void _show_prop_names(bool p_show) {
  3917. for (int i = 0; i < prop_names.size(); i++) {
  3918. prop_names[i]->set_visible(p_show);
  3919. }
  3920. }
  3921. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  3922. parent_resource = p_parent_resource;
  3923. updating = false;
  3924. node = p_node;
  3925. properties = p_properties;
  3926. node_id = (int)p_node->get_meta("id");
  3927. shader_type = VisualShader::Type((int)p_node->get_meta("shader_type"));
  3928. for (int i = 0; i < p_properties.size(); i++) {
  3929. HBoxContainer *hbox = memnew(HBoxContainer);
  3930. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3931. add_child(hbox);
  3932. Label *prop_name = memnew(Label);
  3933. String prop_name_str = p_names[i];
  3934. prop_name_str = prop_name_str.capitalize() + ":";
  3935. prop_name->set_text(prop_name_str);
  3936. prop_name->set_visible(false);
  3937. hbox->add_child(prop_name);
  3938. prop_names.push_back(prop_name);
  3939. p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  3940. hbox->add_child(p_properties[i]);
  3941. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  3942. if (res_prop) {
  3943. p_properties[i]->connect("resource_selected", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_resource_selected));
  3944. }
  3945. properties[i]->connect("property_changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_property_changed));
  3946. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  3947. properties[i]->update_property();
  3948. properties[i]->set_name_split_ratio(0);
  3949. }
  3950. node->connect("changed", callable_mp(this, &VisualShaderNodePluginDefaultEditor::_node_changed));
  3951. }
  3952. static void _bind_methods() {
  3953. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector); // Used by UndoRedo.
  3954. ClassDB::bind_method("_show_prop_names", &VisualShaderNodePluginDefaultEditor::_show_prop_names); // Used with call_deferred.
  3955. }
  3956. };
  3957. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  3958. if (p_node->is_class("VisualShaderNodeUniformRef")) {
  3959. //create input
  3960. VisualShaderNodePluginUniformRefEditor *uniform_editor = memnew(VisualShaderNodePluginUniformRefEditor);
  3961. uniform_editor->setup(p_node);
  3962. return uniform_editor;
  3963. }
  3964. if (p_node->is_class("VisualShaderNodeInput")) {
  3965. //create input
  3966. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  3967. input_editor->setup(p_node);
  3968. return input_editor;
  3969. }
  3970. Vector<StringName> properties = p_node->get_editable_properties();
  3971. if (properties.size() == 0) {
  3972. return nullptr;
  3973. }
  3974. List<PropertyInfo> props;
  3975. p_node->get_property_list(&props);
  3976. Vector<PropertyInfo> pinfo;
  3977. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  3978. for (int i = 0; i < properties.size(); i++) {
  3979. if (E->get().name == String(properties[i])) {
  3980. pinfo.push_back(E->get());
  3981. }
  3982. }
  3983. }
  3984. if (pinfo.size() == 0) {
  3985. return nullptr;
  3986. }
  3987. properties.clear();
  3988. Ref<VisualShaderNode> node = p_node;
  3989. Vector<EditorProperty *> editors;
  3990. for (int i = 0; i < pinfo.size(); i++) {
  3991. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  3992. if (!prop) {
  3993. return nullptr;
  3994. }
  3995. if (Object::cast_to<EditorPropertyResource>(prop)) {
  3996. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  3997. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3998. } else if (Object::cast_to<EditorPropertyTransform3D>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  3999. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  4000. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  4001. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  4002. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  4003. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  4004. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  4005. }
  4006. editors.push_back(prop);
  4007. properties.push_back(pinfo[i].name);
  4008. }
  4009. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  4010. editor->setup(p_parent_resource, editors, properties, p_node);
  4011. return editor;
  4012. }
  4013. void EditorPropertyShaderMode::_option_selected(int p_which) {
  4014. //will not use this, instead will do all the logic setting manually
  4015. //emit_signal("property_changed", get_edited_property(), p_which);
  4016. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  4017. if (visual_shader->get_mode() == p_which) {
  4018. return;
  4019. }
  4020. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  4021. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  4022. //do is easy
  4023. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  4024. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  4025. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_set_mode", p_which);
  4026. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_set_mode", visual_shader->get_mode());
  4027. //now undo is hell
  4028. //1. restore connections to output
  4029. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  4030. VisualShader::Type type = VisualShader::Type(i);
  4031. List<VisualShader::Connection> conns;
  4032. visual_shader->get_node_connections(type, &conns);
  4033. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  4034. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  4035. 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);
  4036. }
  4037. }
  4038. }
  4039. //2. restore input indices
  4040. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  4041. VisualShader::Type type = VisualShader::Type(i);
  4042. Vector<int> nodes = visual_shader->get_node_list(type);
  4043. for (int j = 0; j < nodes.size(); j++) {
  4044. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  4045. if (!input.is_valid()) {
  4046. continue;
  4047. }
  4048. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  4049. }
  4050. }
  4051. //3. restore enums and flags
  4052. List<PropertyInfo> props;
  4053. visual_shader->get_property_list(&props);
  4054. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  4055. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  4056. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  4057. }
  4058. }
  4059. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  4060. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  4061. //update graph
  4062. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  4063. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  4064. undo_redo->commit_action();
  4065. }
  4066. void EditorPropertyShaderMode::update_property() {
  4067. int which = get_edited_object()->get(get_edited_property());
  4068. options->select(which);
  4069. }
  4070. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  4071. for (int i = 0; i < p_options.size(); i++) {
  4072. options->add_item(p_options[i], i);
  4073. }
  4074. }
  4075. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  4076. options->set_clip_text(p_enable);
  4077. }
  4078. void EditorPropertyShaderMode::_bind_methods() {
  4079. }
  4080. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  4081. options = memnew(OptionButton);
  4082. options->set_clip_text(true);
  4083. add_child(options);
  4084. add_focusable(options);
  4085. options->connect("item_selected", callable_mp(this, &EditorPropertyShaderMode::_option_selected));
  4086. }
  4087. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  4088. return true; //can handle everything
  4089. }
  4090. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  4091. //do none
  4092. }
  4093. bool EditorInspectorShaderModePlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
  4094. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  4095. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  4096. Vector<String> options = p_hint_text.split(",");
  4097. editor->setup(options);
  4098. add_property_editor(p_path, editor);
  4099. return true;
  4100. }
  4101. return false; //can be overridden, although it will most likely be last anyway
  4102. }
  4103. void EditorInspectorShaderModePlugin::parse_end() {
  4104. //do none
  4105. }
  4106. //////////////////////////////////
  4107. void VisualShaderNodePortPreview::_shader_changed() {
  4108. if (shader.is_null()) {
  4109. return;
  4110. }
  4111. Vector<VisualShader::DefaultTextureParam> default_textures;
  4112. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  4113. Ref<Shader> preview_shader;
  4114. preview_shader.instantiate();
  4115. preview_shader->set_code(shader_code);
  4116. for (int i = 0; i < default_textures.size(); i++) {
  4117. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  4118. }
  4119. Ref<ShaderMaterial> material;
  4120. material.instantiate();
  4121. material->set_shader(preview_shader);
  4122. //find if a material is also being edited and copy parameters to this one
  4123. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  4124. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  4125. ShaderMaterial *src_mat;
  4126. if (!object) {
  4127. continue;
  4128. }
  4129. if (object->has_method("get_material_override")) { // trying getting material from MeshInstance
  4130. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material_override"));
  4131. } else if (object->has_method("get_material")) { // from CanvasItem/Node2D
  4132. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material"));
  4133. } else {
  4134. src_mat = Object::cast_to<ShaderMaterial>(object);
  4135. }
  4136. if (src_mat && src_mat->get_shader().is_valid()) {
  4137. List<PropertyInfo> params;
  4138. src_mat->get_shader()->get_param_list(&params);
  4139. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  4140. material->set(E->get().name, src_mat->get(E->get().name));
  4141. }
  4142. }
  4143. }
  4144. set_material(material);
  4145. }
  4146. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  4147. shader = p_shader;
  4148. shader->connect("changed", callable_mp(this, &VisualShaderNodePortPreview::_shader_changed));
  4149. type = p_type;
  4150. port = p_port;
  4151. node = p_node;
  4152. update();
  4153. _shader_changed();
  4154. }
  4155. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  4156. return Size2(100, 100) * EDSCALE;
  4157. }
  4158. void VisualShaderNodePortPreview::_notification(int p_what) {
  4159. if (p_what == NOTIFICATION_DRAW) {
  4160. Vector<Vector2> points;
  4161. Vector<Vector2> uvs;
  4162. Vector<Color> colors;
  4163. points.push_back(Vector2());
  4164. uvs.push_back(Vector2(0, 0));
  4165. colors.push_back(Color(1, 1, 1, 1));
  4166. points.push_back(Vector2(get_size().width, 0));
  4167. uvs.push_back(Vector2(1, 0));
  4168. colors.push_back(Color(1, 1, 1, 1));
  4169. points.push_back(get_size());
  4170. uvs.push_back(Vector2(1, 1));
  4171. colors.push_back(Color(1, 1, 1, 1));
  4172. points.push_back(Vector2(0, get_size().height));
  4173. uvs.push_back(Vector2(0, 1));
  4174. colors.push_back(Color(1, 1, 1, 1));
  4175. draw_primitive(points, colors, uvs);
  4176. }
  4177. }
  4178. void VisualShaderNodePortPreview::_bind_methods() {
  4179. }
  4180. //////////////////////////////////
  4181. String VisualShaderConversionPlugin::converts_to() const {
  4182. return "Shader";
  4183. }
  4184. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  4185. Ref<VisualShader> vshader = p_resource;
  4186. return vshader.is_valid();
  4187. }
  4188. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  4189. Ref<VisualShader> vshader = p_resource;
  4190. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  4191. Ref<Shader> shader;
  4192. shader.instantiate();
  4193. String code = vshader->get_code();
  4194. shader->set_code(code);
  4195. return shader;
  4196. }