visual_shader.cpp 179 KB

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