visual_shader.cpp 95 KB

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