visual_shader_editor_plugin.cpp 254 KB

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