visual_shader.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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. Vector<StringName> VisualShaderNode::get_editable_properties() const {
  59. return Vector<StringName>();
  60. }
  61. Array VisualShaderNode::_get_default_input_values() const {
  62. Array ret;
  63. for (Map<int, Variant>::Element *E = default_input_values.front(); E; E = E->next()) {
  64. ret.push_back(E->key());
  65. ret.push_back(E->get());
  66. }
  67. return ret;
  68. }
  69. void VisualShaderNode::_set_default_input_values(const Array &p_values) {
  70. if (p_values.size() % 2 == 0) {
  71. for (int i = 0; i < p_values.size(); i += 2) {
  72. default_input_values[p_values[i + 0]] = p_values[i + 1];
  73. }
  74. }
  75. emit_changed();
  76. }
  77. String VisualShaderNode::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  78. return String();
  79. }
  80. void VisualShaderNode::_bind_methods() {
  81. ClassDB::bind_method(D_METHOD("set_output_port_for_preview", "port"), &VisualShaderNode::set_output_port_for_preview);
  82. ClassDB::bind_method(D_METHOD("get_output_port_for_preview"), &VisualShaderNode::get_output_port_for_preview);
  83. ClassDB::bind_method(D_METHOD("set_input_port_default_value", "port", "value"), &VisualShaderNode::set_input_port_default_value);
  84. ClassDB::bind_method(D_METHOD("get_input_port_default_value", "port"), &VisualShaderNode::get_input_port_default_value);
  85. ClassDB::bind_method(D_METHOD("_set_default_input_values", "values"), &VisualShaderNode::_set_default_input_values);
  86. ClassDB::bind_method(D_METHOD("_get_default_input_values"), &VisualShaderNode::_get_default_input_values);
  87. ADD_PROPERTY(PropertyInfo(Variant::INT, "output_port_for_preview"), "set_output_port_for_preview", "get_output_port_for_preview");
  88. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "default_input_values", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "_set_default_input_values", "_get_default_input_values");
  89. ADD_SIGNAL(MethodInfo("editor_refresh_request"));
  90. }
  91. VisualShaderNode::VisualShaderNode() {
  92. port_preview = -1;
  93. }
  94. /////////////////////////////////////////////////////////
  95. void VisualShader::add_node(Type p_type, const Ref<VisualShaderNode> &p_node, const Vector2 &p_position, int p_id) {
  96. ERR_FAIL_COND(p_node.is_null());
  97. ERR_FAIL_COND(p_id < 2);
  98. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  99. Graph *g = &graph[p_type];
  100. ERR_FAIL_COND(g->nodes.has(p_id));
  101. Node n;
  102. n.node = p_node;
  103. n.position = p_position;
  104. Ref<VisualShaderNodeUniform> uniform = n.node;
  105. if (uniform.is_valid()) {
  106. String valid_name = validate_uniform_name(uniform->get_uniform_name(), uniform);
  107. uniform->set_uniform_name(valid_name);
  108. }
  109. Ref<VisualShaderNodeInput> input = n.node;
  110. if (input.is_valid()) {
  111. input->shader_mode = shader_mode;
  112. input->shader_type = p_type;
  113. input->connect("input_type_changed", this, "_input_type_changed", varray(p_type, p_id));
  114. }
  115. n.node->connect("changed", this, "_queue_update");
  116. g->nodes[p_id] = n;
  117. _queue_update();
  118. }
  119. void VisualShader::set_node_position(Type p_type, int p_id, const Vector2 &p_position) {
  120. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  121. Graph *g = &graph[p_type];
  122. ERR_FAIL_COND(!g->nodes.has(p_id));
  123. g->nodes[p_id].position = p_position;
  124. }
  125. Vector2 VisualShader::get_node_position(Type p_type, int p_id) const {
  126. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector2());
  127. const Graph *g = &graph[p_type];
  128. ERR_FAIL_COND_V(!g->nodes.has(p_id), Vector2());
  129. return g->nodes[p_id].position;
  130. }
  131. Ref<VisualShaderNode> VisualShader::get_node(Type p_type, int p_id) const {
  132. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Ref<VisualShaderNode>());
  133. const Graph *g = &graph[p_type];
  134. ERR_FAIL_COND_V(!g->nodes.has(p_id), Ref<VisualShaderNode>());
  135. return g->nodes[p_id].node;
  136. }
  137. Vector<int> VisualShader::get_node_list(Type p_type) const {
  138. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Vector<int>());
  139. const Graph *g = &graph[p_type];
  140. Vector<int> ret;
  141. for (Map<int, Node>::Element *E = g->nodes.front(); E; E = E->next()) {
  142. ret.push_back(E->key());
  143. }
  144. return ret;
  145. }
  146. int VisualShader::get_valid_node_id(Type p_type) const {
  147. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, NODE_ID_INVALID);
  148. const Graph *g = &graph[p_type];
  149. return g->nodes.size() ? MAX(2, g->nodes.back()->key() + 1) : 2;
  150. }
  151. int VisualShader::find_node_id(Type p_type, const Ref<VisualShaderNode> &p_node) const {
  152. for (const Map<int, Node>::Element *E = graph[p_type].nodes.front(); E; E = E->next()) {
  153. if (E->get().node == p_node)
  154. return E->key();
  155. }
  156. return NODE_ID_INVALID;
  157. }
  158. void VisualShader::remove_node(Type p_type, int p_id) {
  159. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  160. ERR_FAIL_COND(p_id < 2);
  161. Graph *g = &graph[p_type];
  162. ERR_FAIL_COND(!g->nodes.has(p_id));
  163. Ref<VisualShaderNodeInput> input = g->nodes[p_id].node;
  164. if (input.is_valid()) {
  165. input->disconnect("input_type_changed", this, "_input_type_changed");
  166. }
  167. g->nodes[p_id].node->disconnect("changed", this, "_queue_update");
  168. g->nodes.erase(p_id);
  169. for (List<Connection>::Element *E = g->connections.front(); E;) {
  170. List<Connection>::Element *N = E->next();
  171. if (E->get().from_node == p_id || E->get().to_node == p_id) {
  172. g->connections.erase(E);
  173. }
  174. E = N;
  175. }
  176. _queue_update();
  177. }
  178. 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 {
  179. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  180. const Graph *g = &graph[p_type];
  181. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  182. 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) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. 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 {
  189. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, false);
  190. const Graph *g = &graph[p_type];
  191. if (!g->nodes.has(p_from_node))
  192. return false;
  193. if (p_from_port < 0 || p_from_port >= g->nodes[p_from_node].node->get_output_port_count())
  194. return false;
  195. if (!g->nodes.has(p_to_node))
  196. return false;
  197. if (p_to_port < 0 || p_to_port >= g->nodes[p_to_node].node->get_input_port_count())
  198. return false;
  199. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  200. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  201. if (MAX(0, from_port_type - 1) != (MAX(0, to_port_type - 1))) {
  202. return false;
  203. }
  204. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  205. 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) {
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. Error VisualShader::connect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  212. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, ERR_CANT_CONNECT);
  213. Graph *g = &graph[p_type];
  214. ERR_FAIL_COND_V(!g->nodes.has(p_from_node), ERR_INVALID_PARAMETER);
  215. ERR_FAIL_INDEX_V(p_from_port, g->nodes[p_from_node].node->get_output_port_count(), ERR_INVALID_PARAMETER);
  216. ERR_FAIL_COND_V(!g->nodes.has(p_to_node), ERR_INVALID_PARAMETER);
  217. ERR_FAIL_INDEX_V(p_to_port, g->nodes[p_to_node].node->get_input_port_count(), ERR_INVALID_PARAMETER);
  218. VisualShaderNode::PortType from_port_type = g->nodes[p_from_node].node->get_output_port_type(p_from_port);
  219. VisualShaderNode::PortType to_port_type = g->nodes[p_to_node].node->get_input_port_type(p_to_port);
  220. if (MAX(0, from_port_type - 1) != (MAX(0, to_port_type - 1))) {
  221. ERR_EXPLAIN("Incompatible port types (scalar/vec with transform");
  222. ERR_FAIL_V(ERR_INVALID_PARAMETER)
  223. return ERR_INVALID_PARAMETER;
  224. }
  225. for (List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  226. 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) {
  227. ERR_FAIL_V(ERR_ALREADY_EXISTS);
  228. }
  229. }
  230. Connection c;
  231. c.from_node = p_from_node;
  232. c.from_port = p_from_port;
  233. c.to_node = p_to_node;
  234. c.to_port = p_to_port;
  235. g->connections.push_back(c);
  236. _queue_update();
  237. return OK;
  238. }
  239. void VisualShader::disconnect_nodes(Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port) {
  240. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  241. Graph *g = &graph[p_type];
  242. for (List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  243. 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) {
  244. g->connections.erase(E);
  245. _queue_update();
  246. return;
  247. }
  248. }
  249. }
  250. Array VisualShader::_get_node_connections(Type p_type) const {
  251. ERR_FAIL_INDEX_V(p_type, TYPE_MAX, Array());
  252. const Graph *g = &graph[p_type];
  253. Array ret;
  254. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  255. Dictionary d;
  256. d["from_node"] = E->get().from_node;
  257. d["from_port"] = E->get().from_port;
  258. d["to_node"] = E->get().to_node;
  259. d["to_port"] = E->get().to_port;
  260. ret.push_back(d);
  261. }
  262. return ret;
  263. }
  264. void VisualShader::get_node_connections(Type p_type, List<Connection> *r_connections) const {
  265. ERR_FAIL_INDEX(p_type, TYPE_MAX);
  266. const Graph *g = &graph[p_type];
  267. for (const List<Connection>::Element *E = g->connections.front(); E; E = E->next()) {
  268. r_connections->push_back(E->get());
  269. }
  270. }
  271. void VisualShader::set_mode(Mode p_mode) {
  272. if (shader_mode == p_mode) {
  273. return;
  274. }
  275. //erase input/output connections
  276. modes.clear();
  277. flags.clear();
  278. shader_mode = p_mode;
  279. for (int i = 0; i < TYPE_MAX; i++) {
  280. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  281. Ref<VisualShaderNodeInput> input = E->get().node;
  282. if (input.is_valid()) {
  283. input->shader_mode = shader_mode;
  284. //input->input_index = 0;
  285. }
  286. }
  287. Ref<VisualShaderNodeOutput> output = graph[i].nodes[NODE_ID_OUTPUT].node;
  288. output->shader_mode = shader_mode;
  289. // clear connections since they are no longer valid
  290. for (List<Connection>::Element *E = graph[i].connections.front(); E;) {
  291. bool keep = true;
  292. List<Connection>::Element *N = E->next();
  293. int from = E->get().from_node;
  294. int to = E->get().to_node;
  295. if (!graph[i].nodes.has(from)) {
  296. keep = false;
  297. } else {
  298. Ref<VisualShaderNode> from_node = graph[i].nodes[from].node;
  299. if (from_node->is_class("VisualShaderNodeOutput") || from_node->is_class("VisualShaderNodeInput")) {
  300. keep = false;
  301. }
  302. }
  303. if (!graph[i].nodes.has(to)) {
  304. keep = false;
  305. } else {
  306. Ref<VisualShaderNode> to_node = graph[i].nodes[to].node;
  307. if (to_node->is_class("VisualShaderNodeOutput") || to_node->is_class("VisualShaderNodeInput")) {
  308. keep = false;
  309. }
  310. }
  311. if (!keep) {
  312. graph[i].connections.erase(E);
  313. }
  314. E = N;
  315. }
  316. }
  317. _queue_update();
  318. _change_notify();
  319. }
  320. void VisualShader::set_graph_offset(const Vector2 &p_offset) {
  321. graph_offset = p_offset;
  322. }
  323. Vector2 VisualShader::get_graph_offset() const {
  324. return graph_offset;
  325. }
  326. Shader::Mode VisualShader::get_mode() const {
  327. return shader_mode;
  328. }
  329. bool VisualShader::is_text_shader() const {
  330. return false;
  331. }
  332. String VisualShader::generate_preview_shader(Type p_type, int p_node, int p_port, Vector<DefaultTextureParam> &default_tex_params) const {
  333. Ref<VisualShaderNode> node = get_node(p_type, p_node);
  334. ERR_FAIL_COND_V(!node.is_valid(), String());
  335. ERR_FAIL_COND_V(p_port < 0 || p_port >= node->get_output_port_count(), String());
  336. ERR_FAIL_COND_V(node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_TRANSFORM, String());
  337. StringBuilder global_code;
  338. StringBuilder code;
  339. global_code += String() + "shader_type canvas_item;\n";
  340. //make it faster to go around through shader
  341. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  342. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  343. for (const List<Connection>::Element *E = graph[p_type].connections.front(); E; E = E->next()) {
  344. ConnectionKey from_key;
  345. from_key.node = E->get().from_node;
  346. from_key.port = E->get().from_port;
  347. output_connections.insert(from_key, E);
  348. ConnectionKey to_key;
  349. to_key.node = E->get().to_node;
  350. to_key.port = E->get().to_port;
  351. input_connections.insert(to_key, E);
  352. }
  353. code += "\nvoid fragment() {\n";
  354. Set<int> processed;
  355. Error err = _write_node(p_type, global_code, code, default_tex_params, input_connections, output_connections, p_node, processed, true);
  356. ERR_FAIL_COND_V(err != OK, String());
  357. if (node->get_output_port_type(p_port) == VisualShaderNode::PORT_TYPE_SCALAR) {
  358. code += "\tCOLOR.rgb = vec3( n_out" + itos(p_node) + "p" + itos(p_port) + " );\n";
  359. } else {
  360. code += "\tCOLOR.rgb = n_out" + itos(p_node) + "p" + itos(p_port) + ";\n";
  361. }
  362. code += "}\n";
  363. //set code secretly
  364. global_code += "\n\n";
  365. String final_code = global_code;
  366. final_code += code;
  367. return final_code;
  368. }
  369. #define IS_INITIAL_CHAR(m_d) (((m_d) >= 'a' && (m_d) <= 'z') || ((m_d) >= 'A' && (m_d) <= 'Z'))
  370. #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) == '_')
  371. String VisualShader::validate_uniform_name(const String &p_name, const Ref<VisualShaderNodeUniform> &p_uniform) const {
  372. String name = p_name; //validate name first
  373. while (name.length() && !IS_INITIAL_CHAR(name[0])) {
  374. name = name.substr(1, name.length() - 1);
  375. }
  376. if (name != String()) {
  377. String valid_name;
  378. for (int i = 0; i < name.length(); i++) {
  379. if (IS_SYMBOL_CHAR(name[i])) {
  380. valid_name += String::chr(name[i]);
  381. } else if (name[i] == ' ') {
  382. valid_name += "_";
  383. }
  384. }
  385. name = valid_name;
  386. }
  387. if (name == String()) {
  388. name = p_uniform->get_caption();
  389. }
  390. int attempt = 1;
  391. while (true) {
  392. bool exists = false;
  393. for (int i = 0; i < TYPE_MAX; i++) {
  394. for (const Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  395. Ref<VisualShaderNodeUniform> node = E->get().node;
  396. if (node == p_uniform) { //do not test on self
  397. continue;
  398. }
  399. if (node.is_valid() && node->get_uniform_name() == name) {
  400. exists = true;
  401. break;
  402. }
  403. }
  404. if (exists) {
  405. break;
  406. }
  407. }
  408. if (exists) {
  409. //remove numbers, put new and try again
  410. attempt++;
  411. while (name.length() && name[name.length() - 1] >= '0' && name[name.length() - 1] <= '9') {
  412. name = name.substr(0, name.length() - 1);
  413. }
  414. ERR_FAIL_COND_V(name == String(), String());
  415. name += itos(attempt);
  416. } else {
  417. break;
  418. }
  419. }
  420. return name;
  421. }
  422. VisualShader::RenderModeEnums VisualShader::render_mode_enums[] = {
  423. { Shader::MODE_SPATIAL, "blend" },
  424. { Shader::MODE_SPATIAL, "depth_draw" },
  425. { Shader::MODE_SPATIAL, "cull" },
  426. { Shader::MODE_SPATIAL, "diffuse" },
  427. { Shader::MODE_SPATIAL, "specular" },
  428. { Shader::MODE_CANVAS_ITEM, "blend" },
  429. { Shader::MODE_CANVAS_ITEM, NULL }
  430. };
  431. static const char *type_string[VisualShader::TYPE_MAX] = {
  432. "vertex",
  433. "fragment",
  434. "light"
  435. };
  436. bool VisualShader::_set(const StringName &p_name, const Variant &p_value) {
  437. String name = p_name;
  438. if (name == "mode") {
  439. set_mode(Shader::Mode(int(p_value)));
  440. return true;
  441. } else if (name.begins_with("flags/")) {
  442. StringName flag = name.get_slicec('/', 1);
  443. bool enable = p_value;
  444. if (enable) {
  445. flags.insert(flag);
  446. } else {
  447. flags.erase(flag);
  448. }
  449. _queue_update();
  450. return true;
  451. } else if (name.begins_with("modes/")) {
  452. String mode = name.get_slicec('/', 1);
  453. int value = p_value;
  454. if (value == 0) {
  455. modes.erase(mode); //means its default anyway, so dont store it
  456. } else {
  457. modes[mode] = value;
  458. }
  459. _queue_update();
  460. return true;
  461. } else if (name.begins_with("nodes/")) {
  462. String typestr = name.get_slicec('/', 1);
  463. Type type = TYPE_VERTEX;
  464. for (int i = 0; i < TYPE_MAX; i++) {
  465. if (typestr == type_string[i]) {
  466. type = Type(i);
  467. break;
  468. }
  469. }
  470. String index = name.get_slicec('/', 2);
  471. if (index == "connections") {
  472. Vector<int> conns = p_value;
  473. if (conns.size() % 4 == 0) {
  474. for (int i = 0; i < conns.size(); i += 4) {
  475. connect_nodes(type, conns[i + 0], conns[i + 1], conns[i + 2], conns[i + 3]);
  476. }
  477. }
  478. return true;
  479. }
  480. int id = index.to_int();
  481. String what = name.get_slicec('/', 3);
  482. if (what == "node") {
  483. add_node(type, p_value, Vector2(), id);
  484. return true;
  485. } else if (what == "position") {
  486. set_node_position(type, id, p_value);
  487. return true;
  488. }
  489. }
  490. return false;
  491. }
  492. bool VisualShader::_get(const StringName &p_name, Variant &r_ret) const {
  493. String name = p_name;
  494. if (name == "mode") {
  495. r_ret = get_mode();
  496. return true;
  497. } else if (name.begins_with("flags/")) {
  498. StringName flag = name.get_slicec('/', 1);
  499. r_ret = flags.has(flag);
  500. return true;
  501. } else if (name.begins_with("modes/")) {
  502. String mode = name.get_slicec('/', 1);
  503. if (modes.has(mode)) {
  504. r_ret = modes[mode];
  505. } else {
  506. r_ret = 0;
  507. }
  508. return true;
  509. } else if (name.begins_with("nodes/")) {
  510. String typestr = name.get_slicec('/', 1);
  511. Type type = TYPE_VERTEX;
  512. for (int i = 0; i < TYPE_MAX; i++) {
  513. if (typestr == type_string[i]) {
  514. type = Type(i);
  515. break;
  516. }
  517. }
  518. String index = name.get_slicec('/', 2);
  519. if (index == "connections") {
  520. Vector<int> conns;
  521. for (const List<Connection>::Element *E = graph[type].connections.front(); E; E = E->next()) {
  522. conns.push_back(E->get().from_node);
  523. conns.push_back(E->get().from_port);
  524. conns.push_back(E->get().to_node);
  525. conns.push_back(E->get().to_port);
  526. }
  527. r_ret = conns;
  528. return true;
  529. }
  530. int id = index.to_int();
  531. String what = name.get_slicec('/', 3);
  532. if (what == "node") {
  533. r_ret = get_node(type, id);
  534. return true;
  535. } else if (what == "position") {
  536. r_ret = get_node_position(type, id);
  537. return true;
  538. }
  539. }
  540. return false;
  541. }
  542. void VisualShader::_get_property_list(List<PropertyInfo> *p_list) const {
  543. //mode
  544. p_list->push_back(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Spatial,CanvasItem,Particles"));
  545. //render modes
  546. Map<String, String> blend_mode_enums;
  547. Set<String> toggles;
  548. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  549. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  550. int idx = 0;
  551. bool in_enum = false;
  552. while (render_mode_enums[idx].string) {
  553. if (mode.begins_with(render_mode_enums[idx].string)) {
  554. String begin = render_mode_enums[idx].string;
  555. String option = mode.replace_first(begin + "_", "");
  556. if (!blend_mode_enums.has(begin)) {
  557. blend_mode_enums[begin] = option;
  558. } else {
  559. blend_mode_enums[begin] += "," + option;
  560. }
  561. in_enum = true;
  562. break;
  563. }
  564. idx++;
  565. }
  566. if (!in_enum) {
  567. toggles.insert(mode);
  568. }
  569. }
  570. for (Map<String, String>::Element *E = blend_mode_enums.front(); E; E = E->next()) {
  571. p_list->push_back(PropertyInfo(Variant::INT, "modes/" + E->key(), PROPERTY_HINT_ENUM, E->get()));
  572. }
  573. for (Set<String>::Element *E = toggles.front(); E; E = E->next()) {
  574. p_list->push_back(PropertyInfo(Variant::BOOL, "flags/" + E->get()));
  575. }
  576. for (int i = 0; i < TYPE_MAX; i++) {
  577. for (Map<int, Node>::Element *E = graph[i].nodes.front(); E; E = E->next()) {
  578. String prop_name = "nodes/";
  579. prop_name += type_string[i];
  580. prop_name += "/" + itos(E->key());
  581. if (E->key() != NODE_ID_OUTPUT) {
  582. 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));
  583. }
  584. p_list->push_back(PropertyInfo(Variant::VECTOR2, prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  585. }
  586. p_list->push_back(PropertyInfo(Variant::POOL_INT_ARRAY, "nodes/" + String(type_string[i]) + "/connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
  587. }
  588. }
  589. Error VisualShader::_write_node(Type type, StringBuilder &global_code, 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) const {
  590. const Ref<VisualShaderNode> vsnode = graph[type].nodes[node].node;
  591. //check inputs recursively first
  592. int input_count = vsnode->get_input_port_count();
  593. for (int i = 0; i < input_count; i++) {
  594. ConnectionKey ck;
  595. ck.node = node;
  596. ck.port = i;
  597. if (input_connections.has(ck)) {
  598. int from_node = input_connections[ck]->get().from_node;
  599. if (processed.has(from_node)) {
  600. continue;
  601. }
  602. Error err = _write_node(type, global_code, code, def_tex_params, input_connections, output_connections, from_node, processed, for_preview);
  603. if (err)
  604. return err;
  605. }
  606. }
  607. // then this node
  608. code += "// " + vsnode->get_caption() + ":" + itos(node) + "\n";
  609. Vector<String> input_vars;
  610. input_vars.resize(vsnode->get_input_port_count());
  611. String *inputs = input_vars.ptrw();
  612. for (int i = 0; i < input_count; i++) {
  613. ConnectionKey ck;
  614. ck.node = node;
  615. ck.port = i;
  616. if (input_connections.has(ck)) {
  617. //connected to something, use that output
  618. int from_node = input_connections[ck]->get().from_node;
  619. int from_port = input_connections[ck]->get().from_port;
  620. VisualShaderNode::PortType in_type = vsnode->get_input_port_type(i);
  621. VisualShaderNode::PortType out_type = graph[type].nodes[from_node].node->get_output_port_type(from_port);
  622. String src_var = "n_out" + itos(from_node) + "p" + itos(from_port);
  623. if (in_type == out_type) {
  624. inputs[i] = src_var;
  625. } else if (in_type == VisualShaderNode::PORT_TYPE_SCALAR && out_type == VisualShaderNode::PORT_TYPE_VECTOR) {
  626. inputs[i] = "dot(" + src_var + ",vec3(0.333333,0.333333,0.333333))";
  627. } else if (in_type == VisualShaderNode::PORT_TYPE_VECTOR && out_type == VisualShaderNode::PORT_TYPE_SCALAR) {
  628. inputs[i] = "vec3(" + src_var + ")";
  629. }
  630. } else {
  631. Variant defval = vsnode->get_input_port_default_value(i);
  632. if (defval.get_type() == Variant::REAL || defval.get_type() == Variant::INT) {
  633. float val = defval;
  634. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  635. code += "\tfloat " + inputs[i] + " = " + vformat("%.5f", val) + ";\n";
  636. } else if (defval.get_type() == Variant::VECTOR3) {
  637. Vector3 val = defval;
  638. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  639. code += "\tvec3 " + inputs[i] + " = " + vformat("vec3(%.5f,%.5f,%.5f);\n", val.x, val.y, val.z);
  640. } else if (defval.get_type() == Variant::TRANSFORM) {
  641. Transform val = defval;
  642. val.basis.transpose();
  643. inputs[i] = "n_in" + itos(node) + "p" + itos(i);
  644. Array values;
  645. for (int j = 0; j < 3; j++) {
  646. values.push_back(val.basis[j].x);
  647. values.push_back(val.basis[j].y);
  648. values.push_back(val.basis[j].z);
  649. }
  650. values.push_back(val.origin.x);
  651. values.push_back(val.origin.y);
  652. values.push_back(val.origin.z);
  653. bool err = false;
  654. 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);
  655. } else {
  656. //will go empty, node is expected to know what it is doing at this point and handle it
  657. }
  658. }
  659. }
  660. int output_count = vsnode->get_output_port_count();
  661. Vector<String> output_vars;
  662. output_vars.resize(vsnode->get_output_port_count());
  663. String *outputs = output_vars.ptrw();
  664. for (int i = 0; i < output_count; i++) {
  665. outputs[i] = "n_out" + itos(node) + "p" + itos(i);
  666. switch (vsnode->get_output_port_type(i)) {
  667. case VisualShaderNode::PORT_TYPE_SCALAR: code += String() + "\tfloat " + outputs[i] + ";\n"; break;
  668. case VisualShaderNode::PORT_TYPE_VECTOR: code += String() + "\tvec3 " + outputs[i] + ";\n"; break;
  669. case VisualShaderNode::PORT_TYPE_TRANSFORM: code += String() + "\tmat4 " + outputs[i] + ";\n"; break;
  670. default: {}
  671. }
  672. }
  673. Vector<VisualShader::DefaultTextureParam> params = vsnode->get_default_texture_parameters(type, node);
  674. for (int i = 0; i < params.size(); i++) {
  675. def_tex_params.push_back(params[i]);
  676. }
  677. Ref<VisualShaderNodeInput> input = vsnode;
  678. if (input.is_valid() && for_preview) {
  679. //handle for preview
  680. code += input->generate_code_for_preview(type, node, inputs, outputs);
  681. } else {
  682. //handle normally
  683. global_code += vsnode->generate_global(get_mode(), type, node);
  684. code += vsnode->generate_code(get_mode(), type, node, inputs, outputs);
  685. }
  686. code += "\n"; //
  687. processed.insert(node);
  688. return OK;
  689. }
  690. void VisualShader::_update_shader() const {
  691. if (!dirty)
  692. return;
  693. dirty = false;
  694. StringBuilder global_code;
  695. StringBuilder code;
  696. Vector<VisualShader::DefaultTextureParam> default_tex_params;
  697. static const char *shader_mode_str[Shader::MODE_MAX] = { "spatial", "canvas_item", "particles" };
  698. global_code += String() + "shader_type " + shader_mode_str[shader_mode] + ";\n";
  699. String render_mode;
  700. {
  701. //fill render mode enums
  702. int idx = 0;
  703. while (render_mode_enums[idx].string) {
  704. if (shader_mode == render_mode_enums[idx].mode) {
  705. if (modes.has(render_mode_enums[idx].string)) {
  706. int which = modes[render_mode_enums[idx].string];
  707. int count = 0;
  708. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  709. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  710. if (mode.begins_with(render_mode_enums[idx].string)) {
  711. if (count == which) {
  712. if (render_mode != String()) {
  713. render_mode += ", ";
  714. }
  715. render_mode += mode;
  716. break;
  717. }
  718. count++;
  719. }
  720. }
  721. }
  722. }
  723. idx++;
  724. }
  725. //fill render mode flags
  726. for (int i = 0; i < ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode)).size(); i++) {
  727. String mode = ShaderTypes::get_singleton()->get_modes(VisualServer::ShaderMode(shader_mode))[i];
  728. if (flags.has(mode)) {
  729. if (render_mode != String()) {
  730. render_mode += ", ";
  731. }
  732. render_mode += mode;
  733. }
  734. }
  735. }
  736. if (render_mode != String()) {
  737. global_code += "render_mode " + render_mode + ";\n\n";
  738. }
  739. static const char *func_name[TYPE_MAX] = { "vertex", "fragment", "light" };
  740. for (int i = 0; i < TYPE_MAX; i++) {
  741. //make it faster to go around through shader
  742. VMap<ConnectionKey, const List<Connection>::Element *> input_connections;
  743. VMap<ConnectionKey, const List<Connection>::Element *> output_connections;
  744. for (const List<Connection>::Element *E = graph[i].connections.front(); E; E = E->next()) {
  745. ConnectionKey from_key;
  746. from_key.node = E->get().from_node;
  747. from_key.port = E->get().from_port;
  748. output_connections.insert(from_key, E);
  749. ConnectionKey to_key;
  750. to_key.node = E->get().to_node;
  751. to_key.port = E->get().to_port;
  752. input_connections.insert(to_key, E);
  753. }
  754. code += "\nvoid " + String(func_name[i]) + "() {\n";
  755. Set<int> processed;
  756. Error err = _write_node(Type(i), global_code, code, default_tex_params, input_connections, output_connections, NODE_ID_OUTPUT, processed, false);
  757. ERR_FAIL_COND(err != OK);
  758. code += "}\n";
  759. }
  760. //set code secretly
  761. global_code += "\n\n";
  762. String final_code = global_code;
  763. final_code += code;
  764. const_cast<VisualShader *>(this)->set_code(final_code);
  765. for (int i = 0; i < default_tex_params.size(); i++) {
  766. const_cast<VisualShader *>(this)->set_default_texture_param(default_tex_params[i].name, default_tex_params[i].param);
  767. }
  768. }
  769. void VisualShader::_queue_update() {
  770. if (dirty) {
  771. return;
  772. }
  773. dirty = true;
  774. call_deferred("_update_shader");
  775. }
  776. void VisualShader::_input_type_changed(Type p_type, int p_id) {
  777. //erase connections using this input, as type changed
  778. Graph *g = &graph[p_type];
  779. for (List<Connection>::Element *E = g->connections.front(); E;) {
  780. List<Connection>::Element *N = E->next();
  781. if (E->get().from_node == p_id) {
  782. g->connections.erase(E);
  783. }
  784. E = N;
  785. }
  786. }
  787. void VisualShader::_bind_methods() {
  788. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &VisualShader::set_mode);
  789. ClassDB::bind_method(D_METHOD("add_node", "type", "node", "position", "id"), &VisualShader::add_node);
  790. ClassDB::bind_method(D_METHOD("set_node_position", "type", "id", "position"), &VisualShader::set_node_position);
  791. ClassDB::bind_method(D_METHOD("get_node", "type", "id"), &VisualShader::get_node);
  792. ClassDB::bind_method(D_METHOD("get_node_position", "type", "id"), &VisualShader::get_node_position);
  793. ClassDB::bind_method(D_METHOD("get_node_list", "type"), &VisualShader::get_node_list);
  794. ClassDB::bind_method(D_METHOD("get_valid_node_id", "type"), &VisualShader::get_valid_node_id);
  795. ClassDB::bind_method(D_METHOD("remove_node", "type", "id"), &VisualShader::remove_node);
  796. ClassDB::bind_method(D_METHOD("is_node_connection", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  797. ClassDB::bind_method(D_METHOD("can_connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::is_node_connection);
  798. ClassDB::bind_method(D_METHOD("connect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::connect_nodes);
  799. ClassDB::bind_method(D_METHOD("disconnect_nodes", "type", "from_node", "from_port", "to_node", "to_port"), &VisualShader::disconnect_nodes);
  800. ClassDB::bind_method(D_METHOD("get_node_connections", "type"), &VisualShader::_get_node_connections);
  801. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &VisualShader::set_graph_offset);
  802. ClassDB::bind_method(D_METHOD("get_graph_offset"), &VisualShader::get_graph_offset);
  803. ClassDB::bind_method(D_METHOD("_queue_update"), &VisualShader::_queue_update);
  804. ClassDB::bind_method(D_METHOD("_update_shader"), &VisualShader::_update_shader);
  805. ClassDB::bind_method(D_METHOD("_input_type_changed"), &VisualShader::_input_type_changed);
  806. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_graph_offset", "get_graph_offset");
  807. BIND_ENUM_CONSTANT(TYPE_VERTEX);
  808. BIND_ENUM_CONSTANT(TYPE_FRAGMENT);
  809. BIND_ENUM_CONSTANT(TYPE_LIGHT);
  810. BIND_ENUM_CONSTANT(TYPE_MAX);
  811. BIND_CONSTANT(NODE_ID_INVALID);
  812. BIND_CONSTANT(NODE_ID_OUTPUT);
  813. }
  814. VisualShader::VisualShader() {
  815. shader_mode = Shader::MODE_SPATIAL;
  816. for (int i = 0; i < TYPE_MAX; i++) {
  817. Ref<VisualShaderNodeOutput> output;
  818. output.instance();
  819. output->shader_type = Type(i);
  820. output->shader_mode = shader_mode;
  821. graph[i].nodes[NODE_ID_OUTPUT].node = output;
  822. graph[i].nodes[NODE_ID_OUTPUT].position = Vector2(400, 150);
  823. }
  824. dirty = true;
  825. }
  826. ///////////////////////////////////////////////////////////
  827. const VisualShaderNodeInput::Port VisualShaderNodeInput::ports[] = {
  828. // Spatial, Vertex
  829. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  830. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  831. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  832. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  833. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  834. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV2,0.0)" },
  835. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  836. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  837. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  838. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  839. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  840. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION" },
  841. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  842. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vp_size", "vec3(VIEWPORT_SIZE, 0)" },
  843. // Spatial, Fragment
  844. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  845. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  846. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  847. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  848. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  849. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV2,0.0)" },
  850. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  851. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  852. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec2(POINT_COORD,0.0)" },
  853. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV,0.0)" },
  854. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "side", "float(FRONT_FACING ? 1.0 : 0.0)" },
  855. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  856. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  857. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION" },
  858. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  859. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "vp_size", "vec3(VIEWPORT_SIZE, 0.0)" },
  860. // Spatial, Light
  861. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  862. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "view", "VIEW" },
  863. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light", "LIGHT" },
  864. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_color", "LIGHT_COLOR" },
  865. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "attenuation", "ATTENUATION" },
  866. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "albedo", "ALBEDO" },
  867. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "transmission", "TRANSMISSION" },
  868. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  869. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  870. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "inv_camera", "INV_CAMERA_MATRIX" },
  871. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION" },
  872. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  873. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "vp_size", "vec3(VIEWPORT_SIZE, 0.0)" },
  874. // Canvas Item, Vertex
  875. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "vec3(VERTEX,0.0)" },
  876. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  877. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  878. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  879. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "point_size", "POINT_SIZE" },
  880. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "world", "WORLD_MATRIX" },
  881. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "projection", "PROJECTION_MATRIX" },
  882. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "extra", "EXTRA_MATRIX" },
  883. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  884. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "light_pass", "float(AT_LIGHT_PASS ? 1.0 : 0.0)" },
  885. // Canvas Item, Fragment
  886. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  887. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  888. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  889. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV,0.0)" },
  890. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec2(POINT_COORD,0.0)" },
  891. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  892. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "light_pass", "float(AT_LIGHT_PASS ? 1.0 : 0.0)" },
  893. // Canvas Item, Light
  894. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  895. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  896. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  897. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  898. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_vec", "vec3(LIGHT_VEC,0.0)" },
  899. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_height", "LIGHT_HEIGHT" },
  900. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_color", "LIGHT_COLOR.rgb" },
  901. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_alpha", "LIGHT_COLOR.a" },
  902. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light_uv", "vec3(LIGHT_UV,0.0)" },
  903. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "shadow_color", "SHADOW_COLOR.rgb" },
  904. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(SCREEN_UV,0.0)" },
  905. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "point_coord", "vec2(POINT_COORD,0.0)" },
  906. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  907. // Particles, Vertex
  908. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  909. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  910. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "VELOCITY" },
  911. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "restart", "float(RESTART ? 1.0 : 0.0)" },
  912. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "active", "float(ACTIVE ? 1.0 : 0.0)" },
  913. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "custom", "CUSTOM.rgb" },
  914. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "custom_alpha", "CUSTOM.a" },
  915. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  916. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "delta", "DELTA" },
  917. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "lifetime", "LIFETIME" },
  918. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "index", "float(INDEX)" },
  919. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "emission_transform", "EMISSION_TRANSFORM" },
  920. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  921. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, NULL, NULL },
  922. };
  923. const VisualShaderNodeInput::Port VisualShaderNodeInput::preview_ports[] = {
  924. // Spatial, Fragment
  925. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0,0.0,1.0)" },
  926. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "vec3(0.0,1.0,0.0)" },
  927. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "vec3(1.0,0.0,0.0)" },
  928. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  929. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "vec3(UV,0.0)" },
  930. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  931. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  932. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(UV,0.0)" },
  933. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "side", "1.0" },
  934. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  935. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "vp_size", "vec3(1.0,1.0, 0.0)" },
  936. // Spatial, Light
  937. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0,0.0,1.0)" },
  938. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  939. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "vp_size", "vec3(1.0, 1.0, 0.0)" },
  940. // Canvas Item, Vertex
  941. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "vec3(VERTEX,0.0)" },
  942. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  943. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  944. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  945. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  946. // Canvas Item, Fragment
  947. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  948. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  949. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  950. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(UV,0.0)" },
  951. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  952. // Canvas Item, Light
  953. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "vec3(UV,0.0)" },
  954. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "vec3(0.0,0.0,1.0)" },
  955. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  956. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  957. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "screen_uv", "vec3(UV,0.0)" },
  958. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  959. // Particles, Vertex
  960. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "vec3(1.0)" },
  961. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "1.0" },
  962. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "vec3(0.0,0.0,1.0)" },
  963. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "time", "TIME" },
  964. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, NULL, NULL },
  965. };
  966. int VisualShaderNodeInput::get_input_port_count() const {
  967. return 0;
  968. }
  969. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_port_type(int p_port) const {
  970. return PORT_TYPE_SCALAR;
  971. }
  972. String VisualShaderNodeInput::get_input_port_name(int p_port) const {
  973. return "";
  974. }
  975. int VisualShaderNodeInput::get_output_port_count() const {
  976. return 1;
  977. }
  978. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_output_port_type(int p_port) const {
  979. return get_input_type_by_name(input_name);
  980. }
  981. String VisualShaderNodeInput::get_output_port_name(int p_port) const {
  982. return "";
  983. }
  984. String VisualShaderNodeInput::get_caption() const {
  985. return TTR("Input");
  986. }
  987. String VisualShaderNodeInput::generate_code_for_preview(VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars) const {
  988. int idx = 0;
  989. String code;
  990. while (preview_ports[idx].mode != Shader::MODE_MAX) {
  991. if (preview_ports[idx].mode == shader_mode && preview_ports[idx].shader_type == shader_type && preview_ports[idx].name == input_name) {
  992. code = "\t" + p_output_vars[0] + " = " + preview_ports[idx].string + ";\n";
  993. break;
  994. }
  995. idx++;
  996. }
  997. if (code == String()) {
  998. switch (get_output_port_type(0)) {
  999. case PORT_TYPE_SCALAR: {
  1000. code = "\t" + p_output_vars[0] + " = 0.0;\n";
  1001. } break; //default (none found) is scalar
  1002. case PORT_TYPE_VECTOR: {
  1003. code = "\t" + p_output_vars[0] + " = vec3(0.0);\n";
  1004. } break; //default (none found) is scalar
  1005. case PORT_TYPE_TRANSFORM: {
  1006. 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";
  1007. } break; //default (none found) is scalar
  1008. }
  1009. }
  1010. return code;
  1011. }
  1012. 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) const {
  1013. int idx = 0;
  1014. String code;
  1015. while (ports[idx].mode != Shader::MODE_MAX) {
  1016. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == input_name) {
  1017. code = "\t" + p_output_vars[0] + " = " + ports[idx].string + ";\n";
  1018. break;
  1019. }
  1020. idx++;
  1021. }
  1022. if (code == String()) {
  1023. code = "\t" + p_output_vars[0] + " = 0.0;\n"; //default (none found) is scalar
  1024. }
  1025. return code;
  1026. }
  1027. void VisualShaderNodeInput::set_input_name(String p_name) {
  1028. PortType prev_type = get_input_type_by_name(input_name);
  1029. input_name = p_name;
  1030. emit_changed();
  1031. if (get_input_type_by_name(input_name) != prev_type) {
  1032. emit_signal("input_type_changed");
  1033. }
  1034. }
  1035. String VisualShaderNodeInput::get_input_name() const {
  1036. return input_name;
  1037. }
  1038. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_type_by_name(String p_name) const {
  1039. int idx = 0;
  1040. while (ports[idx].mode != Shader::MODE_MAX) {
  1041. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type && ports[idx].name == p_name) {
  1042. return ports[idx].type;
  1043. }
  1044. idx++;
  1045. }
  1046. return PORT_TYPE_SCALAR;
  1047. }
  1048. int VisualShaderNodeInput::get_input_index_count() const {
  1049. int idx = 0;
  1050. int count = 0;
  1051. while (ports[idx].mode != Shader::MODE_MAX) {
  1052. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1053. count++;
  1054. }
  1055. idx++;
  1056. }
  1057. return count;
  1058. }
  1059. VisualShaderNodeInput::PortType VisualShaderNodeInput::get_input_index_type(int p_index) const {
  1060. int idx = 0;
  1061. int count = 0;
  1062. while (ports[idx].mode != Shader::MODE_MAX) {
  1063. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1064. if (count == p_index) {
  1065. return ports[idx].type;
  1066. }
  1067. count++;
  1068. }
  1069. idx++;
  1070. }
  1071. return PORT_TYPE_SCALAR;
  1072. }
  1073. String VisualShaderNodeInput::get_input_index_name(int p_index) const {
  1074. int idx = 0;
  1075. int count = 0;
  1076. while (ports[idx].mode != Shader::MODE_MAX) {
  1077. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1078. if (count == p_index) {
  1079. return ports[idx].name;
  1080. }
  1081. count++;
  1082. }
  1083. idx++;
  1084. }
  1085. return "";
  1086. }
  1087. void VisualShaderNodeInput::_validate_property(PropertyInfo &property) const {
  1088. if (property.name == "input_name") {
  1089. String port_list;
  1090. int idx = 0;
  1091. while (ports[idx].mode != Shader::MODE_MAX) {
  1092. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1093. if (port_list != String()) {
  1094. port_list += ",";
  1095. }
  1096. port_list += ports[idx].name;
  1097. }
  1098. idx++;
  1099. }
  1100. if (port_list == "") {
  1101. port_list = TTR("None");
  1102. }
  1103. property.hint_string = port_list;
  1104. }
  1105. }
  1106. Vector<StringName> VisualShaderNodeInput::get_editable_properties() const {
  1107. Vector<StringName> props;
  1108. props.push_back("input_name");
  1109. return props;
  1110. }
  1111. void VisualShaderNodeInput::_bind_methods() {
  1112. ClassDB::bind_method(D_METHOD("set_input_name", "name"), &VisualShaderNodeInput::set_input_name);
  1113. ClassDB::bind_method(D_METHOD("get_input_name"), &VisualShaderNodeInput::get_input_name);
  1114. ADD_PROPERTY(PropertyInfo(Variant::STRING, "input_name", PROPERTY_HINT_ENUM, ""), "set_input_name", "get_input_name");
  1115. ADD_SIGNAL(MethodInfo("input_type_changed"));
  1116. }
  1117. VisualShaderNodeInput::VisualShaderNodeInput() {
  1118. input_name = "[None]";
  1119. // changed when set
  1120. shader_type = VisualShader::TYPE_MAX;
  1121. shader_mode = Shader::MODE_MAX;
  1122. }
  1123. ////////////////////////////////////////////
  1124. const VisualShaderNodeOutput::Port VisualShaderNodeOutput::ports[] = {
  1125. // Spatial, Vertex
  1126. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX" },
  1127. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1128. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "tangent", "TANGENT" },
  1129. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "binormal", "BINORMAL" },
  1130. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "UV:xy" },
  1131. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv2", "UV2:xy" },
  1132. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1133. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1134. { Shader::MODE_SPATIAL, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  1135. // Spatial, Fragment
  1136. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "albedo", "ALBEDO" },
  1137. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "ALPHA" },
  1138. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "metallic", "METALLIC" },
  1139. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "roughness", "ROUGHNESS" },
  1140. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "specular", "SPECULAR" },
  1141. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "emission", "EMISSION" },
  1142. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "ao", "AO" },
  1143. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1144. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normalmap", "NORMALMAP" },
  1145. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "normalmap_depth", "NORMALMAP_DEPTH" },
  1146. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "rim", "RIM" },
  1147. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "rim_tint", "RIM_TINT" },
  1148. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "clearcoat", "CLEARCOAT" },
  1149. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "clearcoat_gloss", "CLEARCOAT_GLOSS" },
  1150. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "anisotropy", "ANISOTROPY" },
  1151. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "anisotropy_flow", "ANISOTROPY_FLOW:xy" },
  1152. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "subsurf_scatter", "SSS_STRENGTH" },
  1153. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "transmission", "TRANSMISSION" },
  1154. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha_scissor", "ALPHA_SCISSOR" },
  1155. { Shader::MODE_SPATIAL, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "ao_light_affect", "AO_LIGHT_AFFECT" },
  1156. // Spatial, Light
  1157. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "diffuse", "DIFFUSE_LIGHT" },
  1158. { Shader::MODE_SPATIAL, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "specular", "SPECULAR_LIGHT" },
  1159. // Canvas Item, Vertex
  1160. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "vertex", "VERTEX:xy" },
  1161. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "uv", "UV:xy" },
  1162. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1163. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1164. // Canvas Item, Fragment
  1165. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1166. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1167. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normal", "NORMAL" },
  1168. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_VECTOR, "normalmap", "NORMALMAP" },
  1169. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_FRAGMENT, VisualShaderNode::PORT_TYPE_SCALAR, "normalmap_depth", "NORMALMAP_DEPTH" },
  1170. // Canvas Item, Light
  1171. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_VECTOR, "light", "LIGHT.rgb" },
  1172. { Shader::MODE_CANVAS_ITEM, VisualShader::TYPE_LIGHT, VisualShaderNode::PORT_TYPE_SCALAR, "light_alpha", "LIGHT.rgb" },
  1173. // Particles, Vertex
  1174. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "color", "COLOR.rgb" },
  1175. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "alpha", "COLOR.a" },
  1176. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "velocity", "VELOCITY" },
  1177. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_VECTOR, "custom", "CUSTOM.rgb" },
  1178. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_SCALAR, "custom_alpha", "CUSTOM.a" },
  1179. { Shader::MODE_PARTICLES, VisualShader::TYPE_VERTEX, VisualShaderNode::PORT_TYPE_TRANSFORM, "transform", "TRANSFORM" },
  1180. { Shader::MODE_MAX, VisualShader::TYPE_MAX, VisualShaderNode::PORT_TYPE_TRANSFORM, NULL, NULL },
  1181. };
  1182. int VisualShaderNodeOutput::get_input_port_count() const {
  1183. int idx = 0;
  1184. int count = 0;
  1185. while (ports[idx].mode != Shader::MODE_MAX) {
  1186. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1187. count++;
  1188. }
  1189. idx++;
  1190. }
  1191. return count;
  1192. }
  1193. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_input_port_type(int p_port) const {
  1194. int idx = 0;
  1195. int count = 0;
  1196. while (ports[idx].mode != Shader::MODE_MAX) {
  1197. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1198. if (count == p_port) {
  1199. return ports[idx].type;
  1200. }
  1201. count++;
  1202. }
  1203. idx++;
  1204. }
  1205. return PORT_TYPE_SCALAR;
  1206. }
  1207. String VisualShaderNodeOutput::get_input_port_name(int p_port) const {
  1208. int idx = 0;
  1209. int count = 0;
  1210. while (ports[idx].mode != Shader::MODE_MAX) {
  1211. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1212. if (count == p_port) {
  1213. return String(ports[idx].name).capitalize();
  1214. }
  1215. count++;
  1216. }
  1217. idx++;
  1218. }
  1219. return String();
  1220. }
  1221. Variant VisualShaderNodeOutput::get_input_port_default_value(int p_port) const {
  1222. return Variant();
  1223. }
  1224. int VisualShaderNodeOutput::get_output_port_count() const {
  1225. return 0;
  1226. }
  1227. VisualShaderNodeOutput::PortType VisualShaderNodeOutput::get_output_port_type(int p_port) const {
  1228. return PORT_TYPE_SCALAR;
  1229. }
  1230. String VisualShaderNodeOutput::get_output_port_name(int p_port) const {
  1231. return String();
  1232. }
  1233. bool VisualShaderNodeOutput::is_port_separator(int p_index) const {
  1234. if (shader_mode == Shader::MODE_SPATIAL && shader_type == VisualShader::TYPE_FRAGMENT) {
  1235. String name = get_input_port_name(p_index);
  1236. return (name == "Normal" || name == "Rim" || name == "Alpha Scissor");
  1237. }
  1238. return false;
  1239. }
  1240. String VisualShaderNodeOutput::get_caption() const {
  1241. return TTR("Output");
  1242. }
  1243. 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) const {
  1244. int idx = 0;
  1245. int count = 0;
  1246. String code;
  1247. while (ports[idx].mode != Shader::MODE_MAX) {
  1248. if (ports[idx].mode == shader_mode && ports[idx].shader_type == shader_type) {
  1249. if (p_input_vars[count] != String()) {
  1250. String s = ports[idx].string;
  1251. if (s.find(":") != -1) {
  1252. code += "\t" + s.get_slicec(':', 0) + " = " + p_input_vars[count] + "." + s.get_slicec(':', 1) + ";\n";
  1253. } else {
  1254. code += "\t" + s + " = " + p_input_vars[count] + ";\n";
  1255. }
  1256. }
  1257. count++;
  1258. }
  1259. idx++;
  1260. }
  1261. return code;
  1262. }
  1263. VisualShaderNodeOutput::VisualShaderNodeOutput() {
  1264. }
  1265. ///////////////////////////
  1266. void VisualShaderNodeUniform::set_uniform_name(const String &p_name) {
  1267. uniform_name = p_name;
  1268. emit_signal("name_changed");
  1269. emit_changed();
  1270. }
  1271. String VisualShaderNodeUniform::get_uniform_name() const {
  1272. return uniform_name;
  1273. }
  1274. void VisualShaderNodeUniform::_bind_methods() {
  1275. ClassDB::bind_method(D_METHOD("set_uniform_name", "name"), &VisualShaderNodeUniform::set_uniform_name);
  1276. ClassDB::bind_method(D_METHOD("get_uniform_name"), &VisualShaderNodeUniform::get_uniform_name);
  1277. ADD_PROPERTY(PropertyInfo(Variant::STRING, "uniform_name"), "set_uniform_name", "get_uniform_name");
  1278. }
  1279. VisualShaderNodeUniform::VisualShaderNodeUniform() {
  1280. }