shader_graph_editor_plugin.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935
  1. /*************************************************************************/
  2. /* shader_graph_editor_plugin.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. // FIXME: Godot 3.0 broke compatibility with ShaderGraphEditorPlugin,
  31. // it needs to be ported to the new shader language.
  32. #if 0
  33. #include "shader_graph_editor_plugin.h"
  34. #include "canvas_item_editor_plugin.h"
  35. #include "os/keyboard.h"
  36. #include "scene/gui/check_box.h"
  37. #include "scene/gui/menu_button.h"
  38. #include "scene/gui/panel.h"
  39. #include "spatial_editor_plugin.h"
  40. void GraphColorRampEdit::_gui_input(const InputEvent& p_event) {
  41. if (p_event.type==InputEvent::KEY && p_event->is_pressed() && p_event->get_scancode()==KEY_DELETE && grabbed!=-1) {
  42. points.remove(grabbed);
  43. grabbed=-1;
  44. update();
  45. emit_signal("ramp_changed");
  46. accept_event();
  47. }
  48. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && p_event->is_pressed()) {
  49. update();
  50. int x = p_event->get_position().x;
  51. int total_w = get_size().width-get_size().height-3;
  52. if (x>total_w+3) {
  53. if (grabbed==-1)
  54. return;
  55. Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10);
  56. picker->set_color(points[grabbed].color);
  57. popup->set_position(get_global_position()-Size2(0,ms.height));
  58. popup->set_size(ms);
  59. popup->popup();
  60. return;
  61. }
  62. float ofs = CLAMP(x/float(total_w),0,1);
  63. grabbed=-1;
  64. grabbing=true;
  65. int pos=-1;
  66. for(int i=0;i<points.size();i++) {
  67. if (ABS(x-points[i].offset*total_w)<4) {
  68. grabbed=i;
  69. }
  70. if (points[i].offset<ofs)
  71. pos=i;
  72. }
  73. grabbed_at=ofs;
  74. //grab or select
  75. if (grabbed!=-1) {
  76. return;
  77. }
  78. //insert
  79. Point p;
  80. p.offset=ofs;
  81. Point prev;
  82. Point next;
  83. if (pos==-1) {
  84. prev.color=Color(0,0,0);
  85. prev.offset=0;
  86. if (points.size()) {
  87. next=points[0];
  88. } else {
  89. next.color=Color(1,1,1);
  90. next.offset=1.0;
  91. }
  92. } else {
  93. if (pos==points.size()-1) {
  94. next.color=Color(1,1,1);
  95. next.offset=1.0;
  96. } else {
  97. next=points[pos+1];
  98. }
  99. prev=points[pos];
  100. }
  101. p.color=prev.color.linear_interpolate(next.color,(p.offset-prev.offset)/(next.offset-prev.offset));
  102. points.push_back(p);
  103. points.sort();
  104. for(int i=0;i<points.size();i++) {
  105. if (points[i].offset==ofs) {
  106. grabbed=i;
  107. break;
  108. }
  109. }
  110. emit_signal("ramp_changed");
  111. }
  112. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && !p_event->is_pressed()) {
  113. if (grabbing) {
  114. grabbing=false;
  115. emit_signal("ramp_changed");
  116. }
  117. update();
  118. }
  119. if (p_event.type==InputEvent::MOUSE_MOTION && grabbing) {
  120. int total_w = get_size().width-get_size().height-3;
  121. int x = p_event.mouse_motion.x;
  122. float newofs = CLAMP(x/float(total_w),0,1);
  123. bool valid=true;
  124. for(int i=0;i<points.size();i++) {
  125. if (points[i].offset==newofs && i!=grabbed) {
  126. valid=false;
  127. }
  128. }
  129. if (!valid)
  130. return;
  131. points[grabbed].offset=newofs;
  132. points.sort();
  133. for(int i=0;i<points.size();i++) {
  134. if (points[i].offset==newofs) {
  135. grabbed=i;
  136. break;
  137. }
  138. }
  139. emit_signal("ramp_changed");
  140. update();
  141. }
  142. }
  143. void GraphColorRampEdit::_notification(int p_what){
  144. if (p_what==NOTIFICATION_ENTER_TREE) {
  145. if (!picker->is_connected("color_changed",this,"_color_changed")) {
  146. picker->connect("color_changed",this,"_color_changed");
  147. }
  148. }
  149. if (p_what==NOTIFICATION_DRAW) {
  150. Point prev;
  151. prev.offset=0;
  152. prev.color=Color(0,0,0);
  153. int h = get_size().y;
  154. int total_w = get_size().width-get_size().height-3;
  155. for(int i=-1;i<points.size();i++) {
  156. Point next;
  157. if (i+1==points.size()) {
  158. next.color=Color(1,1,1);
  159. next.offset=1;
  160. } else {
  161. next=points[i+1];
  162. }
  163. if (prev.offset==next.offset) {
  164. prev=next;
  165. continue;
  166. }
  167. Vector<Vector2> points;
  168. Vector<Color> colors;
  169. points.push_back(Vector2(prev.offset*total_w,h));
  170. points.push_back(Vector2(prev.offset*total_w,0));
  171. points.push_back(Vector2(next.offset*total_w,0));
  172. points.push_back(Vector2(next.offset*total_w,h));
  173. colors.push_back(prev.color);
  174. colors.push_back(prev.color);
  175. colors.push_back(next.color);
  176. colors.push_back(next.color);
  177. draw_primitive(points,colors,Vector<Point2>());
  178. prev=next;
  179. }
  180. for(int i=0;i<points.size();i++) {
  181. Color col=i==grabbed?Color(1,0.0,0.0,0.9):Color(1,1,1,0.8);
  182. draw_line(Vector2(points[i].offset*total_w,0),Vector2(points[i].offset*total_w,h-1),Color(0,0,0,0.7));
  183. draw_line(Vector2(points[i].offset*total_w-1,h/2),Vector2(points[i].offset*total_w-1,h-1),col);
  184. draw_line(Vector2(points[i].offset*total_w+1,h/2),Vector2(points[i].offset*total_w+1,h-1),col);
  185. draw_line(Vector2(points[i].offset*total_w-1,h/2),Vector2(points[i].offset*total_w+1,h/2),col);
  186. draw_line(Vector2(points[i].offset*total_w-1,h-1),Vector2(points[i].offset*total_w+1,h-1),col);
  187. }
  188. if (grabbed!=-1) {
  189. draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color);
  190. }
  191. if (has_focus()) {
  192. draw_line(Vector2(-1,-1),Vector2(total_w+1,-1),Color(1,1,1,0.6));
  193. draw_line(Vector2(total_w+1,-1),Vector2(total_w+1,h+1),Color(1,1,1,0.6));
  194. draw_line(Vector2(total_w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6));
  195. draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6));
  196. }
  197. }
  198. }
  199. Size2 GraphColorRampEdit::get_minimum_size() const {
  200. return Vector2(0,16);
  201. }
  202. void GraphColorRampEdit::_color_changed(const Color& p_color) {
  203. if (grabbed==-1)
  204. return;
  205. points[grabbed].color=p_color;
  206. update();
  207. emit_signal("ramp_changed");
  208. }
  209. void GraphColorRampEdit::set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors) {
  210. ERR_FAIL_COND(p_offsets.size()!=p_colors.size());
  211. points.clear();
  212. for(int i=0;i<p_offsets.size();i++) {
  213. Point p;
  214. p.offset=p_offsets[i];
  215. p.color=p_colors[i];
  216. points.push_back(p);
  217. }
  218. points.sort();
  219. update();
  220. }
  221. Vector<float> GraphColorRampEdit::get_offsets() const{
  222. Vector<float> ret;
  223. for(int i=0;i<points.size();i++)
  224. ret.push_back(points[i].offset);
  225. return ret;
  226. }
  227. Vector<Color> GraphColorRampEdit::get_colors() const{
  228. Vector<Color> ret;
  229. for(int i=0;i<points.size();i++)
  230. ret.push_back(points[i].color);
  231. return ret;
  232. }
  233. void GraphColorRampEdit::_bind_methods(){
  234. ClassDB::bind_method(D_METHOD("_gui_input"),&GraphColorRampEdit::_gui_input);
  235. ClassDB::bind_method(D_METHOD("_color_changed"),&GraphColorRampEdit::_color_changed);
  236. ADD_SIGNAL(MethodInfo("ramp_changed"));
  237. }
  238. GraphColorRampEdit::GraphColorRampEdit(){
  239. grabbed=-1;
  240. grabbing=false;
  241. set_focus_mode(FOCUS_ALL);
  242. popup = memnew( PopupPanel );
  243. picker = memnew( ColorPicker );
  244. popup->add_child(picker);
  245. /popup->set_child_rect(picker);
  246. add_child(popup);
  247. }
  248. ////////////
  249. void GraphCurveMapEdit::_gui_input(const InputEvent& p_event) {
  250. if (p_event.type==InputEvent::KEY && p_event->is_pressed() && p_event->get_scancode()==KEY_DELETE && grabbed!=-1) {
  251. points.remove(grabbed);
  252. grabbed=-1;
  253. update();
  254. emit_signal("curve_changed");
  255. accept_event();
  256. }
  257. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && p_event->is_pressed()) {
  258. update();
  259. Point2 p = Vector2(p_event->get_position().x,p_event->get_position().y)/get_size();
  260. p.y=1.0-p.y;
  261. grabbed=-1;
  262. grabbing=true;
  263. for(int i=0;i<points.size();i++) {
  264. Vector2 ps = p*get_size();
  265. Vector2 pt = Vector2(points[i].offset,points[i].height)*get_size();
  266. if (ps.distance_to(pt)<4) {
  267. grabbed=i;
  268. }
  269. }
  270. //grab or select
  271. if (grabbed!=-1) {
  272. return;
  273. }
  274. //insert
  275. Point np;
  276. np.offset=p.x;
  277. np.height=p.y;
  278. points.push_back(np);
  279. points.sort();
  280. for(int i=0;i<points.size();i++) {
  281. if (points[i].offset==p.x && points[i].height==p.y) {
  282. grabbed=i;
  283. break;
  284. }
  285. }
  286. emit_signal("curve_changed");
  287. }
  288. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event->get_button_index()==1 && !p_event->is_pressed()) {
  289. if (grabbing) {
  290. grabbing=false;
  291. emit_signal("curve_changed");
  292. }
  293. update();
  294. }
  295. if (p_event.type==InputEvent::MOUSE_MOTION && grabbing && grabbed != -1) {
  296. Point2 p = Vector2(p_event->get_position().x,p_event->get_position().y)/get_size();
  297. p.y=1.0-p.y;
  298. p.x = CLAMP(p.x,0.0,1.0);
  299. p.y = CLAMP(p.y,0.0,1.0);
  300. bool valid=true;
  301. for(int i=0;i<points.size();i++) {
  302. if (points[i].offset==p.x && points[i].height==p.y && i!=grabbed) {
  303. valid=false;
  304. }
  305. }
  306. if (!valid)
  307. return;
  308. points[grabbed].offset=p.x;
  309. points[grabbed].height=p.y;
  310. points.sort();
  311. for(int i=0;i<points.size();i++) {
  312. if (points[i].offset==p.x && points[i].height==p.y) {
  313. grabbed=i;
  314. break;
  315. }
  316. }
  317. emit_signal("curve_changed");
  318. update();
  319. }
  320. }
  321. void GraphCurveMapEdit::_plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d) {
  322. float geometry[4][4];
  323. float tmp1[4][4];
  324. float tmp2[4][4];
  325. float deltas[4][4];
  326. double x, dx, dx2, dx3;
  327. double y, dy, dy2, dy3;
  328. double d, d2, d3;
  329. int lastx, lasty;
  330. int newx, newy;
  331. int ntimes;
  332. int i,j;
  333. int xmax=get_size().x;
  334. int ymax=get_size().y;
  335. /* construct the geometry matrix from the segment */
  336. for (i = 0; i < 4; i++) {
  337. geometry[i][2] = 0;
  338. geometry[i][3] = 0;
  339. }
  340. geometry[0][0] = (p_a[0] * xmax);
  341. geometry[1][0] = (p_b[0] * xmax);
  342. geometry[2][0] = (p_c[0] * xmax);
  343. geometry[3][0] = (p_d[0] * xmax);
  344. geometry[0][1] = (p_a[1] * ymax);
  345. geometry[1][1] = (p_b[1] * ymax);
  346. geometry[2][1] = (p_c[1] * ymax);
  347. geometry[3][1] = (p_d[1] * ymax);
  348. /* subdivide the curve ntimes (1000) times */
  349. ntimes = 4 * xmax;
  350. /* ntimes can be adjusted to give a finer or coarser curve */
  351. d = 1.0 / ntimes;
  352. d2 = d * d;
  353. d3 = d * d * d;
  354. /* construct a temporary matrix for determining the forward differencing deltas */
  355. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  356. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  357. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  358. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  359. /* compose the basis and geometry matrices */
  360. static const float CR_basis[4][4] = {
  361. { -0.5, 1.5, -1.5, 0.5 },
  362. { 1.0, -2.5, 2.0, -0.5 },
  363. { -0.5, 0.0, 0.5, 0.0 },
  364. { 0.0, 1.0, 0.0, 0.0 },
  365. };
  366. for (i = 0; i < 4; i++)
  367. {
  368. for (j = 0; j < 4; j++)
  369. {
  370. tmp1[i][j] = (CR_basis[i][0] * geometry[0][j] +
  371. CR_basis[i][1] * geometry[1][j] +
  372. CR_basis[i][2] * geometry[2][j] +
  373. CR_basis[i][3] * geometry[3][j]);
  374. }
  375. }
  376. /* compose the above results to get the deltas matrix */
  377. for (i = 0; i < 4; i++)
  378. {
  379. for (j = 0; j < 4; j++)
  380. {
  381. deltas[i][j] = (tmp2[i][0] * tmp1[0][j] +
  382. tmp2[i][1] * tmp1[1][j] +
  383. tmp2[i][2] * tmp1[2][j] +
  384. tmp2[i][3] * tmp1[3][j]);
  385. }
  386. }
  387. /* extract the x deltas */
  388. x = deltas[0][0];
  389. dx = deltas[1][0];
  390. dx2 = deltas[2][0];
  391. dx3 = deltas[3][0];
  392. /* extract the y deltas */
  393. y = deltas[0][1];
  394. dy = deltas[1][1];
  395. dy2 = deltas[2][1];
  396. dy3 = deltas[3][1];
  397. lastx = CLAMP (x, 0, xmax);
  398. lasty = CLAMP (y, 0, ymax);
  399. /* if (fix255)
  400. {
  401. cd->curve[cd->outline][lastx] = lasty;
  402. }
  403. else
  404. {
  405. cd->curve_ptr[cd->outline][lastx] = lasty;
  406. if(gb_debug) printf("bender_plot_curve xmax:%d ymax:%d\n", (int)xmax, (int)ymax);
  407. }
  408. */
  409. /* loop over the curve */
  410. for (i = 0; i < ntimes; i++)
  411. {
  412. /* increment the x values */
  413. x += dx;
  414. dx += dx2;
  415. dx2 += dx3;
  416. /* increment the y values */
  417. y += dy;
  418. dy += dy2;
  419. dy2 += dy3;
  420. newx = CLAMP ((Math::round (x)), 0, xmax);
  421. newy = CLAMP ((Math::round (y)), 0, ymax);
  422. /* if this point is different than the last one...then draw it */
  423. if ((lastx != newx) || (lasty != newy)) {
  424. draw_line(Vector2(lastx,ymax-lasty),Vector2(newx,ymax-newy),Color(0.8,0.8,0.8,0.8),2.0);
  425. }
  426. lastx = newx;
  427. lasty = newy;
  428. }
  429. }
  430. void GraphCurveMapEdit::_notification(int p_what){
  431. if (p_what==NOTIFICATION_DRAW) {
  432. draw_style_box(get_stylebox("bg","Tree"),Rect2(Point2(),get_size()));
  433. int w = get_size().x;
  434. int h = get_size().y;
  435. Vector2 prev=Vector2(0,0);
  436. Vector2 prev2=Vector2(0,0);
  437. for(int i=-1;i<points.size();i++) {
  438. Vector2 next;
  439. Vector2 next2;
  440. if (i+1>=points.size()) {
  441. next=Vector2(1,1);
  442. } else {
  443. next=Vector2(points[i+1].offset,points[i+1].height);
  444. }
  445. if (i+2>=points.size()) {
  446. next2=Vector2(1,1);
  447. } else {
  448. next2=Vector2(points[i+2].offset,points[i+2].height);
  449. }
  450. /*if (i==-1 && prev.offset==next.offset) {
  451. prev=next;
  452. continue;
  453. }*/
  454. _plot_curve(prev2,prev,next,next2);
  455. prev2=prev;
  456. prev=next;
  457. }
  458. for(int i=0;i<points.size();i++) {
  459. Color col=i==grabbed?Color(1,0.0,0.0,0.9):Color(1,1,1,0.8);
  460. draw_rect(Rect2( Vector2(points[i].offset,1.0-points[i].height)*get_size()-Vector2(2,2),Vector2(5,5)),col);
  461. }
  462. /* if (grabbed!=-1) {
  463. draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color);
  464. }
  465. */
  466. if (has_focus()) {
  467. draw_line(Vector2(-1,-1),Vector2(w+1,-1),Color(1,1,1,0.6));
  468. draw_line(Vector2(w+1,-1),Vector2(w+1,h+1),Color(1,1,1,0.6));
  469. draw_line(Vector2(w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6));
  470. draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6));
  471. }
  472. }
  473. }
  474. Size2 GraphCurveMapEdit::get_minimum_size() const {
  475. return Vector2(64,64);
  476. }
  477. void GraphCurveMapEdit::set_points(const Vector<Vector2>& p_points) {
  478. points.clear();
  479. for(int i=0;i<p_points.size();i++) {
  480. Point p;
  481. p.offset=p_points[i].x;
  482. p.height=p_points[i].y;
  483. points.push_back(p);
  484. }
  485. points.sort();
  486. update();
  487. }
  488. Vector<Vector2> GraphCurveMapEdit::get_points() const {
  489. Vector<Vector2> ret;
  490. for(int i=0;i<points.size();i++)
  491. ret.push_back(Vector2(points[i].offset,points[i].height));
  492. return ret;
  493. }
  494. void GraphCurveMapEdit::_bind_methods(){
  495. ClassDB::bind_method(D_METHOD("_gui_input"),&GraphCurveMapEdit::_gui_input);
  496. ADD_SIGNAL(MethodInfo("curve_changed"));
  497. }
  498. GraphCurveMapEdit::GraphCurveMapEdit(){
  499. grabbed=-1;
  500. grabbing=false;
  501. set_focus_mode(FOCUS_ALL);
  502. }
  503. ////cbacks
  504. ///
  505. void ShaderGraphView::_scalar_const_changed(double p_value,int p_id) {
  506. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  507. ur->create_action(TTR("Change Scalar Constant"),UndoRedo::MERGE_ENDS);
  508. ur->add_do_method(graph.ptr(),"scalar_const_node_set_value",type,p_id,p_value);
  509. ur->add_undo_method(graph.ptr(),"scalar_const_node_set_value",type,p_id,graph->scalar_const_node_get_value(type,p_id));
  510. ur->add_do_method(this,"_update_graph");
  511. ur->add_undo_method(this,"_update_graph");
  512. block_update=true;
  513. ur->commit_action();
  514. block_update=false;
  515. }
  516. void ShaderGraphView::_vec_const_changed(double p_value, int p_id,Array p_arr){
  517. Vector3 val;
  518. for(int i=0;i<p_arr.size();i++) {
  519. val[i]=p_arr[i].call("get_val");
  520. }
  521. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  522. ur->create_action(TTR("Change Vec Constant"),UndoRedo::MERGE_ENDS);
  523. ur->add_do_method(graph.ptr(),"vec_const_node_set_value",type,p_id,val);
  524. ur->add_undo_method(graph.ptr(),"vec_const_node_set_value",type,p_id,graph->vec_const_node_get_value(type,p_id));
  525. ur->add_do_method(this,"_update_graph");
  526. ur->add_undo_method(this,"_update_graph");
  527. block_update=true;
  528. ur->commit_action();
  529. block_update=false;
  530. }
  531. void ShaderGraphView::_rgb_const_changed(const Color& p_color, int p_id){
  532. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  533. ur->create_action(TTR("Change RGB Constant"),UndoRedo::MERGE_ENDS);
  534. ur->add_do_method(graph.ptr(),"rgb_const_node_set_value",type,p_id,p_color);
  535. ur->add_undo_method(graph.ptr(),"rgb_const_node_set_value",type,p_id,graph->rgb_const_node_get_value(type,p_id));
  536. ur->add_do_method(this,"_update_graph");
  537. ur->add_undo_method(this,"_update_graph");
  538. block_update=true;
  539. ur->commit_action();
  540. block_update=false;
  541. }
  542. void ShaderGraphView::_scalar_op_changed(int p_op, int p_id){
  543. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  544. ur->create_action(TTR("Change Scalar Operator"));
  545. ur->add_do_method(graph.ptr(),"scalar_op_node_set_op",type,p_id,p_op);
  546. ur->add_undo_method(graph.ptr(),"scalar_op_node_set_op",type,p_id,graph->scalar_op_node_get_op(type,p_id));
  547. ur->add_do_method(this,"_update_graph");
  548. ur->add_undo_method(this,"_update_graph");
  549. block_update=true;
  550. ur->commit_action();
  551. block_update=false;
  552. }
  553. void ShaderGraphView::_vec_op_changed(int p_op, int p_id){
  554. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  555. ur->create_action(TTR("Change Vec Operator"));
  556. ur->add_do_method(graph.ptr(),"vec_op_node_set_op",type,p_id,p_op);
  557. ur->add_undo_method(graph.ptr(),"vec_op_node_set_op",type,p_id,graph->vec_op_node_get_op(type,p_id));
  558. ur->add_do_method(this,"_update_graph");
  559. ur->add_undo_method(this,"_update_graph");
  560. block_update=true;
  561. ur->commit_action();
  562. block_update=false;
  563. }
  564. void ShaderGraphView::_vec_scalar_op_changed(int p_op, int p_id){
  565. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  566. ur->create_action(TTR("Change Vec Scalar Operator"));
  567. ur->add_do_method(graph.ptr(),"vec_scalar_op_node_set_op",type,p_id,p_op);
  568. ur->add_undo_method(graph.ptr(),"vec_scalar_op_node_set_op",type,p_id,graph->vec_scalar_op_node_get_op(type,p_id));
  569. ur->add_do_method(this,"_update_graph");
  570. ur->add_undo_method(this,"_update_graph");
  571. block_update=true;
  572. ur->commit_action();
  573. block_update=false;
  574. }
  575. void ShaderGraphView::_rgb_op_changed(int p_op, int p_id){
  576. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  577. ur->create_action(TTR("Change RGB Operator"));
  578. ur->add_do_method(graph.ptr(),"rgb_op_node_set_op",type,p_id,p_op);
  579. ur->add_undo_method(graph.ptr(),"rgb_op_node_set_op",type,p_id,graph->rgb_op_node_get_op(type,p_id));
  580. ur->add_do_method(this,"_update_graph");
  581. ur->add_undo_method(this,"_update_graph");
  582. block_update=true;
  583. ur->commit_action();
  584. block_update=false;
  585. }
  586. void ShaderGraphView::_xform_inv_rev_changed(bool p_enabled, int p_id){
  587. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  588. ur->create_action(TTR("Toggle Rot Only"));
  589. ur->add_do_method(graph.ptr(),"xform_vec_mult_node_set_no_translation",type,p_id,p_enabled);
  590. ur->add_undo_method(graph.ptr(),"xform_vec_mult_node_set_no_translation",type,p_id,graph->xform_vec_mult_node_get_no_translation(type,p_id));
  591. ur->add_do_method(this,"_update_graph");
  592. ur->add_undo_method(this,"_update_graph");
  593. block_update=true;
  594. ur->commit_action();
  595. block_update=false;
  596. }
  597. void ShaderGraphView::_scalar_func_changed(int p_func, int p_id){
  598. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  599. ur->create_action(TTR("Change Scalar Function"));
  600. ur->add_do_method(graph.ptr(),"scalar_func_node_set_function",type,p_id,p_func);
  601. ur->add_undo_method(graph.ptr(),"scalar_func_node_set_function",type,p_id,graph->scalar_func_node_get_function(type,p_id));
  602. ur->add_do_method(this,"_update_graph");
  603. ur->add_undo_method(this,"_update_graph");
  604. block_update=true;
  605. ur->commit_action();
  606. block_update=false;
  607. }
  608. void ShaderGraphView::_vec_func_changed(int p_func, int p_id){
  609. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  610. ur->create_action(TTR("Change Vec Function"));
  611. ur->add_do_method(graph.ptr(),"vec_func_node_set_function",type,p_id,p_func);
  612. ur->add_undo_method(graph.ptr(),"vec_func_node_set_function",type,p_id,graph->vec_func_node_get_function(type,p_id));
  613. ur->add_do_method(this,"_update_graph");
  614. ur->add_undo_method(this,"_update_graph");
  615. block_update=true;
  616. ur->commit_action();
  617. block_update=false;
  618. }
  619. void ShaderGraphView::_scalar_input_changed(double p_value,int p_id){
  620. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  621. ur->create_action(TTR("Change Scalar Uniform"),UndoRedo::MERGE_ENDS);
  622. ur->add_do_method(graph.ptr(),"scalar_input_node_set_value",type,p_id,p_value);
  623. ur->add_undo_method(graph.ptr(),"scalar_input_node_set_value",type,p_id,graph->scalar_input_node_get_value(type,p_id));
  624. ur->add_do_method(this,"_update_graph");
  625. ur->add_undo_method(this,"_update_graph");
  626. block_update=true;
  627. ur->commit_action();
  628. block_update=false;
  629. }
  630. void ShaderGraphView::_vec_input_changed(double p_value, int p_id,Array p_arr){
  631. Vector3 val;
  632. for(int i=0;i<p_arr.size();i++) {
  633. val[i]=p_arr[i].call("get_val");
  634. }
  635. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  636. ur->create_action(TTR("Change Vec Uniform"),UndoRedo::MERGE_ENDS);
  637. ur->add_do_method(graph.ptr(),"vec_input_node_set_value",type,p_id,val);
  638. ur->add_undo_method(graph.ptr(),"vec_input_node_set_value",type,p_id,graph->vec_input_node_get_value(type,p_id));
  639. ur->add_do_method(this,"_update_graph");
  640. ur->add_undo_method(this,"_update_graph");
  641. block_update=true;
  642. ur->commit_action();
  643. block_update=false;
  644. }
  645. void ShaderGraphView::_xform_input_changed(int p_id, Node *p_button){
  646. ToolButton *tb = Object::cast_to<ToolButton>(p_button);
  647. ped_popup->set_position(tb->get_global_position()+Vector2(0,tb->get_size().height));
  648. ped_popup->set_size(tb->get_size());
  649. edited_id=p_id;
  650. edited_def=-1;
  651. ped_popup->edit(NULL,"",Variant::TRANSFORM,graph->xform_input_node_get_value(type,p_id),PROPERTY_HINT_NONE,"");
  652. ped_popup->popup();
  653. }
  654. void ShaderGraphView::_xform_const_changed(int p_id, Node *p_button){
  655. ToolButton *tb = Object::cast_to<ToolButton>(p_button);
  656. ped_popup->set_position(tb->get_global_position()+Vector2(0,tb->get_size().height));
  657. ped_popup->set_size(tb->get_size());
  658. edited_id=p_id;
  659. edited_def=-1;
  660. ped_popup->edit(NULL,"",Variant::TRANSFORM,graph->xform_const_node_get_value(type,p_id),PROPERTY_HINT_NONE,"");
  661. ped_popup->popup();
  662. }
  663. void ShaderGraphView::_rgb_input_changed(const Color& p_color, int p_id){
  664. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  665. ur->create_action(TTR("Change RGB Uniform"),UndoRedo::MERGE_ENDS);
  666. ur->add_do_method(graph.ptr(),"rgb_input_node_set_value",type,p_id,p_color);
  667. ur->add_undo_method(graph.ptr(),"rgb_input_node_set_value",type,p_id,graph->rgb_input_node_get_value(type,p_id));
  668. ur->add_do_method(this,"_update_graph");
  669. ur->add_undo_method(this,"_update_graph");
  670. block_update=true;
  671. ur->commit_action();
  672. block_update=false;
  673. }
  674. void ShaderGraphView::_tex_input_change(int p_id, Node *p_button){
  675. }
  676. void ShaderGraphView::_cube_input_change(int p_id){
  677. }
  678. void ShaderGraphView::_variant_edited() {
  679. if (edited_def != -1) {
  680. Variant v = ped_popup->get_variant();
  681. Variant v2 = graph->default_get_value(type,edited_id,edited_def);
  682. if (v2.get_type() == Variant::NIL)
  683. switch (v.get_type()) {
  684. case Variant::VECTOR3:
  685. v2=Vector3();
  686. break;
  687. case Variant::REAL:
  688. v2=0.0;
  689. break;
  690. case Variant::TRANSFORM:
  691. v2=Transform();
  692. break;
  693. case Variant::COLOR:
  694. v2=Color();
  695. break;
  696. default: {}
  697. }
  698. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  699. ur->create_action(TTR("Change Default Value"));
  700. ur->add_do_method(graph.ptr(),"default_set_value",type,edited_id,edited_def, v);
  701. ur->add_undo_method(graph.ptr(),"default_set_value",type,edited_id,edited_def, v2);
  702. ur->add_do_method(this,"_update_graph");
  703. ur->add_undo_method(this,"_update_graph");
  704. ur->commit_action();
  705. return;
  706. }
  707. if (graph->node_get_type(type,edited_id)==ShaderGraph::NODE_XFORM_CONST) {
  708. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  709. ur->create_action(TTR("Change XForm Uniform"));
  710. ur->add_do_method(graph.ptr(),"xform_const_node_set_value",type,edited_id,ped_popup->get_variant());
  711. ur->add_undo_method(graph.ptr(),"xform_const_node_set_value",type,edited_id,graph->xform_const_node_get_value(type,edited_id));
  712. ur->add_do_method(this,"_update_graph");
  713. ur->add_undo_method(this,"_update_graph");
  714. ur->commit_action();
  715. }
  716. if (graph->node_get_type(type,edited_id)==ShaderGraph::NODE_XFORM_INPUT) {
  717. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  718. ur->create_action(TTR("Change XForm Uniform"));
  719. ur->add_do_method(graph.ptr(),"xform_input_node_set_value",type,edited_id,ped_popup->get_variant());
  720. ur->add_undo_method(graph.ptr(),"xform_input_node_set_value",type,edited_id,graph->xform_input_node_get_value(type,edited_id));
  721. ur->add_do_method(this,"_update_graph");
  722. ur->add_undo_method(this,"_update_graph");
  723. ur->commit_action();
  724. }
  725. if (graph->node_get_type(type,edited_id)==ShaderGraph::NODE_TEXTURE_INPUT) {
  726. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  727. ur->create_action(TTR("Change Texture Uniform"));
  728. ur->add_do_method(graph.ptr(),"texture_input_node_set_value",type,edited_id,ped_popup->get_variant());
  729. ur->add_undo_method(graph.ptr(),"texture_input_node_set_value",type,edited_id,graph->texture_input_node_get_value(type,edited_id));
  730. ur->add_do_method(this,"_update_graph");
  731. ur->add_undo_method(this,"_update_graph");
  732. ur->commit_action();
  733. }
  734. if (graph->node_get_type(type,edited_id)==ShaderGraph::NODE_CUBEMAP_INPUT) {
  735. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  736. ur->create_action(TTR("Change Cubemap Uniform"));
  737. ur->add_do_method(graph.ptr(),"cubemap_input_node_set_value",type,edited_id,ped_popup->get_variant());
  738. ur->add_undo_method(graph.ptr(),"cubemap_input_node_set_value",type,edited_id,graph->cubemap_input_node_get_value(type,edited_id));
  739. ur->add_do_method(this,"_update_graph");
  740. ur->add_undo_method(this,"_update_graph");
  741. ur->commit_action();
  742. }
  743. }
  744. void ShaderGraphView::_comment_edited(int p_id,Node* p_button) {
  745. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  746. TextEdit *te=Object::cast_to<TextEdit>(p_button);
  747. ur->create_action(TTR("Change Comment"),UndoRedo::MERGE_ENDS);
  748. ur->add_do_method(graph.ptr(),"comment_node_set_text",type,p_id,te->get_text());
  749. ur->add_undo_method(graph.ptr(),"comment_node_set_text",type,p_id,graph->comment_node_get_text(type,p_id));
  750. ur->add_do_method(this,"_update_graph");
  751. ur->add_undo_method(this,"_update_graph");
  752. block_update=true;
  753. ur->commit_action();
  754. block_update=false;
  755. }
  756. void ShaderGraphView::_color_ramp_changed(int p_id,Node* p_ramp) {
  757. GraphColorRampEdit *cr=Object::cast_to<GraphColorRampEdit>(p_ramp);
  758. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  759. Vector<float> offsets=cr->get_offsets();
  760. Vector<Color> colors=cr->get_colors();
  761. PoolVector<float> new_offsets;
  762. PoolVector<Color> new_colors;
  763. {
  764. new_offsets.resize(offsets.size());
  765. new_colors.resize(colors.size());
  766. PoolVector<float>::Write ow=new_offsets.write();
  767. PoolVector<Color>::Write cw=new_colors.write();
  768. for(int i=0;i<new_offsets.size();i++) {
  769. ow[i]=offsets[i];
  770. cw[i]=colors[i];
  771. }
  772. }
  773. PoolVector<float> old_offsets=graph->color_ramp_node_get_offsets(type,p_id);
  774. PoolVector<Color> old_colors=graph->color_ramp_node_get_colors(type,p_id);
  775. if (old_offsets.size()!=new_offsets.size())
  776. ur->create_action(TTR("Add/Remove to Color Ramp"));
  777. else
  778. ur->create_action(TTR("Modify Color Ramp"),UndoRedo::MERGE_ENDS);
  779. ur->add_do_method(graph.ptr(),"color_ramp_node_set_ramp",type,p_id,new_colors,new_offsets);
  780. ur->add_undo_method(graph.ptr(),"color_ramp_node_set_ramp",type,p_id,old_colors,old_offsets);
  781. ur->add_do_method(this,"_update_graph");
  782. ur->add_undo_method(this,"_update_graph");
  783. block_update=true;
  784. ur->commit_action();
  785. block_update=false;
  786. }
  787. void ShaderGraphView::_curve_changed(int p_id,Node* p_curve) {
  788. GraphCurveMapEdit *cr=Object::cast_to<GraphCurveMapEdit>(p_curve);
  789. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  790. Vector<Point2> points=cr->get_points();
  791. PoolVector<Vector2> new_points;
  792. {
  793. new_points.resize(points.size());
  794. PoolVector<Vector2>::Write ow=new_points.write();
  795. for(int i=0;i<new_points.size();i++) {
  796. ow[i]=points[i];
  797. }
  798. }
  799. PoolVector<Vector2> old_points=graph->curve_map_node_get_points(type,p_id);
  800. if (old_points.size()!=new_points.size())
  801. ur->create_action(TTR("Add/Remove to Curve Map"));
  802. else
  803. ur->create_action(TTR("Modify Curve Map"),UndoRedo::MERGE_ENDS);
  804. ur->add_do_method(graph.ptr(),"curve_map_node_set_points",type,p_id,new_points);
  805. ur->add_undo_method(graph.ptr(),"curve_map_node_set_points",type,p_id,old_points);
  806. ur->add_do_method(this,"_update_graph");
  807. ur->add_undo_method(this,"_update_graph");
  808. block_update=true;
  809. ur->commit_action();
  810. block_update=false;
  811. }
  812. void ShaderGraphView::_input_name_changed(const String& p_name, int p_id, Node *p_line_edit) {
  813. LineEdit *le=Object::cast_to<LineEdit>(p_line_edit);
  814. ERR_FAIL_COND(!le);
  815. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  816. ur->create_action(TTR("Change Input Name"));
  817. ur->add_do_method(graph.ptr(),"input_node_set_name",type,p_id,p_name);
  818. ur->add_undo_method(graph.ptr(),"input_node_set_name",type,p_id,graph->input_node_get_name(type,p_id));
  819. ur->add_do_method(this,"_update_graph");
  820. ur->add_undo_method(this,"_update_graph");
  821. block_update=true;
  822. ur->commit_action();
  823. block_update=false;
  824. le->set_text(graph->input_node_get_name(type,p_id));
  825. }
  826. void ShaderGraphView::_tex_edited(int p_id,Node* p_button) {
  827. ToolButton *tb = Object::cast_to<ToolButton>(p_button);
  828. ped_popup->set_position(tb->get_global_position()+Vector2(0,tb->get_size().height));
  829. ped_popup->set_size(tb->get_size());
  830. edited_id=p_id;
  831. edited_def=-1;
  832. ped_popup->edit(NULL,"",Variant::OBJECT,graph->texture_input_node_get_value(type,p_id),PROPERTY_HINT_RESOURCE_TYPE,"Texture");
  833. }
  834. void ShaderGraphView::_cube_edited(int p_id,Node* p_button) {
  835. ToolButton *tb = Object::cast_to<ToolButton>(p_button);
  836. ped_popup->set_position(tb->get_global_position()+Vector2(0,tb->get_size().height));
  837. ped_popup->set_size(tb->get_size());
  838. edited_id=p_id;
  839. edited_def=-1;
  840. ped_popup->edit(NULL,"",Variant::OBJECT,graph->cubemap_input_node_get_value(type,p_id),PROPERTY_HINT_RESOURCE_TYPE,"CubeMap");
  841. }
  842. //////////////view/////////////
  843. void ShaderGraphView::_connection_request(const String& p_from, int p_from_slot,const String& p_to,int p_to_slot) {
  844. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  845. int from_idx=-1;
  846. int to_idx=-1;
  847. for (Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  848. if (p_from==E->get()->get_name())
  849. from_idx=E->key();
  850. if (p_to==E->get()->get_name())
  851. to_idx=E->key();
  852. }
  853. ERR_FAIL_COND(from_idx==-1);
  854. ERR_FAIL_COND(to_idx==-1);
  855. ur->create_action(TTR("Connect Graph Nodes"));
  856. List<ShaderGraph::Connection> conns;
  857. graph->get_node_connections(type,&conns);
  858. //disconnect/reconnect dependencies
  859. ur->add_undo_method(graph.ptr(),"disconnect_node",type,from_idx,p_from_slot,to_idx,p_to_slot);
  860. for(List<ShaderGraph::Connection>::Element *E=conns.front();E;E=E->next()) {
  861. if (E->get().dst_id==to_idx && E->get().dst_slot==p_to_slot) {
  862. ur->add_do_method(graph.ptr(),"disconnect_node",type,E->get().src_id,E->get().src_slot,E->get().dst_id,E->get().dst_slot);
  863. ur->add_undo_method(graph.ptr(),"connect_node",type,E->get().src_id,E->get().src_slot,E->get().dst_id,E->get().dst_slot);
  864. }
  865. }
  866. ur->add_do_method(graph.ptr(),"connect_node",type,from_idx,p_from_slot,to_idx,p_to_slot);
  867. ur->add_do_method(this,"_update_graph");
  868. ur->add_undo_method(this,"_update_graph");
  869. ur->commit_action();
  870. }
  871. void ShaderGraphView::_disconnection_request(const String& p_from, int p_from_slot,const String& p_to,int p_to_slot) {
  872. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  873. int from_idx=-1;
  874. int to_idx=-1;
  875. for (Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  876. if (p_from==E->get()->get_name())
  877. from_idx=E->key();
  878. if (p_to==E->get()->get_name())
  879. to_idx=E->key();
  880. }
  881. ERR_FAIL_COND(from_idx==-1);
  882. ERR_FAIL_COND(to_idx==-1);
  883. if (!graph->is_node_connected(type,from_idx,p_from_slot,to_idx,p_to_slot))
  884. return; //nothing to disconnect
  885. ur->create_action(TTR("Disconnect Graph Nodes"));
  886. List<ShaderGraph::Connection> conns;
  887. graph->get_node_connections(type,&conns);
  888. //disconnect/reconnect dependencies
  889. ur->add_do_method(graph.ptr(),"disconnect_node",type,from_idx,p_from_slot,to_idx,p_to_slot);
  890. ur->add_undo_method(graph.ptr(),"connect_node",type,from_idx,p_from_slot,to_idx,p_to_slot);
  891. ur->add_do_method(this,"_update_graph");
  892. ur->add_undo_method(this,"_update_graph");
  893. ur->commit_action();
  894. }
  895. void ShaderGraphView::_node_removed(int p_id) {
  896. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  897. ur->create_action(TTR("Remove Shader Graph Node"));
  898. ur->add_do_method(graph.ptr(),"node_remove",type,p_id);
  899. ur->add_undo_method(graph.ptr(),"node_add",type,graph->node_get_type(type,p_id),p_id);
  900. ur->add_undo_method(graph.ptr(),"node_set_state",type,p_id,graph->node_get_state(type,p_id));
  901. List<ShaderGraph::Connection> conns;
  902. graph->get_node_connections(type,&conns);
  903. for(List<ShaderGraph::Connection>::Element *E=conns.front();E;E=E->next()) {
  904. if (E->get().dst_id==p_id || E->get().src_id==p_id) {
  905. ur->add_undo_method(graph.ptr(),"connect_node",type,E->get().src_id,E->get().src_slot,E->get().dst_id,E->get().dst_slot);
  906. }
  907. }
  908. ur->add_do_method(this,"_update_graph");
  909. ur->add_undo_method(this,"_update_graph");
  910. ur->commit_action();
  911. }
  912. void ShaderGraphView::_begin_node_move()
  913. {
  914. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  915. ur->create_action(TTR("Move Shader Graph Node"));
  916. }
  917. void ShaderGraphView::_node_moved(const Vector2& p_from, const Vector2& p_to,int p_id) {
  918. ERR_FAIL_COND(!node_map.has(p_id));
  919. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  920. ur->add_do_method(this,"_move_node",p_id,p_to);
  921. ur->add_undo_method(this,"_move_node",p_id,p_from);
  922. }
  923. void ShaderGraphView::_end_node_move()
  924. {
  925. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  926. ur->commit_action();
  927. }
  928. void ShaderGraphView::_move_node(int p_id,const Vector2& p_to) {
  929. ERR_FAIL_COND(!node_map.has(p_id));
  930. node_map[p_id]->set_offset(p_to);
  931. graph->node_set_position(type,p_id,p_to);
  932. }
  933. void ShaderGraphView::_duplicate_nodes_request()
  934. {
  935. Array s_id;
  936. for(Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  937. ShaderGraph::NodeType t=graph->node_get_type(type, E->key());
  938. if (t==ShaderGraph::NODE_OUTPUT || t==ShaderGraph::NODE_INPUT)
  939. continue;
  940. GraphNode *gn = E->get();
  941. if (gn && gn->is_selected())
  942. s_id.push_back(E->key());
  943. }
  944. if (s_id.size()==0)
  945. return;
  946. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  947. ur->create_action(TTR("Duplicate Graph Node(s)"));
  948. ur->add_do_method(this,"_duplicate_nodes",s_id);
  949. List<int> n_ids = graph->generate_ids(type, s_id.size());
  950. for (List<int>::Element *E=n_ids.front();E;E=E->next())
  951. ur->add_undo_method(graph.ptr(),"node_remove",type,E->get());
  952. ur->add_do_method(this,"_update_graph");
  953. ur->add_undo_method(this,"_update_graph");
  954. ur->commit_action();
  955. }
  956. void ShaderGraphView::_duplicate_nodes(const Array &p_nodes)
  957. {
  958. List<int> n = List<int>();
  959. for (int i=0; i<p_nodes.size();i++)
  960. n.push_back(p_nodes.get(i));
  961. graph->duplicate_nodes(type, n);
  962. call_deferred("_update_graph");
  963. }
  964. void ShaderGraphView::_delete_nodes_request()
  965. {
  966. List<int> s_id=List<int>();
  967. for(Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  968. ShaderGraph::NodeType t=graph->node_get_type(type, E->key());
  969. if (t==ShaderGraph::NODE_OUTPUT)
  970. continue;
  971. GraphNode *gn = E->get();
  972. if (gn && gn->is_selected())
  973. s_id.push_back(E->key());
  974. }
  975. if (s_id.size()==0)
  976. return;
  977. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  978. ur->create_action(TTR("Delete Shader Graph Node(s)"));
  979. for (List<int>::Element *N=s_id.front();N;N=N->next()) {
  980. ur->add_do_method(graph.ptr(),"node_remove",type,N->get());
  981. ur->add_undo_method(graph.ptr(),"node_add",type,graph->node_get_type(type,N->get()),N->get());
  982. ur->add_undo_method(graph.ptr(),"node_set_state",type,N->get(),graph->node_get_state(type,N->get()));
  983. List<ShaderGraph::Connection> conns;
  984. graph->get_node_connections(type,&conns);
  985. for(List<ShaderGraph::Connection>::Element *E=conns.front();E;E=E->next()) {
  986. if (E->get().dst_id==N->get() || E->get().src_id==N->get()) {
  987. ur->add_undo_method(graph.ptr(),"connect_node",type,E->get().src_id,E->get().src_slot,E->get().dst_id,E->get().dst_slot);
  988. }
  989. }
  990. }
  991. ur->add_do_method(this,"_update_graph");
  992. ur->add_undo_method(this,"_update_graph");
  993. ur->commit_action();
  994. }
  995. void ShaderGraphView::_default_changed(int p_id, Node *p_button, int p_param, int v_type, String p_hint)
  996. {
  997. ToolButton *tb = Object::cast_to<ToolButton>(p_button);
  998. ped_popup->set_position(tb->get_global_position()+Vector2(0,tb->get_size().height));
  999. ped_popup->set_size(tb->get_size());
  1000. edited_id=p_id;
  1001. edited_def=p_param;
  1002. Variant::Type vt = (Variant::Type)v_type;
  1003. Variant v = graph->default_get_value(type,p_id,edited_def);
  1004. int h=PROPERTY_HINT_NONE;
  1005. if (v.get_type() == Variant::NIL)
  1006. switch (vt) {
  1007. case Variant::VECTOR3:
  1008. v=Vector3();
  1009. break;
  1010. case Variant::REAL:
  1011. h=PROPERTY_HINT_RANGE;
  1012. v=0.0;
  1013. break;
  1014. case Variant::TRANSFORM:
  1015. v=Transform();
  1016. break;
  1017. case Variant::COLOR:
  1018. h=PROPERTY_HINT_COLOR_NO_ALPHA;
  1019. v=Color();
  1020. break;
  1021. default: {}
  1022. }
  1023. ped_popup->edit(NULL,"",vt,v,h,p_hint);
  1024. ped_popup->popup();
  1025. }
  1026. ToolButton *ShaderGraphView::make_label(String text, Variant::Type v_type) {
  1027. ToolButton *l = memnew( ToolButton );
  1028. l->set_text(text);
  1029. l->set_text_align(ToolButton::ALIGN_LEFT);
  1030. l->add_style_override("hover", l->get_stylebox("normal", "ToolButton"));
  1031. l->add_style_override("pressed", l->get_stylebox("normal", "ToolButton"));
  1032. l->add_style_override("focus", l->get_stylebox("normal", "ToolButton"));
  1033. switch (v_type) {
  1034. case Variant::REAL:
  1035. l->set_icon(ped_popup->get_icon("Real", "EditorIcons"));
  1036. break;
  1037. case Variant::VECTOR3:
  1038. l->set_icon(ped_popup->get_icon("Vector", "EditorIcons"));
  1039. break;
  1040. case Variant::TRANSFORM:
  1041. l->set_icon(ped_popup->get_icon("Matrix", "EditorIcons"));
  1042. break;
  1043. case Variant::COLOR:
  1044. l->set_icon(ped_popup->get_icon("Color", "EditorIcons"));
  1045. break;
  1046. default: {}
  1047. }
  1048. return l;
  1049. }
  1050. ToolButton *ShaderGraphView::make_editor(String text,GraphNode* gn,int p_id,int param,Variant::Type v_type, String p_hint) {
  1051. ToolButton *edit = memnew( ToolButton );
  1052. edit->set_text(text);
  1053. edit->set_text_align(ToolButton::ALIGN_LEFT);
  1054. edit->set_flat(false);
  1055. edit->add_style_override("normal", gn->get_stylebox("defaultframe", "GraphNode"));
  1056. edit->add_style_override("hover", gn->get_stylebox("defaultframe", "GraphNode"));
  1057. edit->add_style_override("pressed", gn->get_stylebox("defaultframe", "GraphNode"));
  1058. edit->add_style_override("focus", gn->get_stylebox("defaultfocus", "GraphNode"));
  1059. edit->connect("pressed",this,"_default_changed",varray(p_id,edit,param,v_type,p_hint));
  1060. switch (v_type) {
  1061. case Variant::REAL:
  1062. edit->set_icon(ped_popup->get_icon("Real", "EditorIcons"));
  1063. break;
  1064. case Variant::VECTOR3:
  1065. edit->set_icon(ped_popup->get_icon("Vector", "EditorIcons"));
  1066. break;
  1067. case Variant::TRANSFORM:
  1068. edit->set_icon(ped_popup->get_icon("Matrix", "EditorIcons"));
  1069. break;
  1070. case Variant::COLOR: {
  1071. Image icon_color = Image(15,15,false,Image::FORMAT_RGB8);
  1072. Color c = graph->default_get_value(type,p_id,param);
  1073. for (int x=1;x<14;x++)
  1074. for (int y=1;y<14;y++)
  1075. icon_color.set_pixel(x,y,c);
  1076. Ref<ImageTexture> t;
  1077. t.instance();
  1078. t->create_from_image(icon_color);
  1079. edit->set_icon(t);
  1080. } break;
  1081. default: {}
  1082. }
  1083. return edit;
  1084. }
  1085. void ShaderGraphView::_create_node(int p_id) {
  1086. GraphNode *gn = memnew( GraphNode );
  1087. gn->set_show_close_button(true);
  1088. Color typecol[4]={
  1089. Color(0.9,0.4,1),
  1090. Color(0.8,1,0.2),
  1091. Color(1,0.2,0.2),
  1092. Color(0,1,1)
  1093. };
  1094. const String hint_spin = "-65536,65535,0.001";
  1095. const String hint_slider = "0.0,1.0,0.01,slider";
  1096. switch(graph->node_get_type(type,p_id)) {
  1097. case ShaderGraph::NODE_INPUT: {
  1098. gn->set_title("Input");
  1099. List<ShaderGraph::SlotInfo> si;
  1100. ShaderGraph::get_input_output_node_slot_info(graph->get_mode(),type,&si);
  1101. int idx=0;
  1102. for (List<ShaderGraph::SlotInfo>::Element *E=si.front();E;E=E->next()) {
  1103. ShaderGraph::SlotInfo& s=E->get();
  1104. if (s.dir==ShaderGraph::SLOT_IN) {
  1105. Label *l= memnew( Label );
  1106. l->set_text(s.name);
  1107. l->set_align(Label::ALIGN_RIGHT);
  1108. gn->add_child(l);
  1109. gn->set_slot(idx,false,0,Color(),true,s.type,typecol[s.type]);
  1110. idx++;
  1111. }
  1112. }
  1113. } break; // all inputs (case Shader type dependent)
  1114. case ShaderGraph::NODE_SCALAR_CONST: {
  1115. gn->set_title("Scalar");
  1116. SpinBox *sb = memnew( SpinBox );
  1117. sb->set_min(-100000);
  1118. sb->set_max(100000);
  1119. sb->set_step(0.001);
  1120. sb->set_val(graph->scalar_const_node_get_value(type,p_id));
  1121. sb->connect("value_changed",this,"_scalar_const_changed",varray(p_id));
  1122. gn->add_child(sb);
  1123. gn->set_slot(0,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1124. } break; //scalar constant
  1125. case ShaderGraph::NODE_VEC_CONST: {
  1126. gn->set_title("Vector");
  1127. Array v3p(true);
  1128. for(int i=0;i<3;i++) {
  1129. HBoxContainer *hbc = memnew( HBoxContainer );
  1130. Label *l = memnew( Label );
  1131. l->set_text(String::chr('X'+i));
  1132. hbc->add_child(l);
  1133. SpinBox *sb = memnew( SpinBox );
  1134. sb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1135. sb->set_min(-100000);
  1136. sb->set_max(100000);
  1137. sb->set_step(0.001);
  1138. sb->set_val(graph->vec_const_node_get_value(type,p_id)[i]);
  1139. sb->connect("value_changed",this,"_vec_const_changed",varray(p_id,v3p));
  1140. v3p.push_back(sb);
  1141. hbc->add_child(sb);
  1142. gn->add_child(hbc);
  1143. }
  1144. gn->set_slot(0,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1145. } break; //vec3 constant
  1146. case ShaderGraph::NODE_RGB_CONST: {
  1147. gn->set_title("Color");
  1148. ColorPickerButton *cpb = memnew( ColorPickerButton );
  1149. cpb->set_color(graph->rgb_const_node_get_value(type,p_id));
  1150. cpb->connect("color_changed",this,"_rgb_const_changed",varray(p_id));
  1151. gn->add_child(cpb);
  1152. Label *l = memnew( Label );
  1153. l->set_text("RGB");
  1154. l->set_align(Label::ALIGN_RIGHT);
  1155. gn->add_child(l);
  1156. l = memnew( Label );
  1157. l->set_text("Alpha");
  1158. l->set_align(Label::ALIGN_RIGHT);
  1159. gn->add_child(l);
  1160. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1161. gn->set_slot(2,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1162. } break; //rgb constant (shows a color picker instead)
  1163. case ShaderGraph::NODE_XFORM_CONST: {
  1164. gn->set_title("XForm");
  1165. ToolButton *edit = memnew( ToolButton );
  1166. edit->set_text("edit..");
  1167. edit->connect("pressed",this,"_xform_const_changed",varray(p_id,edit));
  1168. gn->add_child(edit);
  1169. gn->set_slot(0,false,0,Color(),true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM]);
  1170. } break; // 4x4 matrix constant
  1171. case ShaderGraph::NODE_TIME: {
  1172. gn->set_title("Time");
  1173. Label *l = memnew( Label );
  1174. l->set_text("(s)");
  1175. l->set_align(Label::ALIGN_RIGHT);
  1176. gn->add_child(l);
  1177. gn->set_slot(0,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1178. } break; // time in seconds
  1179. case ShaderGraph::NODE_SCREEN_TEX: {
  1180. gn->set_title("ScreenTex");
  1181. HBoxContainer *hbc = memnew( HBoxContainer );
  1182. hbc->add_constant_override("separation",0);
  1183. if (!graph->is_slot_connected(type,p_id,0)) {
  1184. Vector3 v = graph->default_get_value(type, p_id, 0);
  1185. hbc->add_child(make_editor("UV: " + v,gn,p_id,0,Variant::VECTOR3));
  1186. } else {
  1187. hbc->add_child(make_label("UV",Variant::VECTOR3));
  1188. }
  1189. hbc->add_spacer();
  1190. hbc->add_child( memnew(Label("RGB")));
  1191. gn->add_child(hbc);
  1192. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1193. } break; // screen texture sampler (takes UV) (only usable in fragment case Shader)
  1194. case ShaderGraph::NODE_SCALAR_OP: {
  1195. gn->set_title("ScalarOp");
  1196. static const char* op_name[ShaderGraph::SCALAR_MAX_OP]={
  1197. ("Add"),
  1198. ("Sub"),
  1199. ("Mul"),
  1200. ("Div"),
  1201. ("Mod"),
  1202. ("Pow"),
  1203. ("Max"),
  1204. ("Min"),
  1205. ("Atan2")
  1206. };
  1207. OptionButton *ob = memnew( OptionButton );
  1208. for(int i=0;i<ShaderGraph::SCALAR_MAX_OP;i++) {
  1209. ob->add_item(op_name[i],i);
  1210. }
  1211. ob->select(graph->scalar_op_node_get_op(type,p_id));
  1212. ob->connect("item_selected",this,"_scalar_op_changed",varray(p_id));
  1213. gn->add_child(ob);
  1214. HBoxContainer *hbc = memnew( HBoxContainer );
  1215. hbc->add_constant_override("separation",0);
  1216. if (graph->is_slot_connected(type, p_id, 0)) {
  1217. hbc->add_child(make_label("a",Variant::REAL));
  1218. } else {
  1219. float v = graph->default_get_value(type,p_id,0);
  1220. hbc->add_child(make_editor(String("a: ")+Variant(v),gn,p_id,0,Variant::REAL,hint_spin));
  1221. }
  1222. hbc->add_spacer();
  1223. hbc->add_child( memnew(Label("out")));
  1224. gn->add_child(hbc);
  1225. if (graph->is_slot_connected(type, p_id, 1)) {
  1226. gn->add_child(make_label("b",Variant::REAL));
  1227. } else {
  1228. float v = graph->default_get_value(type,p_id,1);
  1229. gn->add_child(make_editor(String("b: ")+Variant(v),gn,p_id,1,Variant::REAL,hint_spin));
  1230. }
  1231. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1232. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1233. } break; // scalar vs scalar op (mul: { } break; add: { } break; div: { } break; etc)
  1234. case ShaderGraph::NODE_VEC_OP: {
  1235. gn->set_title("VecOp");
  1236. static const char* op_name[ShaderGraph::VEC_MAX_OP]={
  1237. ("Add"),
  1238. ("Sub"),
  1239. ("Mul"),
  1240. ("Div"),
  1241. ("Mod"),
  1242. ("Pow"),
  1243. ("Max"),
  1244. ("Min"),
  1245. ("Cross")
  1246. };
  1247. OptionButton *ob = memnew( OptionButton );
  1248. for(int i=0;i<ShaderGraph::VEC_MAX_OP;i++) {
  1249. ob->add_item(op_name[i],i);
  1250. }
  1251. ob->select(graph->vec_op_node_get_op(type,p_id));
  1252. ob->connect("item_selected",this,"_vec_op_changed",varray(p_id));
  1253. gn->add_child(ob);
  1254. HBoxContainer *hbc = memnew( HBoxContainer );
  1255. hbc->add_constant_override("separation",0);
  1256. if (graph->is_slot_connected(type, p_id, 0)) {
  1257. hbc->add_child(make_label("a",Variant::VECTOR3));
  1258. } else {
  1259. Vector3 v = graph->default_get_value(type,p_id,0);
  1260. hbc->add_child(make_editor(String("a: ")+v,gn,p_id,0,Variant::VECTOR3));
  1261. }
  1262. hbc->add_spacer();
  1263. hbc->add_child( memnew(Label("out")));
  1264. gn->add_child(hbc);
  1265. if (graph->is_slot_connected(type, p_id, 1)) {
  1266. gn->add_child(make_label("b",Variant::VECTOR3));
  1267. } else {
  1268. Vector3 v = graph->default_get_value(type,p_id,1);
  1269. gn->add_child(make_editor(String("b: ")+v,gn,p_id,1,Variant::VECTOR3));
  1270. }
  1271. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1272. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1273. } break; // vec3 vs vec3 op (mul: { } break;ad: { } break;div: { } break;crossprod: { } break;etc)
  1274. case ShaderGraph::NODE_VEC_SCALAR_OP: {
  1275. gn->set_title("VecScalarOp");
  1276. static const char* op_name[ShaderGraph::VEC_SCALAR_MAX_OP]={
  1277. ("Mul"),
  1278. ("Div"),
  1279. ("Pow"),
  1280. };
  1281. OptionButton *ob = memnew( OptionButton );
  1282. for(int i=0;i<ShaderGraph::VEC_SCALAR_MAX_OP;i++) {
  1283. ob->add_item(op_name[i],i);
  1284. }
  1285. ob->select(graph->vec_scalar_op_node_get_op(type,p_id));
  1286. ob->connect("item_selected",this,"_vec_scalar_op_changed",varray(p_id));
  1287. gn->add_child(ob);
  1288. HBoxContainer *hbc = memnew( HBoxContainer );
  1289. hbc->add_constant_override("separation",0);
  1290. if (graph->is_slot_connected(type, p_id, 0)) {
  1291. hbc->add_child(make_label("a",Variant::VECTOR3));
  1292. } else {
  1293. Vector3 v = graph->default_get_value(type,p_id,0);
  1294. hbc->add_child(make_editor(String("a: ")+v,gn,p_id,0,Variant::VECTOR3));
  1295. }
  1296. hbc->add_spacer();
  1297. hbc->add_child( memnew(Label("out")));
  1298. gn->add_child(hbc);
  1299. if (graph->is_slot_connected(type, p_id, 1)) {
  1300. gn->add_child(make_label("b",Variant::REAL));
  1301. } else {
  1302. float v = graph->default_get_value(type,p_id,1);
  1303. gn->add_child(make_editor(String("b: ")+Variant(v),gn,p_id,1,Variant::REAL,hint_spin));
  1304. }
  1305. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1306. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1307. } break; // vec3 vs scalar op (mul: { } break; add: { } break; div: { } break; etc)
  1308. case ShaderGraph::NODE_RGB_OP: {
  1309. gn->set_title("RGB Op");
  1310. static const char* op_name[ShaderGraph::RGB_MAX_OP]={
  1311. ("Screen"),
  1312. ("Difference"),
  1313. ("Darken"),
  1314. ("Lighten"),
  1315. ("Overlay"),
  1316. ("Dodge"),
  1317. ("Burn"),
  1318. ("SoftLight"),
  1319. ("HardLight")
  1320. };
  1321. OptionButton *ob = memnew( OptionButton );
  1322. for(int i=0;i<ShaderGraph::RGB_MAX_OP;i++) {
  1323. ob->add_item(op_name[i],i);
  1324. }
  1325. ob->select(graph->rgb_op_node_get_op(type,p_id));
  1326. ob->connect("item_selected",this,"_rgb_op_changed",varray(p_id));
  1327. gn->add_child(ob);
  1328. HBoxContainer *hbc = memnew( HBoxContainer );
  1329. hbc->add_constant_override("separation",0);
  1330. if (graph->is_slot_connected(type, p_id, 0)) {
  1331. hbc->add_child(make_label("a",Variant::COLOR));
  1332. } else {
  1333. hbc->add_child(make_editor(String("a: "),gn,p_id,0,Variant::COLOR));
  1334. }
  1335. hbc->add_spacer();
  1336. hbc->add_child( memnew(Label("out")));
  1337. gn->add_child(hbc);
  1338. if (graph->is_slot_connected(type, p_id, 1)) {
  1339. gn->add_child(make_label("b",Variant::COLOR));
  1340. } else {
  1341. gn->add_child(make_editor(String("b: "),gn,p_id,1,Variant::COLOR));
  1342. }
  1343. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1344. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1345. } break; // vec3 vs vec3 rgb op (with scalar amount): { } break; like brighten: { } break; darken: { } break; burn: { } break; dodge: { } break; multiply: { } break; etc.
  1346. case ShaderGraph::NODE_XFORM_MULT: {
  1347. gn->set_title("XFMult");
  1348. HBoxContainer *hbc = memnew( HBoxContainer );
  1349. if (graph->is_slot_connected(type, p_id, 0)) {
  1350. hbc->add_child(make_label("a",Variant::TRANSFORM));
  1351. } else {
  1352. hbc->add_child(make_editor(String("a: edit..."),gn,p_id,0,Variant::TRANSFORM));
  1353. }
  1354. hbc->add_spacer();
  1355. hbc->add_child( memnew(Label("out")));
  1356. gn->add_child(hbc);
  1357. if (graph->is_slot_connected(type, p_id, 1)) {
  1358. gn->add_child(make_label("b",Variant::TRANSFORM));
  1359. } else {
  1360. gn->add_child(make_editor(String("b: edit..."),gn,p_id,1,Variant::TRANSFORM));
  1361. }
  1362. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM],true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM]);
  1363. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM],false,0,Color());
  1364. } break; // mat4 x mat4
  1365. case ShaderGraph::NODE_XFORM_VEC_MULT: {
  1366. gn->set_title("XFVecMult");
  1367. CheckBox *button = memnew (CheckBox("RotOnly"));
  1368. button->set_pressed(graph->xform_vec_mult_node_get_no_translation(type,p_id));
  1369. button->connect("toggled",this,"_xform_inv_rev_changed",varray(p_id));
  1370. gn->add_child(button);
  1371. HBoxContainer *hbc = memnew( HBoxContainer );
  1372. hbc->add_constant_override("separation",0);
  1373. if (graph->is_slot_connected(type, p_id, 0)) {
  1374. hbc->add_child(make_label("xf",Variant::TRANSFORM));
  1375. } else {
  1376. hbc->add_child(make_editor(String("xf: edit..."),gn,p_id,0,Variant::TRANSFORM));
  1377. }
  1378. hbc->add_spacer();
  1379. Label *l = memnew(Label("out"));
  1380. l->set_align(Label::ALIGN_RIGHT);
  1381. hbc->add_child( l);
  1382. gn->add_child(hbc);
  1383. if (graph->is_slot_connected(type, p_id, 1)) {
  1384. gn->add_child(make_label("a",Variant::VECTOR3));
  1385. } else {
  1386. Vector3 v = graph->default_get_value(type,p_id,1);
  1387. gn->add_child(make_editor(String("a: ")+v,gn,p_id,1,Variant::VECTOR3));
  1388. }
  1389. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1390. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1391. } break;
  1392. case ShaderGraph::NODE_XFORM_VEC_INV_MULT: {
  1393. gn->set_title("XFVecInvMult");
  1394. CheckBox *button = memnew( CheckBox("RotOnly"));
  1395. button->set_pressed(graph->xform_vec_mult_node_get_no_translation(type,p_id));
  1396. button->connect("toggled",this,"_xform_inv_rev_changed",varray(p_id));
  1397. gn->add_child(button);
  1398. if (graph->is_slot_connected(type, p_id, 0)) {
  1399. gn->add_child(make_label("a",Variant::VECTOR3));
  1400. } else {
  1401. Vector3 v = graph->default_get_value(type,p_id,0);
  1402. gn->add_child(make_editor(String("a: ")+v,gn,p_id,0,Variant::VECTOR3));
  1403. }
  1404. HBoxContainer *hbc = memnew( HBoxContainer );
  1405. hbc->add_constant_override("separation",0);
  1406. if (graph->is_slot_connected(type, p_id, 1)) {
  1407. hbc->add_child(make_label("xf", Variant::TRANSFORM));
  1408. } else {
  1409. hbc->add_child(make_editor(String("xf: edit..."),gn,p_id,1,Variant::TRANSFORM));
  1410. }
  1411. hbc->add_spacer();
  1412. Label *l = memnew(Label("out"));
  1413. l->set_align(Label::ALIGN_RIGHT);
  1414. hbc->add_child( l);
  1415. gn->add_child(hbc);
  1416. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1417. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1418. } break; // mat4 x vec3 inverse mult (with no-translation option)
  1419. case ShaderGraph::NODE_SCALAR_FUNC: {
  1420. gn->set_title("ScalarFunc");
  1421. static const char* func_name[ShaderGraph::SCALAR_MAX_FUNC]={
  1422. ("Sin"),
  1423. ("Cos"),
  1424. ("Tan"),
  1425. ("ASin"),
  1426. ("ACos"),
  1427. ("ATan"),
  1428. ("SinH"),
  1429. ("CosH"),
  1430. ("TanH"),
  1431. ("Log"),
  1432. ("Exp"),
  1433. ("Sqrt"),
  1434. ("Abs"),
  1435. ("Sign"),
  1436. ("Floor"),
  1437. ("Round"),
  1438. ("Ceil"),
  1439. ("Frac"),
  1440. ("Satr"),
  1441. ("Neg")
  1442. };
  1443. OptionButton *ob = memnew( OptionButton );
  1444. for(int i=0;i<ShaderGraph::SCALAR_MAX_FUNC;i++) {
  1445. ob->add_item(func_name[i],i);
  1446. }
  1447. ob->select(graph->scalar_func_node_get_function(type,p_id));
  1448. ob->connect("item_selected",this,"_scalar_func_changed",varray(p_id));
  1449. gn->add_child(ob);
  1450. HBoxContainer *hbc = memnew( HBoxContainer );
  1451. if (graph->is_slot_connected(type, p_id, 0)) {
  1452. hbc->add_child(make_label("in", Variant::REAL));
  1453. } else {
  1454. float v = graph->default_get_value(type,p_id,0);
  1455. hbc->add_child(make_editor(String("in: ")+Variant(v),gn,p_id,0,Variant::REAL,hint_spin));
  1456. }
  1457. hbc->add_spacer();
  1458. hbc->add_child( memnew(Label("out")));
  1459. gn->add_child(hbc);
  1460. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1461. } break; // scalar function (sin: { } break; cos: { } break; etc)
  1462. case ShaderGraph::NODE_VEC_FUNC: {
  1463. gn->set_title("VecFunc");
  1464. static const char* func_name[ShaderGraph::VEC_MAX_FUNC]={
  1465. ("Normalize"),
  1466. ("Saturate"),
  1467. ("Negate"),
  1468. ("Reciprocal"),
  1469. ("RGB to HSV"),
  1470. ("HSV to RGB"),
  1471. };
  1472. OptionButton *ob = memnew( OptionButton );
  1473. for(int i=0;i<ShaderGraph::VEC_MAX_FUNC;i++) {
  1474. ob->add_item(func_name[i],i);
  1475. }
  1476. ob->select(graph->vec_func_node_get_function(type,p_id));
  1477. ob->connect("item_selected",this,"_vec_func_changed",varray(p_id));
  1478. gn->add_child(ob);
  1479. HBoxContainer *hbc = memnew( HBoxContainer );
  1480. hbc->add_constant_override("separation",0);
  1481. if (graph->is_slot_connected(type, p_id, 0)) {
  1482. hbc->add_child(make_label("in", Variant::VECTOR3));
  1483. } else {
  1484. Vector3 v = graph->default_get_value(type,p_id,0);
  1485. hbc->add_child(make_editor(String("in: ")+v,gn,p_id,0,Variant::VECTOR3));
  1486. }
  1487. hbc->add_spacer();
  1488. hbc->add_child( memnew(Label("out")));
  1489. gn->add_child(hbc);
  1490. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1491. } break; // vector function (normalize: { } break; negate: { } break; reciprocal: { } break; rgb2hsv: { } break; hsv2rgb: { } break; etc: { } break; etc)
  1492. case ShaderGraph::NODE_VEC_LEN: {
  1493. gn->set_title("VecLength");
  1494. HBoxContainer *hbc = memnew( HBoxContainer );
  1495. if (graph->is_slot_connected(type, p_id, 0)) {
  1496. hbc->add_child(make_label("in", Variant::VECTOR3));
  1497. } else {
  1498. Vector3 v = graph->default_get_value(type,p_id,0);
  1499. hbc->add_child(make_editor(String("in: ")+v,gn,p_id,0,Variant::VECTOR3));
  1500. }
  1501. hbc->add_spacer();
  1502. hbc->add_child( memnew(Label("len")));
  1503. gn->add_child(hbc);
  1504. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1505. } break; // vec3 length
  1506. case ShaderGraph::NODE_DOT_PROD: {
  1507. gn->set_title("DotProduct");
  1508. HBoxContainer *hbc = memnew( HBoxContainer );
  1509. hbc->add_constant_override("separation",0);
  1510. if (graph->is_slot_connected(type, p_id, 0)) {
  1511. hbc->add_child(make_label("a", Variant::VECTOR3));
  1512. } else {
  1513. Vector3 v = graph->default_get_value(type,p_id,0);
  1514. hbc->add_child(make_editor(String("a: ")+v,gn,p_id,0,Variant::VECTOR3));
  1515. }
  1516. hbc->add_spacer();
  1517. hbc->add_child( memnew(Label("dp")));
  1518. gn->add_child(hbc);
  1519. if (graph->is_slot_connected(type, p_id, 1)) {
  1520. gn->add_child(make_label("b", Variant::VECTOR3));
  1521. } else {
  1522. Vector3 v = graph->default_get_value(type,p_id,1);
  1523. gn->add_child(make_editor(String("b: ")+v,gn,p_id,1,Variant::VECTOR3));
  1524. }
  1525. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1526. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1527. } break; // vec3 . vec3 (dot product -> scalar output)
  1528. case ShaderGraph::NODE_VEC_TO_SCALAR: {
  1529. gn->set_title("Vec2Scalar");
  1530. HBoxContainer *hbc = memnew( HBoxContainer );
  1531. hbc->add_constant_override("separation",0);
  1532. if (graph->is_slot_connected(type, p_id, 0)) {
  1533. hbc->add_child(make_label("vec", Variant::VECTOR3));
  1534. } else {
  1535. Vector3 v = graph->default_get_value(type,p_id,0);
  1536. hbc->add_child(make_editor(String("vec: ")+v,gn,p_id,0,Variant::VECTOR3));
  1537. }
  1538. hbc->add_spacer();
  1539. Label *l=memnew(Label("x"));
  1540. l->set_align(Label::ALIGN_RIGHT);
  1541. hbc->add_child( l);
  1542. gn->add_child(hbc);
  1543. l=memnew(Label("y"));
  1544. l->set_align(Label::ALIGN_RIGHT);
  1545. gn->add_child( l );
  1546. l=memnew(Label("z"));
  1547. l->set_align(Label::ALIGN_RIGHT);
  1548. gn->add_child( l);
  1549. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1550. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1551. gn->set_slot(2,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1552. } break; // 1 vec3 input: { } break; 3 scalar outputs
  1553. case ShaderGraph::NODE_SCALAR_TO_VEC: {
  1554. gn->set_title("Scalar2Vec");
  1555. HBoxContainer *hbc = memnew( HBoxContainer );
  1556. if (graph->is_slot_connected(type, p_id, 0)) {
  1557. hbc->add_child(make_label("x", Variant::REAL));
  1558. } else {
  1559. float v = graph->default_get_value(type,p_id,0);
  1560. hbc->add_child(make_editor(String("x: ")+Variant(v),gn,p_id,0,Variant::REAL));
  1561. }
  1562. hbc->add_spacer();
  1563. hbc->add_child( memnew(Label("vec")));
  1564. gn->add_child(hbc);
  1565. if (graph->is_slot_connected(type, p_id, 1)) {
  1566. gn->add_child(make_label("y", Variant::REAL));
  1567. } else {
  1568. float v = graph->default_get_value(type,p_id,1);
  1569. gn->add_child(make_editor(String("y: ")+Variant(v),gn,p_id,1,Variant::REAL));
  1570. }
  1571. if (graph->is_slot_connected(type, p_id, 2)) {
  1572. gn->add_child(make_label("in", Variant::REAL));
  1573. } else {
  1574. float v = graph->default_get_value(type,p_id,2);
  1575. gn->add_child(make_editor(String("in: ")+Variant(v),gn,p_id,2,Variant::REAL));
  1576. }
  1577. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1578. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1579. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1580. } break; // 3 scalar input: { } break; 1 vec3 output
  1581. case ShaderGraph::NODE_VEC_TO_XFORM: {
  1582. gn->set_title("Vec2XForm");
  1583. HBoxContainer *hbc = memnew( HBoxContainer );
  1584. hbc->add_constant_override("separation",0);
  1585. if (graph->is_slot_connected(type, p_id, 0)) {
  1586. hbc->add_child(make_label("x", Variant::VECTOR3));
  1587. } else {
  1588. Vector3 v = graph->default_get_value(type,p_id,0);
  1589. hbc->add_child(make_editor(String("x: ")+v,gn,p_id,0,Variant::VECTOR3));
  1590. }
  1591. hbc->add_spacer();
  1592. hbc->add_child( memnew(Label("xf")));
  1593. gn->add_child(hbc);
  1594. if (graph->is_slot_connected(type, p_id, 1)) {
  1595. gn->add_child(make_label("y", Variant::VECTOR3));
  1596. } else {
  1597. Vector3 v = graph->default_get_value(type,p_id,1);
  1598. gn->add_child(make_editor(String("y: ")+v,gn,p_id,1,Variant::VECTOR3));
  1599. }
  1600. if (graph->is_slot_connected(type, p_id, 2)) {
  1601. gn->add_child(make_label("z", Variant::VECTOR3));
  1602. } else {
  1603. Vector3 v = graph->default_get_value(type,p_id,2);
  1604. gn->add_child(make_editor(String("z: ")+v,gn,p_id,2,Variant::VECTOR3));
  1605. }
  1606. if (graph->is_slot_connected(type, p_id, 3)) {
  1607. gn->add_child(make_label("ofs", Variant::VECTOR3));
  1608. } else {
  1609. Vector3 v = graph->default_get_value(type,p_id,3);
  1610. gn->add_child(make_editor(String("ofs: ")+v,gn,p_id,3,Variant::VECTOR3));
  1611. }
  1612. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM]);
  1613. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1614. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1615. gn->set_slot(3,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1616. } break; // 3 vec input: { } break; 1 xform output
  1617. case ShaderGraph::NODE_XFORM_TO_VEC: {
  1618. gn->set_title("XForm2Vec");
  1619. HBoxContainer *hbc = memnew( HBoxContainer );
  1620. hbc->add_constant_override("separation",0);
  1621. if (graph->is_slot_connected(type, p_id, 0)) {
  1622. hbc->add_child(make_label("fx", Variant::TRANSFORM));
  1623. } else {
  1624. hbc->add_child(make_editor(String("fx: edit..."),gn,p_id,0,Variant::TRANSFORM));
  1625. }
  1626. hbc->add_spacer();
  1627. Label *l=memnew(Label("x"));
  1628. l->set_align(Label::ALIGN_RIGHT);
  1629. hbc->add_child( l);
  1630. gn->add_child(hbc);
  1631. l=memnew(Label("y"));
  1632. l->set_align(Label::ALIGN_RIGHT);
  1633. gn->add_child( l );
  1634. l=memnew(Label("z"));
  1635. l->set_align(Label::ALIGN_RIGHT);
  1636. gn->add_child( l);
  1637. l=memnew(Label("ofs"));
  1638. l->set_align(Label::ALIGN_RIGHT);
  1639. gn->add_child( l);
  1640. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1641. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1642. gn->set_slot(2,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1643. gn->set_slot(3,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1644. } break; // 3 vec input: { } break; 1 xform output
  1645. case ShaderGraph::NODE_SCALAR_INTERP: {
  1646. gn->set_title("ScalarInterp");
  1647. HBoxContainer *hbc = memnew( HBoxContainer );
  1648. hbc->add_constant_override("separation",0);
  1649. if (graph->is_slot_connected(type, p_id, 0)) {
  1650. hbc->add_child(make_label("a", Variant::REAL));
  1651. } else {
  1652. float v = graph->default_get_value(type,p_id,0);
  1653. hbc->add_child(make_editor(String("a: ")+Variant(v),gn,p_id,0,Variant::REAL,hint_spin));
  1654. }
  1655. hbc->add_spacer();
  1656. hbc->add_child( memnew(Label("interp")));
  1657. gn->add_child(hbc);
  1658. if (graph->is_slot_connected(type, p_id, 1)) {
  1659. gn->add_child(make_label("b", Variant::REAL));
  1660. } else {
  1661. float v = graph->default_get_value(type,p_id,1);
  1662. gn->add_child(make_editor(String("b: ")+Variant(v),gn,p_id,1,Variant::REAL,hint_spin));
  1663. }
  1664. if (graph->is_slot_connected(type, p_id, 2)) {
  1665. gn->add_child(make_label("c", Variant::REAL));
  1666. } else {
  1667. float v = graph->default_get_value(type,p_id,2);
  1668. gn->add_child(make_editor(String("c: ")+Variant(v),gn,p_id,2,Variant::REAL,hint_slider));
  1669. }
  1670. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1671. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1672. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1673. } break; // scalar interpolation (with optional curve)
  1674. case ShaderGraph::NODE_VEC_INTERP: {
  1675. gn->set_title("VecInterp");
  1676. HBoxContainer *hbc = memnew( HBoxContainer );
  1677. if (graph->is_slot_connected(type, p_id, 0)) {
  1678. hbc->add_child(make_label("a", Variant::VECTOR3));
  1679. } else {
  1680. Vector3 v = graph->default_get_value(type,p_id,0);
  1681. hbc->add_child(make_editor(String("a: ")+v,gn,p_id,0,Variant::VECTOR3));
  1682. }
  1683. hbc->add_spacer();
  1684. hbc->add_child( memnew(Label("interp")));
  1685. gn->add_child(hbc);
  1686. if (graph->is_slot_connected(type, p_id, 1)) {
  1687. gn->add_child(make_label("b", Variant::VECTOR3));
  1688. } else {
  1689. Vector3 v = graph->default_get_value(type,p_id,1);
  1690. gn->add_child(make_editor(String("b: ")+v,gn,p_id,1,Variant::VECTOR3));
  1691. }
  1692. if (graph->is_slot_connected(type, p_id, 2)) {
  1693. gn->add_child(make_label("c", Variant::REAL));
  1694. } else {
  1695. float v = graph->default_get_value(type,p_id,2);
  1696. gn->add_child(make_editor(String("c: ")+Variant(v),gn,p_id,2,Variant::REAL,hint_slider));
  1697. }
  1698. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1699. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],false,0,Color());
  1700. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],false,0,Color());
  1701. } break; // vec3 interpolation (with optional curve)
  1702. case ShaderGraph::NODE_COLOR_RAMP: {
  1703. gn->set_title("ColorRamp");
  1704. GraphColorRampEdit * ramp = memnew( GraphColorRampEdit );
  1705. PoolVector<real_t> offsets = graph->color_ramp_node_get_offsets(type,p_id);
  1706. PoolVector<Color> colors = graph->color_ramp_node_get_colors(type,p_id);
  1707. int oc = offsets.size();
  1708. if (oc) {
  1709. PoolVector<real_t>::Read rofs = offsets.read();
  1710. PoolVector<Color>::Read rcol = colors.read();
  1711. Vector<float> ofsv;
  1712. Vector<Color> colorv;
  1713. for(int i=0;i<oc;i++) {
  1714. ofsv.push_back(rofs[i]);
  1715. colorv.push_back(rcol[i]);
  1716. }
  1717. ramp->set_ramp(ofsv,colorv);
  1718. }
  1719. ramp->connect("ramp_changed",this,"_color_ramp_changed",varray(p_id,ramp));
  1720. ramp->set_custom_minimum_size(Size2(128,1));
  1721. gn->add_child(ramp);
  1722. HBoxContainer *hbc = memnew( HBoxContainer );
  1723. hbc->add_constant_override("separation",0);
  1724. if (graph->is_slot_connected(type, p_id, 0)) {
  1725. hbc->add_child(make_label("c", Variant::REAL));
  1726. } else {
  1727. float v = graph->default_get_value(type,p_id,0);
  1728. hbc->add_child(make_editor(String("c: ")+Variant(v),gn,p_id,0,Variant::REAL,hint_slider));
  1729. }
  1730. hbc->add_spacer();
  1731. Label *l=memnew(Label("rgb"));
  1732. l->set_align(Label::ALIGN_RIGHT);
  1733. hbc->add_child( l);
  1734. gn->add_child(hbc);
  1735. l=memnew(Label("alpha"));
  1736. l->set_align(Label::ALIGN_RIGHT);
  1737. gn->add_child( l);
  1738. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1739. gn->set_slot(2,false,ShaderGraph::SLOT_MAX,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1740. } break; // scalar interpolation (with optional curve)
  1741. case ShaderGraph::NODE_CURVE_MAP: {
  1742. gn->set_title("CurveMap");
  1743. GraphCurveMapEdit * map = memnew( GraphCurveMapEdit );
  1744. PoolVector<Vector2> points = graph->curve_map_node_get_points(type,p_id);
  1745. int oc = points.size();
  1746. if (oc) {
  1747. PoolVector<Vector2>::Read rofs = points.read();
  1748. Vector<Vector2> ofsv;
  1749. for(int i=0;i<oc;i++) {
  1750. ofsv.push_back(rofs[i]);
  1751. }
  1752. map->set_points(ofsv);
  1753. }
  1754. map->connect("curve_changed",this,"_curve_changed",varray(p_id,map));
  1755. //map->connect("map_changed",this,"_curve_map_changed",varray(p_id,map));
  1756. map->set_custom_minimum_size(Size2(128,64));
  1757. gn->add_child(map);
  1758. HBoxContainer *hbc = memnew( HBoxContainer );
  1759. hbc->add_constant_override("separation",0);
  1760. if (graph->is_slot_connected(type, p_id, 0)) {
  1761. hbc->add_child(make_label("c", Variant::REAL));
  1762. } else {
  1763. float v = graph->default_get_value(type,p_id,0);
  1764. hbc->add_child(make_editor(String("c: ")+Variant(v),gn,p_id,0,Variant::REAL,hint_slider));
  1765. }
  1766. hbc->add_spacer();
  1767. Label *l=memnew(Label("cmap"));
  1768. l->set_align(Label::ALIGN_RIGHT);
  1769. hbc->add_child( l);
  1770. gn->add_child(hbc);
  1771. gn->set_slot(1,true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR],true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1772. } break; // scalar interpolation (with optional curve)
  1773. case ShaderGraph::NODE_SCALAR_INPUT: {
  1774. gn->set_title("ScalarUniform");
  1775. LineEdit *le = memnew( LineEdit );
  1776. gn->add_child(le);
  1777. le->set_text(graph->input_node_get_name(type,p_id));
  1778. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1779. SpinBox *sb = memnew( SpinBox );
  1780. sb->set_min(-100000);
  1781. sb->set_max(100000);
  1782. sb->set_step(0.001);
  1783. sb->set_val(graph->scalar_input_node_get_value(type,p_id));
  1784. sb->connect("value_changed",this,"_scalar_input_changed",varray(p_id));
  1785. gn->add_child(sb);
  1786. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1787. } break; // scalar uniform (assignable in material)
  1788. case ShaderGraph::NODE_VEC_INPUT: {
  1789. gn->set_title("VectorUniform");
  1790. LineEdit *le = memnew( LineEdit );
  1791. gn->add_child(le);
  1792. le->set_text(graph->input_node_get_name(type,p_id));
  1793. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1794. Array v3p(true);
  1795. for(int i=0;i<3;i++) {
  1796. HBoxContainer *hbc = memnew( HBoxContainer );
  1797. Label *l = memnew( Label );
  1798. l->set_text(String::chr('X'+i));
  1799. hbc->add_child(l);
  1800. SpinBox *sb = memnew( SpinBox );
  1801. sb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1802. sb->set_min(-100000);
  1803. sb->set_max(100000);
  1804. sb->set_step(0.001);
  1805. sb->set_val(graph->vec_input_node_get_value(type,p_id)[i]);
  1806. sb->connect("value_changed",this,"_vec_input_changed",varray(p_id,v3p));
  1807. v3p.push_back(sb);
  1808. hbc->add_child(sb);
  1809. gn->add_child(hbc);
  1810. }
  1811. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1812. } break; // vec3 uniform (assignable in material)
  1813. case ShaderGraph::NODE_RGB_INPUT: {
  1814. gn->set_title("ColorUniform");
  1815. LineEdit *le = memnew( LineEdit );
  1816. gn->add_child(le);
  1817. le->set_text(graph->input_node_get_name(type,p_id));
  1818. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1819. ColorPickerButton *cpb = memnew( ColorPickerButton );
  1820. cpb->set_color(graph->rgb_input_node_get_value(type,p_id));
  1821. cpb->connect("color_changed",this,"_rgb_input_changed",varray(p_id));
  1822. gn->add_child(cpb);
  1823. Label *l = memnew( Label );
  1824. l->set_text("RGB");
  1825. l->set_align(Label::ALIGN_RIGHT);
  1826. gn->add_child(l);
  1827. l = memnew( Label );
  1828. l->set_text("Alpha");
  1829. l->set_align(Label::ALIGN_RIGHT);
  1830. gn->add_child(l);
  1831. gn->set_slot(2,false,0,Color(),true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1832. gn->set_slot(3,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1833. } break; // color uniform (assignable in material)
  1834. case ShaderGraph::NODE_XFORM_INPUT: {
  1835. gn->set_title("XFUniform");
  1836. LineEdit *le = memnew( LineEdit );
  1837. gn->add_child(le);
  1838. le->set_text(graph->input_node_get_name(type,p_id));
  1839. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1840. ToolButton *edit = memnew( ToolButton );
  1841. edit->set_text("edit..");
  1842. edit->connect("pressed",this,"_xform_input_changed",varray(p_id,edit));
  1843. gn->add_child(edit);
  1844. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_XFORM,typecol[ShaderGraph::SLOT_TYPE_XFORM]);
  1845. } break; // mat4 uniform (assignable in material)
  1846. case ShaderGraph::NODE_TEXTURE_INPUT: {
  1847. gn->set_title("TexUniform");
  1848. LineEdit *le = memnew( LineEdit );
  1849. gn->add_child(le);
  1850. le->set_text(graph->input_node_get_name(type,p_id));
  1851. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1852. TextureRect *tex = memnew( TextureRect );
  1853. tex->set_expand(true);
  1854. tex->set_custom_minimum_size(Size2(80,80));
  1855. tex->set_drag_forwarding(this);
  1856. gn->add_child(tex);
  1857. tex->set_mouse_filter(MOUSE_FILTER_PASS);
  1858. tex->set_texture(graph->texture_input_node_get_value(type,p_id));
  1859. ToolButton *edit = memnew( ToolButton );
  1860. edit->set_text("edit..");
  1861. edit->connect("pressed",this,"_tex_edited",varray(p_id,edit));
  1862. gn->add_child(edit);
  1863. HBoxContainer *hbc = memnew( HBoxContainer );
  1864. hbc->add_constant_override("separation",0);
  1865. if (graph->is_slot_connected(type, p_id, 0)) {
  1866. hbc->add_child(make_label("UV", Variant::VECTOR3));
  1867. } else {
  1868. Vector3 v = graph->default_get_value(type,p_id,0);
  1869. hbc->add_child(make_editor(String("UV: ")+v,gn,p_id,0,Variant::VECTOR3));
  1870. }
  1871. hbc->add_spacer();
  1872. Label *l=memnew(Label("RGB"));
  1873. l->set_align(Label::ALIGN_RIGHT);
  1874. hbc->add_child(l);
  1875. gn->add_child(hbc);
  1876. l = memnew( Label );
  1877. l->set_text("Alpha");
  1878. l->set_align(Label::ALIGN_RIGHT);
  1879. gn->add_child(l);
  1880. gn->set_slot(3,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1881. gn->set_slot(4,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1882. } break; // texture input (assignable in material)
  1883. case ShaderGraph::NODE_CUBEMAP_INPUT: {
  1884. gn->set_title("TexUniform");
  1885. LineEdit *le = memnew( LineEdit );
  1886. gn->add_child(le);
  1887. le->set_text(graph->input_node_get_name(type,p_id));
  1888. le->connect("text_entered",this,"_input_name_changed",varray(p_id,le));
  1889. ToolButton *edit = memnew( ToolButton );
  1890. edit->set_text("edit..");
  1891. edit->connect("pressed",this,"_cube_edited",varray(p_id,edit));
  1892. gn->add_child(edit);
  1893. HBoxContainer *hbc = memnew( HBoxContainer );
  1894. hbc->add_constant_override("separation",0);
  1895. if (graph->is_slot_connected(type, p_id, 0)) {
  1896. hbc->add_child(make_label("UV", Variant::VECTOR3));
  1897. } else {
  1898. Vector3 v = graph->default_get_value(type,p_id,0);
  1899. hbc->add_child(make_editor(String("UV: ")+v,gn,p_id,0,Variant::VECTOR3));
  1900. }
  1901. hbc->add_spacer();
  1902. Label *l=memnew(Label("RGB"));
  1903. l->set_align(Label::ALIGN_RIGHT);
  1904. hbc->add_child(l);
  1905. gn->add_child(hbc);
  1906. l = memnew( Label );
  1907. l->set_text("Alpha");
  1908. l->set_align(Label::ALIGN_RIGHT);
  1909. gn->add_child(l);
  1910. gn->set_slot(2,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1911. gn->set_slot(3,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1912. } break; // cubemap input (assignable in material)
  1913. case ShaderGraph::NODE_DEFAULT_TEXTURE: {
  1914. gn->set_title("CanvasItemTex");
  1915. HBoxContainer *hbc = memnew( HBoxContainer );
  1916. hbc->add_constant_override("separation",0);
  1917. if (graph->is_slot_connected(type, p_id, 0)) {
  1918. hbc->add_child(make_label("UV", Variant::VECTOR3));
  1919. } else {
  1920. Vector3 v = graph->default_get_value(type,p_id,0);
  1921. hbc->add_child(make_editor(String("UV: ")+v,gn,p_id,0,Variant::VECTOR3));
  1922. }
  1923. hbc->add_spacer();
  1924. Label *l=memnew(Label("RGB"));
  1925. l->set_align(Label::ALIGN_RIGHT);
  1926. hbc->add_child(l);
  1927. gn->add_child(hbc);
  1928. l = memnew( Label );
  1929. l->set_text("Alpha");
  1930. l->set_align(Label::ALIGN_RIGHT);
  1931. gn->add_child(l);
  1932. gn->set_slot(0,true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC],true,ShaderGraph::SLOT_TYPE_VEC,typecol[ShaderGraph::SLOT_TYPE_VEC]);
  1933. gn->set_slot(1,false,0,Color(),true,ShaderGraph::SLOT_TYPE_SCALAR,typecol[ShaderGraph::SLOT_TYPE_SCALAR]);
  1934. } break; // screen texture sampler (takes UV) (only usable in fragment case Shader)
  1935. case ShaderGraph::NODE_OUTPUT: {
  1936. gn->set_title("Output");
  1937. gn->set_show_close_button(false);
  1938. List<ShaderGraph::SlotInfo> si;
  1939. ShaderGraph::get_input_output_node_slot_info(graph->get_mode(),type,&si);
  1940. Array colors;
  1941. colors.push_back("Color");
  1942. colors.push_back("LightColor");
  1943. colors.push_back("Light");
  1944. colors.push_back("ShadowColor");
  1945. colors.push_back("Diffuse");
  1946. colors.push_back("Specular");
  1947. colors.push_back("Emission");
  1948. Array reals;
  1949. reals.push_back("Alpha");
  1950. reals.push_back("DiffuseAlpha");
  1951. reals.push_back("NormalMapDepth");
  1952. reals.push_back("SpecExp");
  1953. reals.push_back("Glow");
  1954. reals.push_back("ShadeParam");
  1955. reals.push_back("SpecularExp");
  1956. reals.push_back("LightAlpha");
  1957. reals.push_back("ShadowAlpha");
  1958. reals.push_back("PointSize");
  1959. reals.push_back("Discard");
  1960. int idx=0;
  1961. for (List<ShaderGraph::SlotInfo>::Element *E=si.front();E;E=E->next()) {
  1962. ShaderGraph::SlotInfo& s=E->get();
  1963. if (s.dir==ShaderGraph::SLOT_OUT) {
  1964. Variant::Type v;
  1965. if (colors.find(s.name)>=0)
  1966. v=Variant::COLOR;
  1967. else if (reals.find(s.name)>=0)
  1968. v=Variant::REAL;
  1969. else
  1970. v=Variant::VECTOR3;
  1971. gn->add_child(make_label(s.name, v));
  1972. gn->set_slot(idx,true,s.type,typecol[s.type],false,0,Color());
  1973. idx++;
  1974. }
  1975. }
  1976. } break; // output (case Shader type dependent)
  1977. case ShaderGraph::NODE_COMMENT: {
  1978. gn->set_title("Comment");
  1979. TextEdit *te = memnew(TextEdit);
  1980. te->set_custom_minimum_size(Size2(100,100));
  1981. gn->add_child(te);
  1982. te->set_text(graph->comment_node_get_text(type,p_id));
  1983. te->connect("text_changed",this,"_comment_edited",varray(p_id,te));
  1984. } break; // comment
  1985. }
  1986. gn->connect("dragged",this,"_node_moved",varray(p_id));
  1987. gn->connect("close_request",this,"_node_removed",varray(p_id),CONNECT_DEFERRED);
  1988. graph_edit->add_child(gn);
  1989. node_map[p_id]=gn;
  1990. gn->set_offset(graph->node_get_position(type,p_id));
  1991. }
  1992. void ShaderGraphView::_update_graph() {
  1993. if (block_update)
  1994. return;
  1995. for (Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  1996. memdelete(E->get());
  1997. }
  1998. node_map.clear();
  1999. if (!graph.is_valid())
  2000. return;
  2001. List<int> nl;
  2002. graph->get_node_list(type,&nl);
  2003. for(List<int>::Element *E=nl.front();E;E=E->next()) {
  2004. _create_node(E->get());
  2005. }
  2006. graph_edit->clear_connections();
  2007. List<ShaderGraph::Connection> connections;
  2008. graph->get_node_connections(type,&connections);
  2009. for(List<ShaderGraph::Connection>::Element *E=connections.front();E;E=E->next()) {
  2010. ERR_CONTINUE(!node_map.has(E->get().src_id) || !node_map.has(E->get().dst_id));
  2011. graph_edit->connect_node(node_map[E->get().src_id]->get_name(),E->get().src_slot,node_map[E->get().dst_id]->get_name(),E->get().dst_slot);
  2012. }
  2013. }
  2014. void ShaderGraphView::_sg_updated() {
  2015. if (!graph.is_valid())
  2016. return;
  2017. switch(graph->get_graph_error(type)) {
  2018. case ShaderGraph::GRAPH_OK: status->set_text(""); break;
  2019. case ShaderGraph::GRAPH_ERROR_CYCLIC: status->set_text(TTR("Error: Cyclic Connection Link")); break;
  2020. case ShaderGraph::GRAPH_ERROR_MISSING_CONNECTIONS: status->set_text(TTR("Error: Missing Input Connections")); break;
  2021. }
  2022. }
  2023. Variant ShaderGraphView::get_drag_data_fw(const Point2 &p_point, Control *p_from)
  2024. {
  2025. TextureRect* frame = Object::cast_to<TextureRect>(p_from);
  2026. if (!frame)
  2027. return Variant();
  2028. if (!frame->get_texture().is_valid())
  2029. return Variant();
  2030. RES res = frame->get_texture();
  2031. return EditorNode::get_singleton()->drag_resource(res,p_from);
  2032. return Variant();
  2033. }
  2034. bool ShaderGraphView::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const
  2035. {
  2036. if (p_data.get_type() != Variant::DICTIONARY)
  2037. return false;
  2038. Dictionary d = p_data;
  2039. if (d.has("type")){
  2040. if (d["type"] == "resource" && d.has("resource")) {
  2041. Variant val = d["resource"];
  2042. if (val.get_type()==Variant::OBJECT) {
  2043. RES res = val;
  2044. if (res.is_valid() && Object::cast_to<Texture>(res))
  2045. return true;
  2046. }
  2047. }
  2048. else if (d["type"] == "files" && d.has("files")) {
  2049. Vector<String> files = d["files"];
  2050. if (files.size() != 1)
  2051. return false;
  2052. return (ResourceLoader::get_resource_type(files[0]) == "ImageTexture");
  2053. }
  2054. }
  2055. return false;
  2056. }
  2057. void ShaderGraphView::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from)
  2058. {
  2059. if (!can_drop_data_fw(p_point, p_data, p_from))
  2060. return;
  2061. TextureRect *frame = Object::cast_to<TextureRect>(p_from);
  2062. if (!frame)
  2063. return;
  2064. Dictionary d = p_data;
  2065. Ref<Texture> tex;
  2066. if (d.has("type")) {
  2067. if (d["type"] == "resource" && d.has("resource")){
  2068. Variant val = d["resource"];
  2069. if (val.get_type()==Variant::OBJECT) {
  2070. RES res = val;
  2071. if (res.is_valid())
  2072. tex = Ref<Texture>(Object::cast_to<Texture>(*res));
  2073. }
  2074. }
  2075. else if (d["type"] == "files" && d.has("files")) {
  2076. Vector<String> files = d["files"];
  2077. RES res = ResourceLoader::load(files[0]);
  2078. if (res.is_valid())
  2079. tex = Ref<Texture>(Object::cast_to<Texture>(*res));
  2080. }
  2081. }
  2082. if (!tex.is_valid()) return;
  2083. GraphNode *gn = Object::cast_to<GraphNode>(frame->get_parent());
  2084. if (!gn) return;
  2085. int id = -1;
  2086. for(Map<int,GraphNode*>::Element *E = node_map.front();E;E=E->next())
  2087. if (E->get() == gn) {
  2088. id = E->key();
  2089. break;
  2090. }
  2091. print_line(String::num(double(id)));
  2092. if (id < 0) return;
  2093. if (graph->node_get_type(type,id)==ShaderGraph::NODE_TEXTURE_INPUT) {
  2094. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  2095. ur->create_action(TTR("Change Texture Uniform"));
  2096. ur->add_do_method(graph.ptr(),"texture_input_node_set_value",type,id,tex);
  2097. ur->add_undo_method(graph.ptr(),"texture_input_node_set_value",type,id,graph->texture_input_node_get_value(type,id));
  2098. ur->add_do_method(this,"_update_graph");
  2099. ur->add_undo_method(this,"_update_graph");
  2100. ur->commit_action();
  2101. }
  2102. }
  2103. void ShaderGraphView::set_graph(Ref<ShaderGraph> p_graph){
  2104. if (graph.is_valid()) {
  2105. graph->disconnect("updated",this,"_sg_updated");
  2106. }
  2107. graph=p_graph;
  2108. if (graph.is_valid()) {
  2109. graph->connect("updated",this,"_sg_updated");
  2110. }
  2111. _update_graph();
  2112. _sg_updated();
  2113. }
  2114. void ShaderGraphView::_notification(int p_what) {
  2115. if (p_what==NOTIFICATION_ENTER_TREE) {
  2116. ped_popup->connect("variant_changed",this,"_variant_edited");
  2117. }
  2118. }
  2119. void ShaderGraphView::add_node(int p_type, const Vector2 &location) {
  2120. if (p_type==ShaderGraph::NODE_INPUT && graph->node_count(type, p_type)>0)
  2121. return;
  2122. List<int> existing;
  2123. graph->get_node_list(type,&existing);
  2124. existing.sort();
  2125. int newid=1;
  2126. for(List<int>::Element *E=existing.front();E;E=E->next()) {
  2127. if (!E->next() || (E->get()+1!=E->next()->get())){
  2128. newid=E->get()+1;
  2129. break;
  2130. }
  2131. }
  2132. Vector2 init_ofs = location;
  2133. while(true) {
  2134. bool valid=true;
  2135. for(List<int>::Element *E=existing.front();E;E=E->next()) {
  2136. Vector2 pos = graph->node_get_position(type,E->get());
  2137. if (init_ofs==pos) {
  2138. init_ofs+=Vector2(20,20);
  2139. valid=false;
  2140. break;
  2141. }
  2142. }
  2143. if (valid)
  2144. break;
  2145. }
  2146. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  2147. ur->create_action(TTR("Add Shader Graph Node"));
  2148. ur->add_do_method(graph.ptr(),"node_add",type,p_type,newid);
  2149. ur->add_do_method(graph.ptr(),"node_set_position",type,newid,init_ofs);
  2150. ur->add_undo_method(graph.ptr(),"node_remove",type,newid);
  2151. ur->add_do_method(this,"_update_graph");
  2152. ur->add_undo_method(this,"_update_graph");
  2153. ur->commit_action();
  2154. }
  2155. void ShaderGraphView::_bind_methods() {
  2156. ClassDB::bind_method("_update_graph",&ShaderGraphView::_update_graph);
  2157. ClassDB::bind_method("_begin_node_move", &ShaderGraphView::_begin_node_move);
  2158. ClassDB::bind_method("_node_moved",&ShaderGraphView::_node_moved);
  2159. ClassDB::bind_method("_end_node_move", &ShaderGraphView::_end_node_move);
  2160. ClassDB::bind_method("_move_node",&ShaderGraphView::_move_node);
  2161. ClassDB::bind_method("_node_removed",&ShaderGraphView::_node_removed);
  2162. ClassDB::bind_method("_connection_request",&ShaderGraphView::_connection_request);
  2163. ClassDB::bind_method("_disconnection_request",&ShaderGraphView::_disconnection_request);
  2164. ClassDB::bind_method("_duplicate_nodes_request", &ShaderGraphView::_duplicate_nodes_request);
  2165. ClassDB::bind_method("_duplicate_nodes", &ShaderGraphView::_duplicate_nodes);
  2166. ClassDB::bind_method("_delete_nodes_request", &ShaderGraphView::_delete_nodes_request);
  2167. ClassDB::bind_method("_default_changed",&ShaderGraphView::_default_changed);
  2168. ClassDB::bind_method("_scalar_const_changed",&ShaderGraphView::_scalar_const_changed);
  2169. ClassDB::bind_method("_vec_const_changed",&ShaderGraphView::_vec_const_changed);
  2170. ClassDB::bind_method("_rgb_const_changed",&ShaderGraphView::_rgb_const_changed);
  2171. ClassDB::bind_method("_xform_const_changed",&ShaderGraphView::_xform_const_changed);
  2172. ClassDB::bind_method("_scalar_op_changed",&ShaderGraphView::_scalar_op_changed);
  2173. ClassDB::bind_method("_vec_op_changed",&ShaderGraphView::_vec_op_changed);
  2174. ClassDB::bind_method("_vec_scalar_op_changed",&ShaderGraphView::_vec_scalar_op_changed);
  2175. ClassDB::bind_method("_rgb_op_changed",&ShaderGraphView::_rgb_op_changed);
  2176. ClassDB::bind_method("_xform_inv_rev_changed",&ShaderGraphView::_xform_inv_rev_changed);
  2177. ClassDB::bind_method("_scalar_func_changed",&ShaderGraphView::_scalar_func_changed);
  2178. ClassDB::bind_method("_vec_func_changed",&ShaderGraphView::_vec_func_changed);
  2179. ClassDB::bind_method("_scalar_input_changed",&ShaderGraphView::_scalar_input_changed);
  2180. ClassDB::bind_method("_vec_input_changed",&ShaderGraphView::_vec_input_changed);
  2181. ClassDB::bind_method("_xform_input_changed",&ShaderGraphView::_xform_input_changed);
  2182. ClassDB::bind_method("_rgb_input_changed",&ShaderGraphView::_rgb_input_changed);
  2183. ClassDB::bind_method("_tex_input_change",&ShaderGraphView::_tex_input_change);
  2184. ClassDB::bind_method("_cube_input_change",&ShaderGraphView::_cube_input_change);
  2185. ClassDB::bind_method("_input_name_changed",&ShaderGraphView::_input_name_changed);
  2186. ClassDB::bind_method("_tex_edited",&ShaderGraphView::_tex_edited);
  2187. ClassDB::bind_method("_variant_edited",&ShaderGraphView::_variant_edited);
  2188. ClassDB::bind_method("_cube_edited",&ShaderGraphView::_cube_edited);
  2189. ClassDB::bind_method("_comment_edited",&ShaderGraphView::_comment_edited);
  2190. ClassDB::bind_method("_color_ramp_changed",&ShaderGraphView::_color_ramp_changed);
  2191. ClassDB::bind_method("_curve_changed",&ShaderGraphView::_curve_changed);
  2192. ClassDB::bind_method(D_METHOD("get_drag_data_fw"), &ShaderGraphView::get_drag_data_fw);
  2193. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &ShaderGraphView::can_drop_data_fw);
  2194. ClassDB::bind_method(D_METHOD("drop_data_fw"), &ShaderGraphView::drop_data_fw);
  2195. ClassDB::bind_method("_sg_updated",&ShaderGraphView::_sg_updated);
  2196. }
  2197. ShaderGraphView::ShaderGraphView(ShaderGraph::ShaderType p_type) {
  2198. type=p_type;
  2199. graph_edit = memnew( GraphEdit );
  2200. block_update=false;
  2201. ped_popup = memnew( CustomPropertyEditor );
  2202. graph_edit->add_child(ped_popup);
  2203. status = memnew( Label );
  2204. graph_edit->get_top_layer()->add_child(status);
  2205. graph_edit->connect("_begin_node_move", this, "_begin_node_move");
  2206. graph_edit->connect("_end_node_move", this, "_end_node_move");
  2207. status->set_position(Vector2(5,5));
  2208. status->add_color_override("font_color_shadow",Color(0,0,0));
  2209. status->add_color_override("font_color",Color(1,0.4,0.3));
  2210. status->add_constant_override("shadow_as_outline",1);
  2211. status->add_constant_override("shadow_offset_x",2);
  2212. status->add_constant_override("shadow_offset_y",2);
  2213. status->set_text("");
  2214. }
  2215. //////////////edit//////////////
  2216. void ShaderGraphEditor::edit(Ref<ShaderGraph> p_shader) {
  2217. for(int i=0;i<ShaderGraph::SHADER_TYPE_MAX;i++) {
  2218. graph_edits[i]->set_graph(p_shader);
  2219. }
  2220. }
  2221. void ShaderGraphEditor::_add_node(int p_type) {
  2222. ShaderGraph::ShaderType shader_type=ShaderGraph::ShaderType(tabs->get_current_tab());
  2223. graph_edits[shader_type]->add_node(p_type, next_location);
  2224. }
  2225. void ShaderGraphEditor::_popup_requested(const Vector2 &p_position)
  2226. {
  2227. Vector2 scroll_ofs=graph_edits[tabs->get_current_tab()]->get_graph_edit()->get_scroll_ofs();
  2228. next_location = get_local_mouse_position() + scroll_ofs;
  2229. popup->set_global_position(p_position);
  2230. popup->set_size( Size2( 200, 0) );
  2231. popup->popup();
  2232. popup->call_deferred("grab_click_focus");
  2233. popup->set_invalidate_click_until_motion();
  2234. }
  2235. void ShaderGraphEditor::_notification(int p_what) {
  2236. if (p_what==NOTIFICATION_ENTER_TREE) {
  2237. for(int i=0;i<ShaderGraph::NODE_TYPE_MAX;i++) {
  2238. if (i==ShaderGraph::NODE_OUTPUT)
  2239. continue;
  2240. if (!_2d && i==ShaderGraph::NODE_DEFAULT_TEXTURE)
  2241. continue;
  2242. String nn = node_names[i];
  2243. String ic = nn.get_slice(":",0);
  2244. String v = nn.get_slice(":",1);
  2245. bool addsep=false;
  2246. if (nn.ends_with(":")) {
  2247. addsep=true;
  2248. }
  2249. popup->add_icon_item(get_icon(ic,"EditorIcons"),v,i);
  2250. if (addsep)
  2251. popup->add_separator();
  2252. }
  2253. popup->connect("id_pressed",this,"_add_node");
  2254. }
  2255. }
  2256. void ShaderGraphEditor::_bind_methods() {
  2257. ClassDB::bind_method("_add_node",&ShaderGraphEditor::_add_node);
  2258. ClassDB::bind_method("_popup_requested",&ShaderGraphEditor::_popup_requested);
  2259. }
  2260. const char* ShaderGraphEditor::node_names[ShaderGraph::NODE_TYPE_MAX]={
  2261. ("GraphInput:Input"), // all inputs (shader type dependent)
  2262. ("GraphScalar:Scalar Constant"), //scalar constant
  2263. ("GraphVector:Vector Constant"), //vec3 constant
  2264. ("GraphRgb:RGB Constant"), //rgb constant (shows a color picker instead)
  2265. ("GraphXform:XForm Constant"), // 4x4 matrix constant
  2266. ("GraphTime:Time:"), // time in seconds
  2267. ("GraphTexscreen:Screen Sample"), // screen texture sampler (takes uv) (only usable in fragment shader)
  2268. ("GraphScalarOp:Scalar Operator"), // scalar vs scalar op (mul", add", div", etc)
  2269. ("GraphVecOp:Vector Operator"), // vec3 vs vec3 op (mul",ad",div",crossprod",etc)
  2270. ("GraphVecScalarOp:Scalar+Vector Operator"), // vec3 vs scalar op (mul", add", div", etc)
  2271. ("GraphRgbOp:RGB Operator:"), // vec3 vs vec3 rgb op (with scalar amount)", like brighten", darken", burn", dodge", multiply", etc.
  2272. ("GraphXformMult:XForm Multiply"), // mat4 x mat4
  2273. ("GraphXformVecMult:XForm+Vector Multiply"), // mat4 x vec3 mult (with no-translation option)
  2274. ("GraphXformVecImult:Form+Vector InvMultiply:"), // mat4 x vec3 inverse mult (with no-translation option)
  2275. ("GraphXformScalarFunc:Scalar Function"), // scalar function (sin", cos", etc)
  2276. ("GraphXformVecFunc:Vector Function"), // vector function (normalize", negate", reciprocal", rgb2hsv", hsv2rgb", etc", etc)
  2277. ("GraphVecLength:Vector Length"), // vec3 length
  2278. ("GraphVecDp:Dot Product:"), // vec3 . vec3 (dot product -> scalar output)
  2279. ("GraphVecToScalars:Vector -> Scalars"), // 1 vec3 input", 3 scalar outputs
  2280. ("GraphScalarsToVec:Scalars -> Vector"), // 3 scalar input", 1 vec3 output
  2281. ("GraphXformToVecs:XForm -> Vectors"), // 3 vec input", 1 xform output
  2282. ("GraphVecsToXform:Vectors -> XForm:"), // 3 vec input", 1 xform output
  2283. ("GraphScalarInterp:Scalar Interpolate"), // scalar interpolation (with optional curve)
  2284. ("GraphVecInterp:Vector Interpolate:"), // vec3 interpolation (with optional curve)
  2285. ("GraphColorRamp:Color Ramp"), // vec3 interpolation (with optional curve)
  2286. ("GraphCurveMap:Curve Remap:"), // vec3 interpolation (with optional curve)
  2287. ("GraphScalarUniform:Scalar Uniform"), // scalar uniform (assignable in material)
  2288. ("GraphVectorUniform:Vector Uniform"), // vec3 uniform (assignable in material)
  2289. ("GraphRgbUniform:RGB Uniform"), // color uniform (assignable in material)
  2290. ("GraphXformUniform:XForm Uniform"), // mat4 uniform (assignable in material)
  2291. ("GraphTextureUniform:Texture Uniform"), // texture input (assignable in material)
  2292. ("GraphCubeUniform:CubeMap Uniform:"), // cubemap input (assignable in material)
  2293. ("GraphDefaultTexture:CanvasItem Texture:"), // cubemap input (assignable in material)
  2294. ("Output"), // output (shader type dependent)
  2295. ("GraphComment:Comment"), // comment
  2296. };
  2297. ShaderGraphEditor::ShaderGraphEditor(bool p_2d) {
  2298. _2d=p_2d;
  2299. popup = memnew( PopupMenu );
  2300. add_child(popup);
  2301. tabs = memnew(TabContainer);
  2302. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  2303. add_child(tabs);
  2304. const char* sname[ShaderGraph::SHADER_TYPE_MAX]={
  2305. "Vertex",
  2306. "Fragment",
  2307. "Light"
  2308. };
  2309. for(int i=0;i<ShaderGraph::SHADER_TYPE_MAX;i++) {
  2310. graph_edits[i]= memnew( ShaderGraphView(ShaderGraph::ShaderType(i)) );
  2311. add_child(graph_edits[i]);
  2312. graph_edits[i]->get_graph_edit()->set_name(sname[i]);
  2313. tabs->add_child(graph_edits[i]->get_graph_edit());
  2314. graph_edits[i]->get_graph_edit()->connect("connection_request",graph_edits[i],"_connection_request");
  2315. graph_edits[i]->get_graph_edit()->connect("disconnection_request",graph_edits[i],"_disconnection_request");
  2316. graph_edits[i]->get_graph_edit()->connect("duplicate_nodes_request", graph_edits[i], "_duplicate_nodes_request");
  2317. graph_edits[i]->get_graph_edit()->connect("popup_request",this,"_popup_requested");
  2318. graph_edits[i]->get_graph_edit()->connect("delete_nodes_request",graph_edits[i],"_delete_nodes_request");
  2319. graph_edits[i]->get_graph_edit()->set_right_disconnects(true);
  2320. }
  2321. tabs->set_current_tab(1);
  2322. set_custom_minimum_size(Size2(100,300));
  2323. }
  2324. void ShaderGraphEditorPlugin::edit(Object *p_object) {
  2325. shader_editor->edit(Object::cast_to<ShaderGraph>(p_object));
  2326. }
  2327. bool ShaderGraphEditorPlugin::handles(Object *p_object) const {
  2328. ShaderGraph *shader=Object::cast_to<ShaderGraph>(p_object);
  2329. if (!shader)
  2330. return false;
  2331. if (_2d)
  2332. return shader->get_mode()==Shader::MODE_CANVAS_ITEM;
  2333. else
  2334. return shader->get_mode()==Shader::MODE_MATERIAL;
  2335. }
  2336. void ShaderGraphEditorPlugin::make_visible(bool p_visible) {
  2337. if (p_visible) {
  2338. shader_editor->show();
  2339. } else {
  2340. shader_editor->hide();
  2341. }
  2342. }
  2343. ShaderGraphEditorPlugin::ShaderGraphEditorPlugin(EditorNode *p_node, bool p_2d) {
  2344. _2d=p_2d;
  2345. editor=p_node;
  2346. shader_editor = memnew( ShaderGraphEditor(p_2d) );
  2347. shader_editor->hide();
  2348. if (p_2d)
  2349. CanvasItemEditor::get_singleton()->get_bottom_split()->add_child(shader_editor);
  2350. else
  2351. SpatialEditor::get_singleton()->get_shader_split()->add_child(shader_editor);
  2352. //editor->get_viewport()->add_child(shader_editor);
  2353. //shader_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2354. //shader_editor->hide();
  2355. }
  2356. ShaderGraphEditorPlugin::~ShaderGraphEditorPlugin()
  2357. {
  2358. }
  2359. #endif