visual_shader_editor_plugin.cpp 265 KB

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