visual_shader_editor_plugin.cpp 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/io/resource_loader.h"
  32. #include "core/math/math_defs.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/project_settings.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_properties.h"
  38. #include "editor/editor_scale.h"
  39. #include "scene/animation/animation_player.h"
  40. #include "scene/gui/menu_button.h"
  41. #include "scene/gui/panel.h"
  42. #include "scene/main/viewport.h"
  43. #include "scene/resources/visual_shader_nodes.h"
  44. #include "servers/visual/shader_types.h"
  45. Control *VisualShaderNodePlugin::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  46. if (get_script_instance()) {
  47. return get_script_instance()->call("create_editor", p_parent_resource, p_node);
  48. }
  49. return nullptr;
  50. }
  51. void VisualShaderNodePlugin::_bind_methods() {
  52. 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")));
  53. }
  54. ///////////////////
  55. void VisualShaderEditor::edit(VisualShader *p_visual_shader) {
  56. bool changed = false;
  57. if (p_visual_shader) {
  58. if (visual_shader.is_null()) {
  59. changed = true;
  60. } else {
  61. if (visual_shader.ptr() != p_visual_shader) {
  62. changed = true;
  63. }
  64. }
  65. visual_shader = Ref<VisualShader>(p_visual_shader);
  66. if (!visual_shader->is_connected("changed", this, "_update_preview")) {
  67. visual_shader->connect("changed", this, "_update_preview");
  68. }
  69. visual_shader->set_graph_offset(graph->get_scroll_ofs() / EDSCALE);
  70. } else {
  71. if (visual_shader.is_valid()) {
  72. if (visual_shader->is_connected("changed", this, "")) {
  73. visual_shader->disconnect("changed", this, "_update_preview");
  74. }
  75. }
  76. visual_shader.unref();
  77. }
  78. if (visual_shader.is_null()) {
  79. hide();
  80. } else {
  81. if (changed) { // to avoid tree collapse
  82. _clear_buffer();
  83. _update_options_menu();
  84. _update_preview();
  85. }
  86. _update_graph();
  87. }
  88. }
  89. void VisualShaderEditor::add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  90. if (plugins.find(p_plugin) != -1) {
  91. return;
  92. }
  93. plugins.push_back(p_plugin);
  94. }
  95. void VisualShaderEditor::remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin) {
  96. plugins.erase(p_plugin);
  97. }
  98. void VisualShaderEditor::clear_custom_types() {
  99. for (int i = 0; i < add_options.size(); i++) {
  100. if (add_options[i].is_custom) {
  101. add_options.remove(i);
  102. i--;
  103. }
  104. }
  105. }
  106. 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, const String &p_subcategory) {
  107. ERR_FAIL_COND(!p_name.is_valid_identifier());
  108. ERR_FAIL_COND(!p_script.is_valid());
  109. for (int i = 0; i < add_options.size(); i++) {
  110. if (add_options[i].is_custom) {
  111. if (add_options[i].script == p_script) {
  112. return;
  113. }
  114. }
  115. }
  116. AddOption ao;
  117. ao.name = p_name;
  118. ao.script = p_script;
  119. ao.return_type = p_return_icon_type;
  120. ao.description = p_description;
  121. ao.category = p_category;
  122. ao.sub_category = p_subcategory;
  123. ao.is_custom = true;
  124. bool begin = false;
  125. for (int i = 0; i < add_options.size(); i++) {
  126. if (add_options[i].is_custom) {
  127. if (add_options[i].category == p_category) {
  128. if (!begin) {
  129. begin = true;
  130. }
  131. } else {
  132. if (begin) {
  133. add_options.insert(i, ao);
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. add_options.push_back(ao);
  140. }
  141. bool VisualShaderEditor::_is_available(int p_mode) {
  142. int current_mode = edit_type->get_selected();
  143. if (p_mode != -1) {
  144. switch (current_mode) {
  145. case VisualShader::TYPE_VERTEX:
  146. current_mode = 1;
  147. break;
  148. case VisualShader::TYPE_FRAGMENT:
  149. current_mode = 2;
  150. break;
  151. case VisualShader::TYPE_LIGHT:
  152. current_mode = 4;
  153. break;
  154. default:
  155. break;
  156. }
  157. int temp_mode = 0;
  158. if (p_mode & VisualShader::TYPE_FRAGMENT) {
  159. temp_mode |= 2;
  160. }
  161. if (p_mode & VisualShader::TYPE_LIGHT) {
  162. temp_mode |= 4;
  163. }
  164. if (temp_mode == 0) {
  165. temp_mode |= 1;
  166. }
  167. p_mode = temp_mode;
  168. }
  169. return (p_mode == -1 || (p_mode & current_mode) != 0);
  170. }
  171. void VisualShaderEditor::update_custom_nodes() {
  172. if (members_dialog->is_visible()) {
  173. return;
  174. }
  175. clear_custom_types();
  176. List<StringName> class_list;
  177. ScriptServer::get_global_class_list(&class_list);
  178. Dictionary added;
  179. for (int i = 0; i < class_list.size(); i++) {
  180. if (ScriptServer::get_global_class_native_base(class_list[i]) == "VisualShaderNodeCustom") {
  181. String script_path = ScriptServer::get_global_class_path(class_list[i]);
  182. Ref<Resource> res = ResourceLoader::load(script_path);
  183. ERR_FAIL_COND(res.is_null());
  184. ERR_FAIL_COND(!res->is_class("Script"));
  185. Ref<Script> script = Ref<Script>(res);
  186. Ref<VisualShaderNodeCustom> ref;
  187. ref.instance();
  188. ref->set_script(script.get_ref_ptr());
  189. String name;
  190. if (ref->has_method("_get_name")) {
  191. name = (String)ref->call("_get_name");
  192. } else {
  193. name = "Unnamed";
  194. }
  195. String description = "";
  196. if (ref->has_method("_get_description")) {
  197. description = (String)ref->call("_get_description");
  198. }
  199. int return_icon_type = -1;
  200. if (ref->has_method("_get_return_icon_type")) {
  201. return_icon_type = (int)ref->call("_get_return_icon_type");
  202. }
  203. String category = "";
  204. if (ref->has_method("_get_category")) {
  205. category = (String)ref->call("_get_category");
  206. }
  207. if (category == "") {
  208. category = "Custom";
  209. }
  210. String subcategory = "";
  211. if (ref->has_method("_get_subcategory")) {
  212. subcategory = (String)ref->call("_get_subcategory");
  213. }
  214. Dictionary dict;
  215. dict["name"] = name;
  216. dict["script"] = script;
  217. dict["description"] = description;
  218. dict["return_icon_type"] = return_icon_type;
  219. dict["category"] = category;
  220. dict["subcategory"] = subcategory;
  221. String key;
  222. key = category;
  223. key += "/";
  224. if (subcategory != "") {
  225. key += subcategory;
  226. key += "/";
  227. }
  228. key += name;
  229. added[key] = dict;
  230. }
  231. }
  232. Array keys = added.keys();
  233. keys.sort();
  234. for (int i = 0; i < keys.size(); i++) {
  235. const Variant &key = keys.get(i);
  236. const Dictionary &value = (Dictionary)added[key];
  237. add_custom_type(value["name"], value["script"], value["description"], value["return_icon_type"], value["category"], value["subcategory"]);
  238. }
  239. _update_options_menu();
  240. }
  241. String VisualShaderEditor::_get_description(int p_idx) {
  242. if (add_options[p_idx].highend) {
  243. return TTR("(GLES3 only)") + " " + add_options[p_idx].description; // TODO: change it to (Vulkan Only) when its ready
  244. } else {
  245. return add_options[p_idx].description;
  246. }
  247. }
  248. void VisualShaderEditor::_update_options_menu() {
  249. node_desc->set_text("");
  250. members_dialog->get_ok()->set_disabled(true);
  251. String prev_category;
  252. String prev_sub_category;
  253. members->clear();
  254. TreeItem *root = members->create_item();
  255. TreeItem *category = nullptr;
  256. TreeItem *sub_category = nullptr;
  257. String filter = node_filter->get_text().strip_edges();
  258. bool use_filter = !filter.empty();
  259. Vector<String> categories;
  260. Vector<String> sub_categories;
  261. int item_count = 0;
  262. int item_count2 = 0;
  263. bool is_first_item = true;
  264. Color unsupported_color = get_color("error_color", "Editor");
  265. Color supported_color = get_color("warning_color", "Editor");
  266. static bool low_driver = ProjectSettings::get_singleton()->get("rendering/quality/driver/driver_name") == "GLES2";
  267. int current_func = -1;
  268. if (!visual_shader.is_null()) {
  269. current_func = visual_shader->get_mode();
  270. }
  271. for (int i = 0; i < add_options.size() + 1; i++) {
  272. if (i == add_options.size()) {
  273. if (sub_category != nullptr && item_count2 == 0) {
  274. memdelete(sub_category);
  275. --item_count;
  276. }
  277. if (category != nullptr && item_count == 0) {
  278. memdelete(category);
  279. }
  280. break;
  281. }
  282. if (!use_filter || add_options[i].name.findn(filter) != -1) {
  283. if ((add_options[i].func != current_func && add_options[i].func != -1) || !_is_available(add_options[i].mode)) {
  284. continue;
  285. }
  286. if (prev_category != add_options[i].category) {
  287. if (category != nullptr && item_count == 0) {
  288. memdelete(category);
  289. }
  290. item_count = 0;
  291. prev_sub_category = "";
  292. category = members->create_item(root);
  293. category->set_text(0, add_options[i].category);
  294. category->set_selectable(0, false);
  295. if (!use_filter) {
  296. category->set_collapsed(true);
  297. }
  298. }
  299. if (add_options[i].sub_category != "") {
  300. if (prev_sub_category != add_options[i].sub_category) {
  301. if (category != nullptr) {
  302. if (sub_category != nullptr && item_count2 == 0) {
  303. memdelete(sub_category);
  304. --item_count;
  305. }
  306. ++item_count;
  307. item_count2 = 0;
  308. sub_category = members->create_item(category);
  309. sub_category->set_text(0, add_options[i].sub_category);
  310. sub_category->set_selectable(0, false);
  311. if (!use_filter) {
  312. sub_category->set_collapsed(true);
  313. }
  314. }
  315. }
  316. } else {
  317. sub_category = nullptr;
  318. }
  319. TreeItem *p_category = nullptr;
  320. if (sub_category != nullptr) {
  321. p_category = sub_category;
  322. ++item_count2;
  323. } else if (category != nullptr) {
  324. p_category = category;
  325. ++item_count;
  326. }
  327. if (p_category != nullptr) {
  328. TreeItem *item = members->create_item(p_category);
  329. if (add_options[i].highend && low_driver) {
  330. item->set_custom_color(0, unsupported_color);
  331. } else if (add_options[i].highend) {
  332. item->set_custom_color(0, supported_color);
  333. }
  334. item->set_text(0, add_options[i].name);
  335. if (is_first_item && use_filter) {
  336. item->select(0);
  337. node_desc->set_text(_get_description(i));
  338. is_first_item = false;
  339. }
  340. switch (add_options[i].return_type) {
  341. case VisualShaderNode::PORT_TYPE_SCALAR:
  342. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"));
  343. break;
  344. case VisualShaderNode::PORT_TYPE_VECTOR:
  345. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"));
  346. break;
  347. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  348. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"));
  349. break;
  350. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  351. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"));
  352. break;
  353. case VisualShaderNode::PORT_TYPE_SAMPLER:
  354. item->set_icon(0, EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"));
  355. break;
  356. default:
  357. break;
  358. }
  359. item->set_meta("id", i);
  360. }
  361. prev_sub_category = add_options[i].sub_category;
  362. prev_category = add_options[i].category;
  363. }
  364. }
  365. }
  366. Size2 VisualShaderEditor::get_minimum_size() const {
  367. return Size2(10, 200);
  368. }
  369. void VisualShaderEditor::_draw_color_over_button(Object *obj, Color p_color) {
  370. Button *button = Object::cast_to<Button>(obj);
  371. if (!button) {
  372. return;
  373. }
  374. Ref<StyleBox> normal = get_stylebox("normal", "Button");
  375. button->draw_rect(Rect2(normal->get_offset(), button->get_size() - normal->get_minimum_size()), p_color);
  376. }
  377. 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) {
  378. Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));
  379. style->set_default_margin(MARGIN_LEFT, p_margin_left * EDSCALE);
  380. style->set_default_margin(MARGIN_RIGHT, p_margin_right * EDSCALE);
  381. style->set_default_margin(MARGIN_BOTTOM, p_margin_bottom * EDSCALE);
  382. style->set_default_margin(MARGIN_TOP, p_margin_top * EDSCALE);
  383. return style;
  384. }
  385. void VisualShaderEditor::_update_created_node(GraphNode *node) {
  386. if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
  387. Ref<StyleBoxFlat> sb = node->get_stylebox("frame", "GraphNode");
  388. Color c = sb->get_border_color();
  389. Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
  390. mono_color.a = 0.85;
  391. c = mono_color;
  392. node->add_color_override("title_color", c);
  393. c.a = 0.7;
  394. node->add_color_override("close_color", c);
  395. node->add_color_override("resizer_color", c);
  396. }
  397. }
  398. void VisualShaderEditor::_update_graph() {
  399. if (updating) {
  400. return;
  401. }
  402. if (visual_shader.is_null()) {
  403. return;
  404. }
  405. graph->set_scroll_ofs(visual_shader->get_graph_offset() * EDSCALE);
  406. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  407. graph->clear_connections();
  408. //erase all nodes
  409. for (int i = 0; i < graph->get_child_count(); i++) {
  410. if (Object::cast_to<GraphNode>(graph->get_child(i))) {
  411. Node *node = graph->get_child(i);
  412. graph->remove_child(node);
  413. memdelete(node);
  414. i--;
  415. }
  416. }
  417. static const Color type_color[5] = {
  418. Color(0.38, 0.85, 0.96), // scalar
  419. Color(0.84, 0.49, 0.93), // vector
  420. Color(0.55, 0.65, 0.94), // boolean
  421. Color(0.96, 0.66, 0.43), // transform
  422. Color(1.0, 1.0, 0.0) // sampler
  423. };
  424. List<VisualShader::Connection> connections;
  425. visual_shader->get_node_connections(type, &connections);
  426. Ref<StyleBoxEmpty> label_style = make_empty_stylebox(2, 1, 2, 1);
  427. Vector<int> nodes = visual_shader->get_node_list(type);
  428. VisualShaderNodeUniformRef::clear_uniforms();
  429. // scan for all uniforms
  430. for (int t = 0; t < VisualShader::TYPE_MAX; t++) {
  431. Vector<int> tnodes = visual_shader->get_node_list((VisualShader::Type)t);
  432. for (int i = 0; i < tnodes.size(); i++) {
  433. Ref<VisualShaderNode> vsnode = visual_shader->get_node((VisualShader::Type)t, tnodes[i]);
  434. Ref<VisualShaderNodeUniform> uniform = vsnode;
  435. if (uniform.is_valid()) {
  436. Ref<VisualShaderNodeScalarUniform> scalar_uniform = vsnode;
  437. Ref<VisualShaderNodeVec3Uniform> vec3_uniform = vsnode;
  438. Ref<VisualShaderNodeColorUniform> color_uniform = vsnode;
  439. Ref<VisualShaderNodeBooleanUniform> bool_uniform = vsnode;
  440. Ref<VisualShaderNodeTransformUniform> transform_uniform = vsnode;
  441. VisualShaderNodeUniformRef::UniformType uniform_type;
  442. if (scalar_uniform.is_valid()) {
  443. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SCALAR;
  444. } else if (bool_uniform.is_valid()) {
  445. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_BOOLEAN;
  446. } else if (vec3_uniform.is_valid()) {
  447. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_VECTOR;
  448. } else if (transform_uniform.is_valid()) {
  449. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_TRANSFORM;
  450. } else if (color_uniform.is_valid()) {
  451. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_COLOR;
  452. } else {
  453. uniform_type = VisualShaderNodeUniformRef::UniformType::UNIFORM_TYPE_SAMPLER;
  454. }
  455. VisualShaderNodeUniformRef::add_uniform(uniform->get_uniform_name(), uniform_type);
  456. }
  457. }
  458. }
  459. Control *offset;
  460. for (int n_i = 0; n_i < nodes.size(); n_i++) {
  461. Vector2 position = visual_shader->get_node_position(type, nodes[n_i]);
  462. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, nodes[n_i]);
  463. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(vsnode.ptr());
  464. bool is_group = !group_node.is_null();
  465. Size2 size = Size2(0, 0);
  466. VisualShaderNodeCustom *custom = Object::cast_to<VisualShaderNodeCustom>(vsnode.ptr());
  467. if (custom) {
  468. custom->_set_initialized(true);
  469. }
  470. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(group_node.ptr());
  471. bool is_expression = !expression_node.is_null();
  472. String expression = "";
  473. GraphNode *node = memnew(GraphNode);
  474. if (is_group) {
  475. size = group_node->get_size();
  476. node->set_resizable(true);
  477. node->connect("resize_request", this, "_node_resized", varray((int)type, nodes[n_i]));
  478. }
  479. if (is_expression) {
  480. expression = expression_node->get_expression();
  481. }
  482. /*if (!vsnode->is_connected("changed", this, "_node_changed")) {
  483. vsnode->connect("changed", this, "_node_changed", varray(vsnode->get_instance_id()), CONNECT_DEFERRED);
  484. }*/
  485. node->set_offset(position);
  486. node->set_title(vsnode->get_caption());
  487. node->set_name(itos(nodes[n_i]));
  488. if (nodes[n_i] >= 2) {
  489. node->set_show_close_button(true);
  490. node->connect("close_request", this, "_delete_request", varray(nodes[n_i]), CONNECT_DEFERRED);
  491. }
  492. node->connect("dragged", this, "_node_dragged", varray(nodes[n_i]));
  493. Control *custom_editor = nullptr;
  494. int port_offset = 0;
  495. if (is_group) {
  496. port_offset += 2;
  497. }
  498. Ref<VisualShaderNodeUniform> uniform = vsnode;
  499. HBoxContainer *uniform_hbox = nullptr;
  500. if (uniform.is_valid()) {
  501. graph->add_child(node);
  502. _update_created_node(node);
  503. LineEdit *uniform_name = memnew(LineEdit);
  504. uniform_name->set_h_size_flags(SIZE_EXPAND_FILL);
  505. uniform_name->set_text(uniform->get_uniform_name());
  506. uniform_name->connect("text_entered", this, "_line_edit_changed", varray(uniform_name, nodes[n_i]));
  507. uniform_name->connect("focus_exited", this, "_line_edit_focus_out", varray(uniform_name, nodes[n_i]));
  508. if (vsnode->get_output_port_count() == 1 && vsnode->get_output_port_name(0) == "") {
  509. uniform_hbox = memnew(HBoxContainer);
  510. uniform_hbox->add_constant_override("separation", 7 * EDSCALE);
  511. uniform_hbox->add_child(uniform_name);
  512. node->add_child(uniform_hbox);
  513. } else {
  514. node->add_child(uniform_name);
  515. }
  516. port_offset++;
  517. }
  518. for (int i = 0; i < plugins.size(); i++) {
  519. custom_editor = plugins.write[i]->create_editor(visual_shader, vsnode);
  520. if (custom_editor) {
  521. break;
  522. }
  523. }
  524. if (custom_editor) {
  525. if (uniform_hbox == nullptr && vsnode->get_output_port_count() > 0 && vsnode->get_output_port_name(0) == "" && (vsnode->get_input_port_count() == 0 || vsnode->get_input_port_name(0) == "")) {
  526. //will be embedded in first port
  527. } else {
  528. port_offset++;
  529. node->add_child(custom_editor);
  530. custom_editor = nullptr;
  531. }
  532. }
  533. if (is_group) {
  534. offset = memnew(Control);
  535. offset->set_custom_minimum_size(Size2(0, 6 * EDSCALE));
  536. node->add_child(offset);
  537. if (group_node->is_editable()) {
  538. HBoxContainer *hb2 = memnew(HBoxContainer);
  539. Button *add_input_btn = memnew(Button);
  540. add_input_btn->set_text(TTR("Add Input"));
  541. add_input_btn->connect("pressed", this, "_add_input_port", varray(nodes[n_i], group_node->get_free_input_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "input" + itos(group_node->get_free_input_port_id())), CONNECT_DEFERRED);
  542. hb2->add_child(add_input_btn);
  543. hb2->add_spacer();
  544. Button *add_output_btn = memnew(Button);
  545. add_output_btn->set_text(TTR("Add Output"));
  546. add_output_btn->connect("pressed", this, "_add_output_port", varray(nodes[n_i], group_node->get_free_output_port_id(), VisualShaderNode::PORT_TYPE_VECTOR, "output" + itos(group_node->get_free_output_port_id())), CONNECT_DEFERRED);
  547. hb2->add_child(add_output_btn);
  548. node->add_child(hb2);
  549. }
  550. }
  551. for (int i = 0; i < MAX(vsnode->get_input_port_count(), vsnode->get_output_port_count()); i++) {
  552. if (vsnode->is_port_separator(i)) {
  553. node->add_child(memnew(HSeparator));
  554. port_offset++;
  555. }
  556. bool valid_left = i < vsnode->get_input_port_count();
  557. VisualShaderNode::PortType port_left = VisualShaderNode::PORT_TYPE_SCALAR;
  558. bool port_left_used = false;
  559. String name_left;
  560. if (valid_left) {
  561. name_left = vsnode->get_input_port_name(i);
  562. port_left = vsnode->get_input_port_type(i);
  563. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  564. if (E->get().to_node == nodes[n_i] && E->get().to_port == i) {
  565. port_left_used = true;
  566. }
  567. }
  568. }
  569. bool valid_right = i < vsnode->get_output_port_count();
  570. VisualShaderNode::PortType port_right = VisualShaderNode::PORT_TYPE_SCALAR;
  571. String name_right;
  572. if (valid_right) {
  573. name_right = vsnode->get_output_port_name(i);
  574. port_right = vsnode->get_output_port_type(i);
  575. }
  576. HBoxContainer *hb = nullptr;
  577. bool is_uniform_hbox = false;
  578. if (i == 0 && uniform_hbox != nullptr) {
  579. hb = uniform_hbox;
  580. is_uniform_hbox = true;
  581. } else {
  582. hb = memnew(HBoxContainer);
  583. hb->add_constant_override("separation", 7 * EDSCALE);
  584. }
  585. Variant default_value;
  586. if (valid_left && !port_left_used) {
  587. default_value = vsnode->get_input_port_default_value(i);
  588. }
  589. if (default_value.get_type() != Variant::NIL) { // only a label
  590. Button *button = memnew(Button);
  591. hb->add_child(button);
  592. button->connect("pressed", this, "_edit_port_default_input", varray(button, nodes[n_i], i));
  593. switch (default_value.get_type()) {
  594. case Variant::COLOR: {
  595. button->set_custom_minimum_size(Size2(30, 0) * EDSCALE);
  596. button->connect("draw", this, "_draw_color_over_button", varray(button, default_value));
  597. } break;
  598. case Variant::BOOL: {
  599. button->set_text(((bool)default_value) ? "true" : "false");
  600. } break;
  601. case Variant::INT:
  602. case Variant::REAL: {
  603. button->set_text(String::num(default_value, 4));
  604. } break;
  605. case Variant::VECTOR3: {
  606. Vector3 v = default_value;
  607. button->set_text(String::num(v.x, 3) + "," + String::num(v.y, 3) + "," + String::num(v.z, 3));
  608. } break;
  609. default: {
  610. }
  611. }
  612. }
  613. if (i == 0 && custom_editor) {
  614. hb->add_child(custom_editor);
  615. custom_editor->set_h_size_flags(SIZE_EXPAND_FILL);
  616. } else {
  617. if (valid_left) {
  618. if (is_group) {
  619. OptionButton *type_box = memnew(OptionButton);
  620. hb->add_child(type_box);
  621. type_box->add_item(TTR("Scalar"));
  622. type_box->add_item(TTR("Vector"));
  623. type_box->add_item(TTR("Boolean"));
  624. type_box->add_item(TTR("Transform"));
  625. type_box->add_item(TTR("Sampler"));
  626. type_box->select(group_node->get_input_port_type(i));
  627. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  628. type_box->connect("item_selected", this, "_change_input_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  629. LineEdit *name_box = memnew(LineEdit);
  630. hb->add_child(name_box);
  631. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  632. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  633. name_box->set_text(name_left);
  634. name_box->connect("text_entered", this, "_change_input_port_name", varray(name_box, nodes[n_i], i));
  635. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, false));
  636. Button *remove_btn = memnew(Button);
  637. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  638. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  639. remove_btn->connect("pressed", this, "_remove_input_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  640. hb->add_child(remove_btn);
  641. } else {
  642. Label *label = memnew(Label);
  643. label->set_text(name_left);
  644. label->add_style_override("normal", label_style); //more compact
  645. hb->add_child(label);
  646. if (vsnode->get_input_port_default_hint(i) != "" && !port_left_used) {
  647. Label *hint_label = memnew(Label);
  648. hint_label->set_text("[" + vsnode->get_input_port_default_hint(i) + "]");
  649. hint_label->add_color_override("font_color", get_color("font_color_readonly", "TextEdit"));
  650. hint_label->add_style_override("normal", label_style);
  651. hb->add_child(hint_label);
  652. }
  653. }
  654. }
  655. if (!is_group && !is_uniform_hbox) {
  656. hb->add_spacer();
  657. }
  658. if (valid_right) {
  659. if (is_group) {
  660. Button *remove_btn = memnew(Button);
  661. remove_btn->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Remove", "EditorIcons"));
  662. remove_btn->set_tooltip(TTR("Remove") + " " + name_left);
  663. remove_btn->connect("pressed", this, "_remove_output_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  664. hb->add_child(remove_btn);
  665. LineEdit *name_box = memnew(LineEdit);
  666. hb->add_child(name_box);
  667. name_box->set_custom_minimum_size(Size2(65 * EDSCALE, 0));
  668. name_box->set_h_size_flags(SIZE_EXPAND_FILL);
  669. name_box->set_text(name_right);
  670. name_box->connect("text_entered", this, "_change_output_port_name", varray(name_box, nodes[n_i], i));
  671. name_box->connect("focus_exited", this, "_port_name_focus_out", varray(name_box, nodes[n_i], i, true));
  672. OptionButton *type_box = memnew(OptionButton);
  673. hb->add_child(type_box);
  674. type_box->add_item(TTR("Scalar"));
  675. type_box->add_item(TTR("Vector"));
  676. type_box->add_item(TTR("Boolean"));
  677. type_box->add_item(TTR("Transform"));
  678. type_box->select(group_node->get_output_port_type(i));
  679. type_box->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  680. type_box->connect("item_selected", this, "_change_output_port_type", varray(nodes[n_i], i), CONNECT_DEFERRED);
  681. } else {
  682. if (name_right != "") {
  683. Label *label = memnew(Label);
  684. label->set_text(name_right);
  685. label->add_style_override("normal", label_style); //more compact
  686. hb->add_child(label);
  687. }
  688. }
  689. }
  690. }
  691. if (valid_right && edit_type->get_selected() == VisualShader::TYPE_FRAGMENT && port_right != VisualShaderNode::PORT_TYPE_TRANSFORM && port_right != VisualShaderNode::PORT_TYPE_SAMPLER) {
  692. TextureButton *preview = memnew(TextureButton);
  693. preview->set_toggle_mode(true);
  694. preview->set_normal_texture(get_icon("GuiVisibilityHidden", "EditorIcons"));
  695. preview->set_pressed_texture(get_icon("GuiVisibilityVisible", "EditorIcons"));
  696. preview->set_v_size_flags(SIZE_SHRINK_CENTER);
  697. if (vsnode->get_output_port_for_preview() == i) {
  698. preview->set_pressed(true);
  699. }
  700. preview->connect("pressed", this, "_preview_select_port", varray(nodes[n_i], i), CONNECT_DEFERRED);
  701. hb->add_child(preview);
  702. }
  703. if (is_group) {
  704. offset = memnew(Control);
  705. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  706. node->add_child(offset);
  707. port_offset++;
  708. }
  709. int idx = 0;
  710. if (!is_uniform_hbox) {
  711. node->add_child(hb);
  712. idx = i + port_offset;
  713. }
  714. node->set_slot(idx, valid_left, port_left, type_color[port_left], valid_right, port_right, type_color[port_right]);
  715. }
  716. if (vsnode->get_output_port_for_preview() >= 0) {
  717. int port_type = vsnode->get_output_port_type(vsnode->get_output_port_for_preview());
  718. if (port_type != VisualShaderNode::PORT_TYPE_TRANSFORM && port_type != VisualShaderNode::PORT_TYPE_SAMPLER) {
  719. offset = memnew(Control);
  720. offset->set_custom_minimum_size(Size2(0, 5 * EDSCALE));
  721. node->add_child(offset);
  722. VisualShaderNodePortPreview *port_preview = memnew(VisualShaderNodePortPreview);
  723. port_preview->setup(visual_shader, type, nodes[n_i], vsnode->get_output_port_for_preview());
  724. port_preview->set_h_size_flags(SIZE_SHRINK_CENTER);
  725. node->add_child(port_preview);
  726. }
  727. }
  728. offset = memnew(Control);
  729. offset->set_custom_minimum_size(Size2(0, 4 * EDSCALE));
  730. node->add_child(offset);
  731. String error = vsnode->get_warning(visual_shader->get_mode(), type);
  732. if (error != String()) {
  733. Label *error_label = memnew(Label);
  734. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  735. error_label->set_text(error);
  736. node->add_child(error_label);
  737. }
  738. if (is_expression) {
  739. TextEdit *expression_box = memnew(TextEdit);
  740. expression_node->set_control(expression_box, 0);
  741. node->add_child(expression_box);
  742. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  743. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  744. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  745. Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
  746. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  747. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  748. expression_box->set_syntax_coloring(true);
  749. expression_box->add_color_override("background_color", background_color);
  750. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  751. if (ShaderLanguage::is_control_flow_keyword(E->get())) {
  752. expression_box->add_keyword_color(E->get(), control_flow_keyword_color);
  753. } else {
  754. expression_box->add_keyword_color(E->get(), keyword_color);
  755. }
  756. }
  757. expression_box->add_font_override("font", get_font("expression", "EditorFonts"));
  758. expression_box->add_color_override("font_color", text_color);
  759. expression_box->add_color_override("symbol_color", symbol_color);
  760. expression_box->add_color_region("/*", "*/", comment_color, false);
  761. expression_box->add_color_region("//", "", comment_color, false);
  762. expression_box->set_text(expression);
  763. expression_box->set_context_menu_enabled(false);
  764. expression_box->set_show_line_numbers(true);
  765. expression_box->connect("focus_exited", this, "_expression_focus_out", varray(expression_box, nodes[n_i]));
  766. }
  767. if (!uniform.is_valid()) {
  768. graph->add_child(node);
  769. _update_created_node(node);
  770. if (is_group) {
  771. call_deferred("_set_node_size", (int)type, nodes[n_i], size);
  772. }
  773. }
  774. }
  775. for (List<VisualShader::Connection>::Element *E = connections.front(); E; E = E->next()) {
  776. int from = E->get().from_node;
  777. int from_idx = E->get().from_port;
  778. int to = E->get().to_node;
  779. int to_idx = E->get().to_port;
  780. graph->connect_node(itos(from), from_idx, itos(to), to_idx);
  781. }
  782. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  783. graph->set_minimap_opacity(graph_minimap_opacity);
  784. }
  785. void VisualShaderEditor::_add_input_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  786. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  787. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  788. if (node.is_null()) {
  789. return;
  790. }
  791. undo_redo->create_action(TTR("Add input port"));
  792. undo_redo->add_do_method(node.ptr(), "add_input_port", p_port, p_port_type, p_name);
  793. undo_redo->add_undo_method(node.ptr(), "remove_input_port", p_port);
  794. undo_redo->add_do_method(this, "_update_graph");
  795. undo_redo->add_undo_method(this, "_update_graph");
  796. undo_redo->add_do_method(this, "_rebuild");
  797. undo_redo->add_undo_method(this, "_rebuild");
  798. undo_redo->commit_action();
  799. }
  800. void VisualShaderEditor::_add_output_port(int p_node, int p_port, int p_port_type, const String &p_name) {
  801. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  802. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  803. if (node.is_null()) {
  804. return;
  805. }
  806. undo_redo->create_action(TTR("Add output port"));
  807. undo_redo->add_do_method(node.ptr(), "add_output_port", p_port, p_port_type, p_name);
  808. undo_redo->add_undo_method(node.ptr(), "remove_output_port", p_port);
  809. undo_redo->add_do_method(this, "_update_graph");
  810. undo_redo->add_undo_method(this, "_update_graph");
  811. undo_redo->add_do_method(this, "_rebuild");
  812. undo_redo->add_undo_method(this, "_rebuild");
  813. undo_redo->commit_action();
  814. }
  815. void VisualShaderEditor::_change_input_port_type(int p_type, int p_node, int p_port) {
  816. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  817. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  818. if (node.is_null()) {
  819. return;
  820. }
  821. undo_redo->create_action(TTR("Change input port type"));
  822. undo_redo->add_do_method(node.ptr(), "set_input_port_type", p_port, p_type);
  823. undo_redo->add_undo_method(node.ptr(), "set_input_port_type", p_port, node->get_input_port_type(p_port));
  824. undo_redo->add_do_method(this, "_update_graph");
  825. undo_redo->add_undo_method(this, "_update_graph");
  826. undo_redo->add_do_method(this, "_rebuild");
  827. undo_redo->add_undo_method(this, "_rebuild");
  828. undo_redo->commit_action();
  829. }
  830. void VisualShaderEditor::_change_output_port_type(int p_type, int p_node, int p_port) {
  831. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  832. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  833. if (node.is_null()) {
  834. return;
  835. }
  836. undo_redo->create_action(TTR("Change output port type"));
  837. undo_redo->add_do_method(node.ptr(), "set_output_port_type", p_port, p_type);
  838. undo_redo->add_undo_method(node.ptr(), "set_output_port_type", p_port, node->get_output_port_type(p_port));
  839. undo_redo->add_do_method(this, "_update_graph");
  840. undo_redo->add_undo_method(this, "_update_graph");
  841. undo_redo->add_do_method(this, "_rebuild");
  842. undo_redo->add_undo_method(this, "_rebuild");
  843. undo_redo->commit_action();
  844. }
  845. void VisualShaderEditor::_change_input_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  846. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  847. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  848. ERR_FAIL_COND(!node.is_valid());
  849. undo_redo->create_action(TTR("Change input port name"));
  850. undo_redo->add_do_method(node.ptr(), "set_input_port_name", p_port_id, p_text);
  851. undo_redo->add_undo_method(node.ptr(), "set_input_port_name", p_port_id, node->get_input_port_name(p_port_id));
  852. undo_redo->add_do_method(this, "_rebuild");
  853. undo_redo->add_undo_method(this, "_rebuild");
  854. undo_redo->commit_action();
  855. }
  856. void VisualShaderEditor::_change_output_port_name(const String &p_text, Object *line_edit, int p_node_id, int p_port_id) {
  857. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  858. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  859. ERR_FAIL_COND(!node.is_valid());
  860. undo_redo->create_action(TTR("Change output port name"));
  861. undo_redo->add_do_method(node.ptr(), "set_output_port_name", p_port_id, p_text);
  862. undo_redo->add_undo_method(node.ptr(), "set_output_port_name", p_port_id, node->get_output_port_name(p_port_id));
  863. undo_redo->add_do_method(this, "_rebuild");
  864. undo_redo->add_undo_method(this, "_rebuild");
  865. undo_redo->commit_action();
  866. }
  867. void VisualShaderEditor::_remove_input_port(int p_node, int p_port) {
  868. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  869. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  870. if (node.is_null()) {
  871. return;
  872. }
  873. undo_redo->create_action(TTR("Remove input port"));
  874. List<VisualShader::Connection> conns;
  875. visual_shader->get_node_connections(type, &conns);
  876. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  877. int from_node = E->get().from_node;
  878. int from_port = E->get().from_port;
  879. int to_node = E->get().to_node;
  880. int to_port = E->get().to_port;
  881. if (to_node == p_node) {
  882. if (to_port == p_port) {
  883. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  884. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  885. } else if (to_port > p_port) {
  886. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  887. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  888. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port - 1);
  889. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port - 1);
  890. }
  891. }
  892. }
  893. undo_redo->add_do_method(node.ptr(), "remove_input_port", p_port);
  894. 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));
  895. undo_redo->add_do_method(this, "_update_graph");
  896. undo_redo->add_undo_method(this, "_update_graph");
  897. undo_redo->add_do_method(this, "_rebuild");
  898. undo_redo->add_undo_method(this, "_rebuild");
  899. undo_redo->commit_action();
  900. }
  901. void VisualShaderEditor::_remove_output_port(int p_node, int p_port) {
  902. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  903. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  904. if (node.is_null()) {
  905. return;
  906. }
  907. undo_redo->create_action(TTR("Remove output port"));
  908. List<VisualShader::Connection> conns;
  909. visual_shader->get_node_connections(type, &conns);
  910. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  911. int from_node = E->get().from_node;
  912. int from_port = E->get().from_port;
  913. int to_node = E->get().to_node;
  914. int to_port = E->get().to_port;
  915. if (from_node == p_node) {
  916. if (from_port == p_port) {
  917. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  918. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  919. } else if (from_port > p_port) {
  920. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port, to_node, to_port);
  921. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port, to_node, to_port);
  922. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, from_node, from_port - 1, to_node, to_port);
  923. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_port - 1, to_node, to_port);
  924. }
  925. }
  926. }
  927. undo_redo->add_do_method(node.ptr(), "remove_output_port", p_port);
  928. 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));
  929. undo_redo->add_do_method(this, "_update_graph");
  930. undo_redo->add_undo_method(this, "_update_graph");
  931. undo_redo->add_do_method(this, "_rebuild");
  932. undo_redo->add_undo_method(this, "_rebuild");
  933. undo_redo->commit_action();
  934. }
  935. void VisualShaderEditor::_expression_focus_out(Object *text_edit, int p_node) {
  936. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  937. Ref<VisualShaderNodeExpression> node = visual_shader->get_node(type, p_node);
  938. if (node.is_null()) {
  939. return;
  940. }
  941. TextEdit *expression_box = Object::cast_to<TextEdit>(text_edit);
  942. if (node->get_expression() == expression_box->get_text()) {
  943. return;
  944. }
  945. undo_redo->create_action(TTR("Set expression"));
  946. undo_redo->add_do_method(node.ptr(), "set_expression", expression_box->get_text());
  947. undo_redo->add_undo_method(node.ptr(), "set_expression", node->get_expression());
  948. undo_redo->add_do_method(this, "_rebuild");
  949. undo_redo->add_undo_method(this, "_rebuild");
  950. undo_redo->commit_action();
  951. }
  952. void VisualShaderEditor::_rebuild() {
  953. if (visual_shader != nullptr) {
  954. EditorNode::get_singleton()->get_log()->clear();
  955. visual_shader->rebuild();
  956. }
  957. }
  958. void VisualShaderEditor::_set_node_size(int p_type, int p_node, const Vector2 &p_size) {
  959. VisualShader::Type type = VisualShader::Type(p_type);
  960. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  961. if (node.is_null()) {
  962. return;
  963. }
  964. Ref<VisualShaderNodeGroupBase> group_node = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  965. if (group_node.is_null()) {
  966. return;
  967. }
  968. Vector2 size = p_size;
  969. group_node->set_size(size);
  970. GraphNode *gn = nullptr;
  971. if (edit_type->get_selected() == p_type) { // check - otherwise the error will be emitted
  972. Node *node2 = graph->get_node(itos(p_node));
  973. gn = Object::cast_to<GraphNode>(node2);
  974. if (!gn) {
  975. return;
  976. }
  977. gn->set_custom_minimum_size(size);
  978. gn->set_size(Size2(1, 1));
  979. }
  980. Ref<VisualShaderNodeExpression> expression_node = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  981. if (!expression_node.is_null()) {
  982. Control *text_box = expression_node->get_control(0);
  983. Size2 box_size = size;
  984. if (gn != nullptr) {
  985. if (box_size.x < 150 * EDSCALE || box_size.y < 0) {
  986. box_size.x = gn->get_size().x;
  987. }
  988. }
  989. box_size.x -= text_box->get_margin(MARGIN_LEFT);
  990. box_size.x -= 28 * EDSCALE;
  991. box_size.y -= text_box->get_margin(MARGIN_TOP);
  992. box_size.y -= 28 * EDSCALE;
  993. text_box->set_custom_minimum_size(Size2(box_size.x, box_size.y));
  994. text_box->set_size(Size2(1, 1));
  995. }
  996. }
  997. void VisualShaderEditor::_node_resized(const Vector2 &p_new_size, int p_type, int p_node) {
  998. VisualShader::Type type = VisualShader::Type(p_type);
  999. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node);
  1000. if (node.is_null()) {
  1001. return;
  1002. }
  1003. undo_redo->create_action(TTR("Resize VisualShader node"), UndoRedo::MERGE_ENDS);
  1004. undo_redo->add_do_method(this, "_set_node_size", p_type, p_node, p_new_size);
  1005. undo_redo->add_undo_method(this, "_set_node_size", p_type, p_node, node->get_size());
  1006. undo_redo->commit_action();
  1007. }
  1008. void VisualShaderEditor::_preview_select_port(int p_node, int p_port) {
  1009. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1010. Ref<VisualShaderNode> node = visual_shader->get_node(type, p_node);
  1011. if (node.is_null()) {
  1012. return;
  1013. }
  1014. if (node->get_output_port_for_preview() == p_port) {
  1015. p_port = -1; //toggle it
  1016. }
  1017. undo_redo->create_action(TTR("Set Uniform Name"));
  1018. undo_redo->add_do_method(node.ptr(), "set_output_port_for_preview", p_port);
  1019. undo_redo->add_undo_method(node.ptr(), "set_output_port_for_preview", node->get_output_port_for_preview());
  1020. undo_redo->add_do_method(this, "_update_graph");
  1021. undo_redo->add_undo_method(this, "_update_graph");
  1022. undo_redo->commit_action();
  1023. }
  1024. void VisualShaderEditor::_line_edit_changed(const String &p_text, Object *line_edit, int p_node_id) {
  1025. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1026. Ref<VisualShaderNodeUniform> node = visual_shader->get_node(type, p_node_id);
  1027. ERR_FAIL_COND(!node.is_valid());
  1028. String validated_name = visual_shader->validate_uniform_name(p_text, node);
  1029. updating = true;
  1030. undo_redo->create_action(TTR("Set Uniform Name"));
  1031. undo_redo->add_do_method(node.ptr(), "set_uniform_name", validated_name);
  1032. undo_redo->add_undo_method(node.ptr(), "set_uniform_name", node->get_uniform_name());
  1033. undo_redo->add_do_method(this, "_update_graph");
  1034. undo_redo->add_undo_method(this, "_update_graph");
  1035. undo_redo->commit_action();
  1036. updating = false;
  1037. Object::cast_to<LineEdit>(line_edit)->set_text(validated_name);
  1038. }
  1039. void VisualShaderEditor::_line_edit_focus_out(Object *line_edit, int p_node_id) {
  1040. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1041. _line_edit_changed(text, line_edit, p_node_id);
  1042. }
  1043. void VisualShaderEditor::_port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output) {
  1044. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1045. Ref<VisualShaderNodeGroupBase> node = visual_shader->get_node(type, p_node_id);
  1046. ERR_FAIL_COND(!node.is_valid());
  1047. String text = Object::cast_to<LineEdit>(line_edit)->get_text();
  1048. if (!p_output) {
  1049. if (node->get_input_port_name(p_port_id) == text) {
  1050. return;
  1051. }
  1052. } else {
  1053. if (node->get_output_port_name(p_port_id) == text) {
  1054. return;
  1055. }
  1056. }
  1057. List<String> input_names;
  1058. List<String> output_names;
  1059. for (int i = 0; i < node->get_input_port_count(); i++) {
  1060. if (!p_output && i == p_port_id) {
  1061. continue;
  1062. }
  1063. input_names.push_back(node->get_input_port_name(i));
  1064. }
  1065. for (int i = 0; i < node->get_output_port_count(); i++) {
  1066. if (p_output && i == p_port_id) {
  1067. continue;
  1068. }
  1069. output_names.push_back(node->get_output_port_name(i));
  1070. }
  1071. String validated_name = visual_shader->validate_port_name(text, input_names, output_names);
  1072. if (validated_name == "") {
  1073. if (!p_output) {
  1074. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_input_port_name(p_port_id));
  1075. } else {
  1076. Object::cast_to<LineEdit>(line_edit)->set_text(node->get_output_port_name(p_port_id));
  1077. }
  1078. return;
  1079. }
  1080. if (!p_output) {
  1081. _change_input_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1082. } else {
  1083. _change_output_port_name(validated_name, line_edit, p_node_id, p_port_id);
  1084. }
  1085. }
  1086. void VisualShaderEditor::_port_edited() {
  1087. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1088. Variant value = property_editor->get_variant();
  1089. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, editing_node);
  1090. ERR_FAIL_COND(!vsn.is_valid());
  1091. undo_redo->create_action(TTR("Set Input Default Port"));
  1092. Ref<VisualShaderNodeCustom> custom = Object::cast_to<VisualShaderNodeCustom>(vsn.ptr());
  1093. if (custom.is_valid()) {
  1094. undo_redo->add_do_method(custom.ptr(), "_set_input_port_default_value", editing_port, value);
  1095. undo_redo->add_undo_method(custom.ptr(), "_set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1096. } else {
  1097. undo_redo->add_do_method(vsn.ptr(), "set_input_port_default_value", editing_port, value);
  1098. undo_redo->add_undo_method(vsn.ptr(), "set_input_port_default_value", editing_port, vsn->get_input_port_default_value(editing_port));
  1099. }
  1100. undo_redo->add_do_method(this, "_update_graph");
  1101. undo_redo->add_undo_method(this, "_update_graph");
  1102. undo_redo->commit_action();
  1103. property_editor->hide();
  1104. }
  1105. void VisualShaderEditor::_edit_port_default_input(Object *p_button, int p_node, int p_port) {
  1106. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1107. Ref<VisualShaderNode> vsn = visual_shader->get_node(type, p_node);
  1108. Button *button = Object::cast_to<Button>(p_button);
  1109. ERR_FAIL_COND(!button);
  1110. Variant value = vsn->get_input_port_default_value(p_port);
  1111. property_editor->set_global_position(button->get_global_position() + Vector2(0, button->get_size().height));
  1112. property_editor->edit(nullptr, "", value.get_type(), value, 0, "");
  1113. property_editor->popup();
  1114. editing_node = p_node;
  1115. editing_port = p_port;
  1116. }
  1117. void VisualShaderEditor::_add_custom_node(const String &p_path) {
  1118. int idx = -1;
  1119. for (int i = custom_node_option_idx; i < add_options.size(); i++) {
  1120. if (add_options[i].script.is_valid()) {
  1121. if (add_options[i].script->get_path() == p_path) {
  1122. idx = i;
  1123. break;
  1124. }
  1125. }
  1126. }
  1127. if (idx != -1) {
  1128. _add_node(idx);
  1129. }
  1130. }
  1131. void VisualShaderEditor::_add_texture_node(const String &p_path) {
  1132. VisualShaderNodeTexture *texture = (VisualShaderNodeTexture *)_add_node(texture_node_option_idx, -1);
  1133. texture->set_texture(ResourceLoader::load(p_path));
  1134. }
  1135. VisualShaderNode *VisualShaderEditor::_add_node(int p_idx, int p_op_idx) {
  1136. ERR_FAIL_INDEX_V(p_idx, add_options.size(), nullptr);
  1137. Ref<VisualShaderNode> vsnode;
  1138. bool is_custom = add_options[p_idx].is_custom;
  1139. if (!is_custom && add_options[p_idx].type != String()) {
  1140. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(add_options[p_idx].type));
  1141. ERR_FAIL_COND_V(!vsn, nullptr);
  1142. VisualShaderNodeScalarConstant *constant = Object::cast_to<VisualShaderNodeScalarConstant>(vsn);
  1143. if (constant) {
  1144. if ((int)add_options[p_idx].value != -1) {
  1145. constant->set_constant(add_options[p_idx].value);
  1146. }
  1147. }
  1148. if (p_op_idx != -1) {
  1149. VisualShaderNodeInput *input = Object::cast_to<VisualShaderNodeInput>(vsn);
  1150. if (input) {
  1151. input->set_input_name(add_options[p_idx].sub_func_str);
  1152. }
  1153. VisualShaderNodeIs *is = Object::cast_to<VisualShaderNodeIs>(vsn);
  1154. if (is) {
  1155. is->set_function((VisualShaderNodeIs::Function)p_op_idx);
  1156. }
  1157. VisualShaderNodeCompare *cmp = Object::cast_to<VisualShaderNodeCompare>(vsn);
  1158. if (cmp) {
  1159. cmp->set_function((VisualShaderNodeCompare::Function)p_op_idx);
  1160. }
  1161. VisualShaderNodeColorOp *colorOp = Object::cast_to<VisualShaderNodeColorOp>(vsn);
  1162. if (colorOp) {
  1163. colorOp->set_operator((VisualShaderNodeColorOp::Operator)p_op_idx);
  1164. }
  1165. VisualShaderNodeColorFunc *colorFunc = Object::cast_to<VisualShaderNodeColorFunc>(vsn);
  1166. if (colorFunc) {
  1167. colorFunc->set_function((VisualShaderNodeColorFunc::Function)p_op_idx);
  1168. }
  1169. VisualShaderNodeScalarOp *scalarOp = Object::cast_to<VisualShaderNodeScalarOp>(vsn);
  1170. if (scalarOp) {
  1171. scalarOp->set_operator((VisualShaderNodeScalarOp::Operator)p_op_idx);
  1172. }
  1173. VisualShaderNodeScalarFunc *scalarFunc = Object::cast_to<VisualShaderNodeScalarFunc>(vsn);
  1174. if (scalarFunc) {
  1175. scalarFunc->set_function((VisualShaderNodeScalarFunc::Function)p_op_idx);
  1176. }
  1177. VisualShaderNodeVectorOp *vecOp = Object::cast_to<VisualShaderNodeVectorOp>(vsn);
  1178. if (vecOp) {
  1179. vecOp->set_operator((VisualShaderNodeVectorOp::Operator)p_op_idx);
  1180. }
  1181. VisualShaderNodeVectorFunc *vecFunc = Object::cast_to<VisualShaderNodeVectorFunc>(vsn);
  1182. if (vecFunc) {
  1183. vecFunc->set_function((VisualShaderNodeVectorFunc::Function)p_op_idx);
  1184. }
  1185. VisualShaderNodeTransformFunc *matFunc = Object::cast_to<VisualShaderNodeTransformFunc>(vsn);
  1186. if (matFunc) {
  1187. matFunc->set_function((VisualShaderNodeTransformFunc::Function)p_op_idx);
  1188. }
  1189. VisualShaderNodeScalarDerivativeFunc *sderFunc = Object::cast_to<VisualShaderNodeScalarDerivativeFunc>(vsn);
  1190. if (sderFunc) {
  1191. sderFunc->set_function((VisualShaderNodeScalarDerivativeFunc::Function)p_op_idx);
  1192. }
  1193. VisualShaderNodeVectorDerivativeFunc *vderFunc = Object::cast_to<VisualShaderNodeVectorDerivativeFunc>(vsn);
  1194. if (vderFunc) {
  1195. vderFunc->set_function((VisualShaderNodeVectorDerivativeFunc::Function)p_op_idx);
  1196. }
  1197. }
  1198. vsnode = Ref<VisualShaderNode>(vsn);
  1199. } else {
  1200. ERR_FAIL_COND_V(add_options[p_idx].script.is_null(), nullptr);
  1201. String base_type = add_options[p_idx].script->get_instance_base_type();
  1202. VisualShaderNode *vsn = Object::cast_to<VisualShaderNode>(ClassDB::instance(base_type));
  1203. ERR_FAIL_COND_V(!vsn, nullptr);
  1204. vsnode = Ref<VisualShaderNode>(vsn);
  1205. vsnode->set_script(add_options[p_idx].script.get_ref_ptr());
  1206. }
  1207. Point2 position = graph->get_scroll_ofs();
  1208. if (saved_node_pos_dirty) {
  1209. position += saved_node_pos;
  1210. } else {
  1211. position += graph->get_size() * 0.5;
  1212. position /= EDSCALE;
  1213. }
  1214. position /= graph->get_zoom();
  1215. saved_node_pos_dirty = false;
  1216. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1217. int id_to_use = visual_shader->get_valid_node_id(type);
  1218. undo_redo->create_action(TTR("Add Node to Visual Shader"));
  1219. undo_redo->add_do_method(visual_shader.ptr(), "add_node", type, vsnode, position, id_to_use);
  1220. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_to_use);
  1221. VisualShaderNodeExpression *expr = Object::cast_to<VisualShaderNodeExpression>(vsnode.ptr());
  1222. if (expr) {
  1223. undo_redo->add_do_method(expr, "set_size", Size2(250 * EDSCALE, 150 * EDSCALE));
  1224. }
  1225. if (to_node != -1 && to_slot != -1) {
  1226. if (vsnode->get_output_port_count() > 0) {
  1227. int _from_node = id_to_use;
  1228. int _from_slot = 0;
  1229. 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))) {
  1230. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1231. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, _from_node, _from_slot, to_node, to_slot);
  1232. }
  1233. }
  1234. } else if (from_node != -1 && from_slot != -1) {
  1235. if (vsnode->get_input_port_count() > 0) {
  1236. int _to_node = id_to_use;
  1237. int _to_slot = 0;
  1238. 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))) {
  1239. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1240. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from_node, from_slot, _to_node, _to_slot);
  1241. }
  1242. }
  1243. }
  1244. undo_redo->add_do_method(this, "_update_graph");
  1245. undo_redo->add_undo_method(this, "_update_graph");
  1246. undo_redo->commit_action();
  1247. return vsnode.ptr();
  1248. }
  1249. void VisualShaderEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node) {
  1250. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1251. drag_buffer.push_back({ type, p_node, p_from, p_to });
  1252. if (!drag_dirty) {
  1253. call_deferred("_nodes_dragged");
  1254. }
  1255. drag_dirty = true;
  1256. }
  1257. void VisualShaderEditor::_nodes_dragged() {
  1258. drag_dirty = false;
  1259. undo_redo->create_action(TTR("Node(s) Moved"));
  1260. for (List<DragOp>::Element *E = drag_buffer.front(); E; E = E->next()) {
  1261. undo_redo->add_do_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().to);
  1262. undo_redo->add_undo_method(visual_shader.ptr(), "set_node_position", E->get().type, E->get().node, E->get().from);
  1263. }
  1264. updating = true;
  1265. undo_redo->add_do_method(this, "_update_graph");
  1266. undo_redo->add_undo_method(this, "_update_graph");
  1267. drag_buffer.clear();
  1268. undo_redo->commit_action();
  1269. updating = false;
  1270. }
  1271. void VisualShaderEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1272. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1273. int from = p_from.to_int();
  1274. int to = p_to.to_int();
  1275. if (!visual_shader->can_connect_nodes(type, from, p_from_index, to, p_to_index)) {
  1276. return;
  1277. }
  1278. undo_redo->create_action(TTR("Nodes Connected"));
  1279. List<VisualShader::Connection> conns;
  1280. visual_shader->get_node_connections(type, &conns);
  1281. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1282. if (E->get().to_node == to && E->get().to_port == p_to_index) {
  1283. 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);
  1284. 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);
  1285. }
  1286. }
  1287. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1288. undo_redo->add_undo_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1289. undo_redo->add_do_method(this, "_update_graph");
  1290. undo_redo->add_undo_method(this, "_update_graph");
  1291. undo_redo->commit_action();
  1292. }
  1293. void VisualShaderEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
  1294. graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
  1295. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1296. int from = p_from.to_int();
  1297. int to = p_to.to_int();
  1298. //updating = true; seems graph edit can handle this, no need to protect
  1299. undo_redo->create_action(TTR("Nodes Disconnected"));
  1300. undo_redo->add_do_method(visual_shader.ptr(), "disconnect_nodes", type, from, p_from_index, to, p_to_index);
  1301. undo_redo->add_undo_method(visual_shader.ptr(), "connect_nodes", type, from, p_from_index, to, p_to_index);
  1302. undo_redo->add_do_method(this, "_update_graph");
  1303. undo_redo->add_undo_method(this, "_update_graph");
  1304. undo_redo->commit_action();
  1305. //updating = false;
  1306. }
  1307. void VisualShaderEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
  1308. from_node = p_from.to_int();
  1309. from_slot = p_from_slot;
  1310. _show_members_dialog(true);
  1311. }
  1312. void VisualShaderEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
  1313. to_node = p_to.to_int();
  1314. to_slot = p_to_slot;
  1315. _show_members_dialog(true);
  1316. }
  1317. void VisualShaderEditor::_delete_request(int which) {
  1318. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1319. Ref<VisualShaderNode> node = Ref<VisualShaderNode>(visual_shader->get_node(type, which));
  1320. undo_redo->create_action(TTR("Delete Node"));
  1321. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, which);
  1322. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, which), which);
  1323. undo_redo->add_do_method(this, "_clear_buffer");
  1324. undo_redo->add_undo_method(this, "_clear_buffer");
  1325. // restore size, inputs and outputs if node is group
  1326. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1327. if (group) {
  1328. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1329. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1330. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1331. }
  1332. // restore expression text if node is expression
  1333. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1334. if (expression) {
  1335. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1336. }
  1337. List<VisualShader::Connection> conns;
  1338. visual_shader->get_node_connections(type, &conns);
  1339. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1340. if (E->get().from_node == which || E->get().to_node == which) {
  1341. 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);
  1342. }
  1343. }
  1344. undo_redo->add_do_method(this, "_update_graph");
  1345. undo_redo->add_undo_method(this, "_update_graph");
  1346. undo_redo->commit_action();
  1347. }
  1348. void VisualShaderEditor::_node_selected(Object *p_node) {
  1349. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1350. GraphNode *gn = Object::cast_to<GraphNode>(p_node);
  1351. ERR_FAIL_COND(!gn);
  1352. int id = String(gn->get_name()).to_int();
  1353. Ref<VisualShaderNode> vsnode = visual_shader->get_node(type, id);
  1354. ERR_FAIL_COND(!vsnode.is_valid());
  1355. //do not rely on this, makes editor more complex
  1356. //EditorNode::get_singleton()->push_item(vsnode.ptr(), "", true);
  1357. }
  1358. void VisualShaderEditor::_graph_gui_input(const Ref<InputEvent> &p_event) {
  1359. Ref<InputEventMouseButton> mb = p_event;
  1360. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1361. _show_members_dialog(true);
  1362. }
  1363. }
  1364. void VisualShaderEditor::_show_members_dialog(bool at_mouse_pos) {
  1365. if (at_mouse_pos) {
  1366. saved_node_pos_dirty = true;
  1367. saved_node_pos = graph->get_local_mouse_position();
  1368. Point2 gpos = Input::get_singleton()->get_mouse_position();
  1369. members_dialog->popup();
  1370. members_dialog->set_position(gpos);
  1371. } else {
  1372. members_dialog->popup();
  1373. saved_node_pos_dirty = false;
  1374. members_dialog->set_position(graph->get_global_position() + Point2(5 * EDSCALE, 65 * EDSCALE));
  1375. }
  1376. // keep dialog within window bounds
  1377. Size2 window_size = OS::get_singleton()->get_window_size();
  1378. Rect2 dialog_rect = members_dialog->get_global_rect();
  1379. if (dialog_rect.position.y + dialog_rect.size.y > window_size.y) {
  1380. int difference = dialog_rect.position.y + dialog_rect.size.y - window_size.y;
  1381. members_dialog->set_position(members_dialog->get_position() - Point2(0, difference));
  1382. }
  1383. if (dialog_rect.position.x + dialog_rect.size.x > window_size.x) {
  1384. int difference = dialog_rect.position.x + dialog_rect.size.x - window_size.x;
  1385. members_dialog->set_position(members_dialog->get_position() - Point2(difference, 0));
  1386. }
  1387. node_filter->call_deferred("grab_focus"); // still not visible
  1388. node_filter->select_all();
  1389. }
  1390. void VisualShaderEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  1391. Ref<InputEventKey> ie = p_ie;
  1392. if (ie.is_valid() && (ie->get_scancode() == KEY_UP || ie->get_scancode() == KEY_DOWN || ie->get_scancode() == KEY_ENTER || ie->get_scancode() == KEY_KP_ENTER)) {
  1393. members->call("_gui_input", ie);
  1394. node_filter->accept_event();
  1395. }
  1396. }
  1397. void VisualShaderEditor::_notification(int p_what) {
  1398. if (p_what == NOTIFICATION_ENTER_TREE) {
  1399. node_filter->set_clear_button_enabled(true);
  1400. // collapse tree by default
  1401. TreeItem *category = members->get_root()->get_children();
  1402. while (category) {
  1403. category->set_collapsed(true);
  1404. TreeItem *sub_category = category->get_children();
  1405. while (sub_category) {
  1406. sub_category->set_collapsed(true);
  1407. sub_category = sub_category->get_next();
  1408. }
  1409. category = category->get_next();
  1410. }
  1411. }
  1412. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  1413. Dictionary dd = get_viewport()->gui_get_drag_data();
  1414. if (members->is_visible_in_tree() && dd.has("id")) {
  1415. members->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
  1416. }
  1417. } else if (p_what == NOTIFICATION_DRAG_END) {
  1418. members->set_drop_mode_flags(0);
  1419. }
  1420. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  1421. error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  1422. error_label->add_color_override("font_color", get_color("error_color", "Editor"));
  1423. node_filter->set_right_icon(Control::get_icon("Search", "EditorIcons"));
  1424. preview_shader->set_icon(Control::get_icon("Shader", "EditorIcons"));
  1425. {
  1426. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  1427. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  1428. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  1429. Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
  1430. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  1431. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  1432. preview_text->add_color_override("background_color", background_color);
  1433. for (List<String>::Element *E = keyword_list.front(); E; E = E->next()) {
  1434. if (ShaderLanguage::is_control_flow_keyword(E->get())) {
  1435. preview_text->add_keyword_color(E->get(), control_flow_keyword_color);
  1436. } else {
  1437. preview_text->add_keyword_color(E->get(), keyword_color);
  1438. }
  1439. }
  1440. preview_text->add_font_override("font", get_font("expression", "EditorFonts"));
  1441. preview_text->add_color_override("font_color", text_color);
  1442. preview_text->add_color_override("symbol_color", symbol_color);
  1443. preview_text->add_color_region("/*", "*/", comment_color, false);
  1444. preview_text->add_color_region("//", "", comment_color, false);
  1445. error_text->add_font_override("font", get_font("status_source", "EditorFonts"));
  1446. error_text->add_color_override("font_color", get_color("error_color", "Editor"));
  1447. }
  1448. tools->set_icon(EditorNode::get_singleton()->get_gui_base()->get_icon("Tools", "EditorIcons"));
  1449. if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree()) {
  1450. _update_graph();
  1451. }
  1452. }
  1453. }
  1454. void VisualShaderEditor::_scroll_changed(const Vector2 &p_scroll) {
  1455. if (updating) {
  1456. return;
  1457. }
  1458. updating = true;
  1459. visual_shader->set_graph_offset(p_scroll / EDSCALE);
  1460. updating = false;
  1461. }
  1462. void VisualShaderEditor::_node_changed(int p_id) {
  1463. if (updating) {
  1464. return;
  1465. }
  1466. if (is_visible_in_tree()) {
  1467. _update_graph();
  1468. }
  1469. }
  1470. void VisualShaderEditor::_dup_update_excluded(int p_type, Set<int> &r_excluded) {
  1471. r_excluded.clear();
  1472. VisualShader::Type type = (VisualShader::Type)p_type;
  1473. for (int i = 0; i < graph->get_child_count(); i++) {
  1474. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1475. if (gn) {
  1476. int id = String(gn->get_name()).to_int();
  1477. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1478. Ref<VisualShaderNodeOutput> output = node;
  1479. if (output.is_valid()) {
  1480. r_excluded.insert(id);
  1481. continue;
  1482. }
  1483. r_excluded.insert(id);
  1484. }
  1485. }
  1486. }
  1487. void VisualShaderEditor::_dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded) {
  1488. VisualShader::Type type = (VisualShader::Type)p_type;
  1489. selection_center.x = 0.0f;
  1490. selection_center.y = 0.0f;
  1491. for (int i = 0; i < graph->get_child_count(); i++) {
  1492. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1493. if (gn) {
  1494. int id = String(gn->get_name()).to_int();
  1495. Ref<VisualShaderNode> node = visual_shader->get_node(type, id);
  1496. Ref<VisualShaderNodeOutput> output = node;
  1497. if (output.is_valid()) { // can't duplicate output
  1498. r_excluded.insert(id);
  1499. continue;
  1500. }
  1501. if (node.is_valid() && gn->is_selected()) {
  1502. Vector2 pos = visual_shader->get_node_position(type, id);
  1503. selection_center += pos;
  1504. r_nodes.push_back(id);
  1505. }
  1506. r_excluded.insert(id);
  1507. }
  1508. }
  1509. selection_center /= (float)r_nodes.size();
  1510. }
  1511. 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) {
  1512. VisualShader::Type type = (VisualShader::Type)p_type;
  1513. VisualShader::Type pasted_type = (VisualShader::Type)p_pasted_type;
  1514. int base_id = visual_shader->get_valid_node_id(type);
  1515. int id_from = base_id;
  1516. Map<int, int> connection_remap;
  1517. Set<int> unsupported_set;
  1518. for (List<int>::Element *E = r_nodes.front(); E; E = E->next()) {
  1519. connection_remap[E->get()] = id_from;
  1520. Ref<VisualShaderNode> node = visual_shader->get_node(pasted_type, E->get());
  1521. bool unsupported = false;
  1522. for (int i = 0; i < add_options.size(); i++) {
  1523. if (add_options[i].type == node->get_class_name()) {
  1524. if (!_is_available(add_options[i].mode)) {
  1525. unsupported = true;
  1526. }
  1527. break;
  1528. }
  1529. }
  1530. if (unsupported) {
  1531. unsupported_set.insert(E->get());
  1532. continue;
  1533. }
  1534. Ref<VisualShaderNode> dupli = node->duplicate();
  1535. 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);
  1536. undo_redo->add_undo_method(visual_shader.ptr(), "remove_node", type, id_from);
  1537. // duplicate size, inputs and outputs if node is group
  1538. Ref<VisualShaderNodeGroupBase> group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1539. if (!group.is_null()) {
  1540. undo_redo->add_do_method(dupli.ptr(), "set_size", group->get_size());
  1541. undo_redo->add_do_method(dupli.ptr(), "set_inputs", group->get_inputs());
  1542. undo_redo->add_do_method(dupli.ptr(), "set_outputs", group->get_outputs());
  1543. }
  1544. // duplicate expression text if node is expression
  1545. Ref<VisualShaderNodeExpression> expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1546. if (!expression.is_null()) {
  1547. undo_redo->add_do_method(dupli.ptr(), "set_expression", expression->get_expression());
  1548. }
  1549. id_from++;
  1550. }
  1551. List<VisualShader::Connection> conns;
  1552. visual_shader->get_node_connections(pasted_type, &conns);
  1553. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1554. if (unsupported_set.has(E->get().from_node) || unsupported_set.has(E->get().to_node)) {
  1555. continue;
  1556. }
  1557. if (connection_remap.has(E->get().from_node) && connection_remap.has(E->get().to_node)) {
  1558. undo_redo->add_do_method(visual_shader.ptr(), "connect_nodes_forced", type, connection_remap[E->get().from_node], E->get().from_port, connection_remap[E->get().to_node], E->get().to_port);
  1559. }
  1560. }
  1561. undo_redo->add_do_method(this, "_update_graph");
  1562. undo_redo->add_undo_method(this, "_update_graph");
  1563. undo_redo->commit_action();
  1564. if (p_select) {
  1565. // reselect duplicated nodes by excluding the other ones
  1566. for (int i = 0; i < graph->get_child_count(); i++) {
  1567. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1568. if (gn) {
  1569. int id = String(gn->get_name()).to_int();
  1570. if (!r_excluded.has(id)) {
  1571. gn->set_selected(true);
  1572. } else {
  1573. gn->set_selected(false);
  1574. }
  1575. }
  1576. }
  1577. }
  1578. }
  1579. void VisualShaderEditor::_clear_buffer() {
  1580. copy_nodes_buffer.clear();
  1581. copy_nodes_excluded_buffer.clear();
  1582. }
  1583. void VisualShaderEditor::_duplicate_nodes() {
  1584. int type = edit_type->get_selected();
  1585. List<int> nodes;
  1586. Set<int> excluded;
  1587. _dup_copy_nodes(type, nodes, excluded);
  1588. if (nodes.empty()) {
  1589. return;
  1590. }
  1591. undo_redo->create_action(TTR("Duplicate Nodes"));
  1592. _dup_paste_nodes(type, type, nodes, excluded, Vector2(10, 10) * EDSCALE, true);
  1593. }
  1594. void VisualShaderEditor::_copy_nodes() {
  1595. copy_type = edit_type->get_selected();
  1596. _clear_buffer();
  1597. _dup_copy_nodes(copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer);
  1598. }
  1599. void VisualShaderEditor::_paste_nodes() {
  1600. if (copy_nodes_buffer.empty()) {
  1601. return;
  1602. }
  1603. int type = edit_type->get_selected();
  1604. undo_redo->create_action(TTR("Paste Nodes"));
  1605. float scale = graph->get_zoom();
  1606. _dup_paste_nodes(type, copy_type, copy_nodes_buffer, copy_nodes_excluded_buffer, (graph->get_scroll_ofs() / scale + graph->get_local_mouse_position() / scale - selection_center), false);
  1607. _dup_update_excluded(type, copy_nodes_excluded_buffer); // to prevent selection of previous copies at new paste
  1608. }
  1609. void VisualShaderEditor::_on_nodes_delete(const Array &p_nodes) {
  1610. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1611. List<int> to_erase;
  1612. if (p_nodes.empty()) {
  1613. for (int i = 0; i < graph->get_child_count(); i++) {
  1614. GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
  1615. if (gn) {
  1616. if (gn->is_selected() && gn->is_close_button_visible()) {
  1617. to_erase.push_back(gn->get_name().operator String().to_int());
  1618. }
  1619. }
  1620. }
  1621. } else {
  1622. for (int i = 0; i < p_nodes.size(); i++) {
  1623. to_erase.push_back(p_nodes[i].operator String().to_int());
  1624. }
  1625. }
  1626. if (to_erase.empty()) {
  1627. return;
  1628. }
  1629. undo_redo->create_action(TTR("Delete Nodes"));
  1630. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1631. Ref<VisualShaderNode> node = visual_shader->get_node(type, F->get());
  1632. undo_redo->add_do_method(visual_shader.ptr(), "remove_node", type, F->get());
  1633. undo_redo->add_undo_method(visual_shader.ptr(), "add_node", type, node, visual_shader->get_node_position(type, F->get()), F->get());
  1634. undo_redo->add_do_method(this, "_clear_buffer");
  1635. undo_redo->add_undo_method(this, "_clear_buffer");
  1636. // restore size, inputs and outputs if node is group
  1637. VisualShaderNodeGroupBase *group = Object::cast_to<VisualShaderNodeGroupBase>(node.ptr());
  1638. if (group) {
  1639. undo_redo->add_undo_method(group, "set_size", group->get_size());
  1640. undo_redo->add_undo_method(group, "set_inputs", group->get_inputs());
  1641. undo_redo->add_undo_method(group, "set_outputs", group->get_outputs());
  1642. }
  1643. // restore expression text if node is expression
  1644. VisualShaderNodeExpression *expression = Object::cast_to<VisualShaderNodeExpression>(node.ptr());
  1645. if (expression) {
  1646. undo_redo->add_undo_method(expression, "set_expression", expression->get_expression());
  1647. }
  1648. }
  1649. List<VisualShader::Connection> conns;
  1650. visual_shader->get_node_connections(type, &conns);
  1651. List<VisualShader::Connection> used_conns;
  1652. for (List<int>::Element *F = to_erase.front(); F; F = F->next()) {
  1653. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1654. if (E->get().from_node == F->get() || E->get().to_node == F->get()) {
  1655. bool cancel = false;
  1656. for (List<VisualShader::Connection>::Element *R = used_conns.front(); R; R = R->next()) {
  1657. 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) {
  1658. cancel = true; // to avoid ERR_ALREADY_EXISTS warning
  1659. break;
  1660. }
  1661. }
  1662. if (!cancel) {
  1663. 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);
  1664. used_conns.push_back(E->get());
  1665. }
  1666. }
  1667. }
  1668. }
  1669. undo_redo->add_do_method(this, "_update_graph");
  1670. undo_redo->add_undo_method(this, "_update_graph");
  1671. undo_redo->commit_action();
  1672. }
  1673. void VisualShaderEditor::_mode_selected(int p_id) {
  1674. _update_options_menu();
  1675. _update_graph();
  1676. }
  1677. void VisualShaderEditor::_input_select_item(Ref<VisualShaderNodeInput> input, String name) {
  1678. String prev_name = input->get_input_name();
  1679. if (name == prev_name) {
  1680. return;
  1681. }
  1682. bool type_changed = input->get_input_type_by_name(name) != input->get_input_type_by_name(prev_name);
  1683. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1684. undo_redo->create_action(TTR("Visual Shader Input Type Changed"));
  1685. undo_redo->add_do_method(input.ptr(), "set_input_name", name);
  1686. undo_redo->add_undo_method(input.ptr(), "set_input_name", prev_name);
  1687. if (type_changed) {
  1688. //restore connections if type changed
  1689. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1690. int id = visual_shader->find_node_id(type, input);
  1691. List<VisualShader::Connection> conns;
  1692. visual_shader->get_node_connections(type, &conns);
  1693. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1694. if (E->get().from_node == id) {
  1695. 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);
  1696. }
  1697. }
  1698. }
  1699. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1700. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1701. undo_redo->commit_action();
  1702. }
  1703. void VisualShaderEditor::_uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform_ref, String p_name) {
  1704. String prev_name = p_uniform_ref->get_uniform_name();
  1705. if (p_name == prev_name) {
  1706. return;
  1707. }
  1708. bool type_changed = p_uniform_ref->get_uniform_type_by_name(p_name) != p_uniform_ref->get_uniform_type_by_name(prev_name);
  1709. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  1710. undo_redo->create_action(TTR("UniformRef Name Changed"));
  1711. undo_redo->add_do_method(p_uniform_ref.ptr(), "set_uniform_name", p_name);
  1712. undo_redo->add_undo_method(p_uniform_ref.ptr(), "set_uniform_name", prev_name);
  1713. if (type_changed) {
  1714. //restore connections if type changed
  1715. VisualShader::Type type = VisualShader::Type(edit_type->get_selected());
  1716. int id = visual_shader->find_node_id(type, p_uniform_ref);
  1717. List<VisualShader::Connection> conns;
  1718. visual_shader->get_node_connections(type, &conns);
  1719. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  1720. if (E->get().from_node == id) {
  1721. 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);
  1722. 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);
  1723. }
  1724. }
  1725. }
  1726. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1727. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  1728. undo_redo->commit_action();
  1729. }
  1730. void VisualShaderEditor::_member_filter_changed(const String &p_text) {
  1731. _update_options_menu();
  1732. }
  1733. void VisualShaderEditor::_member_selected() {
  1734. TreeItem *item = members->get_selected();
  1735. if (item != nullptr && item->has_meta("id")) {
  1736. members_dialog->get_ok()->set_disabled(false);
  1737. node_desc->set_text(_get_description(item->get_meta("id")));
  1738. } else {
  1739. members_dialog->get_ok()->set_disabled(true);
  1740. node_desc->set_text("");
  1741. }
  1742. }
  1743. void VisualShaderEditor::_member_unselected() {
  1744. }
  1745. void VisualShaderEditor::_member_create() {
  1746. TreeItem *item = members->get_selected();
  1747. if (item != nullptr && item->has_meta("id")) {
  1748. int idx = members->get_selected()->get_meta("id");
  1749. _add_node(idx, add_options[idx].sub_func);
  1750. members_dialog->hide();
  1751. }
  1752. }
  1753. void VisualShaderEditor::_member_cancel() {
  1754. to_node = -1;
  1755. to_slot = -1;
  1756. from_node = -1;
  1757. from_slot = -1;
  1758. }
  1759. void VisualShaderEditor::_tools_menu_option(int p_idx) {
  1760. TreeItem *category = members->get_root()->get_children();
  1761. switch (p_idx) {
  1762. case EXPAND_ALL:
  1763. while (category) {
  1764. category->set_collapsed(false);
  1765. TreeItem *sub_category = category->get_children();
  1766. while (sub_category) {
  1767. sub_category->set_collapsed(false);
  1768. sub_category = sub_category->get_next();
  1769. }
  1770. category = category->get_next();
  1771. }
  1772. break;
  1773. case COLLAPSE_ALL:
  1774. while (category) {
  1775. category->set_collapsed(true);
  1776. TreeItem *sub_category = category->get_children();
  1777. while (sub_category) {
  1778. sub_category->set_collapsed(true);
  1779. sub_category = sub_category->get_next();
  1780. }
  1781. category = category->get_next();
  1782. }
  1783. break;
  1784. default:
  1785. break;
  1786. }
  1787. }
  1788. Variant VisualShaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1789. if (p_from == members) {
  1790. TreeItem *it = members->get_item_at_position(p_point);
  1791. if (!it) {
  1792. return Variant();
  1793. }
  1794. if (!it->has_meta("id")) {
  1795. return Variant();
  1796. }
  1797. int id = it->get_meta("id");
  1798. AddOption op = add_options[id];
  1799. Dictionary d;
  1800. d["id"] = id;
  1801. if (op.sub_func == -1) {
  1802. d["sub_func"] = op.sub_func_str;
  1803. } else {
  1804. d["sub_func"] = op.sub_func;
  1805. }
  1806. Label *label = memnew(Label);
  1807. label->set_text(it->get_text(0));
  1808. set_drag_preview(label);
  1809. return d;
  1810. }
  1811. return Variant();
  1812. }
  1813. bool VisualShaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1814. if (p_from == graph) {
  1815. Dictionary d = p_data;
  1816. if (d.has("id")) {
  1817. return true;
  1818. }
  1819. if (d.has("files")) {
  1820. return true;
  1821. }
  1822. }
  1823. return false;
  1824. }
  1825. void VisualShaderEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1826. if (p_from == graph) {
  1827. Dictionary d = p_data;
  1828. if (d.has("id")) {
  1829. int idx = d["id"];
  1830. saved_node_pos = p_point;
  1831. saved_node_pos_dirty = true;
  1832. _add_node(idx, add_options[idx].sub_func);
  1833. } else if (d.has("files")) {
  1834. if (d["files"].get_type() == Variant::POOL_STRING_ARRAY) {
  1835. int j = 0;
  1836. PoolStringArray arr = d["files"];
  1837. for (int i = 0; i < arr.size(); i++) {
  1838. String type = ResourceLoader::get_resource_type(arr[i]);
  1839. if (type == "GDScript") {
  1840. Ref<Script> script = ResourceLoader::load(arr[i]);
  1841. if (script->get_instance_base_type() == "VisualShaderNodeCustom") {
  1842. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1843. saved_node_pos_dirty = true;
  1844. _add_custom_node(arr[i]);
  1845. j++;
  1846. }
  1847. } else if (ClassDB::get_parent_class(type) == "Texture") {
  1848. saved_node_pos = p_point + Vector2(0, j * 210 * EDSCALE);
  1849. saved_node_pos_dirty = true;
  1850. _add_texture_node(arr[i]);
  1851. j++;
  1852. }
  1853. }
  1854. }
  1855. }
  1856. }
  1857. }
  1858. void VisualShaderEditor::_show_preview_text() {
  1859. preview_showed = !preview_showed;
  1860. preview_vbox->set_visible(preview_showed);
  1861. if (preview_showed) {
  1862. if (pending_update_preview) {
  1863. _update_preview();
  1864. pending_update_preview = false;
  1865. }
  1866. }
  1867. }
  1868. void VisualShaderEditor::_update_preview() {
  1869. if (!preview_showed) {
  1870. pending_update_preview = true;
  1871. return;
  1872. }
  1873. String code = visual_shader->get_code();
  1874. preview_text->set_text(code);
  1875. ShaderLanguage sl;
  1876. Error err = sl.compile(code, ShaderTypes::get_singleton()->get_functions(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(visual_shader->get_mode())), ShaderTypes::get_singleton()->get_types());
  1877. for (int i = 0; i < preview_text->get_line_count(); i++) {
  1878. preview_text->set_line_as_marked(i, false);
  1879. }
  1880. if (err != OK) {
  1881. preview_text->set_line_as_marked(sl.get_error_line() - 1, true);
  1882. error_text->set_visible(true);
  1883. String text = "error(" + itos(sl.get_error_line()) + "): " + sl.get_error_text();
  1884. error_text->set_text(text);
  1885. shader_error = true;
  1886. } else {
  1887. error_text->set_visible(false);
  1888. shader_error = false;
  1889. }
  1890. }
  1891. void VisualShaderEditor::_bind_methods() {
  1892. ClassDB::bind_method("_rebuild", &VisualShaderEditor::_rebuild);
  1893. ClassDB::bind_method("_update_graph", &VisualShaderEditor::_update_graph);
  1894. ClassDB::bind_method("_update_options_menu", &VisualShaderEditor::_update_options_menu);
  1895. ClassDB::bind_method("_expression_focus_out", &VisualShaderEditor::_expression_focus_out);
  1896. ClassDB::bind_method("_add_node", &VisualShaderEditor::_add_node);
  1897. ClassDB::bind_method("_node_dragged", &VisualShaderEditor::_node_dragged);
  1898. ClassDB::bind_method("_connection_request", &VisualShaderEditor::_connection_request);
  1899. ClassDB::bind_method("_disconnection_request", &VisualShaderEditor::_disconnection_request);
  1900. ClassDB::bind_method("_node_selected", &VisualShaderEditor::_node_selected);
  1901. ClassDB::bind_method("_scroll_changed", &VisualShaderEditor::_scroll_changed);
  1902. ClassDB::bind_method("_delete_request", &VisualShaderEditor::_delete_request);
  1903. ClassDB::bind_method("_on_nodes_delete", &VisualShaderEditor::_on_nodes_delete);
  1904. ClassDB::bind_method("_node_changed", &VisualShaderEditor::_node_changed);
  1905. ClassDB::bind_method("_edit_port_default_input", &VisualShaderEditor::_edit_port_default_input);
  1906. ClassDB::bind_method("_port_edited", &VisualShaderEditor::_port_edited);
  1907. ClassDB::bind_method("_connection_to_empty", &VisualShaderEditor::_connection_to_empty);
  1908. ClassDB::bind_method("_connection_from_empty", &VisualShaderEditor::_connection_from_empty);
  1909. ClassDB::bind_method("_line_edit_focus_out", &VisualShaderEditor::_line_edit_focus_out);
  1910. ClassDB::bind_method("_line_edit_changed", &VisualShaderEditor::_line_edit_changed);
  1911. ClassDB::bind_method("_port_name_focus_out", &VisualShaderEditor::_port_name_focus_out);
  1912. ClassDB::bind_method("_duplicate_nodes", &VisualShaderEditor::_duplicate_nodes);
  1913. ClassDB::bind_method("_copy_nodes", &VisualShaderEditor::_copy_nodes);
  1914. ClassDB::bind_method("_paste_nodes", &VisualShaderEditor::_paste_nodes);
  1915. ClassDB::bind_method("_mode_selected", &VisualShaderEditor::_mode_selected);
  1916. ClassDB::bind_method("_input_select_item", &VisualShaderEditor::_input_select_item);
  1917. ClassDB::bind_method("_uniform_select_item", &VisualShaderEditor::_uniform_select_item);
  1918. ClassDB::bind_method("_preview_select_port", &VisualShaderEditor::_preview_select_port);
  1919. ClassDB::bind_method("_graph_gui_input", &VisualShaderEditor::_graph_gui_input);
  1920. ClassDB::bind_method("_add_input_port", &VisualShaderEditor::_add_input_port);
  1921. ClassDB::bind_method("_change_input_port_type", &VisualShaderEditor::_change_input_port_type);
  1922. ClassDB::bind_method("_change_input_port_name", &VisualShaderEditor::_change_input_port_name);
  1923. ClassDB::bind_method("_remove_input_port", &VisualShaderEditor::_remove_input_port);
  1924. ClassDB::bind_method("_add_output_port", &VisualShaderEditor::_add_output_port);
  1925. ClassDB::bind_method("_change_output_port_type", &VisualShaderEditor::_change_output_port_type);
  1926. ClassDB::bind_method("_change_output_port_name", &VisualShaderEditor::_change_output_port_name);
  1927. ClassDB::bind_method("_remove_output_port", &VisualShaderEditor::_remove_output_port);
  1928. ClassDB::bind_method("_node_resized", &VisualShaderEditor::_node_resized);
  1929. ClassDB::bind_method("_set_node_size", &VisualShaderEditor::_set_node_size);
  1930. ClassDB::bind_method("_clear_buffer", &VisualShaderEditor::_clear_buffer);
  1931. ClassDB::bind_method("_show_preview_text", &VisualShaderEditor::_show_preview_text);
  1932. ClassDB::bind_method("_update_preview", &VisualShaderEditor::_update_preview);
  1933. ClassDB::bind_method("_nodes_dragged", &VisualShaderEditor::_nodes_dragged);
  1934. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &VisualShaderEditor::get_drag_data_fw);
  1935. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &VisualShaderEditor::can_drop_data_fw);
  1936. ClassDB::bind_method(D_METHOD("drop_data_fw"), &VisualShaderEditor::drop_data_fw);
  1937. ClassDB::bind_method("_is_available", &VisualShaderEditor::_is_available);
  1938. ClassDB::bind_method("_tools_menu_option", &VisualShaderEditor::_tools_menu_option);
  1939. ClassDB::bind_method("_show_members_dialog", &VisualShaderEditor::_show_members_dialog);
  1940. ClassDB::bind_method("_sbox_input", &VisualShaderEditor::_sbox_input);
  1941. ClassDB::bind_method("_member_filter_changed", &VisualShaderEditor::_member_filter_changed);
  1942. ClassDB::bind_method("_member_selected", &VisualShaderEditor::_member_selected);
  1943. ClassDB::bind_method("_member_unselected", &VisualShaderEditor::_member_unselected);
  1944. ClassDB::bind_method("_member_create", &VisualShaderEditor::_member_create);
  1945. ClassDB::bind_method("_member_cancel", &VisualShaderEditor::_member_cancel);
  1946. }
  1947. VisualShaderEditor *VisualShaderEditor::singleton = nullptr;
  1948. VisualShaderEditor::VisualShaderEditor() {
  1949. singleton = this;
  1950. updating = false;
  1951. saved_node_pos_dirty = false;
  1952. saved_node_pos = Point2(0, 0);
  1953. ShaderLanguage::get_keyword_list(&keyword_list);
  1954. preview_showed = false;
  1955. pending_update_preview = false;
  1956. shader_error = false;
  1957. to_node = -1;
  1958. to_slot = -1;
  1959. from_node = -1;
  1960. from_slot = -1;
  1961. main_box = memnew(HSplitContainer);
  1962. main_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1963. main_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1964. add_child(main_box);
  1965. graph = memnew(GraphEdit);
  1966. graph->get_zoom_hbox()->set_h_size_flags(SIZE_EXPAND_FILL);
  1967. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1968. graph->set_h_size_flags(SIZE_EXPAND_FILL);
  1969. main_box->add_child(graph);
  1970. graph->set_drag_forwarding(this);
  1971. float graph_minimap_opacity = EditorSettings::get_singleton()->get("editors/visual_editors/minimap_opacity");
  1972. graph->set_minimap_opacity(graph_minimap_opacity);
  1973. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SCALAR);
  1974. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_BOOLEAN);
  1975. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_VECTOR);
  1976. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_TRANSFORM);
  1977. graph->add_valid_right_disconnect_type(VisualShaderNode::PORT_TYPE_SAMPLER);
  1978. //graph->add_valid_left_disconnect_type(0);
  1979. graph->set_v_size_flags(SIZE_EXPAND_FILL);
  1980. graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
  1981. graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
  1982. graph->connect("node_selected", this, "_node_selected");
  1983. graph->connect("scroll_offset_changed", this, "_scroll_changed");
  1984. graph->connect("duplicate_nodes_request", this, "_duplicate_nodes");
  1985. graph->connect("copy_nodes_request", this, "_copy_nodes");
  1986. graph->connect("paste_nodes_request", this, "_paste_nodes");
  1987. graph->connect("delete_nodes_request", this, "_on_nodes_delete");
  1988. graph->connect("gui_input", this, "_graph_gui_input");
  1989. graph->connect("connection_to_empty", this, "_connection_to_empty");
  1990. graph->connect("connection_from_empty", this, "_connection_from_empty");
  1991. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_SCALAR);
  1992. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_VECTOR);
  1993. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SCALAR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1994. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_SCALAR);
  1995. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_VECTOR);
  1996. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_VECTOR, VisualShaderNode::PORT_TYPE_BOOLEAN);
  1997. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_SCALAR);
  1998. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_VECTOR);
  1999. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShaderNode::PORT_TYPE_BOOLEAN);
  2000. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShaderNode::PORT_TYPE_TRANSFORM);
  2001. graph->add_valid_connection_type(VisualShaderNode::PORT_TYPE_SAMPLER, VisualShaderNode::PORT_TYPE_SAMPLER);
  2002. VSeparator *vs = memnew(VSeparator);
  2003. graph->get_zoom_hbox()->add_child(vs);
  2004. graph->get_zoom_hbox()->move_child(vs, 0);
  2005. edit_type = memnew(OptionButton);
  2006. edit_type->add_item(TTR("Vertex"));
  2007. edit_type->add_item(TTR("Fragment"));
  2008. edit_type->add_item(TTR("Light"));
  2009. edit_type->select(1);
  2010. edit_type->connect("item_selected", this, "_mode_selected");
  2011. graph->get_zoom_hbox()->add_child(edit_type);
  2012. graph->get_zoom_hbox()->move_child(edit_type, 0);
  2013. add_node = memnew(ToolButton);
  2014. graph->get_zoom_hbox()->add_child(add_node);
  2015. add_node->set_text(TTR("Add Node..."));
  2016. graph->get_zoom_hbox()->move_child(add_node, 0);
  2017. add_node->connect("pressed", this, "_show_members_dialog", varray(false));
  2018. preview_shader = memnew(ToolButton);
  2019. preview_shader->set_toggle_mode(true);
  2020. preview_shader->set_tooltip(TTR("Show resulted shader code."));
  2021. graph->get_zoom_hbox()->add_child(preview_shader);
  2022. preview_shader->connect("pressed", this, "_show_preview_text");
  2023. ///////////////////////////////////////
  2024. // PREVIEW PANEL
  2025. ///////////////////////////////////////
  2026. preview_vbox = memnew(VBoxContainer);
  2027. preview_vbox->set_visible(preview_showed);
  2028. main_box->add_child(preview_vbox);
  2029. preview_text = memnew(TextEdit);
  2030. preview_vbox->add_child(preview_text);
  2031. preview_text->set_h_size_flags(SIZE_EXPAND_FILL);
  2032. preview_text->set_v_size_flags(SIZE_EXPAND_FILL);
  2033. preview_text->set_custom_minimum_size(Size2(400 * EDSCALE, 0));
  2034. preview_text->set_syntax_coloring(true);
  2035. preview_text->set_show_line_numbers(true);
  2036. preview_text->set_readonly(true);
  2037. error_text = memnew(Label);
  2038. preview_vbox->add_child(error_text);
  2039. error_text->set_visible(false);
  2040. ///////////////////////////////////////
  2041. // SHADER NODES TREE
  2042. ///////////////////////////////////////
  2043. VBoxContainer *members_vb = memnew(VBoxContainer);
  2044. members_vb->set_v_size_flags(SIZE_EXPAND_FILL);
  2045. HBoxContainer *filter_hb = memnew(HBoxContainer);
  2046. members_vb->add_child(filter_hb);
  2047. node_filter = memnew(LineEdit);
  2048. filter_hb->add_child(node_filter);
  2049. node_filter->connect("text_changed", this, "_member_filter_changed");
  2050. node_filter->connect("gui_input", this, "_sbox_input");
  2051. node_filter->set_h_size_flags(SIZE_EXPAND_FILL);
  2052. node_filter->set_placeholder(TTR("Search"));
  2053. tools = memnew(MenuButton);
  2054. filter_hb->add_child(tools);
  2055. tools->set_tooltip(TTR("Options"));
  2056. tools->get_popup()->connect("id_pressed", this, "_tools_menu_option");
  2057. tools->get_popup()->add_item(TTR("Expand All"), EXPAND_ALL);
  2058. tools->get_popup()->add_item(TTR("Collapse All"), COLLAPSE_ALL);
  2059. members = memnew(Tree);
  2060. members_vb->add_child(members);
  2061. members->set_drag_forwarding(this);
  2062. members->set_h_size_flags(SIZE_EXPAND_FILL);
  2063. members->set_v_size_flags(SIZE_EXPAND_FILL);
  2064. members->set_hide_root(true);
  2065. members->set_allow_reselect(true);
  2066. members->set_hide_folding(false);
  2067. members->set_custom_minimum_size(Size2(180 * EDSCALE, 200 * EDSCALE));
  2068. members->connect("item_activated", this, "_member_create");
  2069. members->connect("item_selected", this, "_member_selected");
  2070. members->connect("nothing_selected", this, "_member_unselected");
  2071. Label *desc_label = memnew(Label);
  2072. members_vb->add_child(desc_label);
  2073. desc_label->set_text(TTR("Description:"));
  2074. node_desc = memnew(RichTextLabel);
  2075. members_vb->add_child(node_desc);
  2076. node_desc->set_h_size_flags(SIZE_EXPAND_FILL);
  2077. node_desc->set_v_size_flags(SIZE_FILL);
  2078. node_desc->set_custom_minimum_size(Size2(0, 70 * EDSCALE));
  2079. members_dialog = memnew(ConfirmationDialog);
  2080. members_dialog->set_title(TTR("Create Shader Node"));
  2081. members_dialog->add_child(members_vb);
  2082. members_dialog->get_ok()->set_text(TTR("Create"));
  2083. members_dialog->get_ok()->connect("pressed", this, "_member_create");
  2084. members_dialog->get_ok()->set_disabled(true);
  2085. members_dialog->set_resizable(true);
  2086. members_dialog->set_as_minsize();
  2087. members_dialog->connect("hide", this, "_member_cancel");
  2088. add_child(members_dialog);
  2089. alert = memnew(AcceptDialog);
  2090. alert->set_as_minsize();
  2091. alert->get_label()->set_autowrap(true);
  2092. alert->get_label()->set_align(Label::ALIGN_CENTER);
  2093. alert->get_label()->set_valign(Label::VALIGN_CENTER);
  2094. alert->get_label()->set_custom_minimum_size(Size2(400, 60) * EDSCALE);
  2095. add_child(alert);
  2096. ///////////////////////////////////////
  2097. // SHADER NODES TREE OPTIONS
  2098. ///////////////////////////////////////
  2099. // COLOR
  2100. add_options.push_back(AddOption("ColorFunc", "Color", "Common", "VisualShaderNodeColorFunc", TTR("Color function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2101. add_options.push_back(AddOption("ColorOp", "Color", "Common", "VisualShaderNodeColorOp", TTR("Color operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2102. add_options.push_back(AddOption("Grayscale", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Grayscale function."), VisualShaderNodeColorFunc::FUNC_GRAYSCALE, VisualShaderNode::PORT_TYPE_VECTOR));
  2103. add_options.push_back(AddOption("HSV2RGB", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts HSV vector to RGB equivalent."), VisualShaderNodeVectorFunc::FUNC_HSV2RGB, VisualShaderNode::PORT_TYPE_VECTOR));
  2104. add_options.push_back(AddOption("RGB2HSV", "Color", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts RGB vector to HSV equivalent."), VisualShaderNodeVectorFunc::FUNC_RGB2HSV, VisualShaderNode::PORT_TYPE_VECTOR));
  2105. add_options.push_back(AddOption("Sepia", "Color", "Functions", "VisualShaderNodeColorFunc", TTR("Sepia function."), VisualShaderNodeColorFunc::FUNC_SEPIA, VisualShaderNode::PORT_TYPE_VECTOR));
  2106. add_options.push_back(AddOption("Burn", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Burn operator."), VisualShaderNodeColorOp::OP_BURN, VisualShaderNode::PORT_TYPE_VECTOR));
  2107. add_options.push_back(AddOption("Darken", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Darken operator."), VisualShaderNodeColorOp::OP_DARKEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2108. add_options.push_back(AddOption("Difference", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Difference operator."), VisualShaderNodeColorOp::OP_DIFFERENCE, VisualShaderNode::PORT_TYPE_VECTOR));
  2109. add_options.push_back(AddOption("Dodge", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Dodge operator."), VisualShaderNodeColorOp::OP_DODGE, VisualShaderNode::PORT_TYPE_VECTOR));
  2110. add_options.push_back(AddOption("HardLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("HardLight operator."), VisualShaderNodeColorOp::OP_HARD_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2111. add_options.push_back(AddOption("Lighten", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Lighten operator."), VisualShaderNodeColorOp::OP_LIGHTEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2112. add_options.push_back(AddOption("Overlay", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Overlay operator."), VisualShaderNodeColorOp::OP_OVERLAY, VisualShaderNode::PORT_TYPE_VECTOR));
  2113. add_options.push_back(AddOption("Screen", "Color", "Operators", "VisualShaderNodeColorOp", TTR("Screen operator."), VisualShaderNodeColorOp::OP_SCREEN, VisualShaderNode::PORT_TYPE_VECTOR));
  2114. add_options.push_back(AddOption("SoftLight", "Color", "Operators", "VisualShaderNodeColorOp", TTR("SoftLight operator."), VisualShaderNodeColorOp::OP_SOFT_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR));
  2115. add_options.push_back(AddOption("ColorConstant", "Color", "Variables", "VisualShaderNodeColorConstant", TTR("Color constant."), -1, -1));
  2116. add_options.push_back(AddOption("ColorUniform", "Color", "Variables", "VisualShaderNodeColorUniform", TTR("Color uniform."), -1, -1));
  2117. // CONDITIONAL
  2118. const String &compare_func_desc = TTR("Returns the boolean result of the %s comparison between two parameters.");
  2119. add_options.push_back(AddOption("Equal (==)", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Equal (==)")), VisualShaderNodeCompare::FUNC_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2120. add_options.push_back(AddOption("GreaterThan (>)", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Greater Than (>)")), VisualShaderNodeCompare::FUNC_GREATER_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2121. 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));
  2122. 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));
  2123. 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));
  2124. 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));
  2125. add_options.push_back(AddOption("LessThan (<)", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Less Than (<)")), VisualShaderNodeCompare::FUNC_LESS_THAN, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2126. 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));
  2127. add_options.push_back(AddOption("NotEqual (!=)", "Conditional", "Functions", "VisualShaderNodeCompare", vformat(compare_func_desc, TTR("Not Equal (!=)")), VisualShaderNodeCompare::FUNC_NOT_EQUAL, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2128. 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));
  2129. 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));
  2130. 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));
  2131. 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));
  2132. add_options.push_back(AddOption("BooleanConstant", "Conditional", "Variables", "VisualShaderNodeBooleanConstant", TTR("Boolean constant."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2133. add_options.push_back(AddOption("BooleanUniform", "Conditional", "Variables", "VisualShaderNodeBooleanUniform", TTR("Boolean uniform."), -1, VisualShaderNode::PORT_TYPE_BOOLEAN));
  2134. // INPUT
  2135. // SPATIAL-FOR-ALL
  2136. const String input_param_shader_modes = TTR("'%s' input parameter for all shader modes.");
  2137. add_options.push_back(AddOption("Camera", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "camera", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2138. 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));
  2139. 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));
  2140. add_options.push_back(AddOption("Normal", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_SPATIAL));
  2141. 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));
  2142. add_options.push_back(AddOption("Projection", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "camera"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2143. add_options.push_back(AddOption("Time", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_SPATIAL));
  2144. 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));
  2145. add_options.push_back(AddOption("World", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, -1, Shader::MODE_SPATIAL));
  2146. // CANVASITEM-FOR-ALL
  2147. 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));
  2148. 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));
  2149. add_options.push_back(AddOption("ModulateAlpha", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "modulate_alpha"), "modulate_alpha", VisualShaderNode::PORT_TYPE_SCALAR, -1, Shader::MODE_CANVAS_ITEM));
  2150. add_options.push_back(AddOption("ModulateColor", "Input", "All", "VisualShaderNodeInput", vformat(input_param_shader_modes, "modulate_color"), "modulate_color", VisualShaderNode::PORT_TYPE_VECTOR, -1, Shader::MODE_CANVAS_ITEM));
  2151. 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));
  2152. 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));
  2153. 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));
  2154. /////////////////
  2155. add_options.push_back(AddOption("Input", "Input", "Common", "VisualShaderNodeInput", TTR("Input parameter.")));
  2156. // SPATIAL INPUTS
  2157. const String input_param_for_vertex_and_fragment_shader_modes = TTR("'%s' input parameter for vertex and fragment shader modes.");
  2158. const String input_param_for_fragment_and_light_shader_modes = TTR("'%s' input parameter for fragment and light shader modes.");
  2159. const String input_param_for_fragment_shader_mode = TTR("'%s' input parameter for fragment shader mode.");
  2160. const String input_param_for_light_shader_mode = TTR("'%s' input parameter for light shader mode.");
  2161. const String input_param_for_vertex_shader_mode = TTR("'%s' input parameter for vertex shader mode.");
  2162. const String input_param_for_vertex_and_fragment_shader_mode = TTR("'%s' input parameter for vertex and fragment shader mode.");
  2163. add_options.push_back(AddOption("Alpha", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2164. add_options.push_back(AddOption("Binormal", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2165. add_options.push_back(AddOption("Color", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2166. add_options.push_back(AddOption("DepthTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "depth_texture"), "depth_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2167. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2168. add_options.push_back(AddOption("FrontFacing", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "front_facing"), "front_facing", VisualShaderNode::PORT_TYPE_BOOLEAN, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2169. add_options.push_back(AddOption("PointCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "point_coord"), "point_coord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2170. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2171. add_options.push_back(AddOption("ScreenUV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_uv"), "screen_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2172. add_options.push_back(AddOption("Side", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "side"), "side", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2173. add_options.push_back(AddOption("Tangent", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2174. add_options.push_back(AddOption("UV", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2175. add_options.push_back(AddOption("UV2", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2176. add_options.push_back(AddOption("Vertex", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2177. add_options.push_back(AddOption("View", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2178. add_options.push_back(AddOption("NodePositionWorld", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "node_position_world"), "node_position_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2179. add_options.push_back(AddOption("CameraPositionWorld", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "camera_position_world"), "camera_position_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2180. add_options.push_back(AddOption("CameraDirectionWorld", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "camera_direction_world"), "camera_direction_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2181. add_options.push_back(AddOption("NodePositionView", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "node_position_view"), "node_position_view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_SPATIAL));
  2182. add_options.push_back(AddOption("Albedo", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "albedo"), "albedo", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2183. add_options.push_back(AddOption("Attenuation", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "attenuation"), "attenuation", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2184. add_options.push_back(AddOption("Diffuse", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "diffuse"), "diffuse", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2185. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2186. add_options.push_back(AddOption("Light", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light"), "light", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2187. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2188. add_options.push_back(AddOption("Metallic", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "metallic"), "metallic", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2189. add_options.push_back(AddOption("Roughness", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "roughness"), "roughness", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2190. add_options.push_back(AddOption("Specular", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "specular"), "specular", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2191. add_options.push_back(AddOption("Transmission", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "transmission"), "transmission", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2192. add_options.push_back(AddOption("View", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "view"), "view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2193. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2194. add_options.push_back(AddOption("Binormal", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "binormal"), "binormal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2195. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2196. add_options.push_back(AddOption("ModelView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "modelview"), "modelview", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2197. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2198. add_options.push_back(AddOption("Tangent", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_mode, "tangent"), "tangent", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2199. add_options.push_back(AddOption("UV", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv"), "uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2200. add_options.push_back(AddOption("UV2", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "uv2"), "uv2", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2201. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2202. add_options.push_back(AddOption("NodePositionWorld", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "node_position_world"), "node_position_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2203. add_options.push_back(AddOption("CameraPositionWorld", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "camera_position_world"), "camera_position_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2204. add_options.push_back(AddOption("CameraDirectionWorld", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "camera_direction_world"), "camera_direction_world", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2205. add_options.push_back(AddOption("NodePositionView", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_and_fragment_shader_modes, "node_position_view"), "node_position_view", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_SPATIAL));
  2206. // CANVASITEM INPUTS
  2207. add_options.push_back(AddOption("FragCoord", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2208. 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, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2209. add_options.push_back(AddOption("NormalTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "normal_texture"), "normal_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2210. 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, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2211. 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, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2212. add_options.push_back(AddOption("ScreenTexture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_shader_mode, "screen_texture"), "screen_texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2213. 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, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2214. add_options.push_back(AddOption("Texture", "Input", "Fragment", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_FRAGMENT, Shader::MODE_CANVAS_ITEM));
  2215. add_options.push_back(AddOption("FragCoord", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "fragcoord"), "fragcoord", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2216. add_options.push_back(AddOption("LightAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_alpha"), "light_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2217. add_options.push_back(AddOption("LightColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_color"), "light_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2218. add_options.push_back(AddOption("LightHeight", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_height"), "light_height", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2219. add_options.push_back(AddOption("LightUV", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_uv"), "light_uv", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2220. add_options.push_back(AddOption("LightVector", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "light_vec"), "light_vec", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2221. add_options.push_back(AddOption("Normal", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "normal"), "normal", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2222. 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, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2223. 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, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2224. add_options.push_back(AddOption("ShadowAlpha", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_alpha"), "shadow_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2225. add_options.push_back(AddOption("ShadowColor", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_color"), "shadow_color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2226. add_options.push_back(AddOption("ShadowVec", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_light_shader_mode, "shadow_vec"), "shadow_vec", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2227. add_options.push_back(AddOption("Texture", "Input", "Light", "VisualShaderNodeInput", vformat(input_param_for_fragment_and_light_shader_modes, "texture"), "texture", VisualShaderNode::PORT_TYPE_SAMPLER, VisualShader::TYPE_LIGHT, Shader::MODE_CANVAS_ITEM));
  2228. add_options.push_back(AddOption("Extra", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "extra"), "extra", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2229. 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, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2230. add_options.push_back(AddOption("PointSize", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "point_size"), "point_size", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2231. add_options.push_back(AddOption("Projection", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "projection"), "projection", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2232. add_options.push_back(AddOption("Vertex", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "vertex"), "vertex", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2233. add_options.push_back(AddOption("World", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "world"), "world", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_CANVAS_ITEM));
  2234. // PARTICLES INPUTS
  2235. add_options.push_back(AddOption("Active", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "active"), "active", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2236. add_options.push_back(AddOption("Alpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "alpha"), "alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2237. add_options.push_back(AddOption("Color", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "color"), "color", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2238. add_options.push_back(AddOption("Custom", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom"), "custom", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2239. add_options.push_back(AddOption("CustomAlpha", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "custom_alpha"), "custom_alpha", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2240. add_options.push_back(AddOption("Delta", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "delta"), "delta", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2241. add_options.push_back(AddOption("EmissionTransform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "emission_transform"), "emission_transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2242. add_options.push_back(AddOption("Index", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "index"), "index", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2243. add_options.push_back(AddOption("LifeTime", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "lifetime"), "lifetime", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2244. add_options.push_back(AddOption("Restart", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "restart"), "restart", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2245. add_options.push_back(AddOption("Time", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "time"), "time", VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2246. add_options.push_back(AddOption("Transform", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "transform"), "transform", VisualShaderNode::PORT_TYPE_TRANSFORM, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2247. add_options.push_back(AddOption("Velocity", "Input", "Vertex", "VisualShaderNodeInput", vformat(input_param_for_vertex_shader_mode, "velocity"), "velocity", VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_VERTEX, Shader::MODE_PARTICLES));
  2248. // SCALAR
  2249. add_options.push_back(AddOption("ScalarFunc", "Scalar", "Common", "VisualShaderNodeScalarFunc", TTR("Scalar function."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2250. add_options.push_back(AddOption("ScalarOp", "Scalar", "Common", "VisualShaderNodeScalarOp", TTR("Scalar operator."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2251. //CONSTANTS
  2252. add_options.push_back(AddOption("E", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("E constant (2.718282). Represents the base of the natural logarithm."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_E));
  2253. add_options.push_back(AddOption("Epsilon", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Epsilon constant (0.00001). Smallest possible scalar number."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, CMP_EPSILON));
  2254. add_options.push_back(AddOption("Phi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Phi constant (1.618034). Golden ratio."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, 1.618034f));
  2255. add_options.push_back(AddOption("Pi/4", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/4 constant (0.785398) or 45 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 4));
  2256. add_options.push_back(AddOption("Pi/2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi/2 constant (1.570796) or 90 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI / 2));
  2257. add_options.push_back(AddOption("Pi", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Pi constant (3.141593) or 180 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_PI));
  2258. add_options.push_back(AddOption("Tau", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Tau constant (6.283185) or 360 degrees."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_TAU));
  2259. add_options.push_back(AddOption("Sqrt2", "Scalar", "Constants", "VisualShaderNodeScalarConstant", TTR("Sqrt2 constant (1.414214). Square root of 2."), -1, VisualShaderNode::PORT_TYPE_SCALAR, -1, -1, Math_SQRT2));
  2260. // FUNCTIONS
  2261. add_options.push_back(AddOption("Abs", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeScalarFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_SCALAR));
  2262. add_options.push_back(AddOption("ACos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_SCALAR));
  2263. add_options.push_back(AddOption("ACosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ACOSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2264. add_options.push_back(AddOption("ASin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2265. add_options.push_back(AddOption("ASinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_ASINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2266. add_options.push_back(AddOption("ATan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2267. add_options.push_back(AddOption("ATan2", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeScalarOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_SCALAR));
  2268. add_options.push_back(AddOption("ATanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_ATANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2269. add_options.push_back(AddOption("Ceil", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer that is greater than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_CEIL, VisualShaderNode::PORT_TYPE_SCALAR));
  2270. add_options.push_back(AddOption("Clamp", "Scalar", "Functions", "VisualShaderNodeScalarClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2271. add_options.push_back(AddOption("Cos", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_SCALAR));
  2272. add_options.push_back(AddOption("CosH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeScalarFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_SCALAR));
  2273. add_options.push_back(AddOption("Degrees", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeScalarFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_SCALAR));
  2274. add_options.push_back(AddOption("Exp", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-e Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_SCALAR));
  2275. add_options.push_back(AddOption("Exp2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 Exponential."), VisualShaderNodeScalarFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_SCALAR));
  2276. add_options.push_back(AddOption("Floor", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer less than or equal to the parameter."), VisualShaderNodeScalarFunc::FUNC_FLOOR, VisualShaderNode::PORT_TYPE_SCALAR));
  2277. add_options.push_back(AddOption("Fract", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeScalarFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_SCALAR));
  2278. add_options.push_back(AddOption("InverseSqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the inverse of the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_INVERSE_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2279. add_options.push_back(AddOption("Log", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Natural logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_SCALAR));
  2280. add_options.push_back(AddOption("Log2", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Base-2 logarithm."), VisualShaderNodeScalarFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_SCALAR));
  2281. add_options.push_back(AddOption("Max", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the greater of two values."), VisualShaderNodeScalarOp::OP_MAX, VisualShaderNode::PORT_TYPE_SCALAR));
  2282. add_options.push_back(AddOption("Min", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the lesser of two values."), VisualShaderNodeScalarOp::OP_MIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2283. add_options.push_back(AddOption("Mix", "Scalar", "Functions", "VisualShaderNodeScalarInterp", TTR("Linear interpolation between two scalars."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2284. add_options.push_back(AddOption("Negate (*-1)", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeScalarFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2285. add_options.push_back(AddOption("OneMinus (1-)", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 - scalar"), VisualShaderNodeScalarFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_SCALAR));
  2286. add_options.push_back(AddOption("Pow (^)", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Returns the value of the first parameter raised to the power of the second."), VisualShaderNodeScalarOp::OP_POW, VisualShaderNode::PORT_TYPE_SCALAR));
  2287. add_options.push_back(AddOption("Radians", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeScalarFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_SCALAR));
  2288. add_options.push_back(AddOption("Reciprocal", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("1.0 / scalar"), VisualShaderNodeScalarFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_SCALAR));
  2289. add_options.push_back(AddOption("Round", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_SCALAR));
  2290. add_options.push_back(AddOption("RoundEven", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the nearest even integer to the parameter."), VisualShaderNodeScalarFunc::FUNC_ROUNDEVEN, VisualShaderNode::PORT_TYPE_SCALAR));
  2291. add_options.push_back(AddOption("Saturate", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Clamps the value between 0.0 and 1.0."), VisualShaderNodeScalarFunc::FUNC_SATURATE, VisualShaderNode::PORT_TYPE_SCALAR));
  2292. add_options.push_back(AddOption("Sign", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_SCALAR));
  2293. add_options.push_back(AddOption("Sin", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_SCALAR));
  2294. add_options.push_back(AddOption("SinH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeScalarFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_SCALAR));
  2295. add_options.push_back(AddOption("Sqrt", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeScalarFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_SCALAR));
  2296. 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));
  2297. add_options.push_back(AddOption("Step", "Scalar", "Functions", "VisualShaderNodeScalarOp", TTR("Step function( scalar(edge), scalar(x) ).\n\nReturns 0.0 if 'x' is smaller than 'edge' and otherwise 1.0."), VisualShaderNodeScalarOp::OP_STEP, VisualShaderNode::PORT_TYPE_SCALAR));
  2298. add_options.push_back(AddOption("Tan", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_SCALAR));
  2299. add_options.push_back(AddOption("TanH", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeScalarFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_SCALAR));
  2300. add_options.push_back(AddOption("Trunc", "Scalar", "Functions", "VisualShaderNodeScalarFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeScalarFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_SCALAR));
  2301. add_options.push_back(AddOption("Add (+)", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Adds scalar to scalar."), VisualShaderNodeScalarOp::OP_ADD, VisualShaderNode::PORT_TYPE_SCALAR));
  2302. add_options.push_back(AddOption("Divide (/)", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Divides scalar by scalar."), VisualShaderNodeScalarOp::OP_DIV, VisualShaderNode::PORT_TYPE_SCALAR));
  2303. add_options.push_back(AddOption("Multiply (*)", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Multiplies scalar by scalar."), VisualShaderNodeScalarOp::OP_MUL, VisualShaderNode::PORT_TYPE_SCALAR));
  2304. add_options.push_back(AddOption("Remainder", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Returns the remainder of the two scalars."), VisualShaderNodeScalarOp::OP_MOD, VisualShaderNode::PORT_TYPE_SCALAR));
  2305. add_options.push_back(AddOption("Subtract (-)", "Scalar", "Operators", "VisualShaderNodeScalarOp", TTR("Subtracts scalar from scalar."), VisualShaderNodeScalarOp::OP_SUB, VisualShaderNode::PORT_TYPE_SCALAR));
  2306. add_options.push_back(AddOption("ScalarConstant", "Scalar", "Variables", "VisualShaderNodeScalarConstant", TTR("Scalar constant."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2307. add_options.push_back(AddOption("ScalarUniform", "Scalar", "Variables", "VisualShaderNodeScalarUniform", TTR("Scalar uniform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2308. // TEXTURES
  2309. add_options.push_back(AddOption("CubeMap", "Textures", "Functions", "VisualShaderNodeCubeMap", TTR("Perform the cubic texture lookup."), -1, -1));
  2310. texture_node_option_idx = add_options.size();
  2311. add_options.push_back(AddOption("Texture", "Textures", "Functions", "VisualShaderNodeTexture", TTR("Perform the texture lookup."), -1, -1));
  2312. add_options.push_back(AddOption("CubeMapUniform", "Textures", "Variables", "VisualShaderNodeCubeMapUniform", TTR("Cubic texture uniform lookup."), -1, -1));
  2313. add_options.push_back(AddOption("TextureUniform", "Textures", "Variables", "VisualShaderNodeTextureUniform", TTR("2D texture uniform lookup."), -1, -1));
  2314. add_options.push_back(AddOption("TextureUniformTriplanar", "Textures", "Variables", "VisualShaderNodeTextureUniformTriplanar", TTR("2D texture uniform lookup with triplanar."), -1, -1, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, Shader::MODE_SPATIAL));
  2315. // TRANSFORM
  2316. add_options.push_back(AddOption("TransformFunc", "Transform", "Common", "VisualShaderNodeTransformFunc", TTR("Transform function."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2317. 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));
  2318. add_options.push_back(AddOption("TransformCompose", "Transform", "Composition", "VisualShaderNodeTransformCompose", TTR("Composes transform from four vectors."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2319. add_options.push_back(AddOption("TransformDecompose", "Transform", "Composition", "VisualShaderNodeTransformDecompose", TTR("Decomposes transform to four vectors.")));
  2320. add_options.push_back(AddOption("Determinant", "Transform", "Functions", "VisualShaderNodeDeterminant", TTR("Calculates the determinant of a transform."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2321. add_options.push_back(AddOption("Inverse", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the inverse of a transform."), VisualShaderNodeTransformFunc::FUNC_INVERSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2322. add_options.push_back(AddOption("Transpose", "Transform", "Functions", "VisualShaderNodeTransformFunc", TTR("Calculates the transpose of a transform."), VisualShaderNodeTransformFunc::FUNC_TRANSPOSE, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2323. add_options.push_back(AddOption("TransformMult (*)", "Transform", "Operators", "VisualShaderNodeTransformMult", TTR("Multiplies transform by transform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2324. add_options.push_back(AddOption("TransformVectorMult (*)", "Transform", "Operators", "VisualShaderNodeTransformVecMult", TTR("Multiplies vector by transform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2325. add_options.push_back(AddOption("TransformConstant", "Transform", "Variables", "VisualShaderNodeTransformConstant", TTR("Transform constant."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2326. add_options.push_back(AddOption("TransformUniform", "Transform", "Variables", "VisualShaderNodeTransformUniform", TTR("Transform uniform."), -1, VisualShaderNode::PORT_TYPE_TRANSFORM));
  2327. // VECTOR
  2328. add_options.push_back(AddOption("VectorFunc", "Vector", "Common", "VisualShaderNodeVectorFunc", TTR("Vector function."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2329. add_options.push_back(AddOption("VectorOp", "Vector", "Common", "VisualShaderNodeVectorOp", TTR("Vector operator."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2330. add_options.push_back(AddOption("VectorCompose", "Vector", "Composition", "VisualShaderNodeVectorCompose", TTR("Composes vector from three scalars."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2331. add_options.push_back(AddOption("VectorDecompose", "Vector", "Composition", "VisualShaderNodeVectorDecompose", TTR("Decomposes vector to three scalars.")));
  2332. add_options.push_back(AddOption("Abs", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the absolute value of the parameter."), VisualShaderNodeVectorFunc::FUNC_ABS, VisualShaderNode::PORT_TYPE_VECTOR));
  2333. add_options.push_back(AddOption("ACos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ACOS, VisualShaderNode::PORT_TYPE_VECTOR));
  2334. 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));
  2335. add_options.push_back(AddOption("ASin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_ASIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2336. 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));
  2337. add_options.push_back(AddOption("ATan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the arc-tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_ATAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2338. add_options.push_back(AddOption("ATan2", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the arc-tangent of the parameters."), VisualShaderNodeVectorOp::OP_ATAN2, VisualShaderNode::PORT_TYPE_VECTOR));
  2339. 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));
  2340. 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));
  2341. add_options.push_back(AddOption("Clamp", "Vector", "Functions", "VisualShaderNodeVectorClamp", TTR("Constrains a value to lie between two further values."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2342. add_options.push_back(AddOption("Cos", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COS, VisualShaderNode::PORT_TYPE_VECTOR));
  2343. add_options.push_back(AddOption("CosH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic cosine of the parameter."), VisualShaderNodeVectorFunc::FUNC_COSH, VisualShaderNode::PORT_TYPE_VECTOR));
  2344. add_options.push_back(AddOption("Cross", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Calculates the cross product of two vectors."), VisualShaderNodeVectorOp::OP_CROSS, VisualShaderNode::PORT_TYPE_VECTOR));
  2345. add_options.push_back(AddOption("Degrees", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in radians to degrees."), VisualShaderNodeVectorFunc::FUNC_DEGREES, VisualShaderNode::PORT_TYPE_VECTOR));
  2346. add_options.push_back(AddOption("Distance", "Vector", "Functions", "VisualShaderNodeVectorDistance", TTR("Returns the distance between two points."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2347. add_options.push_back(AddOption("Dot", "Vector", "Functions", "VisualShaderNodeDotProduct", TTR("Calculates the dot product of two vectors."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2348. add_options.push_back(AddOption("Exp", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-e Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP, VisualShaderNode::PORT_TYPE_VECTOR));
  2349. add_options.push_back(AddOption("Exp2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 Exponential."), VisualShaderNodeVectorFunc::FUNC_EXP2, VisualShaderNode::PORT_TYPE_VECTOR));
  2350. 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));
  2351. 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));
  2352. add_options.push_back(AddOption("Fract", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Computes the fractional part of the argument."), VisualShaderNodeVectorFunc::FUNC_FRAC, VisualShaderNode::PORT_TYPE_VECTOR));
  2353. 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));
  2354. add_options.push_back(AddOption("Length", "Vector", "Functions", "VisualShaderNodeVectorLen", TTR("Calculates the length of a vector."), -1, VisualShaderNode::PORT_TYPE_SCALAR));
  2355. add_options.push_back(AddOption("Log", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Natural logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG, VisualShaderNode::PORT_TYPE_VECTOR));
  2356. add_options.push_back(AddOption("Log2", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Base-2 logarithm."), VisualShaderNodeVectorFunc::FUNC_LOG2, VisualShaderNode::PORT_TYPE_VECTOR));
  2357. add_options.push_back(AddOption("Max", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the greater of two values."), VisualShaderNodeVectorOp::OP_MAX, VisualShaderNode::PORT_TYPE_VECTOR));
  2358. add_options.push_back(AddOption("Min", "Vector", "Functions", "VisualShaderNodeVectorOp", TTR("Returns the lesser of two values."), VisualShaderNodeVectorOp::OP_MIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2359. add_options.push_back(AddOption("Mix", "Vector", "Functions", "VisualShaderNodeVectorInterp", TTR("Linear interpolation between two vectors."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2360. add_options.push_back(AddOption("MixS", "Vector", "Functions", "VisualShaderNodeVectorScalarMix", TTR("Linear interpolation between two vectors using scalar."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2361. add_options.push_back(AddOption("Negate (*-1)", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the opposite value of the parameter."), VisualShaderNodeVectorFunc::FUNC_NEGATE, VisualShaderNode::PORT_TYPE_VECTOR));
  2362. add_options.push_back(AddOption("Normalize", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Calculates the normalize product of vector."), VisualShaderNodeVectorFunc::FUNC_NORMALIZE, VisualShaderNode::PORT_TYPE_VECTOR));
  2363. add_options.push_back(AddOption("OneMinus", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 - vector"), VisualShaderNodeVectorFunc::FUNC_ONEMINUS, VisualShaderNode::PORT_TYPE_VECTOR));
  2364. 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));
  2365. add_options.push_back(AddOption("Radians", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Converts a quantity in degrees to radians."), VisualShaderNodeVectorFunc::FUNC_RADIANS, VisualShaderNode::PORT_TYPE_VECTOR));
  2366. add_options.push_back(AddOption("Reciprocal", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("1.0 / vector"), VisualShaderNodeVectorFunc::FUNC_RECIPROCAL, VisualShaderNode::PORT_TYPE_VECTOR));
  2367. 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));
  2368. 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));
  2369. add_options.push_back(AddOption("Round", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the nearest integer to the parameter."), VisualShaderNodeVectorFunc::FUNC_ROUND, VisualShaderNode::PORT_TYPE_VECTOR));
  2370. 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));
  2371. 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));
  2372. add_options.push_back(AddOption("Sign", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Extracts the sign of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIGN, VisualShaderNode::PORT_TYPE_VECTOR));
  2373. add_options.push_back(AddOption("Sin", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SIN, VisualShaderNode::PORT_TYPE_VECTOR));
  2374. add_options.push_back(AddOption("SinH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic sine of the parameter."), VisualShaderNodeVectorFunc::FUNC_SINH, VisualShaderNode::PORT_TYPE_VECTOR));
  2375. add_options.push_back(AddOption("Sqrt", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the square root of the parameter."), VisualShaderNodeVectorFunc::FUNC_SQRT, VisualShaderNode::PORT_TYPE_VECTOR));
  2376. 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));
  2377. 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));
  2378. 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));
  2379. 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));
  2380. add_options.push_back(AddOption("Tan", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TAN, VisualShaderNode::PORT_TYPE_VECTOR));
  2381. add_options.push_back(AddOption("TanH", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Returns the hyperbolic tangent of the parameter."), VisualShaderNodeVectorFunc::FUNC_TANH, VisualShaderNode::PORT_TYPE_VECTOR));
  2382. add_options.push_back(AddOption("Trunc", "Vector", "Functions", "VisualShaderNodeVectorFunc", TTR("Finds the truncated value of the parameter."), VisualShaderNodeVectorFunc::FUNC_TRUNC, VisualShaderNode::PORT_TYPE_VECTOR));
  2383. add_options.push_back(AddOption("Add (+)", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Adds vector to vector."), VisualShaderNodeVectorOp::OP_ADD, VisualShaderNode::PORT_TYPE_VECTOR));
  2384. add_options.push_back(AddOption("Divide (/)", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Divides vector by vector."), VisualShaderNodeVectorOp::OP_DIV, VisualShaderNode::PORT_TYPE_VECTOR));
  2385. add_options.push_back(AddOption("Multiply (*)", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Multiplies vector by vector."), VisualShaderNodeVectorOp::OP_MUL, VisualShaderNode::PORT_TYPE_VECTOR));
  2386. add_options.push_back(AddOption("Remainder", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Returns the remainder of the two vectors."), VisualShaderNodeVectorOp::OP_MOD, VisualShaderNode::PORT_TYPE_VECTOR));
  2387. add_options.push_back(AddOption("Subtract (-)", "Vector", "Operators", "VisualShaderNodeVectorOp", TTR("Subtracts vector from vector."), VisualShaderNodeVectorOp::OP_SUB, VisualShaderNode::PORT_TYPE_VECTOR));
  2388. add_options.push_back(AddOption("VectorConstant", "Vector", "Variables", "VisualShaderNodeVec3Constant", TTR("Vector constant."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2389. add_options.push_back(AddOption("VectorUniform", "Vector", "Variables", "VisualShaderNodeVec3Uniform", TTR("Vector uniform."), -1, VisualShaderNode::PORT_TYPE_VECTOR));
  2390. // SPECIAL
  2391. 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.")));
  2392. 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));
  2393. 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.")));
  2394. add_options.push_back(AddOption("UniformRef", "Special", "", "VisualShaderNodeUniformRef", TTR("A reference to an existing uniform.")));
  2395. add_options.push_back(AddOption("ScalarDerivativeFunc", "Special", "Common", "VisualShaderNodeScalarDerivativeFunc", TTR("(Fragment/Light mode only) Scalar derivative function."), -1, VisualShaderNode::PORT_TYPE_SCALAR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2396. add_options.push_back(AddOption("VectorDerivativeFunc", "Special", "Common", "VisualShaderNodeVectorDerivativeFunc", TTR("(Fragment/Light mode only) Vector derivative function."), -1, VisualShaderNode::PORT_TYPE_VECTOR, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2397. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2398. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2399. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2400. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2401. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2402. 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, VisualShader::TYPE_FRAGMENT | VisualShader::TYPE_LIGHT, -1, -1, true));
  2403. custom_node_option_idx = add_options.size();
  2404. /////////////////////////////////////////////////////////////////////
  2405. _update_options_menu();
  2406. error_panel = memnew(PanelContainer);
  2407. add_child(error_panel);
  2408. error_label = memnew(Label);
  2409. error_panel->add_child(error_label);
  2410. error_label->set_text("eh");
  2411. error_panel->hide();
  2412. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2413. Ref<VisualShaderNodePluginDefault> default_plugin;
  2414. default_plugin.instance();
  2415. add_plugin(default_plugin);
  2416. property_editor = memnew(CustomPropertyEditor);
  2417. add_child(property_editor);
  2418. property_editor->connect("variant_changed", this, "_port_edited");
  2419. }
  2420. void VisualShaderEditorPlugin::edit(Object *p_object) {
  2421. visual_shader_editor->edit(Object::cast_to<VisualShader>(p_object));
  2422. }
  2423. bool VisualShaderEditorPlugin::handles(Object *p_object) const {
  2424. return p_object->is_class("VisualShader");
  2425. }
  2426. void VisualShaderEditorPlugin::make_visible(bool p_visible) {
  2427. if (p_visible) {
  2428. //editor->hide_animation_player_editors();
  2429. //editor->animation_panel_make_visible(true);
  2430. button->show();
  2431. editor->make_bottom_panel_item_visible(visual_shader_editor);
  2432. visual_shader_editor->update_custom_nodes();
  2433. visual_shader_editor->set_process_input(true);
  2434. //visual_shader_editor->set_process(true);
  2435. } else {
  2436. if (visual_shader_editor->is_visible_in_tree()) {
  2437. editor->hide_bottom_panel();
  2438. }
  2439. button->hide();
  2440. visual_shader_editor->set_process_input(false);
  2441. //visual_shader_editor->set_process(false);
  2442. }
  2443. }
  2444. VisualShaderEditorPlugin::VisualShaderEditorPlugin(EditorNode *p_node) {
  2445. editor = p_node;
  2446. visual_shader_editor = memnew(VisualShaderEditor);
  2447. visual_shader_editor->set_custom_minimum_size(Size2(0, 300) * EDSCALE);
  2448. button = editor->add_bottom_panel_item(TTR("VisualShader"), visual_shader_editor);
  2449. button->hide();
  2450. }
  2451. VisualShaderEditorPlugin::~VisualShaderEditorPlugin() {
  2452. }
  2453. ////////////////
  2454. class VisualShaderNodePluginInputEditor : public OptionButton {
  2455. GDCLASS(VisualShaderNodePluginInputEditor, OptionButton);
  2456. Ref<VisualShaderNodeInput> input;
  2457. protected:
  2458. static void _bind_methods() {
  2459. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginInputEditor::_item_selected);
  2460. }
  2461. public:
  2462. void _notification(int p_what) {
  2463. if (p_what == NOTIFICATION_READY) {
  2464. connect("item_selected", this, "_item_selected");
  2465. }
  2466. }
  2467. void _item_selected(int p_item) {
  2468. VisualShaderEditor::get_singleton()->call_deferred("_input_select_item", input, get_item_text(p_item));
  2469. }
  2470. void setup(const Ref<VisualShaderNodeInput> &p_input) {
  2471. input = p_input;
  2472. Ref<Texture> type_icon[5] = {
  2473. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  2474. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  2475. EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"),
  2476. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  2477. EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"),
  2478. };
  2479. add_item("[None]");
  2480. int to_select = -1;
  2481. for (int i = 0; i < input->get_input_index_count(); i++) {
  2482. if (input->get_input_name() == input->get_input_index_name(i)) {
  2483. to_select = i + 1;
  2484. }
  2485. add_icon_item(type_icon[input->get_input_index_type(i)], input->get_input_index_name(i));
  2486. }
  2487. if (to_select >= 0) {
  2488. select(to_select);
  2489. }
  2490. }
  2491. };
  2492. ////////////////
  2493. class VisualShaderNodePluginUniformRefEditor : public OptionButton {
  2494. GDCLASS(VisualShaderNodePluginUniformRefEditor, OptionButton);
  2495. Ref<VisualShaderNodeUniformRef> uniform_ref;
  2496. protected:
  2497. static void _bind_methods() {
  2498. ClassDB::bind_method("_item_selected", &VisualShaderNodePluginUniformRefEditor::_item_selected);
  2499. }
  2500. public:
  2501. void _notification(int p_what) {
  2502. if (p_what == NOTIFICATION_READY) {
  2503. connect("item_selected", this, "_item_selected");
  2504. }
  2505. }
  2506. void _item_selected(int p_item) {
  2507. VisualShaderEditor::get_singleton()->call_deferred("_uniform_select_item", uniform_ref, get_item_text(p_item));
  2508. }
  2509. void setup(const Ref<VisualShaderNodeUniformRef> &p_uniform_ref) {
  2510. uniform_ref = p_uniform_ref;
  2511. Ref<Texture> type_icon[6] = {
  2512. EditorNode::get_singleton()->get_gui_base()->get_icon("float", "EditorIcons"),
  2513. EditorNode::get_singleton()->get_gui_base()->get_icon("bool", "EditorIcons"),
  2514. EditorNode::get_singleton()->get_gui_base()->get_icon("Vector3", "EditorIcons"),
  2515. EditorNode::get_singleton()->get_gui_base()->get_icon("Transform", "EditorIcons"),
  2516. EditorNode::get_singleton()->get_gui_base()->get_icon("Color", "EditorIcons"),
  2517. EditorNode::get_singleton()->get_gui_base()->get_icon("ImageTexture", "EditorIcons"),
  2518. };
  2519. add_item("[None]");
  2520. int to_select = -1;
  2521. for (int i = 0; i < p_uniform_ref->get_uniforms_count(); i++) {
  2522. if (p_uniform_ref->get_uniform_name() == p_uniform_ref->get_uniform_name_by_index(i)) {
  2523. to_select = i + 1;
  2524. }
  2525. add_icon_item(type_icon[p_uniform_ref->get_uniform_type_by_index(i)], p_uniform_ref->get_uniform_name_by_index(i));
  2526. }
  2527. if (to_select >= 0) {
  2528. select(to_select);
  2529. }
  2530. }
  2531. };
  2532. ////////////////
  2533. class VisualShaderNodePluginDefaultEditor : public VBoxContainer {
  2534. GDCLASS(VisualShaderNodePluginDefaultEditor, VBoxContainer);
  2535. Ref<Resource> parent_resource;
  2536. public:
  2537. void _property_changed(const String &prop, const Variant &p_value, const String &p_field, bool p_changing = false) {
  2538. if (p_changing) {
  2539. return;
  2540. }
  2541. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2542. updating = true;
  2543. undo_redo->create_action(TTR("Edit Visual Property:") + " " + prop, UndoRedo::MERGE_ENDS);
  2544. undo_redo->add_do_property(node.ptr(), prop, p_value);
  2545. undo_redo->add_undo_property(node.ptr(), prop, node->get(prop));
  2546. if (p_value.get_type() == Variant::OBJECT) {
  2547. RES prev_res = node->get(prop);
  2548. RES curr_res = p_value;
  2549. if (curr_res.is_null()) {
  2550. undo_redo->add_do_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2551. } else {
  2552. undo_redo->add_do_method(this, "_open_inspector", (RES)curr_res.ptr());
  2553. }
  2554. if (!prev_res.is_null()) {
  2555. undo_redo->add_undo_method(this, "_open_inspector", (RES)prev_res.ptr());
  2556. } else {
  2557. undo_redo->add_undo_method(this, "_open_inspector", (RES)parent_resource.ptr());
  2558. }
  2559. }
  2560. undo_redo->add_do_method(this, "_refresh_request");
  2561. undo_redo->add_undo_method(this, "_refresh_request");
  2562. undo_redo->commit_action();
  2563. updating = false;
  2564. }
  2565. void _node_changed() {
  2566. if (updating) {
  2567. return;
  2568. }
  2569. for (int i = 0; i < properties.size(); i++) {
  2570. properties[i]->update_property();
  2571. }
  2572. }
  2573. void _refresh_request() {
  2574. VisualShaderEditor::get_singleton()->call_deferred("_update_graph");
  2575. }
  2576. void _resource_selected(const String &p_path, RES p_resource) {
  2577. _open_inspector(p_resource);
  2578. }
  2579. void _open_inspector(RES p_resource) {
  2580. EditorNode::get_singleton()->get_inspector()->edit(p_resource.ptr());
  2581. }
  2582. bool updating;
  2583. Ref<VisualShaderNode> node;
  2584. Vector<EditorProperty *> properties;
  2585. void setup(Ref<Resource> p_parent_resource, Vector<EditorProperty *> p_properties, const Vector<StringName> &p_names, Ref<VisualShaderNode> p_node) {
  2586. parent_resource = p_parent_resource;
  2587. updating = false;
  2588. node = p_node;
  2589. properties = p_properties;
  2590. for (int i = 0; i < p_properties.size(); i++) {
  2591. HBoxContainer *hbox = memnew(HBoxContainer);
  2592. hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2593. add_child(hbox);
  2594. if (p_node->is_show_prop_names()) {
  2595. Label *prop_name = memnew(Label);
  2596. prop_name->set_text(String(p_names[i]).capitalize() + ":");
  2597. hbox->add_child(prop_name);
  2598. }
  2599. p_properties[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  2600. hbox->add_child(p_properties[i]);
  2601. bool res_prop = Object::cast_to<EditorPropertyResource>(p_properties[i]);
  2602. if (res_prop) {
  2603. p_properties[i]->connect("resource_selected", this, "_resource_selected");
  2604. }
  2605. properties[i]->connect("property_changed", this, "_property_changed");
  2606. properties[i]->set_object_and_property(node.ptr(), p_names[i]);
  2607. properties[i]->update_property();
  2608. properties[i]->set_name_split_ratio(0);
  2609. }
  2610. node->connect("changed", this, "_node_changed");
  2611. node->connect("editor_refresh_request", this, "_refresh_request", varray(), CONNECT_DEFERRED);
  2612. }
  2613. static void _bind_methods() {
  2614. ClassDB::bind_method("_property_changed", &VisualShaderNodePluginDefaultEditor::_property_changed, DEFVAL(String()), DEFVAL(false));
  2615. ClassDB::bind_method("_node_changed", &VisualShaderNodePluginDefaultEditor::_node_changed);
  2616. ClassDB::bind_method("_refresh_request", &VisualShaderNodePluginDefaultEditor::_refresh_request);
  2617. ClassDB::bind_method("_resource_selected", &VisualShaderNodePluginDefaultEditor::_resource_selected);
  2618. ClassDB::bind_method("_open_inspector", &VisualShaderNodePluginDefaultEditor::_open_inspector);
  2619. }
  2620. };
  2621. Control *VisualShaderNodePluginDefault::create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) {
  2622. if (p_node->is_class("VisualShaderNodeUniformRef")) {
  2623. //create input
  2624. VisualShaderNodePluginUniformRefEditor *uniform_editor = memnew(VisualShaderNodePluginUniformRefEditor);
  2625. uniform_editor->setup(p_node);
  2626. return uniform_editor;
  2627. }
  2628. if (p_node->is_class("VisualShaderNodeInput")) {
  2629. //create input
  2630. VisualShaderNodePluginInputEditor *input_editor = memnew(VisualShaderNodePluginInputEditor);
  2631. input_editor->setup(p_node);
  2632. return input_editor;
  2633. }
  2634. Vector<StringName> properties = p_node->get_editable_properties();
  2635. if (properties.size() == 0) {
  2636. return nullptr;
  2637. }
  2638. List<PropertyInfo> props;
  2639. p_node->get_property_list(&props);
  2640. Vector<PropertyInfo> pinfo;
  2641. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2642. for (int i = 0; i < properties.size(); i++) {
  2643. if (E->get().name == String(properties[i])) {
  2644. pinfo.push_back(E->get());
  2645. }
  2646. }
  2647. }
  2648. if (pinfo.size() == 0) {
  2649. return nullptr;
  2650. }
  2651. properties.clear();
  2652. Ref<VisualShaderNode> node = p_node;
  2653. Vector<EditorProperty *> editors;
  2654. for (int i = 0; i < pinfo.size(); i++) {
  2655. EditorProperty *prop = EditorInspector::instantiate_property_editor(node.ptr(), pinfo[i].type, pinfo[i].name, pinfo[i].hint, pinfo[i].hint_string, pinfo[i].usage);
  2656. if (!prop) {
  2657. return nullptr;
  2658. }
  2659. if (Object::cast_to<EditorPropertyResource>(prop)) {
  2660. Object::cast_to<EditorPropertyResource>(prop)->set_use_sub_inspector(false);
  2661. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2662. } else if (Object::cast_to<EditorPropertyTransform>(prop) || Object::cast_to<EditorPropertyVector3>(prop)) {
  2663. prop->set_custom_minimum_size(Size2(250 * EDSCALE, 0));
  2664. } else if (Object::cast_to<EditorPropertyFloat>(prop)) {
  2665. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2666. } else if (Object::cast_to<EditorPropertyEnum>(prop)) {
  2667. prop->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2668. Object::cast_to<EditorPropertyEnum>(prop)->set_option_button_clip(false);
  2669. }
  2670. editors.push_back(prop);
  2671. properties.push_back(pinfo[i].name);
  2672. }
  2673. VisualShaderNodePluginDefaultEditor *editor = memnew(VisualShaderNodePluginDefaultEditor);
  2674. editor->setup(p_parent_resource, editors, properties, p_node);
  2675. return editor;
  2676. }
  2677. void EditorPropertyShaderMode::_option_selected(int p_which) {
  2678. //will not use this, instead will do all the logic setting manually
  2679. //emit_signal("property_changed", get_edited_property(), p_which);
  2680. Ref<VisualShader> visual_shader(Object::cast_to<VisualShader>(get_edited_object()));
  2681. if (visual_shader->get_mode() == p_which) {
  2682. return;
  2683. }
  2684. UndoRedo *undo_redo = EditorNode::get_singleton()->get_undo_redo();
  2685. undo_redo->create_action(TTR("Visual Shader Mode Changed"));
  2686. //do is easy
  2687. undo_redo->add_do_method(visual_shader.ptr(), "set_mode", p_which);
  2688. undo_redo->add_undo_method(visual_shader.ptr(), "set_mode", visual_shader->get_mode());
  2689. //now undo is hell
  2690. //1. restore connections to output
  2691. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2692. VisualShader::Type type = VisualShader::Type(i);
  2693. List<VisualShader::Connection> conns;
  2694. visual_shader->get_node_connections(type, &conns);
  2695. for (List<VisualShader::Connection>::Element *E = conns.front(); E; E = E->next()) {
  2696. if (E->get().to_node == VisualShader::NODE_ID_OUTPUT) {
  2697. 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);
  2698. }
  2699. }
  2700. }
  2701. //2. restore input indices
  2702. for (int i = 0; i < VisualShader::TYPE_MAX; i++) {
  2703. VisualShader::Type type = VisualShader::Type(i);
  2704. Vector<int> nodes = visual_shader->get_node_list(type);
  2705. for (int j = 0; j < nodes.size(); j++) {
  2706. Ref<VisualShaderNodeInput> input = visual_shader->get_node(type, nodes[j]);
  2707. if (!input.is_valid()) {
  2708. continue;
  2709. }
  2710. undo_redo->add_undo_method(input.ptr(), "set_input_name", input->get_input_name());
  2711. }
  2712. }
  2713. //3. restore enums and flags
  2714. List<PropertyInfo> props;
  2715. visual_shader->get_property_list(&props);
  2716. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  2717. if (E->get().name.begins_with("flags/") || E->get().name.begins_with("modes/")) {
  2718. undo_redo->add_undo_property(visual_shader.ptr(), E->get().name, visual_shader->get(E->get().name));
  2719. }
  2720. }
  2721. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2722. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_options_menu");
  2723. //update graph
  2724. undo_redo->add_do_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2725. undo_redo->add_undo_method(VisualShaderEditor::get_singleton(), "_update_graph");
  2726. undo_redo->commit_action();
  2727. }
  2728. void EditorPropertyShaderMode::update_property() {
  2729. int which = get_edited_object()->get(get_edited_property());
  2730. options->select(which);
  2731. }
  2732. void EditorPropertyShaderMode::setup(const Vector<String> &p_options) {
  2733. for (int i = 0; i < p_options.size(); i++) {
  2734. options->add_item(p_options[i], i);
  2735. }
  2736. }
  2737. void EditorPropertyShaderMode::set_option_button_clip(bool p_enable) {
  2738. options->set_clip_text(p_enable);
  2739. }
  2740. void EditorPropertyShaderMode::_bind_methods() {
  2741. ClassDB::bind_method(D_METHOD("_option_selected"), &EditorPropertyShaderMode::_option_selected);
  2742. }
  2743. EditorPropertyShaderMode::EditorPropertyShaderMode() {
  2744. options = memnew(OptionButton);
  2745. options->set_clip_text(true);
  2746. add_child(options);
  2747. add_focusable(options);
  2748. options->connect("item_selected", this, "_option_selected");
  2749. }
  2750. bool EditorInspectorShaderModePlugin::can_handle(Object *p_object) {
  2751. return true; //can handle everything
  2752. }
  2753. void EditorInspectorShaderModePlugin::parse_begin(Object *p_object) {
  2754. //do none
  2755. }
  2756. 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) {
  2757. if (p_path == "mode" && p_object->is_class("VisualShader") && p_type == Variant::INT) {
  2758. EditorPropertyShaderMode *editor = memnew(EditorPropertyShaderMode);
  2759. Vector<String> options = p_hint_text.split(",");
  2760. editor->setup(options);
  2761. add_property_editor(p_path, editor);
  2762. return true;
  2763. }
  2764. return false; //can be overridden, although it will most likely be last anyway
  2765. }
  2766. void EditorInspectorShaderModePlugin::parse_end() {
  2767. //do none
  2768. }
  2769. //////////////////////////////////
  2770. void VisualShaderNodePortPreview::_shader_changed() {
  2771. if (shader.is_null()) {
  2772. return;
  2773. }
  2774. Vector<VisualShader::DefaultTextureParam> default_textures;
  2775. String shader_code = shader->generate_preview_shader(type, node, port, default_textures);
  2776. Ref<Shader> preview_shader;
  2777. preview_shader.instance();
  2778. preview_shader->set_code(shader_code);
  2779. for (int i = 0; i < default_textures.size(); i++) {
  2780. preview_shader->set_default_texture_param(default_textures[i].name, default_textures[i].param);
  2781. }
  2782. Ref<ShaderMaterial> material;
  2783. material.instance();
  2784. material->set_shader(preview_shader);
  2785. //find if a material is also being edited and copy parameters to this one
  2786. for (int i = EditorNode::get_singleton()->get_editor_history()->get_path_size() - 1; i >= 0; i--) {
  2787. Object *object = ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_path_object(i));
  2788. ShaderMaterial *src_mat;
  2789. if (!object) {
  2790. continue;
  2791. }
  2792. if (object->has_method("get_material_override")) { // trying getting material from MeshInstance
  2793. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material_override"));
  2794. } else if (object->has_method("get_material")) { // from CanvasItem/Node2D
  2795. src_mat = Object::cast_to<ShaderMaterial>(object->call("get_material"));
  2796. } else {
  2797. src_mat = Object::cast_to<ShaderMaterial>(object);
  2798. }
  2799. if (src_mat && src_mat->get_shader().is_valid()) {
  2800. List<PropertyInfo> params;
  2801. src_mat->get_shader()->get_param_list(&params);
  2802. for (List<PropertyInfo>::Element *E = params.front(); E; E = E->next()) {
  2803. material->set(E->get().name, src_mat->get(E->get().name));
  2804. }
  2805. }
  2806. }
  2807. set_material(material);
  2808. }
  2809. void VisualShaderNodePortPreview::setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port) {
  2810. shader = p_shader;
  2811. shader->connect("changed", this, "_shader_changed");
  2812. type = p_type;
  2813. port = p_port;
  2814. node = p_node;
  2815. update();
  2816. _shader_changed();
  2817. }
  2818. Size2 VisualShaderNodePortPreview::get_minimum_size() const {
  2819. return Size2(100, 100) * EDSCALE;
  2820. }
  2821. void VisualShaderNodePortPreview::_notification(int p_what) {
  2822. if (p_what == NOTIFICATION_DRAW) {
  2823. Vector<Vector2> points;
  2824. Vector<Vector2> uvs;
  2825. Vector<Color> colors;
  2826. points.push_back(Vector2());
  2827. uvs.push_back(Vector2(0, 0));
  2828. colors.push_back(Color(1, 1, 1, 1));
  2829. points.push_back(Vector2(get_size().width, 0));
  2830. uvs.push_back(Vector2(1, 0));
  2831. colors.push_back(Color(1, 1, 1, 1));
  2832. points.push_back(get_size());
  2833. uvs.push_back(Vector2(1, 1));
  2834. colors.push_back(Color(1, 1, 1, 1));
  2835. points.push_back(Vector2(0, get_size().height));
  2836. uvs.push_back(Vector2(0, 1));
  2837. colors.push_back(Color(1, 1, 1, 1));
  2838. draw_primitive(points, colors, uvs);
  2839. }
  2840. }
  2841. void VisualShaderNodePortPreview::_bind_methods() {
  2842. ClassDB::bind_method("_shader_changed", &VisualShaderNodePortPreview::_shader_changed);
  2843. }
  2844. VisualShaderNodePortPreview::VisualShaderNodePortPreview() {
  2845. }
  2846. //////////////////////////////////
  2847. String VisualShaderConversionPlugin::converts_to() const {
  2848. return "Shader";
  2849. }
  2850. bool VisualShaderConversionPlugin::handles(const Ref<Resource> &p_resource) const {
  2851. Ref<VisualShader> vshader = p_resource;
  2852. return vshader.is_valid();
  2853. }
  2854. Ref<Resource> VisualShaderConversionPlugin::convert(const Ref<Resource> &p_resource) const {
  2855. Ref<VisualShader> vshader = p_resource;
  2856. ERR_FAIL_COND_V(!vshader.is_valid(), Ref<Resource>());
  2857. Ref<Shader> shader;
  2858. shader.instance();
  2859. String code = vshader->get_code();
  2860. shader->set_code(code);
  2861. return shader;
  2862. }