shader_graph_editor_plugin.cpp 93 KB

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