visual_shader_editor_plugin.cpp 210 KB

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