2
0

visual_shader_editor_plugin.cpp 215 KB

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