shader_graph.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  1. /*************************************************************************/
  2. /* shader_graph.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "shader_graph.h"
  31. #include "scene/scene_string_names.h"
  32. // FIXME: Needs to be ported to the new 3.0 shader API
  33. #if 0
  34. Array ShaderGraph::_get_node_list(ShaderType p_type) const {
  35. List<int> nodes;
  36. get_node_list(p_type,&nodes);
  37. Array arr(true);
  38. for (List<int>::Element *E=nodes.front();E;E=E->next())
  39. arr.push_back(E->get());
  40. return arr;
  41. }
  42. Array ShaderGraph::_get_connections(ShaderType p_type) const {
  43. List<Connection> connections;
  44. get_node_connections(p_type,&connections);
  45. Array arr(true);
  46. for (List<Connection>::Element *E=connections.front();E;E=E->next()) {
  47. Dictionary d(true);
  48. d["src_id"]=E->get().src_id;
  49. d["src_slot"]=E->get().src_slot;
  50. d["dst_id"]=E->get().dst_id;
  51. d["dst_slot"]=E->get().dst_slot;
  52. arr.push_back(d);
  53. }
  54. return arr;
  55. }
  56. void ShaderGraph::_set_data(const Dictionary &p_data) {
  57. Dictionary d=p_data;
  58. ERR_FAIL_COND(!d.has("shaders"));
  59. Array sh=d["shaders"];
  60. ERR_FAIL_COND(sh.size()!=3);
  61. for(int t=0;t<3;t++) {
  62. Array data=sh[t];
  63. ERR_FAIL_COND((data.size()%6)!=0);
  64. shader[t].node_map.clear();
  65. for(int i=0;i<data.size();i+=6) {
  66. Node n;
  67. n.id=data[i+0];
  68. n.type=NodeType(int(data[i+1]));
  69. n.pos=data[i+2];
  70. n.param1=data[i+3];
  71. n.param2=data[i+4];
  72. Array conns=data[i+5];
  73. ERR_FAIL_COND((conns.size()%3)!=0);
  74. for(int j=0;j<conns.size();j+=3) {
  75. SourceSlot ss;
  76. int ls=conns[j+0];
  77. if (ls == SLOT_DEFAULT_VALUE) {
  78. n.defaults[conns[j+1]]=conns[j+2];
  79. } else {
  80. ss.id=conns[j+1];
  81. ss.slot=conns[j+2];
  82. n.connections[ls]=ss;
  83. }
  84. }
  85. shader[t].node_map[n.id]=n;
  86. }
  87. }
  88. _pending_update_shader=true;
  89. _update_shader();
  90. }
  91. Dictionary ShaderGraph::_get_data() const {
  92. Array sh;
  93. for(int i=0;i<3;i++) {
  94. Array data;
  95. int ec = shader[i].node_map.size();
  96. data.resize(ec*6);
  97. int idx=0;
  98. for (Map<int,Node>::Element*E=shader[i].node_map.front();E;E=E->next()) {
  99. data[idx+0]=E->key();
  100. data[idx+1]=E->get().type;
  101. data[idx+2]=E->get().pos;
  102. data[idx+3]=E->get().param1;
  103. data[idx+4]=E->get().param2;
  104. Array conns;
  105. conns.resize(E->get().connections.size()*3+E->get().defaults.size()*3);
  106. int idx2=0;
  107. for(Map<int,SourceSlot>::Element*F=E->get().connections.front();F;F=F->next()) {
  108. conns[idx2+0]=F->key();
  109. conns[idx2+1]=F->get().id;
  110. conns[idx2+2]=F->get().slot;
  111. idx2+=3;
  112. }
  113. for(Map<int,Variant>::Element*F=E->get().defaults.front();F;F=F->next()) {
  114. conns[idx2+0]=SLOT_DEFAULT_VALUE;
  115. conns[idx2+1]=F->key();
  116. conns[idx2+2]=F->get();
  117. idx2+=3;
  118. }
  119. data[idx+5]=conns;
  120. idx+=6;
  121. }
  122. sh.push_back(data);
  123. }
  124. Dictionary data;
  125. data["shaders"]=sh;
  126. return data;
  127. }
  128. ShaderGraph::GraphError ShaderGraph::get_graph_error(ShaderType p_type) const {
  129. ERR_FAIL_INDEX_V(p_type,3,GRAPH_OK);
  130. return shader[p_type].error;
  131. }
  132. int ShaderGraph::node_count(ShaderType p_which, int p_type)
  133. {
  134. int count=0;
  135. for (Map<int,Node>::Element *E=shader[p_which].node_map.front();E;E=E->next())
  136. if (E->get().type==p_type)
  137. count++;
  138. return count;
  139. }
  140. void ShaderGraph::_bind_methods() {
  141. ClassDB::bind_method(D_METHOD("_update_shader"),&ShaderGraph::_update_shader);
  142. ClassDB::bind_method(D_METHOD("node_add","shader_type","node_type","id"),&ShaderGraph::node_add);
  143. ClassDB::bind_method(D_METHOD("node_remove","shader_type","id"),&ShaderGraph::node_remove);
  144. ClassDB::bind_method(D_METHOD("node_set_position","shader_type","id","position"),&ShaderGraph::node_set_position);
  145. ClassDB::bind_method(D_METHOD("node_get_position","shader_type","id"),&ShaderGraph::node_get_position);
  146. ClassDB::bind_method(D_METHOD("node_get_type","shader_type","id"),&ShaderGraph::node_get_type);
  147. ClassDB::bind_method(D_METHOD("get_node_list","shader_type"),&ShaderGraph::_get_node_list);
  148. ClassDB::bind_method(D_METHOD("default_set_value","shader_type","id","param_id","value"), &ShaderGraph::default_set_value);
  149. ClassDB::bind_method(D_METHOD("default_get_value","shader_type","id","param_id"), &ShaderGraph::default_get_value);
  150. ClassDB::bind_method(D_METHOD("scalar_const_node_set_value","shader_type","id","value"),&ShaderGraph::scalar_const_node_set_value);
  151. ClassDB::bind_method(D_METHOD("scalar_const_node_get_value","shader_type","id"),&ShaderGraph::scalar_const_node_get_value);
  152. ClassDB::bind_method(D_METHOD("vec_const_node_set_value","shader_type","id","value"),&ShaderGraph::vec_const_node_set_value);
  153. ClassDB::bind_method(D_METHOD("vec_const_node_get_value","shader_type","id"),&ShaderGraph::vec_const_node_get_value);
  154. ClassDB::bind_method(D_METHOD("rgb_const_node_set_value","shader_type","id","value"),&ShaderGraph::rgb_const_node_set_value);
  155. ClassDB::bind_method(D_METHOD("rgb_const_node_get_value","shader_type","id"),&ShaderGraph::rgb_const_node_get_value);
  156. ClassDB::bind_method(D_METHOD("xform_const_node_set_value","shader_type","id","value"),&ShaderGraph::xform_const_node_set_value);
  157. ClassDB::bind_method(D_METHOD("xform_const_node_get_value","shader_type","id"),&ShaderGraph::xform_const_node_get_value);
  158. //void get_node_list(ShaderType p_which,List<int> *p_node_list) const;
  159. ClassDB::bind_method(D_METHOD("texture_node_set_filter_size","shader_type","id","filter_size"),&ShaderGraph::texture_node_set_filter_size);
  160. ClassDB::bind_method(D_METHOD("texture_node_get_filter_size","shader_type","id"),&ShaderGraph::texture_node_get_filter_size);
  161. ClassDB::bind_method(D_METHOD("texture_node_set_filter_strength","shader_type","id","filter_strength"),&ShaderGraph::texture_node_set_filter_strength);
  162. ClassDB::bind_method(D_METHOD("texture_node_get_filter_strength","shader_type","id"),&ShaderGraph::texture_node_get_filter_strength);
  163. ClassDB::bind_method(D_METHOD("scalar_op_node_set_op","shader_type","id","op"),&ShaderGraph::scalar_op_node_set_op);
  164. ClassDB::bind_method(D_METHOD("scalar_op_node_get_op","shader_type","id"),&ShaderGraph::scalar_op_node_get_op);
  165. ClassDB::bind_method(D_METHOD("vec_op_node_set_op","shader_type","id","op"),&ShaderGraph::vec_op_node_set_op);
  166. ClassDB::bind_method(D_METHOD("vec_op_node_get_op","shader_type","id"),&ShaderGraph::vec_op_node_get_op);
  167. ClassDB::bind_method(D_METHOD("vec_scalar_op_node_set_op","shader_type","id","op"),&ShaderGraph::vec_scalar_op_node_set_op);
  168. ClassDB::bind_method(D_METHOD("vec_scalar_op_node_get_op","shader_type","id"),&ShaderGraph::vec_scalar_op_node_get_op);
  169. ClassDB::bind_method(D_METHOD("rgb_op_node_set_op","shader_type","id","op"),&ShaderGraph::rgb_op_node_set_op);
  170. ClassDB::bind_method(D_METHOD("rgb_op_node_get_op","shader_type","id"),&ShaderGraph::rgb_op_node_get_op);
  171. ClassDB::bind_method(D_METHOD("xform_vec_mult_node_set_no_translation","shader_type","id","disable"),&ShaderGraph::xform_vec_mult_node_set_no_translation);
  172. ClassDB::bind_method(D_METHOD("xform_vec_mult_node_get_no_translation","shader_type","id"),&ShaderGraph::xform_vec_mult_node_get_no_translation);
  173. ClassDB::bind_method(D_METHOD("scalar_func_node_set_function","shader_type","id","func"),&ShaderGraph::scalar_func_node_set_function);
  174. ClassDB::bind_method(D_METHOD("scalar_func_node_get_function","shader_type","id"),&ShaderGraph::scalar_func_node_get_function);
  175. ClassDB::bind_method(D_METHOD("vec_func_node_set_function","shader_type","id","func"),&ShaderGraph::vec_func_node_set_function);
  176. ClassDB::bind_method(D_METHOD("vec_func_node_get_function","shader_type","id"),&ShaderGraph::vec_func_node_get_function);
  177. ClassDB::bind_method(D_METHOD("input_node_set_name","shader_type","id","name"),&ShaderGraph::input_node_set_name);
  178. ClassDB::bind_method(D_METHOD("input_node_get_name","shader_type","id"),&ShaderGraph::input_node_get_name);
  179. ClassDB::bind_method(D_METHOD("scalar_input_node_set_value","shader_type","id","value"),&ShaderGraph::scalar_input_node_set_value);
  180. ClassDB::bind_method(D_METHOD("scalar_input_node_get_value","shader_type","id"),&ShaderGraph::scalar_input_node_get_value);
  181. ClassDB::bind_method(D_METHOD("vec_input_node_set_value","shader_type","id","value"),&ShaderGraph::vec_input_node_set_value);
  182. ClassDB::bind_method(D_METHOD("vec_input_node_get_value","shader_type","id"),&ShaderGraph::vec_input_node_get_value);
  183. ClassDB::bind_method(D_METHOD("rgb_input_node_set_value","shader_type","id","value"),&ShaderGraph::rgb_input_node_set_value);
  184. ClassDB::bind_method(D_METHOD("rgb_input_node_get_value","shader_type","id"),&ShaderGraph::rgb_input_node_get_value);
  185. ClassDB::bind_method(D_METHOD("xform_input_node_set_value","shader_type","id","value"),&ShaderGraph::xform_input_node_set_value);
  186. ClassDB::bind_method(D_METHOD("xform_input_node_get_value","shader_type","id"),&ShaderGraph::xform_input_node_get_value);
  187. ClassDB::bind_method(D_METHOD("texture_input_node_set_value","shader_type","id","value"),&ShaderGraph::texture_input_node_set_value);
  188. ClassDB::bind_method(D_METHOD("texture_input_node_get_value","shader_type","id"),&ShaderGraph::texture_input_node_get_value);
  189. ClassDB::bind_method(D_METHOD("cubemap_input_node_set_value","shader_type","id","value"),&ShaderGraph::cubemap_input_node_set_value);
  190. ClassDB::bind_method(D_METHOD("cubemap_input_node_get_value","shader_type","id"),&ShaderGraph::cubemap_input_node_get_value);
  191. ClassDB::bind_method(D_METHOD("comment_node_set_text","shader_type","id","text"),&ShaderGraph::comment_node_set_text);
  192. ClassDB::bind_method(D_METHOD("comment_node_get_text","shader_type","id"),&ShaderGraph::comment_node_get_text);
  193. ClassDB::bind_method(D_METHOD("color_ramp_node_set_ramp","shader_type","id","colors","offsets"),&ShaderGraph::color_ramp_node_set_ramp);
  194. ClassDB::bind_method(D_METHOD("color_ramp_node_get_colors","shader_type","id"),&ShaderGraph::color_ramp_node_get_colors);
  195. ClassDB::bind_method(D_METHOD("color_ramp_node_get_offsets","shader_type","id"),&ShaderGraph::color_ramp_node_get_offsets);
  196. ClassDB::bind_method(D_METHOD("curve_map_node_set_points","shader_type","id","points"),&ShaderGraph::curve_map_node_set_points);
  197. ClassDB::bind_method(D_METHOD("curve_map_node_get_points","shader_type","id"),&ShaderGraph::curve_map_node_get_points);
  198. ClassDB::bind_method(D_METHOD("connect_node","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::connect_node);
  199. ClassDB::bind_method(D_METHOD("is_node_connected","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::is_node_connected);
  200. ClassDB::bind_method(D_METHOD("disconnect_node","shader_type","src_id","src_slot","dst_id","dst_slot"),&ShaderGraph::disconnect_node);
  201. ClassDB::bind_method(D_METHOD("get_node_connections","shader_type"),&ShaderGraph::_get_connections);
  202. ClassDB::bind_method(D_METHOD("clear","shader_type"),&ShaderGraph::clear);
  203. ClassDB::bind_method(D_METHOD("node_set_state","shader_type","id","state"),&ShaderGraph::node_set_state);
  204. ClassDB::bind_method(D_METHOD("node_get_state","shader_type","id"),&ShaderGraph::node_get_state);
  205. ClassDB::bind_method(D_METHOD("_set_data"),&ShaderGraph::_set_data);
  206. ClassDB::bind_method(D_METHOD("_get_data"),&ShaderGraph::_get_data);
  207. ADD_PROPERTY( PropertyInfo(Variant::DICTIONARY,"_data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), "_set_data","_get_data");
  208. //void get_connections(ShaderType p_which,List<Connection> *p_connections) const;
  209. BIND_ENUM_CONSTANT( NODE_INPUT ); // all inputs (shader type dependent)
  210. BIND_ENUM_CONSTANT( NODE_SCALAR_CONST ); //scalar constant
  211. BIND_ENUM_CONSTANT( NODE_VEC_CONST ); //vec3 constant
  212. BIND_ENUM_CONSTANT( NODE_RGB_CONST ); //rgb constant (shows a color picker instead)
  213. BIND_ENUM_CONSTANT( NODE_XFORM_CONST ); // 4x4 matrix constant
  214. BIND_ENUM_CONSTANT( NODE_TIME ); // time in seconds
  215. BIND_ENUM_CONSTANT( NODE_SCREEN_TEX ); // screen texture sampler (takes UV) (only usable in fragment shader)
  216. BIND_ENUM_CONSTANT( NODE_SCALAR_OP ); // scalar vs scalar op (mul ); add ); div ); etc)
  217. BIND_ENUM_CONSTANT( NODE_VEC_OP ); // vec3 vs vec3 op (mul );ad );div );crossprod );etc)
  218. BIND_ENUM_CONSTANT( NODE_VEC_SCALAR_OP ); // vec3 vs scalar op (mul ); add ); div ); etc)
  219. BIND_ENUM_CONSTANT( NODE_RGB_OP ); // vec3 vs vec3 rgb op (with scalar amount) ); like brighten ); darken ); burn ); dodge ); multiply ); etc.
  220. BIND_ENUM_CONSTANT( NODE_XFORM_MULT ); // mat4 x mat4
  221. BIND_ENUM_CONSTANT( NODE_XFORM_VEC_MULT ); // mat4 x vec3 mult (with no-translation option)
  222. BIND_ENUM_CONSTANT( NODE_XFORM_VEC_INV_MULT ); // mat4 x vec3 inverse mult (with no-translation option)
  223. BIND_ENUM_CONSTANT( NODE_SCALAR_FUNC ); // scalar function (sin ); cos ); etc)
  224. BIND_ENUM_CONSTANT( NODE_VEC_FUNC ); // vector function (normalize ); negate ); reciprocal ); rgb2hsv ); hsv2rgb ); etc ); etc)
  225. BIND_ENUM_CONSTANT( NODE_VEC_LEN ); // vec3 length
  226. BIND_ENUM_CONSTANT( NODE_DOT_PROD ); // vec3 . vec3 (dot product -> scalar output)
  227. BIND_ENUM_CONSTANT( NODE_VEC_TO_SCALAR ); // 1 vec3 input ); 3 scalar outputs
  228. BIND_ENUM_CONSTANT( NODE_SCALAR_TO_VEC ); // 3 scalar input ); 1 vec3 output
  229. BIND_ENUM_CONSTANT( NODE_VEC_TO_XFORM ); // 3 vec input ); 1 xform output
  230. BIND_ENUM_CONSTANT( NODE_XFORM_TO_VEC ); // 3 vec input ); 1 xform output
  231. BIND_ENUM_CONSTANT( NODE_SCALAR_INTERP ); // scalar interpolation (with optional curve)
  232. BIND_ENUM_CONSTANT( NODE_VEC_INTERP ); // vec3 interpolation (with optional curve)
  233. BIND_ENUM_CONSTANT( NODE_COLOR_RAMP );
  234. BIND_ENUM_CONSTANT( NODE_CURVE_MAP );
  235. BIND_ENUM_CONSTANT( NODE_SCALAR_INPUT ); // scalar uniform (assignable in material)
  236. BIND_ENUM_CONSTANT( NODE_VEC_INPUT ); // vec3 uniform (assignable in material)
  237. BIND_ENUM_CONSTANT( NODE_RGB_INPUT ); // color uniform (assignable in material)
  238. BIND_ENUM_CONSTANT( NODE_XFORM_INPUT ); // mat4 uniform (assignable in material)
  239. BIND_ENUM_CONSTANT( NODE_TEXTURE_INPUT ); // texture input (assignable in material)
  240. BIND_ENUM_CONSTANT( NODE_CUBEMAP_INPUT ); // cubemap input (assignable in material)
  241. BIND_ENUM_CONSTANT( NODE_DEFAULT_TEXTURE );
  242. BIND_ENUM_CONSTANT( NODE_OUTPUT ); // output (shader type dependent)
  243. BIND_ENUM_CONSTANT( NODE_COMMENT ); // comment
  244. BIND_ENUM_CONSTANT( NODE_TYPE_MAX );
  245. BIND_ENUM_CONSTANT( SLOT_TYPE_SCALAR );
  246. BIND_ENUM_CONSTANT( SLOT_TYPE_VEC );
  247. BIND_ENUM_CONSTANT( SLOT_TYPE_XFORM );
  248. BIND_ENUM_CONSTANT( SLOT_TYPE_TEXTURE );
  249. BIND_ENUM_CONSTANT( SLOT_MAX );
  250. BIND_ENUM_CONSTANT( SHADER_TYPE_VERTEX );
  251. BIND_ENUM_CONSTANT( SHADER_TYPE_FRAGMENT );
  252. BIND_ENUM_CONSTANT( SHADER_TYPE_LIGHT );
  253. BIND_ENUM_CONSTANT( SHADER_TYPE_MAX );
  254. BIND_ENUM_CONSTANT( SLOT_IN );
  255. BIND_ENUM_CONSTANT( SLOT_OUT );
  256. BIND_ENUM_CONSTANT( GRAPH_OK );
  257. BIND_ENUM_CONSTANT( GRAPH_ERROR_CYCLIC );
  258. BIND_ENUM_CONSTANT( GRAPH_ERROR_MISSING_CONNECTIONS );
  259. BIND_ENUM_CONSTANT( SCALAR_OP_ADD );
  260. BIND_ENUM_CONSTANT( SCALAR_OP_SUB );
  261. BIND_ENUM_CONSTANT( SCALAR_OP_MUL );
  262. BIND_ENUM_CONSTANT( SCALAR_OP_DIV );
  263. BIND_ENUM_CONSTANT( SCALAR_OP_MOD );
  264. BIND_ENUM_CONSTANT( SCALAR_OP_POW );
  265. BIND_ENUM_CONSTANT( SCALAR_OP_MAX );
  266. BIND_ENUM_CONSTANT( SCALAR_OP_MIN );
  267. BIND_ENUM_CONSTANT( SCALAR_OP_ATAN2 );
  268. BIND_ENUM_CONSTANT( SCALAR_MAX_OP );
  269. BIND_ENUM_CONSTANT( VEC_OP_ADD );
  270. BIND_ENUM_CONSTANT( VEC_OP_SUB );
  271. BIND_ENUM_CONSTANT( VEC_OP_MUL );
  272. BIND_ENUM_CONSTANT( VEC_OP_DIV );
  273. BIND_ENUM_CONSTANT( VEC_OP_MOD );
  274. BIND_ENUM_CONSTANT( VEC_OP_POW );
  275. BIND_ENUM_CONSTANT( VEC_OP_MAX );
  276. BIND_ENUM_CONSTANT( VEC_OP_MIN );
  277. BIND_ENUM_CONSTANT( VEC_OP_CROSS );
  278. BIND_ENUM_CONSTANT( VEC_MAX_OP );
  279. BIND_ENUM_CONSTANT( VEC_SCALAR_OP_MUL );
  280. BIND_ENUM_CONSTANT( VEC_SCALAR_OP_DIV );
  281. BIND_ENUM_CONSTANT( VEC_SCALAR_OP_POW );
  282. BIND_ENUM_CONSTANT( VEC_SCALAR_MAX_OP );
  283. BIND_ENUM_CONSTANT( RGB_OP_SCREEN );
  284. BIND_ENUM_CONSTANT( RGB_OP_DIFFERENCE );
  285. BIND_ENUM_CONSTANT( RGB_OP_DARKEN );
  286. BIND_ENUM_CONSTANT( RGB_OP_LIGHTEN );
  287. BIND_ENUM_CONSTANT( RGB_OP_OVERLAY );
  288. BIND_ENUM_CONSTANT( RGB_OP_DODGE );
  289. BIND_ENUM_CONSTANT( RGB_OP_BURN );
  290. BIND_ENUM_CONSTANT( RGB_OP_SOFT_LIGHT );
  291. BIND_ENUM_CONSTANT( RGB_OP_HARD_LIGHT );
  292. BIND_ENUM_CONSTANT( RGB_MAX_OP );
  293. BIND_ENUM_CONSTANT( SCALAR_FUNC_SIN );
  294. BIND_ENUM_CONSTANT( SCALAR_FUNC_COS );
  295. BIND_ENUM_CONSTANT( SCALAR_FUNC_TAN );
  296. BIND_ENUM_CONSTANT( SCALAR_FUNC_ASIN );
  297. BIND_ENUM_CONSTANT( SCALAR_FUNC_ACOS );
  298. BIND_ENUM_CONSTANT( SCALAR_FUNC_ATAN );
  299. BIND_ENUM_CONSTANT( SCALAR_FUNC_SINH );
  300. BIND_ENUM_CONSTANT( SCALAR_FUNC_COSH );
  301. BIND_ENUM_CONSTANT( SCALAR_FUNC_TANH );
  302. BIND_ENUM_CONSTANT( SCALAR_FUNC_LOG );
  303. BIND_ENUM_CONSTANT( SCALAR_FUNC_EXP );
  304. BIND_ENUM_CONSTANT( SCALAR_FUNC_SQRT );
  305. BIND_ENUM_CONSTANT( SCALAR_FUNC_ABS );
  306. BIND_ENUM_CONSTANT( SCALAR_FUNC_SIGN );
  307. BIND_ENUM_CONSTANT( SCALAR_FUNC_FLOOR );
  308. BIND_ENUM_CONSTANT( SCALAR_FUNC_ROUND );
  309. BIND_ENUM_CONSTANT( SCALAR_FUNC_CEIL );
  310. BIND_ENUM_CONSTANT( SCALAR_FUNC_FRAC );
  311. BIND_ENUM_CONSTANT( SCALAR_FUNC_SATURATE );
  312. BIND_ENUM_CONSTANT( SCALAR_FUNC_NEGATE );
  313. BIND_ENUM_CONSTANT( SCALAR_MAX_FUNC );
  314. BIND_ENUM_CONSTANT( VEC_FUNC_NORMALIZE );
  315. BIND_ENUM_CONSTANT( VEC_FUNC_SATURATE );
  316. BIND_ENUM_CONSTANT( VEC_FUNC_NEGATE );
  317. BIND_ENUM_CONSTANT( VEC_FUNC_RECIPROCAL );
  318. BIND_ENUM_CONSTANT( VEC_FUNC_RGB2HSV );
  319. BIND_ENUM_CONSTANT( VEC_FUNC_HSV2RGB );
  320. BIND_ENUM_CONSTANT( VEC_MAX_FUNC );
  321. ADD_SIGNAL(MethodInfo("updated"));
  322. }
  323. String ShaderGraph::_find_unique_name(const String& p_base) {
  324. int idx=1;
  325. while(true) {
  326. String tocmp=p_base;
  327. if (idx>1) {
  328. tocmp+="_"+itos(idx);
  329. }
  330. bool valid=true;
  331. for(int i=0;i<3;i++) {
  332. if (!valid)
  333. break;
  334. for (Map<int,Node>::Element *E=shader[i].node_map.front();E;E=E->next()) {
  335. if (E->get().type!=NODE_SCALAR_INPUT && E->get().type!=NODE_VEC_INPUT && E->get().type==NODE_RGB_INPUT && E->get().type==NODE_XFORM_INPUT && E->get().type==NODE_TEXTURE_INPUT && E->get().type==NODE_CUBEMAP_INPUT)
  336. continue;
  337. String name = E->get().param1;
  338. if (name==tocmp) {
  339. valid=false;
  340. break;
  341. }
  342. }
  343. }
  344. if (!valid) {
  345. idx++;
  346. continue;
  347. }
  348. return tocmp;
  349. }
  350. return String();
  351. }
  352. void ShaderGraph::node_add(ShaderType p_type, NodeType p_node_type,int p_id) {
  353. ERR_FAIL_INDEX(p_type,3);
  354. ERR_FAIL_COND(p_id==0);
  355. ERR_FAIL_COND(p_node_type==NODE_OUTPUT); //can't create output
  356. ERR_FAIL_COND( shader[p_type].node_map.has(p_id ) );
  357. ERR_FAIL_INDEX( p_node_type, NODE_TYPE_MAX );
  358. Node node;
  359. if (p_node_type==NODE_INPUT) {
  360. //see if it already exists
  361. for(Map<int,Node>::Element *E=shader[p_type].node_map.front();E;E=E->next()) {
  362. if (E->get().type==NODE_INPUT) {
  363. ERR_EXPLAIN("Only one input node can be added to the graph.");
  364. ERR_FAIL_COND(E->get().type==NODE_INPUT);
  365. }
  366. }
  367. }
  368. node.type=p_node_type;
  369. node.id=p_id;
  370. switch(p_node_type) {
  371. case NODE_INPUT: {} break; // all inputs (shader type dependent)
  372. case NODE_SCALAR_CONST: { node.param1=0;} break; //scalar constant
  373. case NODE_VEC_CONST: {node.param1=Vector3();} break; //vec3 constant
  374. case NODE_RGB_CONST: {node.param1=Color();} break; //rgb constant (shows a color picker instead)
  375. case NODE_XFORM_CONST: {node.param1=Transform();} break; // 4x4 matrix constant
  376. case NODE_TIME: {} break; // time in seconds
  377. case NODE_SCREEN_TEX: {Array arr; arr.push_back(0); arr.push_back(0); node.param2=arr;} break; // screen texture sampler (takes UV) (only usable in fragment shader)
  378. case NODE_SCALAR_OP: {node.param1=SCALAR_OP_ADD;} break; // scalar vs scalar op (mul: {} break; add: {} break; div: {} break; etc)
  379. case NODE_VEC_OP: {node.param1=VEC_OP_ADD;} break; // vec3 vs vec3 op (mul: {} break;ad: {} break;div: {} break;crossprod: {} break;etc)
  380. case NODE_VEC_SCALAR_OP: {node.param1=VEC_SCALAR_OP_MUL;} break; // vec3 vs scalar op (mul: {} break; add: {} break; div: {} break; etc)
  381. case NODE_RGB_OP: {node.param1=RGB_OP_SCREEN;} break; // vec3 vs vec3 rgb op (with scalar amount): {} break; like brighten: {} break; darken: {} break; burn: {} break; dodge: {} break; multiply: {} break; etc.
  382. case NODE_XFORM_MULT: {} break; // mat4 x mat4
  383. case NODE_XFORM_VEC_MULT: {} break; // mat4 x vec3 mult (with no-translation option)
  384. case NODE_XFORM_VEC_INV_MULT: {} break; // mat4 x vec3 inverse mult (with no-translation option)
  385. case NODE_SCALAR_FUNC: {node.param1=SCALAR_FUNC_SIN;} break; // scalar function (sin: {} break; cos: {} break; etc)
  386. case NODE_VEC_FUNC: {node.param1=VEC_FUNC_NORMALIZE;} break; // vector function (normalize: {} break; negate: {} break; reciprocal: {} break; rgb2hsv: {} break; hsv2rgb: {} break; etc: {} break; etc)
  387. case NODE_VEC_LEN: {} break; // vec3 length
  388. case NODE_DOT_PROD: {} break; // vec3 . vec3 (dot product -> scalar output)
  389. case NODE_VEC_TO_SCALAR: {} break; // 1 vec3 input: {} break; 3 scalar outputs
  390. case NODE_SCALAR_TO_VEC: {} break; // 3 scalar input: {} break; 1 vec3 output
  391. case NODE_VEC_TO_XFORM: {} break; // 3 scalar input: {} break; 1 vec3 output
  392. case NODE_XFORM_TO_VEC: {} break; // 3 scalar input: {} break; 1 vec3 output
  393. case NODE_SCALAR_INTERP: {} break; // scalar interpolation (with optional curve)
  394. case NODE_VEC_INTERP: {} break; // vec3 interpolation (with optional curve)
  395. case NODE_COLOR_RAMP: { node.param1=PoolVector<Color>(); node.param2=PoolVector<real_t>();} break; // vec3 interpolation (with optional curve)
  396. case NODE_CURVE_MAP: { node.param1=PoolVector<Vector2>();} break; // vec3 interpolation (with optional curve)
  397. case NODE_SCALAR_INPUT: {node.param1=_find_unique_name("Scalar"); node.param2=0;} break; // scalar uniform (assignable in material)
  398. case NODE_VEC_INPUT: {node.param1=_find_unique_name("Vec3");node.param2=Vector3();} break; // vec3 uniform (assignable in material)
  399. case NODE_RGB_INPUT: {node.param1=_find_unique_name("Color");node.param2=Color();} break; // color uniform (assignable in material)
  400. case NODE_XFORM_INPUT: {node.param1=_find_unique_name("XForm"); node.param2=Transform();} break; // mat4 uniform (assignable in material)
  401. case NODE_TEXTURE_INPUT: {node.param1=_find_unique_name("Tex"); } break; // texture input (assignable in material)
  402. case NODE_CUBEMAP_INPUT: {node.param1=_find_unique_name("Cube"); } break; // cubemap input (assignable in material)
  403. case NODE_DEFAULT_TEXTURE: {}; break;
  404. case NODE_OUTPUT: {} break; // output (shader type dependent)
  405. case NODE_COMMENT: {} break; // comment
  406. case NODE_TYPE_MAX: {};
  407. }
  408. shader[p_type].node_map[p_id]=node;
  409. _request_update();
  410. }
  411. void ShaderGraph::node_set_position(ShaderType p_type,int p_id, const Vector2& p_pos) {
  412. ERR_FAIL_INDEX(p_type,3);
  413. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  414. shader[p_type].node_map[p_id].pos=p_pos;
  415. _request_update();
  416. }
  417. Vector2 ShaderGraph::node_get_position(ShaderType p_type,int p_id) const {
  418. ERR_FAIL_INDEX_V(p_type,3,Vector2());
  419. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Vector2());
  420. return shader[p_type].node_map[p_id].pos;
  421. }
  422. void ShaderGraph::node_remove(ShaderType p_type,int p_id) {
  423. ERR_FAIL_COND(p_id==0);
  424. ERR_FAIL_INDEX(p_type,3);
  425. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  426. //erase connections associated with node
  427. for(Map<int,Node>::Element *E=shader[p_type].node_map.front();E;E=E->next()) {
  428. if (E->key()==p_id)
  429. continue; //no self
  430. for (Map<int,SourceSlot>::Element *F=E->get().connections.front();F;) {
  431. Map<int,SourceSlot>::Element *N=F->next();
  432. if (F->get().id==p_id) {
  433. E->get().connections.erase(F);
  434. }
  435. F=N;
  436. }
  437. }
  438. shader[p_type].node_map.erase(p_id);
  439. _request_update();
  440. }
  441. void ShaderGraph::get_node_list(ShaderType p_type,List<int> *p_node_list) const {
  442. ERR_FAIL_INDEX(p_type,3);
  443. Map<int,Node>::Element *E = shader[p_type].node_map.front();
  444. while(E) {
  445. p_node_list->push_back(E->key());
  446. E=E->next();
  447. }
  448. }
  449. ShaderGraph::NodeType ShaderGraph::node_get_type(ShaderType p_type,int p_id) const {
  450. ERR_FAIL_INDEX_V(p_type,3,NODE_TYPE_MAX);
  451. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),NODE_TYPE_MAX);
  452. return shader[p_type].node_map[p_id].type;
  453. }
  454. Error ShaderGraph::connect_node(ShaderType p_type,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) {
  455. ERR_FAIL_INDEX_V(p_type,3,ERR_INVALID_PARAMETER);
  456. ERR_FAIL_COND_V(p_src_id==p_dst_id, ERR_INVALID_PARAMETER);
  457. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_src_id), ERR_INVALID_PARAMETER);
  458. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_dst_id), ERR_INVALID_PARAMETER);
  459. NodeType type_src=shader[p_type].node_map[p_src_id].type;
  460. NodeType type_dst=shader[p_type].node_map[p_dst_id].type;
  461. ERR_FAIL_INDEX_V( p_src_slot, get_node_output_slot_count(get_mode(),p_type,type_src), ERR_INVALID_PARAMETER );
  462. ERR_FAIL_INDEX_V( p_dst_slot, get_node_input_slot_count(get_mode(),p_type,type_dst), ERR_INVALID_PARAMETER );
  463. ERR_FAIL_COND_V(get_node_output_slot_type(get_mode(),p_type,type_src,p_src_slot) != get_node_input_slot_type(get_mode(),p_type,type_dst,p_dst_slot), ERR_INVALID_PARAMETER );
  464. SourceSlot ts;
  465. ts.id=p_src_id;
  466. ts.slot=p_src_slot;
  467. shader[p_type].node_map[p_dst_id].connections[p_dst_slot]=ts;
  468. _request_update();
  469. return OK;
  470. }
  471. bool ShaderGraph::is_node_connected(ShaderType p_type,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) const {
  472. ERR_FAIL_INDEX_V(p_type,3,false);
  473. SourceSlot ts;
  474. ts.id=p_src_id;
  475. ts.slot=p_src_slot;
  476. return shader[p_type].node_map.has(p_dst_id) && shader[p_type].node_map[p_dst_id].connections.has(p_dst_slot) &&
  477. shader[p_type].node_map[p_dst_id].connections[p_dst_slot]==ts;
  478. }
  479. void ShaderGraph::disconnect_node(ShaderType p_type,int p_src_id,int p_src_slot, int p_dst_id,int p_dst_slot) {
  480. ERR_FAIL_INDEX(p_type,3);
  481. SourceSlot ts;
  482. ts.id=p_src_id;
  483. ts.slot=p_src_slot;
  484. if (shader[p_type].node_map.has(p_dst_id) && shader[p_type].node_map[p_dst_id].connections.has(p_dst_slot) &&
  485. shader[p_type].node_map[p_dst_id].connections[p_dst_slot]==ts) {
  486. shader[p_type].node_map[p_dst_id].connections.erase(p_dst_slot);
  487. }
  488. _request_update();
  489. }
  490. void ShaderGraph::get_node_connections(ShaderType p_type,List<Connection> *p_connections) const {
  491. ERR_FAIL_INDEX(p_type,3);
  492. for(const Map<int,Node>::Element *E=shader[p_type].node_map.front();E;E=E->next()) {
  493. for (const Map<int,SourceSlot>::Element *F=E->get().connections.front();F;F=F->next()) {
  494. Connection c;
  495. c.dst_id=E->key();
  496. c.dst_slot=F->key();
  497. c.src_id=F->get().id;
  498. c.src_slot=F->get().slot;
  499. p_connections->push_back(c);
  500. }
  501. }
  502. }
  503. bool ShaderGraph::is_slot_connected(ShaderGraph::ShaderType p_type, int p_dst_id, int slot_id)
  504. {
  505. for(const Map<int,Node>::Element *E=shader[p_type].node_map.front();E;E=E->next()) {
  506. for (const Map<int,SourceSlot>::Element *F=E->get().connections.front();F;F=F->next()) {
  507. if (p_dst_id == E->key() && slot_id==F->key())
  508. return true;
  509. }
  510. }
  511. return false;
  512. }
  513. void ShaderGraph::clear(ShaderType p_type) {
  514. ERR_FAIL_INDEX(p_type,3);
  515. shader[p_type].node_map.clear();
  516. Node out;
  517. out.pos=Vector2(300,300);
  518. out.type=NODE_OUTPUT;
  519. shader[p_type].node_map.insert(0,out);
  520. _request_update();
  521. }
  522. void ShaderGraph::scalar_const_node_set_value(ShaderType p_type,int p_id,float p_value) {
  523. ERR_FAIL_INDEX(p_type,3);
  524. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  525. Node& n = shader[p_type].node_map[p_id];
  526. ERR_FAIL_COND(n.type!=NODE_SCALAR_CONST);
  527. n.param1=p_value;
  528. _request_update();
  529. }
  530. float ShaderGraph::scalar_const_node_get_value(ShaderType p_type,int p_id) const{
  531. ERR_FAIL_INDEX_V(p_type,3,0);
  532. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),0);
  533. const Node& n = shader[p_type].node_map[p_id];
  534. ERR_FAIL_COND_V(n.type!=NODE_SCALAR_CONST,0);
  535. return n.param1;
  536. }
  537. void ShaderGraph::vec_const_node_set_value(ShaderType p_type,int p_id,const Vector3& p_value){
  538. ERR_FAIL_INDEX(p_type,3);
  539. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  540. Node& n = shader[p_type].node_map[p_id];
  541. ERR_FAIL_COND(n.type!=NODE_VEC_CONST);
  542. n.param1=p_value;
  543. _request_update();
  544. }
  545. Vector3 ShaderGraph::vec_const_node_get_value(ShaderType p_type,int p_id) const{
  546. ERR_FAIL_INDEX_V(p_type,3,Vector3());
  547. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Vector3());
  548. const Node& n = shader[p_type].node_map[p_id];
  549. ERR_FAIL_COND_V(n.type!=NODE_VEC_CONST,Vector3());
  550. return n.param1;
  551. }
  552. void ShaderGraph::rgb_const_node_set_value(ShaderType p_type,int p_id,const Color& p_value){
  553. ERR_FAIL_INDEX(p_type,3);
  554. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  555. Node& n = shader[p_type].node_map[p_id];
  556. ERR_FAIL_COND(n.type!=NODE_RGB_CONST);
  557. n.param1=p_value;
  558. _request_update();
  559. }
  560. Color ShaderGraph::rgb_const_node_get_value(ShaderType p_type,int p_id) const{
  561. ERR_FAIL_INDEX_V(p_type,3,Color());
  562. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Color());
  563. const Node& n = shader[p_type].node_map[p_id];
  564. ERR_FAIL_COND_V(n.type!=NODE_RGB_CONST,Color());
  565. return n.param1;
  566. }
  567. void ShaderGraph::xform_const_node_set_value(ShaderType p_type,int p_id,const Transform& p_value){
  568. ERR_FAIL_INDEX(p_type,3);
  569. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  570. Node& n = shader[p_type].node_map[p_id];
  571. ERR_FAIL_COND(n.type!=NODE_XFORM_CONST);
  572. n.param1=p_value;
  573. _request_update();
  574. }
  575. Transform ShaderGraph::xform_const_node_get_value(ShaderType p_type,int p_id) const{
  576. ERR_FAIL_INDEX_V(p_type,3,Transform());
  577. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Transform());
  578. const Node& n = shader[p_type].node_map[p_id];
  579. ERR_FAIL_COND_V(n.type!=NODE_XFORM_CONST,Transform());
  580. return n.param1;
  581. }
  582. void ShaderGraph::texture_node_set_filter_size(ShaderType p_type,int p_id,int p_size){
  583. ERR_FAIL_INDEX(p_type,3);
  584. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  585. Node& n = shader[p_type].node_map[p_id];
  586. ERR_FAIL_COND(n.type!=NODE_TEXTURE_INPUT && n.type!=NODE_SCREEN_TEX);
  587. Array arr = n.param2;
  588. arr[0]=p_size;
  589. n.param2=arr;
  590. _request_update();
  591. }
  592. int ShaderGraph::texture_node_get_filter_size(ShaderType p_type,int p_id) const{
  593. ERR_FAIL_INDEX_V(p_type,3,0);
  594. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),0);
  595. const Node& n = shader[p_type].node_map[p_id];
  596. ERR_FAIL_COND_V(n.type!=NODE_TEXTURE_INPUT && n.type!=NODE_SCREEN_TEX,0);
  597. Array arr = n.param2;
  598. return arr[0];
  599. }
  600. void ShaderGraph::texture_node_set_filter_strength(ShaderType p_type,float p_id,float p_strength){
  601. ERR_FAIL_INDEX(p_type,3);
  602. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  603. Node& n = shader[p_type].node_map[p_id];
  604. ERR_FAIL_COND(n.type!=NODE_TEXTURE_INPUT && n.type!=NODE_SCREEN_TEX);
  605. Array arr = n.param2;
  606. arr[1]=p_strength;
  607. n.param2=arr;
  608. _request_update();
  609. }
  610. float ShaderGraph::texture_node_get_filter_strength(ShaderType p_type,float p_id) const{
  611. ERR_FAIL_INDEX_V(p_type,3,0);
  612. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),0);
  613. const Node& n = shader[p_type].node_map[p_id];
  614. ERR_FAIL_COND_V(n.type!=NODE_TEXTURE_INPUT && n.type!=NODE_SCREEN_TEX,0);
  615. Array arr = n.param2;
  616. return arr[1];
  617. }
  618. void ShaderGraph::duplicate_nodes(ShaderType p_which, List<int> &p_nodes)
  619. {
  620. //Create new node IDs
  621. Map<int,int> duplicates = Map<int,int>();
  622. int i=1;
  623. for(List<int>::Element *E=p_nodes.front();E; E=E->next()) {
  624. while (shader[p_which].node_map.has(i))
  625. i++;
  626. duplicates.insert(E->get(), i);
  627. i++;
  628. }
  629. for(List<int>::Element *E = p_nodes.front();E; E=E->next()) {
  630. const Node &n=shader[p_which].node_map[E->get()];
  631. Node nn=n;
  632. nn.id=duplicates.find(n.id)->get();
  633. nn.pos += Vector2(0,100);
  634. for (Map<int,SourceSlot>::Element *C=nn.connections.front();C;C=C->next()) {
  635. SourceSlot &c=C->get();
  636. if (p_nodes.find(c.id))
  637. c.id=duplicates.find(c.id)->get();
  638. }
  639. shader[p_which].node_map[nn.id]=nn;
  640. }
  641. _request_update();
  642. }
  643. List<int> ShaderGraph::generate_ids(ShaderType p_type, int count)
  644. {
  645. List<int> ids = List<int>();
  646. int i=1;
  647. while (ids.size() < count) {
  648. while (shader[p_type].node_map.has(i))
  649. i++;
  650. ids.push_back(i);
  651. i++;
  652. }
  653. return ids;
  654. }
  655. void ShaderGraph::scalar_op_node_set_op(ShaderType p_type,float p_id,ScalarOp p_op){
  656. ERR_FAIL_INDEX(p_type,3);
  657. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  658. Node& n = shader[p_type].node_map[p_id];
  659. ERR_FAIL_COND(n.type!=NODE_SCALAR_OP);
  660. n.param1=p_op;
  661. _request_update();
  662. }
  663. ShaderGraph::ScalarOp ShaderGraph::scalar_op_node_get_op(ShaderType p_type,float p_id) const{
  664. ERR_FAIL_INDEX_V(p_type,3,SCALAR_MAX_OP);
  665. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),SCALAR_MAX_OP);
  666. const Node& n = shader[p_type].node_map[p_id];
  667. ERR_FAIL_COND_V(n.type!=NODE_SCALAR_OP,SCALAR_MAX_OP);
  668. int op = n.param1;
  669. return ScalarOp(op);
  670. }
  671. void ShaderGraph::vec_op_node_set_op(ShaderType p_type,float p_id,VecOp p_op){
  672. ERR_FAIL_INDEX(p_type,3);
  673. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  674. Node& n = shader[p_type].node_map[p_id];
  675. ERR_FAIL_COND(n.type!=NODE_VEC_OP);
  676. n.param1=p_op;
  677. _request_update();
  678. }
  679. ShaderGraph::VecOp ShaderGraph::vec_op_node_get_op(ShaderType p_type,float p_id) const{
  680. ERR_FAIL_INDEX_V(p_type,3,VEC_MAX_OP);
  681. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),VEC_MAX_OP);
  682. const Node& n = shader[p_type].node_map[p_id];
  683. ERR_FAIL_COND_V(n.type!=NODE_VEC_OP,VEC_MAX_OP);
  684. int op = n.param1;
  685. return VecOp(op);
  686. }
  687. void ShaderGraph::vec_scalar_op_node_set_op(ShaderType p_type,float p_id,VecScalarOp p_op){
  688. ERR_FAIL_INDEX(p_type,3);
  689. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  690. Node& n = shader[p_type].node_map[p_id];
  691. ERR_FAIL_COND(n.type!=NODE_VEC_SCALAR_OP);
  692. n.param1=p_op;
  693. _request_update();
  694. }
  695. ShaderGraph::VecScalarOp ShaderGraph::vec_scalar_op_node_get_op(ShaderType p_type,float p_id) const{
  696. ERR_FAIL_INDEX_V(p_type,3,VEC_SCALAR_MAX_OP);
  697. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),VEC_SCALAR_MAX_OP);
  698. const Node& n = shader[p_type].node_map[p_id];
  699. ERR_FAIL_COND_V(n.type!=NODE_VEC_SCALAR_OP,VEC_SCALAR_MAX_OP);
  700. int op = n.param1;
  701. return VecScalarOp(op);
  702. }
  703. void ShaderGraph::rgb_op_node_set_op(ShaderType p_type,float p_id,RGBOp p_op){
  704. ERR_FAIL_INDEX(p_type,3);
  705. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  706. Node& n = shader[p_type].node_map[p_id];
  707. ERR_FAIL_COND(n.type!=NODE_RGB_OP);
  708. n.param1=p_op;
  709. _request_update();
  710. }
  711. ShaderGraph::RGBOp ShaderGraph::rgb_op_node_get_op(ShaderType p_type,float p_id) const{
  712. ERR_FAIL_INDEX_V(p_type,3,RGB_MAX_OP);
  713. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),RGB_MAX_OP);
  714. const Node& n = shader[p_type].node_map[p_id];
  715. ERR_FAIL_COND_V(n.type!=NODE_RGB_OP,RGB_MAX_OP);
  716. int op = n.param1;
  717. return RGBOp(op);
  718. }
  719. void ShaderGraph::xform_vec_mult_node_set_no_translation(ShaderType p_type,int p_id,bool p_no_translation){
  720. ERR_FAIL_INDEX(p_type,3);
  721. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  722. Node& n = shader[p_type].node_map[p_id];
  723. ERR_FAIL_COND(n.type!=NODE_XFORM_VEC_MULT && n.type!=NODE_XFORM_VEC_INV_MULT);
  724. n.param1=p_no_translation;
  725. _request_update();
  726. }
  727. bool ShaderGraph::xform_vec_mult_node_get_no_translation(ShaderType p_type,int p_id) const{
  728. ERR_FAIL_INDEX_V(p_type,3,false);
  729. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),false);
  730. const Node& n = shader[p_type].node_map[p_id];
  731. ERR_FAIL_COND_V(n.type!=NODE_XFORM_VEC_MULT && n.type!=NODE_XFORM_VEC_INV_MULT,false);
  732. return n.param1;
  733. }
  734. void ShaderGraph::scalar_func_node_set_function(ShaderType p_type,int p_id,ScalarFunc p_func){
  735. ERR_FAIL_INDEX(p_type,3);
  736. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  737. Node& n = shader[p_type].node_map[p_id];
  738. ERR_FAIL_COND(n.type!=NODE_SCALAR_FUNC);
  739. int func = p_func;
  740. ERR_FAIL_INDEX(func,SCALAR_MAX_FUNC);
  741. n.param1=func;
  742. _request_update();
  743. }
  744. ShaderGraph::ScalarFunc ShaderGraph::scalar_func_node_get_function(ShaderType p_type,int p_id) const{
  745. ERR_FAIL_INDEX_V(p_type,3,SCALAR_MAX_FUNC);
  746. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),SCALAR_MAX_FUNC);
  747. const Node& n = shader[p_type].node_map[p_id];
  748. ERR_FAIL_COND_V(n.type!=NODE_SCALAR_FUNC,SCALAR_MAX_FUNC);
  749. int func = n.param1;
  750. return ScalarFunc(func);
  751. }
  752. void ShaderGraph::default_set_value(ShaderGraph::ShaderType p_which, int p_id, int p_param, const Variant &p_value)
  753. {
  754. ERR_FAIL_INDEX(p_which,3);
  755. ERR_FAIL_COND(!shader[p_which].node_map.has(p_id));
  756. Node& n = shader[p_which].node_map[p_id];
  757. if(p_value.get_type()==Variant::NIL)
  758. n.defaults.erase(n.defaults.find(p_param));
  759. else
  760. n.defaults[p_param]=p_value;
  761. _request_update();
  762. }
  763. Variant ShaderGraph::default_get_value(ShaderGraph::ShaderType p_which, int p_id, int p_param)
  764. {
  765. ERR_FAIL_INDEX_V(p_which,3,Variant());
  766. ERR_FAIL_COND_V(!shader[p_which].node_map.has(p_id),Variant());
  767. const Node& n = shader[p_which].node_map[p_id];
  768. if (!n.defaults.has(p_param))
  769. return Variant();
  770. return n.defaults[p_param];
  771. }
  772. void ShaderGraph::vec_func_node_set_function(ShaderType p_type,int p_id,VecFunc p_func){
  773. ERR_FAIL_INDEX(p_type,3);
  774. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  775. Node& n = shader[p_type].node_map[p_id];
  776. ERR_FAIL_COND(n.type!=NODE_VEC_FUNC);
  777. int func = p_func;
  778. ERR_FAIL_INDEX(func,VEC_MAX_FUNC);
  779. n.param1=func;
  780. _request_update();
  781. }
  782. ShaderGraph::VecFunc ShaderGraph::vec_func_node_get_function(ShaderType p_type, int p_id) const{
  783. ERR_FAIL_INDEX_V(p_type,3,VEC_MAX_FUNC);
  784. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),VEC_MAX_FUNC);
  785. const Node& n = shader[p_type].node_map[p_id];
  786. ERR_FAIL_COND_V(n.type!=NODE_VEC_FUNC,VEC_MAX_FUNC);
  787. int func = n.param1;
  788. return VecFunc(func);
  789. }
  790. void ShaderGraph::color_ramp_node_set_ramp(ShaderType p_type,int p_id,const PoolVector<Color>& p_colors, const PoolVector<real_t>& p_offsets){
  791. ERR_FAIL_INDEX(p_type,3);
  792. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  793. ERR_FAIL_COND(p_colors.size()!=p_offsets.size());
  794. Node& n = shader[p_type].node_map[p_id];
  795. n.param1=p_colors;
  796. n.param2=p_offsets;
  797. _request_update();
  798. }
  799. PoolVector<Color> ShaderGraph::color_ramp_node_get_colors(ShaderType p_type,int p_id) const{
  800. ERR_FAIL_INDEX_V(p_type,3,PoolVector<Color>());
  801. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),PoolVector<Color>());
  802. const Node& n = shader[p_type].node_map[p_id];
  803. return n.param1;
  804. }
  805. PoolVector<real_t> ShaderGraph::color_ramp_node_get_offsets(ShaderType p_type,int p_id) const{
  806. ERR_FAIL_INDEX_V(p_type,3,PoolVector<real_t>());
  807. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),PoolVector<real_t>());
  808. const Node& n = shader[p_type].node_map[p_id];
  809. return n.param2;
  810. }
  811. void ShaderGraph::curve_map_node_set_points(ShaderType p_type,int p_id,const PoolVector<Vector2>& p_points) {
  812. ERR_FAIL_INDEX(p_type,3);
  813. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  814. Node& n = shader[p_type].node_map[p_id];
  815. n.param1=p_points;
  816. _request_update();
  817. }
  818. PoolVector<Vector2> ShaderGraph::curve_map_node_get_points(ShaderType p_type,int p_id) const{
  819. ERR_FAIL_INDEX_V(p_type,3,PoolVector<Vector2>());
  820. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),PoolVector<Vector2>());
  821. const Node& n = shader[p_type].node_map[p_id];
  822. return n.param1;
  823. }
  824. void ShaderGraph::input_node_set_name(ShaderType p_type,int p_id,const String& p_name){
  825. ERR_FAIL_INDEX(p_type,3);
  826. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  827. ERR_FAIL_COND(!p_name.is_valid_identifier());
  828. Node& n = shader[p_type].node_map[p_id];
  829. ERR_FAIL_COND(n.type!=NODE_SCALAR_INPUT && n.type!=NODE_VEC_INPUT && n.type==NODE_RGB_INPUT && n.type==NODE_XFORM_INPUT && n.type==NODE_TEXTURE_INPUT && n.type==NODE_CUBEMAP_INPUT);
  830. n.param1="";
  831. n.param1=_find_unique_name(p_name);
  832. _request_update();
  833. }
  834. String ShaderGraph::input_node_get_name(ShaderType p_type,int p_id){
  835. ERR_FAIL_INDEX_V(p_type,3,String());
  836. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),String());
  837. const Node& n = shader[p_type].node_map[p_id];
  838. ERR_FAIL_COND_V(n.type!=NODE_SCALAR_INPUT && n.type!=NODE_VEC_INPUT && n.type==NODE_RGB_INPUT && n.type==NODE_XFORM_INPUT && n.type==NODE_TEXTURE_INPUT && n.type==NODE_CUBEMAP_INPUT,String());
  839. return n.param1;
  840. }
  841. void ShaderGraph::scalar_input_node_set_value(ShaderType p_type,int p_id,float p_value) {
  842. ERR_FAIL_INDEX(p_type,3);
  843. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  844. Node& n = shader[p_type].node_map[p_id];
  845. ERR_FAIL_COND(n.type!=NODE_SCALAR_INPUT);
  846. n.param2=p_value;
  847. _request_update();
  848. }
  849. float ShaderGraph::scalar_input_node_get_value(ShaderType p_type,int p_id) const{
  850. ERR_FAIL_INDEX_V(p_type,3,0);
  851. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),0);
  852. const Node& n = shader[p_type].node_map[p_id];
  853. ERR_FAIL_COND_V(n.type!=NODE_SCALAR_INPUT,0);
  854. return n.param2;
  855. }
  856. void ShaderGraph::vec_input_node_set_value(ShaderType p_type,int p_id,const Vector3& p_value){
  857. ERR_FAIL_INDEX(p_type,3);
  858. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  859. Node& n = shader[p_type].node_map[p_id];
  860. ERR_FAIL_COND(n.type!=NODE_VEC_INPUT);
  861. n.param2=p_value;
  862. _request_update();
  863. }
  864. Vector3 ShaderGraph::vec_input_node_get_value(ShaderType p_type,int p_id) const{
  865. ERR_FAIL_INDEX_V(p_type,3,Vector3());
  866. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Vector3());
  867. const Node& n = shader[p_type].node_map[p_id];
  868. ERR_FAIL_COND_V(n.type!=NODE_VEC_INPUT,Vector3());
  869. return n.param2;
  870. }
  871. void ShaderGraph::rgb_input_node_set_value(ShaderType p_type,int p_id,const Color& p_value){
  872. ERR_FAIL_INDEX(p_type,3);
  873. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  874. Node& n = shader[p_type].node_map[p_id];
  875. ERR_FAIL_COND(n.type!=NODE_RGB_INPUT);
  876. n.param2=p_value;
  877. _request_update();
  878. }
  879. Color ShaderGraph::rgb_input_node_get_value(ShaderType p_type,int p_id) const{
  880. ERR_FAIL_INDEX_V(p_type,3,Color());
  881. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Color());
  882. const Node& n = shader[p_type].node_map[p_id];
  883. ERR_FAIL_COND_V(n.type!=NODE_RGB_INPUT,Color());
  884. return n.param2;
  885. }
  886. void ShaderGraph::xform_input_node_set_value(ShaderType p_type,int p_id,const Transform& p_value){
  887. ERR_FAIL_INDEX(p_type,3);
  888. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  889. Node& n = shader[p_type].node_map[p_id];
  890. ERR_FAIL_COND(n.type!=NODE_XFORM_INPUT);
  891. n.param2=p_value;
  892. _request_update();
  893. }
  894. Transform ShaderGraph::xform_input_node_get_value(ShaderType p_type,int p_id) const{
  895. ERR_FAIL_INDEX_V(p_type,3,Transform());
  896. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Transform());
  897. const Node& n = shader[p_type].node_map[p_id];
  898. ERR_FAIL_COND_V(n.type!=NODE_XFORM_INPUT,Transform());
  899. return n.param2;
  900. }
  901. void ShaderGraph::texture_input_node_set_value(ShaderType p_type,int p_id,const Ref<Texture>& p_texture) {
  902. ERR_FAIL_INDEX(p_type,3);
  903. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  904. Node& n = shader[p_type].node_map[p_id];
  905. ERR_FAIL_COND(n.type!=NODE_TEXTURE_INPUT);
  906. n.param2=p_texture;
  907. _request_update();
  908. }
  909. Ref<Texture> ShaderGraph::texture_input_node_get_value(ShaderType p_type,int p_id) const{
  910. ERR_FAIL_INDEX_V(p_type,3,Ref<Texture>());
  911. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Ref<Texture>());
  912. const Node& n = shader[p_type].node_map[p_id];
  913. ERR_FAIL_COND_V(n.type!=NODE_TEXTURE_INPUT,Ref<Texture>());
  914. return n.param2;
  915. }
  916. void ShaderGraph::cubemap_input_node_set_value(ShaderType p_type,int p_id,const Ref<CubeMap>& p_cubemap){
  917. ERR_FAIL_INDEX(p_type,3);
  918. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  919. Node& n = shader[p_type].node_map[p_id];
  920. ERR_FAIL_COND(n.type!=NODE_CUBEMAP_INPUT);
  921. n.param2=p_cubemap;
  922. _request_update();
  923. }
  924. Ref<CubeMap> ShaderGraph::cubemap_input_node_get_value(ShaderType p_type,int p_id) const{
  925. ERR_FAIL_INDEX_V(p_type,3,Ref<CubeMap>());
  926. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Ref<CubeMap>());
  927. const Node& n = shader[p_type].node_map[p_id];
  928. ERR_FAIL_COND_V(n.type!=NODE_CUBEMAP_INPUT,Ref<CubeMap>());
  929. return n.param2;
  930. }
  931. void ShaderGraph::comment_node_set_text(ShaderType p_type,int p_id,const String& p_comment) {
  932. ERR_FAIL_INDEX(p_type,3);
  933. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  934. Node& n = shader[p_type].node_map[p_id];
  935. ERR_FAIL_COND(n.type!=NODE_COMMENT);
  936. n.param1=p_comment;
  937. }
  938. String ShaderGraph::comment_node_get_text(ShaderType p_type,int p_id) const{
  939. ERR_FAIL_INDEX_V(p_type,3,String());
  940. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),String());
  941. const Node& n = shader[p_type].node_map[p_id];
  942. ERR_FAIL_COND_V(n.type!=NODE_COMMENT,String());
  943. return n.param1;
  944. }
  945. void ShaderGraph::_request_update() {
  946. if (_pending_update_shader)
  947. return;
  948. _pending_update_shader=true;
  949. call_deferred("_update_shader");
  950. }
  951. Variant ShaderGraph::node_get_state(ShaderType p_type,int p_id) const {
  952. ERR_FAIL_INDEX_V(p_type,3,Variant());
  953. ERR_FAIL_COND_V(!shader[p_type].node_map.has(p_id),Variant());
  954. const Node& n = shader[p_type].node_map[p_id];
  955. Dictionary s;
  956. s["position"]=n.pos;
  957. s["param1"]=n.param1;
  958. s["param2"]=n.param2;
  959. Array keys;
  960. for (Map<int,Variant>::Element *E=n.defaults.front();E;E=E->next()) {
  961. keys.append(E->key());
  962. s[E->key()]=E->get();
  963. }
  964. s["default_keys"]=keys;
  965. return s;
  966. }
  967. void ShaderGraph::node_set_state(ShaderType p_type,int p_id,const Variant& p_state) {
  968. ERR_FAIL_INDEX(p_type,3);
  969. ERR_FAIL_COND(!shader[p_type].node_map.has(p_id));
  970. Node& n = shader[p_type].node_map[p_id];
  971. Dictionary d = p_state;
  972. ERR_FAIL_COND(!d.has("position"));
  973. ERR_FAIL_COND(!d.has("param1"));
  974. ERR_FAIL_COND(!d.has("param2"));
  975. ERR_FAIL_COND(!d.has("default_keys"));
  976. n.pos=d["position"];
  977. n.param1=d["param1"];
  978. n.param2=d["param2"];
  979. Array keys = d["default_keys"];
  980. for(int i=0;i<keys.size();i++) {
  981. n.defaults[keys[i]]=d[keys[i]];
  982. }
  983. }
  984. ShaderGraph::ShaderGraph(Mode p_mode) : Shader(p_mode) {
  985. //shader = VisualServer::get_singleton()->shader_create();
  986. _pending_update_shader=false;
  987. Node input;
  988. input.id=1;
  989. input.pos=Vector2(50,40);
  990. input.type=NODE_INPUT;
  991. Node output;
  992. output.id=0;
  993. output.pos=Vector2(350,40);
  994. output.type=NODE_OUTPUT;
  995. for(int i=0;i<3;i++) {
  996. shader[i].node_map.insert(0,output);
  997. shader[i].node_map.insert(1,input);
  998. }
  999. }
  1000. ShaderGraph::~ShaderGraph() {
  1001. //VisualServer::get_singleton()->free(shader);
  1002. }
  1003. const ShaderGraph::InOutParamInfo ShaderGraph::inout_param_info[]={
  1004. //material vertex in
  1005. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Vertex","SRC_VERTEX","",SLOT_TYPE_VEC,SLOT_IN},
  1006. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Normal","SRC_NORMAL","",SLOT_TYPE_VEC,SLOT_IN},
  1007. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Tangent","SRC_TANGENT","",SLOT_TYPE_VEC,SLOT_IN},
  1008. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"BinormalF","SRC_BINORMALF","",SLOT_TYPE_SCALAR,SLOT_IN},
  1009. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Color","SRC_COLOR","",SLOT_TYPE_VEC,SLOT_IN},
  1010. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Alpha","SRC_ALPHA","",SLOT_TYPE_SCALAR,SLOT_IN},
  1011. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"UV","SRC_UV","",SLOT_TYPE_VEC,SLOT_IN},
  1012. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"UV2","SRC_UV2","",SLOT_TYPE_VEC,SLOT_IN},
  1013. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"WorldMatrix","WORLD_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1014. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"InvCameraMatrix","INV_CAMERA_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1015. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"ProjectionMatrix","PROJECTION_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1016. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"ModelviewMatrix","MODELVIEW_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1017. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"InstanceID","INSTANCE_ID","",SLOT_TYPE_SCALAR,SLOT_IN},
  1018. //material vertex out
  1019. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Vertex","VERTEX","",SLOT_TYPE_VEC,SLOT_OUT},
  1020. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Normal","NORMAL","",SLOT_TYPE_VEC,SLOT_OUT},
  1021. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Tangent","TANGENT","",SLOT_TYPE_VEC,SLOT_OUT},
  1022. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Binormal","BINORMAL","",SLOT_TYPE_VEC,SLOT_OUT},
  1023. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"UV","UV",".xy",SLOT_TYPE_VEC,SLOT_OUT},
  1024. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"UV2","UV2",".xy",SLOT_TYPE_VEC,SLOT_OUT},
  1025. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Color","COLOR.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1026. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Alpha","COLOR.a","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1027. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Var1","VAR1.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1028. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"Var2","VAR2.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1029. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"SpecExp","SPEC_EXP","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1030. {MODE_MATERIAL,SHADER_TYPE_VERTEX,"PointSize","POINT_SIZE","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1031. //pixel vertex in
  1032. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Vertex","VERTEX","",SLOT_TYPE_VEC,SLOT_IN},
  1033. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Position","POSITION.xyz","",SLOT_TYPE_VEC,SLOT_IN},
  1034. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Normal","IN_NORMAL","",SLOT_TYPE_VEC,SLOT_IN},
  1035. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Tangent","TANGENT","",SLOT_TYPE_VEC,SLOT_IN},
  1036. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Binormal","BINORMAL","",SLOT_TYPE_VEC,SLOT_IN},
  1037. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"UV","vec3(UV,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1038. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"UV2","vec3(UV2,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1039. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"UVScreen","vec3(SCREEN_UV,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1040. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"PointCoord","POINT_COORD","",SLOT_TYPE_VEC,SLOT_IN},
  1041. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Color","COLOR.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1042. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Alpha","COLOR.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1043. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"InvCameraMatrix","INV_CAMERA_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1044. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Var1","VAR1.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1045. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Var2","VAR2.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1046. //pixel vertex out
  1047. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Diffuse","DIFFUSE_OUT","",SLOT_TYPE_VEC,SLOT_OUT},
  1048. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"DiffuseAlpha","ALPHA_OUT","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1049. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Specular","SPECULAR","",SLOT_TYPE_VEC,SLOT_OUT},
  1050. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"SpecularExp","SPEC_EXP","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1051. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Emission","EMISSION","",SLOT_TYPE_VEC,SLOT_OUT},
  1052. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Glow","GLOW","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1053. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"ShadeParam","SHADE_PARAM","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1054. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Normal","NORMAL","",SLOT_TYPE_VEC,SLOT_OUT},
  1055. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"NormalMap","NORMALMAP","",SLOT_TYPE_VEC,SLOT_OUT},
  1056. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"NormalMapDepth","NORMALMAP_DEPTH","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1057. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,"Discard","DISCARD",">0.5",SLOT_TYPE_SCALAR,SLOT_OUT},
  1058. //light in
  1059. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"Normal","NORMAL","",SLOT_TYPE_VEC,SLOT_IN},
  1060. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"LightDir","LIGHT_DIR","",SLOT_TYPE_VEC,SLOT_IN},
  1061. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"LightDiffuse","LIGHT_DIFFUSE","",SLOT_TYPE_VEC,SLOT_IN},
  1062. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"LightSpecular","LIGHT_SPECULAR","",SLOT_TYPE_VEC,SLOT_IN},
  1063. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"EyeVec","EYE_VEC","",SLOT_TYPE_VEC,SLOT_IN},
  1064. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"Diffuse","DIFFUSE","",SLOT_TYPE_VEC,SLOT_IN},
  1065. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"Specular","SPECULAR","",SLOT_TYPE_VEC,SLOT_IN},
  1066. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"SpecExp","SPECULAR_EXP","",SLOT_TYPE_SCALAR,SLOT_IN},
  1067. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"ShadeParam","SHADE_PARAM","",SLOT_TYPE_SCALAR,SLOT_IN},
  1068. //light out
  1069. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"Light","LIGHT","",SLOT_TYPE_VEC,SLOT_OUT},
  1070. {MODE_MATERIAL,SHADER_TYPE_LIGHT,"Shadow", "SHADOW", "",SLOT_TYPE_VEC, SLOT_OUT },
  1071. //canvas item vertex in
  1072. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Vertex","vec3(SRC_VERTEX,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1073. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"UV","SRC_UV","",SLOT_TYPE_VEC,SLOT_IN},
  1074. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Color","SRC_COLOR.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1075. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Alpha","SRC_COLOR.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1076. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"WorldMatrix","WORLD_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1077. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"ExtraMatrix","EXTRA_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1078. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"ProjectionMatrix","PROJECTION_MATRIX","",SLOT_TYPE_XFORM,SLOT_IN},
  1079. //canvas item vertex out
  1080. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Vertex","VERTEX",".xy",SLOT_TYPE_VEC,SLOT_OUT},
  1081. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"UV","UV",".xy",SLOT_TYPE_VEC,SLOT_OUT},
  1082. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Color","COLOR.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1083. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Alpha","COLOR.a","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1084. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Var1","VAR1.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1085. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"Var2","VAR2.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1086. {MODE_CANVAS_ITEM,SHADER_TYPE_VERTEX,"PointSize","POINT_SIZE","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1087. //canvas item fragment in
  1088. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Color","SRC_COLOR.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1089. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Alpha","SRC_COLOR.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1090. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"UV","vec3(UV,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1091. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"UVScreen","vec3(SCREEN_UV,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1092. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"TexPixelSize","vec3(TEXTURE_PIXEL_SIZE,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1093. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Var1","VAR1.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1094. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Var2","VAR2.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1095. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"PointCoord","POINT_COORD","",SLOT_TYPE_VEC,SLOT_IN},
  1096. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Position","POSITION","",SLOT_TYPE_VEC,SLOT_IN},
  1097. //canvas item fragment out
  1098. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Color","COLOR.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1099. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Alpha","COLOR.a","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1100. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"Normal","NORMAL","",SLOT_TYPE_VEC,SLOT_OUT},
  1101. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"NormalMap","NORMALMAP","",SLOT_TYPE_VEC,SLOT_OUT},
  1102. {MODE_CANVAS_ITEM,SHADER_TYPE_FRAGMENT,"NormalMapDepth","NORMALMAP_DEPTH","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1103. //canvas item light in
  1104. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Color","COLOR.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1105. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Alpha","COLOR.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1106. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Normal","NORMAL","",SLOT_TYPE_VEC,SLOT_IN},
  1107. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"UV","vec3(UV,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1108. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"LightColor","LIGHT_COLOR.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1109. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"LightAlpha","LIGHT_COLOR.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1110. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"LightHeight","LIGHT_HEIGHT","",SLOT_TYPE_SCALAR,SLOT_IN},
  1111. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"ShadowColor","LIGHT_SHADOW.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1112. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"ShadowAlpha","LIGHT_SHADOW.a","",SLOT_TYPE_SCALAR,SLOT_IN},
  1113. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"TexPixelSize","vec3(TEXTURE_PIXEL_SIZE,0)","",SLOT_TYPE_VEC,SLOT_IN},
  1114. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Var1","VAR1.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1115. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Var2","VAR2.rgb","",SLOT_TYPE_VEC,SLOT_IN},
  1116. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"PointCoord","POINT_COORD","",SLOT_TYPE_VEC,SLOT_IN},
  1117. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"Position","POSITION","",SLOT_TYPE_VEC,SLOT_IN},
  1118. //canvas item light out
  1119. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"LightColor","LIGHT.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1120. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"LightAlpha","LIGHT.a","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1121. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"ShadowColor","SHADOW.rgb","",SLOT_TYPE_VEC,SLOT_OUT},
  1122. {MODE_CANVAS_ITEM,SHADER_TYPE_LIGHT,"ShadowAlpha","SHADOW.a","",SLOT_TYPE_SCALAR,SLOT_OUT},
  1123. //end
  1124. {MODE_MATERIAL,SHADER_TYPE_FRAGMENT,NULL,NULL,NULL,SLOT_TYPE_SCALAR,SLOT_OUT},
  1125. };
  1126. void ShaderGraph::get_input_output_node_slot_info(Mode p_mode, ShaderType p_type, List<SlotInfo> *r_slots) {
  1127. const InOutParamInfo* iop = &inout_param_info[0];
  1128. while(iop->name) {
  1129. if (p_mode==iop->shader_mode && p_type==iop->shader_type) {
  1130. SlotInfo si;
  1131. si.dir=iop->dir;
  1132. si.name=iop->name;
  1133. si.type=iop->slot_type;
  1134. r_slots->push_back(si);
  1135. }
  1136. iop++;
  1137. }
  1138. }
  1139. const ShaderGraph::NodeSlotInfo ShaderGraph::node_slot_info[]= {
  1140. {NODE_SCALAR_CONST,{SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, //scalar constant
  1141. {NODE_VEC_CONST,{SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, //vec3 constant
  1142. {NODE_RGB_CONST,{SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, //rgb constant (shows a color picker instead)
  1143. {NODE_XFORM_CONST,{SLOT_MAX},{SLOT_TYPE_XFORM,SLOT_MAX}}, // 4x4 matrix constant
  1144. {NODE_TIME,{SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // time in seconds
  1145. {NODE_SCREEN_TEX,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // screen texture sampler (takes UV) (only usable in fragment shader)
  1146. {NODE_SCALAR_OP,{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar vs scalar op (mul,{SLOT_MAX},{SLOT_MAX}}, add,{SLOT_MAX},{SLOT_MAX}}, div,{SLOT_MAX},{SLOT_MAX}}, etc)
  1147. {NODE_VEC_OP,{SLOT_TYPE_VEC,SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // scalar vs scalar op (mul,{SLOT_MAX},{SLOT_MAX}}, add,{SLOT_MAX},{SLOT_MAX}}, div,{SLOT_MAX},{SLOT_MAX}}, etc)
  1148. {NODE_VEC_SCALAR_OP,{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 vs scalar op (mul,{SLOT_MAX},{SLOT_MAX}}, add,{SLOT_MAX},{SLOT_MAX}}, div,{SLOT_MAX},{SLOT_MAX}}, etc)
  1149. {NODE_RGB_OP,{SLOT_TYPE_VEC,SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 vs scalar op (mul,{SLOT_MAX},{SLOT_MAX}}, add,{SLOT_MAX},{SLOT_MAX}}, div,{SLOT_MAX},{SLOT_MAX}}, etc)
  1150. {NODE_XFORM_MULT,{SLOT_TYPE_XFORM,SLOT_TYPE_XFORM,SLOT_MAX},{SLOT_TYPE_XFORM,SLOT_MAX}}, // mat4 x mat4
  1151. {NODE_XFORM_VEC_MULT,{SLOT_TYPE_XFORM,SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // mat4 x vec3 mult (with no-translation option)
  1152. {NODE_XFORM_VEC_INV_MULT,{SLOT_TYPE_VEC,SLOT_TYPE_XFORM,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // mat4 x vec3 inverse mult (with no-translation option)
  1153. {NODE_SCALAR_FUNC,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar function (sin,{SLOT_MAX},{SLOT_MAX}}, cos,{SLOT_MAX},{SLOT_MAX}}, etc)
  1154. {NODE_VEC_FUNC,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vector function (normalize,{SLOT_MAX},{SLOT_MAX}}, negate,{SLOT_MAX},{SLOT_MAX}}, reciprocal,{SLOT_MAX},{SLOT_MAX}}, rgb2hsv,{SLOT_MAX},{SLOT_MAX}}, hsv2rgb,{SLOT_MAX},{SLOT_MAX}}, etc,{SLOT_MAX},{SLOT_MAX}}, etc)
  1155. {NODE_VEC_LEN,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 length
  1156. {NODE_DOT_PROD,{SLOT_TYPE_VEC,SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 . vec3 (dot product -> scalar output)
  1157. {NODE_VEC_TO_SCALAR,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR}}, // 1 vec3 input,{SLOT_MAX},{SLOT_MAX}}, 3 scalar outputs
  1158. {NODE_SCALAR_TO_VEC,{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR},{SLOT_TYPE_VEC,SLOT_MAX}}, // 3 scalar input,{SLOT_MAX},{SLOT_MAX}}, 1 vec3 output
  1159. {NODE_SCALAR_INTERP,{SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR,SLOT_TYPE_SCALAR},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar interpolation (with optional curve)
  1160. {NODE_VEC_INTERP,{SLOT_TYPE_VEC,SLOT_TYPE_VEC,SLOT_TYPE_SCALAR},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 interpolation (with optional curve)
  1161. {NODE_COLOR_RAMP,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 interpolation (with optional curve)
  1162. {NODE_CURVE_MAP,{SLOT_TYPE_SCALAR,SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // vec3 interpolation (with optional curve)
  1163. {NODE_SCALAR_INPUT,{SLOT_MAX},{SLOT_TYPE_SCALAR,SLOT_MAX}}, // scalar uniform (assignable in material)
  1164. {NODE_VEC_INPUT,{SLOT_MAX},{SLOT_TYPE_VEC,SLOT_MAX}}, // vec3 uniform (assignable in material)
  1165. {NODE_RGB_INPUT,{SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // color uniform (assignable in material)
  1166. {NODE_XFORM_INPUT,{SLOT_MAX},{SLOT_TYPE_XFORM,SLOT_MAX}}, // mat4 uniform (assignable in material)
  1167. {NODE_TEXTURE_INPUT,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // texture input (assignable in material)
  1168. {NODE_CUBEMAP_INPUT,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // cubemap input (assignable in material)
  1169. {NODE_DEFAULT_TEXTURE,{SLOT_TYPE_VEC,SLOT_MAX},{SLOT_TYPE_VEC,SLOT_TYPE_SCALAR,SLOT_MAX}}, // cubemap input (assignable in material)
  1170. {NODE_COMMENT,{SLOT_MAX},{SLOT_MAX}}, // comment
  1171. {NODE_TYPE_MAX,{SLOT_MAX},{SLOT_MAX}}
  1172. };
  1173. int ShaderGraph::get_node_input_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type) {
  1174. if (p_type==NODE_INPUT || p_type==NODE_OUTPUT) {
  1175. const InOutParamInfo* iop = &inout_param_info[0];
  1176. int pc=0;
  1177. while(iop->name) {
  1178. if (p_mode==iop->shader_mode && p_shader_type==iop->shader_type) {
  1179. if (iop->dir==SLOT_OUT)
  1180. pc++;
  1181. }
  1182. iop++;
  1183. }
  1184. return pc;
  1185. } else if (p_type==NODE_VEC_TO_XFORM){
  1186. return 4;
  1187. } else if (p_type==NODE_XFORM_TO_VEC){
  1188. return 1;
  1189. } else {
  1190. const NodeSlotInfo*nsi=&node_slot_info[0];
  1191. while(nsi->type!=NODE_TYPE_MAX) {
  1192. if (nsi->type==p_type) {
  1193. int pc=0;
  1194. for(int i=0;i<NodeSlotInfo::MAX_INS;i++) {
  1195. if (nsi->ins[i]==SLOT_MAX)
  1196. break;
  1197. pc++;
  1198. }
  1199. return pc;
  1200. }
  1201. nsi++;
  1202. }
  1203. return 0;
  1204. }
  1205. }
  1206. int ShaderGraph::get_node_output_slot_count(Mode p_mode, ShaderType p_shader_type,NodeType p_type){
  1207. if (p_type==NODE_INPUT || p_type==NODE_OUTPUT) {
  1208. const InOutParamInfo* iop = &inout_param_info[0];
  1209. int pc=0;
  1210. while(iop->name) {
  1211. if (p_mode==iop->shader_mode && p_shader_type==iop->shader_type) {
  1212. if (iop->dir==SLOT_IN)
  1213. pc++;
  1214. }
  1215. iop++;
  1216. }
  1217. return pc;
  1218. } else if (p_type==NODE_VEC_TO_XFORM){
  1219. return 1;
  1220. } else if (p_type==NODE_XFORM_TO_VEC){
  1221. return 4;
  1222. } else {
  1223. const NodeSlotInfo*nsi=&node_slot_info[0];
  1224. while(nsi->type!=NODE_TYPE_MAX) {
  1225. if (nsi->type==p_type) {
  1226. int pc=0;
  1227. for(int i=0;i<NodeSlotInfo::MAX_OUTS;i++) {
  1228. if (nsi->outs[i]==SLOT_MAX)
  1229. break;
  1230. pc++;
  1231. }
  1232. return pc;
  1233. }
  1234. nsi++;
  1235. }
  1236. return 0;
  1237. }
  1238. }
  1239. ShaderGraph::SlotType ShaderGraph::get_node_input_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx){
  1240. if (p_type==NODE_INPUT || p_type==NODE_OUTPUT) {
  1241. const InOutParamInfo* iop = &inout_param_info[0];
  1242. int pc=0;
  1243. while(iop->name) {
  1244. if (p_mode==iop->shader_mode && p_shader_type==iop->shader_type) {
  1245. if (iop->dir==SLOT_OUT) {
  1246. if (pc==p_idx)
  1247. return iop->slot_type;
  1248. pc++;
  1249. }
  1250. }
  1251. iop++;
  1252. }
  1253. ERR_FAIL_V(SLOT_MAX);
  1254. } else if (p_type==NODE_VEC_TO_XFORM){
  1255. return SLOT_TYPE_VEC;
  1256. } else if (p_type==NODE_XFORM_TO_VEC){
  1257. return SLOT_TYPE_XFORM;
  1258. } else {
  1259. const NodeSlotInfo*nsi=&node_slot_info[0];
  1260. while(nsi->type!=NODE_TYPE_MAX) {
  1261. if (nsi->type==p_type) {
  1262. for(int i=0;i<NodeSlotInfo::MAX_INS;i++) {
  1263. if (nsi->ins[i]==SLOT_MAX)
  1264. break;
  1265. if (i==p_idx)
  1266. return nsi->ins[i];
  1267. }
  1268. }
  1269. nsi++;
  1270. }
  1271. ERR_FAIL_V(SLOT_MAX);
  1272. }
  1273. }
  1274. ShaderGraph::SlotType ShaderGraph::get_node_output_slot_type(Mode p_mode, ShaderType p_shader_type,NodeType p_type,int p_idx){
  1275. if (p_type==NODE_INPUT || p_type==NODE_OUTPUT) {
  1276. const InOutParamInfo* iop = &inout_param_info[0];
  1277. int pc=0;
  1278. while(iop->name) {
  1279. if (p_mode==iop->shader_mode && p_shader_type==iop->shader_type) {
  1280. if (iop->dir==SLOT_IN) {
  1281. if (pc==p_idx)
  1282. return iop->slot_type;
  1283. pc++;
  1284. }
  1285. }
  1286. iop++;
  1287. }
  1288. ERR_FAIL_V(SLOT_MAX);
  1289. } else if (p_type==NODE_VEC_TO_XFORM){
  1290. return SLOT_TYPE_XFORM;
  1291. } else if (p_type==NODE_XFORM_TO_VEC){
  1292. return SLOT_TYPE_VEC;
  1293. } else {
  1294. const NodeSlotInfo*nsi=&node_slot_info[0];
  1295. while(nsi->type!=NODE_TYPE_MAX) {
  1296. if (nsi->type==p_type) {
  1297. for(int i=0;i<NodeSlotInfo::MAX_OUTS;i++) {
  1298. if (nsi->outs[i]==SLOT_MAX)
  1299. break;
  1300. if (i==p_idx)
  1301. return nsi->outs[i];
  1302. }
  1303. }
  1304. nsi++;
  1305. }
  1306. ERR_FAIL_V(SLOT_MAX);
  1307. }
  1308. }
  1309. void ShaderGraph::_update_shader() {
  1310. String code[3];
  1311. List<StringName> names;
  1312. get_default_texture_param_list(&names);
  1313. for (List<StringName>::Element *E=names.front();E;E=E->next()) {
  1314. set_default_texture_param(E->get(),Ref<Texture>());
  1315. }
  1316. for(int i=0;i<3;i++) {
  1317. int idx=0;
  1318. for (Map<int,Node>::Element *E=shader[i].node_map.front();E;E=E->next()) {
  1319. E->get().sort_order=idx++;
  1320. }
  1321. //simple method for graph solving using bubblesort derived algorithm
  1322. int iters=0;
  1323. int iter_max=shader[i].node_map.size()*shader[i].node_map.size();
  1324. while(true) {
  1325. if (iters>iter_max)
  1326. break;
  1327. int swaps=0;
  1328. for (Map<int,Node>::Element *E=shader[i].node_map.front();E;E=E->next()) {
  1329. for(Map<int,SourceSlot>::Element *F=E->get().connections.front();F;F=F->next()) {
  1330. //this is kinda slow, could be sped up
  1331. Map<int,Node>::Element *G = shader[i].node_map.find(F->get().id);
  1332. ERR_FAIL_COND(!G);
  1333. if (G->get().sort_order > E->get().sort_order) {
  1334. SWAP(G->get().sort_order,E->get().sort_order);
  1335. swaps++;
  1336. }
  1337. }
  1338. }
  1339. iters++;
  1340. if (swaps==0) {
  1341. iters=0;
  1342. break;
  1343. }
  1344. }
  1345. if (iters>0) {
  1346. shader[i].error=GRAPH_ERROR_CYCLIC;
  1347. continue;
  1348. }
  1349. Vector<Node*> order;
  1350. order.resize(shader[i].node_map.size());
  1351. for (Map<int,Node>::Element *E=shader[i].node_map.front();E;E=E->next()) {
  1352. order[E->get().sort_order]=&E->get();
  1353. }
  1354. //generate code for the ordered graph
  1355. bool failed=false;
  1356. if (i==SHADER_TYPE_FRAGMENT && get_mode()==MODE_MATERIAL) {
  1357. code[i]+="vec3 DIFFUSE_OUT=vec3(0,0,0);\n";
  1358. code[i]+="float ALPHA_OUT=0;\n";
  1359. }
  1360. Map<String,String> inputs_xlate;
  1361. Map<String,String> input_names_xlate;
  1362. Set<String> inputs_used;
  1363. for(int j=0;j<order.size();j++) {
  1364. Node *n=order[j];
  1365. if (n->type==NODE_INPUT) {
  1366. const InOutParamInfo* iop = &inout_param_info[0];
  1367. int idx=0;
  1368. while(iop->name) {
  1369. if (get_mode()==iop->shader_mode && i==iop->shader_type && SLOT_IN==iop->dir) {
  1370. const char *typestr[4]={"float","vec3","mat4","texture"};
  1371. String vname=("nd"+itos(n->id)+"sl"+itos(idx));
  1372. inputs_xlate[vname]=String(typestr[iop->slot_type])+" "+vname+"="+iop->variable+";\n";
  1373. input_names_xlate[vname]=iop->variable;
  1374. idx++;
  1375. }
  1376. iop++;
  1377. }
  1378. } else if (n->type==NODE_OUTPUT) {
  1379. bool use_alpha=false;
  1380. const InOutParamInfo* iop = &inout_param_info[0];
  1381. int idx=0;
  1382. while(iop->name) {
  1383. if (get_mode()==iop->shader_mode && i==iop->shader_type && SLOT_OUT==iop->dir) {
  1384. if (n->connections.has(idx)) {
  1385. String iname=("nd"+itos(n->connections[idx].id)+"sl"+itos(n->connections[idx].slot));
  1386. if (node_get_type(ShaderType(i),n->connections[idx].id)==NODE_INPUT)
  1387. inputs_used.insert(iname);
  1388. code[i]+=String(iop->variable)+"="+iname+String(iop->postfix)+";\n";
  1389. if (i==SHADER_TYPE_FRAGMENT && get_mode()==MODE_MATERIAL && String(iop->name)=="DiffuseAlpha")
  1390. use_alpha=true;
  1391. }
  1392. idx++;
  1393. }
  1394. iop++;
  1395. }
  1396. if (i==SHADER_TYPE_FRAGMENT && get_mode()==MODE_MATERIAL) {
  1397. if (use_alpha) {
  1398. code[i]+="DIFFUSE_ALPHA=vec4(DIFFUSE_OUT,ALPHA_OUT);\n";
  1399. } else {
  1400. code[i]+="DIFFUSE=DIFFUSE_OUT;\n";
  1401. }
  1402. }
  1403. } else {
  1404. Vector<String> inputs;
  1405. int max = get_node_input_slot_count(get_mode(),ShaderType(i),n->type);
  1406. for(int k=0;k<max;k++) {
  1407. String iname;
  1408. if (!n->connections.has(k)) {
  1409. iname="nd"+itos(n->id)+"sl"+itos(k)+"def";
  1410. } else {
  1411. iname="nd"+itos(n->connections[k].id)+"sl"+itos(n->connections[k].slot);
  1412. if (node_get_type(ShaderType(i),n->connections[k].id)==NODE_INPUT) {
  1413. inputs_used.insert(iname);
  1414. }
  1415. }
  1416. inputs.push_back(iname);
  1417. }
  1418. if (failed)
  1419. break;
  1420. if (n->type==NODE_TEXTURE_INPUT || n->type==NODE_CUBEMAP_INPUT) {
  1421. set_default_texture_param(n->param1,n->param2);
  1422. }
  1423. _add_node_code(ShaderType(i),n,inputs,code[i]);
  1424. }
  1425. }
  1426. if (failed)
  1427. continue;
  1428. for(Set<String>::Element *E=inputs_used.front();E;E=E->next()) {
  1429. ERR_CONTINUE( !inputs_xlate.has(E->get()));
  1430. code[i]=inputs_xlate[E->get()]+code[i];
  1431. String name=input_names_xlate[E->get()];
  1432. if (i==SHADER_TYPE_VERTEX && get_mode()==MODE_MATERIAL) {
  1433. if (name==("SRC_COLOR"))
  1434. code[i]="vec3 SRC_COLOR=COLOR.rgb;\n"+code[i];
  1435. if (name==("SRC_ALPHA"))
  1436. code[i]="float SRC_ALPHA=COLOR.a;\n"+code[i];
  1437. if (name==("SRC_UV"))
  1438. code[i]="vec3 SRC_UV=vec3(UV,0);\n"+code[i];
  1439. if (name==("SRC_UV2"))
  1440. code[i]="float SRC_UV2=vec3(UV2,0);\n"+code[i];
  1441. } else if (i==SHADER_TYPE_FRAGMENT && get_mode()==MODE_MATERIAL) {
  1442. if (name==("IN_NORMAL"))
  1443. code[i]="vec3 IN_NORMAL=NORMAL;\n"+code[i];
  1444. } else if (i==SHADER_TYPE_VERTEX && get_mode()==MODE_CANVAS_ITEM) {
  1445. if (name==("SRC_COLOR"))
  1446. code[i]="vec3 SRC_COLOR=COLOR.rgb;\n"+code[i];
  1447. if (name==("SRC_UV"))
  1448. code[i]="vec3 SRC_UV=vec3(UV,0);\n"+code[i];
  1449. }
  1450. }
  1451. shader[i].error=GRAPH_OK;
  1452. }
  1453. bool all_ok=true;
  1454. for(int i=0;i<3;i++) {
  1455. if (shader[i].error!=GRAPH_OK)
  1456. all_ok=false;
  1457. }
  1458. /*print_line("VERTEX: \n"+code[0]);
  1459. print_line("FRAGMENT: \n"+code[1]);
  1460. print_line("LIGHT: \n"+code[2]);*/
  1461. if (all_ok) {
  1462. set_code(code[0],code[1],code[2]);
  1463. }
  1464. //do shader here
  1465. _pending_update_shader=false;
  1466. emit_signal(SceneStringNames::get_singleton()->updated);
  1467. }
  1468. void ShaderGraph::_plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d,uint8_t* p_heights,bool *p_useds) {
  1469. float geometry[4][4];
  1470. float tmp1[4][4];
  1471. float tmp2[4][4];
  1472. float deltas[4][4];
  1473. double x, dx, dx2, dx3;
  1474. double y, dy, dy2, dy3;
  1475. double d, d2, d3;
  1476. int lastx, lasty;
  1477. int newx, newy;
  1478. int ntimes;
  1479. int i,j;
  1480. int xmax=255;
  1481. int ymax=255;
  1482. /* construct the geometry matrix from the segment */
  1483. for (i = 0; i < 4; i++) {
  1484. geometry[i][2] = 0;
  1485. geometry[i][3] = 0;
  1486. }
  1487. geometry[0][0] = (p_a[0] * xmax);
  1488. geometry[1][0] = (p_b[0] * xmax);
  1489. geometry[2][0] = (p_c[0] * xmax);
  1490. geometry[3][0] = (p_d[0] * xmax);
  1491. geometry[0][1] = (p_a[1] * ymax);
  1492. geometry[1][1] = (p_b[1] * ymax);
  1493. geometry[2][1] = (p_c[1] * ymax);
  1494. geometry[3][1] = (p_d[1] * ymax);
  1495. /* subdivide the curve ntimes (1000) times */
  1496. ntimes = 4 * xmax;
  1497. /* ntimes can be adjusted to give a finer or coarser curve */
  1498. d = 1.0 / ntimes;
  1499. d2 = d * d;
  1500. d3 = d * d * d;
  1501. /* construct a temporary matrix for determining the forward differencing deltas */
  1502. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  1503. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  1504. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  1505. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  1506. /* compose the basis and geometry matrices */
  1507. static const float CR_basis[4][4] = {
  1508. { -0.5, 1.5, -1.5, 0.5 },
  1509. { 1.0, -2.5, 2.0, -0.5 },
  1510. { -0.5, 0.0, 0.5, 0.0 },
  1511. { 0.0, 1.0, 0.0, 0.0 },
  1512. };
  1513. for (i = 0; i < 4; i++)
  1514. {
  1515. for (j = 0; j < 4; j++)
  1516. {
  1517. tmp1[i][j] = (CR_basis[i][0] * geometry[0][j] +
  1518. CR_basis[i][1] * geometry[1][j] +
  1519. CR_basis[i][2] * geometry[2][j] +
  1520. CR_basis[i][3] * geometry[3][j]);
  1521. }
  1522. }
  1523. /* compose the above results to get the deltas matrix */
  1524. for (i = 0; i < 4; i++)
  1525. {
  1526. for (j = 0; j < 4; j++)
  1527. {
  1528. deltas[i][j] = (tmp2[i][0] * tmp1[0][j] +
  1529. tmp2[i][1] * tmp1[1][j] +
  1530. tmp2[i][2] * tmp1[2][j] +
  1531. tmp2[i][3] * tmp1[3][j]);
  1532. }
  1533. }
  1534. /* extract the x deltas */
  1535. x = deltas[0][0];
  1536. dx = deltas[1][0];
  1537. dx2 = deltas[2][0];
  1538. dx3 = deltas[3][0];
  1539. /* extract the y deltas */
  1540. y = deltas[0][1];
  1541. dy = deltas[1][1];
  1542. dy2 = deltas[2][1];
  1543. dy3 = deltas[3][1];
  1544. lastx = CLAMP (x, 0, xmax);
  1545. lasty = CLAMP (y, 0, ymax);
  1546. p_heights[lastx] = lasty;
  1547. p_useds[lastx] = true;
  1548. /* loop over the curve */
  1549. for (i = 0; i < ntimes; i++)
  1550. {
  1551. /* increment the x values */
  1552. x += dx;
  1553. dx += dx2;
  1554. dx2 += dx3;
  1555. /* increment the y values */
  1556. y += dy;
  1557. dy += dy2;
  1558. dy2 += dy3;
  1559. newx = CLAMP ((Math::round (x)), 0, xmax);
  1560. newy = CLAMP ((Math::round (y)), 0, ymax);
  1561. /* if this point is different than the last one...then draw it */
  1562. if ((lastx != newx) || (lasty != newy))
  1563. {
  1564. p_useds[newx]=true;
  1565. p_heights[newx]=newy;
  1566. }
  1567. lastx = newx;
  1568. lasty = newy;
  1569. }
  1570. }
  1571. void ShaderGraph::_add_node_code(ShaderType p_type,Node *p_node,const Vector<String>& p_inputs,String& code) {
  1572. const char *typestr[4]={"float","vec3","mat4","texture"};
  1573. #define OUTNAME(id, slot) (String(typestr[get_node_output_slot_type(get_mode(), p_type, p_node->type, slot)]) + " " + ("nd" + itos(id) + "sl" + itos(slot)))
  1574. #define OUTVAR(id, slot) ("nd" + itos(id) + "sl" + itos(slot))
  1575. #define DEF_VEC(slot) \
  1576. if (p_inputs[slot].ends_with("def")) { \
  1577. Vector3 v = p_node->defaults[slot]; \
  1578. code += String(typestr[1]) + " " + p_inputs[slot] + "=vec3(" + v + ");\n"; \
  1579. }
  1580. #define DEF_SCALAR(slot) \
  1581. if (p_inputs[slot].ends_with("def")) { \
  1582. double v = p_node->defaults[slot]; \
  1583. code += String(typestr[0]) + " " + p_inputs[slot] + "=" + rtos(v) + ";\n"; \
  1584. }
  1585. #define DEF_COLOR(slot) \
  1586. if (p_inputs[slot].ends_with("def")) { \
  1587. Color col = p_node->defaults[slot]; \
  1588. code += String(typestr[1]) + " " + p_inputs[slot] + "=vec3(" + rtos(col.r) + "," + rtos(col.g) + "," + rtos(col.b) + ");\n"; \
  1589. }
  1590. #define DEF_MATRIX(slot) \
  1591. if (p_inputs[slot].ends_with("def")) { \
  1592. Transform xf = p_node->defaults[slot]; \
  1593. code += String(typestr[2]) + " " + p_inputs[slot] + "=mat4(\n"; \
  1594. code += "\tvec4(vec3(" + rtos(xf.basis.get_axis(0).x) + "," + rtos(xf.basis.get_axis(0).y) + "," + rtos(xf.basis.get_axis(0).z) + "),0),\n"; \
  1595. code += "\tvec4(vec3(" + rtos(xf.basis.get_axis(1).x) + "," + rtos(xf.basis.get_axis(1).y) + "," + rtos(xf.basis.get_axis(1).z) + "),0),\n"; \
  1596. code += "\tvec4(vec3(" + rtos(xf.basis.get_axis(2).x) + "," + rtos(xf.basis.get_axis(2).y) + "," + rtos(xf.basis.get_axis(2).z) + "),0),\n"; \
  1597. code += "\tvec4(vec3(" + rtos(xf.origin.x) + "," + rtos(xf.origin.y) + "," + rtos(xf.origin.z) + "),1)\n"; \
  1598. code += ");\n"; \
  1599. }
  1600. switch(p_node->type) {
  1601. case NODE_INPUT: {
  1602. }break;
  1603. case NODE_SCALAR_CONST: {
  1604. double scalar = p_node->param1;
  1605. code+=OUTNAME(p_node->id,0)+"="+rtos(scalar)+";\n";
  1606. }break;
  1607. case NODE_VEC_CONST: {
  1608. Vector3 vec = p_node->param1;
  1609. code+=OUTNAME(p_node->id,0)+"=vec3("+rtos(vec.x)+","+rtos(vec.y)+","+rtos(vec.z)+");\n";
  1610. }break;
  1611. case NODE_RGB_CONST: {
  1612. Color col = p_node->param1;
  1613. code+=OUTNAME(p_node->id,0)+"=vec3("+rtos(col.r)+","+rtos(col.g)+","+rtos(col.b)+");\n";
  1614. code+=OUTNAME(p_node->id,1)+"="+rtos(col.a)+";\n";
  1615. }break;
  1616. case NODE_XFORM_CONST: {
  1617. Transform xf = p_node->param1;
  1618. code+=OUTNAME(p_node->id,0)+"=mat4(\n";
  1619. code+="\tvec4(vec3("+rtos(xf.basis.get_axis(0).x)+","+rtos(xf.basis.get_axis(0).y)+","+rtos(xf.basis.get_axis(0).z)+"),0),\n";
  1620. code+="\tvec4(vec3("+rtos(xf.basis.get_axis(1).x)+","+rtos(xf.basis.get_axis(1).y)+","+rtos(xf.basis.get_axis(1).z)+"),0),\n";
  1621. code+="\tvec4(vec3("+rtos(xf.basis.get_axis(2).x)+","+rtos(xf.basis.get_axis(2).y)+","+rtos(xf.basis.get_axis(2).z)+"),0),\n";
  1622. code+="\tvec4(vec3("+rtos(xf.origin.x)+","+rtos(xf.origin.y)+","+rtos(xf.origin.z)+"),1)\n";
  1623. code+=");";
  1624. }break;
  1625. case NODE_TIME: {
  1626. code+=OUTNAME(p_node->id,0)+"=TIME;\n";
  1627. }break;
  1628. case NODE_SCREEN_TEX: {
  1629. DEF_VEC(0);
  1630. code+=OUTNAME(p_node->id,0)+"=texscreen("+p_inputs[0]+".xy);\n";
  1631. }break;
  1632. case NODE_SCALAR_OP: {
  1633. DEF_SCALAR(0);
  1634. DEF_SCALAR(1);
  1635. int op = p_node->param1;
  1636. String optxt;
  1637. switch(op) {
  1638. case SCALAR_OP_ADD: optxt = p_inputs[0]+"+"+p_inputs[1]+";"; break;
  1639. case SCALAR_OP_SUB: optxt = p_inputs[0]+"-"+p_inputs[1]+";"; break;
  1640. case SCALAR_OP_MUL: optxt = p_inputs[0]+"*"+p_inputs[1]+";"; break;
  1641. case SCALAR_OP_DIV: optxt = p_inputs[0]+"/"+p_inputs[1]+";"; break;
  1642. case SCALAR_OP_MOD: optxt = "mod("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1643. case SCALAR_OP_POW: optxt = "pow("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1644. case SCALAR_OP_MAX: optxt = "max("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1645. case SCALAR_OP_MIN: optxt = "min("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1646. case SCALAR_OP_ATAN2: optxt = "atan2("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1647. }
  1648. code+=OUTNAME(p_node->id,0)+"="+optxt+"\n";
  1649. }break;
  1650. case NODE_VEC_OP: {
  1651. DEF_VEC(0);
  1652. DEF_VEC(1);
  1653. int op = p_node->param1;
  1654. String optxt;
  1655. switch(op) {
  1656. case VEC_OP_ADD: optxt = p_inputs[0]+"+"+p_inputs[1]+";"; break;
  1657. case VEC_OP_SUB: optxt = p_inputs[0]+"-"+p_inputs[1]+";"; break;
  1658. case VEC_OP_MUL: optxt = p_inputs[0]+"*"+p_inputs[1]+";"; break;
  1659. case VEC_OP_DIV: optxt = p_inputs[0]+"/"+p_inputs[1]+";"; break;
  1660. case VEC_OP_MOD: optxt = "mod("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1661. case VEC_OP_POW: optxt = "pow("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1662. case VEC_OP_MAX: optxt = "max("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1663. case VEC_OP_MIN: optxt = "min("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1664. case VEC_OP_CROSS: optxt = "cross("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1665. }
  1666. code+=OUTNAME(p_node->id,0)+"="+optxt+"\n";
  1667. }break;
  1668. case NODE_VEC_SCALAR_OP: {
  1669. DEF_VEC(0);
  1670. DEF_SCALAR(1);
  1671. int op = p_node->param1;
  1672. String optxt;
  1673. switch(op) {
  1674. case VEC_SCALAR_OP_MUL: optxt = p_inputs[0]+"*"+p_inputs[1]+";"; break;
  1675. case VEC_SCALAR_OP_DIV: optxt = p_inputs[0]+"/"+p_inputs[1]+";"; break;
  1676. case VEC_SCALAR_OP_POW: optxt = "pow("+p_inputs[0]+","+p_inputs[1]+");"; break;
  1677. }
  1678. code+=OUTNAME(p_node->id,0)+"="+optxt+"\n";
  1679. }break;
  1680. case NODE_RGB_OP: {
  1681. DEF_COLOR(0);
  1682. DEF_COLOR(1);
  1683. int op = p_node->param1;
  1684. static const char*axisn[3]={"x","y","z"};
  1685. switch(op) {
  1686. case RGB_OP_SCREEN: {
  1687. code += OUTNAME(p_node->id,0)+"=vec3(1.0)-(vec3(1.0)-"+p_inputs[0]+")*(vec3(1.0)-"+p_inputs[1]+");\n";
  1688. } break;
  1689. case RGB_OP_DIFFERENCE: {
  1690. code += OUTNAME(p_node->id,0)+"=abs("+p_inputs[0]+"-"+p_inputs[1]+");\n";
  1691. } break;
  1692. case RGB_OP_DARKEN: {
  1693. code += OUTNAME(p_node->id,0)+"=min("+p_inputs[0]+","+p_inputs[1]+");\n";
  1694. } break;
  1695. case RGB_OP_LIGHTEN: {
  1696. code += OUTNAME(p_node->id,0)+"=max("+p_inputs[0]+","+p_inputs[1]+");\n";
  1697. } break;
  1698. case RGB_OP_OVERLAY: {
  1699. code += OUTNAME(p_node->id,0)+";\n";
  1700. for(int i=0;i<3;i++) {
  1701. code += "{\n";
  1702. code += "\tfloat base="+p_inputs[0]+"."+axisn[i]+";\n";
  1703. code += "\tfloat blend="+p_inputs[1]+"."+axisn[i]+";\n";
  1704. code += "\tif (base < 0.5) {\n";
  1705. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = 2.0 * base * blend;\n";
  1706. code += "\t} else {\n";
  1707. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);\n";
  1708. code += "\t}\n";
  1709. code += "}\n";
  1710. }
  1711. } break;
  1712. case RGB_OP_DODGE: {
  1713. code += OUTNAME(p_node->id,0)+"=("+p_inputs[0]+")/(vec3(1.0)-"+p_inputs[1]+");\n";
  1714. } break;
  1715. case RGB_OP_BURN: {
  1716. code += OUTNAME(p_node->id,0)+"=vec3(1.0)-(vec3(1.0)-"+p_inputs[0]+")/("+p_inputs[1]+");\n";
  1717. } break;
  1718. case RGB_OP_SOFT_LIGHT: {
  1719. code += OUTNAME(p_node->id,0)+";\n";
  1720. for(int i=0;i<3;i++) {
  1721. code += "{\n";
  1722. code += "\tfloat base="+p_inputs[0]+"."+axisn[i]+";\n";
  1723. code += "\tfloat blend="+p_inputs[1]+"."+axisn[i]+";\n";
  1724. code += "\tif (base < 0.5) {\n";
  1725. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = (base * (blend+0.5));\n";
  1726. code += "\t} else {\n";
  1727. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = (1 - (1-base) * (1-(blend-0.5)));\n";
  1728. code += "\t}\n";
  1729. code += "}\n";
  1730. }
  1731. } break;
  1732. case RGB_OP_HARD_LIGHT: {
  1733. code += OUTNAME(p_node->id,0)+";\n";
  1734. for(int i=0;i<3;i++) {
  1735. code += "{\n";
  1736. code += "\tfloat base="+p_inputs[0]+"."+axisn[i]+";\n";
  1737. code += "\tfloat blend="+p_inputs[1]+"."+axisn[i]+";\n";
  1738. code += "\tif (base < 0.5) {\n";
  1739. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = (base * (2*blend));\n";
  1740. code += "\t} else {\n";
  1741. code += "\t\t"+OUTVAR(p_node->id,0)+"."+axisn[i]+" = (1 - (1-base) * (1-2*(blend-0.5)));\n";
  1742. code += "\t}\n";
  1743. code += "}\n";
  1744. }
  1745. } break;
  1746. }
  1747. }break;
  1748. case NODE_XFORM_MULT: {
  1749. DEF_MATRIX(0);
  1750. DEF_MATRIX(1);
  1751. code += OUTNAME(p_node->id,0)+"="+p_inputs[0]+"*"+p_inputs[1]+";\n";
  1752. }break;
  1753. case NODE_XFORM_VEC_MULT: {
  1754. DEF_MATRIX(0);
  1755. DEF_VEC(1);
  1756. bool no_translation = p_node->param1;
  1757. if (no_translation) {
  1758. code += OUTNAME(p_node->id,0)+"=("+p_inputs[0]+"*vec4("+p_inputs[1]+",0)).xyz;\n";
  1759. } else {
  1760. code += OUTNAME(p_node->id,0)+"=("+p_inputs[0]+"*vec4("+p_inputs[1]+",1)).xyz;\n";
  1761. }
  1762. }break;
  1763. case NODE_XFORM_VEC_INV_MULT: {
  1764. DEF_VEC(0);
  1765. DEF_MATRIX(1);
  1766. bool no_translation = p_node->param1;
  1767. if (no_translation) {
  1768. code += OUTNAME(p_node->id,0)+"=("+p_inputs[1]+"*vec4("+p_inputs[0]+",0)).xyz;\n";
  1769. } else {
  1770. code += OUTNAME(p_node->id,0)+"=("+p_inputs[1]+"*vec4("+p_inputs[0]+",1)).xyz;\n";
  1771. }
  1772. }break;
  1773. case NODE_SCALAR_FUNC: {
  1774. DEF_SCALAR(0);
  1775. static const char*scalar_func_id[SCALAR_MAX_FUNC]={
  1776. "sin($)",
  1777. "cos($)",
  1778. "tan($)",
  1779. "asin($)",
  1780. "acos($)",
  1781. "atan($)",
  1782. "sinh($)",
  1783. "cosh($)",
  1784. "tanh($)",
  1785. "log($)",
  1786. "exp($)",
  1787. "sqrt($)",
  1788. "abs($)",
  1789. "sign($)",
  1790. "floor($)",
  1791. "round($)",
  1792. "ceil($)",
  1793. "fract($)",
  1794. "min(max($,0),1)",
  1795. "-($)",
  1796. };
  1797. int func = p_node->param1;
  1798. ERR_FAIL_INDEX(func,SCALAR_MAX_FUNC);
  1799. code += OUTNAME(p_node->id,0)+"="+String(scalar_func_id[func]).replace("$",p_inputs[0])+";\n";
  1800. } break;
  1801. case NODE_VEC_FUNC: {
  1802. DEF_VEC(0);
  1803. static const char*vec_func_id[VEC_MAX_FUNC]={
  1804. "normalize($)",
  1805. "max(min($,vec3(1,1,1)),vec3(0,0,0))",
  1806. "-($)",
  1807. "1.0/($)",
  1808. "",
  1809. "",
  1810. };
  1811. int func = p_node->param1;
  1812. ERR_FAIL_INDEX(func,VEC_MAX_FUNC);
  1813. if (func==VEC_FUNC_RGB2HSV) {
  1814. code += OUTNAME(p_node->id,0)+";\n";
  1815. code+="{\n";
  1816. code+="\tvec3 c = "+p_inputs[0]+";\n";
  1817. code+="\tvec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n";
  1818. code+="\tvec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n";
  1819. code+="\tvec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n";
  1820. code+="\tfloat d = q.x - min(q.w, q.y);\n";
  1821. code+="\tfloat e = 1.0e-10;\n";
  1822. code+="\t"+OUTVAR(p_node->id,0)+"=vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n";
  1823. code+="}\n";
  1824. } else if (func==VEC_FUNC_HSV2RGB) {
  1825. code += OUTNAME(p_node->id,0)+";\n";
  1826. code+="{\n";
  1827. code+="\tvec3 c = "+p_inputs[0]+";\n";
  1828. code+="\tvec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n";
  1829. code+="\tvec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n";
  1830. code+="\t"+OUTVAR(p_node->id,0)+"=c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n";
  1831. code+="}\n";
  1832. } else {
  1833. code += OUTNAME(p_node->id,0)+"="+String(vec_func_id[func]).replace("$",p_inputs[0])+";\n";
  1834. }
  1835. }break;
  1836. case NODE_VEC_LEN: {
  1837. DEF_VEC(0);
  1838. code += OUTNAME(p_node->id,0)+"=length("+p_inputs[0]+");\n";
  1839. }break;
  1840. case NODE_DOT_PROD: {
  1841. DEF_VEC(0);
  1842. DEF_VEC(1);
  1843. code += OUTNAME(p_node->id,0)+"=dot("+p_inputs[1]+","+p_inputs[0]+");\n";
  1844. }break;
  1845. case NODE_VEC_TO_SCALAR: {
  1846. DEF_VEC(0);
  1847. code += OUTNAME(p_node->id,0)+"="+p_inputs[0]+".x;\n";
  1848. code += OUTNAME(p_node->id,1)+"="+p_inputs[0]+".y;\n";
  1849. code += OUTNAME(p_node->id,2)+"="+p_inputs[0]+".z;\n";
  1850. }break;
  1851. case NODE_SCALAR_TO_VEC: {
  1852. DEF_SCALAR(0);
  1853. DEF_SCALAR(1);
  1854. DEF_SCALAR(2);
  1855. code += OUTNAME(p_node->id,0)+"=vec3("+p_inputs[0]+","+p_inputs[1]+","+p_inputs[2]+""+");\n";
  1856. }break;
  1857. case NODE_VEC_TO_XFORM: {
  1858. DEF_VEC(0);
  1859. DEF_VEC(1);
  1860. DEF_VEC(2);
  1861. DEF_VEC(3);
  1862. code += OUTNAME(p_node->id, 0) + "=mat4(" +
  1863. "vec4(" + p_inputs[0] + ".x," + p_inputs[0] + ".y," + p_inputs[0] + ".z, 0.0),"
  1864. "vec4(" + p_inputs[1] + ".x," + p_inputs[1] + ".y," + p_inputs[1] + ".z, 0.0),"
  1865. "vec4(" + p_inputs[2] + ".x," + p_inputs[2] + ".y," + p_inputs[2] + ".z, 0.0),"
  1866. "vec4(" + p_inputs[3] + ".x," + p_inputs[3] + ".y," + p_inputs[3] + ".z, 1.0));\n";
  1867. }break;
  1868. case NODE_XFORM_TO_VEC: {
  1869. DEF_MATRIX(0);
  1870. code += OUTNAME(p_node->id, 0) + ";\n";
  1871. code += OUTNAME(p_node->id, 1) + ";\n";
  1872. code += OUTNAME(p_node->id, 2) + ";\n";
  1873. code += OUTNAME(p_node->id, 3) + ";\n";
  1874. code += "{\n";
  1875. code += "\tvec4 xform_row_01=" + p_inputs[0] + ".x;\n";
  1876. code += "\tvec4 xform_row_02=" + p_inputs[0] + ".y;\n";
  1877. code += "\tvec4 xform_row_03=" + p_inputs[0] + ".z;\n";
  1878. code += "\tvec4 xform_row_04=" + p_inputs[0] + ".w;\n";
  1879. code += "\t" + OUTVAR(p_node->id, 0) + "=vec3(xform_row_01.x, xform_row_01.y, xform_row_01.z);\n";
  1880. code += "\t" + OUTVAR(p_node->id, 1) + "=vec3(xform_row_02.x, xform_row_02.y, xform_row_02.z);\n";
  1881. code += "\t" + OUTVAR(p_node->id, 2) + "=vec3(xform_row_03.x, xform_row_03.y, xform_row_03.z);\n";
  1882. code += "\t" + OUTVAR(p_node->id, 3) + "=vec3(xform_row_04.x, xform_row_04.y, xform_row_04.z);\n";
  1883. code += "}\n";
  1884. }break;
  1885. case NODE_SCALAR_INTERP: {
  1886. DEF_SCALAR(0);
  1887. DEF_SCALAR(1);
  1888. DEF_SCALAR(2);
  1889. code += OUTNAME(p_node->id,0)+"=mix("+p_inputs[0]+","+p_inputs[1]+","+p_inputs[2]+");\n";
  1890. }break;
  1891. case NODE_VEC_INTERP: {
  1892. DEF_VEC(0);
  1893. DEF_VEC(1);
  1894. DEF_SCALAR(2);
  1895. code += OUTNAME(p_node->id,0)+"=mix("+p_inputs[0]+","+p_inputs[1]+","+p_inputs[2]+");\n";
  1896. }break;
  1897. case NODE_COLOR_RAMP: {
  1898. DEF_SCALAR(0);
  1899. static const int color_ramp_len=512;
  1900. PoolVector<uint8_t> cramp;
  1901. cramp.resize(color_ramp_len*4);
  1902. {
  1903. PoolVector<Color> colors=p_node->param1;
  1904. PoolVector<real_t> offsets=p_node->param2;
  1905. int cc =colors.size();
  1906. PoolVector<uint8_t>::Write crw = cramp.write();
  1907. PoolVector<Color>::Read cr = colors.read();
  1908. PoolVector<real_t>::Read ofr = offsets.read();
  1909. int at=0;
  1910. Color color_at(0,0,0,1);
  1911. for(int i=0;i<=cc;i++) {
  1912. int pos;
  1913. Color to;
  1914. if (i==cc) {
  1915. if (at==color_ramp_len)
  1916. break;
  1917. pos=color_ramp_len;
  1918. to=Color(1,1,1,1);
  1919. } else {
  1920. to=cr[i];
  1921. pos= MIN(ofr[i]*color_ramp_len,color_ramp_len);
  1922. }
  1923. for(int j=at;j<pos;j++) {
  1924. float t = (j-at)/float(pos-at);
  1925. Color c = color_at.linear_interpolate(to,t);
  1926. crw[j*4+0]=Math::fast_ftoi( CLAMP(c.r*255.0,0,255) );
  1927. crw[j*4+1]=Math::fast_ftoi( CLAMP(c.g*255.0,0,255) );
  1928. crw[j*4+2]=Math::fast_ftoi( CLAMP(c.b*255.0,0,255) );
  1929. crw[j*4+3]=Math::fast_ftoi( CLAMP(c.a*255.0,0,255) );
  1930. }
  1931. at=pos;
  1932. color_at=to;
  1933. }
  1934. }
  1935. Image gradient(color_ramp_len,1,0,Image::FORMAT_RGBA8,cramp);
  1936. Ref<ImageTexture> it = memnew( ImageTexture );
  1937. it->create_from_image(gradient,Texture::FLAG_FILTER|Texture::FLAG_MIPMAPS);
  1938. String crampname= "cramp_"+itos(p_node->id);
  1939. set_default_texture_param(crampname,it);
  1940. code +="uniform texture "+crampname+";\n";
  1941. code +="vec4 "+crampname+"_r=tex("+crampname+",vec2("+p_inputs[0]+",0));\n";
  1942. code += OUTNAME(p_node->id,0)+"="+crampname+"_r.rgb;\n";
  1943. code += OUTNAME(p_node->id,1)+"="+crampname+"_r.a;\n";
  1944. }break;
  1945. case NODE_CURVE_MAP: {
  1946. DEF_SCALAR(0);
  1947. static const int curve_map_len=256;
  1948. bool mapped[256];
  1949. zeromem(mapped,sizeof(mapped));
  1950. PoolVector<uint8_t> cmap;
  1951. cmap.resize(curve_map_len);
  1952. {
  1953. PoolVector<Point2> points=p_node->param1;
  1954. int pc =points.size();
  1955. PoolVector<uint8_t>::Write cmw = cmap.write();
  1956. PoolVector<Point2>::Read pr = points.read();
  1957. Vector2 prev=Vector2(0,0);
  1958. Vector2 prev2=Vector2(0,0);
  1959. for(int i=-1;i<pc;i++) {
  1960. Vector2 next;
  1961. Vector2 next2;
  1962. if (i+1>=pc) {
  1963. next=Vector2(1,1);
  1964. } else {
  1965. next=Vector2(pr[i+1].x,pr[i+1].y);
  1966. }
  1967. if (i+2>=pc) {
  1968. next2=Vector2(1,1);
  1969. } else {
  1970. next2=Vector2(pr[i+2].x,pr[i+2].y);
  1971. }
  1972. /*if (i==-1 && prev.offset==next.offset) {
  1973. prev=next;
  1974. continue;
  1975. }*/
  1976. _plot_curve(prev2,prev,next,next2,cmw.ptr(),mapped);
  1977. prev2=prev;
  1978. prev=next;
  1979. }
  1980. uint8_t pp=0;
  1981. for(int i=0;i<curve_map_len;i++) {
  1982. if (!mapped[i]) {
  1983. cmw[i]=pp;
  1984. } else {
  1985. pp=cmw[i];
  1986. }
  1987. }
  1988. }
  1989. Image gradient(curve_map_len,1,0,Image::FORMAT_L8,cmap);
  1990. Ref<ImageTexture> it = memnew( ImageTexture );
  1991. it->create_from_image(gradient,Texture::FLAG_FILTER|Texture::FLAG_MIPMAPS);
  1992. String cmapname= "cmap_"+itos(p_node->id);
  1993. set_default_texture_param(cmapname,it);
  1994. code +="uniform texture "+cmapname+";\n";
  1995. code += OUTNAME(p_node->id,0)+"=tex("+cmapname+",vec2("+p_inputs[0]+",0)).r;\n";
  1996. }break;
  1997. case NODE_SCALAR_INPUT: {
  1998. String name = p_node->param1;
  1999. float dv=p_node->param2;
  2000. code +="uniform float "+name+"="+rtos(dv)+";\n";
  2001. code += OUTNAME(p_node->id,0)+"="+name+";\n";
  2002. }break;
  2003. case NODE_VEC_INPUT: {
  2004. String name = p_node->param1;
  2005. Vector3 dv=p_node->param2;
  2006. code +="uniform vec3 "+name+"=vec3("+rtos(dv.x)+","+rtos(dv.y)+","+rtos(dv.z)+");\n";
  2007. code += OUTNAME(p_node->id,0)+"="+name+";\n";
  2008. }break;
  2009. case NODE_RGB_INPUT: {
  2010. String name = p_node->param1;
  2011. Color dv= p_node->param2;
  2012. code +="uniform color "+name+"=vec4("+rtos(dv.r)+","+rtos(dv.g)+","+rtos(dv.b)+","+rtos(dv.a)+");\n";
  2013. code += OUTNAME(p_node->id,0)+"="+name+".rgb;\n";
  2014. code += OUTNAME(p_node->id,1)+"="+name+".a;\n";
  2015. }break;
  2016. case NODE_XFORM_INPUT: {
  2017. String name = p_node->param1;
  2018. Transform dv= p_node->param2;
  2019. code +="uniform mat4 "+name+"=mat4(\n";
  2020. code+="\tvec4(vec3("+rtos(dv.basis.get_axis(0).x)+","+rtos(dv.basis.get_axis(0).y)+","+rtos(dv.basis.get_axis(0).z)+"),0),\n";
  2021. code+="\tvec4(vec3("+rtos(dv.basis.get_axis(1).x)+","+rtos(dv.basis.get_axis(1).y)+","+rtos(dv.basis.get_axis(1).z)+"),0),\n";
  2022. code+="\tvec4(vec3("+rtos(dv.basis.get_axis(2).x)+","+rtos(dv.basis.get_axis(2).y)+","+rtos(dv.basis.get_axis(2).z)+"),0),\n";
  2023. code+="\tvec4(vec3("+rtos(dv.origin.x)+","+rtos(dv.origin.y)+","+rtos(dv.origin.z)+"),1)\n";
  2024. code+=");";
  2025. code += OUTNAME(p_node->id,0)+"="+name+";\n";
  2026. }break;
  2027. case NODE_TEXTURE_INPUT: {
  2028. DEF_VEC(0);
  2029. String name = p_node->param1;
  2030. String rname="rt_read_tex"+itos(p_node->id);
  2031. code +="uniform texture "+name+";";
  2032. code +="vec4 "+rname+"=tex("+name+","+p_inputs[0]+".xy);\n";
  2033. code += OUTNAME(p_node->id,0)+"="+rname+".rgb;\n";
  2034. code += OUTNAME(p_node->id,1)+"="+rname+".a;\n";
  2035. }break;
  2036. case NODE_CUBEMAP_INPUT: {
  2037. DEF_VEC(0);
  2038. String name = p_node->param1;
  2039. code +="uniform cubemap "+name+";";
  2040. String rname="rt_read_tex"+itos(p_node->id);
  2041. code +="vec4 "+rname+"=texcube("+name+","+p_inputs[0]+".xy);\n";
  2042. code += OUTNAME(p_node->id,0)+"="+rname+".rgb;\n";
  2043. code += OUTNAME(p_node->id,1)+"="+rname+".a;\n";
  2044. }break;
  2045. case NODE_DEFAULT_TEXTURE: {
  2046. DEF_VEC(0);
  2047. if (get_mode()==MODE_CANVAS_ITEM && p_type==SHADER_TYPE_FRAGMENT) {
  2048. String rname="rt_default_tex"+itos(p_node->id);
  2049. code +="vec4 "+rname+"=tex(TEXTURE,"+p_inputs[0]+".xy);\n";
  2050. code += OUTNAME(p_node->id,0)+"="+rname+".rgb;\n";
  2051. code += OUTNAME(p_node->id,1)+"="+rname+".a;\n";
  2052. } else {
  2053. //not supported
  2054. code += OUTNAME(p_node->id,0)+"=vec3(0,0,0);\n";
  2055. code += OUTNAME(p_node->id,1)+"=1.0;\n";
  2056. }
  2057. } break;
  2058. case NODE_OUTPUT: {
  2059. }break;
  2060. case NODE_COMMENT: {
  2061. }break;
  2062. case NODE_TYPE_MAX: {
  2063. }
  2064. }
  2065. #undef DEF_SCALAR
  2066. #undef DEF_COLOR
  2067. #undef DEF_MATRIX
  2068. #undef DEF_VEC
  2069. }
  2070. #endif