visual_shader_editor_plugin.cpp 240 KB

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