visual_shader.cpp 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856
  1. /*************************************************************************/
  2. /* visual_shader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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.h"
  31. #include "core/templates/vmap.h"
  32. #include "servers/rendering/shader_types.h"
  33. #include "visual_shader_nodes.h"
  34. #include "visual_shader_particle_nodes.h"
  35. #include "visual_shader_sdf_nodes.h"
  36. String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name) {
  37. static const char *typepf[VisualShader::TYPE_MAX] = { "vtx", "frg", "lgt", "start", "process", "collide", "start_custom", "process_custom", "sky", "fog" };
  38. return p_name + "_" + String(typepf[p_type]) + "_" + itos(p_id);
  39. }
  40. bool VisualShaderNode::is_simple_decl() const {
  41. return simple_decl;
  42. }
  43. void VisualShaderNode::set_output_port_for_preview(int p_index) {
  44. port_preview = p_index;
  45. }
  46. int VisualShaderNode::get_output_port_for_preview() const {
  47. return port_preview;
  48. }
  49. void VisualShaderNode::set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value) {
  50. Variant value = p_value;
  51. if (p_prev_value.get_type() != Variant::NIL) {
  52. switch (p_value.get_type()) {
  53. case Variant::FLOAT: {
  54. switch (p_prev_value.get_type()) {
  55. case Variant::INT: {
  56. value = (float)p_prev_value;
  57. } break;
  58. case Variant::FLOAT: {
  59. value = p_prev_value;
  60. } break;
  61. case Variant::VECTOR2: {
  62. Vector2 pv = p_prev_value;
  63. value = pv.x;
  64. } break;
  65. case Variant::VECTOR3: {
  66. Vector3 pv = p_prev_value;
  67. value = pv.x;
  68. } break;
  69. case Variant::QUATERNION: {
  70. Quaternion pv = p_prev_value;
  71. value = pv.x;
  72. } break;
  73. default:
  74. break;
  75. }
  76. } break;
  77. case Variant::INT: {
  78. switch (p_prev_value.get_type()) {
  79. case Variant::INT: {
  80. value = p_prev_value;
  81. } break;
  82. case Variant::FLOAT: {
  83. value = (int)p_prev_value;
  84. } break;
  85. case Variant::VECTOR2: {
  86. Vector2 pv = p_prev_value;
  87. value = (int)pv.x;
  88. } break;
  89. case Variant::VECTOR3: {
  90. Vector3 pv = p_prev_value;
  91. value = (int)pv.x;
  92. } break;
  93. case Variant::QUATERNION: {
  94. Quaternion pv = p_prev_value;
  95. value = (int)pv.x;
  96. } break;
  97. default:
  98. break;
  99. }
  100. } break;
  101. case Variant::VECTOR2: {
  102. switch (p_prev_value.get_type()) {
  103. case Variant::INT: {
  104. float pv = (float)(int)p_prev_value;
  105. value = Vector2(pv, pv);
  106. } break;
  107. case Variant::FLOAT: {
  108. float pv = p_prev_value;
  109. value = Vector2(pv, pv);
  110. } break;
  111. case Variant::VECTOR2: {
  112. value = p_prev_value;
  113. } break;
  114. case Variant::VECTOR3: {
  115. Vector3 pv = p_prev_value;
  116. value = Vector2(pv.x, pv.y);
  117. } break;
  118. case Variant::QUATERNION: {
  119. Quaternion pv = p_prev_value;
  120. value = Vector2(pv.x, pv.y);
  121. } break;
  122. default:
  123. break;
  124. }
  125. } break;
  126. case Variant::VECTOR3: {
  127. switch (p_prev_value.get_type()) {
  128. case Variant::INT: {
  129. float pv = (float)(int)p_prev_value;
  130. value = Vector3(pv, pv, pv);
  131. } break;
  132. case Variant::FLOAT: {
  133. float pv = p_prev_value;
  134. value = Vector3(pv, pv, pv);
  135. } break;
  136. case Variant::VECTOR2: {
  137. Vector2 pv = p_prev_value;
  138. value = Vector3(pv.x, pv.y, pv.y);
  139. } break;
  140. case Variant::VECTOR3: {
  141. value = p_prev_value;
  142. } break;
  143. case Variant::QUATERNION: {
  144. Quaternion pv = p_prev_value;
  145. value = Vector3(pv.x, pv.y, pv.z);
  146. } break;
  147. default:
  148. break;
  149. }
  150. } break;
  151. case Variant::QUATERNION: {
  152. switch (p_prev_value.get_type()) {
  153. case Variant::INT: {
  154. float pv = (float)(int)p_prev_value;
  155. value = Quaternion(pv, pv, pv, pv);
  156. } break;
  157. case Variant::FLOAT: {
  158. float pv = p_prev_value;
  159. value = Quaternion(pv, pv, pv, pv);
  160. } break;
  161. case Variant::VECTOR2: {
  162. Vector2 pv = p_prev_value;
  163. value = Quaternion(pv.x, pv.y, pv.y, pv.y);
  164. } break;
  165. case Variant::VECTOR3: {
  166. Vector3 pv = p_prev_value;
  167. value = Quaternion(pv.x, pv.y, pv.z, pv.z);
  168. } break;
  169. case Variant::QUATERNION: {
  170. value = p_prev_value;
  171. } break;
  172. default:
  173. break;
  174. }
  175. } break;
  176. default:
  177. break;
  178. }
  179. }
  180. default_input_values[p_port] = value;
  181. emit_changed();
  182. }
  183. Variant VisualShaderNode::get_input_port_default_value(int p_port) const {
  184. if (default_input_values.has(p_port)) {
  185. return default_input_values[p_port];
  186. }
  187. return Variant();
  188. }
  189. void VisualShaderNode::remove_input_port_default_value(int p_port) {
  190. if (default_input_values.has(p_port)) {
  191. default_input_values.erase(p_port);
  192. emit_changed();
  193. }
  194. }
  195. void VisualShaderNode::clear_default_input_values() {
  196. if (!default_input_values.is_empty()) {
  197. default_input_values.clear();
  198. emit_changed();
  199. }
  200. }
  201. bool VisualShaderNode::is_port_separator(int p_index) const {
  202. return false;
  203. }
  204. bool VisualShaderNode::is_output_port_connected(int p_port) const {
  205. if (connected_output_ports.has(p_port)) {
  206. return connected_output_ports[p_port] > 0;
  207. }
  208. return false;
  209. }
  210. void VisualShaderNode::set_output_port_connected(int p_port, bool p_connected) {
  211. if (p_connected) {
  212. connected_output_ports[p_port]++;
  213. } else {
  214. connected_output_ports[p_port]--;
  215. }
  216. }
  217. bool VisualShaderNode::is_input_port_connected(int p_port) const {
  218. if (connected_input_ports.has(p_port)) {
  219. return connected_input_ports[p_port];
  220. }
  221. return false;
  222. }
  223. void VisualShaderNode::set_input_port_connected(int p_port, bool p_connected) {
  224. connected_input_ports[p_port] = p_connected;
  225. }
  226. bool VisualShaderNode::is_generate_input_var(int p_port) const {
  227. return true;
  228. }
  229. bool VisualShaderNode::is_output_port_expandable(int p_port) const {
  230. return false;
  231. }
  232. bool VisualShaderNode::has_output_port_preview(int p_port) const {
  233. return true;
  234. }
  235. void VisualShaderNode::_set_output_ports_expanded(const Array &p_values) {
  236. for (int i = 0; i < p_values.size(); i++) {
  237. expanded_output_ports[p_values[i]] = true;
  238. }
  239. emit_changed();
  240. }
  241. Array VisualShaderNode::_get_output_ports_expanded() const {
  242. Array arr;
  243. for (int i = 0; i < get_output_port_count(); i++) {
  244. if (_is_output_port_expanded(i)) {
  245. arr.push_back(i);
  246. }
  247. }
  248. return arr;
  249. }
  250. void VisualShaderNode::_set_output_port_expanded(int p_port, bool p_expanded) {
  251. expanded_output_ports[p_port] = p_expanded;
  252. emit_changed();
  253. }
  254. bool VisualShaderNode::_is_output_port_expanded(int p_port) const {
  255. if (expanded_output_ports.has(p_port)) {
  256. return expanded_output_ports[p_port];
  257. }
  258. return false;
  259. }
  260. int VisualShaderNode::get_expanded_output_port_count() const {
  261. int count = get_output_port_count();
  262. int count2 = count;
  263. for (int i = 0; i < count; i++) {
  264. if (is_output_port_expandable(i) && _is_output_port_expanded(i)) {
  265. switch (get_output_port_type(i)) {
  266. case PORT_TYPE_VECTOR_2D: {
  267. count2 += 2;
  268. } break;
  269. case PORT_TYPE_VECTOR_3D: {
  270. count2 += 3;
  271. } break;
  272. case PORT_TYPE_VECTOR_4D: {
  273. count2 += 4;
  274. } break;
  275. default:
  276. break;
  277. }
  278. }
  279. }
  280. return count2;
  281. }
  282. bool VisualShaderNode::is_code_generated() const {
  283. return true;
  284. }
  285. bool VisualShaderNode::is_show_prop_names() const {
  286. return false;
  287. }
  288. bool VisualShaderNode::is_use_prop_slots() const {
  289. return false;
  290. }
  291. bool VisualShaderNode::is_disabled() const {
  292. return disabled;
  293. }
  294. void VisualShaderNode::set_disabled(bool p_disabled) {
  295. disabled = p_disabled;
  296. }
  297. Vector<VisualShader::DefaultTextureParam> VisualShaderNode::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  298. return Vector<VisualShader::DefaultTextureParam>();
  299. }
  300. String VisualShaderNode::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  301. return String();
  302. }
  303. String VisualShaderNode::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  304. return String();
  305. }
  306. String VisualShaderNode::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  307. return String();
  308. }
  309. Vector<StringName> VisualShaderNode::get_editable_properties() const {
  310. return Vector<StringName>();
  311. }
  312. HashMap<StringName, String> VisualShaderNode::get_editable_properties_names() const {
  313. return HashMap<StringName, String>();
  314. }
  315. Array VisualShaderNode::get_default_input_values() const {
  316. Array ret;
  317. for (const KeyValue<int, Variant> &E : default_input_values) {
  318. ret.push_back(E.key);
  319. ret.push_back(E.value);
  320. }
  321. return ret;
  322. }
  323. void VisualShaderNode::set_default_input_values(const Array &p_values) {
  324. if (p_values.size() % 2 == 0) {
  325. for (int i = 0; i < p_values.size(); i += 2) {
  326. default_input_values[p_values[i + 0]] = p_values[i + 1];
  327. }
  328. }
  329. emit_changed();
  330. }
  331. String VisualShaderNode::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  332. return String();
  333. }
  334. bool VisualShaderNode::is_input_port_default(int p_port, Shader::Mode p_mode) const {
  335. return false;
  336. }
  337. void VisualShaderNode::_bind_methods() {
  338. ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
  339. ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
  340. ClassDB::bind_method(D_METHOD("_set_output_port_expanded", "port"), &VisualShaderNode::_set_output_port_expanded);
  341. ClassDB::bind_method(D_METHOD("_is_output_port_expanded"), &VisualShaderNode::_is_output_port_expanded);
  342. ClassDB::bind_method(D_METHOD("_set_output_ports_expanded", "values"), &VisualShaderNode::_set_output_ports_expanded);
  343. ClassDB::bind_method(D_METHOD("_get_output_ports_expanded"), &VisualShaderNode::_get_output_ports_expanded);
  344. ClassDB::bind_method(D_METHOD("set_input_port_default_value", "port", "value", "prev_value"), &VisualShaderNode::set_input_port_default_value, DEFVAL(Variant()));
  345. ClassDB::bind_method(D_METHOD("get_input_port_default_value", "port"), &VisualShaderNode::get_input_port_default_value);
  346. ClassDB::bind_method(D_METHOD("remove_input_port_default_value", "port"), &VisualShaderNode::remove_input_port_default_value);
  347. ClassDB::bind_method(D_METHOD("clear_default_input_values"), &VisualShaderNode::clear_default_input_values);
  348. ClassDB::bind_method(D_METHOD("set_default_input_values", "values"), &VisualShaderNode::set_default_input_values);
  349. ClassDB::bind_method(D_METHOD("get_default_input_values"), &VisualShaderNode::get_default_input_values);
  350. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_port_for_preview"), "set_output_port_for_preview", "get_output_port_for_preview");
  351. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "default_input_values", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_default_input_values", "get_default_input_values");
  352. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "expanded_output_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_output_ports_expanded", "_get_output_ports_expanded");
  353. ADD_SIGNAL(MethodInfo("editor_refresh_request"));
  354. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR);
  355. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR_INT);
  356. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_2D);
  357. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_3D);
  358. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR_4D);
  359. BIND_ENUM_CONSTANT(PORT_TYPE_BOOLEAN);
  360. BIND_ENUM_CONSTANT(PORT_TYPE_TRANSFORM);
  361. BIND_ENUM_CONSTANT(PORT_TYPE_SAMPLER);
  362. BIND_ENUM_CONSTANT(PORT_TYPE_MAX);
  363. }
  364. VisualShaderNode::VisualShaderNode() {
  365. }
  366. /////////////////////////////////////////////////////////
  367. void VisualShaderNodeCustom::update_ports() {
  368. {
  369. input_ports.clear();
  370. int input_port_count;
  371. if (GDVIRTUAL_CALL(_get_input_port_count, input_port_count)) {
  372. for (int i = 0; i < input_port_count; i++) {
  373. Port port;
  374. if (!GDVIRTUAL_CALL(_get_input_port_name, i, port.name)) {
  375. port.name = "in" + itos(i);
  376. }
  377. if (!GDVIRTUAL_CALL(_get_input_port_type, i, port.type)) {
  378. port.type = (int)PortType::PORT_TYPE_SCALAR;
  379. }
  380. input_ports.push_back(port);
  381. }
  382. }
  383. }
  384. {
  385. output_ports.clear();
  386. int output_port_count;
  387. if (GDVIRTUAL_CALL(_get_output_port_count, output_port_count)) {
  388. for (int i = 0; i < output_port_count; i++) {
  389. Port port;
  390. if (!GDVIRTUAL_CALL(_get_output_port_name, i, port.name)) {
  391. port.name = "out" + itos(i);
  392. }
  393. if (!GDVIRTUAL_CALL(_get_output_port_type, i, port.type)) {
  394. port.type = (int)PortType::PORT_TYPE_SCALAR;
  395. }
  396. output_ports.push_back(port);
  397. }
  398. }
  399. }
  400. }
  401. String VisualShaderNodeCustom::get_caption() const {
  402. String ret;
  403. if (GDVIRTUAL_CALL(_get_name, ret)) {
  404. return ret;
  405. }
  406. return "Unnamed";
  407. }
  408. int VisualShaderNodeCustom::get_input_port_count() const {
  409. return input_ports.size();
  410. }
  411. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_input_port_type(int p_port) const {
  412. ERR_FAIL_INDEX_V(p_port, input_ports.size(), PORT_TYPE_SCALAR);
  413. return (PortType)input_ports[p_port].type;
  414. }
  415. String VisualShaderNodeCustom::get_input_port_name(int p_port) const {
  416. ERR_FAIL_INDEX_V(p_port, input_ports.size(), "");
  417. return input_ports[p_port].name;
  418. }
  419. int VisualShaderNodeCustom::get_output_port_count() const {
  420. return output_ports.size();
  421. }
  422. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_output_port_type(int p_port) const {
  423. ERR_FAIL_INDEX_V(p_port, output_ports.size(), PORT_TYPE_SCALAR);
  424. return (PortType)output_ports[p_port].type;
  425. }
  426. String VisualShaderNodeCustom::get_output_port_name(int p_port) const {
  427. ERR_FAIL_INDEX_V(p_port, output_ports.size(), "");
  428. return output_ports[p_port].name;
  429. }
  430. String VisualShaderNodeCustom::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  431. ERR_FAIL_COND_V(!GDVIRTUAL_IS_OVERRIDDEN(_get_code), "");
  432. TypedArray<String> input_vars;
  433. for (int i = 0; i < get_input_port_count(); i++) {
  434. input_vars.push_back(p_input_vars[i]);
  435. }
  436. TypedArray<String> output_vars;
  437. for (int i = 0; i < get_output_port_count(); i++) {
  438. output_vars.push_back(p_output_vars[i]);
  439. }
  440. String _code;
  441. GDVIRTUAL_CALL(_get_code, input_vars, output_vars, p_mode, p_type, _code);
  442. if (_is_valid_code(_code)) {
  443. String code = " {\n";
  444. bool nend = _code.ends_with("\n");
  445. _code = _code.insert(0, " ");
  446. _code = _code.replace("\n", "\n ");
  447. code += _code;
  448. if (!nend) {
  449. code += "\n }";
  450. } else {
  451. code.remove_at(code.size() - 1);
  452. code += "}";
  453. }
  454. code += "\n";
  455. return code;
  456. }
  457. return String();
  458. }
  459. String VisualShaderNodeCustom::generate_global_per_node(Shader::Mode p_mode, int p_id) const {
  460. String _code;
  461. if (GDVIRTUAL_CALL(_get_global_code, p_mode, _code)) {
  462. if (_is_valid_code(_code)) {
  463. String code = "// " + get_caption() + "\n";
  464. code += _code;
  465. code += "\n";
  466. return code;
  467. }
  468. }
  469. return String();
  470. }
  471. String VisualShaderNodeCustom::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  472. String _code;
  473. if (GDVIRTUAL_CALL(_get_func_code, p_mode, p_type, _code)) {
  474. if (_is_valid_code(_code)) {
  475. bool nend = _code.ends_with("\n");
  476. String code = "// " + get_caption() + "\n";
  477. code += " {\n";
  478. _code = _code.insert(0, " ");
  479. _code = _code.replace("\n", "\n ");
  480. code += _code;
  481. if (!nend) {
  482. code += "\n }";
  483. } else {
  484. code.remove_at(code.size() - 1);
  485. code += "}";
  486. }
  487. code += "\n";
  488. return code;
  489. }
  490. }
  491. return String();
  492. }
  493. bool VisualShaderNodeCustom::is_available(Shader::Mode p_mode, VisualShader::Type p_type) const {
  494. bool ret;
  495. if (GDVIRTUAL_CALL(_is_available, p_mode, p_type, ret)) {
  496. return ret;
  497. }
  498. return true;
  499. }
  500. void VisualShaderNodeCustom::set_input_port_default_value(int p_port, const Variant &p_value, const Variant &p_prev_value) {
  501. if (!is_initialized) {
  502. VisualShaderNode::set_input_port_default_value(p_port, p_value, p_prev_value);
  503. }
  504. }
  505. void VisualShaderNodeCustom::set_default_input_values(const Array &p_values) {
  506. if (!is_initialized) {
  507. VisualShaderNode::set_default_input_values(p_values);
  508. }
  509. }
  510. void VisualShaderNodeCustom::remove_input_port_default_value(int p_port) {
  511. if (!is_initialized) {
  512. VisualShaderNode::remove_input_port_default_value(p_port);
  513. }
  514. }
  515. void VisualShaderNodeCustom::clear_default_input_values() {
  516. if (!is_initialized) {
  517. VisualShaderNode::clear_default_input_values();
  518. }
  519. }
  520. void VisualShaderNodeCustom::_set_input_port_default_value(int p_port, const Variant &p_value) {
  521. VisualShaderNode::set_input_port_default_value(p_port, p_value);
  522. }
  523. bool VisualShaderNodeCustom::_is_valid_code(const String &p_code) const {
  524. if (p_code.is_empty() || p_code == "null") {
  525. return false;
  526. }
  527. return true;
  528. }
  529. bool VisualShaderNodeCustom::_is_initialized() {
  530. return is_initialized;
  531. }
  532. void VisualShaderNodeCustom::_set_initialized(bool p_enabled) {
  533. is_initialized = p_enabled;
  534. }
  535. void VisualShaderNodeCustom::_bind_methods() {
  536. GDVIRTUAL_BIND(_get_name);
  537. GDVIRTUAL_BIND(_get_description);
  538. GDVIRTUAL_BIND(_get_category);
  539. GDVIRTUAL_BIND(_get_return_icon_type);
  540. GDVIRTUAL_BIND(_get_input_port_count);
  541. GDVIRTUAL_BIND(_get_input_port_type, "port");
  542. GDVIRTUAL_BIND(_get_input_port_name, "port");
  543. GDVIRTUAL_BIND(_get_output_port_count);
  544. GDVIRTUAL_BIND(_get_output_port_type, "port");
  545. GDVIRTUAL_BIND(_get_output_port_name, "port");
  546. GDVIRTUAL_BIND(_get_code, "input_vars", "output_vars", "mode", "type");
  547. GDVIRTUAL_BIND(_get_func_code, "mode", "type");
  548. GDVIRTUAL_BIND(_get_global_code, "mode");
  549. GDVIRTUAL_BIND(_is_highend);
  550. GDVIRTUAL_BIND(_is_available, "mode", "type");
  551. ClassDB::bind_method(D_METHOD("_set_initialized", "enabled"), &VisualShaderNodeCustom::_set_initialized);
  552. ClassDB::bind_method(D_METHOD("_is_initialized"), &VisualShaderNodeCustom::_is_initialized);
  553. ClassDB::bind_method(D_METHOD("_set_input_port_default_value", "port", "value"), &VisualShaderNodeCustom::_set_input_port_default_value);
  554. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_initialized", "_is_initialized");
  555. }
  556. VisualShaderNodeCustom::VisualShaderNodeCustom() {
  557. simple_decl = false;
  558. }
  559. /////////////////////////////////////////////////////////
  560. void VisualShader::set_shader_type(Type p_type) {
  561. current_type = p_type;
  562. }
  563. VisualShader::Type VisualShader::get_shader_type() const {
  564. return current_type;
  565. }
  566. void VisualShader::add_varying(const String &p_name, VaryingMode p_mode, VaryingType p_type) {
  567. ERR_FAIL_COND(!p_name.is_valid_identifier());
  568. ERR_FAIL_INDEX((int)p_mode, (int)VARYING_MODE_MAX);
  569. ERR_FAIL_INDEX((int)p_type, (int)VARYING_TYPE_MAX);
  570. ERR_FAIL_COND(varyings.has(p_name));
  571. Varying var = Varying(p_name, p_mode, p_type);
  572. varyings[p_name] = var;
  573. varyings_list.push_back(var);
  574. _queue_update();
  575. }
  576. void VisualShader::remove_varying(const String &p_name) {
  577. ERR_FAIL_COND(!varyings.has(p_name));
  578. varyings.erase(p_name);
  579. for (List<Varying>::Element *E = varyings_list.front(); E; E = E->next()) {
  580. if (E->get().name == p_name) {
  581. varyings_list.erase(E);
  582. break;
  583. }
  584. }
  585. _queue_update();
  586. }
  587. bool VisualShader::has_varying(const String &p_name) const {
  588. return varyings.has(p_name);
  589. }
  590. int VisualShader::get_varyings_count() const {
  591. return varyings_list.size();
  592. }
  593. const VisualShader::Varying *VisualShader::get_varying_by_index(int p_idx) const {
  594. ERR_FAIL_INDEX_V(p_idx, varyings_list.size(), nullptr);
  595. return &varyings_list[p_idx];
  596. }
  597. void VisualShader::set_varying_mode(const String &p_name, VaryingMode p_mode) {
  598. ERR_FAIL_INDEX((int)p_mode, (int)VARYING_MODE_MAX);
  599. ERR_FAIL_COND(!varyings.has(p_name));
  600. if (varyings[p_name].mode == p_mode) {
  601. return;
  602. }
  603. varyings[p_name].mode = p_mode;
  604. _queue_update();
  605. }
  606. VisualShader::VaryingMode VisualShader::get_varying_mode(const String &p_name) {
  607. ERR_FAIL_COND_V(!varyings.has(p_name), VARYING_MODE_MAX);
  608. return varyings[p_name].mode;
  609. }
  610. void VisualShader::set_varying_type(const String &p_name, VaryingType p_type) {
  611. ERR_FAIL_INDEX((int)p_type, (int)VARYING_TYPE_MAX);
  612. ERR_FAIL_COND(!varyings.has(p_name));
  613. if (varyings[p_name].type == p_type) {
  614. return;
  615. }
  616. varyings[p_name].type = p_type;
  617. _queue_update();
  618. }
  619. VisualShader::VaryingType VisualShader::get_varying_type(const String &p_name) {
  620. ERR_FAIL_COND_V(!varyings.has(p_name), VARYING_TYPE_MAX);
  621. return varyings[p_name].type;
  622. }
  623. void VisualShader::set_engine_version(const Dictionary &p_engine_version) {
  624. ERR_FAIL_COND(!p_engine_version.has("major"));
  625. ERR_FAIL_COND(!p_engine_version.has("minor"));
  626. engine_version["major"] = p_engine_version["major"];
  627. engine_version["minor"] = p_engine_version["minor"];
  628. }
  629. Dictionary VisualShader::get_engine_version() const {
  630. return engine_version;
  631. }
  632. #ifndef DISABLE_DEPRECATED
  633. void VisualShader::update_engine_version(const Dictionary &p_new_version) {
  634. if (engine_version.is_empty()) { // before 4.0
  635. for (int i = 0; i < TYPE_MAX; i++) {
  636. for (KeyValue<int, Node> &E : graph[i].nodes) {
  637. Ref<VisualShaderNodeInput> input = Object::cast_to<VisualShaderNodeInput>(E.value.node.ptr());
  638. if (input.is_valid()) {
  639. if (input->get_input_name() == "side") {
  640. input->set_input_name("front_facing");
  641. }
  642. }
  643. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(E.value.node.ptr());
  644. if (expression.is_valid()) {
  645. for (int j = 0; j < expression->get_input_port_count(); j++) {
  646. int type = expression->get_input_port_type(j);
  647. if (type > 0) { // + PORT_TYPE_SCALAR_INT + PORT_TYPE_VECTOR_2D
  648. type += 2;
  649. }
  650. expression->set_input_port_type(j, type);
  651. }
  652. for (int j = 0; j < expression->get_output_port_count(); j++) {
  653. int type = expression->get_output_port_type(j);
  654. if (type > 0) { // + PORT_TYPE_SCALAR_INT + PORT_TYPE_VECTOR_2D
  655. type += 2;
  656. }
  657. expression->set_output_port_type(j, type);
  658. }
  659. }
  660. Ref<VisualShaderNodeStep> step = Object::cast_to<VisualShaderNodeStep>(E.value.node.ptr());
  661. if (step.is_valid()) {
  662. int op_type = int(step->get_op_type());
  663. if (int(op_type) > 0) { // + OP_TYPE_VECTOR_2D + OP_TYPE_VECTOR_2D_SCALAR
  664. op_type += 2;
  665. }
  666. step->set_op_type(VisualShaderNodeStep::OpType(op_type));
  667. }
  668. Ref<VisualShaderNodeSmoothStep> sstep = Object::cast_to<VisualShaderNodeSmoothStep>(E.value.node.ptr());
  669. if (sstep.is_valid()) {
  670. int op_type = int(sstep->get_op_type());
  671. if (int(op_type) > 0) { // + OP_TYPE_VECTOR_2D + OP_TYPE_VECTOR_2D_SCALAR
  672. op_type += 2;
  673. }
  674. sstep->set_op_type(VisualShaderNodeSmoothStep::OpType(op_type));
  675. }
  676. Ref<VisualShaderNodeMix> mix = Object::cast_to<VisualShaderNodeMix>(E.value.node.ptr());
  677. if (mix.is_valid()) {
  678. int op_type = int(mix->get_op_type());
  679. if (int(op_type) > 0) { // + OP_TYPE_VECTOR_2D + OP_TYPE_VECTOR_2D_SCALAR
  680. op_type += 2;
  681. }
  682. mix->set_op_type(VisualShaderNodeMix::OpType(op_type));
  683. }
  684. Ref<VisualShaderNodeCompare> compare = Object::cast_to<VisualShaderNodeCompare>(E.value.node.ptr());
  685. if (compare.is_valid()) {
  686. int ctype = int(compare->get_comparison_type());
  687. if (int(ctype) > 0) { // + CTYPE_SCALAR_INT + CTYPE_VECTOR_2D
  688. ctype += 2;
  689. }
  690. compare->set_comparison_type(VisualShaderNodeCompare::ComparisonType(ctype));
  691. }
  692. }
  693. }
  694. }
  695. set_engine_version(p_new_version);
  696. }
  697. #endif /* DISABLE_DEPRECATED */
  698. void VisualShader::add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id) {
  699. ERR_FAIL_COND(p_node.is_null());
  700. ERR_FAIL_COND(p_id < 2);
  701. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  702. Graph *g = &graph[p_type];
  703. ERR_FAIL_COND(g->nodes.has(p_id));
  704. Node n;
  705. n.node = p_node;
  706. n.position = p_position;
  707. Ref<VisualShaderNodeUniform> uniform = n.node;
  708. if (uniform.is_valid()) {
  709. String valid_name = validate_uniform_name(uniform->get_uniform_name(), uniform);
  710. uniform->set_uniform_name(valid_name);
  711. }
  712. Ref<VisualShaderNodeInput> input = n.node;
  713. if (input.is_valid()) {
  714. input->shader_mode = shader_mode;
  715. input->shader_type = p_type;
  716. }
  717. n.node->connect("changed", callable_mp(this, &VisualShader::_queue_update));
  718. Ref<VisualShaderNodeCustom> custom = n.node;
  719. if (custom.is_valid()) {
  720. custom->update_ports();
  721. }
  722. g->nodes[p_id] = n;
  723. _queue_update();
  724. }
  725. void VisualShader::set_node_position(Type p_type, int p_id, const Vector2 &p_position) {
  726. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  727. Graph *g = &graph[p_type];
  728. ERR_FAIL_COND(!g->nodes.has(p_id));
  729. g->nodes[p_id].position = p_position;
  730. }
  731. Vector2 VisualShader::get_node_position(Type p_type, int p_id) const {
  732. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector2());
  733. const Graph *g = &graph[p_type];
  734. ERR_FAIL_COND_V(!g->nodes.has(p_id), Vector2());
  735. return g->nodes[p_id].position;
  736. }
  737. Ref<VisualShaderNode> VisualShader::get_node(Type p_type, int p_id) const {
  738. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Ref<VisualShaderNode>());
  739. const Graph *g = &graph[p_type];
  740. ERR_FAIL_COND_V(!g->nodes.has(p_id), Ref<VisualShaderNode>());
  741. return g->nodes[p_id].node;
  742. }
  743. Vector<int> VisualShader::get_node_list(Type p_type) const {
  744. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector<int>());
  745. const Graph *g = &graph[p_type];
  746. Vector<int> ret;
  747. for (const KeyValue<int, Node> &E : g->nodes) {
  748. ret.push_back(E.key);
  749. }
  750. return ret;
  751. }
  752. int VisualShader::get_valid_node_id(Type p_type) const {
  753. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, NODE_ID_INVALID);
  754. const Graph *g = &graph[p_type];
  755. return g->nodes.size() ? MAX(2, g->nodes.back()->key() + 1) : 2;
  756. }
  757. int VisualShader::find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const {
  758. for (const KeyValue<int, Node> &E : graph[p_type].nodes) {
  759. if (E.value.node == p_node) {
  760. return E.key;
  761. }
  762. }
  763. return NODE_ID_INVALID;
  764. }
  765. void VisualShader::remove_node(Type p_type, int p_id) {
  766. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  767. ERR_FAIL_COND(p_id < 2);
  768. Graph *g = &graph[p_type];
  769. ERR_FAIL_COND(!g->nodes.has(p_id));
  770. g->nodes[p_id].node->disconnect("changed", callable_mp(this, &VisualShader::_queue_update));
  771. g->nodes.erase(p_id);
  772. for (List<Connection>::Element *E = g->connections.front(); E;) {
  773. List<Connection>::Element *N = E->next();
  774. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  775. g->connections.erase(E);
  776. if (E->get().from_node == p_id) {
  777. g->nodes[E->get().to_node].prev_connected_nodes.erase(p_id);
  778. g->nodes[E->get().to_node].node->set_input_port_connected(E->get().to_port, false);
  779. }
  780. }
  781. E = N;
  782. }
  783. _queue_update();
  784. }
  785. void VisualShader::replace_node(Type p_type, int p_id, const StringName &p_new_class) {
  786. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  787. ERR_FAIL_COND(p_id < 2);
  788. Graph *g = &graph[p_type];
  789. ERR_FAIL_COND(!g->nodes.has(p_id));
  790. if (g->nodes[p_id].node->get_class_name() == p_new_class) {
  791. return;
  792. }
  793. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instantiate(p_new_class));
  794. vsn->connect("changed", callable_mp(this, &VisualShader::_queue_update));
  795. g->nodes[p_id].node = Ref<VisualShaderNode>(vsn);
  796. _queue_update();
  797. }
  798. bool VisualShader::is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  799. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  800. const Graph *g = &graph[p_type];
  801. for (const Connection &E : g->connections) {
  802. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  803. return true;
  804. }
  805. }
  806. return false;
  807. }
  808. bool VisualShader::is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const {
  809. bool result = false;
  810. const VisualShader::Node &node = p_graph->nodes[p_node];
  811. for (const int &E : node.prev_connected_nodes) {
  812. if (E == p_target) {
  813. return true;
  814. }
  815. result = is_nodes_connected_relatively(p_graph, E, p_target);
  816. if (result) {
  817. break;
  818. }
  819. }
  820. return result;
  821. }
  822. bool VisualShader::can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  823. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  824. const Graph *g = &graph[p_type];
  825. if (!g->nodes.has(p_from_node)) {
  826. return false;
  827. }
  828. if (p_from_node == p_to_node) {
  829. return false;
  830. }
  831. if (p_from_port < 0 || p_from_port >= g->nodes[p_from_node].node->get_expanded_output_port_count()) {
  832. return false;
  833. }
  834. if (!g->nodes.has(p_to_node)) {
  835. return false;
  836. }
  837. if (p_to_port < 0 || p_to_port >= g->nodes[p_to_node].node->get_input_port_count()) {
  838. return false;
  839. }
  840. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  841. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  842. if (!is_port_types_compatible(from_port_type, to_port_type)) {
  843. return false;
  844. }
  845. for (const Connection &E : g->connections) {
  846. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  847. return false;
  848. }
  849. }
  850. if (is_nodes_connected_relatively(g, p_from_node, p_to_node)) {
  851. return false;
  852. }
  853. return true;
  854. }
  855. bool VisualShader::is_port_types_compatible(int p_a, int p_b) const {
  856. return MAX(0, p_a - 4) == (MAX(0, p_b - 4));
  857. }
  858. void VisualShader::connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  859. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  860. Graph *g = &graph[p_type];
  861. ERR_FAIL_COND(!g->nodes.has(p_from_node));
  862. ERR_FAIL_INDEX(p_from_port, g->nodes[p_from_node].node->get_expanded_output_port_count());
  863. ERR_FAIL_COND(!g->nodes.has(p_to_node));
  864. ERR_FAIL_INDEX(p_to_port, g->nodes[p_to_node].node->get_input_port_count());
  865. Connection c;
  866. c.from_node = p_from_node;
  867. c.from_port = p_from_port;
  868. c.to_node = p_to_node;
  869. c.to_port = p_to_port;
  870. g->connections.push_back(c);
  871. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  872. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  873. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  874. _queue_update();
  875. }
  876. Error VisualShader::connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  877. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, ERR_CANT_CONNECT);
  878. Graph *g = &graph[p_type];
  879. ERR_FAIL_COND_V(!g->nodes.has(p_from_node), ERR_INVALID_PARAMETER);
  880. ERR_FAIL_INDEX_V(p_from_port, g->nodes[p_from_node].node->get_expanded_output_port_count(), ERR_INVALID_PARAMETER);
  881. ERR_FAIL_COND_V(!g->nodes.has(p_to_node), ERR_INVALID_PARAMETER);
  882. ERR_FAIL_INDEX_V(p_to_port, g->nodes[p_to_node].node->get_input_port_count(), ERR_INVALID_PARAMETER);
  883. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  884. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  885. ERR_FAIL_COND_V_MSG(!is_port_types_compatible(from_port_type, to_port_type), ERR_INVALID_PARAMETER, "Incompatible port types (scalar/vec/bool) with transform.");
  886. for (const Connection &E : g->connections) {
  887. if (E.from_node == p_from_node && E.from_port == p_from_port && E.to_node == p_to_node && E.to_port == p_to_port) {
  888. ERR_FAIL_V(ERR_ALREADY_EXISTS);
  889. }
  890. }
  891. Connection c;
  892. c.from_node = p_from_node;
  893. c.from_port = p_from_port;
  894. c.to_node = p_to_node;
  895. c.to_port = p_to_port;
  896. g->connections.push_back(c);
  897. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  898. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  899. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  900. _queue_update();
  901. return OK;
  902. }
  903. void VisualShader::disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  904. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  905. Graph *g = &graph[p_type];
  906. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  907. 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) {
  908. g->connections.erase(E);
  909. g->nodes[p_to_node].prev_connected_nodes.erase(p_from_node);
  910. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, false);
  911. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, false);
  912. _queue_update();
  913. return;
  914. }
  915. }
  916. }
  917. Array VisualShader::_get_node_connections(Type p_type) const {
  918. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Array());
  919. const Graph *g = &graph[p_type];
  920. Array ret;
  921. for (const Connection &E : g->connections) {
  922. Dictionary d;
  923. d["from_node"] = E.from_node;
  924. d["from_port"] = E.from_port;
  925. d["to_node"] = E.to_node;
  926. d["to_port"] = E.to_port;
  927. ret.push_back(d);
  928. }
  929. return ret;
  930. }
  931. void VisualShader::get_node_connections(Type p_type, List<Connection> *r_connections) const {
  932. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  933. const Graph *g = &graph[p_type];
  934. for (const Connection &E : g->connections) {
  935. r_connections->push_back(E);
  936. }
  937. }
  938. void VisualShader::set_mode(Mode p_mode) {
  939. ERR_FAIL_INDEX_MSG(p_mode, Mode::MODE_MAX, vformat("Invalid shader mode: %d.", p_mode));
  940. if (shader_mode == p_mode) {
  941. return;
  942. }
  943. //erase input/output connections
  944. modes.clear();
  945. flags.clear();
  946. shader_mode = p_mode;
  947. for (int i = 0; i < TYPE_MAX; i++) {
  948. for (KeyValue<int, Node> &E : graph[i].nodes) {
  949. Ref<VisualShaderNodeInput> input = E.value.node;
  950. if (input.is_valid()) {
  951. input->shader_mode = shader_mode;
  952. //input->input_index = 0;
  953. }
  954. }
  955. Ref<VisualShaderNodeOutput> output = graph[i].nodes[NODE_ID_OUTPUT].node;
  956. output->shader_mode = shader_mode;
  957. // clear connections since they are no longer valid
  958. for (List<Connection>::Element *E = graph[i].connections.front(); E;) {
  959. bool keep = true;
  960. List<Connection>::Element *N = E->next();
  961. int from = E->get().from_node;
  962. int to = E->get().to_node;
  963. if (!graph[i].nodes.has(from)) {
  964. keep = false;
  965. } else {
  966. Ref<VisualShaderNode> from_node = graph[i].nodes[from].node;
  967. if (from_node->is_class("VisualShaderNodeOutput") || from_node->is_class("VisualShaderNodeInput")) {
  968. keep = false;
  969. }
  970. }
  971. if (!graph[i].nodes.has(to)) {
  972. keep = false;
  973. } else {
  974. Ref<VisualShaderNode> to_node = graph[i].nodes[to].node;
  975. if (to_node->is_class("VisualShaderNodeOutput") || to_node->is_class("VisualShaderNodeInput")) {
  976. keep = false;
  977. }
  978. }
  979. if (!keep) {
  980. graph[i].connections.erase(E);
  981. }
  982. E = N;
  983. }
  984. }
  985. _queue_update();
  986. notify_property_list_changed();
  987. }
  988. void VisualShader::set_graph_offset(const Vector2 &p_offset) {
  989. graph_offset = p_offset;
  990. }
  991. Vector2 VisualShader::get_graph_offset() const {
  992. return graph_offset;
  993. }
  994. Shader::Mode VisualShader::get_mode() const {
  995. return shader_mode;
  996. }
  997. bool VisualShader::is_text_shader() const {
  998. return false;
  999. }
  1000. String VisualShader::generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &default_tex_params) const {
  1001. Ref<VisualShaderNode> node = get_node(p_type, p_node);
  1002. ERR_FAIL_COND_V(!node.is_valid(), String());
  1003. ERR_FAIL_COND_V(p_port < 0 || p_port >= node->get_expanded_output_port_count(), String());
  1004. ERR_FAIL_COND_V(node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_TRANSFORM, String());
  1005. StringBuilder global_code;
  1006. StringBuilder global_code_per_node;
  1007. HashMap<Type, StringBuilder> global_code_per_func;
  1008. StringBuilder code;
  1009. RBSet<StringName> classes;
  1010. global_code += String() + "shader_type canvas_item;\n";
  1011. String global_expressions;
  1012. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  1013. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1014. Ref<VisualShaderNodeGlobalExpression> global_expression = E.value.node;
  1015. if (global_expression.is_valid()) {
  1016. String expr = "";
  1017. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  1018. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  1019. expr = expr.replace("\n", "\n ");
  1020. expr += "\n";
  1021. global_expressions += expr;
  1022. }
  1023. }
  1024. }
  1025. global_code += "\n";
  1026. global_code += global_expressions;
  1027. //make it faster to go around through shader
  1028. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  1029. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  1030. for (const List<Connection>::Element *E = graph[p_type].connections.front(); E; E = E->next()) {
  1031. ConnectionKey from_key;
  1032. from_key.node = E->get().from_node;
  1033. from_key.port = E->get().from_port;
  1034. output_connections.insert(from_key, E);
  1035. ConnectionKey to_key;
  1036. to_key.node = E->get().to_node;
  1037. to_key.port = E->get().to_port;
  1038. input_connections.insert(to_key, E);
  1039. }
  1040. code += "\nvoid fragment() {\n";
  1041. RBSet<int> processed;
  1042. Error err = _write_node(p_type, &global_code, &global_code_per_node, &global_code_per_func, code, default_tex_params, input_connections, output_connections, p_node, processed, true, classes);
  1043. ERR_FAIL_COND_V(err != OK, String());
  1044. switch (node->get_output_port_type(p_port)) {
  1045. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1046. code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + ");\n";
  1047. } break;
  1048. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1049. code += " COLOR.rgb = vec3(float(n_out" + itos(p_node) + "p" + itos(p_port) + "));\n";
  1050. } break;
  1051. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1052. code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + " ? 1.0 : 0.0);\n";
  1053. } break;
  1054. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1055. code += " COLOR.rgb = vec3(n_out" + itos(p_node) + "p" + itos(p_port) + ", 0.0);\n";
  1056. } break;
  1057. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1058. code += " COLOR.rgb = n_out" + itos(p_node) + "p" + itos(p_port) + ";\n";
  1059. } break;
  1060. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1061. code += " COLOR.rgb = n_out" + itos(p_node) + "p" + itos(p_port) + ".xyz;\n";
  1062. } break;
  1063. default: {
  1064. code += " COLOR.rgb = vec3(0.0);\n";
  1065. } break;
  1066. }
  1067. code += "}\n";
  1068. //set code secretly
  1069. global_code += "\n\n";
  1070. String final_code = global_code;
  1071. final_code += global_code_per_node;
  1072. final_code += code;
  1073. return final_code;
  1074. }
  1075. String VisualShader::validate_port_name(const String &p_port_name, VisualShaderNode *p_node, int p_port_id, bool p_output) const {
  1076. String name = p_port_name;
  1077. if (name.is_empty()) {
  1078. return String();
  1079. }
  1080. while (name.length() && !is_ascii_char(name[0])) {
  1081. name = name.substr(1, name.length() - 1);
  1082. }
  1083. if (!name.is_empty()) {
  1084. String valid_name;
  1085. for (int i = 0; i < name.length(); i++) {
  1086. if (is_ascii_identifier_char(name[i])) {
  1087. valid_name += String::chr(name[i]);
  1088. } else if (name[i] == ' ') {
  1089. valid_name += "_";
  1090. }
  1091. }
  1092. name = valid_name;
  1093. } else {
  1094. return String();
  1095. }
  1096. List<String> input_names;
  1097. List<String> output_names;
  1098. for (int i = 0; i < p_node->get_input_port_count(); i++) {
  1099. if (!p_output && i == p_port_id) {
  1100. continue;
  1101. }
  1102. if (name == p_node->get_input_port_name(i)) {
  1103. return String();
  1104. }
  1105. }
  1106. for (int i = 0; i < p_node->get_output_port_count(); i++) {
  1107. if (p_output && i == p_port_id) {
  1108. continue;
  1109. }
  1110. if (name == p_node->get_output_port_name(i)) {
  1111. return String();
  1112. }
  1113. }
  1114. return name;
  1115. }
  1116. String VisualShader::validate_uniform_name(const String &p_name, const Ref<VisualShaderNodeUniform> &p_uniform) const {
  1117. String name = p_name; //validate name first
  1118. while (name.length() && !is_ascii_char(name[0])) {
  1119. name = name.substr(1, name.length() - 1);
  1120. }
  1121. if (!name.is_empty()) {
  1122. String valid_name;
  1123. for (int i = 0; i < name.length(); i++) {
  1124. if (is_ascii_identifier_char(name[i])) {
  1125. valid_name += String::chr(name[i]);
  1126. } else if (name[i] == ' ') {
  1127. valid_name += "_";
  1128. }
  1129. }
  1130. name = valid_name;
  1131. }
  1132. if (name.is_empty()) {
  1133. name = p_uniform->get_caption();
  1134. }
  1135. int attempt = 1;
  1136. while (true) {
  1137. bool exists = false;
  1138. for (int i = 0; i < TYPE_MAX; i++) {
  1139. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1140. Ref<VisualShaderNodeUniform> node = E.value.node;
  1141. if (node == p_uniform) { //do not test on self
  1142. continue;
  1143. }
  1144. if (node.is_valid() && node->get_uniform_name() == name) {
  1145. exists = true;
  1146. break;
  1147. }
  1148. }
  1149. if (exists) {
  1150. break;
  1151. }
  1152. }
  1153. if (exists) {
  1154. //remove numbers, put new and try again
  1155. attempt++;
  1156. while (name.length() && is_digit(name[name.length() - 1])) {
  1157. name = name.substr(0, name.length() - 1);
  1158. }
  1159. ERR_FAIL_COND_V(name.is_empty(), String());
  1160. name += itos(attempt);
  1161. } else {
  1162. break;
  1163. }
  1164. }
  1165. return name;
  1166. }
  1167. static const char *type_string[VisualShader::TYPE_MAX] = {
  1168. "vertex",
  1169. "fragment",
  1170. "light",
  1171. "start",
  1172. "process",
  1173. "collide",
  1174. "start_custom",
  1175. "process_custom",
  1176. "sky",
  1177. "fog",
  1178. };
  1179. bool VisualShader::_set(const StringName &p_name, const Variant &p_value) {
  1180. String name = p_name;
  1181. if (name == "mode") {
  1182. set_mode(Shader::Mode(int(p_value)));
  1183. return true;
  1184. } else if (name.begins_with("flags/")) {
  1185. StringName flag = name.get_slicec('/', 1);
  1186. bool enable = p_value;
  1187. if (enable) {
  1188. flags.insert(flag);
  1189. } else {
  1190. flags.erase(flag);
  1191. }
  1192. _queue_update();
  1193. return true;
  1194. } else if (name.begins_with("modes/")) {
  1195. String mode = name.get_slicec('/', 1);
  1196. int value = p_value;
  1197. if (value == 0) {
  1198. modes.erase(mode); //means it's default anyway, so don't store it
  1199. } else {
  1200. modes[mode] = value;
  1201. }
  1202. _queue_update();
  1203. return true;
  1204. } else if (name.begins_with("varyings/")) {
  1205. String var_name = name.get_slicec('/', 1);
  1206. Varying value = Varying();
  1207. value.name = var_name;
  1208. if (value.from_string(p_value) && !varyings.has(var_name)) {
  1209. varyings[var_name] = value;
  1210. varyings_list.push_back(value);
  1211. }
  1212. _queue_update();
  1213. return true;
  1214. } else if (name.begins_with("nodes/")) {
  1215. String typestr = name.get_slicec('/', 1);
  1216. Type type = TYPE_VERTEX;
  1217. for (int i = 0; i < TYPE_MAX; i++) {
  1218. if (typestr == type_string[i]) {
  1219. type = Type(i);
  1220. break;
  1221. }
  1222. }
  1223. String index = name.get_slicec('/', 2);
  1224. if (index == "connections") {
  1225. Vector<int> conns = p_value;
  1226. if (conns.size() % 4 == 0) {
  1227. for (int i = 0; i < conns.size(); i += 4) {
  1228. connect_nodes_forced(type, conns[i + 0], conns[i + 1], conns[i + 2], conns[i + 3]);
  1229. }
  1230. }
  1231. return true;
  1232. }
  1233. int id = index.to_int();
  1234. String what = name.get_slicec('/', 3);
  1235. if (what == "node") {
  1236. add_node(type, p_value, Vector2(), id);
  1237. return true;
  1238. } else if (what == "position") {
  1239. set_node_position(type, id, p_value);
  1240. return true;
  1241. } else if (what == "size") {
  1242. ((VisualShaderNodeResizableBase *)get_node(type, id).ptr())->set_size(p_value);
  1243. return true;
  1244. } else if (what == "input_ports") {
  1245. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_inputs(p_value);
  1246. return true;
  1247. } else if (what == "output_ports") {
  1248. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_outputs(p_value);
  1249. return true;
  1250. } else if (what == "expression") {
  1251. ((VisualShaderNodeExpression *)get_node(type, id).ptr())->set_expression(p_value);
  1252. return true;
  1253. }
  1254. }
  1255. return false;
  1256. }
  1257. bool VisualShader::_get(const StringName &p_name, Variant &r_ret) const {
  1258. String name = p_name;
  1259. if (name == "mode") {
  1260. r_ret = get_mode();
  1261. return true;
  1262. } else if (name.begins_with("flags/")) {
  1263. StringName flag = name.get_slicec('/', 1);
  1264. r_ret = flags.has(flag);
  1265. return true;
  1266. } else if (name.begins_with("modes/")) {
  1267. String mode = name.get_slicec('/', 1);
  1268. if (modes.has(mode)) {
  1269. r_ret = modes[mode];
  1270. } else {
  1271. r_ret = 0;
  1272. }
  1273. return true;
  1274. } else if (name.begins_with("varyings/")) {
  1275. String var_name = name.get_slicec('/', 1);
  1276. if (varyings.has(var_name)) {
  1277. r_ret = varyings[var_name].to_string();
  1278. } else {
  1279. r_ret = String();
  1280. }
  1281. return true;
  1282. } else if (name.begins_with("nodes/")) {
  1283. String typestr = name.get_slicec('/', 1);
  1284. Type type = TYPE_VERTEX;
  1285. for (int i = 0; i < TYPE_MAX; i++) {
  1286. if (typestr == type_string[i]) {
  1287. type = Type(i);
  1288. break;
  1289. }
  1290. }
  1291. String index = name.get_slicec('/', 2);
  1292. if (index == "connections") {
  1293. Vector<int> conns;
  1294. for (const Connection &E : graph[type].connections) {
  1295. conns.push_back(E.from_node);
  1296. conns.push_back(E.from_port);
  1297. conns.push_back(E.to_node);
  1298. conns.push_back(E.to_port);
  1299. }
  1300. r_ret = conns;
  1301. return true;
  1302. }
  1303. int id = index.to_int();
  1304. String what = name.get_slicec('/', 3);
  1305. if (what == "node") {
  1306. r_ret = get_node(type, id);
  1307. return true;
  1308. } else if (what == "position") {
  1309. r_ret = get_node_position(type, id);
  1310. return true;
  1311. } else if (what == "size") {
  1312. r_ret = ((VisualShaderNodeResizableBase *)get_node(type, id).ptr())->get_size();
  1313. return true;
  1314. } else if (what == "input_ports") {
  1315. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_inputs();
  1316. return true;
  1317. } else if (what == "output_ports") {
  1318. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_outputs();
  1319. return true;
  1320. } else if (what == "expression") {
  1321. r_ret = ((VisualShaderNodeExpression *)get_node(type, id).ptr())->get_expression();
  1322. return true;
  1323. }
  1324. }
  1325. return false;
  1326. }
  1327. void VisualShader::reset_state() {
  1328. #ifndef _MSC_VER
  1329. #warning everything needs to be cleared here
  1330. #endif
  1331. emit_changed();
  1332. }
  1333. void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const {
  1334. //mode
  1335. p_list->push_back(PropertyInfo(Variant::INT, PNAME("mode"), PROPERTY_HINT_ENUM, "Node3D,CanvasItem,Particles,Sky,Fog"));
  1336. //render modes
  1337. HashMap<String, String> blend_mode_enums;
  1338. RBSet<String> toggles;
  1339. const Vector<ShaderLanguage::ModeInfo> &rmodes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader_mode));
  1340. for (int i = 0; i < rmodes.size(); i++) {
  1341. const ShaderLanguage::ModeInfo &info = rmodes[i];
  1342. if (!info.options.is_empty()) {
  1343. const String begin = String(info.name);
  1344. for (int j = 0; j < info.options.size(); j++) {
  1345. const String option = String(info.options[j]).capitalize();
  1346. if (!blend_mode_enums.has(begin)) {
  1347. blend_mode_enums[begin] = option;
  1348. } else {
  1349. blend_mode_enums[begin] += "," + option;
  1350. }
  1351. }
  1352. } else {
  1353. toggles.insert(String(info.name));
  1354. }
  1355. }
  1356. for (const KeyValue<String, String> &E : blend_mode_enums) {
  1357. p_list->push_back(PropertyInfo(Variant::INT, vformat("%s/%s", PNAME("modes"), E.key), PROPERTY_HINT_ENUM, E.value));
  1358. }
  1359. for (const String &E : toggles) {
  1360. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("flags"), E)));
  1361. }
  1362. for (const KeyValue<String, Varying> &E : varyings) {
  1363. p_list->push_back(PropertyInfo(Variant::STRING, vformat("%s/%s", PNAME("varyings"), E.key)));
  1364. }
  1365. for (int i = 0; i < TYPE_MAX; i++) {
  1366. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1367. String prop_name = "nodes/";
  1368. prop_name += type_string[i];
  1369. prop_name += "/" + itos(E.key);
  1370. if (E.key != NODE_ID_OUTPUT) {
  1371. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  1372. }
  1373. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1374. if (Object::cast_to<VisualShaderNodeGroupBase>(E.value.node.ptr()) != nullptr) {
  1375. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1376. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/input_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1377. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/output_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1378. }
  1379. if (Object::cast_to<VisualShaderNodeExpression>(E.value.node.ptr()) != nullptr) {
  1380. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/expression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1381. }
  1382. }
  1383. p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, "nodes/" + String(type_string[i]) + "/connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1384. }
  1385. }
  1386. Error VisualShader::_write_node(Type type, StringBuilder *global_code, StringBuilder *global_code_per_node, HashMap<Type, StringBuilder> *global_code_per_func, StringBuilder &code, Vector<VisualShader::DefaultTextureParam> &def_tex_params, const VMap<ConnectionKey, const List<Connection>::Element *> &input_connections, const VMap<ConnectionKey, const List<Connection>::Element *> &output_connections, int node, RBSet<int> &processed, bool for_preview, RBSet<StringName> &r_classes) const {
  1387. const Ref<VisualShaderNode> vsnode = graph[type].nodes[node].node;
  1388. if (vsnode->is_disabled()) {
  1389. code += "// " + vsnode->get_caption() + ":" + itos(node) + "\n";
  1390. code += " // Node is disabled and code is not generated.\n";
  1391. return OK;
  1392. }
  1393. //check inputs recursively first
  1394. int input_count = vsnode->get_input_port_count();
  1395. for (int i = 0; i < input_count; i++) {
  1396. ConnectionKey ck;
  1397. ck.node = node;
  1398. ck.port = i;
  1399. if (input_connections.has(ck)) {
  1400. int from_node = input_connections[ck]->get().from_node;
  1401. if (processed.has(from_node)) {
  1402. continue;
  1403. }
  1404. Error err = _write_node(type, global_code, global_code_per_node, global_code_per_func, code, def_tex_params, input_connections, output_connections, from_node, processed, for_preview, r_classes);
  1405. if (err) {
  1406. return err;
  1407. }
  1408. }
  1409. }
  1410. // then this node
  1411. Vector<VisualShader::DefaultTextureParam> params = vsnode->get_default_texture_parameters(type, node);
  1412. for (int i = 0; i < params.size(); i++) {
  1413. def_tex_params.push_back(params[i]);
  1414. }
  1415. Ref<VisualShaderNodeInput> input = vsnode;
  1416. bool skip_global = input.is_valid() && for_preview;
  1417. if (!skip_global) {
  1418. Ref<VisualShaderNodeUniform> uniform = vsnode;
  1419. if (!uniform.is_valid() || !uniform->is_global_code_generated()) {
  1420. if (global_code) {
  1421. *global_code += vsnode->generate_global(get_mode(), type, node);
  1422. }
  1423. }
  1424. String class_name = vsnode->get_class_name();
  1425. if (class_name == "VisualShaderNodeCustom") {
  1426. class_name = vsnode->get_script_instance()->get_script()->get_path();
  1427. }
  1428. if (!r_classes.has(class_name)) {
  1429. if (global_code_per_node) {
  1430. *global_code_per_node += vsnode->generate_global_per_node(get_mode(), node);
  1431. }
  1432. for (int i = 0; i < TYPE_MAX; i++) {
  1433. if (global_code_per_func) {
  1434. (*global_code_per_func)[Type(i)] += vsnode->generate_global_per_func(get_mode(), Type(i), node);
  1435. }
  1436. }
  1437. r_classes.insert(class_name);
  1438. }
  1439. }
  1440. if (!vsnode->is_code_generated()) { // just generate globals and ignore locals
  1441. processed.insert(node);
  1442. return OK;
  1443. }
  1444. String node_name = "// " + vsnode->get_caption() + ":" + itos(node) + "\n";
  1445. String node_code;
  1446. Vector<String> input_vars;
  1447. input_vars.resize(vsnode->get_input_port_count());
  1448. String *inputs = input_vars.ptrw();
  1449. for (int i = 0; i < input_count; i++) {
  1450. ConnectionKey ck;
  1451. ck.node = node;
  1452. ck.port = i;
  1453. if (input_connections.has(ck)) {
  1454. //connected to something, use that output
  1455. int from_node = input_connections[ck]->get().from_node;
  1456. if (graph[type].nodes[from_node].node->is_disabled()) {
  1457. continue;
  1458. }
  1459. int from_port = input_connections[ck]->get().from_port;
  1460. VisualShaderNode::PortType in_type = vsnode->get_input_port_type(i);
  1461. VisualShaderNode::PortType out_type = graph[type].nodes[from_node].node->get_output_port_type(from_port);
  1462. String src_var = "n_out" + itos(from_node) + "p" + itos(from_port);
  1463. if (in_type == VisualShaderNode::PORT_TYPE_SAMPLER && out_type == VisualShaderNode::PORT_TYPE_SAMPLER) {
  1464. VisualShaderNode *ptr = const_cast<VisualShaderNode *>(graph[type].nodes[from_node].node.ptr());
  1465. if (ptr->has_method("get_input_real_name")) {
  1466. inputs[i] = ptr->call("get_input_real_name");
  1467. } else if (ptr->has_method("get_uniform_name")) {
  1468. inputs[i] = ptr->call("get_uniform_name");
  1469. } else {
  1470. inputs[i] = "";
  1471. }
  1472. } else if (in_type == out_type) {
  1473. inputs[i] = src_var;
  1474. } else {
  1475. switch (in_type) {
  1476. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1477. switch (out_type) {
  1478. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1479. inputs[i] = "float(" + src_var + ")";
  1480. } break;
  1481. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1482. inputs[i] = "(" + src_var + " ? 1.0 : 0.0)";
  1483. } break;
  1484. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1485. inputs[i] = "dot(" + src_var + ", vec2(0.5, 0.5))";
  1486. } break;
  1487. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1488. inputs[i] = "dot(" + src_var + ", vec3(0.333333, 0.333333, 0.333333))";
  1489. } break;
  1490. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1491. inputs[i] = "dot(" + src_var + ", vec4(0.25, 0.25, 0.25, 0.25))";
  1492. } break;
  1493. default:
  1494. break;
  1495. }
  1496. } break;
  1497. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1498. switch (out_type) {
  1499. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1500. inputs[i] = "int(" + src_var + ")";
  1501. } break;
  1502. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1503. inputs[i] = "(" + src_var + " ? 1 : 0)";
  1504. } break;
  1505. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1506. inputs[i] = "dot(float(" + src_var + "), vec2(0.5, 0.5))";
  1507. } break;
  1508. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1509. inputs[i] = "dot(float(" + src_var + "), vec3(0.333333, 0.333333, 0.333333))";
  1510. } break;
  1511. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1512. inputs[i] = "dot(float(" + src_var + "), vec4(0.25, 0.25, 0.25, 0.25))";
  1513. } break;
  1514. default:
  1515. break;
  1516. }
  1517. } break;
  1518. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1519. switch (out_type) {
  1520. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1521. inputs[i] = src_var + " > 0.0 ? true : false";
  1522. } break;
  1523. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1524. inputs[i] = src_var + " > 0 ? true : false";
  1525. } break;
  1526. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1527. inputs[i] = "all(bvec2(" + src_var + "))";
  1528. } break;
  1529. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1530. inputs[i] = "all(bvec3(" + src_var + "))";
  1531. } break;
  1532. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1533. inputs[i] = "all(bvec4(" + src_var + "))";
  1534. } break;
  1535. default:
  1536. break;
  1537. }
  1538. } break;
  1539. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1540. switch (out_type) {
  1541. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1542. inputs[i] = "vec2(" + src_var + ")";
  1543. } break;
  1544. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1545. inputs[i] = "vec2(float(" + src_var + "))";
  1546. } break;
  1547. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1548. inputs[i] = "vec2(" + src_var + " ? 1.0 : 0.0)";
  1549. } break;
  1550. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  1551. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1552. inputs[i] = "vec2(" + src_var + ".xy)";
  1553. } break;
  1554. default:
  1555. break;
  1556. }
  1557. } break;
  1558. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1559. switch (out_type) {
  1560. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1561. inputs[i] = "vec3(" + src_var + ")";
  1562. } break;
  1563. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1564. inputs[i] = "vec3(float(" + src_var + "))";
  1565. } break;
  1566. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1567. inputs[i] = "vec3(" + src_var + " ? 1.0 : 0.0)";
  1568. } break;
  1569. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1570. inputs[i] = "vec3(" + src_var + ", 0.0)";
  1571. } break;
  1572. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1573. inputs[i] = "vec3(" + src_var + ".xyz)";
  1574. } break;
  1575. default:
  1576. break;
  1577. }
  1578. } break;
  1579. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1580. switch (out_type) {
  1581. case VisualShaderNode::PORT_TYPE_SCALAR: {
  1582. inputs[i] = "vec4(" + src_var + ")";
  1583. } break;
  1584. case VisualShaderNode::PORT_TYPE_SCALAR_INT: {
  1585. inputs[i] = "vec4(float(" + src_var + "))";
  1586. } break;
  1587. case VisualShaderNode::PORT_TYPE_BOOLEAN: {
  1588. inputs[i] = "vec4(" + src_var + " ? 1.0 : 0.0)";
  1589. } break;
  1590. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1591. inputs[i] = "vec4(" + src_var + ", 0.0, 0.0)";
  1592. } break;
  1593. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1594. inputs[i] = "vec4(" + src_var + ", 0.0)";
  1595. } break;
  1596. default:
  1597. break;
  1598. }
  1599. } break;
  1600. default:
  1601. break;
  1602. }
  1603. }
  1604. } else {
  1605. if (!vsnode->is_generate_input_var(i)) {
  1606. continue;
  1607. }
  1608. Variant defval = vsnode->get_input_port_default_value(i);
  1609. if (defval.get_type() == Variant::FLOAT) {
  1610. float val = defval;
  1611. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1612. node_code += " float " + inputs[i] + " = " + vformat("%.5f", val) + ";\n";
  1613. } else if (defval.get_type() == Variant::INT) {
  1614. int val = defval;
  1615. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1616. node_code += " int " + inputs[i] + " = " + itos(val) + ";\n";
  1617. } else if (defval.get_type() == Variant::BOOL) {
  1618. bool val = defval;
  1619. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1620. node_code += " bool " + inputs[i] + " = " + (val ? "true" : "false") + ";\n";
  1621. } else if (defval.get_type() == Variant::VECTOR2) {
  1622. Vector2 val = defval;
  1623. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1624. node_code += " vec2 " + inputs[i] + " = " + vformat("vec2(%.5f, %.5f);\n", val.x, val.y);
  1625. } else if (defval.get_type() == Variant::VECTOR3) {
  1626. Vector3 val = defval;
  1627. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1628. node_code += " vec3 " + inputs[i] + " = " + vformat("vec3(%.5f, %.5f, %.5f);\n", val.x, val.y, val.z);
  1629. } else if (defval.get_type() == Variant::QUATERNION) {
  1630. Quaternion val = defval;
  1631. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1632. node_code += " vec4 " + inputs[i] + " = " + vformat("vec4(%.5f, %.5f, %.5f, %.5f);\n", val.x, val.y, val.z, val.w);
  1633. } else if (defval.get_type() == Variant::TRANSFORM3D) {
  1634. Transform3D val = defval;
  1635. val.basis.transpose();
  1636. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1637. Array values;
  1638. for (int j = 0; j < 3; j++) {
  1639. values.push_back(val.basis[j].x);
  1640. values.push_back(val.basis[j].y);
  1641. values.push_back(val.basis[j].z);
  1642. }
  1643. values.push_back(val.origin.x);
  1644. values.push_back(val.origin.y);
  1645. values.push_back(val.origin.z);
  1646. bool err = false;
  1647. node_code += " mat4 " + inputs[i] + " = " + String("mat4(vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 1.0));\n").sprintf(values, &err);
  1648. } else {
  1649. //will go empty, node is expected to know what it is doing at this point and handle it
  1650. }
  1651. }
  1652. }
  1653. int output_count = vsnode->get_output_port_count();
  1654. int initial_output_count = output_count;
  1655. HashMap<int, bool> expanded_output_ports;
  1656. for (int i = 0; i < initial_output_count; i++) {
  1657. bool expanded = false;
  1658. if (vsnode->is_output_port_expandable(i) && vsnode->_is_output_port_expanded(i)) {
  1659. expanded = true;
  1660. switch (vsnode->get_output_port_type(i)) {
  1661. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1662. output_count += 2;
  1663. } break;
  1664. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1665. output_count += 3;
  1666. } break;
  1667. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1668. output_count += 4;
  1669. } break;
  1670. default:
  1671. break;
  1672. }
  1673. }
  1674. expanded_output_ports.insert(i, expanded);
  1675. }
  1676. Vector<String> output_vars;
  1677. output_vars.resize(output_count);
  1678. String *outputs = output_vars.ptrw();
  1679. if (vsnode->is_simple_decl()) { // less code to generate for some simple_decl nodes
  1680. for (int i = 0, j = 0; i < initial_output_count; i++, j++) {
  1681. String var_name = "n_out" + itos(node) + "p" + itos(j);
  1682. switch (vsnode->get_output_port_type(i)) {
  1683. case VisualShaderNode::PORT_TYPE_SCALAR:
  1684. outputs[i] = "float " + var_name;
  1685. break;
  1686. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  1687. outputs[i] = "int " + var_name;
  1688. break;
  1689. case VisualShaderNode::PORT_TYPE_VECTOR_2D:
  1690. outputs[i] = "vec2 " + var_name;
  1691. break;
  1692. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  1693. outputs[i] = "vec3 " + var_name;
  1694. break;
  1695. case VisualShaderNode::PORT_TYPE_VECTOR_4D:
  1696. outputs[i] = "vec4 " + var_name;
  1697. break;
  1698. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1699. outputs[i] = "bool " + var_name;
  1700. break;
  1701. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1702. outputs[i] = "mat4 " + var_name;
  1703. break;
  1704. default:
  1705. break;
  1706. }
  1707. if (expanded_output_ports[i]) {
  1708. switch (vsnode->get_output_port_type(i)) {
  1709. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1710. j += 2;
  1711. } break;
  1712. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1713. j += 3;
  1714. } break;
  1715. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1716. j += 4;
  1717. } break;
  1718. default:
  1719. break;
  1720. }
  1721. }
  1722. }
  1723. } else {
  1724. for (int i = 0, j = 0; i < initial_output_count; i++, j++) {
  1725. outputs[i] = "n_out" + itos(node) + "p" + itos(j);
  1726. switch (vsnode->get_output_port_type(i)) {
  1727. case VisualShaderNode::PORT_TYPE_SCALAR:
  1728. code += " float " + outputs[i] + ";\n";
  1729. break;
  1730. case VisualShaderNode::PORT_TYPE_SCALAR_INT:
  1731. code += " int " + outputs[i] + ";\n";
  1732. break;
  1733. case VisualShaderNode::PORT_TYPE_VECTOR_2D:
  1734. code += " vec2 " + outputs[i] + ";\n";
  1735. break;
  1736. case VisualShaderNode::PORT_TYPE_VECTOR_3D:
  1737. code += " vec3 " + outputs[i] + ";\n";
  1738. break;
  1739. case VisualShaderNode::PORT_TYPE_VECTOR_4D:
  1740. code += " vec4 " + outputs[i] + ";\n";
  1741. break;
  1742. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1743. code += " bool " + outputs[i] + ";\n";
  1744. break;
  1745. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1746. code += " mat4 " + outputs[i] + ";\n";
  1747. break;
  1748. default:
  1749. break;
  1750. }
  1751. if (expanded_output_ports[i]) {
  1752. switch (vsnode->get_output_port_type(i)) {
  1753. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1754. j += 2;
  1755. } break;
  1756. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1757. j += 3;
  1758. } break;
  1759. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1760. j += 4;
  1761. } break;
  1762. default:
  1763. break;
  1764. }
  1765. }
  1766. }
  1767. }
  1768. node_code += vsnode->generate_code(get_mode(), type, node, inputs, outputs, for_preview);
  1769. if (!node_code.is_empty()) {
  1770. code += node_name;
  1771. code += node_code;
  1772. }
  1773. for (int i = 0; i < output_count; i++) {
  1774. if (expanded_output_ports[i]) {
  1775. switch (vsnode->get_output_port_type(i)) {
  1776. case VisualShaderNode::PORT_TYPE_VECTOR_2D: {
  1777. if (vsnode->is_output_port_connected(i + 1) || (for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  1778. String r = "n_out" + itos(node) + "p" + itos(i + 1);
  1779. code += " float " + r + " = n_out" + itos(node) + "p" + itos(i) + ".r;\n";
  1780. outputs[i + 1] = r;
  1781. }
  1782. if (vsnode->is_output_port_connected(i + 2) || (for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  1783. String g = "n_out" + itos(node) + "p" + itos(i + 2);
  1784. code += " float " + g + " = n_out" + itos(node) + "p" + itos(i) + ".g;\n";
  1785. outputs[i + 2] = g;
  1786. }
  1787. i += 2;
  1788. } break;
  1789. case VisualShaderNode::PORT_TYPE_VECTOR_3D: {
  1790. if (vsnode->is_output_port_connected(i + 1) || (for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  1791. String r = "n_out" + itos(node) + "p" + itos(i + 1);
  1792. code += " float " + r + " = n_out" + itos(node) + "p" + itos(i) + ".r;\n";
  1793. outputs[i + 1] = r;
  1794. }
  1795. if (vsnode->is_output_port_connected(i + 2) || (for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  1796. String g = "n_out" + itos(node) + "p" + itos(i + 2);
  1797. code += " float " + g + " = n_out" + itos(node) + "p" + itos(i) + ".g;\n";
  1798. outputs[i + 2] = g;
  1799. }
  1800. if (vsnode->is_output_port_connected(i + 3) || (for_preview && vsnode->get_output_port_for_preview() == (i + 3))) { // blue-component
  1801. String b = "n_out" + itos(node) + "p" + itos(i + 3);
  1802. code += " float " + b + " = n_out" + itos(node) + "p" + itos(i) + ".b;\n";
  1803. outputs[i + 3] = b;
  1804. }
  1805. i += 3;
  1806. } break;
  1807. case VisualShaderNode::PORT_TYPE_VECTOR_4D: {
  1808. if (vsnode->is_output_port_connected(i + 1) || (for_preview && vsnode->get_output_port_for_preview() == (i + 1))) { // red-component
  1809. String r = "n_out" + itos(node) + "p" + itos(i + 1);
  1810. code += " float " + r + " = n_out" + itos(node) + "p" + itos(i) + ".r;\n";
  1811. outputs[i + 1] = r;
  1812. }
  1813. if (vsnode->is_output_port_connected(i + 2) || (for_preview && vsnode->get_output_port_for_preview() == (i + 2))) { // green-component
  1814. String g = "n_out" + itos(node) + "p" + itos(i + 2);
  1815. code += " float " + g + " = n_out" + itos(node) + "p" + itos(i) + ".g;\n";
  1816. outputs[i + 2] = g;
  1817. }
  1818. if (vsnode->is_output_port_connected(i + 3) || (for_preview && vsnode->get_output_port_for_preview() == (i + 3))) { // blue-component
  1819. String b = "n_out" + itos(node) + "p" + itos(i + 3);
  1820. code += " float " + b + " = n_out" + itos(node) + "p" + itos(i) + ".b;\n";
  1821. outputs[i + 3] = b;
  1822. }
  1823. if (vsnode->is_output_port_connected(i + 4) || (for_preview && vsnode->get_output_port_for_preview() == (i + 4))) { // alpha-component
  1824. String a = "n_out" + itos(node) + "p" + itos(i + 4);
  1825. code += " float " + a + " = n_out" + itos(node) + "p" + itos(i) + ".a;\n";
  1826. outputs[i + 4] = a;
  1827. }
  1828. i += 4;
  1829. } break;
  1830. default:
  1831. break;
  1832. }
  1833. }
  1834. }
  1835. if (!node_code.is_empty()) {
  1836. code += "\n";
  1837. }
  1838. code += "\n"; //
  1839. processed.insert(node);
  1840. return OK;
  1841. }
  1842. bool VisualShader::has_func_name(RenderingServer::ShaderMode p_mode, const String &p_func_name) const {
  1843. if (!ShaderTypes::get_singleton()->get_functions(p_mode).has(p_func_name)) {
  1844. if (p_mode == RenderingServer::ShaderMode::SHADER_PARTICLES) {
  1845. if (p_func_name == "start_custom" || p_func_name == "process_custom" || p_func_name == "collide") {
  1846. return true;
  1847. }
  1848. }
  1849. return false;
  1850. }
  1851. return true;
  1852. }
  1853. void VisualShader::_update_shader() const {
  1854. if (!dirty.is_set()) {
  1855. return;
  1856. }
  1857. dirty.clear();
  1858. StringBuilder global_code;
  1859. StringBuilder global_code_per_node;
  1860. HashMap<Type, StringBuilder> global_code_per_func;
  1861. StringBuilder code;
  1862. Vector<VisualShader::DefaultTextureParam> default_tex_params;
  1863. RBSet<StringName> classes;
  1864. HashMap<int, int> insertion_pos;
  1865. static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles", "sky", "fog" };
  1866. global_code += String() + "shader_type " + shader_mode_str[shader_mode] + ";\n";
  1867. String render_mode;
  1868. {
  1869. const Vector<ShaderLanguage::ModeInfo> &rmodes = ShaderTypes::get_singleton()->get_modes(RenderingServer::ShaderMode(shader_mode));
  1870. Vector<String> flag_names;
  1871. // Add enum modes first.
  1872. for (int i = 0; i < rmodes.size(); i++) {
  1873. const ShaderLanguage::ModeInfo &info = rmodes[i];
  1874. const String temp = String(info.name);
  1875. if (!info.options.is_empty()) {
  1876. if (modes.has(temp) && modes[temp] < info.options.size()) {
  1877. if (!render_mode.is_empty()) {
  1878. render_mode += ", ";
  1879. }
  1880. render_mode += temp + "_" + info.options[modes[temp]];
  1881. }
  1882. } else if (flags.has(temp)) {
  1883. flag_names.push_back(temp);
  1884. }
  1885. }
  1886. // Add flags afterward.
  1887. for (int i = 0; i < flag_names.size(); i++) {
  1888. if (!render_mode.is_empty()) {
  1889. render_mode += ", ";
  1890. }
  1891. render_mode += flag_names[i];
  1892. }
  1893. }
  1894. if (!render_mode.is_empty()) {
  1895. global_code += "render_mode " + render_mode + ";\n\n";
  1896. }
  1897. static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light", "start", "process", "collide", "start_custom", "process_custom", "sky", "fog" };
  1898. String global_expressions;
  1899. RBSet<String> used_uniform_names;
  1900. List<VisualShaderNodeUniform *> uniforms;
  1901. HashMap<int, List<int>> emitters;
  1902. HashMap<int, List<int>> varying_setters;
  1903. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  1904. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  1905. continue;
  1906. }
  1907. for (const KeyValue<int, Node> &E : graph[i].nodes) {
  1908. Ref<VisualShaderNodeGlobalExpression> global_expression = E.value.node;
  1909. if (global_expression.is_valid()) {
  1910. String expr = "";
  1911. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  1912. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  1913. expr = expr.replace("\n", "\n ");
  1914. expr += "\n";
  1915. global_expressions += expr;
  1916. }
  1917. Ref<VisualShaderNodeUniformRef> uniform_ref = E.value.node;
  1918. if (uniform_ref.is_valid()) {
  1919. used_uniform_names.insert(uniform_ref->get_uniform_name());
  1920. }
  1921. Ref<VisualShaderNodeUniform> uniform = E.value.node;
  1922. if (uniform.is_valid()) {
  1923. uniforms.push_back(uniform.ptr());
  1924. }
  1925. Ref<VisualShaderNodeVaryingSetter> varying_setter = E.value.node;
  1926. if (varying_setter.is_valid() && varying_setter->is_input_port_connected(0)) {
  1927. if (!varying_setters.has(i)) {
  1928. varying_setters.insert(i, List<int>());
  1929. }
  1930. varying_setters[i].push_back(E.key);
  1931. }
  1932. Ref<VisualShaderNodeParticleEmit> emit_particle = E.value.node;
  1933. if (emit_particle.is_valid()) {
  1934. if (!emitters.has(i)) {
  1935. emitters.insert(i, List<int>());
  1936. }
  1937. emitters[i].push_back(E.key);
  1938. }
  1939. }
  1940. }
  1941. for (int i = 0; i < uniforms.size(); i++) {
  1942. VisualShaderNodeUniform *uniform = uniforms[i];
  1943. if (used_uniform_names.has(uniform->get_uniform_name())) {
  1944. global_code += uniform->generate_global(get_mode(), Type(i), -1);
  1945. const_cast<VisualShaderNodeUniform *>(uniform)->set_global_code_generated(true);
  1946. } else {
  1947. const_cast<VisualShaderNodeUniform *>(uniform)->set_global_code_generated(false);
  1948. }
  1949. }
  1950. if (!varyings.is_empty()) {
  1951. global_code += "\n// Varyings\n";
  1952. for (const KeyValue<String, Varying> &E : varyings) {
  1953. global_code += "varying ";
  1954. switch (E.value.type) {
  1955. case VaryingType::VARYING_TYPE_FLOAT:
  1956. global_code += "float ";
  1957. break;
  1958. case VaryingType::VARYING_TYPE_VECTOR_2D:
  1959. global_code += "vec2 ";
  1960. break;
  1961. case VaryingType::VARYING_TYPE_VECTOR_3D:
  1962. global_code += "vec3 ";
  1963. break;
  1964. case VaryingType::VARYING_TYPE_VECTOR_4D:
  1965. global_code += "vec4 ";
  1966. break;
  1967. case VaryingType::VARYING_TYPE_COLOR:
  1968. global_code += "vec4 ";
  1969. break;
  1970. case VaryingType::VARYING_TYPE_TRANSFORM:
  1971. global_code += "mat4 ";
  1972. break;
  1973. default:
  1974. break;
  1975. }
  1976. global_code += E.key + ";\n";
  1977. }
  1978. global_code += "\n";
  1979. }
  1980. HashMap<int, String> code_map;
  1981. RBSet<int> empty_funcs;
  1982. for (int i = 0; i < TYPE_MAX; i++) {
  1983. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  1984. continue;
  1985. }
  1986. //make it faster to go around through shader
  1987. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  1988. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  1989. StringBuilder func_code;
  1990. RBSet<int> processed;
  1991. bool is_empty_func = false;
  1992. if (shader_mode != Shader::MODE_PARTICLES && shader_mode != Shader::MODE_SKY && shader_mode != Shader::MODE_FOG) {
  1993. is_empty_func = true;
  1994. }
  1995. String varying_code;
  1996. if (shader_mode == Shader::MODE_SPATIAL || shader_mode == Shader::MODE_CANVAS_ITEM) {
  1997. for (const KeyValue<String, Varying> &E : varyings) {
  1998. if ((E.value.mode == VARYING_MODE_VERTEX_TO_FRAG_LIGHT && i == TYPE_VERTEX) || (E.value.mode == VARYING_MODE_FRAG_TO_LIGHT && i == TYPE_FRAGMENT)) {
  1999. bool found = false;
  2000. for (int key : varying_setters[i]) {
  2001. Ref<VisualShaderNodeVaryingSetter> setter = Object::cast_to<VisualShaderNodeVaryingSetter>(const_cast<VisualShaderNode *>(graph[i].nodes[key].node.ptr()));
  2002. if (setter.is_valid() && E.value.name == setter->get_varying_name()) {
  2003. found = true;
  2004. break;
  2005. }
  2006. }
  2007. if (!found) {
  2008. String code2;
  2009. switch (E.value.type) {
  2010. case VaryingType::VARYING_TYPE_FLOAT:
  2011. code2 += "0.0";
  2012. break;
  2013. case VaryingType::VARYING_TYPE_VECTOR_2D:
  2014. code2 += "vec2(0.0)";
  2015. break;
  2016. case VaryingType::VARYING_TYPE_VECTOR_3D:
  2017. code2 += "vec3(0.0)";
  2018. break;
  2019. case VaryingType::VARYING_TYPE_VECTOR_4D:
  2020. code2 += "vec4(0.0)";
  2021. break;
  2022. case VaryingType::VARYING_TYPE_COLOR:
  2023. code2 += "vec4(0.0)";
  2024. break;
  2025. case VaryingType::VARYING_TYPE_TRANSFORM:
  2026. code2 += "mat4(1.0)";
  2027. break;
  2028. default:
  2029. break;
  2030. }
  2031. varying_code += vformat(" %s = %s;\n", E.key, code2);
  2032. }
  2033. is_empty_func = false;
  2034. }
  2035. }
  2036. }
  2037. for (const List<Connection>::Element *E = graph[i].connections.front(); E; E = E->next()) {
  2038. ConnectionKey from_key;
  2039. from_key.node = E->get().from_node;
  2040. from_key.port = E->get().from_port;
  2041. output_connections.insert(from_key, E);
  2042. ConnectionKey to_key;
  2043. to_key.node = E->get().to_node;
  2044. to_key.port = E->get().to_port;
  2045. input_connections.insert(to_key, E);
  2046. if (is_empty_func && to_key.node == NODE_ID_OUTPUT) {
  2047. is_empty_func = false;
  2048. }
  2049. }
  2050. if (is_empty_func) {
  2051. empty_funcs.insert(i);
  2052. continue;
  2053. }
  2054. if (shader_mode != Shader::MODE_PARTICLES) {
  2055. func_code += "\nvoid " + String(func_name[i]) + "() {\n";
  2056. }
  2057. insertion_pos.insert(i, code.get_string_length() + func_code.get_string_length());
  2058. Error err = _write_node(Type(i), &global_code, &global_code_per_node, &global_code_per_func, func_code, default_tex_params, input_connections, output_connections, NODE_ID_OUTPUT, processed, false, classes);
  2059. ERR_FAIL_COND(err != OK);
  2060. if (varying_setters.has(i)) {
  2061. for (int &E : varying_setters[i]) {
  2062. err = _write_node(Type(i), nullptr, nullptr, nullptr, func_code, default_tex_params, input_connections, output_connections, E, processed, false, classes);
  2063. ERR_FAIL_COND(err != OK);
  2064. }
  2065. }
  2066. if (emitters.has(i)) {
  2067. for (int &E : emitters[i]) {
  2068. err = _write_node(Type(i), &global_code, &global_code_per_node, &global_code_per_func, func_code, default_tex_params, input_connections, output_connections, E, processed, false, classes);
  2069. ERR_FAIL_COND(err != OK);
  2070. }
  2071. }
  2072. if (shader_mode == Shader::MODE_PARTICLES) {
  2073. code_map.insert(i, func_code);
  2074. } else {
  2075. func_code += varying_code;
  2076. func_code += "}\n";
  2077. code += func_code;
  2078. }
  2079. }
  2080. String global_compute_code;
  2081. if (shader_mode == Shader::MODE_PARTICLES) {
  2082. bool has_start = !code_map[TYPE_START].is_empty();
  2083. bool has_start_custom = !code_map[TYPE_START_CUSTOM].is_empty();
  2084. bool has_process = !code_map[TYPE_PROCESS].is_empty();
  2085. bool has_process_custom = !code_map[TYPE_PROCESS_CUSTOM].is_empty();
  2086. bool has_collide = !code_map[TYPE_COLLIDE].is_empty();
  2087. code += "void start() {\n";
  2088. if (has_start || has_start_custom) {
  2089. code += " uint __seed = __hash(NUMBER + uint(1) + RANDOM_SEED);\n";
  2090. code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
  2091. code += " float __radians;\n";
  2092. code += " vec3 __vec3_buff1;\n";
  2093. code += " vec3 __vec3_buff2;\n";
  2094. code += " float __scalar_buff1;\n";
  2095. code += " float __scalar_buff2;\n";
  2096. code += " int __scalar_ibuff;\n";
  2097. code += " vec4 __vec4_buff;\n";
  2098. code += " vec3 __ndiff = normalize(__diff);\n\n";
  2099. }
  2100. if (has_start) {
  2101. code += " {\n";
  2102. code += code_map[TYPE_START].replace("\n ", "\n ");
  2103. code += " }\n";
  2104. if (has_start_custom) {
  2105. code += " \n";
  2106. }
  2107. }
  2108. if (has_start_custom) {
  2109. code += " {\n";
  2110. code += code_map[TYPE_START_CUSTOM].replace("\n ", "\n ");
  2111. code += " }\n";
  2112. }
  2113. code += "}\n\n";
  2114. code += "void process() {\n";
  2115. if (has_process || has_process_custom || has_collide) {
  2116. code += " uint __seed = __hash(NUMBER + uint(1) + RANDOM_SEED);\n";
  2117. code += " vec3 __vec3_buff1;\n";
  2118. code += " vec3 __diff = TRANSFORM[3].xyz - EMISSION_TRANSFORM[3].xyz;\n";
  2119. code += " vec3 __ndiff = normalize(__diff);\n\n";
  2120. }
  2121. code += " {\n";
  2122. String tab = " ";
  2123. if (has_collide) {
  2124. code += " if (COLLIDED) {\n\n";
  2125. code += code_map[TYPE_COLLIDE].replace("\n ", "\n ");
  2126. if (has_process) {
  2127. code += " } else {\n\n";
  2128. tab += " ";
  2129. }
  2130. }
  2131. if (has_process) {
  2132. code += code_map[TYPE_PROCESS].replace("\n ", "\n " + tab);
  2133. }
  2134. if (has_collide) {
  2135. code += " }\n";
  2136. }
  2137. code += " }\n";
  2138. if (has_process_custom) {
  2139. code += " {\n\n";
  2140. code += code_map[TYPE_PROCESS_CUSTOM].replace("\n ", "\n ");
  2141. code += " }\n";
  2142. }
  2143. code += "}\n\n";
  2144. global_compute_code += "float __rand_from_seed(inout uint seed) {\n";
  2145. global_compute_code += " int k;\n";
  2146. global_compute_code += " int s = int(seed);\n";
  2147. global_compute_code += " if (s == 0)\n";
  2148. global_compute_code += " s = 305420679;\n";
  2149. global_compute_code += " k = s / 127773;\n";
  2150. global_compute_code += " s = 16807 * (s - k * 127773) - 2836 * k;\n";
  2151. global_compute_code += " if (s < 0)\n";
  2152. global_compute_code += " s += 2147483647;\n";
  2153. global_compute_code += " seed = uint(s);\n";
  2154. global_compute_code += " return float(seed % uint(65536)) / 65535.0;\n";
  2155. global_compute_code += "}\n\n";
  2156. global_compute_code += "float __rand_from_seed_m1_p1(inout uint seed) {\n";
  2157. global_compute_code += " return __rand_from_seed(seed) * 2.0 - 1.0;\n";
  2158. global_compute_code += "}\n\n";
  2159. global_compute_code += "float __randf_range(inout uint seed, float from, float to) {\n";
  2160. global_compute_code += " return __rand_from_seed(seed) * (to - from) + from;\n";
  2161. global_compute_code += "}\n\n";
  2162. global_compute_code += "vec2 __randv2_range(inout uint seed, vec2 from, vec2 to) {\n";
  2163. global_compute_code += " return vec2(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y));\n";
  2164. global_compute_code += "}\n\n";
  2165. global_compute_code += "vec3 __randv3_range(inout uint seed, vec3 from, vec3 to) {\n";
  2166. global_compute_code += " return vec3(__randf_range(seed, from.x, to.x), __randf_range(seed, from.y, to.y), __randf_range(seed, from.z, to.z));\n";
  2167. global_compute_code += "}\n\n";
  2168. global_compute_code += "uint __hash(uint x) {\n";
  2169. global_compute_code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  2170. global_compute_code += " x = ((x >> uint(16)) ^ x) * uint(73244475);\n";
  2171. global_compute_code += " x = (x >> uint(16)) ^ x;\n";
  2172. global_compute_code += " return x;\n";
  2173. global_compute_code += "}\n\n";
  2174. global_compute_code += "mat3 __build_rotation_mat3(vec3 axis, float angle) {\n";
  2175. global_compute_code += " axis = normalize(axis);\n";
  2176. global_compute_code += " float s = sin(angle);\n";
  2177. global_compute_code += " float c = cos(angle);\n";
  2178. global_compute_code += " float oc = 1.0 - c;\n";
  2179. global_compute_code += " return mat3(vec3(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s), vec3(oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s), vec3(oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c));\n";
  2180. global_compute_code += "}\n\n";
  2181. global_compute_code += "mat4 __build_rotation_mat4(vec3 axis, float angle) {\n";
  2182. global_compute_code += " axis = normalize(axis);\n";
  2183. global_compute_code += " float s = sin(angle);\n";
  2184. global_compute_code += " float c = cos(angle);\n";
  2185. global_compute_code += " float oc = 1.0 - c;\n";
  2186. global_compute_code += " return mat4(vec4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0), vec4(oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0), vec4(oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0), vec4(0, 0, 0, 1));\n";
  2187. global_compute_code += "}\n\n";
  2188. global_compute_code += "vec2 __get_random_unit_vec2(inout uint seed) {\n";
  2189. global_compute_code += " return normalize(vec2(__rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed)));\n";
  2190. global_compute_code += "}\n\n";
  2191. global_compute_code += "vec3 __get_random_unit_vec3(inout uint seed) {\n";
  2192. global_compute_code += " return normalize(vec3(__rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed), __rand_from_seed_m1_p1(seed)));\n";
  2193. global_compute_code += "}\n\n";
  2194. }
  2195. //set code secretly
  2196. global_code += "\n\n";
  2197. String final_code = global_code;
  2198. final_code += global_compute_code;
  2199. final_code += global_code_per_node;
  2200. final_code += global_expressions;
  2201. String tcode = code;
  2202. for (int i = 0; i < TYPE_MAX; i++) {
  2203. if (!has_func_name(RenderingServer::ShaderMode(shader_mode), func_name[i])) {
  2204. continue;
  2205. }
  2206. String func_code = global_code_per_func[Type(i)].as_string();
  2207. if (empty_funcs.has(Type(i)) && !func_code.is_empty()) {
  2208. func_code = vformat("%s%s%s", String("\nvoid " + String(func_name[i]) + "() {\n"), func_code, "}\n");
  2209. }
  2210. tcode = tcode.insert(insertion_pos[i], func_code);
  2211. }
  2212. final_code += tcode;
  2213. const_cast<VisualShader *>(this)->set_code(final_code);
  2214. for (int i = 0; i < default_tex_params.size(); i++) {
  2215. for (int j = 0; j < default_tex_params[i].params.size(); j++) {
  2216. const_cast<VisualShader *>(this)->set_default_texture_param(default_tex_params[i].name, default_tex_params[i].params[j], j);
  2217. }
  2218. }
  2219. if (previous_code != final_code) {
  2220. const_cast<VisualShader *>(this)->emit_signal(SNAME("changed"));
  2221. }
  2222. previous_code = final_code;
  2223. }
  2224. void VisualShader::_queue_update() {
  2225. if (dirty.is_set()) {
  2226. return;
  2227. }
  2228. dirty.set();
  2229. call_deferred(SNAME("_update_shader"));
  2230. }
  2231. void VisualShader::rebuild() {
  2232. dirty.set();
  2233. _update_shader();
  2234. }
  2235. void VisualShader::_bind_methods() {
  2236. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShader::set_mode);
  2237. ClassDB::bind_method(D_METHOD("add_node", "type", "node", "position", "id"), &VisualShader::add_node);
  2238. ClassDB::bind_method(D_METHOD("get_node", "type", "id"), &VisualShader::get_node);
  2239. ClassDB::bind_method(D_METHOD("set_node_position", "type", "id", "position"), &VisualShader::set_node_position);
  2240. ClassDB::bind_method(D_METHOD("get_node_position", "type", "id"), &VisualShader::get_node_position);
  2241. ClassDB::bind_method(D_METHOD("get_node_list", "type"), &VisualShader::get_node_list);
  2242. ClassDB::bind_method(D_METHOD("get_valid_node_id", "type"), &VisualShader::get_valid_node_id);
  2243. ClassDB::bind_method(D_METHOD("remove_node", "type", "id"), &VisualShader::remove_node);
  2244. ClassDB::bind_method(D_METHOD("replace_node", "type", "id", "new_class"), &VisualShader::replace_node);
  2245. ClassDB::bind_method(D_METHOD("is_node_connection", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  2246. ClassDB::bind_method(D_METHOD("can_connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::can_connect_nodes);
  2247. ClassDB::bind_method(D_METHOD("connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes);
  2248. ClassDB::bind_method(D_METHOD("disconnect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::disconnect_nodes);
  2249. ClassDB::bind_method(D_METHOD("connect_nodes_forced", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes_forced);
  2250. ClassDB::bind_method(D_METHOD("get_node_connections", "type"), &VisualShader::_get_node_connections);
  2251. ClassDB::bind_method(D_METHOD("set_engine_version", "version"), &VisualShader::set_engine_version);
  2252. ClassDB::bind_method(D_METHOD("get_engine_version"), &VisualShader::get_engine_version);
  2253. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &VisualShader::set_graph_offset);
  2254. ClassDB::bind_method(D_METHOD("get_graph_offset"), &VisualShader::get_graph_offset);
  2255. ClassDB::bind_method(D_METHOD("add_varying", "name", "mode", "type"), &VisualShader::add_varying);
  2256. ClassDB::bind_method(D_METHOD("remove_varying", "name"), &VisualShader::remove_varying);
  2257. ClassDB::bind_method(D_METHOD("has_varying", "name"), &VisualShader::has_varying);
  2258. ClassDB::bind_method(D_METHOD("_update_shader"), &VisualShader::_update_shader);
  2259. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  2260. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "engine_version", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_engine_version", "get_engine_version");
  2261. ADD_PROPERTY_DEFAULT("code", ""); // Inherited from Shader, prevents showing default code as override in docs.
  2262. BIND_ENUM_CONSTANT(TYPE_VERTEX);
  2263. BIND_ENUM_CONSTANT(TYPE_FRAGMENT);
  2264. BIND_ENUM_CONSTANT(TYPE_LIGHT);
  2265. BIND_ENUM_CONSTANT(TYPE_START);
  2266. BIND_ENUM_CONSTANT(TYPE_PROCESS);
  2267. BIND_ENUM_CONSTANT(TYPE_COLLIDE);
  2268. BIND_ENUM_CONSTANT(TYPE_START_CUSTOM);
  2269. BIND_ENUM_CONSTANT(TYPE_PROCESS_CUSTOM);
  2270. BIND_ENUM_CONSTANT(TYPE_SKY);
  2271. BIND_ENUM_CONSTANT(TYPE_FOG);
  2272. BIND_ENUM_CONSTANT(TYPE_MAX);
  2273. BIND_ENUM_CONSTANT(VARYING_MODE_VERTEX_TO_FRAG_LIGHT);
  2274. BIND_ENUM_CONSTANT(VARYING_MODE_FRAG_TO_LIGHT);
  2275. BIND_ENUM_CONSTANT(VARYING_MODE_MAX);
  2276. BIND_ENUM_CONSTANT(VARYING_TYPE_FLOAT);
  2277. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_2D);
  2278. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_3D);
  2279. BIND_ENUM_CONSTANT(VARYING_TYPE_VECTOR_4D);
  2280. BIND_ENUM_CONSTANT(VARYING_TYPE_COLOR);
  2281. BIND_ENUM_CONSTANT(VARYING_TYPE_TRANSFORM);
  2282. BIND_ENUM_CONSTANT(VARYING_TYPE_MAX);
  2283. BIND_CONSTANT(NODE_ID_INVALID);
  2284. BIND_CONSTANT(NODE_ID_OUTPUT);
  2285. }
  2286. VisualShader::VisualShader() {
  2287. dirty.set();
  2288. for (int i = 0; i < TYPE_MAX; i++) {
  2289. if (i > (int)TYPE_LIGHT && i < (int)TYPE_SKY) {
  2290. Ref<VisualShaderNodeParticleOutput> output;
  2291. output.instantiate();
  2292. output->shader_type = Type(i);
  2293. output->shader_mode = shader_mode;
  2294. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  2295. } else {
  2296. Ref<VisualShaderNodeOutput> output;
  2297. output.instantiate();
  2298. output->shader_type = Type(i);
  2299. output->shader_mode = shader_mode;
  2300. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  2301. }
  2302. graph[i].nodes[NODE_ID_OUTPUT].position = Vector2(400, 150);
  2303. }
  2304. }
  2305. ///////////////////////////////////////////////////////////
  2306. const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = {
  2307. // Node3D
  2308. // Node3D, Vertex
  2309. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "vertex", "VERTEX" },
  2310. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "vertex_id", "VERTEX_ID" },
  2311. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2312. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "TANGENT" },
  2313. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "BINORMAL" },
  2314. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2315. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2316. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2317. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  2318. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "instance_id", "INSTANCE_ID" },
  2319. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "instance_custom", "INSTANCE_CUSTOM" },
  2320. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  2321. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2322. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "modelview_matrix", "MODELVIEW_MATRIX" },
  2323. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2324. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2325. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2326. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2327. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2328. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2329. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2330. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_index", "VIEW_INDEX" },
  2331. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_mono_left", "VIEW_MONO_LEFT" },
  2332. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_right", "VIEW_RIGHT" },
  2333. // Node3D, Fragment
  2334. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2335. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "vertex", "VERTEX" },
  2336. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2337. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "TANGENT" },
  2338. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "BINORMAL" },
  2339. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "view", "VIEW" },
  2340. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2341. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2342. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2343. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2344. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2345. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2346. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2347. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2348. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2349. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2350. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2351. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2352. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2353. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "front_facing", "FRONT_FACING" },
  2354. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "screen_texture", "SCREEN_TEXTURE" },
  2355. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "normal_roughness_texture", "NORMAL_ROUGHNESS_TEXTURE" },
  2356. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "depth_texture", "DEPTH_TEXTURE" },
  2357. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_index", "VIEW_INDEX" },
  2358. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_mono_left", "VIEW_MONO_LEFT" },
  2359. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR_INT, "view_right", "VIEW_RIGHT" },
  2360. // Node3D, Light
  2361. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2362. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2363. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2364. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV2" },
  2365. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "view", "VIEW" },
  2366. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light", "LIGHT" },
  2367. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_color", "LIGHT_COLOR" },
  2368. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "attenuation", "ATTENUATION" },
  2369. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "albedo", "ALBEDO" },
  2370. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "backlight", "BACKLIGHT" },
  2371. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "diffuse", "DIFFUSE_LIGHT" },
  2372. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "specular", "SPECULAR_LIGHT" },
  2373. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  2374. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "metallic", "METALLIC" },
  2375. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2376. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "view_matrix", "VIEW_MATRIX" },
  2377. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_view_matrix", "INV_VIEW_MATRIX" },
  2378. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection_matrix", "PROJECTION_MATRIX" },
  2379. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection_matrix", "INV_PROJECTION_MATRIX" },
  2380. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2381. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "VIEWPORT_SIZE" },
  2382. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  2383. // Canvas Item
  2384. // Canvas Item, Vertex
  2385. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2386. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2387. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2388. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  2389. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2390. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "model_matrix", "MODEL_MATRIX" },
  2391. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "canvas_matrix", "CANVAS_MATRIX" },
  2392. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "screen_matrix", "SCREEN_MATRIX" },
  2393. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2394. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_light_pass", "AT_LIGHT_PASS" },
  2395. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "instance_custom", "INSTANCE_CUSTOM" },
  2396. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "instance_id", "INSTANCE_ID" },
  2397. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR_INT, "vertex_id", "VERTEX_ID" },
  2398. // Canvas Item, Fragment
  2399. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2400. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2401. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2402. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2403. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2404. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_pixel_size", "SCREEN_PIXEL_SIZE" },
  2405. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2406. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2407. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_light_pass", "AT_LIGHT_PASS" },
  2408. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  2409. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "normal_texture", "NORMAL_TEXTURE" },
  2410. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "screen_texture", "SCREEN_TEXTURE" },
  2411. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "specular_shininess", "SPECULAR_SHININESS" },
  2412. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "specular_shininess_texture", "SPECULAR_SHININESS_TEXTURE" },
  2413. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2414. // Canvas Item, Light
  2415. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2416. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2417. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "NORMAL" },
  2418. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2419. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "light", "LIGHT" },
  2420. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "light_color", "LIGHT_COLOR" },
  2421. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_position", "LIGHT_POSITION" },
  2422. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light_vertex", "LIGHT_VERTEX" },
  2423. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "shadow", "SHADOW_MODULATE" },
  2424. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2425. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "texture_pixel_size", "TEXTURE_PIXEL_SIZE" },
  2426. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "point_coord", "POINT_COORD" },
  2427. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2428. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  2429. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "specular_shininess", "SPECULAR_SHININESS" },
  2430. // Particles, Start
  2431. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2432. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2433. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2434. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2435. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2436. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2437. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2438. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2439. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2440. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR_INT, "index", "INDEX" },
  2441. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2442. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2443. // Particles, Start (Custom)
  2444. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2445. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2446. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2447. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2448. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2449. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2450. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2451. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2452. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2453. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_INT, "index", "INDEX" },
  2454. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2455. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2456. // Particles, Process
  2457. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2458. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2459. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2460. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2461. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2462. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2463. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2464. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2465. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2466. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR_INT, "index", "INDEX" },
  2467. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2468. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2469. // Particles, Process (Custom)
  2470. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2471. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2472. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2473. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2474. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2475. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2476. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2477. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2478. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2479. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR_INT, "index", "INDEX" },
  2480. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2481. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2482. // Particles, Collide
  2483. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "attractor_force", "ATTRACTOR_FORCE" },
  2484. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "collision_depth", "COLLISION_DEPTH" },
  2485. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "collision_normal", "COLLISION_NORMAL" },
  2486. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "COLOR" },
  2487. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_3D, "velocity", "VELOCITY" },
  2488. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_BOOLEAN, "restart", "RESTART" },
  2489. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_BOOLEAN, "active", "ACTIVE" },
  2490. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_VECTOR_4D, "custom", "CUSTOM" },
  2491. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  2492. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  2493. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  2494. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR_INT, "index", "INDEX" },
  2495. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  2496. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2497. // Sky, Sky
  2498. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_cubemap_pass", "AT_CUBEMAP_PASS" },
  2499. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_half_res_pass", "AT_HALF_RES_PASS" },
  2500. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "at_quarter_res_pass", "AT_QUARTER_RES_PASS" },
  2501. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "eyedir", "EYEDIR" },
  2502. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "half_res_color", "HALF_RES_COLOR" },
  2503. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light0_color", "LIGHT0_COLOR" },
  2504. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light0_direction", "LIGHT0_DIRECTION" },
  2505. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light0_enabled", "LIGHT0_ENABLED" },
  2506. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light0_energy", "LIGHT0_ENERGY" },
  2507. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light1_color", "LIGHT1_COLOR" },
  2508. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light1_direction", "LIGHT1_DIRECTION" },
  2509. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light1_enabled", "LIGHT1_ENABLED" },
  2510. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light1_energy", "LIGHT1_ENERGY" },
  2511. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light2_color", "LIGHT2_COLOR" },
  2512. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light2_direction", "LIGHT2_DIRECTION" },
  2513. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light2_enabled", "LIGHT2_ENABLED" },
  2514. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light2_energy", "LIGHT2_ENERGY" },
  2515. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light3_color", "LIGHT3_COLOR" },
  2516. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "light3_direction", "LIGHT3_DIRECTION" },
  2517. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_BOOLEAN, "light3_enabled", "LIGHT3_ENABLED" },
  2518. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "light3_energy", "LIGHT3_ENERGY" },
  2519. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "position", "POSITION" },
  2520. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "quarter_res_color", "QUARTER_RES_COLOR" },
  2521. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SAMPLER, "radiance", "RADIANCE" },
  2522. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2523. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "sky_coords", "SKY_COORDS" },
  2524. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2525. // Fog, Fog
  2526. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "world_position", "WORLD_POSITION" },
  2527. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "object_position", "OBJECT_POSITION" },
  2528. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "uvw", "UVW" },
  2529. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "extents", "EXTENTS" },
  2530. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "sdf", "SDF" },
  2531. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2532. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  2533. };
  2534. const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = {
  2535. // Spatial, Vertex
  2536. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2537. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "vec3(0.0, 1.0, 0.0)" },
  2538. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "vec3(1.0, 0.0, 0.0)" },
  2539. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2540. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2541. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2542. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  2543. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2544. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2545. // Spatial, Fragment
  2546. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2547. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2548. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "tangent", "vec3(0.0, 1.0, 0.0)" },
  2549. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "binormal", "vec3(1.0, 0.0, 0.0)" },
  2550. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2551. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2552. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2553. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  2554. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2555. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2556. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2557. // Spatial, Light
  2558. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2559. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2560. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2561. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv2", "UV" },
  2562. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "viewport_size", "vec2(1.0)" },
  2563. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2564. // Canvas Item, Vertex
  2565. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "vertex", "VERTEX" },
  2566. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2567. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2568. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  2569. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2570. // Canvas Item, Fragment
  2571. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2572. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2573. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec3(1.0)" },
  2574. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  2575. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2576. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2577. // Canvas Item, Light
  2578. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "fragcoord", "FRAGCOORD" },
  2579. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "uv", "UV" },
  2580. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "normal", "vec3(0.0, 0.0, 1.0)" },
  2581. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "color", "vec4(1.0)" },
  2582. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  2583. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2584. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2585. // Particles
  2586. { Shader::MODE_PARTICLES, VisualShader::TYPE_START, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2587. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2588. { Shader::MODE_PARTICLES, VisualShader::TYPE_COLLIDE, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2589. { Shader::MODE_PARTICLES, VisualShader::TYPE_START_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2590. { Shader::MODE_PARTICLES, VisualShader::TYPE_PROCESS_CUSTOM, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2591. // Sky
  2592. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_2D, "screen_uv", "SCREEN_UV" },
  2593. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2594. // Fog
  2595. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  2596. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  2597. };
  2598. int VisualShaderNodeInput::get_input_port_count() const {
  2599. return 0;
  2600. }
  2601. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_port_type(int p_port) const {
  2602. return PORT_TYPE_SCALAR;
  2603. }
  2604. String VisualShaderNodeInput::get_input_port_name(int p_port) const {
  2605. return "";
  2606. }
  2607. int VisualShaderNodeInput::get_output_port_count() const {
  2608. return 1;
  2609. }
  2610. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_output_port_type(int p_port) const {
  2611. return p_port == 0 ? get_input_type_by_name(input_name) : PORT_TYPE_SCALAR;
  2612. }
  2613. String VisualShaderNodeInput::get_output_port_name(int p_port) const {
  2614. return "";
  2615. }
  2616. String VisualShaderNodeInput::get_caption() const {
  2617. return "Input";
  2618. }
  2619. bool VisualShaderNodeInput::is_output_port_expandable(int p_port) const {
  2620. if (p_port == 0) {
  2621. switch (get_input_type_by_name(input_name)) {
  2622. case PORT_TYPE_VECTOR_2D:
  2623. return true;
  2624. case PORT_TYPE_VECTOR_3D:
  2625. return true;
  2626. case PORT_TYPE_VECTOR_4D:
  2627. return true;
  2628. default:
  2629. return false;
  2630. }
  2631. }
  2632. return false;
  2633. }
  2634. String VisualShaderNodeInput::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2635. if (get_output_port_type(0) == PORT_TYPE_SAMPLER) {
  2636. return "";
  2637. }
  2638. if (p_for_preview) {
  2639. int idx = 0;
  2640. String code;
  2641. while (preview_ports[idx].mode != Shader::MODE_MAX) {
  2642. if (preview_ports[idx].mode == shader_mode && preview_ports[idx].shader_type == shader_type && preview_ports[idx].name == input_name) {
  2643. code = " " + p_output_vars[0] + " = " + preview_ports[idx].string + ";\n";
  2644. break;
  2645. }
  2646. idx++;
  2647. }
  2648. if (code.is_empty()) {
  2649. switch (get_output_port_type(0)) {
  2650. case PORT_TYPE_SCALAR: {
  2651. code = " " + p_output_vars[0] + " = 0.0;\n";
  2652. } break;
  2653. case PORT_TYPE_SCALAR_INT: {
  2654. code = " " + p_output_vars[0] + " = 0;\n";
  2655. } break;
  2656. case PORT_TYPE_VECTOR_2D: {
  2657. code = " " + p_output_vars[0] + " = vec2(0.0);\n";
  2658. } break;
  2659. case PORT_TYPE_VECTOR_3D: {
  2660. code = " " + p_output_vars[0] + " = vec3(0.0);\n";
  2661. } break;
  2662. case PORT_TYPE_VECTOR_4D: {
  2663. code = " " + p_output_vars[0] + " = vec4(0.0);\n";
  2664. } break;
  2665. case PORT_TYPE_BOOLEAN: {
  2666. code = " " + p_output_vars[0] + " = false;\n";
  2667. } break;
  2668. default:
  2669. break;
  2670. }
  2671. }
  2672. return code;
  2673. } else {
  2674. int idx = 0;
  2675. String code;
  2676. while (ports[idx].mode != Shader::MODE_MAX) {
  2677. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  2678. code = " " + p_output_vars[0] + " = " + ports[idx].string + ";\n";
  2679. break;
  2680. }
  2681. idx++;
  2682. }
  2683. if (code.is_empty()) {
  2684. code = " " + p_output_vars[0] + " = 0.0;\n"; //default (none found) is scalar
  2685. }
  2686. return code;
  2687. }
  2688. }
  2689. void VisualShaderNodeInput::set_input_name(String p_name) {
  2690. PortType prev_type = get_input_type_by_name(input_name);
  2691. input_name = p_name;
  2692. emit_changed();
  2693. if (get_input_type_by_name(input_name) != prev_type) {
  2694. emit_signal(SNAME("input_type_changed"));
  2695. }
  2696. }
  2697. String VisualShaderNodeInput::get_input_name() const {
  2698. return input_name;
  2699. }
  2700. String VisualShaderNodeInput::get_input_real_name() const {
  2701. int idx = 0;
  2702. while (ports[idx].mode != Shader::MODE_MAX) {
  2703. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  2704. return String(ports[idx].string);
  2705. }
  2706. idx++;
  2707. }
  2708. return "";
  2709. }
  2710. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_type_by_name(String p_name) const {
  2711. int idx = 0;
  2712. while (ports[idx].mode != Shader::MODE_MAX) {
  2713. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == p_name) {
  2714. return ports[idx].type;
  2715. }
  2716. idx++;
  2717. }
  2718. return PORT_TYPE_SCALAR;
  2719. }
  2720. int VisualShaderNodeInput::get_input_index_count() const {
  2721. int idx = 0;
  2722. int count = 0;
  2723. while (ports[idx].mode != Shader::MODE_MAX) {
  2724. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  2725. count++;
  2726. }
  2727. idx++;
  2728. }
  2729. return count;
  2730. }
  2731. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_index_type(int p_index) const {
  2732. int idx = 0;
  2733. int count = 0;
  2734. while (ports[idx].mode != Shader::MODE_MAX) {
  2735. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  2736. if (count == p_index) {
  2737. return ports[idx].type;
  2738. }
  2739. count++;
  2740. }
  2741. idx++;
  2742. }
  2743. return PORT_TYPE_SCALAR;
  2744. }
  2745. String VisualShaderNodeInput::get_input_index_name(int p_index) const {
  2746. int idx = 0;
  2747. int count = 0;
  2748. while (ports[idx].mode != Shader::MODE_MAX) {
  2749. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  2750. if (count == p_index) {
  2751. return ports[idx].name;
  2752. }
  2753. count++;
  2754. }
  2755. idx++;
  2756. }
  2757. return "";
  2758. }
  2759. void VisualShaderNodeInput::_validate_property(PropertyInfo &property) const {
  2760. if (property.name == "input_name") {
  2761. String port_list;
  2762. int idx = 0;
  2763. while (ports[idx].mode != Shader::MODE_MAX) {
  2764. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  2765. if (!port_list.is_empty()) {
  2766. port_list += ",";
  2767. }
  2768. port_list += ports[idx].name;
  2769. }
  2770. idx++;
  2771. }
  2772. if (port_list.is_empty()) {
  2773. port_list = RTR("None");
  2774. }
  2775. property.hint_string = port_list;
  2776. }
  2777. }
  2778. Vector<StringName> VisualShaderNodeInput::get_editable_properties() const {
  2779. Vector<StringName> props;
  2780. props.push_back("input_name");
  2781. return props;
  2782. }
  2783. void VisualShaderNodeInput::set_shader_type(VisualShader::Type p_shader_type) {
  2784. shader_type = p_shader_type;
  2785. }
  2786. void VisualShaderNodeInput::set_shader_mode(Shader::Mode p_shader_mode) {
  2787. shader_mode = p_shader_mode;
  2788. }
  2789. void VisualShaderNodeInput::_bind_methods() {
  2790. ClassDB::bind_method(D_METHOD("set_input_name", "name"), &VisualShaderNodeInput::set_input_name);
  2791. ClassDB::bind_method(D_METHOD("get_input_name"), &VisualShaderNodeInput::get_input_name);
  2792. ClassDB::bind_method(D_METHOD("get_input_real_name"), &VisualShaderNodeInput::get_input_real_name);
  2793. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "input_name", PROPERTY_HINT_ENUM, ""), "set_input_name", "get_input_name");
  2794. ADD_SIGNAL(MethodInfo("input_type_changed"));
  2795. }
  2796. VisualShaderNodeInput::VisualShaderNodeInput() {
  2797. }
  2798. ////////////// UniformRef
  2799. List<VisualShaderNodeUniformRef::Uniform> uniforms;
  2800. void VisualShaderNodeUniformRef::add_uniform(const String &p_name, UniformType p_type) {
  2801. uniforms.push_back({ p_name, p_type });
  2802. }
  2803. void VisualShaderNodeUniformRef::clear_uniforms() {
  2804. uniforms.clear();
  2805. }
  2806. bool VisualShaderNodeUniformRef::has_uniform(const String &p_name) {
  2807. for (const VisualShaderNodeUniformRef::Uniform &E : uniforms) {
  2808. if (E.name == p_name) {
  2809. return true;
  2810. }
  2811. }
  2812. return false;
  2813. }
  2814. String VisualShaderNodeUniformRef::get_caption() const {
  2815. return "UniformRef";
  2816. }
  2817. int VisualShaderNodeUniformRef::get_input_port_count() const {
  2818. return 0;
  2819. }
  2820. VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_input_port_type(int p_port) const {
  2821. return PortType::PORT_TYPE_SCALAR;
  2822. }
  2823. String VisualShaderNodeUniformRef::get_input_port_name(int p_port) const {
  2824. return "";
  2825. }
  2826. int VisualShaderNodeUniformRef::get_output_port_count() const {
  2827. switch (uniform_type) {
  2828. case UniformType::UNIFORM_TYPE_FLOAT:
  2829. return 1;
  2830. case UniformType::UNIFORM_TYPE_INT:
  2831. return 1;
  2832. case UniformType::UNIFORM_TYPE_BOOLEAN:
  2833. return 1;
  2834. case UniformType::UNIFORM_TYPE_VECTOR2:
  2835. return 1;
  2836. case UniformType::UNIFORM_TYPE_VECTOR3:
  2837. return 1;
  2838. case UniformType::UNIFORM_TYPE_VECTOR4:
  2839. return 1;
  2840. case UniformType::UNIFORM_TYPE_TRANSFORM:
  2841. return 1;
  2842. case UniformType::UNIFORM_TYPE_COLOR:
  2843. return 2;
  2844. case UniformType::UNIFORM_TYPE_SAMPLER:
  2845. return 1;
  2846. default:
  2847. break;
  2848. }
  2849. return 1;
  2850. }
  2851. VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_output_port_type(int p_port) const {
  2852. switch (uniform_type) {
  2853. case UniformType::UNIFORM_TYPE_FLOAT:
  2854. return PortType::PORT_TYPE_SCALAR;
  2855. case UniformType::UNIFORM_TYPE_INT:
  2856. return PortType::PORT_TYPE_SCALAR_INT;
  2857. case UniformType::UNIFORM_TYPE_BOOLEAN:
  2858. return PortType::PORT_TYPE_BOOLEAN;
  2859. case UniformType::UNIFORM_TYPE_VECTOR2:
  2860. return PortType::PORT_TYPE_VECTOR_2D;
  2861. case UniformType::UNIFORM_TYPE_VECTOR3:
  2862. return PortType::PORT_TYPE_VECTOR_3D;
  2863. case UniformType::UNIFORM_TYPE_VECTOR4:
  2864. return PortType::PORT_TYPE_VECTOR_4D;
  2865. case UniformType::UNIFORM_TYPE_TRANSFORM:
  2866. return PortType::PORT_TYPE_TRANSFORM;
  2867. case UniformType::UNIFORM_TYPE_COLOR:
  2868. if (p_port == 0) {
  2869. return PortType::PORT_TYPE_VECTOR_3D;
  2870. } else if (p_port == 1) {
  2871. return PORT_TYPE_SCALAR;
  2872. }
  2873. break;
  2874. case UniformType::UNIFORM_TYPE_SAMPLER:
  2875. return PortType::PORT_TYPE_SAMPLER;
  2876. default:
  2877. break;
  2878. }
  2879. return PORT_TYPE_SCALAR;
  2880. }
  2881. String VisualShaderNodeUniformRef::get_output_port_name(int p_port) const {
  2882. switch (uniform_type) {
  2883. case UniformType::UNIFORM_TYPE_FLOAT:
  2884. return "";
  2885. case UniformType::UNIFORM_TYPE_INT:
  2886. return "";
  2887. case UniformType::UNIFORM_TYPE_BOOLEAN:
  2888. return "";
  2889. case UniformType::UNIFORM_TYPE_VECTOR2:
  2890. return "";
  2891. case UniformType::UNIFORM_TYPE_VECTOR3:
  2892. return "";
  2893. case UniformType::UNIFORM_TYPE_VECTOR4:
  2894. return "";
  2895. case UniformType::UNIFORM_TYPE_TRANSFORM:
  2896. return "";
  2897. case UniformType::UNIFORM_TYPE_COLOR:
  2898. if (p_port == 0) {
  2899. return "rgb";
  2900. } else if (p_port == 1) {
  2901. return "alpha";
  2902. }
  2903. break;
  2904. case UniformType::UNIFORM_TYPE_SAMPLER:
  2905. return "";
  2906. break;
  2907. default:
  2908. break;
  2909. }
  2910. return "";
  2911. }
  2912. void VisualShaderNodeUniformRef::set_uniform_name(const String &p_name) {
  2913. uniform_name = p_name;
  2914. if (uniform_name != "[None]") {
  2915. uniform_type = get_uniform_type_by_name(uniform_name);
  2916. } else {
  2917. uniform_type = UniformType::UNIFORM_TYPE_FLOAT;
  2918. }
  2919. emit_changed();
  2920. }
  2921. String VisualShaderNodeUniformRef::get_uniform_name() const {
  2922. return uniform_name;
  2923. }
  2924. int VisualShaderNodeUniformRef::get_uniforms_count() const {
  2925. return uniforms.size();
  2926. }
  2927. String VisualShaderNodeUniformRef::get_uniform_name_by_index(int p_idx) const {
  2928. if (p_idx >= 0 && p_idx < uniforms.size()) {
  2929. return uniforms[p_idx].name;
  2930. }
  2931. return "";
  2932. }
  2933. VisualShaderNodeUniformRef::UniformType VisualShaderNodeUniformRef::get_uniform_type_by_name(const String &p_name) const {
  2934. for (int i = 0; i < uniforms.size(); i++) {
  2935. if (uniforms[i].name == p_name) {
  2936. return uniforms[i].type;
  2937. }
  2938. }
  2939. return UniformType::UNIFORM_TYPE_FLOAT;
  2940. }
  2941. VisualShaderNodeUniformRef::UniformType VisualShaderNodeUniformRef::get_uniform_type_by_index(int p_idx) const {
  2942. if (p_idx >= 0 && p_idx < uniforms.size()) {
  2943. return uniforms[p_idx].type;
  2944. }
  2945. return UniformType::UNIFORM_TYPE_FLOAT;
  2946. }
  2947. VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_port_type_by_index(int p_idx) const {
  2948. if (p_idx >= 0 && p_idx < uniforms.size()) {
  2949. switch (uniforms[p_idx].type) {
  2950. case UniformType::UNIFORM_TYPE_FLOAT:
  2951. return PORT_TYPE_SCALAR;
  2952. case UniformType::UNIFORM_TYPE_INT:
  2953. return PORT_TYPE_SCALAR_INT;
  2954. case UniformType::UNIFORM_TYPE_SAMPLER:
  2955. return PORT_TYPE_SAMPLER;
  2956. case UniformType::UNIFORM_TYPE_VECTOR2:
  2957. return PORT_TYPE_VECTOR_2D;
  2958. case UniformType::UNIFORM_TYPE_VECTOR3:
  2959. return PORT_TYPE_VECTOR_3D;
  2960. case UniformType::UNIFORM_TYPE_VECTOR4:
  2961. return PORT_TYPE_VECTOR_4D;
  2962. case UniformType::UNIFORM_TYPE_TRANSFORM:
  2963. return PORT_TYPE_TRANSFORM;
  2964. case UniformType::UNIFORM_TYPE_COLOR:
  2965. return PORT_TYPE_VECTOR_3D;
  2966. default:
  2967. break;
  2968. }
  2969. }
  2970. return PORT_TYPE_SCALAR;
  2971. }
  2972. String VisualShaderNodeUniformRef::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2973. switch (uniform_type) {
  2974. case UniformType::UNIFORM_TYPE_FLOAT:
  2975. if (uniform_name == "[None]") {
  2976. return " " + p_output_vars[0] + " = 0.0;\n";
  2977. }
  2978. break;
  2979. case UniformType::UNIFORM_TYPE_COLOR: {
  2980. String code = " " + p_output_vars[0] + " = " + get_uniform_name() + ".rgb;\n";
  2981. code += " " + p_output_vars[1] + " = " + get_uniform_name() + ".a;\n";
  2982. return code;
  2983. } break;
  2984. case UniformType::UNIFORM_TYPE_SAMPLER:
  2985. return String();
  2986. default:
  2987. break;
  2988. }
  2989. return " " + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  2990. }
  2991. void VisualShaderNodeUniformRef::_set_uniform_type(int p_uniform_type) {
  2992. uniform_type = (UniformType)p_uniform_type;
  2993. }
  2994. int VisualShaderNodeUniformRef::_get_uniform_type() const {
  2995. return (int)uniform_type;
  2996. }
  2997. void VisualShaderNodeUniformRef::_bind_methods() {
  2998. ClassDB::bind_method(D_METHOD("set_uniform_name", "name"), &VisualShaderNodeUniformRef::set_uniform_name);
  2999. ClassDB::bind_method(D_METHOD("get_uniform_name"), &VisualShaderNodeUniformRef::get_uniform_name);
  3000. ClassDB::bind_method(D_METHOD("_set_uniform_type", "type"), &VisualShaderNodeUniformRef::_set_uniform_type);
  3001. ClassDB::bind_method(D_METHOD("_get_uniform_type"), &VisualShaderNodeUniformRef::_get_uniform_type);
  3002. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "uniform_name", PROPERTY_HINT_ENUM, ""), "set_uniform_name", "get_uniform_name");
  3003. ADD_PROPERTY(PropertyInfo(Variant::INT, "uniform_type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_uniform_type", "_get_uniform_type");
  3004. }
  3005. Vector<StringName> VisualShaderNodeUniformRef::get_editable_properties() const {
  3006. Vector<StringName> props;
  3007. props.push_back("uniform_name");
  3008. props.push_back("uniform_type");
  3009. return props;
  3010. }
  3011. VisualShaderNodeUniformRef::VisualShaderNodeUniformRef() {
  3012. }
  3013. ////////////////////////////////////////////
  3014. const VisualShaderNodeOutput::Port VisualShaderNodeOutput::ports[] = {
  3015. ////////////////////////////////////////////////////////////////////////
  3016. // Node3D.
  3017. ////////////////////////////////////////////////////////////////////////
  3018. // Node3D, Vertex.
  3019. ////////////////////////////////////////////////////////////////////////
  3020. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Vertex", "VERTEX" },
  3021. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3022. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Tangent", "TANGENT" },
  3023. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Binormal", "BINORMAL" },
  3024. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV", "UV" },
  3025. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV2", "UV2" },
  3026. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "Color", "COLOR" },
  3027. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Roughness", "ROUGHNESS" },
  3028. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Point Size", "POINT_SIZE" },
  3029. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "Model View Matrix", "MODELVIEW_MATRIX" },
  3030. ////////////////////////////////////////////////////////////////////////
  3031. // Node3D, Fragment.
  3032. ////////////////////////////////////////////////////////////////////////
  3033. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Albedo", "ALBEDO" },
  3034. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3035. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Metallic", "METALLIC" },
  3036. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Roughness", "ROUGHNESS" },
  3037. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Specular", "SPECULAR" },
  3038. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Emission", "EMISSION" },
  3039. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "AO", "AO" },
  3040. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "AO Light Affect", "AO_LIGHT_AFFECT" },
  3041. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3042. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal Map", "NORMAL_MAP" },
  3043. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Normal Map Depth", "NORMAL_MAP_DEPTH" },
  3044. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Rim", "RIM" },
  3045. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Rim Tint", "RIM_TINT" },
  3046. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Clearcoat", "CLEARCOAT" },
  3047. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Clearcoat Roughness", "CLEARCOAT_ROUGHNESS" },
  3048. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Anisotropy", "ANISOTROPY" },
  3049. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Anisotropy Flow", "ANISOTROPY_FLOW" },
  3050. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Subsurf Scatter", "SSS_STRENGTH" },
  3051. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Backlight", "BACKLIGHT" },
  3052. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha Scissor Threshold", "ALPHA_SCISSOR_THRESHOLD" },
  3053. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha Hash Scale", "ALPHA_HASH_SCALE" },
  3054. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha AA Edge", "ALPHA_ANTIALIASING_EDGE" },
  3055. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Alpha UV", "ALPHA_TEXTURE_COORDINATE" },
  3056. ////////////////////////////////////////////////////////////////////////
  3057. // Node3D, Light.
  3058. ////////////////////////////////////////////////////////////////////////
  3059. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Diffuse", "DIFFUSE_LIGHT" },
  3060. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Specular", "SPECULAR_LIGHT" },
  3061. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3062. ////////////////////////////////////////////////////////////////////////
  3063. // Canvas Item.
  3064. ////////////////////////////////////////////////////////////////////////
  3065. // Canvas Item, Vertex.
  3066. ////////////////////////////////////////////////////////////////////////
  3067. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Vertex", "VERTEX" },
  3068. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_2D, "UV", "UV" },
  3069. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR_4D, "Color", "COLOR" },
  3070. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "Point Size", "POINT_SIZE" },
  3071. ////////////////////////////////////////////////////////////////////////
  3072. // Canvas Item, Fragment.
  3073. ////////////////////////////////////////////////////////////////////////
  3074. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "Color", "COLOR" },
  3075. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal", "NORMAL" },
  3076. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Normal Map", "NORMAL_MAP" },
  3077. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "Normal Map Depth", "NORMAL_MAP_DEPTH" },
  3078. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Light Vertex", "LIGHT_VERTEX" },
  3079. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR_2D, "Shadow Vertex", "SHADOW_VERTEX" },
  3080. ////////////////////////////////////////////////////////////////////////
  3081. // Canvas Item, Light.
  3082. ////////////////////////////////////////////////////////////////////////
  3083. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR_4D, "Light", "LIGHT" },
  3084. ////////////////////////////////////////////////////////////////////////
  3085. // Sky, Sky.
  3086. ////////////////////////////////////////////////////////////////////////
  3087. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Color", "COLOR" },
  3088. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_SCALAR, "Alpha", "ALPHA" },
  3089. { Shader::MODE_SKY, VisualShader::TYPE_SKY, VisualShaderNode::PORT_TYPE_VECTOR_4D, "Fog", "FOG" },
  3090. ////////////////////////////////////////////////////////////////////////
  3091. // Fog, Fog.
  3092. ////////////////////////////////////////////////////////////////////////
  3093. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_SCALAR, "Density", "DENSITY" },
  3094. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Albedo", "ALBEDO" },
  3095. { Shader::MODE_FOG, VisualShader::TYPE_FOG, VisualShaderNode::PORT_TYPE_VECTOR_3D, "Emission", "EMISSION" },
  3096. ////////////////////////////////////////////////////////////////////////
  3097. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  3098. };
  3099. int VisualShaderNodeOutput::get_input_port_count() const {
  3100. int idx = 0;
  3101. int count = 0;
  3102. while (ports[idx].mode != Shader::MODE_MAX) {
  3103. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3104. count++;
  3105. }
  3106. idx++;
  3107. }
  3108. return count;
  3109. }
  3110. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_input_port_type(int p_port) const {
  3111. int idx = 0;
  3112. int count = 0;
  3113. while (ports[idx].mode != Shader::MODE_MAX) {
  3114. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3115. if (count == p_port) {
  3116. return ports[idx].type;
  3117. }
  3118. count++;
  3119. }
  3120. idx++;
  3121. }
  3122. return PORT_TYPE_SCALAR;
  3123. }
  3124. String VisualShaderNodeOutput::get_input_port_name(int p_port) const {
  3125. int idx = 0;
  3126. int count = 0;
  3127. while (ports[idx].mode != Shader::MODE_MAX) {
  3128. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3129. if (count == p_port) {
  3130. return String(ports[idx].name);
  3131. }
  3132. count++;
  3133. }
  3134. idx++;
  3135. }
  3136. return String();
  3137. }
  3138. Variant VisualShaderNodeOutput::get_input_port_default_value(int p_port) const {
  3139. return Variant();
  3140. }
  3141. int VisualShaderNodeOutput::get_output_port_count() const {
  3142. return 0;
  3143. }
  3144. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_output_port_type(int p_port) const {
  3145. return PORT_TYPE_SCALAR;
  3146. }
  3147. String VisualShaderNodeOutput::get_output_port_name(int p_port) const {
  3148. return String();
  3149. }
  3150. bool VisualShaderNodeOutput::is_port_separator(int p_index) const {
  3151. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_VERTEX) {
  3152. String name = get_input_port_name(p_index);
  3153. return bool(name == "Model View Matrix");
  3154. }
  3155. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_FRAGMENT) {
  3156. String name = get_input_port_name(p_index);
  3157. return bool(name == "AO" || name == "Normal" || name == "Rim" || name == "Clearcoat" || name == "Anisotropy" || name == "Subsurf Scatter" || name == "Alpha Scissor Threshold");
  3158. }
  3159. return false;
  3160. }
  3161. String VisualShaderNodeOutput::get_caption() const {
  3162. return "Output";
  3163. }
  3164. String VisualShaderNodeOutput::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3165. int idx = 0;
  3166. int count = 0;
  3167. String code;
  3168. while (ports[idx].mode != Shader::MODE_MAX) {
  3169. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  3170. if (!p_input_vars[count].is_empty()) {
  3171. String s = ports[idx].string;
  3172. if (s.contains(":")) {
  3173. code += " " + s.get_slicec(':', 0) + " = " + p_input_vars[count] + "." + s.get_slicec(':', 1) + ";\n";
  3174. } else {
  3175. code += " " + s + " = " + p_input_vars[count] + ";\n";
  3176. }
  3177. }
  3178. count++;
  3179. }
  3180. idx++;
  3181. }
  3182. return code;
  3183. }
  3184. VisualShaderNodeOutput::VisualShaderNodeOutput() {
  3185. }
  3186. ///////////////////////////
  3187. void VisualShaderNodeUniform::set_uniform_name(const String &p_name) {
  3188. uniform_name = p_name;
  3189. emit_signal(SNAME("name_changed"));
  3190. emit_changed();
  3191. }
  3192. String VisualShaderNodeUniform::get_uniform_name() const {
  3193. return uniform_name;
  3194. }
  3195. void VisualShaderNodeUniform::set_qualifier(VisualShaderNodeUniform::Qualifier p_qual) {
  3196. ERR_FAIL_INDEX(int(p_qual), int(QUAL_MAX));
  3197. if (qualifier == p_qual) {
  3198. return;
  3199. }
  3200. qualifier = p_qual;
  3201. emit_changed();
  3202. }
  3203. VisualShaderNodeUniform::Qualifier VisualShaderNodeUniform::get_qualifier() const {
  3204. return qualifier;
  3205. }
  3206. void VisualShaderNodeUniform::set_global_code_generated(bool p_enabled) {
  3207. global_code_generated = p_enabled;
  3208. }
  3209. bool VisualShaderNodeUniform::is_global_code_generated() const {
  3210. return global_code_generated;
  3211. }
  3212. void VisualShaderNodeUniform::_bind_methods() {
  3213. ClassDB::bind_method(D_METHOD("set_uniform_name", "name"), &VisualShaderNodeUniform::set_uniform_name);
  3214. ClassDB::bind_method(D_METHOD("get_uniform_name"), &VisualShaderNodeUniform::get_uniform_name);
  3215. ClassDB::bind_method(D_METHOD("set_qualifier", "qualifier"), &VisualShaderNodeUniform::set_qualifier);
  3216. ClassDB::bind_method(D_METHOD("get_qualifier"), &VisualShaderNodeUniform::get_qualifier);
  3217. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "uniform_name"), "set_uniform_name", "get_uniform_name");
  3218. ADD_PROPERTY(PropertyInfo(Variant::INT, "qualifier", PROPERTY_HINT_ENUM, "None,Global,Instance"), "set_qualifier", "get_qualifier");
  3219. BIND_ENUM_CONSTANT(QUAL_NONE);
  3220. BIND_ENUM_CONSTANT(QUAL_GLOBAL);
  3221. BIND_ENUM_CONSTANT(QUAL_INSTANCE);
  3222. BIND_ENUM_CONSTANT(QUAL_MAX);
  3223. }
  3224. String VisualShaderNodeUniform::_get_qual_str() const {
  3225. if (is_qualifier_supported(qualifier)) {
  3226. switch (qualifier) {
  3227. case QUAL_NONE:
  3228. break;
  3229. case QUAL_GLOBAL:
  3230. return "global ";
  3231. case QUAL_INSTANCE:
  3232. return "instance ";
  3233. default:
  3234. break;
  3235. }
  3236. }
  3237. return String();
  3238. }
  3239. String VisualShaderNodeUniform::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  3240. List<String> keyword_list;
  3241. ShaderLanguage::get_keyword_list(&keyword_list);
  3242. if (keyword_list.find(uniform_name)) {
  3243. return RTR("Shader keywords cannot be used as uniform names.\nChoose another name.");
  3244. }
  3245. if (!is_qualifier_supported(qualifier)) {
  3246. String qualifier_str;
  3247. switch (qualifier) {
  3248. case QUAL_NONE:
  3249. break;
  3250. case QUAL_GLOBAL:
  3251. qualifier_str = "global";
  3252. break;
  3253. case QUAL_INSTANCE:
  3254. qualifier_str = "instance";
  3255. break;
  3256. default:
  3257. break;
  3258. }
  3259. return vformat(RTR("This uniform type does not support the '%s' qualifier."), qualifier_str);
  3260. } else if (qualifier == Qualifier::QUAL_GLOBAL) {
  3261. RS::GlobalVariableType gvt = RS::get_singleton()->global_variable_get_type(uniform_name);
  3262. if (gvt == RS::GLOBAL_VAR_TYPE_MAX) {
  3263. return vformat(RTR("Global uniform '%s' does not exist.\nCreate it in the Project Settings."), uniform_name);
  3264. }
  3265. bool incompatible_type = false;
  3266. switch (gvt) {
  3267. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  3268. if (!Object::cast_to<VisualShaderNodeFloatUniform>(this)) {
  3269. incompatible_type = true;
  3270. }
  3271. } break;
  3272. case RS::GLOBAL_VAR_TYPE_INT: {
  3273. if (!Object::cast_to<VisualShaderNodeIntUniform>(this)) {
  3274. incompatible_type = true;
  3275. }
  3276. } break;
  3277. case RS::GLOBAL_VAR_TYPE_BOOL: {
  3278. if (!Object::cast_to<VisualShaderNodeBooleanUniform>(this)) {
  3279. incompatible_type = true;
  3280. }
  3281. } break;
  3282. case RS::GLOBAL_VAR_TYPE_COLOR: {
  3283. if (!Object::cast_to<VisualShaderNodeColorUniform>(this)) {
  3284. incompatible_type = true;
  3285. }
  3286. } break;
  3287. case RS::GLOBAL_VAR_TYPE_VEC3: {
  3288. if (!Object::cast_to<VisualShaderNodeVec3Uniform>(this)) {
  3289. incompatible_type = true;
  3290. }
  3291. } break;
  3292. case RS::GLOBAL_VAR_TYPE_VEC4: {
  3293. if (!Object::cast_to<VisualShaderNodeVec4Uniform>(this)) {
  3294. incompatible_type = true;
  3295. }
  3296. } break;
  3297. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  3298. if (!Object::cast_to<VisualShaderNodeTransformUniform>(this)) {
  3299. incompatible_type = true;
  3300. }
  3301. } break;
  3302. case RS::GLOBAL_VAR_TYPE_SAMPLER2D: {
  3303. if (!Object::cast_to<VisualShaderNodeTextureUniform>(this)) {
  3304. incompatible_type = true;
  3305. }
  3306. } break;
  3307. case RS::GLOBAL_VAR_TYPE_SAMPLER3D: {
  3308. if (!Object::cast_to<VisualShaderNodeTexture3DUniform>(this)) {
  3309. incompatible_type = true;
  3310. }
  3311. } break;
  3312. case RS::GLOBAL_VAR_TYPE_SAMPLER2DARRAY: {
  3313. if (!Object::cast_to<VisualShaderNodeTexture2DArrayUniform>(this)) {
  3314. incompatible_type = true;
  3315. }
  3316. } break;
  3317. case RS::GLOBAL_VAR_TYPE_SAMPLERCUBE: {
  3318. if (!Object::cast_to<VisualShaderNodeCubemapUniform>(this)) {
  3319. incompatible_type = true;
  3320. }
  3321. } break;
  3322. default:
  3323. break;
  3324. }
  3325. if (incompatible_type) {
  3326. return vformat(RTR("Global uniform '%s' has an incompatible type for this kind of node.\nChange it in the Project Settings."), uniform_name);
  3327. }
  3328. }
  3329. return String();
  3330. }
  3331. Vector<StringName> VisualShaderNodeUniform::get_editable_properties() const {
  3332. Vector<StringName> props;
  3333. props.push_back("qualifier");
  3334. return props;
  3335. }
  3336. VisualShaderNodeUniform::VisualShaderNodeUniform() {
  3337. }
  3338. ////////////// ResizeableBase
  3339. void VisualShaderNodeResizableBase::set_size(const Vector2 &p_size) {
  3340. size = p_size;
  3341. }
  3342. Vector2 VisualShaderNodeResizableBase::get_size() const {
  3343. return size;
  3344. }
  3345. void VisualShaderNodeResizableBase::set_allow_v_resize(bool p_enabled) {
  3346. allow_v_resize = p_enabled;
  3347. }
  3348. bool VisualShaderNodeResizableBase::is_allow_v_resize() const {
  3349. return allow_v_resize;
  3350. }
  3351. void VisualShaderNodeResizableBase::_bind_methods() {
  3352. ClassDB::bind_method(D_METHOD("set_size", "size"), &VisualShaderNodeResizableBase::set_size);
  3353. ClassDB::bind_method(D_METHOD("get_size"), &VisualShaderNodeResizableBase::get_size);
  3354. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  3355. }
  3356. VisualShaderNodeResizableBase::VisualShaderNodeResizableBase() {
  3357. set_allow_v_resize(true);
  3358. }
  3359. ////////////// Comment
  3360. String VisualShaderNodeComment::get_caption() const {
  3361. return title;
  3362. }
  3363. int VisualShaderNodeComment::get_input_port_count() const {
  3364. return 0;
  3365. }
  3366. VisualShaderNodeComment::PortType VisualShaderNodeComment::get_input_port_type(int p_port) const {
  3367. return PortType::PORT_TYPE_SCALAR;
  3368. }
  3369. String VisualShaderNodeComment::get_input_port_name(int p_port) const {
  3370. return String();
  3371. }
  3372. int VisualShaderNodeComment::get_output_port_count() const {
  3373. return 0;
  3374. }
  3375. VisualShaderNodeComment::PortType VisualShaderNodeComment::get_output_port_type(int p_port) const {
  3376. return PortType::PORT_TYPE_SCALAR;
  3377. }
  3378. String VisualShaderNodeComment::get_output_port_name(int p_port) const {
  3379. return String();
  3380. }
  3381. void VisualShaderNodeComment::set_title(const String &p_title) {
  3382. title = p_title;
  3383. }
  3384. String VisualShaderNodeComment::get_title() const {
  3385. return title;
  3386. }
  3387. void VisualShaderNodeComment::set_description(const String &p_description) {
  3388. description = p_description;
  3389. }
  3390. String VisualShaderNodeComment::get_description() const {
  3391. return description;
  3392. }
  3393. String VisualShaderNodeComment::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3394. return String();
  3395. }
  3396. void VisualShaderNodeComment::_bind_methods() {
  3397. ClassDB::bind_method(D_METHOD("set_title", "title"), &VisualShaderNodeComment::set_title);
  3398. ClassDB::bind_method(D_METHOD("get_title"), &VisualShaderNodeComment::get_title);
  3399. ClassDB::bind_method(D_METHOD("set_description", "description"), &VisualShaderNodeComment::set_description);
  3400. ClassDB::bind_method(D_METHOD("get_description"), &VisualShaderNodeComment::get_description);
  3401. ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
  3402. ADD_PROPERTY(PropertyInfo(Variant::STRING, "description"), "set_description", "get_description");
  3403. }
  3404. VisualShaderNodeComment::VisualShaderNodeComment() {
  3405. }
  3406. ////////////// GroupBase
  3407. void VisualShaderNodeGroupBase::set_inputs(const String &p_inputs) {
  3408. if (inputs == p_inputs) {
  3409. return;
  3410. }
  3411. clear_input_ports();
  3412. inputs = p_inputs;
  3413. Vector<String> input_strings = inputs.split(";", false);
  3414. int input_port_count = input_strings.size();
  3415. for (int i = 0; i < input_port_count; i++) {
  3416. Vector<String> arr = input_strings[i].split(",");
  3417. ERR_FAIL_COND(arr.size() != 3);
  3418. int port_idx = arr[0].to_int();
  3419. int port_type = arr[1].to_int();
  3420. String port_name = arr[2];
  3421. Port port;
  3422. port.type = (PortType)port_type;
  3423. port.name = port_name;
  3424. input_ports[port_idx] = port;
  3425. }
  3426. }
  3427. String VisualShaderNodeGroupBase::get_inputs() const {
  3428. return inputs;
  3429. }
  3430. void VisualShaderNodeGroupBase::set_outputs(const String &p_outputs) {
  3431. if (outputs == p_outputs) {
  3432. return;
  3433. }
  3434. clear_output_ports();
  3435. outputs = p_outputs;
  3436. Vector<String> output_strings = outputs.split(";", false);
  3437. int output_port_count = output_strings.size();
  3438. for (int i = 0; i < output_port_count; i++) {
  3439. Vector<String> arr = output_strings[i].split(",");
  3440. ERR_FAIL_COND(arr.size() != 3);
  3441. int port_idx = arr[0].to_int();
  3442. int port_type = arr[1].to_int();
  3443. String port_name = arr[2];
  3444. Port port;
  3445. port.type = (PortType)port_type;
  3446. port.name = port_name;
  3447. output_ports[port_idx] = port;
  3448. }
  3449. }
  3450. String VisualShaderNodeGroupBase::get_outputs() const {
  3451. return outputs;
  3452. }
  3453. bool VisualShaderNodeGroupBase::is_valid_port_name(const String &p_name) const {
  3454. if (!p_name.is_valid_identifier()) {
  3455. return false;
  3456. }
  3457. for (int i = 0; i < get_input_port_count(); i++) {
  3458. if (get_input_port_name(i) == p_name) {
  3459. return false;
  3460. }
  3461. }
  3462. for (int i = 0; i < get_output_port_count(); i++) {
  3463. if (get_output_port_name(i) == p_name) {
  3464. return false;
  3465. }
  3466. }
  3467. return true;
  3468. }
  3469. void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const String &p_name) {
  3470. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  3471. ERR_FAIL_COND(!is_valid_port_name(p_name));
  3472. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  3473. Vector<String> inputs_strings = inputs.split(";", false);
  3474. int index = 0;
  3475. if (p_id < inputs_strings.size()) {
  3476. for (int i = 0; i < inputs_strings.size(); i++) {
  3477. if (i == p_id) {
  3478. inputs = inputs.insert(index, str);
  3479. break;
  3480. }
  3481. index += inputs_strings[i].size();
  3482. }
  3483. } else {
  3484. inputs += str;
  3485. }
  3486. inputs_strings = inputs.split(";", false);
  3487. index = 0;
  3488. for (int i = 0; i < inputs_strings.size(); i++) {
  3489. int count = 0;
  3490. for (int j = 0; j < inputs_strings[i].size(); j++) {
  3491. if (inputs_strings[i][j] == ',') {
  3492. break;
  3493. }
  3494. count++;
  3495. }
  3496. inputs = inputs.left(index) + inputs.substr(index + count);
  3497. inputs = inputs.insert(index, itos(i));
  3498. index += inputs_strings[i].size();
  3499. }
  3500. _apply_port_changes();
  3501. emit_changed();
  3502. }
  3503. void VisualShaderNodeGroupBase::remove_input_port(int p_id) {
  3504. ERR_FAIL_COND(!has_input_port(p_id));
  3505. Vector<String> inputs_strings = inputs.split(";", false);
  3506. int count = 0;
  3507. int index = 0;
  3508. for (int i = 0; i < inputs_strings.size(); i++) {
  3509. Vector<String> arr = inputs_strings[i].split(",");
  3510. if (arr[0].to_int() == p_id) {
  3511. count = inputs_strings[i].size();
  3512. break;
  3513. }
  3514. index += inputs_strings[i].size();
  3515. }
  3516. inputs = inputs.left(index) + inputs.substr(index + count);
  3517. inputs_strings = inputs.split(";", false);
  3518. inputs = inputs.substr(0, index);
  3519. for (int i = p_id; i < inputs_strings.size(); i++) {
  3520. inputs += inputs_strings[i].replace_first(inputs_strings[i].split(",")[0], itos(i)) + ";";
  3521. }
  3522. _apply_port_changes();
  3523. emit_changed();
  3524. }
  3525. int VisualShaderNodeGroupBase::get_input_port_count() const {
  3526. return input_ports.size();
  3527. }
  3528. bool VisualShaderNodeGroupBase::has_input_port(int p_id) const {
  3529. return input_ports.has(p_id);
  3530. }
  3531. void VisualShaderNodeGroupBase::add_output_port(int p_id, int p_type, const String &p_name) {
  3532. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  3533. ERR_FAIL_COND(!is_valid_port_name(p_name));
  3534. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  3535. Vector<String> outputs_strings = outputs.split(";", false);
  3536. int index = 0;
  3537. if (p_id < outputs_strings.size()) {
  3538. for (int i = 0; i < outputs_strings.size(); i++) {
  3539. if (i == p_id) {
  3540. outputs = outputs.insert(index, str);
  3541. break;
  3542. }
  3543. index += outputs_strings[i].size();
  3544. }
  3545. } else {
  3546. outputs += str;
  3547. }
  3548. outputs_strings = outputs.split(";", false);
  3549. index = 0;
  3550. for (int i = 0; i < outputs_strings.size(); i++) {
  3551. int count = 0;
  3552. for (int j = 0; j < outputs_strings[i].size(); j++) {
  3553. if (outputs_strings[i][j] == ',') {
  3554. break;
  3555. }
  3556. count++;
  3557. }
  3558. outputs = outputs.left(index) + outputs.substr(index + count);
  3559. outputs = outputs.insert(index, itos(i));
  3560. index += outputs_strings[i].size();
  3561. }
  3562. _apply_port_changes();
  3563. emit_changed();
  3564. }
  3565. void VisualShaderNodeGroupBase::remove_output_port(int p_id) {
  3566. ERR_FAIL_COND(!has_output_port(p_id));
  3567. Vector<String> outputs_strings = outputs.split(";", false);
  3568. int count = 0;
  3569. int index = 0;
  3570. for (int i = 0; i < outputs_strings.size(); i++) {
  3571. Vector<String> arr = outputs_strings[i].split(",");
  3572. if (arr[0].to_int() == p_id) {
  3573. count = outputs_strings[i].size();
  3574. break;
  3575. }
  3576. index += outputs_strings[i].size();
  3577. }
  3578. outputs = outputs.left(index) + outputs.substr(index + count);
  3579. outputs_strings = outputs.split(";", false);
  3580. outputs = outputs.substr(0, index);
  3581. for (int i = p_id; i < outputs_strings.size(); i++) {
  3582. outputs += outputs_strings[i].replace_first(outputs_strings[i].split(",")[0], itos(i)) + ";";
  3583. }
  3584. _apply_port_changes();
  3585. emit_changed();
  3586. }
  3587. int VisualShaderNodeGroupBase::get_output_port_count() const {
  3588. return output_ports.size();
  3589. }
  3590. bool VisualShaderNodeGroupBase::has_output_port(int p_id) const {
  3591. return output_ports.has(p_id);
  3592. }
  3593. void VisualShaderNodeGroupBase::clear_input_ports() {
  3594. input_ports.clear();
  3595. }
  3596. void VisualShaderNodeGroupBase::clear_output_ports() {
  3597. output_ports.clear();
  3598. }
  3599. void VisualShaderNodeGroupBase::set_input_port_type(int p_id, int p_type) {
  3600. ERR_FAIL_COND(!has_input_port(p_id));
  3601. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  3602. if (input_ports[p_id].type == p_type) {
  3603. return;
  3604. }
  3605. Vector<String> inputs_strings = inputs.split(";", false);
  3606. int count = 0;
  3607. int index = 0;
  3608. for (int i = 0; i < inputs_strings.size(); i++) {
  3609. Vector<String> arr = inputs_strings[i].split(",");
  3610. ERR_FAIL_COND(arr.size() != 3);
  3611. if (arr[0].to_int() == p_id) {
  3612. index += arr[0].size();
  3613. count = arr[1].size() - 1;
  3614. break;
  3615. }
  3616. index += inputs_strings[i].size();
  3617. }
  3618. inputs = inputs.left(index) + inputs.substr(index + count);
  3619. inputs = inputs.insert(index, itos(p_type));
  3620. _apply_port_changes();
  3621. emit_changed();
  3622. }
  3623. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_input_port_type(int p_id) const {
  3624. ERR_FAIL_COND_V(!input_ports.has(p_id), (PortType)0);
  3625. return input_ports[p_id].type;
  3626. }
  3627. void VisualShaderNodeGroupBase::set_input_port_name(int p_id, const String &p_name) {
  3628. ERR_FAIL_COND(!has_input_port(p_id));
  3629. ERR_FAIL_COND(!is_valid_port_name(p_name));
  3630. if (input_ports[p_id].name == p_name) {
  3631. return;
  3632. }
  3633. Vector<String> inputs_strings = inputs.split(";", false);
  3634. int count = 0;
  3635. int index = 0;
  3636. for (int i = 0; i < inputs_strings.size(); i++) {
  3637. Vector<String> arr = inputs_strings[i].split(",");
  3638. ERR_FAIL_COND(arr.size() != 3);
  3639. if (arr[0].to_int() == p_id) {
  3640. index += arr[0].size() + arr[1].size();
  3641. count = arr[2].size() - 1;
  3642. break;
  3643. }
  3644. index += inputs_strings[i].size();
  3645. }
  3646. inputs = inputs.left(index) + inputs.substr(index + count);
  3647. inputs = inputs.insert(index, p_name);
  3648. _apply_port_changes();
  3649. emit_changed();
  3650. }
  3651. String VisualShaderNodeGroupBase::get_input_port_name(int p_id) const {
  3652. ERR_FAIL_COND_V(!input_ports.has(p_id), "");
  3653. return input_ports[p_id].name;
  3654. }
  3655. void VisualShaderNodeGroupBase::set_output_port_type(int p_id, int p_type) {
  3656. ERR_FAIL_COND(!has_output_port(p_id));
  3657. ERR_FAIL_INDEX(p_type, int(PORT_TYPE_MAX));
  3658. if (output_ports[p_id].type == p_type) {
  3659. return;
  3660. }
  3661. Vector<String> output_strings = outputs.split(";", false);
  3662. int count = 0;
  3663. int index = 0;
  3664. for (int i = 0; i < output_strings.size(); i++) {
  3665. Vector<String> arr = output_strings[i].split(",");
  3666. ERR_FAIL_COND(arr.size() != 3);
  3667. if (arr[0].to_int() == p_id) {
  3668. index += arr[0].size();
  3669. count = arr[1].size() - 1;
  3670. break;
  3671. }
  3672. index += output_strings[i].size();
  3673. }
  3674. outputs = outputs.left(index) + outputs.substr(index + count);
  3675. outputs = outputs.insert(index, itos(p_type));
  3676. _apply_port_changes();
  3677. emit_changed();
  3678. }
  3679. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_output_port_type(int p_id) const {
  3680. ERR_FAIL_COND_V(!output_ports.has(p_id), (PortType)0);
  3681. return output_ports[p_id].type;
  3682. }
  3683. void VisualShaderNodeGroupBase::set_output_port_name(int p_id, const String &p_name) {
  3684. ERR_FAIL_COND(!has_output_port(p_id));
  3685. ERR_FAIL_COND(!is_valid_port_name(p_name));
  3686. if (output_ports[p_id].name == p_name) {
  3687. return;
  3688. }
  3689. Vector<String> output_strings = outputs.split(";", false);
  3690. int count = 0;
  3691. int index = 0;
  3692. for (int i = 0; i < output_strings.size(); i++) {
  3693. Vector<String> arr = output_strings[i].split(",");
  3694. ERR_FAIL_COND(arr.size() != 3);
  3695. if (arr[0].to_int() == p_id) {
  3696. index += arr[0].size() + arr[1].size();
  3697. count = arr[2].size() - 1;
  3698. break;
  3699. }
  3700. index += output_strings[i].size();
  3701. }
  3702. outputs = outputs.left(index) + outputs.substr(index + count);
  3703. outputs = outputs.insert(index, p_name);
  3704. _apply_port_changes();
  3705. emit_changed();
  3706. }
  3707. String VisualShaderNodeGroupBase::get_output_port_name(int p_id) const {
  3708. ERR_FAIL_COND_V(!output_ports.has(p_id), "");
  3709. return output_ports[p_id].name;
  3710. }
  3711. int VisualShaderNodeGroupBase::get_free_input_port_id() const {
  3712. return input_ports.size();
  3713. }
  3714. int VisualShaderNodeGroupBase::get_free_output_port_id() const {
  3715. return output_ports.size();
  3716. }
  3717. void VisualShaderNodeGroupBase::set_ctrl_pressed(Control *p_control, int p_index) {
  3718. controls[p_index] = p_control;
  3719. }
  3720. Control *VisualShaderNodeGroupBase::is_ctrl_pressed(int p_index) {
  3721. ERR_FAIL_COND_V(!controls.has(p_index), nullptr);
  3722. return controls[p_index];
  3723. }
  3724. void VisualShaderNodeGroupBase::_apply_port_changes() {
  3725. Vector<String> inputs_strings = inputs.split(";", false);
  3726. Vector<String> outputs_strings = outputs.split(";", false);
  3727. clear_input_ports();
  3728. clear_output_ports();
  3729. for (int i = 0; i < inputs_strings.size(); i++) {
  3730. Vector<String> arr = inputs_strings[i].split(",");
  3731. ERR_FAIL_COND(arr.size() != 3);
  3732. Port port;
  3733. port.type = (PortType)arr[1].to_int();
  3734. port.name = arr[2];
  3735. input_ports[i] = port;
  3736. }
  3737. for (int i = 0; i < outputs_strings.size(); i++) {
  3738. Vector<String> arr = outputs_strings[i].split(",");
  3739. ERR_FAIL_COND(arr.size() != 3);
  3740. Port port;
  3741. port.type = (PortType)arr[1].to_int();
  3742. port.name = arr[2];
  3743. output_ports[i] = port;
  3744. }
  3745. }
  3746. void VisualShaderNodeGroupBase::set_editable(bool p_enabled) {
  3747. editable = p_enabled;
  3748. }
  3749. bool VisualShaderNodeGroupBase::is_editable() const {
  3750. return editable;
  3751. }
  3752. void VisualShaderNodeGroupBase::_bind_methods() {
  3753. ClassDB::bind_method(D_METHOD("set_inputs", "inputs"), &VisualShaderNodeGroupBase::set_inputs);
  3754. ClassDB::bind_method(D_METHOD("get_inputs"), &VisualShaderNodeGroupBase::get_inputs);
  3755. ClassDB::bind_method(D_METHOD("set_outputs", "outputs"), &VisualShaderNodeGroupBase::set_outputs);
  3756. ClassDB::bind_method(D_METHOD("get_outputs"), &VisualShaderNodeGroupBase::get_outputs);
  3757. ClassDB::bind_method(D_METHOD("is_valid_port_name", "name"), &VisualShaderNodeGroupBase::is_valid_port_name);
  3758. ClassDB::bind_method(D_METHOD("add_input_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_input_port);
  3759. ClassDB::bind_method(D_METHOD("remove_input_port", "id"), &VisualShaderNodeGroupBase::remove_input_port);
  3760. ClassDB::bind_method(D_METHOD("get_input_port_count"), &VisualShaderNodeGroupBase::get_input_port_count);
  3761. ClassDB::bind_method(D_METHOD("has_input_port", "id"), &VisualShaderNodeGroupBase::has_input_port);
  3762. ClassDB::bind_method(D_METHOD("clear_input_ports"), &VisualShaderNodeGroupBase::clear_input_ports);
  3763. ClassDB::bind_method(D_METHOD("add_output_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_output_port);
  3764. ClassDB::bind_method(D_METHOD("remove_output_port", "id"), &VisualShaderNodeGroupBase::remove_output_port);
  3765. ClassDB::bind_method(D_METHOD("get_output_port_count"), &VisualShaderNodeGroupBase::get_output_port_count);
  3766. ClassDB::bind_method(D_METHOD("has_output_port", "id"), &VisualShaderNodeGroupBase::has_output_port);
  3767. ClassDB::bind_method(D_METHOD("clear_output_ports"), &VisualShaderNodeGroupBase::clear_output_ports);
  3768. ClassDB::bind_method(D_METHOD("set_input_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_input_port_name);
  3769. ClassDB::bind_method(D_METHOD("set_input_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_input_port_type);
  3770. ClassDB::bind_method(D_METHOD("set_output_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_output_port_name);
  3771. ClassDB::bind_method(D_METHOD("set_output_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_output_port_type);
  3772. ClassDB::bind_method(D_METHOD("get_free_input_port_id"), &VisualShaderNodeGroupBase::get_free_input_port_id);
  3773. ClassDB::bind_method(D_METHOD("get_free_output_port_id"), &VisualShaderNodeGroupBase::get_free_output_port_id);
  3774. }
  3775. String VisualShaderNodeGroupBase::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3776. return "";
  3777. }
  3778. VisualShaderNodeGroupBase::VisualShaderNodeGroupBase() {
  3779. simple_decl = false;
  3780. }
  3781. ////////////// Expression
  3782. String VisualShaderNodeExpression::get_caption() const {
  3783. return "Expression";
  3784. }
  3785. void VisualShaderNodeExpression::set_expression(const String &p_expression) {
  3786. expression = p_expression;
  3787. emit_changed();
  3788. }
  3789. String VisualShaderNodeExpression::get_expression() const {
  3790. return expression;
  3791. }
  3792. String VisualShaderNodeExpression::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3793. String _expression = expression;
  3794. _expression = _expression.insert(0, "\n");
  3795. _expression = _expression.replace("\n", "\n ");
  3796. static Vector<String> pre_symbols;
  3797. if (pre_symbols.is_empty()) {
  3798. pre_symbols.push_back(" ");
  3799. pre_symbols.push_back(",");
  3800. pre_symbols.push_back(";");
  3801. pre_symbols.push_back("{");
  3802. pre_symbols.push_back("[");
  3803. pre_symbols.push_back("]");
  3804. pre_symbols.push_back("(");
  3805. pre_symbols.push_back(" ");
  3806. pre_symbols.push_back("-");
  3807. pre_symbols.push_back("*");
  3808. pre_symbols.push_back("/");
  3809. pre_symbols.push_back("+");
  3810. pre_symbols.push_back("=");
  3811. pre_symbols.push_back("&");
  3812. pre_symbols.push_back("|");
  3813. pre_symbols.push_back("!");
  3814. }
  3815. static Vector<String> post_symbols;
  3816. if (post_symbols.is_empty()) {
  3817. post_symbols.push_back(" ");
  3818. post_symbols.push_back("\n");
  3819. post_symbols.push_back(",");
  3820. post_symbols.push_back(";");
  3821. post_symbols.push_back("}");
  3822. post_symbols.push_back("[");
  3823. post_symbols.push_back("]");
  3824. post_symbols.push_back(")");
  3825. post_symbols.push_back(" ");
  3826. post_symbols.push_back(".");
  3827. post_symbols.push_back("-");
  3828. post_symbols.push_back("*");
  3829. post_symbols.push_back("/");
  3830. post_symbols.push_back("+");
  3831. post_symbols.push_back("=");
  3832. post_symbols.push_back("&");
  3833. post_symbols.push_back("|");
  3834. post_symbols.push_back("!");
  3835. }
  3836. for (int i = 0; i < get_input_port_count(); i++) {
  3837. for (int j = 0; j < pre_symbols.size(); j++) {
  3838. for (int k = 0; k < post_symbols.size(); k++) {
  3839. _expression = _expression.replace(pre_symbols[j] + get_input_port_name(i) + post_symbols[k], pre_symbols[j] + p_input_vars[i] + post_symbols[k]);
  3840. }
  3841. }
  3842. }
  3843. for (int i = 0; i < get_output_port_count(); i++) {
  3844. for (int j = 0; j < pre_symbols.size(); j++) {
  3845. for (int k = 0; k < post_symbols.size(); k++) {
  3846. _expression = _expression.replace(pre_symbols[j] + get_output_port_name(i) + post_symbols[k], pre_symbols[j] + p_output_vars[i] + post_symbols[k]);
  3847. }
  3848. }
  3849. }
  3850. String output_initializer;
  3851. for (int i = 0; i < get_output_port_count(); i++) {
  3852. int port_type = get_output_port_type(i);
  3853. String tk = "";
  3854. switch (port_type) {
  3855. case PORT_TYPE_SCALAR:
  3856. tk = "0.0";
  3857. break;
  3858. case PORT_TYPE_SCALAR_INT:
  3859. tk = "0";
  3860. break;
  3861. case PORT_TYPE_VECTOR_2D:
  3862. tk = "vec2(0.0, 0.0)";
  3863. break;
  3864. case PORT_TYPE_VECTOR_3D:
  3865. tk = "vec3(0.0, 0.0, 0.0)";
  3866. break;
  3867. case PORT_TYPE_VECTOR_4D:
  3868. tk = "vec4(0.0, 0.0, 0.0, 0.0)";
  3869. break;
  3870. case PORT_TYPE_BOOLEAN:
  3871. tk = "false";
  3872. break;
  3873. case PORT_TYPE_TRANSFORM:
  3874. tk = "mat4(1.0)";
  3875. break;
  3876. default:
  3877. continue;
  3878. }
  3879. output_initializer += " " + p_output_vars[i] + " = " + tk + ";\n";
  3880. }
  3881. String code;
  3882. code += output_initializer;
  3883. code += " {";
  3884. code += _expression;
  3885. code += "\n }\n";
  3886. return code;
  3887. }
  3888. void VisualShaderNodeExpression::_bind_methods() {
  3889. ClassDB::bind_method(D_METHOD("set_expression", "expression"), &VisualShaderNodeExpression::set_expression);
  3890. ClassDB::bind_method(D_METHOD("get_expression"), &VisualShaderNodeExpression::get_expression);
  3891. ADD_PROPERTY(PropertyInfo(Variant::STRING, "expression"), "set_expression", "get_expression");
  3892. }
  3893. VisualShaderNodeExpression::VisualShaderNodeExpression() {
  3894. set_editable(true);
  3895. }
  3896. ////////////// Global Expression
  3897. String VisualShaderNodeGlobalExpression::get_caption() const {
  3898. return "GlobalExpression";
  3899. }
  3900. String VisualShaderNodeGlobalExpression::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  3901. return expression;
  3902. }
  3903. VisualShaderNodeGlobalExpression::VisualShaderNodeGlobalExpression() {
  3904. set_editable(false);
  3905. }
  3906. ////////////// Varying
  3907. List<VisualShaderNodeVarying::Varying> varyings;
  3908. void VisualShaderNodeVarying::add_varying(const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type) { // static
  3909. varyings.push_back({ p_name, p_mode, p_type });
  3910. }
  3911. void VisualShaderNodeVarying::clear_varyings() { // static
  3912. varyings.clear();
  3913. }
  3914. bool VisualShaderNodeVarying::has_varying(const String &p_name) { // static
  3915. for (const VisualShaderNodeVarying::Varying &E : varyings) {
  3916. if (E.name == p_name) {
  3917. return true;
  3918. }
  3919. }
  3920. return false;
  3921. }
  3922. int VisualShaderNodeVarying::get_varyings_count() const {
  3923. return varyings.size();
  3924. }
  3925. String VisualShaderNodeVarying::get_varying_name_by_index(int p_idx) const {
  3926. if (p_idx >= 0 && p_idx < varyings.size()) {
  3927. return varyings[p_idx].name;
  3928. }
  3929. return "";
  3930. }
  3931. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_name(const String &p_name) const {
  3932. for (int i = 0; i < varyings.size(); i++) {
  3933. if (varyings[i].name == p_name) {
  3934. return varyings[i].type;
  3935. }
  3936. }
  3937. return VisualShader::VARYING_TYPE_FLOAT;
  3938. }
  3939. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type_by_index(int p_idx) const {
  3940. if (p_idx >= 0 && p_idx < varyings.size()) {
  3941. return varyings[p_idx].type;
  3942. }
  3943. return VisualShader::VARYING_TYPE_FLOAT;
  3944. }
  3945. VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_name(const String &p_name) const {
  3946. for (int i = 0; i < varyings.size(); i++) {
  3947. if (varyings[i].name == p_name) {
  3948. return varyings[i].mode;
  3949. }
  3950. }
  3951. return VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT;
  3952. }
  3953. VisualShader::VaryingMode VisualShaderNodeVarying::get_varying_mode_by_index(int p_idx) const {
  3954. if (p_idx >= 0 && p_idx < varyings.size()) {
  3955. return varyings[p_idx].mode;
  3956. }
  3957. return VisualShader::VARYING_MODE_VERTEX_TO_FRAG_LIGHT;
  3958. }
  3959. VisualShaderNodeVarying::PortType VisualShaderNodeVarying::get_port_type_by_index(int p_idx) const {
  3960. if (p_idx >= 0 && p_idx < varyings.size()) {
  3961. return get_port_type(varyings[p_idx].type, 0);
  3962. }
  3963. return PORT_TYPE_SCALAR;
  3964. }
  3965. //////////////
  3966. void VisualShaderNodeVarying::_bind_methods() {
  3967. ClassDB::bind_method(D_METHOD("set_varying_name", "name"), &VisualShaderNodeVarying::set_varying_name);
  3968. ClassDB::bind_method(D_METHOD("get_varying_name"), &VisualShaderNodeVarying::get_varying_name);
  3969. ClassDB::bind_method(D_METHOD("set_varying_type", "type"), &VisualShaderNodeVarying::set_varying_type);
  3970. ClassDB::bind_method(D_METHOD("get_varying_type"), &VisualShaderNodeVarying::get_varying_type);
  3971. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "varying_name"), "set_varying_name", "get_varying_name");
  3972. ADD_PROPERTY(PropertyInfo(Variant::INT, "varying_type", PROPERTY_HINT_ENUM, "Float,Vector,Transform"), "set_varying_type", "get_varying_type");
  3973. }
  3974. String VisualShaderNodeVarying::get_type_str() const {
  3975. switch (varying_type) {
  3976. case VisualShader::VARYING_TYPE_FLOAT:
  3977. return "float";
  3978. case VisualShader::VARYING_TYPE_VECTOR_2D:
  3979. return "vec2";
  3980. case VisualShader::VARYING_TYPE_VECTOR_3D:
  3981. return "vec3";
  3982. case VisualShader::VARYING_TYPE_VECTOR_4D:
  3983. return "vec4";
  3984. case VisualShader::VARYING_TYPE_COLOR:
  3985. return "vec4";
  3986. case VisualShader::VARYING_TYPE_TRANSFORM:
  3987. return "mat4";
  3988. default:
  3989. break;
  3990. }
  3991. return "";
  3992. }
  3993. VisualShaderNodeVarying::PortType VisualShaderNodeVarying::get_port_type(VisualShader::VaryingType p_type, int p_port) const {
  3994. switch (p_type) {
  3995. case VisualShader::VARYING_TYPE_VECTOR_2D:
  3996. return PORT_TYPE_VECTOR_2D;
  3997. case VisualShader::VARYING_TYPE_VECTOR_3D:
  3998. return PORT_TYPE_VECTOR_3D;
  3999. case VisualShader::VARYING_TYPE_VECTOR_4D:
  4000. return PORT_TYPE_VECTOR_4D;
  4001. case VisualShader::VARYING_TYPE_COLOR:
  4002. if (p_port == 1) {
  4003. break; // scalar
  4004. }
  4005. return PORT_TYPE_VECTOR_3D;
  4006. case VisualShader::VARYING_TYPE_TRANSFORM:
  4007. return PORT_TYPE_TRANSFORM;
  4008. default:
  4009. break;
  4010. }
  4011. return PORT_TYPE_SCALAR;
  4012. }
  4013. void VisualShaderNodeVarying::set_varying_name(String p_varying_name) {
  4014. if (varying_name == p_varying_name) {
  4015. return;
  4016. }
  4017. varying_name = p_varying_name;
  4018. emit_changed();
  4019. }
  4020. String VisualShaderNodeVarying::get_varying_name() const {
  4021. return varying_name;
  4022. }
  4023. void VisualShaderNodeVarying::set_varying_type(VisualShader::VaryingType p_varying_type) {
  4024. ERR_FAIL_INDEX(p_varying_type, VisualShader::VARYING_TYPE_MAX);
  4025. if (varying_type == p_varying_type) {
  4026. return;
  4027. }
  4028. varying_type = p_varying_type;
  4029. emit_changed();
  4030. }
  4031. VisualShader::VaryingType VisualShaderNodeVarying::get_varying_type() const {
  4032. return varying_type;
  4033. }
  4034. VisualShaderNodeVarying::VisualShaderNodeVarying() {
  4035. }
  4036. ////////////// Varying Setter
  4037. String VisualShaderNodeVaryingSetter::get_caption() const {
  4038. return vformat("VaryingSetter");
  4039. }
  4040. int VisualShaderNodeVaryingSetter::get_input_port_count() const {
  4041. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4042. return 2;
  4043. }
  4044. return 1;
  4045. }
  4046. VisualShaderNodeVaryingSetter::PortType VisualShaderNodeVaryingSetter::get_input_port_type(int p_port) const {
  4047. return get_port_type(varying_type, p_port);
  4048. }
  4049. String VisualShaderNodeVaryingSetter::get_input_port_name(int p_port) const {
  4050. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4051. if (p_port == 0) {
  4052. return "color";
  4053. } else {
  4054. return "alpha";
  4055. }
  4056. }
  4057. return "";
  4058. }
  4059. int VisualShaderNodeVaryingSetter::get_output_port_count() const {
  4060. return 0;
  4061. }
  4062. VisualShaderNodeVaryingSetter::PortType VisualShaderNodeVaryingSetter::get_output_port_type(int p_port) const {
  4063. return PORT_TYPE_SCALAR;
  4064. }
  4065. String VisualShaderNodeVaryingSetter::get_output_port_name(int p_port) const {
  4066. return "";
  4067. }
  4068. String VisualShaderNodeVaryingSetter::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  4069. return vformat("varying %s %s;\n", get_type_str(), varying_name);
  4070. }
  4071. String VisualShaderNodeVaryingSetter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4072. String code;
  4073. if (varying_name == "[None]") {
  4074. return code;
  4075. }
  4076. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4077. code += vformat(" %s = vec4(%s, %s);\n", varying_name, p_input_vars[0], p_input_vars[1]);
  4078. } else {
  4079. code += vformat(" %s = %s;\n", varying_name, p_input_vars[0]);
  4080. }
  4081. return code;
  4082. }
  4083. VisualShaderNodeVaryingSetter::VisualShaderNodeVaryingSetter() {
  4084. }
  4085. ////////////// Varying Getter
  4086. String VisualShaderNodeVaryingGetter::get_caption() const {
  4087. return vformat("VaryingGetter");
  4088. }
  4089. int VisualShaderNodeVaryingGetter::get_input_port_count() const {
  4090. return 0;
  4091. }
  4092. VisualShaderNodeVaryingGetter::PortType VisualShaderNodeVaryingGetter::get_input_port_type(int p_port) const {
  4093. return PORT_TYPE_SCALAR;
  4094. }
  4095. String VisualShaderNodeVaryingGetter::get_input_port_name(int p_port) const {
  4096. return "";
  4097. }
  4098. int VisualShaderNodeVaryingGetter::get_output_port_count() const {
  4099. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4100. return 2;
  4101. }
  4102. return 1;
  4103. }
  4104. VisualShaderNodeVaryingGetter::PortType VisualShaderNodeVaryingGetter::get_output_port_type(int p_port) const {
  4105. return get_port_type(varying_type, p_port);
  4106. }
  4107. String VisualShaderNodeVaryingGetter::get_output_port_name(int p_port) const {
  4108. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4109. if (p_port == 0) {
  4110. return "color";
  4111. } else {
  4112. return "alpha";
  4113. }
  4114. }
  4115. return "";
  4116. }
  4117. bool VisualShaderNodeVaryingGetter::has_output_port_preview(int p_port) const {
  4118. return false;
  4119. }
  4120. String VisualShaderNodeVaryingGetter::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  4121. String from = varying_name;
  4122. String from2;
  4123. if (varying_name == "[None]") {
  4124. switch (varying_type) {
  4125. case VisualShader::VARYING_TYPE_FLOAT:
  4126. from = "0.0";
  4127. break;
  4128. case VisualShader::VARYING_TYPE_VECTOR_2D:
  4129. from = "vec2(0.0)";
  4130. break;
  4131. case VisualShader::VARYING_TYPE_VECTOR_3D:
  4132. from = "vec3(0.0)";
  4133. break;
  4134. case VisualShader::VARYING_TYPE_VECTOR_4D:
  4135. from = "vec4(0.0)";
  4136. break;
  4137. case VisualShader::VARYING_TYPE_COLOR:
  4138. from = "vec3(0.0)";
  4139. from2 = "0.0";
  4140. break;
  4141. case VisualShader::VARYING_TYPE_TRANSFORM:
  4142. from = "mat4(1.0)";
  4143. break;
  4144. default:
  4145. break;
  4146. }
  4147. } else if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4148. from = varying_name + ".rgb";
  4149. from2 = varying_name + ".a";
  4150. }
  4151. if (varying_type == VisualShader::VARYING_TYPE_COLOR) {
  4152. String code;
  4153. code += vformat(" %s = %s;\n", p_output_vars[0], from);
  4154. code += vformat(" %s = %s;\n", p_output_vars[1], from2);
  4155. return code;
  4156. }
  4157. return vformat(" %s = %s;\n", p_output_vars[0], from);
  4158. }
  4159. VisualShaderNodeVaryingGetter::VisualShaderNodeVaryingGetter() {
  4160. varying_name = "[None]";
  4161. }