visual_shader.cpp 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  1. /**************************************************************************/
  2. /* visual_shader.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.h"
  31. #include "core/vmap.h"
  32. #include "servers/visual/shader_types.h"
  33. bool VisualShaderNode::is_simple_decl() const {
  34. return simple_decl;
  35. }
  36. void VisualShaderNode::set_output_port_for_preview(int p_index) {
  37. port_preview = p_index;
  38. }
  39. int VisualShaderNode::get_output_port_for_preview() const {
  40. return port_preview;
  41. }
  42. void VisualShaderNode::set_input_port_default_value(int p_port, const Variant &p_value) {
  43. default_input_values[p_port] = p_value;
  44. emit_changed();
  45. }
  46. Variant VisualShaderNode::get_input_port_default_value(int p_port) const {
  47. if (default_input_values.has(p_port)) {
  48. return default_input_values[p_port];
  49. }
  50. return Variant();
  51. }
  52. bool VisualShaderNode::is_port_separator(int p_index) const {
  53. return false;
  54. }
  55. bool VisualShaderNode::is_output_port_connected(int p_port) const {
  56. if (connected_output_ports.has(p_port)) {
  57. return connected_output_ports[p_port] > 0;
  58. }
  59. return false;
  60. }
  61. void VisualShaderNode::set_output_port_connected(int p_port, bool p_connected) {
  62. if (p_connected) {
  63. connected_output_ports[p_port]++;
  64. } else {
  65. connected_output_ports[p_port]--;
  66. }
  67. }
  68. bool VisualShaderNode::is_input_port_connected(int p_port) const {
  69. if (connected_input_ports.has(p_port)) {
  70. return connected_input_ports[p_port];
  71. }
  72. return false;
  73. }
  74. void VisualShaderNode::set_input_port_connected(int p_port, bool p_connected) {
  75. connected_input_ports[p_port] = p_connected;
  76. }
  77. bool VisualShaderNode::is_generate_input_var(int p_port) const {
  78. return true;
  79. }
  80. bool VisualShaderNode::is_code_generated() const {
  81. return true;
  82. }
  83. bool VisualShaderNode::is_show_prop_names() const {
  84. return false;
  85. }
  86. Vector<VisualShader::DefaultTextureParam> VisualShaderNode::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  87. return Vector<VisualShader::DefaultTextureParam>();
  88. }
  89. String VisualShaderNode::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  90. return String();
  91. }
  92. String VisualShaderNode::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  93. return String();
  94. }
  95. String VisualShaderNode::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  96. return String();
  97. }
  98. Vector<StringName> VisualShaderNode::get_editable_properties() const {
  99. return Vector<StringName>();
  100. }
  101. Array VisualShaderNode::get_default_input_values() const {
  102. Array ret;
  103. for (Map<int, Variant>::Element *E = default_input_values.front(); E; E = E->next()) {
  104. ret.push_back(E->key());
  105. ret.push_back(E->get());
  106. }
  107. return ret;
  108. }
  109. void VisualShaderNode::set_default_input_values(const Array &p_values) {
  110. if (p_values.size() % 2 == 0) {
  111. for (int i = 0; i < p_values.size(); i += 2) {
  112. default_input_values[p_values[i + 0]] = p_values[i + 1];
  113. }
  114. }
  115. emit_changed();
  116. }
  117. String VisualShaderNode::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  118. return String();
  119. }
  120. String VisualShaderNode::get_input_port_default_hint(int p_port) const {
  121. return "";
  122. }
  123. void VisualShaderNode::_bind_methods() {
  124. ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
  125. ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
  126. ClassDB::bind_method(D_METHOD("set_input_port_default_value", "port", "value"), &VisualShaderNode::set_input_port_default_value);
  127. ClassDB::bind_method(D_METHOD("get_input_port_default_value", "port"), &VisualShaderNode::get_input_port_default_value);
  128. ClassDB::bind_method(D_METHOD("set_default_input_values", "values"), &VisualShaderNode::set_default_input_values);
  129. ClassDB::bind_method(D_METHOD("get_default_input_values"), &VisualShaderNode::get_default_input_values);
  130. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_port_for_preview"), "set_output_port_for_preview", "get_output_port_for_preview");
  131. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "default_input_values", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_default_input_values", "get_default_input_values");
  132. ADD_SIGNAL(MethodInfo("editor_refresh_request"));
  133. BIND_ENUM_CONSTANT(PORT_TYPE_SCALAR);
  134. BIND_ENUM_CONSTANT(PORT_TYPE_VECTOR);
  135. BIND_ENUM_CONSTANT(PORT_TYPE_BOOLEAN);
  136. BIND_ENUM_CONSTANT(PORT_TYPE_TRANSFORM);
  137. BIND_ENUM_CONSTANT(PORT_TYPE_SAMPLER);
  138. BIND_ENUM_CONSTANT(PORT_TYPE_MAX);
  139. }
  140. VisualShaderNode::VisualShaderNode() {
  141. port_preview = -1;
  142. simple_decl = true;
  143. }
  144. /////////////////////////////////////////////////////////
  145. void VisualShaderNodeCustom::update_ports() {
  146. ERR_FAIL_COND(!get_script_instance());
  147. input_ports.clear();
  148. if (get_script_instance()->has_method("_get_input_port_count")) {
  149. int input_port_count = (int)get_script_instance()->call("_get_input_port_count");
  150. bool has_name = get_script_instance()->has_method("_get_input_port_name");
  151. bool has_type = get_script_instance()->has_method("_get_input_port_type");
  152. for (int i = 0; i < input_port_count; i++) {
  153. Port port;
  154. if (has_name) {
  155. port.name = (String)get_script_instance()->call("_get_input_port_name", i);
  156. } else {
  157. port.name = "in" + itos(i);
  158. }
  159. if (has_type) {
  160. port.type = (int)get_script_instance()->call("_get_input_port_type", i);
  161. } else {
  162. port.type = (int)PortType::PORT_TYPE_SCALAR;
  163. }
  164. input_ports.push_back(port);
  165. }
  166. }
  167. output_ports.clear();
  168. if (get_script_instance()->has_method("_get_output_port_count")) {
  169. int output_port_count = (int)get_script_instance()->call("_get_output_port_count");
  170. bool has_name = get_script_instance()->has_method("_get_output_port_name");
  171. bool has_type = get_script_instance()->has_method("_get_output_port_type");
  172. for (int i = 0; i < output_port_count; i++) {
  173. Port port;
  174. if (has_name) {
  175. port.name = (String)get_script_instance()->call("_get_output_port_name", i);
  176. } else {
  177. port.name = "out" + itos(i);
  178. }
  179. if (has_type) {
  180. port.type = (int)get_script_instance()->call("_get_output_port_type", i);
  181. } else {
  182. port.type = (int)PortType::PORT_TYPE_SCALAR;
  183. }
  184. output_ports.push_back(port);
  185. }
  186. }
  187. }
  188. String VisualShaderNodeCustom::get_caption() const {
  189. ERR_FAIL_COND_V(!get_script_instance(), "");
  190. if (get_script_instance()->has_method("_get_name")) {
  191. return (String)get_script_instance()->call("_get_name");
  192. }
  193. return "Unnamed";
  194. }
  195. int VisualShaderNodeCustom::get_input_port_count() const {
  196. return input_ports.size();
  197. }
  198. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_input_port_type(int p_port) const {
  199. ERR_FAIL_INDEX_V(p_port, input_ports.size(), PORT_TYPE_SCALAR);
  200. return (PortType)input_ports[p_port].type;
  201. }
  202. String VisualShaderNodeCustom::get_input_port_name(int p_port) const {
  203. ERR_FAIL_INDEX_V(p_port, input_ports.size(), "");
  204. return input_ports[p_port].name;
  205. }
  206. int VisualShaderNodeCustom::get_output_port_count() const {
  207. return output_ports.size();
  208. }
  209. VisualShaderNodeCustom::PortType VisualShaderNodeCustom::get_output_port_type(int p_port) const {
  210. ERR_FAIL_INDEX_V(p_port, output_ports.size(), PORT_TYPE_SCALAR);
  211. return (PortType)output_ports[p_port].type;
  212. }
  213. String VisualShaderNodeCustom::get_output_port_name(int p_port) const {
  214. ERR_FAIL_INDEX_V(p_port, output_ports.size(), "");
  215. return output_ports[p_port].name;
  216. }
  217. String VisualShaderNodeCustom::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  218. ERR_FAIL_COND_V(!get_script_instance(), "");
  219. ERR_FAIL_COND_V(!get_script_instance()->has_method("_get_code"), "");
  220. Array input_vars;
  221. for (int i = 0; i < get_input_port_count(); i++) {
  222. input_vars.push_back(p_input_vars[i]);
  223. }
  224. Array output_vars;
  225. for (int i = 0; i < get_output_port_count(); i++) {
  226. output_vars.push_back(p_output_vars[i]);
  227. }
  228. String code = "\t{\n";
  229. String _code = (String)get_script_instance()->call("_get_code", input_vars, output_vars, (int)p_mode, (int)p_type);
  230. bool nend = _code.ends_with("\n");
  231. _code = _code.insert(0, "\t\t");
  232. _code = _code.replace("\n", "\n\t\t");
  233. code += _code;
  234. if (!nend) {
  235. code += "\n\t}";
  236. } else {
  237. code.remove(code.size() - 1);
  238. code += "}";
  239. }
  240. code += "\n";
  241. return code;
  242. }
  243. String VisualShaderNodeCustom::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  244. ERR_FAIL_COND_V(!get_script_instance(), "");
  245. if (get_script_instance()->has_method("_get_global_code")) {
  246. String code = "// " + get_caption() + "\n";
  247. code += (String)get_script_instance()->call("_get_global_code", (int)p_mode);
  248. code += "\n";
  249. return code;
  250. }
  251. return "";
  252. }
  253. void VisualShaderNodeCustom::set_input_port_default_value(int p_port, const Variant &p_value) {
  254. if (!is_initialized) {
  255. VisualShaderNode::set_input_port_default_value(p_port, p_value);
  256. }
  257. }
  258. void VisualShaderNodeCustom::set_default_input_values(const Array &p_values) {
  259. if (!is_initialized) {
  260. VisualShaderNode::set_default_input_values(p_values);
  261. }
  262. }
  263. void VisualShaderNodeCustom::_set_input_port_default_value(int p_port, const Variant &p_value) {
  264. VisualShaderNode::set_input_port_default_value(p_port, p_value);
  265. }
  266. bool VisualShaderNodeCustom::_is_initialized() {
  267. return is_initialized;
  268. }
  269. void VisualShaderNodeCustom::_set_initialized(bool p_enabled) {
  270. is_initialized = p_enabled;
  271. }
  272. void VisualShaderNodeCustom::_bind_methods() {
  273. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_name"));
  274. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_description"));
  275. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_category"));
  276. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_subcategory"));
  277. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_return_icon_type"));
  278. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_input_port_count"));
  279. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_input_port_type", PropertyInfo(Variant::INT, "port")));
  280. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_input_port_name", PropertyInfo(Variant::INT, "port")));
  281. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_output_port_count"));
  282. BIND_VMETHOD(MethodInfo(Variant::INT, "_get_output_port_type", PropertyInfo(Variant::INT, "port")));
  283. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_output_port_name", PropertyInfo(Variant::INT, "port")));
  284. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_code", PropertyInfo(Variant::ARRAY, "input_vars"), PropertyInfo(Variant::ARRAY, "output_vars"), PropertyInfo(Variant::INT, "mode"), PropertyInfo(Variant::INT, "type")));
  285. BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_global_code", PropertyInfo(Variant::INT, "mode")));
  286. ClassDB::bind_method(D_METHOD("_set_initialized", "enabled"), &VisualShaderNodeCustom::_set_initialized);
  287. ClassDB::bind_method(D_METHOD("_is_initialized"), &VisualShaderNodeCustom::_is_initialized);
  288. ClassDB::bind_method(D_METHOD("_set_input_port_default_value", "port", "value"), &VisualShaderNodeCustom::_set_input_port_default_value);
  289. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "initialized", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_initialized", "_is_initialized");
  290. }
  291. VisualShaderNodeCustom::VisualShaderNodeCustom() {
  292. simple_decl = false;
  293. }
  294. /////////////////////////////////////////////////////////
  295. void VisualShader::add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id) {
  296. ERR_FAIL_COND(p_node.is_null());
  297. ERR_FAIL_COND(p_id < 2);
  298. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  299. Graph *g = &graph[p_type];
  300. ERR_FAIL_COND(g->nodes.has(p_id));
  301. Node n;
  302. n.node = p_node;
  303. n.position = p_position;
  304. Ref<VisualShaderNodeUniform> uniform = n.node;
  305. if (uniform.is_valid()) {
  306. String valid_name = validate_uniform_name(uniform->get_uniform_name(), uniform);
  307. uniform->set_uniform_name(valid_name);
  308. }
  309. Ref<VisualShaderNodeInput> input = n.node;
  310. if (input.is_valid()) {
  311. input->shader_mode = shader_mode;
  312. input->shader_type = p_type;
  313. input->connect("input_type_changed", this, "_input_type_changed", varray(p_type, p_id));
  314. }
  315. n.node->connect("changed", this, "_queue_update");
  316. Ref<VisualShaderNodeCustom> custom = n.node;
  317. if (custom.is_valid()) {
  318. custom->update_ports();
  319. }
  320. g->nodes[p_id] = n;
  321. _queue_update();
  322. }
  323. void VisualShader::set_node_position(Type p_type, int p_id, const Vector2 &p_position) {
  324. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  325. Graph *g = &graph[p_type];
  326. ERR_FAIL_COND(!g->nodes.has(p_id));
  327. g->nodes[p_id].position = p_position;
  328. }
  329. Vector2 VisualShader::get_node_position(Type p_type, int p_id) const {
  330. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector2());
  331. const Graph *g = &graph[p_type];
  332. ERR_FAIL_COND_V(!g->nodes.has(p_id), Vector2());
  333. return g->nodes[p_id].position;
  334. }
  335. Ref<VisualShaderNode> VisualShader::get_node(Type p_type, int p_id) const {
  336. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Ref<VisualShaderNode>());
  337. const Graph *g = &graph[p_type];
  338. ERR_FAIL_COND_V(!g->nodes.has(p_id), Ref<VisualShaderNode>());
  339. return g->nodes[p_id].node;
  340. }
  341. Vector<int> VisualShader::get_node_list(Type p_type) const {
  342. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector<int>());
  343. const Graph *g = &graph[p_type];
  344. Vector<int> ret;
  345. for (Map<int, Node>::Element *E = g->nodes.front(); E; E = E->next()) {
  346. ret.push_back(E->key());
  347. }
  348. return ret;
  349. }
  350. int VisualShader::get_valid_node_id(Type p_type) const {
  351. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, NODE_ID_INVALID);
  352. const Graph *g = &graph[p_type];
  353. return g->nodes.size() ? MAX(2, g->nodes.back()->key() + 1) : 2;
  354. }
  355. int VisualShader::find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const {
  356. for (const Map<int, Node>::Element *E = graph[p_type].nodes.front(); E; E = E->next()) {
  357. if (E->get().node == p_node) {
  358. return E->key();
  359. }
  360. }
  361. return NODE_ID_INVALID;
  362. }
  363. void VisualShader::remove_node(Type p_type, int p_id) {
  364. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  365. ERR_FAIL_COND(p_id < 2);
  366. Graph *g = &graph[p_type];
  367. ERR_FAIL_COND(!g->nodes.has(p_id));
  368. Ref<VisualShaderNodeInput> input = g->nodes[p_id].node;
  369. if (input.is_valid()) {
  370. input->disconnect("input_type_changed", this, "_input_type_changed");
  371. }
  372. g->nodes[p_id].node->disconnect("changed", this, "_queue_update");
  373. g->nodes.erase(p_id);
  374. for (List<Connection>::Element *E = g->connections.front(); E;) {
  375. List<Connection>::Element *N = E->next();
  376. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  377. g->connections.erase(E);
  378. if (E->get().from_node == p_id) {
  379. g->nodes[E->get().to_node].prev_connected_nodes.erase(p_id);
  380. g->nodes[E->get().to_node].node->set_input_port_connected(E->get().to_port, false);
  381. }
  382. }
  383. E = N;
  384. }
  385. _queue_update();
  386. }
  387. bool VisualShader::is_node_connection(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  388. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  389. const Graph *g = &graph[p_type];
  390. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  391. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  392. return true;
  393. }
  394. }
  395. return false;
  396. }
  397. bool VisualShader::is_nodes_connected_relatively(const Graph *p_graph, int p_node, int p_target) const {
  398. bool result = false;
  399. const VisualShader::Node &node = p_graph->nodes[p_node];
  400. for (const List<int>::Element *E = node.prev_connected_nodes.front(); E; E = E->next()) {
  401. if (E->get() == p_target) {
  402. return true;
  403. }
  404. result = is_nodes_connected_relatively(p_graph, E->get(), p_target);
  405. if (result) {
  406. break;
  407. }
  408. }
  409. return result;
  410. }
  411. bool VisualShader::can_connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) const {
  412. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  413. const Graph *g = &graph[p_type];
  414. if (!g->nodes.has(p_from_node)) {
  415. return false;
  416. }
  417. if (p_from_node == p_to_node) {
  418. return false;
  419. }
  420. if (p_from_port < 0 || p_from_port >= g->nodes[p_from_node].node->get_output_port_count()) {
  421. return false;
  422. }
  423. if (!g->nodes.has(p_to_node)) {
  424. return false;
  425. }
  426. if (p_to_port < 0 || p_to_port >= g->nodes[p_to_node].node->get_input_port_count()) {
  427. return false;
  428. }
  429. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  430. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  431. if (!is_port_types_compatible(from_port_type, to_port_type)) {
  432. return false;
  433. }
  434. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  435. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  436. return false;
  437. }
  438. }
  439. if (is_nodes_connected_relatively(g, p_from_node, p_to_node)) {
  440. return false;
  441. }
  442. return true;
  443. }
  444. bool VisualShader::is_port_types_compatible(int p_a, int p_b) const {
  445. return MAX(0, p_a - 2) == (MAX(0, p_b - 2));
  446. }
  447. void VisualShader::connect_nodes_forced(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  448. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  449. Graph *g = &graph[p_type];
  450. ERR_FAIL_COND(!g->nodes.has(p_from_node));
  451. ERR_FAIL_INDEX(p_from_port, g->nodes[p_from_node].node->get_output_port_count());
  452. ERR_FAIL_COND(!g->nodes.has(p_to_node));
  453. ERR_FAIL_INDEX(p_to_port, g->nodes[p_to_node].node->get_input_port_count());
  454. Connection c;
  455. c.from_node = p_from_node;
  456. c.from_port = p_from_port;
  457. c.to_node = p_to_node;
  458. c.to_port = p_to_port;
  459. g->connections.push_back(c);
  460. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  461. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  462. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  463. _queue_update();
  464. }
  465. Error VisualShader::connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  466. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, ERR_CANT_CONNECT);
  467. Graph *g = &graph[p_type];
  468. ERR_FAIL_COND_V(!g->nodes.has(p_from_node), ERR_INVALID_PARAMETER);
  469. ERR_FAIL_INDEX_V(p_from_port, g->nodes[p_from_node].node->get_output_port_count(), ERR_INVALID_PARAMETER);
  470. ERR_FAIL_COND_V(!g->nodes.has(p_to_node), ERR_INVALID_PARAMETER);
  471. ERR_FAIL_INDEX_V(p_to_port, g->nodes[p_to_node].node->get_input_port_count(), ERR_INVALID_PARAMETER);
  472. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  473. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  474. ERR_FAIL_COND_V_MSG(!is_port_types_compatible(from_port_type, to_port_type), ERR_INVALID_PARAMETER, "Incompatible port types (scalar/vec/bool) with transform.");
  475. for (List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  476. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  477. ERR_FAIL_V(ERR_ALREADY_EXISTS);
  478. }
  479. }
  480. Connection c;
  481. c.from_node = p_from_node;
  482. c.from_port = p_from_port;
  483. c.to_node = p_to_node;
  484. c.to_port = p_to_port;
  485. g->connections.push_back(c);
  486. g->nodes[p_to_node].prev_connected_nodes.push_back(p_from_node);
  487. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, true);
  488. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, true);
  489. _queue_update();
  490. return OK;
  491. }
  492. void VisualShader::disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  493. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  494. Graph *g = &graph[p_type];
  495. for (List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  496. if (E->get().from_node == p_from_node && E->get().from_port == p_from_port && E->get().to_node == p_to_node && E->get().to_port == p_to_port) {
  497. g->connections.erase(E);
  498. g->nodes[p_to_node].prev_connected_nodes.erase(p_from_node);
  499. g->nodes[p_from_node].node->set_output_port_connected(p_from_port, false);
  500. g->nodes[p_to_node].node->set_input_port_connected(p_to_port, false);
  501. _queue_update();
  502. return;
  503. }
  504. }
  505. }
  506. Array VisualShader::_get_node_connections(Type p_type) const {
  507. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Array());
  508. const Graph *g = &graph[p_type];
  509. Array ret;
  510. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  511. Dictionary d;
  512. d["from_node"] = E->get().from_node;
  513. d["from_port"] = E->get().from_port;
  514. d["to_node"] = E->get().to_node;
  515. d["to_port"] = E->get().to_port;
  516. ret.push_back(d);
  517. }
  518. return ret;
  519. }
  520. void VisualShader::get_node_connections(Type p_type, List<Connection> *r_connections) const {
  521. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  522. const Graph *g = &graph[p_type];
  523. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  524. r_connections->push_back(E->get());
  525. }
  526. }
  527. void VisualShader::set_mode(Mode p_mode) {
  528. ERR_FAIL_INDEX_MSG(p_mode, Mode::MODE_MAX, vformat("Invalid shader mode: %d.", p_mode));
  529. if (shader_mode == p_mode) {
  530. return;
  531. }
  532. //erase input/output connections
  533. modes.clear();
  534. flags.clear();
  535. shader_mode = p_mode;
  536. for (int i = 0; i < TYPE_MAX; i++) {
  537. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  538. Ref<VisualShaderNodeInput> input = E->get().node;
  539. if (input.is_valid()) {
  540. input->shader_mode = shader_mode;
  541. //input->input_index = 0;
  542. }
  543. }
  544. Ref<VisualShaderNodeOutput> output = graph[i].nodes[NODE_ID_OUTPUT].node;
  545. output->shader_mode = shader_mode;
  546. // clear connections since they are no longer valid
  547. for (List<Connection>::Element *E = graph[i].connections.front(); E;) {
  548. bool keep = true;
  549. List<Connection>::Element *N = E->next();
  550. int from = E->get().from_node;
  551. int to = E->get().to_node;
  552. if (!graph[i].nodes.has(from)) {
  553. keep = false;
  554. } else {
  555. Ref<VisualShaderNode> from_node = graph[i].nodes[from].node;
  556. if (from_node->is_class("VisualShaderNodeOutput") || from_node->is_class("VisualShaderNodeInput")) {
  557. keep = false;
  558. }
  559. }
  560. if (!graph[i].nodes.has(to)) {
  561. keep = false;
  562. } else {
  563. Ref<VisualShaderNode> to_node = graph[i].nodes[to].node;
  564. if (to_node->is_class("VisualShaderNodeOutput") || to_node->is_class("VisualShaderNodeInput")) {
  565. keep = false;
  566. }
  567. }
  568. if (!keep) {
  569. graph[i].connections.erase(E);
  570. }
  571. E = N;
  572. }
  573. }
  574. _queue_update();
  575. _change_notify();
  576. }
  577. void VisualShader::set_graph_offset(const Vector2 &p_offset) {
  578. graph_offset = p_offset;
  579. }
  580. Vector2 VisualShader::get_graph_offset() const {
  581. return graph_offset;
  582. }
  583. Shader::Mode VisualShader::get_mode() const {
  584. return shader_mode;
  585. }
  586. bool VisualShader::is_text_shader() const {
  587. return false;
  588. }
  589. String VisualShader::generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &default_tex_params) const {
  590. Ref<VisualShaderNode> node = get_node(p_type, p_node);
  591. ERR_FAIL_COND_V(!node.is_valid(), String());
  592. ERR_FAIL_COND_V(p_port < 0 || p_port >= node->get_output_port_count(), String());
  593. ERR_FAIL_COND_V(node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_TRANSFORM, String());
  594. StringBuilder global_code;
  595. StringBuilder global_code_per_node;
  596. Map<Type, StringBuilder> global_code_per_func;
  597. StringBuilder code;
  598. Set<StringName> classes;
  599. global_code += String() + "shader_type canvas_item;\n";
  600. String global_expressions;
  601. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  602. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  603. Ref<VisualShaderNodeGlobalExpression> global_expression = Object::cast_to<VisualShaderNodeGlobalExpression>(E->get().node.ptr());
  604. if (global_expression.is_valid()) {
  605. String expr = "";
  606. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  607. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  608. expr = expr.replace("\n", "\n\t");
  609. expr += "\n";
  610. global_expressions += expr;
  611. }
  612. }
  613. }
  614. global_code += "\n";
  615. global_code += global_expressions;
  616. //make it faster to go around through shader
  617. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  618. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  619. for (const List<Connection>::Element *E = graph[p_type].connections.front(); E; E = E->next()) {
  620. ConnectionKey from_key;
  621. from_key.node = E->get().from_node;
  622. from_key.port = E->get().from_port;
  623. output_connections.insert(from_key, E);
  624. ConnectionKey to_key;
  625. to_key.node = E->get().to_node;
  626. to_key.port = E->get().to_port;
  627. input_connections.insert(to_key, E);
  628. }
  629. code += "\nvoid fragment() {\n";
  630. Set<int> processed;
  631. Error err = _write_node(p_type, global_code, global_code_per_node, global_code_per_func, code, default_tex_params, input_connections, output_connections, p_node, processed, true, classes);
  632. ERR_FAIL_COND_V(err != OK, String());
  633. if (node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_SCALAR) {
  634. code += "\tCOLOR.rgb = vec3( n_out" + itos(p_node) + "p" + itos(p_port) + " );\n";
  635. } else if (node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_BOOLEAN) {
  636. code += "\tCOLOR.rgb = vec3( n_out" + itos(p_node) + "p" + itos(p_port) + " ? 1.0 : 0.0 );\n";
  637. } else {
  638. code += "\tCOLOR.rgb = n_out" + itos(p_node) + "p" + itos(p_port) + ";\n";
  639. }
  640. code += "}\n";
  641. //set code secretly
  642. global_code += "\n\n";
  643. String final_code = global_code;
  644. final_code += global_code_per_node;
  645. final_code += code;
  646. return final_code;
  647. }
  648. #define IS_INITIAL_CHAR(m_d) (((m_d) >= 'a' && (m_d) <= 'z') || ((m_d) >= 'A' && (m_d) <= 'Z'))
  649. #define IS_SYMBOL_CHAR(m_d) (((m_d) >= 'a' && (m_d) <= 'z') || ((m_d) >= 'A' && (m_d) <= 'Z') || ((m_d) >= '0' && (m_d) <= '9') || (m_d) == '_')
  650. String VisualShader::validate_port_name(const String &p_name, const List<String> &p_input_ports, const List<String> &p_output_ports) const {
  651. String name = p_name;
  652. while (name.length() && !IS_INITIAL_CHAR(name[0])) {
  653. name = name.substr(1, name.length() - 1);
  654. }
  655. if (name != String()) {
  656. String valid_name;
  657. for (int i = 0; i < name.length(); i++) {
  658. if (IS_SYMBOL_CHAR(name[i])) {
  659. valid_name += String::chr(name[i]);
  660. } else if (name[i] == ' ') {
  661. valid_name += "_";
  662. }
  663. }
  664. name = valid_name;
  665. }
  666. String valid_name = name;
  667. bool is_equal = false;
  668. for (int i = 0; i < p_input_ports.size(); i++) {
  669. if (name == p_input_ports[i]) {
  670. is_equal = true;
  671. break;
  672. }
  673. }
  674. if (!is_equal) {
  675. for (int i = 0; i < p_output_ports.size(); i++) {
  676. if (name == p_output_ports[i]) {
  677. is_equal = true;
  678. break;
  679. }
  680. }
  681. }
  682. if (is_equal) {
  683. name = "";
  684. }
  685. return name;
  686. }
  687. String VisualShader::validate_uniform_name(const String &p_name, const Ref<VisualShaderNodeUniform> &p_uniform) const {
  688. String name = p_name; //validate name first
  689. while (name.length() && !IS_INITIAL_CHAR(name[0])) {
  690. name = name.substr(1, name.length() - 1);
  691. }
  692. if (name != String()) {
  693. String valid_name;
  694. for (int i = 0; i < name.length(); i++) {
  695. if (IS_SYMBOL_CHAR(name[i])) {
  696. valid_name += String::chr(name[i]);
  697. } else if (name[i] == ' ') {
  698. valid_name += "_";
  699. }
  700. }
  701. name = valid_name;
  702. }
  703. if (name == String()) {
  704. name = p_uniform->get_caption();
  705. }
  706. int attempt = 1;
  707. while (true) {
  708. bool exists = false;
  709. for (int i = 0; i < TYPE_MAX; i++) {
  710. for (const Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  711. Ref<VisualShaderNodeUniform> node = E->get().node;
  712. if (node == p_uniform) { //do not test on self
  713. continue;
  714. }
  715. if (node.is_valid() && node->get_uniform_name() == name) {
  716. exists = true;
  717. break;
  718. }
  719. }
  720. if (exists) {
  721. break;
  722. }
  723. }
  724. if (exists) {
  725. //remove numbers, put new and try again
  726. attempt++;
  727. while (name.length() && name[name.length() - 1] >= '0' && name[name.length() - 1] <= '9') {
  728. name = name.substr(0, name.length() - 1);
  729. }
  730. ERR_FAIL_COND_V(name == String(), String());
  731. name += itos(attempt);
  732. } else {
  733. break;
  734. }
  735. }
  736. return name;
  737. }
  738. VisualShader::RenderModeEnums VisualShader::render_mode_enums[] = {
  739. { Shader::MODE_SPATIAL, PNAME("blend") },
  740. { Shader::MODE_SPATIAL, PNAME("depth_draw") },
  741. { Shader::MODE_SPATIAL, PNAME("cull") },
  742. { Shader::MODE_SPATIAL, PNAME("diffuse") },
  743. { Shader::MODE_SPATIAL, PNAME("specular") },
  744. { Shader::MODE_SPATIAL, PNAME("async") },
  745. { Shader::MODE_CANVAS_ITEM, PNAME("blend") },
  746. { Shader::MODE_CANVAS_ITEM, nullptr }
  747. };
  748. static const char *type_string[VisualShader::TYPE_MAX] = {
  749. "vertex",
  750. "fragment",
  751. "light"
  752. };
  753. bool VisualShader::_set(const StringName &p_name, const Variant &p_value) {
  754. String name = p_name;
  755. if (name == "mode") {
  756. set_mode(Shader::Mode(int(p_value)));
  757. return true;
  758. } else if (name.begins_with("flags/")) {
  759. StringName flag = name.get_slicec('/', 1);
  760. bool enable = p_value;
  761. if (enable) {
  762. flags.insert(flag);
  763. } else {
  764. flags.erase(flag);
  765. }
  766. _queue_update();
  767. return true;
  768. } else if (name.begins_with("modes/")) {
  769. String mode = name.get_slicec('/', 1);
  770. int value = p_value;
  771. if (value == 0) {
  772. modes.erase(mode); //means it's default anyway, so don't store it
  773. } else {
  774. modes[mode] = value;
  775. }
  776. _queue_update();
  777. return true;
  778. } else if (name.begins_with("nodes/")) {
  779. String typestr = name.get_slicec('/', 1);
  780. Type type = TYPE_VERTEX;
  781. for (int i = 0; i < TYPE_MAX; i++) {
  782. if (typestr == type_string[i]) {
  783. type = Type(i);
  784. break;
  785. }
  786. }
  787. String index = name.get_slicec('/', 2);
  788. if (index == "connections") {
  789. Vector<int> conns = p_value;
  790. if (conns.size() % 4 == 0) {
  791. for (int i = 0; i < conns.size(); i += 4) {
  792. connect_nodes_forced(type, conns[i + 0], conns[i + 1], conns[i + 2], conns[i + 3]);
  793. }
  794. }
  795. return true;
  796. }
  797. int id = index.to_int();
  798. String what = name.get_slicec('/', 3);
  799. if (what == "node") {
  800. add_node(type, p_value, Vector2(), id);
  801. return true;
  802. } else if (what == "position") {
  803. set_node_position(type, id, p_value);
  804. return true;
  805. } else if (what == "size") {
  806. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_size(p_value);
  807. return true;
  808. } else if (what == "input_ports") {
  809. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_inputs(p_value);
  810. return true;
  811. } else if (what == "output_ports") {
  812. ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->set_outputs(p_value);
  813. return true;
  814. } else if (what == "expression") {
  815. ((VisualShaderNodeExpression *)get_node(type, id).ptr())->set_expression(p_value);
  816. return true;
  817. }
  818. }
  819. return false;
  820. }
  821. bool VisualShader::_get(const StringName &p_name, Variant &r_ret) const {
  822. String name = p_name;
  823. if (name == "mode") {
  824. r_ret = get_mode();
  825. return true;
  826. } else if (name.begins_with("flags/")) {
  827. StringName flag = name.get_slicec('/', 1);
  828. r_ret = flags.has(flag);
  829. return true;
  830. } else if (name.begins_with("modes/")) {
  831. String mode = name.get_slicec('/', 1);
  832. if (modes.has(mode)) {
  833. r_ret = modes[mode];
  834. } else {
  835. r_ret = 0;
  836. }
  837. return true;
  838. } else if (name.begins_with("nodes/")) {
  839. String typestr = name.get_slicec('/', 1);
  840. Type type = TYPE_VERTEX;
  841. for (int i = 0; i < TYPE_MAX; i++) {
  842. if (typestr == type_string[i]) {
  843. type = Type(i);
  844. break;
  845. }
  846. }
  847. String index = name.get_slicec('/', 2);
  848. if (index == "connections") {
  849. Vector<int> conns;
  850. for (const List<Connection>::Element *E = graph[type].connections.front(); E; E = E->next()) {
  851. conns.push_back(E->get().from_node);
  852. conns.push_back(E->get().from_port);
  853. conns.push_back(E->get().to_node);
  854. conns.push_back(E->get().to_port);
  855. }
  856. r_ret = conns;
  857. return true;
  858. }
  859. int id = index.to_int();
  860. String what = name.get_slicec('/', 3);
  861. if (what == "node") {
  862. r_ret = get_node(type, id);
  863. return true;
  864. } else if (what == "position") {
  865. r_ret = get_node_position(type, id);
  866. return true;
  867. } else if (what == "size") {
  868. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_size();
  869. return true;
  870. } else if (what == "input_ports") {
  871. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_inputs();
  872. return true;
  873. } else if (what == "output_ports") {
  874. r_ret = ((VisualShaderNodeGroupBase *)get_node(type, id).ptr())->get_outputs();
  875. return true;
  876. } else if (what == "expression") {
  877. r_ret = ((VisualShaderNodeExpression *)get_node(type, id).ptr())->get_expression();
  878. return true;
  879. }
  880. }
  881. return false;
  882. }
  883. void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const {
  884. //mode
  885. p_list->push_back(PropertyInfo(Variant::INT, PNAME("mode"), PROPERTY_HINT_ENUM, "Spatial,CanvasItem,Particles"));
  886. //render modes
  887. Map<String, String> blend_mode_enums;
  888. Set<String> toggles;
  889. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  890. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  891. int idx = 0;
  892. bool in_enum = false;
  893. while (render_mode_enums[idx].string) {
  894. if (mode.begins_with(render_mode_enums[idx].string)) {
  895. String begin = render_mode_enums[idx].string;
  896. String option = mode.replace_first(begin + "_", "");
  897. if (!blend_mode_enums.has(begin)) {
  898. blend_mode_enums[begin] = option;
  899. } else {
  900. blend_mode_enums[begin] += "," + option;
  901. }
  902. in_enum = true;
  903. break;
  904. }
  905. idx++;
  906. }
  907. if (!in_enum) {
  908. toggles.insert(mode);
  909. }
  910. }
  911. for (Map<String, String>::Element *E = blend_mode_enums.front(); E; E = E->next()) {
  912. p_list->push_back(PropertyInfo(Variant::INT, vformat("%s/%s", PNAME("modes"), E->key()), PROPERTY_HINT_ENUM, E->get()));
  913. }
  914. for (Set<String>::Element *E = toggles.front(); E; E = E->next()) {
  915. p_list->push_back(PropertyInfo(Variant::BOOL, vformat("%s/%s", PNAME("flags"), E->get())));
  916. }
  917. for (int i = 0; i < TYPE_MAX; i++) {
  918. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  919. String prop_name = "nodes/";
  920. prop_name += type_string[i];
  921. prop_name += "/" + itos(E->key());
  922. if (E->key() != NODE_ID_OUTPUT) {
  923. p_list->push_back(PropertyInfo(Variant::OBJECT, prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "VisualShaderNode", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE));
  924. }
  925. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  926. if (Object::cast_to<VisualShaderNodeGroupBase>(E->get().node.ptr()) != nullptr) {
  927. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  928. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/input_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  929. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/output_ports", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  930. }
  931. if (Object::cast_to<VisualShaderNodeExpression>(E->get().node.ptr()) != nullptr) {
  932. p_list->push_back(PropertyInfo(Variant::STRING, prop_name + "/expression", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  933. }
  934. }
  935. p_list->push_back(PropertyInfo(Variant::POOL_INT_ARRAY, "nodes/" + String(type_string[i]) + "/connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  936. }
  937. }
  938. Error VisualShader::_write_node(Type type, StringBuilder &global_code, StringBuilder &global_code_per_node, Map<Type, StringBuilder> &global_code_per_func, StringBuilder &code, Vector<VisualShader::DefaultTextureParam> &def_tex_params, const VMap<ConnectionKey, const List<Connection>::Element *> &input_connections, const VMap<ConnectionKey, const List<Connection>::Element *> &output_connections, int node, Set<int> &processed, bool for_preview, Set<StringName> &r_classes) const {
  939. const Ref<VisualShaderNode> vsnode = graph[type].nodes[node].node;
  940. //check inputs recursively first
  941. int input_count = vsnode->get_input_port_count();
  942. for (int i = 0; i < input_count; i++) {
  943. ConnectionKey ck;
  944. ck.node = node;
  945. ck.port = i;
  946. if (input_connections.has(ck)) {
  947. int from_node = input_connections[ck]->get().from_node;
  948. if (processed.has(from_node)) {
  949. continue;
  950. }
  951. Error err = _write_node(type, global_code, global_code_per_node, global_code_per_func, code, def_tex_params, input_connections, output_connections, from_node, processed, for_preview, r_classes);
  952. if (err) {
  953. return err;
  954. }
  955. }
  956. }
  957. // then this node
  958. Vector<VisualShader::DefaultTextureParam> params = vsnode->get_default_texture_parameters(type, node);
  959. for (int i = 0; i < params.size(); i++) {
  960. def_tex_params.push_back(params[i]);
  961. }
  962. Ref<VisualShaderNodeInput> input = vsnode;
  963. bool skip_global = input.is_valid() && for_preview;
  964. if (!skip_global) {
  965. Ref<VisualShaderNodeUniform> uniform = vsnode;
  966. if (!uniform.is_valid() || !uniform->is_global_code_generated()) {
  967. global_code += vsnode->generate_global(get_mode(), type, node);
  968. }
  969. String class_name = vsnode->get_class_name();
  970. if (class_name == "VisualShaderNodeCustom") {
  971. class_name = vsnode->get_script_instance()->get_script()->get_path();
  972. }
  973. if (!r_classes.has(class_name)) {
  974. global_code_per_node += vsnode->generate_global_per_node(get_mode(), type, node);
  975. for (int i = 0; i < TYPE_MAX; i++) {
  976. global_code_per_func[Type(i)] += vsnode->generate_global_per_func(get_mode(), Type(i), node);
  977. }
  978. r_classes.insert(class_name);
  979. }
  980. }
  981. if (!vsnode->is_code_generated()) { // just generate globals and ignore locals
  982. processed.insert(node);
  983. return OK;
  984. }
  985. code += "// " + vsnode->get_caption() + ":" + itos(node) + "\n";
  986. Vector<String> input_vars;
  987. input_vars.resize(vsnode->get_input_port_count());
  988. String *inputs = input_vars.ptrw();
  989. for (int i = 0; i < input_count; i++) {
  990. ConnectionKey ck;
  991. ck.node = node;
  992. ck.port = i;
  993. if (input_connections.has(ck)) {
  994. //connected to something, use that output
  995. int from_node = input_connections[ck]->get().from_node;
  996. int from_port = input_connections[ck]->get().from_port;
  997. VisualShaderNode::PortType in_type = vsnode->get_input_port_type(i);
  998. VisualShaderNode::PortType out_type = graph[type].nodes[from_node].node->get_output_port_type(from_port);
  999. String src_var = "n_out" + itos(from_node) + "p" + itos(from_port);
  1000. if (in_type == VisualShaderNode::PORT_TYPE_SAMPLER && out_type == VisualShaderNode::PORT_TYPE_SAMPLER) {
  1001. VisualShaderNode *ptr = const_cast<VisualShaderNode *>(graph[type].nodes[from_node].node.ptr());
  1002. if (ptr->has_method("get_input_real_name")) {
  1003. inputs[i] = ptr->call("get_input_real_name");
  1004. } else if (ptr->has_method("get_uniform_name")) {
  1005. inputs[i] = ptr->call("get_uniform_name");
  1006. } else {
  1007. inputs[i] = "";
  1008. }
  1009. } else if (in_type == out_type) {
  1010. inputs[i] = src_var;
  1011. } else if (in_type == VisualShaderNode::PORT_TYPE_SCALAR && out_type == VisualShaderNode::PORT_TYPE_VECTOR) {
  1012. inputs[i] = "dot(" + src_var + ", vec3(0.333333, 0.333333, 0.333333))";
  1013. } else if (in_type == VisualShaderNode::PORT_TYPE_VECTOR && out_type == VisualShaderNode::PORT_TYPE_SCALAR) {
  1014. inputs[i] = "vec3(" + src_var + ")";
  1015. } else if (in_type == VisualShaderNode::PORT_TYPE_BOOLEAN && out_type == VisualShaderNode::PORT_TYPE_VECTOR) {
  1016. inputs[i] = "all(bvec3(" + src_var + "))";
  1017. } else if (in_type == VisualShaderNode::PORT_TYPE_BOOLEAN && out_type == VisualShaderNode::PORT_TYPE_SCALAR) {
  1018. inputs[i] = src_var + " > 0.0 ? true : false";
  1019. } else if (in_type == VisualShaderNode::PORT_TYPE_SCALAR && out_type == VisualShaderNode::PORT_TYPE_BOOLEAN) {
  1020. inputs[i] = "(" + src_var + " ? 1.0 : 0.0)";
  1021. } else if (in_type == VisualShaderNode::PORT_TYPE_VECTOR && out_type == VisualShaderNode::PORT_TYPE_BOOLEAN) {
  1022. inputs[i] = "vec3(" + src_var + " ? 1.0 : 0.0)";
  1023. }
  1024. } else {
  1025. if (!vsnode->is_generate_input_var(i)) {
  1026. continue;
  1027. }
  1028. Variant defval = vsnode->get_input_port_default_value(i);
  1029. if (defval.get_type() == Variant::REAL || defval.get_type() == Variant::INT) {
  1030. float val = defval;
  1031. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1032. code += "\tfloat " + inputs[i] + " = " + vformat("%.5f", val) + ";\n";
  1033. } else if (defval.get_type() == Variant::BOOL) {
  1034. bool val = defval;
  1035. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1036. code += "\tbool " + inputs[i] + " = " + (val ? "true" : "false") + ";\n";
  1037. } else if (defval.get_type() == Variant::VECTOR3) {
  1038. Vector3 val = defval;
  1039. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1040. code += "\tvec3 " + inputs[i] + " = " + vformat("vec3(%.5f, %.5f, %.5f);\n", val.x, val.y, val.z);
  1041. } else if (defval.get_type() == Variant::TRANSFORM) {
  1042. Transform val = defval;
  1043. val.basis.transpose();
  1044. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  1045. Array values;
  1046. for (int j = 0; j < 3; j++) {
  1047. values.push_back(val.basis[j].x);
  1048. values.push_back(val.basis[j].y);
  1049. values.push_back(val.basis[j].z);
  1050. }
  1051. values.push_back(val.origin.x);
  1052. values.push_back(val.origin.y);
  1053. values.push_back(val.origin.z);
  1054. bool err = false;
  1055. code += "\tmat4 " + inputs[i] + " = " + String("mat4(vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 0.0), vec4(%.5f, %.5f, %.5f, 1.0));\n").sprintf(values, &err);
  1056. } else {
  1057. //will go empty, node is expected to know what it is doing at this point and handle it
  1058. }
  1059. }
  1060. }
  1061. int output_count = vsnode->get_output_port_count();
  1062. Vector<String> output_vars;
  1063. output_vars.resize(vsnode->get_output_port_count());
  1064. String *outputs = output_vars.ptrw();
  1065. if (vsnode->is_simple_decl()) { // less code to generate for some simple_decl nodes
  1066. for (int i = 0; i < output_count; i++) {
  1067. String var_name = "n_out" + itos(node) + "p" + itos(i);
  1068. switch (vsnode->get_output_port_type(i)) {
  1069. case VisualShaderNode::PORT_TYPE_SCALAR:
  1070. outputs[i] = "float " + var_name;
  1071. break;
  1072. case VisualShaderNode::PORT_TYPE_VECTOR:
  1073. outputs[i] = "vec3 " + var_name;
  1074. break;
  1075. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1076. outputs[i] = "bool " + var_name;
  1077. break;
  1078. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1079. outputs[i] = "mat4 " + var_name;
  1080. break;
  1081. default: {
  1082. }
  1083. }
  1084. }
  1085. } else {
  1086. for (int i = 0; i < output_count; i++) {
  1087. outputs[i] = "n_out" + itos(node) + "p" + itos(i);
  1088. switch (vsnode->get_output_port_type(i)) {
  1089. case VisualShaderNode::PORT_TYPE_SCALAR:
  1090. code += String() + "\tfloat " + outputs[i] + ";\n";
  1091. break;
  1092. case VisualShaderNode::PORT_TYPE_VECTOR:
  1093. code += String() + "\tvec3 " + outputs[i] + ";\n";
  1094. break;
  1095. case VisualShaderNode::PORT_TYPE_BOOLEAN:
  1096. code += String() + "\tbool " + outputs[i] + ";\n";
  1097. break;
  1098. case VisualShaderNode::PORT_TYPE_TRANSFORM:
  1099. code += String() + "\tmat4 " + outputs[i] + ";\n";
  1100. break;
  1101. default: {
  1102. }
  1103. }
  1104. }
  1105. }
  1106. code += vsnode->generate_code(get_mode(), type, node, inputs, outputs, for_preview);
  1107. code += "\n"; //
  1108. processed.insert(node);
  1109. return OK;
  1110. }
  1111. void VisualShader::_update_shader() const {
  1112. if (!dirty.is_set()) {
  1113. return;
  1114. }
  1115. dirty.clear();
  1116. StringBuilder global_code;
  1117. StringBuilder global_code_per_node;
  1118. Map<Type, StringBuilder> global_code_per_func;
  1119. StringBuilder code;
  1120. Vector<VisualShader::DefaultTextureParam> default_tex_params;
  1121. Set<StringName> classes;
  1122. List<int> insertion_pos;
  1123. static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles" };
  1124. global_code += String() + "shader_type " + shader_mode_str[shader_mode] + ";\n";
  1125. String render_mode;
  1126. {
  1127. //fill render mode enums
  1128. int idx = 0;
  1129. bool specular = false;
  1130. while (render_mode_enums[idx].string) {
  1131. if (shader_mode == render_mode_enums[idx].mode) {
  1132. if (shader_mode == Shader::MODE_SPATIAL) {
  1133. if (String(render_mode_enums[idx].string) == "specular") {
  1134. specular = true;
  1135. }
  1136. }
  1137. if (modes.has(render_mode_enums[idx].string) || specular) {
  1138. int which = 0;
  1139. if (modes.has(render_mode_enums[idx].string)) {
  1140. which = modes[render_mode_enums[idx].string];
  1141. }
  1142. int count = 0;
  1143. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  1144. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  1145. if (mode.begins_with(render_mode_enums[idx].string)) {
  1146. if (count == which) {
  1147. if (render_mode != String()) {
  1148. render_mode += ", ";
  1149. }
  1150. render_mode += mode;
  1151. break;
  1152. }
  1153. count++;
  1154. }
  1155. }
  1156. }
  1157. }
  1158. idx++;
  1159. }
  1160. //fill render mode flags
  1161. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  1162. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  1163. if (flags.has(mode)) {
  1164. if (render_mode != String()) {
  1165. render_mode += ", ";
  1166. }
  1167. render_mode += mode;
  1168. }
  1169. }
  1170. }
  1171. if (render_mode != String()) {
  1172. global_code += "render_mode " + render_mode + ";\n\n";
  1173. }
  1174. static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light" };
  1175. String global_expressions;
  1176. Set<String> used_uniform_names;
  1177. List<VisualShaderNodeUniform *> uniforms;
  1178. for (int i = 0, index = 0; i < TYPE_MAX; i++) {
  1179. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  1180. Ref<VisualShaderNodeGlobalExpression> global_expression = Object::cast_to<VisualShaderNodeGlobalExpression>(E->get().node.ptr());
  1181. if (global_expression.is_valid()) {
  1182. String expr = "";
  1183. expr += "// " + global_expression->get_caption() + ":" + itos(index++) + "\n";
  1184. expr += global_expression->generate_global(get_mode(), Type(i), -1);
  1185. expr = expr.replace("\n", "\n\t");
  1186. expr += "\n";
  1187. global_expressions += expr;
  1188. }
  1189. Ref<VisualShaderNodeUniformRef> uniform_ref = Object::cast_to<VisualShaderNodeUniformRef>(E->get().node.ptr());
  1190. if (uniform_ref.is_valid()) {
  1191. used_uniform_names.insert(uniform_ref->get_uniform_name());
  1192. }
  1193. Ref<VisualShaderNodeUniform> uniform = Object::cast_to<VisualShaderNodeUniform>(E->get().node.ptr());
  1194. if (uniform.is_valid()) {
  1195. uniforms.push_back(uniform.ptr());
  1196. }
  1197. }
  1198. }
  1199. for (int i = 0; i < uniforms.size(); i++) {
  1200. VisualShaderNodeUniform *uniform = uniforms[i];
  1201. if (used_uniform_names.has(uniform->get_uniform_name())) {
  1202. global_code += uniform->generate_global(get_mode(), Type(i), -1);
  1203. const_cast<VisualShaderNodeUniform *>(uniform)->set_global_code_generated(true);
  1204. } else {
  1205. const_cast<VisualShaderNodeUniform *>(uniform)->set_global_code_generated(false);
  1206. }
  1207. }
  1208. for (int i = 0; i < TYPE_MAX; i++) {
  1209. //make it faster to go around through shader
  1210. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  1211. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  1212. for (const List<Connection>::Element *E = graph[i].connections.front(); E; E = E->next()) {
  1213. ConnectionKey from_key;
  1214. from_key.node = E->get().from_node;
  1215. from_key.port = E->get().from_port;
  1216. output_connections.insert(from_key, E);
  1217. ConnectionKey to_key;
  1218. to_key.node = E->get().to_node;
  1219. to_key.port = E->get().to_port;
  1220. input_connections.insert(to_key, E);
  1221. }
  1222. code += "\nvoid " + String(func_name[i]) + "() {\n";
  1223. Set<int> processed;
  1224. Error err = _write_node(Type(i), global_code, global_code_per_node, global_code_per_func, code, default_tex_params, input_connections, output_connections, NODE_ID_OUTPUT, processed, false, classes);
  1225. ERR_FAIL_COND(err != OK);
  1226. insertion_pos.push_back(code.get_string_length());
  1227. code += "}\n";
  1228. }
  1229. //set code secretly
  1230. global_code += "\n\n";
  1231. String final_code = global_code;
  1232. final_code += global_code_per_node;
  1233. final_code += global_expressions;
  1234. String tcode = code;
  1235. for (int i = 0; i < TYPE_MAX; i++) {
  1236. tcode = tcode.insert(insertion_pos[i], global_code_per_func[Type(i)]);
  1237. }
  1238. final_code += tcode;
  1239. const_cast<VisualShader *>(this)->set_code(final_code);
  1240. for (int i = 0; i < default_tex_params.size(); i++) {
  1241. const_cast<VisualShader *>(this)->set_default_texture_param(default_tex_params[i].name, default_tex_params[i].param);
  1242. }
  1243. if (previous_code != final_code) {
  1244. const_cast<VisualShader *>(this)->emit_signal("changed");
  1245. }
  1246. previous_code = final_code;
  1247. }
  1248. void VisualShader::_queue_update() {
  1249. if (dirty.is_set()) {
  1250. return;
  1251. }
  1252. dirty.set();
  1253. call_deferred("_update_shader");
  1254. }
  1255. void VisualShader::_input_type_changed(Type p_type, int p_id) {
  1256. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  1257. //erase connections using this input, as type changed
  1258. Graph *g = &graph[p_type];
  1259. for (List<Connection>::Element *E = g->connections.front(); E;) {
  1260. List<Connection>::Element *N = E->next();
  1261. if (E->get().from_node == p_id) {
  1262. g->connections.erase(E);
  1263. g->nodes[E->get().to_node].prev_connected_nodes.erase(p_id);
  1264. }
  1265. E = N;
  1266. }
  1267. }
  1268. void VisualShader::rebuild() {
  1269. dirty.set();
  1270. _update_shader();
  1271. }
  1272. void VisualShader::_bind_methods() {
  1273. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShader::set_mode);
  1274. ClassDB::bind_method(D_METHOD("add_node", "type", "node", "position", "id"), &VisualShader::add_node);
  1275. ClassDB::bind_method(D_METHOD("get_node", "type", "id"), &VisualShader::get_node);
  1276. ClassDB::bind_method(D_METHOD("set_node_position", "type", "id", "position"), &VisualShader::set_node_position);
  1277. ClassDB::bind_method(D_METHOD("get_node_position", "type", "id"), &VisualShader::get_node_position);
  1278. ClassDB::bind_method(D_METHOD("get_node_list", "type"), &VisualShader::get_node_list);
  1279. ClassDB::bind_method(D_METHOD("get_valid_node_id", "type"), &VisualShader::get_valid_node_id);
  1280. ClassDB::bind_method(D_METHOD("remove_node", "type", "id"), &VisualShader::remove_node);
  1281. ClassDB::bind_method(D_METHOD("is_node_connection", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  1282. ClassDB::bind_method(D_METHOD("can_connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  1283. ClassDB::bind_method(D_METHOD("connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes);
  1284. ClassDB::bind_method(D_METHOD("disconnect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::disconnect_nodes);
  1285. ClassDB::bind_method(D_METHOD("connect_nodes_forced", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes_forced);
  1286. ClassDB::bind_method(D_METHOD("get_node_connections", "type"), &VisualShader::_get_node_connections);
  1287. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &VisualShader::set_graph_offset);
  1288. ClassDB::bind_method(D_METHOD("get_graph_offset"), &VisualShader::get_graph_offset);
  1289. ClassDB::bind_method(D_METHOD("_queue_update"), &VisualShader::_queue_update);
  1290. ClassDB::bind_method(D_METHOD("_update_shader"), &VisualShader::_update_shader);
  1291. ClassDB::bind_method(D_METHOD("_input_type_changed"), &VisualShader::_input_type_changed);
  1292. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_graph_offset", "get_graph_offset");
  1293. ADD_PROPERTY_DEFAULT("code", ""); // Inherited from Shader, prevents showing default code as override in docs.
  1294. BIND_ENUM_CONSTANT(TYPE_VERTEX);
  1295. BIND_ENUM_CONSTANT(TYPE_FRAGMENT);
  1296. BIND_ENUM_CONSTANT(TYPE_LIGHT);
  1297. BIND_ENUM_CONSTANT(TYPE_MAX);
  1298. BIND_CONSTANT(NODE_ID_INVALID);
  1299. BIND_CONSTANT(NODE_ID_OUTPUT);
  1300. }
  1301. VisualShader::VisualShader() {
  1302. shader_mode = Shader::MODE_SPATIAL;
  1303. for (int i = 0; i < TYPE_MAX; i++) {
  1304. Ref<VisualShaderNodeOutput> output;
  1305. output.instance();
  1306. output->shader_type = Type(i);
  1307. output->shader_mode = shader_mode;
  1308. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  1309. graph[i].nodes[NODE_ID_OUTPUT].position = Vector2(400, 150);
  1310. }
  1311. dirty.set();
  1312. }
  1313. ///////////////////////////////////////////////////////////
  1314. const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = {
  1315. // Spatial, Vertex
  1316. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  1317. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1318. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  1319. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  1320. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1321. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV2, 0.0)" },
  1322. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1323. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1324. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  1325. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  1326. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "modelview", "MODELVIEW_MATRIX" },
  1327. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "camera", "CAMERA_MATRIX" },
  1328. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  1329. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION_MATRIX" },
  1330. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection", "INV_PROJECTION_MATRIX" },
  1331. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1332. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(VIEWPORT_SIZE, 0)" },
  1333. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  1334. // Spatial, Fragment
  1335. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.xyz" },
  1336. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  1337. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1338. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  1339. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  1340. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "view", "VIEW" },
  1341. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1342. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV2, 0.0)" },
  1343. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1344. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1345. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec3(POINT_COORD, 0.0)" },
  1346. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1347. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "side", "float(FRONT_FACING ? 1.0 : 0.0)" },
  1348. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  1349. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  1350. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "camera", "CAMERA_MATRIX" },
  1351. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION_MATRIX" },
  1352. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection", "INV_PROJECTION_MATRIX" },
  1353. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1354. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(VIEWPORT_SIZE, 0.0)" },
  1355. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  1356. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_BOOLEAN, "front_facing", "FRONT_FACING" },
  1357. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "screen_texture", "SCREEN_TEXTURE" },
  1358. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "depth_texture", "DEPTH_TEXTURE" },
  1359. // Spatial, Light
  1360. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.xyz" },
  1361. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1362. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "view", "VIEW" },
  1363. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light", "LIGHT" },
  1364. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_color", "LIGHT_COLOR" },
  1365. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "attenuation", "ATTENUATION" },
  1366. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "albedo", "ALBEDO" },
  1367. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "transmission", "TRANSMISSION" },
  1368. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "diffuse", "DIFFUSE_LIGHT" },
  1369. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "specular", "SPECULAR_LIGHT" },
  1370. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  1371. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "metallic", "METALLIC" },
  1372. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  1373. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  1374. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "camera", "CAMERA_MATRIX" },
  1375. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION_MATRIX" },
  1376. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_projection", "INV_PROJECTION_MATRIX" },
  1377. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1378. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(VIEWPORT_SIZE, 0.0)" },
  1379. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_BOOLEAN, "output_is_srgb", "OUTPUT_IS_SRGB" },
  1380. // Canvas Item, Vertex
  1381. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "vec3(VERTEX, 0.0)" },
  1382. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  1383. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1384. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1385. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "modulate_color", "MODULATE.rgb" },
  1386. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "modulate_alpha", "MODULATE.a" },
  1387. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  1388. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "texture_pixel_size", "vec3(TEXTURE_PIXEL_SIZE, 1.0)" },
  1389. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  1390. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION_MATRIX" },
  1391. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "extra", "EXTRA_MATRIX" },
  1392. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1393. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "light_pass", "float(AT_LIGHT_PASS ? 1.0 : 0.0)" },
  1394. // Canvas Item, Fragment
  1395. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.xyz" },
  1396. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1397. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1398. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1399. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "modulate_color", "MODULATE.rgb" },
  1400. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "modulate_alpha", "MODULATE.a" },
  1401. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1402. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "texture_pixel_size", "vec3(TEXTURE_PIXEL_SIZE, 1.0)" },
  1403. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_pixel_size", "vec3(SCREEN_PIXEL_SIZE, 1.0)" },
  1404. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec3(POINT_COORD, 0.0)" },
  1405. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1406. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "light_pass", "float(AT_LIGHT_PASS ? 1.0 : 0.0)" },
  1407. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  1408. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "normal_texture", "NORMAL_TEXTURE" },
  1409. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SAMPLER, "screen_texture", "SCREEN_TEXTURE" },
  1410. // Canvas Item, Light
  1411. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "fragcoord", "FRAGCOORD.xyz" },
  1412. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1413. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1414. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1415. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1416. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "modulate_color", "MODULATE.rgb" },
  1417. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "modulate_alpha", "MODULATE.a" },
  1418. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_vec", "vec3(LIGHT_VEC, 0.0)" },
  1419. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_height", "LIGHT_HEIGHT" },
  1420. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_color", "LIGHT_COLOR.rgb" },
  1421. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_alpha", "LIGHT_COLOR.a" },
  1422. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_uv", "vec3(LIGHT_UV, 0.0)" },
  1423. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "shadow_color", "SHADOW_COLOR.rgb" },
  1424. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "shadow_alpha", "SHADOW_COLOR.a" },
  1425. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "shadow_vec", "vec3(SHADOW_VEC, 0.0)" },
  1426. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1427. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "texture_pixel_size", "vec3(TEXTURE_PIXEL_SIZE, 1.0)" },
  1428. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec3(POINT_COORD, 0.0)" },
  1429. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1430. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SAMPLER, "texture", "TEXTURE" },
  1431. // Particles, Vertex
  1432. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1433. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1434. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "VELOCITY" },
  1435. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "restart", "float(RESTART ? 1.0 : 0.0)" },
  1436. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "active", "float(ACTIVE ? 1.0 : 0.0)" },
  1437. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "custom", "CUSTOM.rgb" },
  1438. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "custom_alpha", "CUSTOM.a" },
  1439. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  1440. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  1441. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  1442. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "index", "float(INDEX)" },
  1443. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  1444. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1445. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  1446. };
  1447. const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = {
  1448. // Spatial, Fragment
  1449. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" },
  1450. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "vec3(0.0, 1.0, 0.0)" },
  1451. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "vec3(1.0, 0.0, 0.0)" },
  1452. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1453. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV, 0.0)" },
  1454. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  1455. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  1456. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1457. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "side", "1.0" },
  1458. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1459. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(1.0,1.0, 0.0)" },
  1460. // Spatial, Light
  1461. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" },
  1462. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1463. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "viewport_size", "vec3(1.0, 1.0, 0.0)" },
  1464. // Canvas Item, Vertex
  1465. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "vec3(VERTEX, 0.0)" },
  1466. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1467. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  1468. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  1469. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1470. // Canvas Item, Fragment
  1471. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1472. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  1473. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  1474. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1475. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1476. // Canvas Item, Light
  1477. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV, 0.0)" },
  1478. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0, 0.0, 1.0)" },
  1479. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  1480. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  1481. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV, 0.0)" },
  1482. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1483. // Particles, Vertex
  1484. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  1485. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  1486. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "vec3(0.0, 0.0, 1.0)" },
  1487. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  1488. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  1489. };
  1490. int VisualShaderNodeInput::get_input_port_count() const {
  1491. return 0;
  1492. }
  1493. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_port_type(int p_port) const {
  1494. return PORT_TYPE_SCALAR;
  1495. }
  1496. String VisualShaderNodeInput::get_input_port_name(int p_port) const {
  1497. return "";
  1498. }
  1499. int VisualShaderNodeInput::get_output_port_count() const {
  1500. return 1;
  1501. }
  1502. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_output_port_type(int p_port) const {
  1503. return get_input_type_by_name(input_name);
  1504. }
  1505. String VisualShaderNodeInput::get_output_port_name(int p_port) const {
  1506. return "";
  1507. }
  1508. String VisualShaderNodeInput::get_caption() const {
  1509. return "Input";
  1510. }
  1511. String VisualShaderNodeInput::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1512. if (get_output_port_type(0) == PORT_TYPE_SAMPLER) {
  1513. return "";
  1514. }
  1515. if (p_for_preview) {
  1516. int idx = 0;
  1517. String code;
  1518. while (preview_ports[idx].mode != Shader::MODE_MAX) {
  1519. if (preview_ports[idx].mode == shader_mode && preview_ports[idx].shader_type == shader_type && preview_ports[idx].name == input_name) {
  1520. code = "\t" + p_output_vars[0] + " = " + preview_ports[idx].string + ";\n";
  1521. break;
  1522. }
  1523. idx++;
  1524. }
  1525. if (code == String()) {
  1526. switch (get_output_port_type(0)) {
  1527. case PORT_TYPE_SCALAR: {
  1528. code = "\t" + p_output_vars[0] + " = 0.0;\n";
  1529. } break; //default (none found) is scalar
  1530. case PORT_TYPE_VECTOR: {
  1531. code = "\t" + p_output_vars[0] + " = vec3(0.0);\n";
  1532. } break; //default (none found) is scalar
  1533. case PORT_TYPE_TRANSFORM: {
  1534. code = "\t" + p_output_vars[0] + " = mat4( vec4(1.0,0.0,0.0,0.0), vec4(0.0,1.0,0.0,0.0), vec4(0.0,0.0,1.0,0.0), vec4(0.0,0.0,0.0,1.0) );\n";
  1535. } break; //default (none found) is scalar
  1536. case PORT_TYPE_BOOLEAN: {
  1537. code = "\t" + p_output_vars[0] + " = false;\n";
  1538. } break;
  1539. default:
  1540. break;
  1541. }
  1542. }
  1543. return code;
  1544. } else {
  1545. int idx = 0;
  1546. String code;
  1547. while (ports[idx].mode != Shader::MODE_MAX) {
  1548. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  1549. code = "\t" + p_output_vars[0] + " = " + ports[idx].string + ";\n";
  1550. break;
  1551. }
  1552. idx++;
  1553. }
  1554. if (code == String()) {
  1555. code = "\t" + p_output_vars[0] + " = 0.0;\n"; //default (none found) is scalar
  1556. }
  1557. return code;
  1558. }
  1559. }
  1560. void VisualShaderNodeInput::set_input_name(String p_name) {
  1561. PortType prev_type = get_input_type_by_name(input_name);
  1562. input_name = p_name;
  1563. emit_changed();
  1564. if (get_input_type_by_name(input_name) != prev_type) {
  1565. emit_signal("input_type_changed");
  1566. }
  1567. }
  1568. String VisualShaderNodeInput::get_input_name() const {
  1569. return input_name;
  1570. }
  1571. String VisualShaderNodeInput::get_input_real_name() const {
  1572. int idx = 0;
  1573. while (ports[idx].mode != Shader::MODE_MAX) {
  1574. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  1575. return String(ports[idx].string);
  1576. }
  1577. idx++;
  1578. }
  1579. return "";
  1580. }
  1581. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_type_by_name(String p_name) const {
  1582. int idx = 0;
  1583. while (ports[idx].mode != Shader::MODE_MAX) {
  1584. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == p_name) {
  1585. return ports[idx].type;
  1586. }
  1587. idx++;
  1588. }
  1589. return PORT_TYPE_SCALAR;
  1590. }
  1591. int VisualShaderNodeInput::get_input_index_count() const {
  1592. int idx = 0;
  1593. int count = 0;
  1594. while (ports[idx].mode != Shader::MODE_MAX) {
  1595. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1596. count++;
  1597. }
  1598. idx++;
  1599. }
  1600. return count;
  1601. }
  1602. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_index_type(int p_index) const {
  1603. int idx = 0;
  1604. int count = 0;
  1605. while (ports[idx].mode != Shader::MODE_MAX) {
  1606. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1607. if (count == p_index) {
  1608. return ports[idx].type;
  1609. }
  1610. count++;
  1611. }
  1612. idx++;
  1613. }
  1614. return PORT_TYPE_SCALAR;
  1615. }
  1616. String VisualShaderNodeInput::get_input_index_name(int p_index) const {
  1617. int idx = 0;
  1618. int count = 0;
  1619. while (ports[idx].mode != Shader::MODE_MAX) {
  1620. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1621. if (count == p_index) {
  1622. return ports[idx].name;
  1623. }
  1624. count++;
  1625. }
  1626. idx++;
  1627. }
  1628. return "";
  1629. }
  1630. void VisualShaderNodeInput::_validate_property(PropertyInfo &property) const {
  1631. if (property.name == "input_name") {
  1632. String port_list;
  1633. int idx = 0;
  1634. while (ports[idx].mode != Shader::MODE_MAX) {
  1635. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1636. if (port_list != String()) {
  1637. port_list += ",";
  1638. }
  1639. port_list += ports[idx].name;
  1640. }
  1641. idx++;
  1642. }
  1643. if (port_list == "") {
  1644. port_list = TTR("None");
  1645. }
  1646. property.hint_string = port_list;
  1647. }
  1648. }
  1649. Vector<StringName> VisualShaderNodeInput::get_editable_properties() const {
  1650. Vector<StringName> props;
  1651. props.push_back("input_name");
  1652. return props;
  1653. }
  1654. void VisualShaderNodeInput::_bind_methods() {
  1655. ClassDB::bind_method(D_METHOD("set_input_name", "name"), &VisualShaderNodeInput::set_input_name);
  1656. ClassDB::bind_method(D_METHOD("get_input_name"), &VisualShaderNodeInput::get_input_name);
  1657. ClassDB::bind_method(D_METHOD("get_input_real_name"), &VisualShaderNodeInput::get_input_real_name);
  1658. ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_name", PROPERTY_HINT_ENUM, ""), "set_input_name", "get_input_name");
  1659. ADD_SIGNAL(MethodInfo("input_type_changed"));
  1660. }
  1661. VisualShaderNodeInput::VisualShaderNodeInput() {
  1662. input_name = "[None]";
  1663. // changed when set
  1664. shader_type = VisualShader::TYPE_MAX;
  1665. shader_mode = Shader::MODE_MAX;
  1666. }
  1667. ////////////// UniformRef
  1668. List<VisualShaderNodeUniformRef::Uniform> uniforms;
  1669. void VisualShaderNodeUniformRef::add_uniform(const String &p_name, UniformType p_type) {
  1670. uniforms.push_back({ p_name, p_type });
  1671. }
  1672. void VisualShaderNodeUniformRef::clear_uniforms() {
  1673. uniforms.clear();
  1674. }
  1675. String VisualShaderNodeUniformRef::get_caption() const {
  1676. return "UniformRef";
  1677. }
  1678. int VisualShaderNodeUniformRef::get_input_port_count() const {
  1679. return 0;
  1680. }
  1681. VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_input_port_type(int p_port) const {
  1682. return PortType::PORT_TYPE_SCALAR;
  1683. }
  1684. String VisualShaderNodeUniformRef::get_input_port_name(int p_port) const {
  1685. return "";
  1686. }
  1687. int VisualShaderNodeUniformRef::get_output_port_count() const {
  1688. switch (uniform_type) {
  1689. case UniformType::UNIFORM_TYPE_SCALAR:
  1690. return 1;
  1691. case UniformType::UNIFORM_TYPE_BOOLEAN:
  1692. return 1;
  1693. case UniformType::UNIFORM_TYPE_VECTOR:
  1694. return 1;
  1695. case UniformType::UNIFORM_TYPE_TRANSFORM:
  1696. return 1;
  1697. case UniformType::UNIFORM_TYPE_COLOR:
  1698. return 2;
  1699. case UniformType::UNIFORM_TYPE_SAMPLER:
  1700. return 1;
  1701. default:
  1702. break;
  1703. }
  1704. return 0;
  1705. }
  1706. VisualShaderNodeUniformRef::PortType VisualShaderNodeUniformRef::get_output_port_type(int p_port) const {
  1707. switch (uniform_type) {
  1708. case UniformType::UNIFORM_TYPE_SCALAR:
  1709. return PortType::PORT_TYPE_SCALAR;
  1710. case UniformType::UNIFORM_TYPE_BOOLEAN:
  1711. return PortType::PORT_TYPE_BOOLEAN;
  1712. case UniformType::UNIFORM_TYPE_VECTOR:
  1713. return PortType::PORT_TYPE_VECTOR;
  1714. case UniformType::UNIFORM_TYPE_TRANSFORM:
  1715. return PortType::PORT_TYPE_TRANSFORM;
  1716. case UniformType::UNIFORM_TYPE_COLOR:
  1717. if (p_port == 0) {
  1718. return PortType::PORT_TYPE_VECTOR;
  1719. } else if (p_port == 1) {
  1720. return PORT_TYPE_SCALAR;
  1721. }
  1722. break;
  1723. case UniformType::UNIFORM_TYPE_SAMPLER:
  1724. return PortType::PORT_TYPE_SAMPLER;
  1725. default:
  1726. break;
  1727. }
  1728. return PORT_TYPE_SCALAR;
  1729. }
  1730. String VisualShaderNodeUniformRef::get_output_port_name(int p_port) const {
  1731. switch (uniform_type) {
  1732. case UniformType::UNIFORM_TYPE_SCALAR:
  1733. return "";
  1734. case UniformType::UNIFORM_TYPE_BOOLEAN:
  1735. return "";
  1736. case UniformType::UNIFORM_TYPE_VECTOR:
  1737. return "";
  1738. case UniformType::UNIFORM_TYPE_TRANSFORM:
  1739. return "";
  1740. case UniformType::UNIFORM_TYPE_COLOR:
  1741. if (p_port == 0) {
  1742. return "rgb";
  1743. } else if (p_port == 1) {
  1744. return "alpha";
  1745. }
  1746. break;
  1747. case UniformType::UNIFORM_TYPE_SAMPLER:
  1748. return "";
  1749. break;
  1750. default:
  1751. break;
  1752. }
  1753. return "";
  1754. }
  1755. void VisualShaderNodeUniformRef::set_uniform_name(const String &p_name) {
  1756. uniform_name = p_name;
  1757. if (p_name != "[None]") {
  1758. uniform_type = get_uniform_type_by_name(p_name);
  1759. } else {
  1760. uniform_type = UniformType::UNIFORM_TYPE_SCALAR;
  1761. }
  1762. emit_changed();
  1763. }
  1764. String VisualShaderNodeUniformRef::get_uniform_name() const {
  1765. return uniform_name;
  1766. }
  1767. int VisualShaderNodeUniformRef::get_uniforms_count() const {
  1768. return uniforms.size();
  1769. }
  1770. String VisualShaderNodeUniformRef::get_uniform_name_by_index(int p_idx) const {
  1771. if (p_idx >= 0 && p_idx < uniforms.size()) {
  1772. return uniforms[p_idx].name;
  1773. }
  1774. return "";
  1775. }
  1776. VisualShaderNodeUniformRef::UniformType VisualShaderNodeUniformRef::get_uniform_type_by_name(const String &p_name) const {
  1777. for (int i = 0; i < uniforms.size(); i++) {
  1778. if (uniforms[i].name == p_name) {
  1779. return uniforms[i].type;
  1780. }
  1781. }
  1782. return UniformType::UNIFORM_TYPE_SCALAR;
  1783. }
  1784. VisualShaderNodeUniformRef::UniformType VisualShaderNodeUniformRef::get_uniform_type_by_index(int p_idx) const {
  1785. if (p_idx >= 0 && p_idx < uniforms.size()) {
  1786. return uniforms[p_idx].type;
  1787. }
  1788. return UniformType::UNIFORM_TYPE_SCALAR;
  1789. }
  1790. String VisualShaderNodeUniformRef::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1791. switch (uniform_type) {
  1792. case UniformType::UNIFORM_TYPE_SCALAR:
  1793. if (uniform_name == "[None]") {
  1794. return "\t" + p_output_vars[0] + " = 0.0f;\n";
  1795. }
  1796. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1797. case UniformType::UNIFORM_TYPE_BOOLEAN:
  1798. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1799. case UniformType::UNIFORM_TYPE_VECTOR:
  1800. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1801. case UniformType::UNIFORM_TYPE_TRANSFORM:
  1802. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  1803. case UniformType::UNIFORM_TYPE_COLOR: {
  1804. String code = "\t" + p_output_vars[0] + " = " + get_uniform_name() + ".rgb;\n";
  1805. code += "\t" + p_output_vars[1] + " = " + get_uniform_name() + ".a;\n";
  1806. return code;
  1807. } break;
  1808. case UniformType::UNIFORM_TYPE_SAMPLER:
  1809. break;
  1810. default:
  1811. break;
  1812. }
  1813. return "";
  1814. }
  1815. void VisualShaderNodeUniformRef::_bind_methods() {
  1816. ClassDB::bind_method(D_METHOD("set_uniform_name", "name"), &VisualShaderNodeUniformRef::set_uniform_name);
  1817. ClassDB::bind_method(D_METHOD("get_uniform_name"), &VisualShaderNodeUniformRef::get_uniform_name);
  1818. ADD_PROPERTY(PropertyInfo(Variant::STRING, "uniform_name", PROPERTY_HINT_ENUM, ""), "set_uniform_name", "get_uniform_name");
  1819. }
  1820. Vector<StringName> VisualShaderNodeUniformRef::get_editable_properties() const {
  1821. Vector<StringName> props;
  1822. props.push_back("uniform_name");
  1823. return props;
  1824. }
  1825. VisualShaderNodeUniformRef::VisualShaderNodeUniformRef() {
  1826. uniform_name = "[None]";
  1827. uniform_type = UniformType::UNIFORM_TYPE_SCALAR;
  1828. }
  1829. ////////////////////////////////////////////
  1830. const VisualShaderNodeOutput::Port VisualShaderNodeOutput::ports[] = {
  1831. // Spatial, Vertex
  1832. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  1833. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1834. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  1835. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  1836. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "UV:xy" },
  1837. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "UV2:xy" },
  1838. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1839. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1840. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  1841. // Spatial, Fragment
  1842. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "albedo", "ALBEDO" },
  1843. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "ALPHA" },
  1844. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "metallic", "METALLIC" },
  1845. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  1846. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "specular", "SPECULAR" },
  1847. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "emission", "EMISSION" },
  1848. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "ao", "AO" },
  1849. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1850. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normalmap", "NORMALMAP" },
  1851. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "normalmap_depth", "NORMALMAP_DEPTH" },
  1852. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "rim", "RIM" },
  1853. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "rim_tint", "RIM_TINT" },
  1854. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "clearcoat", "CLEARCOAT" },
  1855. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "clearcoat_gloss", "CLEARCOAT_GLOSS" },
  1856. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "anisotropy", "ANISOTROPY" },
  1857. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "anisotropy_flow", "ANISOTROPY_FLOW:xy" },
  1858. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "subsurf_scatter", "SSS_STRENGTH" },
  1859. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "transmission", "TRANSMISSION" },
  1860. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha_scissor", "ALPHA_SCISSOR" },
  1861. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "ao_light_affect", "AO_LIGHT_AFFECT" },
  1862. // Spatial, Light
  1863. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "diffuse", "DIFFUSE_LIGHT" },
  1864. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "specular", "SPECULAR_LIGHT" },
  1865. // Canvas Item, Vertex
  1866. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX:xy" },
  1867. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "UV:xy" },
  1868. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1869. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1870. // Canvas Item, Fragment
  1871. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1872. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1873. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1874. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normalmap", "NORMALMAP" },
  1875. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "normalmap_depth", "NORMALMAP_DEPTH" },
  1876. // Canvas Item, Light
  1877. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light", "LIGHT.rgb" },
  1878. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_alpha", "LIGHT.a" },
  1879. // Particles, Vertex
  1880. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1881. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1882. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "VELOCITY" },
  1883. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "custom", "CUSTOM.rgb" },
  1884. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "custom_alpha", "CUSTOM.a" },
  1885. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  1886. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, nullptr, nullptr },
  1887. };
  1888. int VisualShaderNodeOutput::get_input_port_count() const {
  1889. int idx = 0;
  1890. int count = 0;
  1891. while (ports[idx].mode != Shader::MODE_MAX) {
  1892. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1893. count++;
  1894. }
  1895. idx++;
  1896. }
  1897. return count;
  1898. }
  1899. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_input_port_type(int p_port) const {
  1900. int idx = 0;
  1901. int count = 0;
  1902. while (ports[idx].mode != Shader::MODE_MAX) {
  1903. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1904. if (count == p_port) {
  1905. return ports[idx].type;
  1906. }
  1907. count++;
  1908. }
  1909. idx++;
  1910. }
  1911. return PORT_TYPE_SCALAR;
  1912. }
  1913. String VisualShaderNodeOutput::get_input_port_name(int p_port) const {
  1914. int idx = 0;
  1915. int count = 0;
  1916. while (ports[idx].mode != Shader::MODE_MAX) {
  1917. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1918. if (count == p_port) {
  1919. return String(ports[idx].name).capitalize();
  1920. }
  1921. count++;
  1922. }
  1923. idx++;
  1924. }
  1925. return String();
  1926. }
  1927. Variant VisualShaderNodeOutput::get_input_port_default_value(int p_port) const {
  1928. return Variant();
  1929. }
  1930. int VisualShaderNodeOutput::get_output_port_count() const {
  1931. return 0;
  1932. }
  1933. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_output_port_type(int p_port) const {
  1934. return PORT_TYPE_SCALAR;
  1935. }
  1936. String VisualShaderNodeOutput::get_output_port_name(int p_port) const {
  1937. return String();
  1938. }
  1939. bool VisualShaderNodeOutput::is_port_separator(int p_index) const {
  1940. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_FRAGMENT) {
  1941. String name = get_input_port_name(p_index);
  1942. return (name == "Normal" || name == "Rim" || name == "Alpha Scissor");
  1943. }
  1944. return false;
  1945. }
  1946. String VisualShaderNodeOutput::get_caption() const {
  1947. return "Output";
  1948. }
  1949. String VisualShaderNodeOutput::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1950. int idx = 0;
  1951. int count = 0;
  1952. String code;
  1953. while (ports[idx].mode != Shader::MODE_MAX) {
  1954. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1955. if (p_input_vars[count] != String()) {
  1956. String s = ports[idx].string;
  1957. if (s.find(":") != -1) {
  1958. code += "\t" + s.get_slicec(':', 0) + " = " + p_input_vars[count] + "." + s.get_slicec(':', 1) + ";\n";
  1959. } else {
  1960. code += "\t" + s + " = " + p_input_vars[count] + ";\n";
  1961. }
  1962. }
  1963. count++;
  1964. }
  1965. idx++;
  1966. }
  1967. return code;
  1968. }
  1969. VisualShaderNodeOutput::VisualShaderNodeOutput() {
  1970. }
  1971. ///////////////////////////
  1972. void VisualShaderNodeUniform::set_uniform_name(const String &p_name) {
  1973. uniform_name = p_name;
  1974. emit_signal("name_changed");
  1975. emit_changed();
  1976. }
  1977. String VisualShaderNodeUniform::get_uniform_name() const {
  1978. return uniform_name;
  1979. }
  1980. void VisualShaderNodeUniform::set_global_code_generated(bool p_enabled) {
  1981. global_code_generated = p_enabled;
  1982. }
  1983. bool VisualShaderNodeUniform::is_global_code_generated() const {
  1984. return global_code_generated;
  1985. }
  1986. void VisualShaderNodeUniform::_bind_methods() {
  1987. ClassDB::bind_method(D_METHOD("set_uniform_name", "name"), &VisualShaderNodeUniform::set_uniform_name);
  1988. ClassDB::bind_method(D_METHOD("get_uniform_name"), &VisualShaderNodeUniform::get_uniform_name);
  1989. ADD_PROPERTY(PropertyInfo(Variant::STRING, "uniform_name"), "set_uniform_name", "get_uniform_name");
  1990. }
  1991. bool VisualShaderNodeUniform::is_show_prop_names() const {
  1992. return true;
  1993. }
  1994. VisualShaderNodeUniform::VisualShaderNodeUniform() {
  1995. }
  1996. ////////////// GroupBase
  1997. String VisualShaderNodeGroupBase::get_caption() const {
  1998. return "Group";
  1999. }
  2000. void VisualShaderNodeGroupBase::set_size(const Vector2 &p_size) {
  2001. size = p_size;
  2002. }
  2003. Vector2 VisualShaderNodeGroupBase::get_size() const {
  2004. return size;
  2005. }
  2006. void VisualShaderNodeGroupBase::set_inputs(const String &p_inputs) {
  2007. if (inputs == p_inputs) {
  2008. return;
  2009. }
  2010. clear_input_ports();
  2011. inputs = p_inputs;
  2012. Vector<String> input_strings = inputs.split(";", false);
  2013. int input_port_count = input_strings.size();
  2014. for (int i = 0; i < input_port_count; i++) {
  2015. Vector<String> arr = input_strings[i].split(",");
  2016. ERR_FAIL_COND(arr.size() != 3);
  2017. int port_idx = arr[0].to_int();
  2018. int port_type = arr[1].to_int();
  2019. String port_name = arr[2];
  2020. Port port;
  2021. port.type = (PortType)port_type;
  2022. port.name = port_name;
  2023. input_ports[port_idx] = port;
  2024. }
  2025. }
  2026. String VisualShaderNodeGroupBase::get_inputs() const {
  2027. return inputs;
  2028. }
  2029. void VisualShaderNodeGroupBase::set_outputs(const String &p_outputs) {
  2030. if (outputs == p_outputs) {
  2031. return;
  2032. }
  2033. clear_output_ports();
  2034. outputs = p_outputs;
  2035. Vector<String> output_strings = outputs.split(";", false);
  2036. int output_port_count = output_strings.size();
  2037. for (int i = 0; i < output_port_count; i++) {
  2038. Vector<String> arr = output_strings[i].split(",");
  2039. ERR_FAIL_COND(arr.size() != 3);
  2040. int port_idx = arr[0].to_int();
  2041. int port_type = arr[1].to_int();
  2042. String port_name = arr[2];
  2043. Port port;
  2044. port.type = (PortType)port_type;
  2045. port.name = port_name;
  2046. output_ports[port_idx] = port;
  2047. }
  2048. }
  2049. String VisualShaderNodeGroupBase::get_outputs() const {
  2050. return outputs;
  2051. }
  2052. bool VisualShaderNodeGroupBase::is_valid_port_name(const String &p_name) const {
  2053. if (!p_name.is_valid_identifier()) {
  2054. return false;
  2055. }
  2056. for (int i = 0; i < get_input_port_count(); i++) {
  2057. if (get_input_port_name(i) == p_name) {
  2058. return false;
  2059. }
  2060. }
  2061. for (int i = 0; i < get_output_port_count(); i++) {
  2062. if (get_output_port_name(i) == p_name) {
  2063. return false;
  2064. }
  2065. }
  2066. return true;
  2067. }
  2068. void VisualShaderNodeGroupBase::add_input_port(int p_id, int p_type, const String &p_name) {
  2069. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  2070. Vector<String> inputs_strings = inputs.split(";", false);
  2071. int index = 0;
  2072. if (p_id < inputs_strings.size()) {
  2073. for (int i = 0; i < inputs_strings.size(); i++) {
  2074. if (i == p_id) {
  2075. inputs = inputs.insert(index, str);
  2076. break;
  2077. }
  2078. index += inputs_strings[i].size();
  2079. }
  2080. } else {
  2081. inputs += str;
  2082. }
  2083. inputs_strings = inputs.split(";", false);
  2084. index = 0;
  2085. for (int i = 0; i < inputs_strings.size(); i++) {
  2086. int count = 0;
  2087. for (int j = 0; j < inputs_strings[i].size(); j++) {
  2088. if (inputs_strings[i][j] == ',') {
  2089. break;
  2090. }
  2091. count++;
  2092. }
  2093. inputs.erase(index, count);
  2094. inputs = inputs.insert(index, itos(i));
  2095. index += inputs_strings[i].size();
  2096. }
  2097. _apply_port_changes();
  2098. }
  2099. void VisualShaderNodeGroupBase::remove_input_port(int p_id) {
  2100. ERR_FAIL_COND(!has_input_port(p_id));
  2101. Vector<String> inputs_strings = inputs.split(";", false);
  2102. int count = 0;
  2103. int index = 0;
  2104. for (int i = 0; i < inputs_strings.size(); i++) {
  2105. Vector<String> arr = inputs_strings[i].split(",");
  2106. if (arr[0].to_int() == p_id) {
  2107. count = inputs_strings[i].size();
  2108. break;
  2109. }
  2110. index += inputs_strings[i].size();
  2111. }
  2112. inputs.erase(index, count);
  2113. inputs_strings = inputs.split(";", false);
  2114. for (int i = p_id; i < inputs_strings.size(); i++) {
  2115. inputs = inputs.replace_first(inputs_strings[i].split(",")[0], itos(i));
  2116. }
  2117. _apply_port_changes();
  2118. }
  2119. int VisualShaderNodeGroupBase::get_input_port_count() const {
  2120. return input_ports.size();
  2121. }
  2122. bool VisualShaderNodeGroupBase::has_input_port(int p_id) const {
  2123. return input_ports.has(p_id);
  2124. }
  2125. void VisualShaderNodeGroupBase::add_output_port(int p_id, int p_type, const String &p_name) {
  2126. String str = itos(p_id) + "," + itos(p_type) + "," + p_name + ";";
  2127. Vector<String> outputs_strings = outputs.split(";", false);
  2128. int index = 0;
  2129. if (p_id < outputs_strings.size()) {
  2130. for (int i = 0; i < outputs_strings.size(); i++) {
  2131. if (i == p_id) {
  2132. outputs = outputs.insert(index, str);
  2133. break;
  2134. }
  2135. index += outputs_strings[i].size();
  2136. }
  2137. } else {
  2138. outputs += str;
  2139. }
  2140. outputs_strings = outputs.split(";", false);
  2141. index = 0;
  2142. for (int i = 0; i < outputs_strings.size(); i++) {
  2143. int count = 0;
  2144. for (int j = 0; j < outputs_strings[i].size(); j++) {
  2145. if (outputs_strings[i][j] == ',') {
  2146. break;
  2147. }
  2148. count++;
  2149. }
  2150. outputs.erase(index, count);
  2151. outputs = outputs.insert(index, itos(i));
  2152. index += outputs_strings[i].size();
  2153. }
  2154. _apply_port_changes();
  2155. }
  2156. void VisualShaderNodeGroupBase::remove_output_port(int p_id) {
  2157. ERR_FAIL_COND(!has_output_port(p_id));
  2158. Vector<String> outputs_strings = outputs.split(";", false);
  2159. int count = 0;
  2160. int index = 0;
  2161. for (int i = 0; i < outputs_strings.size(); i++) {
  2162. Vector<String> arr = outputs_strings[i].split(",");
  2163. if (arr[0].to_int() == p_id) {
  2164. count = outputs_strings[i].size();
  2165. break;
  2166. }
  2167. index += outputs_strings[i].size();
  2168. }
  2169. outputs.erase(index, count);
  2170. outputs_strings = outputs.split(";", false);
  2171. for (int i = p_id; i < outputs_strings.size(); i++) {
  2172. outputs = outputs.replace_first(outputs_strings[i].split(",")[0], itos(i));
  2173. }
  2174. _apply_port_changes();
  2175. }
  2176. int VisualShaderNodeGroupBase::get_output_port_count() const {
  2177. return output_ports.size();
  2178. }
  2179. bool VisualShaderNodeGroupBase::has_output_port(int p_id) const {
  2180. return output_ports.has(p_id);
  2181. }
  2182. void VisualShaderNodeGroupBase::clear_input_ports() {
  2183. input_ports.clear();
  2184. }
  2185. void VisualShaderNodeGroupBase::clear_output_ports() {
  2186. output_ports.clear();
  2187. }
  2188. void VisualShaderNodeGroupBase::set_input_port_type(int p_id, int p_type) {
  2189. ERR_FAIL_COND(!has_input_port(p_id));
  2190. ERR_FAIL_COND(p_type < 0 || p_type >= PORT_TYPE_MAX);
  2191. if (input_ports[p_id].type == p_type) {
  2192. return;
  2193. }
  2194. Vector<String> inputs_strings = inputs.split(";", false);
  2195. int count = 0;
  2196. int index = 0;
  2197. for (int i = 0; i < inputs_strings.size(); i++) {
  2198. Vector<String> arr = inputs_strings[i].split(",");
  2199. ERR_FAIL_COND(arr.size() != 3);
  2200. if (arr[0].to_int() == p_id) {
  2201. index += arr[0].size();
  2202. count = arr[1].size() - 1;
  2203. break;
  2204. }
  2205. index += inputs_strings[i].size();
  2206. }
  2207. inputs.erase(index, count);
  2208. inputs = inputs.insert(index, itos(p_type));
  2209. _apply_port_changes();
  2210. }
  2211. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_input_port_type(int p_id) const {
  2212. ERR_FAIL_COND_V(!input_ports.has(p_id), (PortType)0);
  2213. return input_ports[p_id].type;
  2214. }
  2215. void VisualShaderNodeGroupBase::set_input_port_name(int p_id, const String &p_name) {
  2216. ERR_FAIL_COND(!has_input_port(p_id));
  2217. ERR_FAIL_COND(!is_valid_port_name(p_name));
  2218. if (input_ports[p_id].name == p_name) {
  2219. return;
  2220. }
  2221. Vector<String> inputs_strings = inputs.split(";", false);
  2222. int count = 0;
  2223. int index = 0;
  2224. for (int i = 0; i < inputs_strings.size(); i++) {
  2225. Vector<String> arr = inputs_strings[i].split(",");
  2226. ERR_FAIL_COND(arr.size() != 3);
  2227. if (arr[0].to_int() == p_id) {
  2228. index += arr[0].size() + arr[1].size();
  2229. count = arr[2].size() - 1;
  2230. break;
  2231. }
  2232. index += inputs_strings[i].size();
  2233. }
  2234. inputs.erase(index, count);
  2235. inputs = inputs.insert(index, p_name);
  2236. _apply_port_changes();
  2237. }
  2238. String VisualShaderNodeGroupBase::get_input_port_name(int p_id) const {
  2239. ERR_FAIL_COND_V(!input_ports.has(p_id), "");
  2240. return input_ports[p_id].name;
  2241. }
  2242. void VisualShaderNodeGroupBase::set_output_port_type(int p_id, int p_type) {
  2243. ERR_FAIL_COND(!has_output_port(p_id));
  2244. ERR_FAIL_COND(p_type < 0 || p_type >= PORT_TYPE_MAX);
  2245. if (output_ports[p_id].type == p_type) {
  2246. return;
  2247. }
  2248. Vector<String> output_strings = outputs.split(";", false);
  2249. int count = 0;
  2250. int index = 0;
  2251. for (int i = 0; i < output_strings.size(); i++) {
  2252. Vector<String> arr = output_strings[i].split(",");
  2253. ERR_FAIL_COND(arr.size() != 3);
  2254. if (arr[0].to_int() == p_id) {
  2255. index += arr[0].size();
  2256. count = arr[1].size() - 1;
  2257. break;
  2258. }
  2259. index += output_strings[i].size();
  2260. }
  2261. outputs.erase(index, count);
  2262. outputs = outputs.insert(index, itos(p_type));
  2263. _apply_port_changes();
  2264. }
  2265. VisualShaderNodeGroupBase::PortType VisualShaderNodeGroupBase::get_output_port_type(int p_id) const {
  2266. ERR_FAIL_COND_V(!output_ports.has(p_id), (PortType)0);
  2267. return output_ports[p_id].type;
  2268. }
  2269. void VisualShaderNodeGroupBase::set_output_port_name(int p_id, const String &p_name) {
  2270. ERR_FAIL_COND(!has_output_port(p_id));
  2271. ERR_FAIL_COND(!is_valid_port_name(p_name));
  2272. if (output_ports[p_id].name == p_name) {
  2273. return;
  2274. }
  2275. Vector<String> output_strings = outputs.split(";", false);
  2276. int count = 0;
  2277. int index = 0;
  2278. for (int i = 0; i < output_strings.size(); i++) {
  2279. Vector<String> arr = output_strings[i].split(",");
  2280. ERR_FAIL_COND(arr.size() != 3);
  2281. if (arr[0].to_int() == p_id) {
  2282. index += arr[0].size() + arr[1].size();
  2283. count = arr[2].size() - 1;
  2284. break;
  2285. }
  2286. index += output_strings[i].size();
  2287. }
  2288. outputs.erase(index, count);
  2289. outputs = outputs.insert(index, p_name);
  2290. _apply_port_changes();
  2291. }
  2292. String VisualShaderNodeGroupBase::get_output_port_name(int p_id) const {
  2293. ERR_FAIL_COND_V(!output_ports.has(p_id), "");
  2294. return output_ports[p_id].name;
  2295. }
  2296. int VisualShaderNodeGroupBase::get_free_input_port_id() const {
  2297. return input_ports.size();
  2298. }
  2299. int VisualShaderNodeGroupBase::get_free_output_port_id() const {
  2300. return output_ports.size();
  2301. }
  2302. void VisualShaderNodeGroupBase::set_control(Control *p_control, int p_index) {
  2303. controls[p_index] = p_control;
  2304. }
  2305. Control *VisualShaderNodeGroupBase::get_control(int p_index) {
  2306. ERR_FAIL_COND_V(!controls.has(p_index), nullptr);
  2307. return controls[p_index];
  2308. }
  2309. void VisualShaderNodeGroupBase::_apply_port_changes() {
  2310. Vector<String> inputs_strings = inputs.split(";", false);
  2311. Vector<String> outputs_strings = outputs.split(";", false);
  2312. clear_input_ports();
  2313. clear_output_ports();
  2314. for (int i = 0; i < inputs_strings.size(); i++) {
  2315. Vector<String> arr = inputs_strings[i].split(",");
  2316. ERR_FAIL_COND(arr.size() != 3);
  2317. Port port;
  2318. port.type = (PortType)arr[1].to_int();
  2319. port.name = arr[2];
  2320. input_ports[i] = port;
  2321. }
  2322. for (int i = 0; i < outputs_strings.size(); i++) {
  2323. Vector<String> arr = outputs_strings[i].split(",");
  2324. ERR_FAIL_COND(arr.size() != 3);
  2325. Port port;
  2326. port.type = (PortType)arr[1].to_int();
  2327. port.name = arr[2];
  2328. output_ports[i] = port;
  2329. }
  2330. }
  2331. void VisualShaderNodeGroupBase::set_editable(bool p_enabled) {
  2332. editable = p_enabled;
  2333. }
  2334. bool VisualShaderNodeGroupBase::is_editable() const {
  2335. return editable;
  2336. }
  2337. void VisualShaderNodeGroupBase::_bind_methods() {
  2338. ClassDB::bind_method(D_METHOD("set_size", "size"), &VisualShaderNodeGroupBase::set_size);
  2339. ClassDB::bind_method(D_METHOD("get_size"), &VisualShaderNodeGroupBase::get_size);
  2340. ClassDB::bind_method(D_METHOD("set_inputs", "inputs"), &VisualShaderNodeGroupBase::set_inputs);
  2341. ClassDB::bind_method(D_METHOD("get_inputs"), &VisualShaderNodeGroupBase::get_inputs);
  2342. ClassDB::bind_method(D_METHOD("set_outputs", "outputs"), &VisualShaderNodeGroupBase::set_outputs);
  2343. ClassDB::bind_method(D_METHOD("get_outputs"), &VisualShaderNodeGroupBase::get_outputs);
  2344. ClassDB::bind_method(D_METHOD("is_valid_port_name", "name"), &VisualShaderNodeGroupBase::is_valid_port_name);
  2345. ClassDB::bind_method(D_METHOD("add_input_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_input_port);
  2346. ClassDB::bind_method(D_METHOD("remove_input_port", "id"), &VisualShaderNodeGroupBase::remove_input_port);
  2347. ClassDB::bind_method(D_METHOD("get_input_port_count"), &VisualShaderNodeGroupBase::get_input_port_count);
  2348. ClassDB::bind_method(D_METHOD("has_input_port", "id"), &VisualShaderNodeGroupBase::has_input_port);
  2349. ClassDB::bind_method(D_METHOD("clear_input_ports"), &VisualShaderNodeGroupBase::clear_input_ports);
  2350. ClassDB::bind_method(D_METHOD("add_output_port", "id", "type", "name"), &VisualShaderNodeGroupBase::add_output_port);
  2351. ClassDB::bind_method(D_METHOD("remove_output_port", "id"), &VisualShaderNodeGroupBase::remove_output_port);
  2352. ClassDB::bind_method(D_METHOD("get_output_port_count"), &VisualShaderNodeGroupBase::get_output_port_count);
  2353. ClassDB::bind_method(D_METHOD("has_output_port", "id"), &VisualShaderNodeGroupBase::has_output_port);
  2354. ClassDB::bind_method(D_METHOD("clear_output_ports"), &VisualShaderNodeGroupBase::clear_output_ports);
  2355. ClassDB::bind_method(D_METHOD("set_input_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_input_port_name);
  2356. ClassDB::bind_method(D_METHOD("set_input_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_input_port_type);
  2357. ClassDB::bind_method(D_METHOD("set_output_port_name", "id", "name"), &VisualShaderNodeGroupBase::set_output_port_name);
  2358. ClassDB::bind_method(D_METHOD("set_output_port_type", "id", "type"), &VisualShaderNodeGroupBase::set_output_port_type);
  2359. ClassDB::bind_method(D_METHOD("get_free_input_port_id"), &VisualShaderNodeGroupBase::get_free_input_port_id);
  2360. ClassDB::bind_method(D_METHOD("get_free_output_port_id"), &VisualShaderNodeGroupBase::get_free_output_port_id);
  2361. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
  2362. }
  2363. String VisualShaderNodeGroupBase::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2364. return "";
  2365. }
  2366. VisualShaderNodeGroupBase::VisualShaderNodeGroupBase() {
  2367. size = Size2(0, 0);
  2368. inputs = "";
  2369. outputs = "";
  2370. editable = false;
  2371. simple_decl = false;
  2372. }
  2373. ////////////// Expression
  2374. String VisualShaderNodeExpression::get_caption() const {
  2375. return "Expression";
  2376. }
  2377. void VisualShaderNodeExpression::set_expression(const String &p_expression) {
  2378. expression = p_expression;
  2379. }
  2380. String VisualShaderNodeExpression::get_expression() const {
  2381. return expression;
  2382. }
  2383. String VisualShaderNodeExpression::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2384. String _expression = expression;
  2385. _expression = _expression.insert(0, "\n");
  2386. _expression = _expression.replace("\n", "\n\t\t");
  2387. static Vector<String> pre_symbols;
  2388. if (pre_symbols.empty()) {
  2389. pre_symbols.push_back("\t");
  2390. pre_symbols.push_back(",");
  2391. pre_symbols.push_back(";");
  2392. pre_symbols.push_back("{");
  2393. pre_symbols.push_back("[");
  2394. pre_symbols.push_back("]");
  2395. pre_symbols.push_back("(");
  2396. pre_symbols.push_back(" ");
  2397. pre_symbols.push_back("-");
  2398. pre_symbols.push_back("*");
  2399. pre_symbols.push_back("/");
  2400. pre_symbols.push_back("+");
  2401. pre_symbols.push_back("=");
  2402. pre_symbols.push_back("&");
  2403. pre_symbols.push_back("|");
  2404. pre_symbols.push_back("!");
  2405. }
  2406. static Vector<String> post_symbols;
  2407. if (post_symbols.empty()) {
  2408. post_symbols.push_back("\t");
  2409. post_symbols.push_back("\n");
  2410. post_symbols.push_back(",");
  2411. post_symbols.push_back(";");
  2412. post_symbols.push_back("}");
  2413. post_symbols.push_back("[");
  2414. post_symbols.push_back("]");
  2415. post_symbols.push_back(")");
  2416. post_symbols.push_back(" ");
  2417. post_symbols.push_back(".");
  2418. post_symbols.push_back("-");
  2419. post_symbols.push_back("*");
  2420. post_symbols.push_back("/");
  2421. post_symbols.push_back("+");
  2422. post_symbols.push_back("=");
  2423. post_symbols.push_back("&");
  2424. post_symbols.push_back("|");
  2425. post_symbols.push_back("!");
  2426. }
  2427. for (int i = 0; i < get_input_port_count(); i++) {
  2428. for (int j = 0; j < pre_symbols.size(); j++) {
  2429. for (int k = 0; k < post_symbols.size(); k++) {
  2430. _expression = _expression.replace(pre_symbols[j] + get_input_port_name(i) + post_symbols[k], pre_symbols[j] + p_input_vars[i] + post_symbols[k]);
  2431. }
  2432. }
  2433. }
  2434. for (int i = 0; i < get_output_port_count(); i++) {
  2435. for (int j = 0; j < pre_symbols.size(); j++) {
  2436. for (int k = 0; k < post_symbols.size(); k++) {
  2437. _expression = _expression.replace(pre_symbols[j] + get_output_port_name(i) + post_symbols[k], pre_symbols[j] + p_output_vars[i] + post_symbols[k]);
  2438. }
  2439. }
  2440. }
  2441. String output_initializer;
  2442. for (int i = 0; i < get_output_port_count(); i++) {
  2443. int port_type = get_output_port_type(i);
  2444. String tk = "";
  2445. switch (port_type) {
  2446. case PORT_TYPE_SCALAR:
  2447. tk = "0.0";
  2448. break;
  2449. case PORT_TYPE_VECTOR:
  2450. tk = "vec3(0.0, 0.0, 0.0)";
  2451. break;
  2452. case PORT_TYPE_BOOLEAN:
  2453. tk = "false";
  2454. break;
  2455. case PORT_TYPE_TRANSFORM:
  2456. tk = "mat4(1.0)";
  2457. break;
  2458. default:
  2459. continue;
  2460. }
  2461. output_initializer += "\t" + p_output_vars[i] + " = " + tk + ";\n";
  2462. }
  2463. String code;
  2464. code += output_initializer;
  2465. code += "\t{";
  2466. code += _expression;
  2467. code += "\n\t}\n";
  2468. return code;
  2469. }
  2470. void VisualShaderNodeExpression::_bind_methods() {
  2471. ClassDB::bind_method(D_METHOD("set_expression", "expression"), &VisualShaderNodeExpression::set_expression);
  2472. ClassDB::bind_method(D_METHOD("get_expression"), &VisualShaderNodeExpression::get_expression);
  2473. ADD_PROPERTY(PropertyInfo(Variant::STRING, "expression"), "set_expression", "get_expression");
  2474. }
  2475. VisualShaderNodeExpression::VisualShaderNodeExpression() {
  2476. expression = "";
  2477. set_editable(true);
  2478. }
  2479. ////////////// Global Expression
  2480. String VisualShaderNodeGlobalExpression::get_caption() const {
  2481. return "GlobalExpression";
  2482. }
  2483. String VisualShaderNodeGlobalExpression::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2484. return expression;
  2485. }
  2486. VisualShaderNodeGlobalExpression::VisualShaderNodeGlobalExpression() {
  2487. set_editable(false);
  2488. }